blob: e7f28c5cd3df89ca07a9fdf68ed4b3391debf41d [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;
Craig Mautner15df08a2015-04-01 12:17:18 -070020import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
21import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
22import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070023import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
24import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070025import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
26import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070027import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070028import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080029import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070030import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070031import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
32import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
33import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070034import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070035import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
36import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010037import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070038import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
39import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070040
Svetoslav7008b512015-06-24 18:47:07 -070041import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070042import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080044import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070045import android.app.ActivityOptions;
46import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070047import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080048import android.app.IActivityContainer;
49import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070050import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070051import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070052import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070053import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070054import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070055import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070056import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040057import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040058import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070059import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070060import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070062import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070063import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070064import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070066import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070067import android.content.pm.PackageManager;
68import android.content.pm.ResolveInfo;
69import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080070import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080071import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080072import android.hardware.display.DisplayManager;
73import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080074import android.hardware.display.DisplayManagerGlobal;
75import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080076import android.hardware.input.InputManager;
77import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080078import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070079import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070080import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070081import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070082import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070083import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070084import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070085import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070086import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070087import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070088import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070089import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040090import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070091import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070092import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070093import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070094import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070095import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040096import android.provider.Settings;
97import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070098import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070099import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700100import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700101import android.util.EventLog;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700102import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700103import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800104import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700105
Craig Mautner4a1cb222013-12-04 16:14:06 -0800106import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800107import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800108import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800109import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800110import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700111import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700112import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800113import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700114import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400115import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700116import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400117import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800118import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700119import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700120import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121
justinzhang5286d3f2014-05-12 17:06:01 -0400122
Craig Mautner8d341ef2013-03-26 09:03:27 -0700123import java.io.FileDescriptor;
124import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700125import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700126import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700127import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700128import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700129import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700130
Craig Mautner4a1cb222013-12-04 16:14:06 -0800131public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800132 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700133 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700134 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700135 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700136 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700137 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700138 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700139 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700140 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
141 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700142 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700143 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700144 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
145 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700146 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700147 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800148
Craig Mautner2219a1b2013-03-25 09:44:30 -0700149 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700150
Craig Mautnerf3333272013-04-22 10:55:53 -0700151 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700152 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700153
Craig Mautner0eea92c2013-05-16 13:35:39 -0700154 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700155 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700156
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700157 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700158 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700159
Craig Mautner05d29032013-05-03 13:40:13 -0700160 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
161 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
162 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700163 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700164 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800165 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
166 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
167 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700168 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400169 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
170 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700171 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700172 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700173 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800174
Craig Mautner4504de52013-12-20 09:06:56 -0800175 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700176
Jason Monk62515be2014-05-21 16:06:19 -0400177 private static final String LOCK_TASK_TAG = "Lock-to-App";
178
Svetoslav7008b512015-06-24 18:47:07 -0700179 // Activity actions an app cannot start if it uses a permission which is not granted.
180 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
181 new ArrayMap<>();
182 static {
183 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
184 Manifest.permission.CAMERA);
185 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
186 Manifest.permission.CAMERA);
187 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
188 Manifest.permission.CALL_PHONE);
189 }
190
Svet Ganov99b60432015-06-27 13:15:22 -0700191 /** Action restriction: launching the activity is not restricted. */
192 private static final int ACTIVITY_RESTRICTION_NONE = 0;
193 /** Action restriction: launching the activity is restricted by a permission. */
194 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
195 /** Action restriction: launching the activity is restricted by an app op. */
196 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700197
justinzhang5286d3f2014-05-12 17:06:01 -0400198 /** Status Bar Service **/
199 private IBinder mToken = new Binder();
200 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400201 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400202
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700203 // For debugging to make sure the caller when acquiring/releasing our
204 // wake lock is the system process.
205 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700206
Craig Mautner27084302013-03-25 08:05:25 -0700207 final ActivityManagerService mService;
208
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800209 private final RecentTasks mRecentTasks;
210
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700211 final ActivityStackSupervisorHandler mHandler;
212
213 /** Short cut */
214 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800215 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700216
Craig Mautner8d341ef2013-03-26 09:03:27 -0700217 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700218 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700219
220 /** Task identifier that activities are currently being started in. Incremented each time a
221 * new task is created. */
222 private int mCurTaskId = 0;
223
Craig Mautner2420ead2013-04-01 17:13:20 -0700224 /** The current user */
225 private int mCurrentUser;
226
Craig Mautnere0a38842013-12-16 16:14:02 -0800227 /** The stack containing the launcher app. Assumed to always be attached to
228 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700229 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700230
Craig Mautnere0a38842013-12-16 16:14:02 -0800231 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700232 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700233
Craig Mautner4a1cb222013-12-04 16:14:06 -0800234 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
235 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800236 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800237 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700238
239 /** List of activities that are waiting for a new activity to become visible before completing
240 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800241 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700242
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700243 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800244 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700245
246 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800247 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700248
Craig Mautnerde4ef022013-04-07 19:01:33 -0700249 /** List of activities that are ready to be stopped, but waiting for the next activity to
250 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800251 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700252
Craig Mautnerf3333272013-04-22 10:55:53 -0700253 /** List of activities that are ready to be finished, but waiting for the previous activity to
254 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800255 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700256
Craig Mautner0eea92c2013-05-16 13:35:39 -0700257 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800258 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700259
Craig Mautnerf3333272013-04-22 10:55:53 -0700260 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700261 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700262
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700263 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700264 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700265
Craig Mautnerde4ef022013-04-07 19:01:33 -0700266 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
267 * is being brought in front of us. */
268 boolean mUserLeaving = false;
269
Craig Mautner0eea92c2013-05-16 13:35:39 -0700270 /** Set when we have taken too long waiting to go to sleep. */
271 boolean mSleepTimeout = false;
272
Jose Lima58e66d62014-05-27 20:00:27 -0700273 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
274 * setWindowManager is called. **/
275 private boolean mLeanbackOnlyDevice;
276
Craig Mautner0eea92c2013-05-16 13:35:39 -0700277 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700278 * We don't want to allow the device to go to sleep while in the process
279 * of launching an activity. This is primarily to allow alarm intent
280 * receivers to launch an activity and get that to run before the device
281 * goes back to sleep.
282 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700283 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700284
285 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700286 * Set when the system is going to sleep, until we have
287 * successfully paused the current activity and released our wake lock.
288 * At that point the system is allowed to actually sleep.
289 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700290 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700291
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700292 /** Stack id of the front stack when user switched, indexed by userId. */
293 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700294
Craig Mautner4504de52013-12-20 09:06:56 -0800295 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800296 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700297 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800298
299 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700300 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800301
Jeff Brownca9bc702014-02-11 14:32:56 -0800302 InputManagerInternal mInputManagerInternal;
303
Craig Mautner15df08a2015-04-01 12:17:18 -0700304 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
305 * may be finished until there is only one entry left. If this is empty the system is not
306 * in lockTask mode. */
307 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000308 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700309 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
310 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000311 */
312 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400313 /**
314 * Notifies the user when entering/exiting lock-task.
315 */
316 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800317
Craig Mautner15df08a2015-04-01 12:17:18 -0700318 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700319
Craig Mautner42d04db2014-11-06 12:13:23 -0800320 /** Used to keep resumeTopActivityLocked() from being entered recursively */
321 boolean inResumeTopActivity;
322
Craig Mautneree36c772014-07-16 14:56:05 -0700323 /**
324 * Description of a request to start a new activity, which has been held
325 * due to app switches being disabled.
326 */
327 static class PendingActivityLaunch {
328 final ActivityRecord r;
329 final ActivityRecord sourceRecord;
330 final int startFlags;
331 final ActivityStack stack;
332
333 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
334 int _startFlags, ActivityStack _stack) {
335 r = _r;
336 sourceRecord = _sourceRecord;
337 startFlags = _startFlags;
338 stack = _stack;
339 }
340 }
341
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800342 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700343 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800344 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700345 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
346 }
347
348 /**
349 * At the time when the constructor runs, the power manager has not yet been
350 * initialized. So we initialize our wakelocks afterwards.
351 */
352 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800353 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700354 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700355 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700356 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700357 }
358
justinzhang5286d3f2014-05-12 17:06:01 -0400359 // This function returns a IStatusBarService. The value is from ServiceManager.
360 // getService and is cached.
361 private IStatusBarService getStatusBarService() {
362 synchronized (mService) {
363 if (mStatusBarService == null) {
364 mStatusBarService = IStatusBarService.Stub.asInterface(
365 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
366 if (mStatusBarService == null) {
367 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
368 }
369 }
370 return mStatusBarService;
371 }
372 }
373
Jason Monk35c62a42014-06-17 10:24:47 -0400374 private IDevicePolicyManager getDevicePolicyManager() {
375 synchronized (mService) {
376 if (mDevicePolicyManager == null) {
377 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
378 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
379 if (mDevicePolicyManager == null) {
380 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
381 }
382 }
383 return mDevicePolicyManager;
384 }
385 }
386
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700387 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800388 synchronized (mService) {
389 mWindowManager = wm;
390
391 mDisplayManager =
392 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
393 mDisplayManager.registerDisplayListener(this, null);
394
395 Display[] displays = mDisplayManager.getDisplays();
396 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
397 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800398 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700399 if (activityDisplay.mDisplay == null) {
400 throw new IllegalStateException("Default Display does not exist");
401 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800402 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800403 }
404
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700405 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800406 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800407
408 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700409
410 // Initialize this here, now that we can get a valid reference to PackageManager.
411 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800412 }
Craig Mautner27084302013-03-25 08:05:25 -0700413 }
414
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200415 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700416 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200417 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700418 }
419
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700420 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800421 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700422 }
423
Craig Mautnerde4ef022013-04-07 19:01:33 -0700424 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800425 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700426 }
427
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800428 /** Top of all visible stacks is/should always be equal to the focused stack.
429 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
430 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700431 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700432 if (stack == null) {
433 return false;
434 }
435
Craig Mautnerdf88d732014-01-27 09:21:32 -0800436 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
437 if (parent != null) {
438 stack = parent.task.stack;
439 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800440 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700441 }
442
Craig Mautner299f9602015-01-26 09:47:33 -0800443 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800444 moveHomeStack(toFront, reason, null);
445 }
446
447 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800448 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800449 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800450 if (topNdx <= 0) {
451 return;
452 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700453
454 // The home stack should either be at the top or bottom of the stack list.
455 if ((toFront && (stacks.get(topNdx) != mHomeStack))
456 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700457 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700458 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
459 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800460 stacks.remove(mHomeStack);
461 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700462 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800463
464 if (lastFocusedStack != null) {
465 mLastFocusedStack = lastFocusedStack;
466 }
467 mFocusedStack = stacks.get(topNdx);
468
Craig Mautnerde313752015-01-22 14:28:03 -0800469 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
470 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800471 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800472
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800473 if (mService.mBooting || !mService.mBooted) {
474 final ActivityRecord r = topRunningActivityLocked();
475 if (r != null && r.idle) {
476 checkFinishBootingLocked();
477 }
478 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700479 }
480
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700481 /** Returns true if the focus activity was adjusted to the home stack top activity. */
482 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700483 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
484 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700485 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700486 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700487
Craig Mautner84984fa2014-06-19 11:19:20 -0700488 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700489
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700490 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700491 if (top == null) {
492 return false;
493 }
494 mService.setFocusedActivityLocked(top, reason);
495 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700496 }
497
Craig Mautner299f9602015-01-26 09:47:33 -0800498 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700499 if (!mService.mBooting && !mService.mBooted) {
500 // Not ready yet!
501 return false;
502 }
503
Craig Mautner84984fa2014-06-19 11:19:20 -0700504 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
505 mWindowManager.showRecentApps();
506 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700507 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700508
Craig Mautner84984fa2014-06-19 11:19:20 -0700509 if (prev != null) {
510 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
511 }
512
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700513 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
514 ActivityRecord r = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700515 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800516 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700517 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700518 }
Craig Mautner299f9602015-01-26 09:47:33 -0800519 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700520 }
521
Craig Mautner8d341ef2013-03-26 09:03:27 -0700522 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700523 return anyTaskForIdLocked(id, true);
524 }
525
526 /**
527 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
528 * @param id Id of the task we would like returned.
529 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
530 * restore the task from recents to the active list.
531 */
532 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800533 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800534 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800535 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800536 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
537 ActivityStack stack = stacks.get(stackNdx);
538 TaskRecord task = stack.taskForIdLocked(id);
539 if (task != null) {
540 return task;
541 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700542 }
543 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800544
545 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700546 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800547 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800548 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700549 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800550 return null;
551 }
552
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700553 if (!restoreFromRecents) {
554 return task;
555 }
556
Wale Ogunwale7de05352014-12-12 15:21:33 -0800557 if (!restoreRecentTaskLocked(task)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700558 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
559 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800560 return null;
561 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700562 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800563 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700564 }
565
Craig Mautner6170f732013-04-02 13:05:23 -0700566 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800567 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800568 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800569 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800570 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
571 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
572 if (r != null) {
573 return r;
574 }
Craig Mautner6170f732013-04-02 13:05:23 -0700575 }
576 }
577 return null;
578 }
579
Craig Mautneref73ee12014-04-23 11:45:37 -0700580 void setNextTaskId(int taskId) {
581 if (taskId > mCurTaskId) {
582 mCurTaskId = taskId;
583 }
584 }
585
Craig Mautner8d341ef2013-03-26 09:03:27 -0700586 int getNextTaskId() {
587 do {
588 mCurTaskId++;
589 if (mCurTaskId <= 0) {
590 mCurTaskId = 1;
591 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700592 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700593 return mCurTaskId;
594 }
595
Craig Mautnerde4ef022013-04-07 19:01:33 -0700596 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800597 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700598 if (stack == null) {
599 return null;
600 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700601 ActivityRecord resumedActivity = stack.mResumedActivity;
602 if (resumedActivity == null || resumedActivity.app == null) {
603 resumedActivity = stack.mPausingActivity;
604 if (resumedActivity == null || resumedActivity.app == null) {
605 resumedActivity = stack.topRunningActivityLocked(null);
606 }
607 }
608 return resumedActivity;
609 }
610
Dianne Hackbornff072722014-09-24 10:56:28 -0700611 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700612 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800613 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800614 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
615 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800616 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
617 final ActivityStack stack = stacks.get(stackNdx);
618 if (!isFrontStack(stack)) {
619 continue;
620 }
621 ActivityRecord hr = stack.topRunningActivityLocked(null);
622 if (hr != null) {
623 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
624 && processName.equals(hr.processName)) {
625 try {
George Mount2c92c972014-03-20 09:38:23 -0700626 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800627 didSomething = true;
628 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700629 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800630 Slog.w(TAG, "Exception in new application when starting activity "
631 + hr.intent.getComponent().flattenToShortString(), e);
632 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700633 }
Craig Mautner20e72272013-04-01 13:45:53 -0700634 }
Craig Mautner20e72272013-04-01 13:45:53 -0700635 }
636 }
637 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700638 if (!didSomething) {
639 ensureActivitiesVisibleLocked(null, 0);
640 }
Craig Mautner20e72272013-04-01 13:45:53 -0700641 return didSomething;
642 }
643
644 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800645 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
646 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800647 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
648 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800649 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800650 continue;
651 }
652 final ActivityRecord resumedActivity = stack.mResumedActivity;
653 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700654 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800655 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800656 return false;
657 }
Craig Mautner20e72272013-04-01 13:45:53 -0700658 }
659 }
660 return true;
661 }
662
Craig Mautnerde4ef022013-04-07 19:01:33 -0700663 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800664 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
665 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800666 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
667 final ActivityStack stack = stacks.get(stackNdx);
668 if (isFrontStack(stack)) {
669 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700670 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 return false;
672 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700673 }
674 }
675 }
676 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700677 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800678 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
679 mLastFocusedStack + " to=" + mFocusedStack);
680 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700681 return true;
682 }
683
684 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800685 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800686 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
687 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800688 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
689 final ActivityStack stack = stacks.get(stackNdx);
690 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800691 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700692 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800693 return false;
694 }
695 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800696 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700697 }
698 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800699 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700700 }
701
Craig Mautner2acc3892013-09-23 10:28:14 -0700702 /**
703 * Pause all activities in either all of the stacks or just the back stacks.
704 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700705 * @return true if any activity was paused as a result of this call.
706 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700707 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700708 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800709 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
710 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800711 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
712 final ActivityStack stack = stacks.get(stackNdx);
713 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700714 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800715 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700716 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
717 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800718 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700719 }
720 }
721 return someActivityPaused;
722 }
723
Craig Mautnerde4ef022013-04-07 19:01:33 -0700724 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700725 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800726 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
727 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800728 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
729 final ActivityStack stack = stacks.get(stackNdx);
730 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700731 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800732 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700733 Slog.d(TAG_STATES,
734 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800735 pausing = false;
736 } else {
737 return false;
738 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700739 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700740 }
741 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700742 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700743 }
744
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700745 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
746 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500747 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800748 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
749 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
750 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
751 final ActivityStack stack = stacks.get(stackNdx);
752 if (stack.mResumedActivity != null &&
753 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700754 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800755 }
756 }
757 }
758 }
759
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700760 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700761 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700762 }
763
764 void sendWaitingVisibleReportLocked(ActivityRecord r) {
765 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700766 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700767 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700768 if (w.who == null) {
769 changed = true;
770 w.timeout = false;
771 if (r != null) {
772 w.who = new ComponentName(r.info.packageName, r.info.name);
773 }
774 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
775 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700776 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700777 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700778 if (changed) {
779 mService.notifyAll();
780 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700781 }
782
783 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
784 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700785 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700786 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700787 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700788 if (w.who == null) {
789 changed = true;
790 w.timeout = timeout;
791 if (r != null) {
792 w.who = new ComponentName(r.info.packageName, r.info.name);
793 }
794 w.thisTime = thisTime;
795 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700796 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700797 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700798 if (changed) {
799 mService.notifyAll();
800 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700801 }
802
Craig Mautner29219d92013-04-16 20:19:12 -0700803 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800804 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700805 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
806 if (r != null) {
807 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700808 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700809
Craig Mautner4a1cb222013-12-04 16:14:06 -0800810 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800811 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800812 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
813 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700814 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700815 r = stack.topRunningActivityLocked(null);
816 if (r != null) {
817 return r;
818 }
819 }
820 }
821 return null;
822 }
823
Dianne Hackborn09233282014-04-30 11:33:59 -0700824 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700825 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800826 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
827 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800828 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800829 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800830 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800831 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
832 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700833 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800834 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700835 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700836 }
837 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700838
839 // The lists are already sorted from most recent to oldest. Just pull the most recent off
840 // each list and add it to list. Stop when all lists are empty or maxNum reached.
841 while (maxNum > 0) {
842 long mostRecentActiveTime = Long.MIN_VALUE;
843 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800844 final int numTaskLists = runningTaskLists.size();
845 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
846 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700847 if (!stackTaskList.isEmpty()) {
848 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
849 if (lastActiveTime > mostRecentActiveTime) {
850 mostRecentActiveTime = lastActiveTime;
851 selectedStackList = stackTaskList;
852 }
853 }
854 }
855 if (selectedStackList != null) {
856 list.add(selectedStackList.remove(0));
857 --maxNum;
858 } else {
859 break;
860 }
861 }
Craig Mautner20e72272013-04-01 13:45:53 -0700862 }
863
Craig Mautner23ac33b2013-04-01 16:26:35 -0700864 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700865 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700866 // Collect information about the target of the Intent.
867 ActivityInfo aInfo;
868 try {
869 ResolveInfo rInfo =
870 AppGlobals.getPackageManager().resolveIntent(
871 intent, resolvedType,
872 PackageManager.MATCH_DEFAULT_ONLY
873 | ActivityManagerService.STOCK_PM_FLAGS, userId);
874 aInfo = rInfo != null ? rInfo.activityInfo : null;
875 } catch (RemoteException e) {
876 aInfo = null;
877 }
878
879 if (aInfo != null) {
880 // Store the found target back into the intent, because now that
881 // we have it we never want to do this again. For example, if the
882 // user navigates back to this point in the history, we should
883 // always restart the exact same activity.
884 intent.setComponent(new ComponentName(
885 aInfo.applicationInfo.packageName, aInfo.name));
886
887 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700888 if (!aInfo.processName.equals("system")) {
889 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700890 mService.setDebugApp(aInfo.processName, true, false);
891 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700892
Man Caocfa78b22015-06-11 20:14:34 -0700893 if ((startFlags & ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700894 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
895 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700896
Man Caocfa78b22015-06-11 20:14:34 -0700897 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
898 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
899 }
900
901 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700902 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700903 }
904 }
905 }
906 return aInfo;
907 }
908
Craig Mautner299f9602015-01-26 09:47:33 -0800909 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
910 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700911 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
912 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
913 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
914 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700915 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
916 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700917 null /* outActivity */, null /* container */, null /* inTask */);
918 if (inResumeTopActivity) {
919 // If we are in resume section already, home activity will be initialized, but not
920 // resumed (to avoid recursive resume) and will stay that way until something pokes it
921 // again. We need to schedule another resume.
922 scheduleResumeTopActivities();
923 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700924 }
925
Craig Mautner23ac33b2013-04-01 16:26:35 -0700926 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700927 String callingPackage, Intent intent, String resolvedType,
928 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700929 IBinder resultTo, String resultWho, int requestCode, int startFlags,
930 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700931 Bundle options, boolean ignoreTargetSecurity, int userId,
932 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700933 // Refuse possible leaked file descriptors
934 if (intent != null && intent.hasFileDescriptors()) {
935 throw new IllegalArgumentException("File descriptors passed in Intent");
936 }
937 boolean componentSpecified = intent.getComponent() != null;
938
939 // Don't modify the client's object!
940 intent = new Intent(intent);
941
942 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700943 ActivityInfo aInfo =
944 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700945
Craig Mautnere0a38842013-12-16 16:14:02 -0800946 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700947 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800948 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700949 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800950 // Cannot start a child activity if the parent is not resumed.
951 return ActivityManager.START_CANCELED;
952 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700953 final int realCallingPid = Binder.getCallingPid();
954 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700955 int callingPid;
956 if (callingUid >= 0) {
957 callingPid = -1;
958 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700959 callingPid = realCallingPid;
960 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700961 } else {
962 callingPid = callingUid = -1;
963 }
964
Craig Mautnere0a38842013-12-16 16:14:02 -0800965 final ActivityStack stack;
966 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800967 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800968 } else {
969 stack = container.mStack;
970 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800971 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700972 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700973 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700974
975 final long origId = Binder.clearCallingIdentity();
976
977 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800978 (aInfo.applicationInfo.privateFlags
979 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700980 // This may be a heavy-weight process! Check to see if we already
981 // have another, different heavy-weight process running.
982 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
983 if (mService.mHeavyWeightProcess != null &&
984 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
985 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700986 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700987 if (caller != null) {
988 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
989 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700990 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700991 } else {
992 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700993 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700994 + intent.toString());
995 ActivityOptions.abort(options);
996 return ActivityManager.START_PERMISSION_DENIED;
997 }
998 }
999
1000 IIntentSender target = mService.getIntentSenderLocked(
1001 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001002 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001003 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1004 | PendingIntent.FLAG_ONE_SHOT, null);
1005
1006 Intent newIntent = new Intent();
1007 if (requestCode >= 0) {
1008 // Caller is requesting a result.
1009 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1010 }
1011 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1012 new IntentSender(target));
1013 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1014 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1015 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1016 hist.packageName);
1017 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1018 hist.task.taskId);
1019 }
1020 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1021 aInfo.packageName);
1022 newIntent.setFlags(intent.getFlags());
1023 newIntent.setClassName("android",
1024 HeavyWeightSwitcherActivity.class.getName());
1025 intent = newIntent;
1026 resolvedType = null;
1027 caller = null;
1028 callingUid = Binder.getCallingUid();
1029 callingPid = Binder.getCallingPid();
1030 componentSpecified = true;
1031 try {
1032 ResolveInfo rInfo =
1033 AppGlobals.getPackageManager().resolveIntent(
1034 intent, null,
1035 PackageManager.MATCH_DEFAULT_ONLY
1036 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1037 aInfo = rInfo != null ? rInfo.activityInfo : null;
1038 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1039 } catch (RemoteException e) {
1040 aInfo = null;
1041 }
1042 }
1043 }
1044 }
1045
Dianne Hackborn91097de2014-04-04 18:02:06 -07001046 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1047 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001048 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001049 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001050 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001051
Craig Mautner85c11a82014-07-17 12:38:18 -07001052 Binder.restoreCallingIdentity(origId);
1053
Craig Mautnerde4ef022013-04-07 19:01:33 -07001054 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001055 // If the caller also wants to switch to a new configuration,
1056 // do so now. This allows a clean switch, as we are waiting
1057 // for the current activity to pause (so we will not destroy
1058 // it), and have not yet started the next activity.
1059 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1060 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001061 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001062 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001063 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001064 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001065 }
1066
Craig Mautner23ac33b2013-04-01 16:26:35 -07001067 if (outResult != null) {
1068 outResult.result = res;
1069 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001070 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001071 do {
1072 try {
1073 mService.wait();
1074 } catch (InterruptedException e) {
1075 }
1076 } while (!outResult.timeout && outResult.who == null);
1077 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001078 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001079 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001080 outResult.timeout = false;
1081 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1082 outResult.totalTime = 0;
1083 outResult.thisTime = 0;
1084 } else {
1085 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001086 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001087 do {
1088 try {
1089 mService.wait();
1090 } catch (InterruptedException e) {
1091 }
1092 } while (!outResult.timeout && outResult.who == null);
1093 }
1094 }
1095 }
1096
1097 return res;
1098 }
1099 }
1100
1101 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1102 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1103 Bundle options, int userId) {
1104 if (intents == null) {
1105 throw new NullPointerException("intents is null");
1106 }
1107 if (resolvedTypes == null) {
1108 throw new NullPointerException("resolvedTypes is null");
1109 }
1110 if (intents.length != resolvedTypes.length) {
1111 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1112 }
1113
Craig Mautner23ac33b2013-04-01 16:26:35 -07001114
1115 int callingPid;
1116 if (callingUid >= 0) {
1117 callingPid = -1;
1118 } else if (caller == null) {
1119 callingPid = Binder.getCallingPid();
1120 callingUid = Binder.getCallingUid();
1121 } else {
1122 callingPid = callingUid = -1;
1123 }
1124 final long origId = Binder.clearCallingIdentity();
1125 try {
1126 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001127 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001128 for (int i=0; i<intents.length; i++) {
1129 Intent intent = intents[i];
1130 if (intent == null) {
1131 continue;
1132 }
1133
1134 // Refuse possible leaked file descriptors
1135 if (intent != null && intent.hasFileDescriptors()) {
1136 throw new IllegalArgumentException("File descriptors passed in Intent");
1137 }
1138
1139 boolean componentSpecified = intent.getComponent() != null;
1140
1141 // Don't modify the client's object!
1142 intent = new Intent(intent);
1143
1144 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001145 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001146 // TODO: New, check if this is correct
1147 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1148
1149 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001150 (aInfo.applicationInfo.privateFlags
1151 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001152 throw new IllegalArgumentException(
1153 "FLAG_CANT_SAVE_STATE not supported here");
1154 }
1155
1156 Bundle theseOptions;
1157 if (options != null && i == intents.length-1) {
1158 theseOptions = options;
1159 } else {
1160 theseOptions = null;
1161 }
Craig Mautner6170f732013-04-02 13:05:23 -07001162 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001163 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1164 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001165 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001166 if (res < 0) {
1167 return res;
1168 }
1169
1170 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1171 }
1172 }
1173 } finally {
1174 Binder.restoreCallingIdentity(origId);
1175 }
1176
1177 return ActivityManager.START_SUCCESS;
1178 }
1179
Craig Mautner2420ead2013-04-01 17:13:20 -07001180 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001181 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001182 throws RemoteException {
1183
Craig Mautner2568c3a2015-03-26 14:22:34 -07001184 if (andResume) {
1185 r.startFreezingScreenLocked(app, 0);
1186 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001187
Craig Mautner2568c3a2015-03-26 14:22:34 -07001188 // schedule launch ticks to collect information about slow apps.
1189 r.startLaunchTickingLocked();
1190 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001191
1192 // Have the window manager re-evaluate the orientation of
1193 // the screen based on the new activity order. Note that
1194 // as a result of this, it can call back into the activity
1195 // manager with a new orientation. We don't care about that,
1196 // because the activity is not currently running so we are
1197 // just restarting it anyway.
1198 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001199 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001200 mService.mConfiguration,
1201 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001202 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001203 }
1204
1205 r.app = app;
1206 app.waitingToKill = null;
1207 r.launchCount++;
1208 r.lastLaunchTime = SystemClock.uptimeMillis();
1209
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001210 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001211
1212 int idx = app.activities.indexOf(r);
1213 if (idx < 0) {
1214 app.activities.add(r);
1215 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001216 mService.updateLruProcessLocked(app, true, null);
1217 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001218
Craig Mautner15df08a2015-04-01 12:17:18 -07001219 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001220 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1221 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001222 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001223 }
1224
1225 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001226 try {
1227 if (app.thread == null) {
1228 throw new RemoteException();
1229 }
1230 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001231 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001232 if (andResume) {
1233 results = r.results;
1234 newIntents = r.newIntents;
1235 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001236 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1237 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1238 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001239 if (andResume) {
1240 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1241 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001242 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001243 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001244 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001245 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001246 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001247 }
1248 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1249 r.sleeping = false;
1250 r.forceNewConfig = false;
1251 mService.showAskCompatModeDialogLocked(r);
1252 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001253 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001254 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1255 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1256 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001257 final String profileFile = mService.mProfileFile;
1258 if (profileFile != null) {
1259 ParcelFileDescriptor profileFd = mService.mProfileFd;
1260 if (profileFd != null) {
1261 try {
1262 profileFd = profileFd.dup();
1263 } catch (IOException e) {
1264 if (profileFd != null) {
1265 try {
1266 profileFd.close();
1267 } catch (IOException o) {
1268 }
1269 profileFd = null;
1270 }
1271 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001272 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001273
1274 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1275 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001276 }
1277 }
1278 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001279
Craig Mautner2568c3a2015-03-26 14:22:34 -07001280 if (andResume) {
1281 app.hasShownUi = true;
1282 app.pendingUiClean = true;
1283 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001284 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001285 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001286 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001287 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001288 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001289 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001290
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001291 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001292 // This may be a heavy-weight process! Note that the package
1293 // manager will ensure that only activity can run in the main
1294 // process of the .apk, which is the only thing that will be
1295 // considered heavy-weight.
1296 if (app.processName.equals(app.info.packageName)) {
1297 if (mService.mHeavyWeightProcess != null
1298 && mService.mHeavyWeightProcess != app) {
1299 Slog.w(TAG, "Starting new heavy weight process " + app
1300 + " when already running "
1301 + mService.mHeavyWeightProcess);
1302 }
1303 mService.mHeavyWeightProcess = app;
1304 Message msg = mService.mHandler.obtainMessage(
1305 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1306 msg.obj = r;
1307 mService.mHandler.sendMessage(msg);
1308 }
1309 }
1310
1311 } catch (RemoteException e) {
1312 if (r.launchFailed) {
1313 // This is the second time we failed -- finish activity
1314 // and give up.
1315 Slog.e(TAG, "Second failure launching "
1316 + r.intent.getComponent().flattenToShortString()
1317 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001318 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001319 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1320 "2nd-crash", false);
1321 return false;
1322 }
1323
1324 // This is the first time we failed -- restart process and
1325 // retry.
1326 app.activities.remove(r);
1327 throw e;
1328 }
1329
1330 r.launchFailed = false;
1331 if (stack.updateLRUListLocked(r)) {
1332 Slog.w(TAG, "Activity " + r
1333 + " being launched, but already in LRU list");
1334 }
1335
1336 if (andResume) {
1337 // As part of the process of launching, ActivityThread also performs
1338 // a resume.
1339 stack.minimalResumeActivityLocked(r);
1340 } else {
1341 // This activity is not starting in the resumed state... which
1342 // should look like we asked it to pause+stop (but remain visible),
1343 // and it has done so and reported back the current icicle and
1344 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001345 if (DEBUG_STATES) Slog.v(TAG_STATES,
1346 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001347 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001348 r.stopped = true;
1349 }
1350
1351 // Launch the new version setup screen if needed. We do this -after-
1352 // launching the initial activity (that is, home), so that it can have
1353 // a chance to initialize itself while in the background, making the
1354 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001355 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001356 mService.startSetupActivityLocked();
1357 }
1358
Dianne Hackborn465fa392014-09-14 14:21:18 -07001359 // Update any services we are bound to that might care about whether
1360 // their client may have activities.
1361 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1362
Craig Mautner2420ead2013-04-01 17:13:20 -07001363 return true;
1364 }
1365
Craig Mautnere79d42682013-04-01 19:01:53 -07001366 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001367 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001368 // Is this activity's application already running?
1369 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001370 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001371
1372 r.task.stack.setLaunchTime(r);
1373
1374 if (app != null && app.thread != null) {
1375 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001376 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1377 || !"android".equals(r.info.packageName)) {
1378 // Don't add this if it is a platform component that is marked
1379 // to run in multiple processes, because this is actually
1380 // part of the framework so doesn't make sense to track as a
1381 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001382 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1383 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001384 }
George Mount2c92c972014-03-20 09:38:23 -07001385 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001386 return;
1387 } catch (RemoteException e) {
1388 Slog.w(TAG, "Exception when starting activity "
1389 + r.intent.getComponent().flattenToShortString(), e);
1390 }
1391
1392 // If a dead object exception was thrown -- fall through to
1393 // restart the application.
1394 }
1395
1396 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001397 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001398 }
1399
Craig Mautner6170f732013-04-02 13:05:23 -07001400 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001401 Intent intent, String resolvedType, ActivityInfo aInfo,
1402 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1403 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001404 int callingPid, int callingUid, String callingPackage,
1405 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001406 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1407 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001408 int err = ActivityManager.START_SUCCESS;
1409
1410 ProcessRecord callerApp = null;
1411 if (caller != null) {
1412 callerApp = mService.getRecordForAppLocked(caller);
1413 if (callerApp != null) {
1414 callingPid = callerApp.pid;
1415 callingUid = callerApp.info.uid;
1416 } else {
1417 Slog.w(TAG, "Unable to find app for caller " + caller
1418 + " (pid=" + callingPid + ") when starting: "
1419 + intent.toString());
1420 err = ActivityManager.START_PERMISSION_DENIED;
1421 }
1422 }
1423
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001424 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1425
Craig Mautner6170f732013-04-02 13:05:23 -07001426 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001427 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001428 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001429 + " on display " + (container == null ? (mFocusedStack == null ?
1430 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1431 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1432 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001433 }
1434
1435 ActivityRecord sourceRecord = null;
1436 ActivityRecord resultRecord = null;
1437 if (resultTo != null) {
1438 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001439 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1440 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001441 if (sourceRecord != null) {
1442 if (requestCode >= 0 && !sourceRecord.finishing) {
1443 resultRecord = sourceRecord;
1444 }
1445 }
1446 }
Craig Mautner6170f732013-04-02 13:05:23 -07001447
Dianne Hackborn91097de2014-04-04 18:02:06 -07001448 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001449
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001450 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001451 // Transfer the result target from the source activity to the new
1452 // one being started, including any failures.
1453 if (requestCode >= 0) {
1454 ActivityOptions.abort(options);
1455 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1456 }
1457 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001458 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1459 resultRecord = null;
1460 }
Craig Mautner6170f732013-04-02 13:05:23 -07001461 resultWho = sourceRecord.resultWho;
1462 requestCode = sourceRecord.requestCode;
1463 sourceRecord.resultTo = null;
1464 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001465 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001466 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001467 if (sourceRecord.launchedFromUid == callingUid) {
1468 // The new activity is being launched from the same uid as the previous
1469 // activity in the flow, and asking to forward its result back to the
1470 // previous. In this case the activity is serving as a trampoline between
1471 // the two, so we also want to update its launchedFromPackage to be the
1472 // same as the previous activity. Note that this is safe, since we know
1473 // these two packages come from the same uid; the caller could just as
1474 // well have supplied that same package name itself. This specifially
1475 // deals with the case of an intent picker/chooser being launched in the app
1476 // flow to redirect to an activity picked by the user, where we want the final
1477 // activity to consider it to have been launched by the previous app activity.
1478 callingPackage = sourceRecord.launchedFromPackage;
1479 }
Craig Mautner6170f732013-04-02 13:05:23 -07001480 }
1481
1482 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1483 // We couldn't find a class that can handle the given Intent.
1484 // That's the end of that!
1485 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1486 }
1487
1488 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1489 // We couldn't find the specific class specified in the Intent.
1490 // Also the end of the line.
1491 err = ActivityManager.START_CLASS_NOT_FOUND;
1492 }
1493
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001494 if (err == ActivityManager.START_SUCCESS
1495 && !isCurrentProfileLocked(userId)
1496 && (aInfo.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
1497 // Trying to launch a background activity that doesn't show for all users.
1498 err = ActivityManager.START_NOT_CURRENT_USER_ACTIVITY;
1499 }
1500
Dianne Hackborn91097de2014-04-04 18:02:06 -07001501 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1502 && sourceRecord.task.voiceSession != null) {
1503 // If this activity is being launched as part of a voice session, we need
1504 // to ensure that it is safe to do so. If the upcoming activity will also
1505 // be part of the voice session, we can only launch it if it has explicitly
1506 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001507 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001508 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1509 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001510 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1511 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001512 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1513 }
1514 } catch (RemoteException e) {
1515 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1516 }
1517 }
1518 }
1519
1520 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1521 // If the caller is starting a new voice session, just make sure the target
1522 // is actually allowing it to run this way.
1523 try {
1524 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1525 intent, resolvedType)) {
1526 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1527 }
1528 } catch (RemoteException e) {
1529 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1530 }
1531 }
1532
louis_changcd5d1982014-08-01 10:09:08 +08001533 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1534
Craig Mautner6170f732013-04-02 13:05:23 -07001535 if (err != ActivityManager.START_SUCCESS) {
1536 if (resultRecord != null) {
1537 resultStack.sendActivityResultLocked(-1,
1538 resultRecord, resultWho, requestCode,
1539 Activity.RESULT_CANCELED, null);
1540 }
Craig Mautner6170f732013-04-02 13:05:23 -07001541 ActivityOptions.abort(options);
1542 return err;
1543 }
1544
Svetoslav7008b512015-06-24 18:47:07 -07001545 boolean abort = false;
1546
Svet Ganov99b60432015-06-27 13:15:22 -07001547 final int startAnyPerm = mService.checkPermission(
1548 START_ANY_ACTIVITY, callingPid, callingUid);
1549
1550 if (startAnyPerm != PERMISSION_GRANTED) {
1551 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001552 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001553 final int actionRestriction = getActionRestrictionForCallingPackage(
1554 intent.getAction(), callingPackage, callingPid, callingUid);
1555
1556 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1557 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1558 if (resultRecord != null) {
1559 resultStack.sendActivityResultLocked(-1,
1560 resultRecord, resultWho, requestCode,
1561 Activity.RESULT_CANCELED, null);
1562 }
1563 String msg;
1564 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1565 msg = "Permission Denial: starting " + intent.toString()
1566 + " from " + callerApp + " (pid=" + callingPid
1567 + ", uid=" + callingUid + ")" + " with revoked permission "
1568 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1569 } else if (!aInfo.exported) {
1570 msg = "Permission Denial: starting " + intent.toString()
1571 + " from " + callerApp + " (pid=" + callingPid
1572 + ", uid=" + callingUid + ")"
1573 + " not exported from uid " + aInfo.applicationInfo.uid;
1574 } else {
1575 msg = "Permission Denial: starting " + intent.toString()
1576 + " from " + callerApp + " (pid=" + callingPid
1577 + ", uid=" + callingUid + ")"
1578 + " requires " + aInfo.permission;
1579 }
1580 Slog.w(TAG, msg);
1581 throw new SecurityException(msg);
1582 }
1583
1584 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1585 String message = "Appop Denial: starting " + intent.toString()
1586 + " from " + callerApp + " (pid=" + callingPid
1587 + ", uid=" + callingUid + ")"
1588 + " requires " + AppOpsManager.permissionToOp(
1589 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1590 Slog.w(TAG, message);
1591 abort = true;
1592 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1593 String message = "Appop Denial: starting " + intent.toString()
1594 + " from " + callerApp + " (pid=" + callingPid
1595 + ", uid=" + callingUid + ")"
1596 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1597 Slog.w(TAG, message);
1598 abort = true;
1599 }
Svetoslav7008b512015-06-24 18:47:07 -07001600 }
1601
1602 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001603 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001604
Craig Mautner6170f732013-04-02 13:05:23 -07001605 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001606 try {
1607 // The Intent we give to the watcher has the extra data
1608 // stripped off, since it can contain private information.
1609 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001610 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001611 aInfo.applicationInfo.packageName);
1612 } catch (RemoteException e) {
1613 mService.mController = null;
1614 }
Ben Gruver5e207332013-04-03 17:41:37 -07001615 }
Craig Mautner6170f732013-04-02 13:05:23 -07001616
Ben Gruver5e207332013-04-03 17:41:37 -07001617 if (abort) {
1618 if (resultRecord != null) {
1619 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001620 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001621 }
Ben Gruver5e207332013-04-03 17:41:37 -07001622 // We pretend to the caller that it was really started, but
1623 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001624 ActivityOptions.abort(options);
1625 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001626 }
1627
1628 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001629 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001630 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001631 if (outActivity != null) {
1632 outActivity[0] = r;
1633 }
1634
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001635 if (r.appTimeTracker == null && sourceRecord != null) {
1636 // If the caller didn't specify an explicit time tracker, we want to continue
1637 // tracking under any it has.
1638 r.appTimeTracker = sourceRecord.appTimeTracker;
1639 }
1640
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001641 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001642 if (voiceSession == null && (stack.mResumedActivity == null
1643 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001644 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1645 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001646 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001647 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001648 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001649 ActivityOptions.abort(options);
1650 return ActivityManager.START_SWITCHES_CANCELED;
1651 }
1652 }
1653
1654 if (mService.mDidAppSwitch) {
1655 // This is the second allowed switch since we stopped switches,
1656 // so now just generally allow switches. Use case: user presses
1657 // home (switches disabled, switch to home, mDidAppSwitch now true);
1658 // user taps a home icon (coming from home so allowed, we hit here
1659 // and now allow anyone to switch again).
1660 mService.mAppSwitchesAllowedTime = 0;
1661 } else {
1662 mService.mDidAppSwitch = true;
1663 }
1664
Craig Mautneree36c772014-07-16 14:56:05 -07001665 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001666
Dianne Hackborn91097de2014-04-04 18:02:06 -07001667 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001668 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001669
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001670 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001671 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001672 // activity start, but we are not actually doing an activity
1673 // switch... just dismiss the keyguard now, because we
1674 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001675 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001676 }
1677 return err;
1678 }
1679
Svet Ganov99b60432015-06-27 13:15:22 -07001680 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001681 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Svet Ganov99b60432015-06-27 13:15:22 -07001682 if (activityInfo.permission == null) {
1683 return ACTIVITY_RESTRICTION_NONE;
1684 }
1685
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001686 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1687 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001688 == PackageManager.PERMISSION_DENIED) {
1689 return ACTIVITY_RESTRICTION_PERMISSION;
1690 }
1691
1692 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1693 if (opCode == AppOpsManager.OP_NONE) {
1694 return ACTIVITY_RESTRICTION_NONE;
1695 }
1696
1697 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1698 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001699 if (!ignoreTargetSecurity) {
1700 return ACTIVITY_RESTRICTION_APPOP;
1701 }
Svet Ganov99b60432015-06-27 13:15:22 -07001702 }
1703
1704 return ACTIVITY_RESTRICTION_NONE;
1705 }
1706
Svetoslav7008b512015-06-24 18:47:07 -07001707 private int getActionRestrictionForCallingPackage(String action,
1708 String callingPackage, int callingPid, int callingUid) {
1709 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001710 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001711 }
1712
1713 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1714 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001715 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001716 }
1717
1718 final PackageInfo packageInfo;
1719 try {
1720 packageInfo = mService.mContext.getPackageManager()
1721 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1722 } catch (PackageManager.NameNotFoundException e) {
1723 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001724 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001725 }
1726
1727 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001728 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001729 }
1730
1731 if (mService.checkPermission(permission, callingPid, callingUid) ==
1732 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001733 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001734 }
1735
1736 final int opCode = AppOpsManager.permissionToOpCode(permission);
1737 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001738 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001739 }
1740
1741 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1742 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001743 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001744 }
1745
Svet Ganov99b60432015-06-27 13:15:22 -07001746 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001747 }
1748
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001749 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001750 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001751
1752 // On leanback only devices we should keep all activities in the same stack.
1753 if (!mLeanbackOnlyDevice &&
1754 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001755
1756 ActivityStack stack;
1757
Wale Ogunwale7d701172015-03-11 15:36:30 -07001758 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001759 stack = task.stack;
1760 if (stack.isOnHomeDisplay()) {
1761 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001762 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1763 "computeStackFocus: Setting " + "focused stack to r=" + r
1764 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001765 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001766 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001767 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001768 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001769 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001770 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001771 }
1772
Craig Mautnere0a38842013-12-16 16:14:02 -08001773 final ActivityContainer container = r.mInitialActivityContainer;
1774 if (container != null) {
1775 // The first time put it on the desired stack, after this put on task stack.
1776 r.mInitialActivityContainer = null;
1777 return container.mStack;
1778 }
1779
Craig Mautner1b4bf852014-05-26 15:06:32 -07001780 if (mFocusedStack != mHomeStack && (!newTask ||
1781 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001782 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001783 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001784 return mFocusedStack;
1785 }
1786
Craig Mautnere0a38842013-12-16 16:14:02 -08001787 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1788 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001789 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001790 if (!stack.isHomeStack()) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001791 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001792 "computeStackFocus: Setting focused stack=" + stack);
1793 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001794 }
1795 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001796
Craig Mautner4a1cb222013-12-04 16:14:06 -08001797 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001798 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001799 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1800 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001801 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001802 }
1803 return mHomeStack;
1804 }
1805
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001806 boolean setFocusedStack(ActivityRecord r, String reason) {
1807 if (r == null) {
1808 // Not sure what you are trying to do, but it is not going to work...
1809 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001810 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001811 final TaskRecord task = r.task;
1812 if (task == null || task.stack == null) {
1813 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1814 return false;
1815 }
1816 task.stack.moveToFront(reason);
1817 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001818 }
1819
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001820 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001821 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001822 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001823 final Intent intent = r.intent;
1824 final int callingUid = r.launchedFromUid;
1825
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001826 // In some flows in to this function, we retrieve the task record and hold on to it
1827 // without a lock before calling back in to here... so the task at this point may
1828 // not actually be in recents. Check for that, and if it isn't in recents just
1829 // consider it invalid.
1830 if (inTask != null && !inTask.inRecents) {
1831 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1832 inTask = null;
1833 }
1834
Craig Mautnerad400af2014-08-13 16:41:36 -07001835 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001836 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1837 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001838
Craig Mautnera228ae92014-07-09 05:44:55 -07001839 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001840 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001841 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001842 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1843 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1844 "\"singleInstance\" or \"singleTask\"");
1845 launchFlags &=
1846 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1847 } else {
1848 switch (r.info.documentLaunchMode) {
1849 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1850 break;
1851 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1852 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1853 break;
1854 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1855 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1856 break;
1857 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1858 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1859 break;
1860 }
1861 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001862
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001863 final boolean launchTaskBehind = r.mLaunchTaskBehind
1864 && !launchSingleTask && !launchSingleInstance
1865 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001866
Wale Ogunwale7d701172015-03-11 15:36:30 -07001867 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1868 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001869 // For whatever reason this activity is being launched into a new
1870 // task... yet the caller has requested a result back. Well, that
1871 // is pretty messed up, so instead immediately send back a cancel
1872 // and let the new task continue launched as normal without a
1873 // dependency on its originator.
1874 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1875 r.resultTo.task.stack.sendActivityResultLocked(-1,
1876 r.resultTo, r.resultWho, r.requestCode,
1877 Activity.RESULT_CANCELED, null);
1878 r.resultTo = null;
1879 }
1880
1881 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1882 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1883 }
1884
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001885 // If we are actually going to launch in to a new task, there are some cases where
1886 // we further want to do multiple task.
1887 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1888 if (launchTaskBehind
1889 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1890 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1891 }
1892 }
1893
Craig Mautner8849a5e2013-04-02 16:41:03 -07001894 // We'll invoke onUserLeaving before onPause only if the launching
1895 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001896 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001897 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1898 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001899
1900 // If the caller has asked not to resume at this point, we make note
1901 // of this in the record so that we can skip it when trying to find
1902 // the top running activity.
1903 if (!doResume) {
1904 r.delayedResume = true;
1905 }
1906
Craig Mautnera254cd72014-05-25 16:47:39 -07001907 ActivityRecord notTop =
1908 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001909
1910 // If the onlyIfNeeded flag is set, then we can do this if the activity
1911 // being launched is the same as the one making the call... or, as
1912 // a special case, if we do not know the caller then we count the
1913 // current top activity as the caller.
1914 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1915 ActivityRecord checkedCaller = sourceRecord;
1916 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001917 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001918 }
1919 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1920 // Caller is not the same as launcher, so always needed.
1921 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1922 }
1923 }
1924
Craig Mautner8849a5e2013-04-02 16:41:03 -07001925 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001926 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001927
1928 // If the caller is not coming from another activity, but has given us an
1929 // explicit task into which they would like us to launch the new activity,
1930 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001931 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1932 final Intent baseIntent = inTask.getBaseIntent();
1933 final ActivityRecord root = inTask.getRootActivity();
1934 if (baseIntent == null) {
1935 ActivityOptions.abort(options);
1936 throw new IllegalArgumentException("Launching into task without base intent: "
1937 + inTask);
1938 }
1939
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001940 // If this task is empty, then we are adding the first activity -- it
1941 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001942 if (launchSingleInstance || launchSingleTask) {
1943 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1944 ActivityOptions.abort(options);
1945 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1946 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001947 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001948 if (root != null) {
1949 ActivityOptions.abort(options);
1950 throw new IllegalArgumentException("Caller with inTask " + inTask
1951 + " has root " + root + " but target is singleInstance/Task");
1952 }
1953 }
1954
1955 // If task is empty, then adopt the interesting intent launch flags in to the
1956 // activity being started.
1957 if (root == null) {
1958 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1959 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1960 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1961 launchFlags = (launchFlags&~flagsOfInterest)
1962 | (baseIntent.getFlags()&flagsOfInterest);
1963 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001964 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001965 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001966
Dianne Hackborn962d5352014-08-29 16:27:40 -07001967 // If the task is not empty and the caller is asking to start it as the root
1968 // of a new task, then we don't actually want to start this on the task. We
1969 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001970 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001971 addingToTask = false;
1972
1973 } else {
1974 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001975 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001976
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001977 reuseTask = inTask;
1978 } else {
1979 inTask = null;
1980 }
1981
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001982 if (inTask == null) {
1983 if (sourceRecord == null) {
1984 // This activity is not being started from another... in this
1985 // case we -always- start a new task.
1986 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1987 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1988 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1989 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1990 }
1991 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1992 // The original activity who is starting us is running as a single
1993 // instance... this new activity it is starting must go on its
1994 // own task.
1995 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1996 } else if (launchSingleInstance || launchSingleTask) {
1997 // The activity being started is a single instance... it always
1998 // gets launched into its own task.
1999 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2000 }
2001 }
2002
2003 ActivityInfo newTaskInfo = null;
2004 Intent newTaskIntent = null;
2005 ActivityStack sourceStack;
2006 if (sourceRecord != null) {
2007 if (sourceRecord.finishing) {
2008 // If the source is finishing, we can't further count it as our source. This
2009 // is because the task it is associated with may now be empty and on its way out,
2010 // so we don't want to blindly throw it in to that task. Instead we will take
2011 // the NEW_TASK flow and try to find a task for it. But save the task information
2012 // so it can be used when creating the new task.
2013 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2014 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2015 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2016 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2017 newTaskInfo = sourceRecord.info;
2018 newTaskIntent = sourceRecord.task.intent;
2019 }
2020 sourceRecord = null;
2021 sourceStack = null;
2022 } else {
2023 sourceStack = sourceRecord.task.stack;
2024 }
2025 } else {
2026 sourceStack = null;
2027 }
2028
2029 boolean movedHome = false;
2030 ActivityStack targetStack;
2031
2032 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002033 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002034
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002035 // We may want to try to place the new activity in to an existing task. We always
2036 // do this if the target activity is singleTask or singleInstance; we will also do
2037 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2038 // us to still place it in a new task: multi task, always doc mode, or being asked to
2039 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002040 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2041 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002042 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002043 // If bring to front is requested, and no result is requested and we have not
2044 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002045 // we can find a task that was started with this same
2046 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002047 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002048 // See if there is a task to bring to the front. If this is
2049 // a SINGLE_INSTANCE activity, there can be one and only one
2050 // instance of it in the history, and it is always in its own
2051 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002052 ActivityRecord intentActivity = !launchSingleInstance ?
2053 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002054 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002055 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2056 // but still needs to be a lock task mode violation since the task gets
2057 // cleared out and the device would otherwise leave the locked task.
2058 if (isLockTaskModeViolation(intentActivity.task,
2059 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2060 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002061 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002062 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002063 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2064 }
Craig Mautner29219d92013-04-16 20:19:12 -07002065 if (r.task == null) {
2066 r.task = intentActivity.task;
2067 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002068 if (intentActivity.task.intent == null) {
2069 // This task was started because of movement of
2070 // the activity based on affinity... now that we
2071 // are actually launching it, we can assign the
2072 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002073 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002074 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002075 targetStack = intentActivity.task.stack;
2076 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002077 // If the target task is not in the front, then we need
2078 // to bring it to the front... except... well, with
2079 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2080 // to have the same behavior as if a new instance was
2081 // being started, which means not bringing it to the front
2082 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002083 final ActivityStack focusStack = getFocusedStack();
2084 ActivityRecord curTop = (focusStack == null)
2085 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002086 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002087 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002088 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002089 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002090 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2091 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002092 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002093 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002094 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2095 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002096 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002097 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002098 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002099 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002100 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002101 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2102 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002103 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002104 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002105 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002106 options = null;
2107 }
2108 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002109 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002110 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002111 + " from " + intentActivity);
2112 targetStack.moveToFront("intentActivityFound");
2113 }
2114
Craig Mautner8849a5e2013-04-02 16:41:03 -07002115 // If the caller has requested that the target task be
2116 // reset, then do so.
2117 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2118 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2119 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002120 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002121 // We don't need to start a new activity, and
2122 // the client said not to do anything if that
2123 // is the case, so this is it! And for paranoia, make
2124 // sure we have correctly resumed the top activity.
2125 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002126 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002127
2128 // Make sure to notify Keyguard as well if we are not running an app
2129 // transition later.
2130 if (!movedToFront) {
2131 notifyActivityDrawnForKeyguard();
2132 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002133 } else {
2134 ActivityOptions.abort(options);
2135 }
2136 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2137 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002138 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002139 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002140 // The caller has requested to completely replace any
2141 // existing task with its new activity. Well that should
2142 // not be too hard...
2143 reuseTask = intentActivity.task;
2144 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002145 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002146 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002147 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002148 // In this situation we want to remove all activities
2149 // from the task up to the one being started. In most
2150 // cases this means we are resetting the task to its
2151 // initial state.
2152 ActivityRecord top =
2153 intentActivity.task.performClearTaskLocked(r, launchFlags);
2154 if (top != null) {
2155 if (top.frontOfTask) {
2156 // Activity aliases may mean we use different
2157 // intents for the top activity, so make sure
2158 // the task now has the identity of the new
2159 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002160 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002161 }
2162 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2163 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002164 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002165 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002166 // A special case: we need to start the activity because it is not
2167 // currently running, and the caller has asked to clear the current
2168 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002169 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002170 // Now pretend like this activity is being started by the top of its
2171 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002172 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002173 TaskRecord task = sourceRecord.task;
2174 if (task != null && task.stack == null) {
2175 // Target stack got cleared when we all activities were removed
2176 // above. Go ahead and reset it.
2177 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2178 targetStack.addTask(
2179 task, !launchTaskBehind /* toTop */, false /* moving */);
2180 }
2181
Craig Mautner8849a5e2013-04-02 16:41:03 -07002182 }
2183 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2184 // In this case the top activity on the task is the
2185 // same as the one being launched, so we take that
2186 // as a request to bring the task to the foreground.
2187 // If the top activity in the task is the root
2188 // activity, deliver this new intent to it if it
2189 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002190 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002191 && intentActivity.realActivity.equals(r.realActivity)) {
2192 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2193 intentActivity.task);
2194 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002195 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002196 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002197 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2198 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002199 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2200 // In this case we are launching the root activity
2201 // of the task, but with a different intent. We
2202 // should start a new instance on top.
2203 addingToTask = true;
2204 sourceRecord = intentActivity;
2205 }
2206 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2207 // In this case an activity is being launched in to an
2208 // existing task, without resetting that task. This
2209 // is typically the situation of launching an activity
2210 // from a notification or shortcut. We want to place
2211 // the new activity on top of the current task.
2212 addingToTask = true;
2213 sourceRecord = intentActivity;
2214 } else if (!intentActivity.task.rootWasReset) {
2215 // In this case we are launching in to an existing task
2216 // that has not yet been started from its front door.
2217 // The current task has been brought to the front.
2218 // Ideally, we'd probably like to place this new task
2219 // at the bottom of its stack, but that's a little hard
2220 // to do with the current organization of the code so
2221 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002222 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002223 }
2224 if (!addingToTask && reuseTask == null) {
2225 // We didn't do anything... but it was needed (a.k.a., client
2226 // don't use that intent!) And for paranoia, make
2227 // sure we have correctly resumed the top activity.
2228 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002229 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002230 if (!movedToFront) {
2231 // Make sure to notify Keyguard as well if we are not running an app
2232 // transition later.
2233 notifyActivityDrawnForKeyguard();
2234 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002235 } else {
2236 ActivityOptions.abort(options);
2237 }
2238 return ActivityManager.START_TASK_TO_FRONT;
2239 }
2240 }
2241 }
2242 }
2243
2244 //String uri = r.intent.toURI();
2245 //Intent intent2 = new Intent(uri);
2246 //Slog.i(TAG, "Given intent: " + r.intent);
2247 //Slog.i(TAG, "URI is: " + uri);
2248 //Slog.i(TAG, "To intent: " + intent2);
2249
2250 if (r.packageName != null) {
2251 // If the activity being launched is the same as the one currently
2252 // at the top, then we need to check if it should only be launched
2253 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002254 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002255 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002256 if (top != null && r.resultTo == null) {
2257 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2258 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002259 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002260 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002261 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2262 top.task);
2263 // For paranoia, make sure we have correctly
2264 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002265 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002266 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002267 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002268 }
2269 ActivityOptions.abort(options);
2270 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2271 // We don't need to start a new activity, and
2272 // the client said not to do anything if that
2273 // is the case, so this is it!
2274 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2275 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002276 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002277 return ActivityManager.START_DELIVERED_TO_TOP;
2278 }
2279 }
2280 }
2281 }
2282
2283 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002284 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002285 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2286 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002287 }
2288 ActivityOptions.abort(options);
2289 return ActivityManager.START_CLASS_NOT_FOUND;
2290 }
2291
2292 boolean newTask = false;
2293 boolean keepCurTransition = false;
2294
Craig Mautnerbb742462014-07-07 15:28:55 -07002295 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002296 sourceRecord.task : null;
2297
Craig Mautner8849a5e2013-04-02 16:41:03 -07002298 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002299 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002300 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002301 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002302 targetStack = computeStackFocus(r, newTask);
2303 targetStack.moveToFront("startingNewTask");
2304
Craig Mautner8849a5e2013-04-02 16:41:03 -07002305 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002306 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2307 newTaskInfo != null ? newTaskInfo : r.info,
2308 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002309 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2310 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002311 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2312 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002313 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002314 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002315 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002316 if (isLockTaskModeViolation(r.task)) {
2317 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2318 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2319 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002320 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002321 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002322 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2323 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002324 // Caller wants to appear on home activity, so before starting
2325 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002326 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002327 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002328 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002329 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002330 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002331 if (isLockTaskModeViolation(sourceTask)) {
2332 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2333 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2334 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002335 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002336 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002337 final TaskRecord topTask = targetStack.topTask();
2338 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002339 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002340 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002341 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002342 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002343 // In this case, we are adding the activity to an existing
2344 // task, but the caller has asked to clear that task if the
2345 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002346 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002347 keepCurTransition = true;
2348 if (top != null) {
2349 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002350 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002351 // For paranoia, make sure we have correctly
2352 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002353 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002354 if (doResume) {
2355 targetStack.resumeTopActivityLocked(null);
2356 }
2357 ActivityOptions.abort(options);
2358 return ActivityManager.START_DELIVERED_TO_TOP;
2359 }
2360 } else if (!addingToTask &&
2361 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2362 // In this case, we are launching an activity in our own task
2363 // that may already be running somewhere in the history, and
2364 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002365 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002366 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002367 final TaskRecord task = top.task;
2368 task.moveActivityToFrontLocked(top);
2369 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002370 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002371 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002372 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002373 if (doResume) {
2374 targetStack.resumeTopActivityLocked(null);
2375 }
2376 return ActivityManager.START_DELIVERED_TO_TOP;
2377 }
2378 }
2379 // An existing activity is starting this new activity, so we want
2380 // to keep the new one in the same task as the one that is starting
2381 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002382 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002383 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002384 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002385
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002386 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002387 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002388 // task it has provided to us.
2389 if (isLockTaskModeViolation(inTask)) {
2390 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2391 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2392 }
2393 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002394 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2395 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002396
2397 // Check whether we should actually launch the new activity in to the task,
2398 // or just reuse the current activity on top.
2399 ActivityRecord top = inTask.getTopActivity();
2400 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2401 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2402 || launchSingleTop || launchSingleTask) {
2403 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2404 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2405 // We don't need to start a new activity, and
2406 // the client said not to do anything if that
2407 // is the case, so this is it!
2408 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2409 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002410 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002411 return ActivityManager.START_DELIVERED_TO_TOP;
2412 }
2413 }
2414
Dianne Hackborn962d5352014-08-29 16:27:40 -07002415 if (!addingToTask) {
2416 // We don't actually want to have this activity added to the task, so just
2417 // stop here but still tell the caller that we consumed the intent.
2418 ActivityOptions.abort(options);
2419 return ActivityManager.START_TASK_TO_FRONT;
2420 }
2421
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002422 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002423 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002424 + " in explicit task " + r.task);
2425
Craig Mautner8849a5e2013-04-02 16:41:03 -07002426 } else {
2427 // This not being started from an existing activity, and not part
2428 // of a new task... just put it in the top task, though these days
2429 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002430 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002431 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002432 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002433 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002434 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002435 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002436 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002437 + " in new guessed " + r.task);
2438 }
2439
2440 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002441 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002442
Craig Mautner76e2a762014-06-24 09:05:43 -07002443 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2444 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2445 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002446 if (newTask) {
2447 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2448 }
2449 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002450 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002451 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002452 if (!launchTaskBehind) {
2453 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002454 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002455 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002456 return ActivityManager.START_SUCCESS;
2457 }
2458
Craig Mautneree36c772014-07-16 14:56:05 -07002459 final void doPendingActivityLaunchesLocked(boolean doResume) {
2460 while (!mPendingActivityLaunches.isEmpty()) {
2461 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002462 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002463 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002464 }
2465 }
2466
Craig Mautner7f13ed32014-07-28 14:00:35 -07002467 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002468 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2469 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002470 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002471 mPendingActivityLaunches.remove(palNdx);
2472 }
2473 }
2474 }
2475
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002476 void setLaunchSource(int uid) {
2477 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2478 }
2479
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002480 void acquireLaunchWakelock() {
2481 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2482 throw new IllegalStateException("Calling must be system uid");
2483 }
2484 mLaunchingActivity.acquire();
2485 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2486 // To be safe, don't allow the wake lock to be held for too long.
2487 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2488 }
2489 }
2490
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002491 /**
2492 * Called when the frontmost task is idle.
2493 * @return the state of mService.mBooting before this was called.
2494 */
2495 private boolean checkFinishBootingLocked() {
2496 final boolean booting = mService.mBooting;
2497 boolean enableScreen = false;
2498 mService.mBooting = false;
2499 if (!mService.mBooted) {
2500 mService.mBooted = true;
2501 enableScreen = true;
2502 }
2503 if (booting || enableScreen) {
2504 mService.postFinishBooting(booting, enableScreen);
2505 }
2506 return booting;
2507 }
2508
Craig Mautnerf3333272013-04-22 10:55:53 -07002509 // Checked.
2510 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2511 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002512 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002513
Craig Mautnerf3333272013-04-22 10:55:53 -07002514 ArrayList<ActivityRecord> stops = null;
2515 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002516 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002517 int NS = 0;
2518 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002519 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002520 boolean activityRemoved = false;
2521
Wale Ogunwale7d701172015-03-11 15:36:30 -07002522 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002523 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002524 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2525 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002526 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2527 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002528 if (fromTimeout) {
2529 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002530 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002531
2532 // This is a hack to semi-deal with a race condition
2533 // in the client where it can be constructed with a
2534 // newer configuration from when we asked it to launch.
2535 // We'll update with whatever configuration it now says
2536 // it used to launch.
2537 if (config != null) {
2538 r.configuration = config;
2539 }
2540
2541 // We are now idle. If someone is waiting for a thumbnail from
2542 // us, we can now deliver.
2543 r.idle = true;
2544
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002545 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002546 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002547 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002548 }
2549 }
2550
2551 if (allResumedActivitiesIdle()) {
2552 if (r != null) {
2553 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002554 }
2555
2556 if (mLaunchingActivity.isHeld()) {
2557 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2558 if (VALIDATE_WAKE_LOCK_CALLER &&
2559 Binder.getCallingUid() != Process.myUid()) {
2560 throw new IllegalStateException("Calling must be system uid");
2561 }
2562 mLaunchingActivity.release();
2563 }
2564 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002565 }
2566
2567 // Atomically retrieve all of the other things to do.
2568 stops = processStoppingActivitiesLocked(true);
2569 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002570 if ((NF = mFinishingActivities.size()) > 0) {
2571 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002572 mFinishingActivities.clear();
2573 }
2574
Craig Mautnerf3333272013-04-22 10:55:53 -07002575 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002576 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002577 mStartingUsers.clear();
2578 }
2579
Craig Mautnerf3333272013-04-22 10:55:53 -07002580 // Stop any activities that are scheduled to do so but have been
2581 // waiting for the next one to start.
2582 for (int i = 0; i < NS; i++) {
2583 r = stops.get(i);
2584 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002585 if (stack != null) {
2586 if (r.finishing) {
2587 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2588 } else {
2589 stack.stopActivityLocked(r);
2590 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002591 }
2592 }
2593
2594 // Finish any activities that are scheduled to do so but have been
2595 // waiting for the next one to start.
2596 for (int i = 0; i < NF; i++) {
2597 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002598 final ActivityStack stack = r.task.stack;
2599 if (stack != null) {
2600 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2601 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002602 }
2603
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002604 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002605 // Complete user switch
2606 if (startingUsers != null) {
2607 for (int i = 0; i < startingUsers.size(); i++) {
2608 mService.finishUserSwitch(startingUsers.get(i));
2609 }
2610 }
2611 // Complete starting up of background users
2612 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002613 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002614 mStartingBackgroundUsers.clear();
2615 for (int i = 0; i < startingUsers.size(); i++) {
2616 mService.finishUserBoot(startingUsers.get(i));
2617 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002618 }
2619 }
2620
2621 mService.trimApplications();
2622 //dump();
2623 //mWindowManager.dump();
2624
Craig Mautnerf3333272013-04-22 10:55:53 -07002625 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002626 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002627 }
2628
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002629 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002630 }
2631
Craig Mautner8e569572013-10-11 17:36:59 -07002632 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002633 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002634 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2635 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002636 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2637 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2638 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002639 }
Craig Mautner19091252013-10-05 00:03:53 -07002640 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002641 }
2642
2643 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002644 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2645 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002646 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2647 stacks.get(stackNdx).closeSystemDialogsLocked();
2648 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002649 }
2650 }
2651
Craig Mautner93529a42013-10-04 15:03:13 -07002652 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002653 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002654 }
2655
Craig Mautner8d341ef2013-03-26 09:03:27 -07002656 /**
2657 * @return true if some activity was finished (or would have finished if doit were true).
2658 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002659 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2660 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002661 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002662 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2663 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002664 final int numStacks = stacks.size();
2665 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2666 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002667 if (stack.finishDisabledPackageActivitiesLocked(
2668 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002669 didSomething = true;
2670 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002671 }
2672 }
2673 return didSomething;
2674 }
2675
Dianne Hackborna413dc02013-07-12 12:02:55 -07002676 void updatePreviousProcessLocked(ActivityRecord r) {
2677 // Now that this process has stopped, we may want to consider
2678 // it to be the previous app to try to keep around in case
2679 // the user wants to return to it.
2680
2681 // First, found out what is currently the foreground app, so that
2682 // we don't blow away the previous app if this activity is being
2683 // hosted by the process that is actually still the foreground.
2684 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002685 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2686 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002687 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2688 final ActivityStack stack = stacks.get(stackNdx);
2689 if (isFrontStack(stack)) {
2690 if (stack.mResumedActivity != null) {
2691 fgApp = stack.mResumedActivity.app;
2692 } else if (stack.mPausingActivity != null) {
2693 fgApp = stack.mPausingActivity.app;
2694 }
2695 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002696 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002697 }
2698 }
2699
2700 // Now set this one as the previous process, only if that really
2701 // makes sense to.
2702 if (r.app != null && fgApp != null && r.app != fgApp
2703 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002704 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002705 mService.mPreviousProcess = r.app;
2706 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2707 }
2708 }
2709
Craig Mautner05d29032013-05-03 13:40:13 -07002710 boolean resumeTopActivitiesLocked() {
2711 return resumeTopActivitiesLocked(null, null, null);
2712 }
2713
2714 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2715 Bundle targetOptions) {
2716 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002717 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002718 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002719 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002720 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002721 if (isFrontStack(targetStack)) {
2722 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2723 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002724
Craig Mautnere0a38842013-12-16 16:14:02 -08002725 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2726 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002727 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2728 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002729 if (stack == targetStack) {
2730 // Already started above.
2731 continue;
2732 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002733 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002734 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002735 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002736 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002737 }
Craig Mautner05d29032013-05-03 13:40:13 -07002738 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002739 }
2740
Todd Kennedy539db512014-12-15 09:57:55 -08002741 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002742 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2743 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002744 final int numStacks = stacks.size();
2745 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2746 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002747 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002748 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002749 }
2750 }
2751
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002752 void finishVoiceTask(IVoiceInteractionSession session) {
2753 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2754 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2755 final int numStacks = stacks.size();
2756 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2757 final ActivityStack stack = stacks.get(stackNdx);
2758 stack.finishVoiceTask(session);
2759 }
2760 }
2761 }
2762
Craig Mautner299f9602015-01-26 09:47:33 -08002763 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002764 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2765 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002766 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002767 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2768 // Caller wants the home activity moved with it. To accomplish this,
2769 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002770 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002771 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002772 if (task.stack == null) {
2773 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2774 + task + " to front. Stack is null");
2775 return;
2776 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002777 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2778 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2779 reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002780 if (DEBUG_STACK) Slog.d(TAG_STACK,
2781 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002782 }
2783
Craig Mautner967212c2013-04-13 21:10:58 -07002784 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002785 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2786 if (activityContainer != null) {
2787 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002788 }
2789 return null;
2790 }
2791
Craig Mautner967212c2013-04-13 21:10:58 -07002792 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002793 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002794 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2795 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002796 }
2797 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002798 }
2799
Craig Mautner4a1cb222013-12-04 16:14:06 -08002800 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002801 ActivityRecord homeActivity = getHomeActivity();
2802 if (homeActivity != null) {
2803 return homeActivity.appToken;
2804 }
2805 return null;
2806 }
2807
2808 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002809 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002810 }
2811
2812 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002813 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2814 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2815 final TaskRecord task = tasks.get(taskNdx);
2816 if (task.isHomeTask()) {
2817 final ArrayList<ActivityRecord> activities = task.mActivities;
2818 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2819 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002820 if (r.isHomeActivity()
2821 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002822 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002823 }
2824 }
2825 }
2826 }
2827 return null;
2828 }
2829
Todd Kennedyca4d8422015-01-15 15:19:22 -08002830 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002831 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002832 ActivityContainer activityContainer =
2833 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002834 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002835 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2836 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002837 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002838 return activityContainer;
2839 }
2840
Craig Mautner34b73df2014-01-12 21:11:08 -08002841 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002842 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2843 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2844 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002845 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2846 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002847 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002848 }
2849 }
2850
Craig Mautner95da1082014-02-24 17:54:35 -08002851 void deleteActivityContainer(IActivityContainer container) {
2852 ActivityContainer activityContainer = (ActivityContainer)container;
2853 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002854 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2855 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002856 final int stackId = activityContainer.mStackId;
2857 mActivityContainers.remove(stackId);
2858 mWindowManager.removeStack(stackId);
2859 }
2860 }
2861
Wale Ogunwale60454db2015-01-23 16:05:07 -08002862 void resizeStackLocked(int stackId, Rect bounds) {
2863 final ActivityStack stack = getStack(stackId);
2864 if (stack == null) {
2865 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2866 return;
2867 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002868
2869 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002870 if (r != null && !r.task.mResizeable) {
2871 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002872 return;
2873 }
2874
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002875 final IntArray changedTaskIds = new IntArray(stack.numTasks());
2876 final List<Configuration> newTaskConfigs = new ArrayList<>(stack.numTasks());
2877 stack.mFullscreen =
2878 mWindowManager.resizeStack(stackId, bounds, changedTaskIds, newTaskConfigs);
2879 for (int i = changedTaskIds.size() - 1; i >= 0; i--) {
2880 final TaskRecord task = anyTaskForIdLocked(changedTaskIds.get(i), false);
2881 if (task == null) {
2882 Slog.wtf(TAG, "Task in WindowManager, but not in ActivityManager???");
2883 continue;
2884 }
2885 task.updateOverrideConfiguration(newTaskConfigs.get(i));
2886 }
2887
2888 if (r != null) {
2889 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2890 // And we need to make sure at this point that all other activities
2891 // are made visible with the correct configuration.
2892 ensureActivitiesVisibleLocked(r, 0);
2893 if (!updated) {
2894 resumeTopActivitiesLocked(stack, null, null);
2895 }
2896 }
2897 }
2898
2899 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2900 if (!task.mResizeable) {
2901 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
2902 return;
2903 }
2904
2905 if (task.mBounds != null && task.mBounds.equals(bounds)) {
2906 // Nothing to do here...
2907 return;
2908 }
2909
2910 task.mBounds = new Rect(bounds);
2911
2912 if (!mWindowManager.isValidTaskId(task.taskId)) {
2913 // Task doesn't exist in window manager yet (e.g. was restored from recents).
2914 // No need to do anything else until we add the task to window manager.
2915 return;
2916 }
2917
2918 final Configuration overrideConfig = mWindowManager.resizeTask(task.taskId, bounds);
2919 if (task.updateOverrideConfiguration(overrideConfig)) {
2920 ActivityRecord r = task.topRunningActivityLocked(null);
Wale Ogunwale60454db2015-01-23 16:05:07 -08002921 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002922 final ActivityStack stack = task.stack;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002923 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2924 // And we need to make sure at this point that all other activities
2925 // are made visible with the correct configuration.
2926 ensureActivitiesVisibleLocked(r, 0);
2927 if (!updated) {
2928 resumeTopActivitiesLocked(stack, null, null);
2929 }
2930 }
2931 }
2932 }
2933
Todd Kennedy4900bf92015-01-16 16:05:14 -08002934 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002935 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2936 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002937 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002938 }
2939
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002940 ActivityContainer activityContainer = new ActivityContainer(stackId);
2941 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002942 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002943 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002944 }
2945
2946 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002947 while (true) {
2948 if (++mLastStackId <= HOME_STACK_ID) {
2949 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002950 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002951 if (getStack(mLastStackId) == null) {
2952 break;
2953 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002954 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002955 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002956 }
2957
Wale Ogunwale7de05352014-12-12 15:21:33 -08002958 private boolean restoreRecentTaskLocked(TaskRecord task) {
2959 ActivityStack stack = null;
2960 // Determine stack to restore task to.
2961 if (mLeanbackOnlyDevice) {
2962 // There is only one stack for lean back devices.
2963 stack = mHomeStack;
2964 } else {
2965 // Look for the top stack on the home display that isn't the home stack.
2966 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2967 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2968 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002969 if (!tmpStack.isHomeStack()) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002970 stack = tmpStack;
2971 break;
2972 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002973 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002974 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002975
2976 if (stack == null) {
2977 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2978 // before an application stack is created...Go ahead and create one on the default
2979 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002980 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002981 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002982 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002983 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2984 "Created stack=" + stack + " for recents restoration.");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002985 }
2986
2987 if (stack == null) {
2988 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002989 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2990 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002991 return false;
2992 }
2993
2994 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002995 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2996 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002997 final ArrayList<ActivityRecord> activities = task.mActivities;
2998 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2999 final ActivityRecord r = activities.get(activityNdx);
3000 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
3001 r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07003002 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0,
Wale Ogunwale7de05352014-12-12 15:21:33 -08003003 r.userId, r.info.configChanges, task.voiceSession != null,
3004 r.mLaunchTaskBehind);
3005 }
3006 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003007 }
3008
Craig Mautner299f9602015-01-26 09:47:33 -08003009 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003010 final TaskRecord task = anyTaskForIdLocked(taskId);
3011 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003012 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003013 return;
3014 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003015 final ActivityStack stack = getStack(stackId);
3016 if (stack == null) {
3017 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
3018 return;
3019 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003020 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
3021 if (task.stack != null) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07003022 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003023 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003024 stack.addTask(task, toTop, true);
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003025 // The task might have already been running and its visibility needs to be synchronized with
3026 // the visibility of the stack / windows.
3027 stack.ensureActivitiesVisibleLocked(null, 0);
Craig Mautner05d29032013-05-03 13:40:13 -07003028 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003029 }
3030
Craig Mautnerac6f8432013-07-17 13:24:59 -07003031 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003032 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003033 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3034 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003035 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3036 final ActivityStack stack = stacks.get(stackNdx);
3037 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003038 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003039 continue;
3040 }
3041 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003042 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3043 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003044 continue;
3045 }
3046 final ActivityRecord ar = stack.findTaskLocked(r);
3047 if (ar != null) {
3048 return ar;
3049 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003050 }
3051 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003052 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003053 return null;
3054 }
3055
3056 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003057 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3058 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003059 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3060 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3061 if (ar != null) {
3062 return ar;
3063 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003064 }
3065 }
3066 return null;
3067 }
3068
Craig Mautner8d341ef2013-03-26 09:03:27 -07003069 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003070 scheduleSleepTimeout();
3071 if (!mGoingToSleep.isHeld()) {
3072 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003073 if (mLaunchingActivity.isHeld()) {
3074 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3075 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003076 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003077 mLaunchingActivity.release();
3078 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003079 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003080 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003081 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003082 }
3083
3084 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003085 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003086
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003087 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003088 final long endTime = System.currentTimeMillis() + timeout;
3089 while (true) {
3090 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003091 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3092 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003093 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3094 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3095 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003096 }
3097 if (cantShutdown) {
3098 long timeRemaining = endTime - System.currentTimeMillis();
3099 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003100 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003101 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003102 } catch (InterruptedException e) {
3103 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003104 } else {
3105 Slog.w(TAG, "Activity manager shutdown timed out");
3106 timedout = true;
3107 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003108 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003109 } else {
3110 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003111 }
3112 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003113
3114 // Force checkReadyForSleep to complete.
3115 mSleepTimeout = true;
3116 checkReadyForSleepLocked();
3117
Craig Mautner8d341ef2013-03-26 09:03:27 -07003118 return timedout;
3119 }
3120
3121 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003122 removeSleepTimeouts();
3123 if (mGoingToSleep.isHeld()) {
3124 mGoingToSleep.release();
3125 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003126 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3127 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003128 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3129 final ActivityStack stack = stacks.get(stackNdx);
3130 stack.awakeFromSleepingLocked();
3131 if (isFrontStack(stack)) {
3132 resumeTopActivitiesLocked();
3133 }
Craig Mautner5314a402013-09-26 12:40:16 -07003134 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003135 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003136 mGoingToSleepActivities.clear();
3137 }
3138
3139 void activitySleptLocked(ActivityRecord r) {
3140 mGoingToSleepActivities.remove(r);
3141 checkReadyForSleepLocked();
3142 }
3143
3144 void checkReadyForSleepLocked() {
3145 if (!mService.isSleepingOrShuttingDown()) {
3146 // Do not care.
3147 return;
3148 }
3149
3150 if (!mSleepTimeout) {
3151 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003152 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3153 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003154 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3155 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3156 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003157 }
3158
3159 if (mStoppingActivities.size() > 0) {
3160 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003161 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003162 + mStoppingActivities.size() + " activities");
3163 scheduleIdleLocked();
3164 dontSleep = true;
3165 }
3166
3167 if (mGoingToSleepActivities.size() > 0) {
3168 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003169 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003170 + mGoingToSleepActivities.size() + " activities");
3171 dontSleep = true;
3172 }
3173
3174 if (dontSleep) {
3175 return;
3176 }
3177 }
3178
Craig Mautnere0a38842013-12-16 16:14:02 -08003179 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3180 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003181 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3182 stacks.get(stackNdx).goToSleep();
3183 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003184 }
3185
3186 removeSleepTimeouts();
3187
3188 if (mGoingToSleep.isHeld()) {
3189 mGoingToSleep.release();
3190 }
3191 if (mService.mShuttingDown) {
3192 mService.notifyAll();
3193 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003194 }
3195
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003196 boolean reportResumedActivityLocked(ActivityRecord r) {
3197 final ActivityStack stack = r.task.stack;
3198 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003199 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003200 }
3201 if (allResumedActivitiesComplete()) {
3202 ensureActivitiesVisibleLocked(null, 0);
3203 mWindowManager.executeAppTransition();
3204 return true;
3205 }
3206 return false;
3207 }
3208
Craig Mautner8d341ef2013-03-26 09:03:27 -07003209 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003210 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3211 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003212 int stackNdx = stacks.size() - 1;
3213 while (stackNdx >= 0) {
3214 stacks.get(stackNdx).handleAppCrashLocked(app);
3215 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003216 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003217 }
3218 }
3219
Jose Lima4b6c6692014-08-12 17:41:12 -07003220 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003221 final ActivityStack stack = r.task.stack;
3222 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003223 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3224 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003225 return false;
3226 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003227 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003228 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3229 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003230
3231 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003232 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003233 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003234 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003235 return true;
3236 }
3237
3238 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003239 if (visible && top.fullscreen) {
3240 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003241 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3242 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3243 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3244 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003245 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003246 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3247 // Only the activity set as currently visible behind should actively reset its
3248 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003249 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3250 "requestVisibleBehind: returning visible=" + visible
3251 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3252 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003253 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003254 }
3255
Jose Lima4b6c6692014-08-12 17:41:12 -07003256 stack.setVisibleBehindActivity(visible ? r : null);
3257 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003258 // Make the activity immediately above r opaque.
3259 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3260 if (next != null) {
3261 mService.convertFromTranslucent(next.appToken);
3262 }
3263 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003264 if (top.app != null && top.app.thread != null) {
3265 // Notify the top app of the change.
3266 try {
3267 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3268 } catch (RemoteException e) {
3269 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003270 }
3271 return true;
3272 }
3273
Craig Mautnerbb742462014-07-07 15:28:55 -07003274 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3275 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3276 r.mLaunchTaskBehind = false;
3277 final TaskRecord task = r.task;
3278 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003279 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003280 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003281 mWindowManager.setAppVisibility(r.appToken, false);
3282 }
3283
3284 void scheduleLaunchTaskBehindComplete(IBinder token) {
3285 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3286 }
3287
Craig Mautnerde4ef022013-04-07 19:01:33 -07003288 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003289 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003290 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3291 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003292 final int topStackNdx = stacks.size() - 1;
3293 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3294 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003295 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003296 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003297 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003298 }
3299
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003300 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3301 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3302 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3303 final int topStackNdx = stacks.size() - 1;
3304 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3305 final ActivityStack stack = stacks.get(stackNdx);
3306 stack.clearOtherAppTimeTrackers(except);
3307 }
3308 }
3309 }
3310
Craig Mautner8d341ef2013-03-26 09:03:27 -07003311 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
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 numStacks = stacks.size();
3315 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3316 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003317 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003318 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003319 }
3320 }
3321
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003322 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3323 // Examine all activities currently running in the process.
3324 TaskRecord firstTask = null;
3325 // Tasks is non-null only if two or more tasks are found.
3326 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003327 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3328 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003329 ActivityRecord r = app.activities.get(i);
3330 // First, if we find an activity that is in the process of being destroyed,
3331 // then we just aren't going to do anything for now; we want things to settle
3332 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003333 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003334 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003335 return;
3336 }
3337 // Don't consider any activies that are currently not in a state where they
3338 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003339 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3340 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003341 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003342 continue;
3343 }
3344 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003345 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003346 + " from " + r);
3347 if (firstTask == null) {
3348 firstTask = r.task;
3349 } else if (firstTask != r.task) {
3350 if (tasks == null) {
3351 tasks = new ArraySet<>();
3352 tasks.add(firstTask);
3353 }
3354 tasks.add(r.task);
3355 }
3356 }
3357 }
3358 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003359 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003360 return;
3361 }
3362 // If we have activities in multiple tasks that are in a position to be destroyed,
3363 // let's iterate through the tasks and release the oldest one.
3364 final int numDisplays = mActivityDisplays.size();
3365 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3366 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3367 // Step through all stacks starting from behind, to hit the oldest things first.
3368 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3369 final ActivityStack stack = stacks.get(stackNdx);
3370 // Try to release activities in this stack; if we manage to, we are done.
3371 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3372 return;
3373 }
3374 }
3375 }
3376 }
3377
Amith Yamasani37a40c22015-06-17 13:25:42 -07003378 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003379 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003380 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003381 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003382
Craig Mautner858d8a62013-04-23 17:08:34 -07003383 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003384 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3385 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003386 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003387 final ActivityStack stack = stacks.get(stackNdx);
3388 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003389 TaskRecord task = stack.topTask();
3390 if (task != null) {
3391 mWindowManager.moveTaskToTop(task.taskId);
3392 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003393 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003394 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003395
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003396 ActivityStack stack = getStack(restoreStackId);
3397 if (stack == null) {
3398 stack = mHomeStack;
3399 }
3400 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003401 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003402 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003403 TaskRecord task = stack.topTask();
3404 if (task != null) {
3405 mWindowManager.moveTaskToTop(task.taskId);
3406 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003407 } else {
3408 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003409 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003410 }
Craig Mautner93529a42013-10-04 15:03:13 -07003411 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003412 }
3413
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003414 /**
3415 * Add background users to send boot completed events to.
3416 * @param userId The user being started in the background
3417 * @param uss The state object for the user.
3418 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003419 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003420 mStartingBackgroundUsers.add(uss);
3421 }
3422
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003423 /** Checks whether the userid is a profile of the current user. */
3424 boolean isCurrentProfileLocked(int userId) {
3425 if (userId == mCurrentUser) return true;
3426 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3427 if (mService.mCurrentProfileIds[i] == userId) return true;
3428 }
3429 return false;
3430 }
3431
Craig Mautnerde4ef022013-04-07 19:01:33 -07003432 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003433 ArrayList<ActivityRecord> stops = null;
3434
3435 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003436 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3437 ActivityRecord s = mStoppingActivities.get(activityNdx);
3438 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003439 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003440 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3441 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003442 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003443 if (s.finishing) {
3444 // If this activity is finishing, it is sitting on top of
3445 // everyone else but we now know it is no longer needed...
3446 // so get rid of it. Otherwise, we need to go through the
3447 // normal flow and hide it once we determine that it is
3448 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003449 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003450 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003451 }
3452 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003453 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003454 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003455 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003456 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003457 }
3458 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003459 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003460 }
3461 }
3462
3463 return stops;
3464 }
3465
Craig Mautnercf910b02013-04-23 11:23:27 -07003466 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003467 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3468 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3469 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3470 final ActivityStack stack = stacks.get(stackNdx);
3471 final ActivityRecord r = stack.topRunningActivityLocked(null);
3472 final ActivityState state = r == null ? DESTROYED : r.state;
3473 if (isFrontStack(stack)) {
3474 if (r == null) Slog.e(TAG,
3475 "validateTop...: null top activity, stack=" + stack);
3476 else {
3477 final ActivityRecord pausing = stack.mPausingActivity;
3478 if (pausing != null && pausing == r) Slog.e(TAG,
3479 "validateTop...: top stack has pausing activity r=" + r
3480 + " state=" + state);
3481 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3482 "validateTop...: activity in front not resumed r=" + r
3483 + " state=" + state);
3484 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003485 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003486 final ActivityRecord resumed = stack.mResumedActivity;
3487 if (resumed != null && resumed == r) Slog.e(TAG,
3488 "validateTop...: back stack has resumed activity r=" + r
3489 + " state=" + state);
3490 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3491 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003492 }
3493 }
3494 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003495 }
3496
Craig Mautnere0570202015-05-13 13:06:11 -07003497 private String lockTaskModeToString() {
3498 switch (mLockTaskModeState) {
3499 case LOCK_TASK_MODE_LOCKED:
3500 return "LOCKED";
3501 case LOCK_TASK_MODE_PINNED:
3502 return "PINNED";
3503 case LOCK_TASK_MODE_NONE:
3504 return "NONE";
3505 default: return "unknown=" + mLockTaskModeState;
3506 }
3507 }
3508
Craig Mautner27084302013-03-25 08:05:25 -07003509 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003510 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003511 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003512 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3513 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3514 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003515 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003516 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3517 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3518 if (packages.size() > 0) {
3519 pw.println(" mLockTaskPackages (userId:packages)=");
3520 for (int i = 0; i < packages.size(); ++i) {
3521 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3522 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3523 }
3524 }
3525 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003526 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003527
Craig Mautner20e72272013-04-01 13:45:53 -07003528 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003529 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003530 }
3531
Dianne Hackborn390517b2013-05-30 15:03:32 -07003532 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3533 boolean needSep, String prefix) {
3534 if (activity != null) {
3535 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3536 if (needSep) {
3537 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003538 }
3539 pw.print(prefix);
3540 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003541 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003542 }
3543 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003544 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003545 }
3546
Craig Mautner8d341ef2013-03-26 09:03:27 -07003547 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3548 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003549 boolean printed = false;
3550 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003551 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3552 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003553 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003554 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003555 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003556 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003557 final ActivityStack stack = stacks.get(stackNdx);
3558 StringBuilder stackHeader = new StringBuilder(128);
3559 stackHeader.append(" Stack #");
3560 stackHeader.append(stack.mStackId);
3561 stackHeader.append(":");
3562 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3563 needSep, stackHeader.toString());
3564 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3565 !dumpAll, false, dumpPackage, true,
3566 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003567
Craig Mautner4a1cb222013-12-04 16:14:06 -08003568 needSep = printed;
3569 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3570 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003571 if (pr) {
3572 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003573 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003574 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003575 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3576 " mResumedActivity: ");
3577 if (pr) {
3578 printed = true;
3579 needSep = false;
3580 }
3581 if (dumpAll) {
3582 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3583 " mLastPausedActivity: ");
3584 if (pr) {
3585 printed = true;
3586 needSep = true;
3587 }
3588 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3589 needSep, " mLastNoHistoryActivity: ");
3590 }
3591 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003592 }
3593 }
3594
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003595 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3596 false, dumpPackage, true, " Activities waiting to finish:", null);
3597 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3598 false, dumpPackage, true, " Activities waiting to stop:", null);
3599 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3600 false, dumpPackage, true, " Activities waiting for another to become visible:",
3601 null);
3602 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3603 false, dumpPackage, true, " Activities waiting to sleep:", null);
3604 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3605 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003606
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003607 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003608 }
3609
Dianne Hackborn390517b2013-05-30 15:03:32 -07003610 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003611 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003612 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003613 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003614 String innerPrefix = null;
3615 String[] args = null;
3616 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003617 for (int i=list.size()-1; i>=0; i--) {
3618 final ActivityRecord r = list.get(i);
3619 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3620 continue;
3621 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003622 if (innerPrefix == null) {
3623 innerPrefix = prefix + " ";
3624 args = new String[0];
3625 }
3626 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003627 final boolean full = !brief && (complete || !r.isInHistory());
3628 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003629 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003630 needNL = false;
3631 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003632 if (header1 != null) {
3633 pw.println(header1);
3634 header1 = null;
3635 }
3636 if (header2 != null) {
3637 pw.println(header2);
3638 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003639 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003640 if (lastTask != r.task) {
3641 lastTask = r.task;
3642 pw.print(prefix);
3643 pw.print(full ? "* " : " ");
3644 pw.println(lastTask);
3645 if (full) {
3646 lastTask.dump(pw, prefix + " ");
3647 } else if (complete) {
3648 // Complete + brief == give a summary. Isn't that obvious?!?
3649 if (lastTask.intent != null) {
3650 pw.print(prefix); pw.print(" ");
3651 pw.println(lastTask.intent.toInsecureStringWithClip());
3652 }
3653 }
3654 }
3655 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3656 pw.print(" #"); pw.print(i); pw.print(": ");
3657 pw.println(r);
3658 if (full) {
3659 r.dump(pw, innerPrefix);
3660 } else if (complete) {
3661 // Complete + brief == give a summary. Isn't that obvious?!?
3662 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3663 if (r.app != null) {
3664 pw.print(innerPrefix); pw.println(r.app);
3665 }
3666 }
3667 if (client && r.app != null && r.app.thread != null) {
3668 // flush anything that is already in the PrintWriter since the thread is going
3669 // to write to the file descriptor directly
3670 pw.flush();
3671 try {
3672 TransferPipe tp = new TransferPipe();
3673 try {
3674 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3675 r.appToken, innerPrefix, args);
3676 // Short timeout, since blocking here can
3677 // deadlock with the application.
3678 tp.go(fd, 2000);
3679 } finally {
3680 tp.kill();
3681 }
3682 } catch (IOException e) {
3683 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3684 } catch (RemoteException e) {
3685 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3686 }
3687 needNL = true;
3688 }
3689 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003690 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003691 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003692
Craig Mautnerf3333272013-04-22 10:55:53 -07003693 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003694 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3695 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003696 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3697 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003698 }
3699
3700 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003701 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003702 }
3703
3704 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003705 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3706 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003707 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3708 }
3709
Craig Mautner05d29032013-05-03 13:40:13 -07003710 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003711 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3712 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3713 }
Craig Mautner05d29032013-05-03 13:40:13 -07003714 }
3715
Craig Mautner0eea92c2013-05-16 13:35:39 -07003716 void removeSleepTimeouts() {
3717 mSleepTimeout = false;
3718 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3719 }
3720
3721 final void scheduleSleepTimeout() {
3722 removeSleepTimeouts();
3723 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3724 }
3725
Craig Mautner4a1cb222013-12-04 16:14:06 -08003726 @Override
3727 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003728 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003729 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3730 }
3731
3732 @Override
3733 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003734 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003735 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3736 }
3737
3738 @Override
3739 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003740 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003741 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3742 }
3743
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003744 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003745 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003746 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003747 newDisplay = mActivityDisplays.get(displayId) == null;
3748 if (newDisplay) {
3749 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003750 if (activityDisplay.mDisplay == null) {
3751 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3752 return;
3753 }
Craig Mautner4504de52013-12-20 09:06:56 -08003754 mActivityDisplays.put(displayId, activityDisplay);
3755 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003756 }
Craig Mautner4504de52013-12-20 09:06:56 -08003757 if (newDisplay) {
3758 mWindowManager.onDisplayAdded(displayId);
3759 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003760 }
3761
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003762 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003763 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003764 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3765 if (activityDisplay != null) {
3766 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003767 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003768 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003769 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003770 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003771 }
3772 }
3773 mWindowManager.onDisplayRemoved(displayId);
3774 }
3775
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003776 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003777 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003778 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3779 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003780 // TODO: Update the bounds.
3781 }
3782 }
3783 mWindowManager.onDisplayChanged(displayId);
3784 }
3785
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003786 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003787 StackInfo info = new StackInfo();
3788 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3789 info.displayId = Display.DEFAULT_DISPLAY;
3790 info.stackId = stack.mStackId;
3791
3792 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3793 final int numTasks = tasks.size();
3794 int[] taskIds = new int[numTasks];
3795 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003796 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003797 for (int i = 0; i < numTasks; ++i) {
3798 final TaskRecord task = tasks.get(i);
3799 taskIds[i] = task.taskId;
3800 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3801 : task.realActivity != null ? task.realActivity.flattenToString()
3802 : task.getTopActivity() != null ? task.getTopActivity().packageName
3803 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003804 taskBounds[i] = new Rect();
3805 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003806 }
3807 info.taskIds = taskIds;
3808 info.taskNames = taskNames;
3809 return info;
3810 }
3811
3812 StackInfo getStackInfoLocked(int stackId) {
3813 ActivityStack stack = getStack(stackId);
3814 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003815 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003816 }
3817 return null;
3818 }
3819
3820 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003821 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003822 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3823 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003824 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003825 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003826 }
3827 }
3828 return list;
3829 }
3830
Craig Mautner15df08a2015-04-01 12:17:18 -07003831 TaskRecord getLockedTaskLocked() {
3832 final int top = mLockTaskModeTasks.size() - 1;
3833 if (top >= 0) {
3834 return mLockTaskModeTasks.get(top);
3835 }
3836 return null;
3837 }
3838
3839 boolean isLockedTask(TaskRecord task) {
3840 return mLockTaskModeTasks.contains(task);
3841 }
3842
3843 boolean isLastLockedTask(TaskRecord task) {
3844 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3845 }
3846
3847 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003848 if (!mLockTaskModeTasks.remove(task)) {
3849 return;
3850 }
3851 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3852 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003853 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003854 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3855 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003856 final Message lockTaskMsg = Message.obtain();
3857 lockTaskMsg.arg1 = task.userId;
3858 lockTaskMsg.what = LOCK_TASK_END_MSG;
3859 mHandler.sendMessage(lockTaskMsg);
3860 }
3861 }
3862
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003863 void showLockTaskToast() {
3864 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003865 }
3866
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003867 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3868 if (mLockTaskModeTasks.contains(task)) {
3869 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3870 }
3871 }
3872
Craig Mautner432f64e2015-05-20 14:59:57 -07003873 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3874 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003875 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003876 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003877 final TaskRecord lockedTask = getLockedTaskLocked();
3878 if (lockedTask != null) {
3879 removeLockedTaskLocked(lockedTask);
3880 if (!mLockTaskModeTasks.isEmpty()) {
3881 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003882 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3883 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003884 lockedTask.performClearTaskLocked();
3885 resumeTopActivitiesLocked();
3886 return;
3887 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003888 }
Craig Mautnere0570202015-05-13 13:06:11 -07003889 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3890 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003891 return;
3892 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003893
3894 // Should have already been checked, but do it again.
3895 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003896 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3897 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003898 return;
3899 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003900 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003901 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003902 return;
3903 }
3904
3905 if (mLockTaskModeTasks.isEmpty()) {
3906 // First locktask.
3907 final Message lockTaskMsg = Message.obtain();
3908 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3909 lockTaskMsg.arg1 = task.userId;
3910 lockTaskMsg.what = LOCK_TASK_START_MSG;
3911 lockTaskMsg.arg2 = lockTaskModeState;
3912 mHandler.sendMessage(lockTaskMsg);
3913 }
3914 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003915 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3916 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003917 mLockTaskModeTasks.remove(task);
3918 mLockTaskModeTasks.add(task);
3919
3920 if (task.mLockTaskUid == -1) {
3921 task.mLockTaskUid = task.mCallingUid;
3922 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003923
3924 if (andResume) {
3925 findTaskToMoveToFrontLocked(task, 0, null, reason);
3926 resumeTopActivitiesLocked();
3927 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003928 }
3929
3930 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003931 return isLockTaskModeViolation(task, false);
3932 }
3933
3934 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3935 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003936 return false;
3937 }
3938 final int lockTaskAuth = task.mLockTaskAuth;
3939 switch (lockTaskAuth) {
3940 case LOCK_TASK_AUTH_DONT_LOCK:
3941 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003942 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003943 case LOCK_TASK_AUTH_LAUNCHABLE:
3944 case LOCK_TASK_AUTH_WHITELISTED:
3945 return false;
3946 case LOCK_TASK_AUTH_PINNABLE:
3947 // Pinnable tasks can't be launched on top of locktask tasks.
3948 return !mLockTaskModeTasks.isEmpty();
3949 default:
3950 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3951 return true;
3952 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003953 }
3954
Craig Mautner15df08a2015-04-01 12:17:18 -07003955 void onLockTaskPackagesUpdatedLocked() {
3956 boolean didSomething = false;
3957 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3958 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003959 final boolean wasWhitelisted =
3960 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3961 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003962 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003963 final boolean isWhitelisted =
3964 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3965 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3966 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003967 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003968 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3969 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003970 removeLockedTaskLocked(lockedTask);
3971 lockedTask.performClearTaskLocked();
3972 didSomething = true;
3973 }
3974 }
3975 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3976 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3977 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3978 final ActivityStack stack = stacks.get(stackNdx);
3979 stack.onLockTaskPackagesUpdatedLocked();
3980 }
3981 }
Craig Mautnere0570202015-05-13 13:06:11 -07003982 final ActivityRecord r = topRunningActivityLocked();
3983 final TaskRecord task = r != null ? r.task : null;
3984 if (mLockTaskModeTasks.isEmpty() && task != null
3985 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3986 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003987 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3988 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3989 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3990 false);
3991 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003992 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003993 if (didSomething) {
3994 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003995 }
3996 }
3997
Benjamin Franz43261142015-02-11 15:59:44 +00003998 int getLockTaskModeState() {
3999 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004000 }
4001
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004002 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004003
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004004 public ActivityStackSupervisorHandler(Looper looper) {
4005 super(looper);
4006 }
4007
Craig Mautnerf3333272013-04-22 10:55:53 -07004008 void activityIdleInternal(ActivityRecord r) {
4009 synchronized (mService) {
4010 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4011 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004012 }
4013
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004014 @Override
4015 public void handleMessage(Message msg) {
4016 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004017 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004018 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4019 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004020 if (mService.mDidDexOpt) {
4021 mService.mDidDexOpt = false;
4022 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4023 nmsg.obj = msg.obj;
4024 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4025 return;
4026 }
4027 // We don't at this point know if the activity is fullscreen,
4028 // so we need to be conservative and assume it isn't.
4029 activityIdleInternal((ActivityRecord)msg.obj);
4030 } break;
4031 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004032 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004033 activityIdleInternal((ActivityRecord)msg.obj);
4034 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004035 case RESUME_TOP_ACTIVITY_MSG: {
4036 synchronized (mService) {
4037 resumeTopActivitiesLocked();
4038 }
4039 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004040 case SLEEP_TIMEOUT_MSG: {
4041 synchronized (mService) {
4042 if (mService.isSleepingOrShuttingDown()) {
4043 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4044 mSleepTimeout = true;
4045 checkReadyForSleepLocked();
4046 }
4047 }
4048 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004049 case LAUNCH_TIMEOUT_MSG: {
4050 if (mService.mDidDexOpt) {
4051 mService.mDidDexOpt = false;
4052 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4053 return;
4054 }
4055 synchronized (mService) {
4056 if (mLaunchingActivity.isHeld()) {
4057 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4058 if (VALIDATE_WAKE_LOCK_CALLER
4059 && Binder.getCallingUid() != Process.myUid()) {
4060 throw new IllegalStateException("Calling must be system uid");
4061 }
4062 mLaunchingActivity.release();
4063 }
4064 }
4065 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004066 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004067 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004068 } break;
4069 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004070 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004071 } break;
4072 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004073 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004074 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004075 case CONTAINER_CALLBACK_VISIBILITY: {
4076 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004077 final IActivityContainerCallback callback = container.mCallback;
4078 if (callback != null) {
4079 try {
4080 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4081 } catch (RemoteException e) {
4082 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004083 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004084 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004085 case LOCK_TASK_START_MSG: {
4086 // When lock task starts, we disable the status bars.
4087 try {
Jason Monk62515be2014-05-21 16:06:19 -04004088 if (mLockTaskNotify == null) {
4089 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004090 }
Jason Monk62515be2014-05-21 16:06:19 -04004091 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004092 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004093 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004094 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004095 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004096 flags = StatusBarManager.DISABLE_MASK
4097 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004098 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004099 flags = StatusBarManager.DISABLE_MASK
4100 & (~StatusBarManager.DISABLE_BACK)
4101 & (~StatusBarManager.DISABLE_HOME)
4102 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004103 }
4104 getStatusBarService().disable(flags, mToken,
4105 mService.mContext.getPackageName());
4106 }
4107 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004108 if (getDevicePolicyManager() != null) {
4109 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004110 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004111 }
justinzhang5286d3f2014-05-12 17:06:01 -04004112 } catch (RemoteException ex) {
4113 throw new RuntimeException(ex);
4114 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004115 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004116 case LOCK_TASK_END_MSG: {
4117 // When lock task ends, we enable the status bars.
4118 try {
Jason Monk62515be2014-05-21 16:06:19 -04004119 if (getStatusBarService() != null) {
4120 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4121 mService.mContext.getPackageName());
4122 }
4123 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004124 if (getDevicePolicyManager() != null) {
4125 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4126 msg.arg1);
4127 }
Jason Monk62515be2014-05-21 16:06:19 -04004128 if (mLockTaskNotify == null) {
4129 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4130 }
4131 mLockTaskNotify.show(false);
4132 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004133 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004134 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004135 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004136 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004137 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004138 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004139 new LockPatternUtils(mService.mContext)
4140 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004141 }
4142 } catch (SettingNotFoundException e) {
4143 // No setting, don't lock.
4144 }
justinzhang5286d3f2014-05-12 17:06:01 -04004145 } catch (RemoteException ex) {
4146 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004147 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004148 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004149 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004150 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004151 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4152 if (mLockTaskNotify == null) {
4153 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4154 }
4155 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4156 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004157 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4158 final ActivityContainer container = (ActivityContainer) msg.obj;
4159 final IActivityContainerCallback callback = container.mCallback;
4160 if (callback != null) {
4161 try {
4162 callback.onAllActivitiesComplete(container.asBinder());
4163 } catch (RemoteException e) {
4164 }
4165 }
4166 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004167 case LAUNCH_TASK_BEHIND_COMPLETE: {
4168 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004169 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004170 if (r != null) {
4171 handleLaunchTaskBehindCompleteLocked(r);
4172 }
4173 }
4174 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004175 }
4176 }
4177 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004178
Ying Wangb081a592014-04-22 15:20:16 -07004179 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004180 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004181 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004182 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004183 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004184 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004185 ActivityRecord mParentActivity = null;
4186 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004187
Craig Mautnere3a00d72014-04-16 08:31:19 -07004188 boolean mVisible = true;
4189
Craig Mautner4a1cb222013-12-04 16:14:06 -08004190 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004191 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004192
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004193 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4194 final static int CONTAINER_STATE_NO_SURFACE = 1;
4195 final static int CONTAINER_STATE_FINISHING = 2;
4196 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4197
4198 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004199 synchronized (mService) {
4200 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004201 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004202 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004203 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004204 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004205 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004206
Craig Mautnere0a38842013-12-16 16:14:02 -08004207 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004208 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Craig Mautner34b73df2014-01-12 21:11:08 -08004209 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08004210 mActivityDisplay = activityDisplay;
4211 mStack.mDisplayId = activityDisplay.mDisplayId;
4212 mStack.mStacks = activityDisplay.mStacks;
4213
4214 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08004215 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004216 }
4217
4218 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004219 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004220 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004221 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4222 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004223 return;
4224 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004225 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004226 }
4227 }
4228
4229 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004230 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004231 synchronized (mService) {
4232 if (mActivityDisplay != null) {
4233 return mActivityDisplay.mDisplayId;
4234 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004235 }
4236 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004237 }
4238
Jeff Brownca9bc702014-02-11 14:32:56 -08004239 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004240 public int getStackId() {
4241 synchronized (mService) {
4242 return mStackId;
4243 }
4244 }
4245
4246 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004247 public boolean injectEvent(InputEvent event) {
4248 final long origId = Binder.clearCallingIdentity();
4249 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004250 synchronized (mService) {
4251 if (mActivityDisplay != null) {
4252 return mInputManagerInternal.injectInputEvent(event,
4253 mActivityDisplay.mDisplayId,
4254 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4255 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004256 }
4257 return false;
4258 } finally {
4259 Binder.restoreCallingIdentity(origId);
4260 }
4261 }
4262
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004263 @Override
4264 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004265 synchronized (mService) {
4266 if (mContainerState == CONTAINER_STATE_FINISHING) {
4267 return;
4268 }
4269 mContainerState = CONTAINER_STATE_FINISHING;
4270
Craig Mautnerd163e752014-06-13 17:18:47 -07004271 long origId = Binder.clearCallingIdentity();
4272 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004273 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004274 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004275 } finally {
4276 Binder.restoreCallingIdentity(origId);
4277 }
4278 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004279 }
4280
Craig Mautner60257702014-09-17 15:02:33 -07004281 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004282 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004283 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004284 if (mActivityDisplay != null) {
4285 mActivityDisplay.detachActivitiesLocked(mStack);
4286 mActivityDisplay = null;
4287 mStack.mDisplayId = -1;
4288 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08004289 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004290 }
4291 }
4292
4293 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004294 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004295 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004296 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004297 Binder.getCallingUid(), mCurrentUser, false,
4298 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004299
Craig Mautnere0a38842013-12-16 16:14:02 -08004300 // TODO: Switch to user app stacks here.
4301 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004302 final Uri data = intent.getData();
4303 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4304 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004305 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004306 checkEmbeddedAllowedInner(userId, intent, mimeType);
4307
4308 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4309 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004310 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004311 }
4312
4313 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004314 public final int startActivityIntentSender(IIntentSender intentSender)
4315 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004316 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4317
4318 if (!(intentSender instanceof PendingIntentRecord)) {
4319 throw new IllegalArgumentException("Bad PendingIntent object");
4320 }
4321
Craig Mautnerb9168362015-02-26 20:40:19 -08004322 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004323 Binder.getCallingUid(), mCurrentUser, false,
4324 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004325
4326 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4327 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4328 pendingIntent.key.requestResolvedType);
4329
4330 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4331 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4332 }
4333
4334 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004335 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004336 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004337 throw new SecurityException(
4338 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4339 }
4340 }
4341
Craig Mautnerdf88d732014-01-27 09:21:32 -08004342 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004343 public IBinder asBinder() {
4344 return this;
4345 }
4346
Craig Mautner4504de52013-12-20 09:06:56 -08004347 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004348 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004349 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004350 }
4351
Craig Mautner4a1cb222013-12-04 16:14:06 -08004352 ActivityStackSupervisor getOuter() {
4353 return ActivityStackSupervisor.this;
4354 }
4355
Craig Mautnerd163e752014-06-13 17:18:47 -07004356 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004357 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004358 }
4359
4360 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004361 synchronized (mService) {
4362 if (mActivityDisplay != null) {
4363 mActivityDisplay.getBounds(outBounds);
4364 } else {
4365 outBounds.set(0, 0);
4366 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004367 }
4368 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004369
Craig Mautner6985bad2014-04-21 15:22:06 -07004370 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004371 void setVisible(boolean visible) {
4372 if (mVisible != visible) {
4373 mVisible = visible;
4374 if (mCallback != null) {
4375 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4376 0 /* unused */, this).sendToTarget();
4377 }
4378 }
4379 }
4380
Craig Mautner6985bad2014-04-21 15:22:06 -07004381 void setDrawn() {
4382 }
4383
Craig Mautner1b4bf852014-05-26 15:06:32 -07004384 // You can always start a new task on a regular ActivityStack.
4385 boolean isEligibleForNewTasks() {
4386 return true;
4387 }
4388
Craig Mautnerd163e752014-06-13 17:18:47 -07004389 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004390 detachLocked();
4391 deleteActivityContainer(this);
4392 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004393 }
4394
Craig Mautner34b73df2014-01-12 21:11:08 -08004395 @Override
4396 public String toString() {
4397 return mIdString + (mActivityDisplay == null ? "N" : "A");
4398 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004399 }
4400
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004401 private class VirtualActivityContainer extends ActivityContainer {
4402 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004403 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004404
4405 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4406 super(getNextStackId());
4407 mParentActivity = parent;
4408 mCallback = callback;
4409 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004410 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004411 }
4412
4413 @Override
4414 public void setSurface(Surface surface, int width, int height, int density) {
4415 super.setSurface(surface, width, height, density);
4416
4417 synchronized (mService) {
4418 final long origId = Binder.clearCallingIdentity();
4419 try {
4420 setSurfaceLocked(surface, width, height, density);
4421 } finally {
4422 Binder.restoreCallingIdentity(origId);
4423 }
4424 }
4425 }
4426
4427 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4428 if (mContainerState == CONTAINER_STATE_FINISHING) {
4429 return;
4430 }
4431 VirtualActivityDisplay virtualActivityDisplay =
4432 (VirtualActivityDisplay) mActivityDisplay;
4433 if (virtualActivityDisplay == null) {
4434 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004435 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004436 mActivityDisplay = virtualActivityDisplay;
4437 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4438 attachToDisplayLocked(virtualActivityDisplay);
4439 }
4440
4441 if (mSurface != null) {
4442 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004443 }
4444
Craig Mautner6985bad2014-04-21 15:22:06 -07004445 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004446 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004447 mStack.resumeTopActivityLocked(null);
4448 } else {
4449 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004450 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004451 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004452 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004453 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004454 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004455
Craig Mautnerd163e752014-06-13 17:18:47 -07004456 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004457
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004458 if (DEBUG_STACK) Slog.d(TAG_STACK,
4459 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004460 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004461
Craig Mautner6985bad2014-04-21 15:22:06 -07004462 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004463 boolean isAttachedLocked() {
4464 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004465 }
4466
4467 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004468 void setDrawn() {
4469 synchronized (mService) {
4470 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004471 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004472 }
4473 }
4474
Craig Mautner1b4bf852014-05-26 15:06:32 -07004475 // Never start a new task on an ActivityView if it isn't explicitly specified.
4476 @Override
4477 boolean isEligibleForNewTasks() {
4478 return false;
4479 }
4480
Craig Mautnerd163e752014-06-13 17:18:47 -07004481 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004482 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004483 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4484 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4485 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4486 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4487 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004488 }
4489 }
4490
Craig Mautner4a1cb222013-12-04 16:14:06 -08004491 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4492 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004493 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004494 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004495 int mDisplayId;
4496 Display mDisplay;
4497 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004498
Craig Mautner4a1cb222013-12-04 16:14:06 -08004499 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4500 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004501 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004502
Jose Lima4b6c6692014-08-12 17:41:12 -07004503 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004504
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004505 ActivityDisplay() {
4506 }
Craig Mautner4504de52013-12-20 09:06:56 -08004507
Craig Mautner1a70a162014-09-13 12:09:31 -07004508 // After instantiation, check that mDisplay is not null before using this. The alternative
4509 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004510 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004511 final Display display = mDisplayManager.getDisplay(displayId);
4512 if (display == null) {
4513 return;
4514 }
4515 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004516 }
4517
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004518 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004519 mDisplay = display;
4520 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004521 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004522 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004523
4524 void attachActivities(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004525 if (DEBUG_STACK) Slog.v(TAG_STACK,
4526 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004527 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004528 }
4529
Craig Mautnere0a38842013-12-16 16:14:02 -08004530 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004531 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004532 + " from displayId=" + mDisplayId);
4533 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004534 }
4535
4536 void getBounds(Point bounds) {
4537 mDisplay.getDisplayInfo(mDisplayInfo);
4538 bounds.x = mDisplayInfo.appWidth;
4539 bounds.y = mDisplayInfo.appHeight;
4540 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004541
Jose Lima4b6c6692014-08-12 17:41:12 -07004542 void setVisibleBehindActivity(ActivityRecord r) {
4543 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004544 }
4545
Jose Lima4b6c6692014-08-12 17:41:12 -07004546 boolean hasVisibleBehindActivity() {
4547 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004548 }
4549
Craig Mautner34b73df2014-01-12 21:11:08 -08004550 @Override
4551 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004552 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4553 }
4554 }
4555
4556 class VirtualActivityDisplay extends ActivityDisplay {
4557 VirtualDisplay mVirtualDisplay;
4558
Craig Mautner6985bad2014-04-21 15:22:06 -07004559 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004560 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004561 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4562 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4563 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4564 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004565
4566 init(mVirtualDisplay.getDisplay());
4567
4568 mWindowManager.handleDisplayAdded(mDisplayId);
4569 }
4570
4571 void setSurface(Surface surface) {
4572 if (mVirtualDisplay != null) {
4573 mVirtualDisplay.setSurface(surface);
4574 }
4575 }
4576
4577 @Override
4578 void detachActivitiesLocked(ActivityStack stack) {
4579 super.detachActivitiesLocked(stack);
4580 if (mVirtualDisplay != null) {
4581 mVirtualDisplay.release();
4582 mVirtualDisplay = null;
4583 }
4584 }
4585
4586 @Override
4587 public String toString() {
4588 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004589 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004590 }
Jose Lima58e66d62014-05-27 20:00:27 -07004591
4592 private boolean isLeanbackOnlyDevice() {
4593 boolean onLeanbackOnly = false;
4594 try {
4595 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4596 PackageManager.FEATURE_LEANBACK_ONLY);
4597 } catch (RemoteException e) {
4598 // noop
4599 }
4600
4601 return onLeanbackOnly;
4602 }
Craig Mautner27084302013-03-25 08:05:25 -07004603}