blob: 4a0fd89413992ab7a0c0b8d58dd1a750074c3f7f [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Wale Ogunwale868a5e12015-08-02 16:19:20 -070020import static android.app.ActivityManager.*;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070021import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
22import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070023import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
24import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070025import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070026import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080027import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070028import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070029import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
30import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
31import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070032import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070033import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
34import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010035import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070036import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
37import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070038
Svetoslav7008b512015-06-24 18:47:07 -070039import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070040import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080042import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.app.ActivityOptions;
44import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070045import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080046import android.app.IActivityContainer;
47import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070048import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070049import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070051import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070052import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070053import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070054import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040055import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040056import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070058import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070059import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070060import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070062import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070063import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070064import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.content.pm.PackageManager;
66import android.content.pm.ResolveInfo;
67import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080068import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080069import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080070import android.hardware.display.DisplayManager;
71import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080072import android.hardware.display.DisplayManagerGlobal;
73import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080074import android.hardware.input.InputManager;
75import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080076import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070077import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070078import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070079import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070080import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070081import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070082import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070083import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070084import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070085import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070086import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070087import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040088import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070089import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070090import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070091import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070092import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070093import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040094import android.provider.Settings;
95import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070096import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070097import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070098import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070099import android.util.EventLog;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700100import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700101import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800102import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700103
Craig Mautner4a1cb222013-12-04 16:14:06 -0800104import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800105import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800106import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800107import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800108import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700109import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700110import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800111import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700112import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400113import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700114import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400115import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800116import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700117import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700118import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700119
justinzhang5286d3f2014-05-12 17:06:01 -0400120
Craig Mautner8d341ef2013-03-26 09:03:27 -0700121import java.io.FileDescriptor;
122import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700123import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700124import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700125import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700126import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700127import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700128
Craig Mautner4a1cb222013-12-04 16:14:06 -0800129public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800130 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700131 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700132 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700133 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700134 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700135 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700136 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700137 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700138 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
139 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700140 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700141 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700142 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
143 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700144 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700145 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800146
Craig Mautnerf3333272013-04-22 10:55:53 -0700147 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700148 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700149
Craig Mautner0eea92c2013-05-16 13:35:39 -0700150 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700151 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700152
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700153 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700154 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700155
Craig Mautner05d29032013-05-03 13:40:13 -0700156 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
157 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
158 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700159 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700160 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800161 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
162 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
163 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700164 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400165 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
166 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700167 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700168 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700169 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800170
Craig Mautner4504de52013-12-20 09:06:56 -0800171 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700172
Jason Monk62515be2014-05-21 16:06:19 -0400173 private static final String LOCK_TASK_TAG = "Lock-to-App";
174
Svetoslav7008b512015-06-24 18:47:07 -0700175 // Activity actions an app cannot start if it uses a permission which is not granted.
176 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
177 new ArrayMap<>();
178 static {
179 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
180 Manifest.permission.CAMERA);
181 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
182 Manifest.permission.CAMERA);
183 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
184 Manifest.permission.CALL_PHONE);
185 }
186
Svet Ganov99b60432015-06-27 13:15:22 -0700187 /** Action restriction: launching the activity is not restricted. */
188 private static final int ACTIVITY_RESTRICTION_NONE = 0;
189 /** Action restriction: launching the activity is restricted by a permission. */
190 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
191 /** Action restriction: launching the activity is restricted by an app op. */
192 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700193
justinzhang5286d3f2014-05-12 17:06:01 -0400194 /** Status Bar Service **/
195 private IBinder mToken = new Binder();
196 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400197 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400198
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700199 // For debugging to make sure the caller when acquiring/releasing our
200 // wake lock is the system process.
201 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700202
Craig Mautner27084302013-03-25 08:05:25 -0700203 final ActivityManagerService mService;
204
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800205 private final RecentTasks mRecentTasks;
206
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700207 final ActivityStackSupervisorHandler mHandler;
208
209 /** Short cut */
210 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800211 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700212
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700213 /** Counter for next free stack ID to use for dynamic activity stacks. */
214 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700215
216 /** Task identifier that activities are currently being started in. Incremented each time a
217 * new task is created. */
218 private int mCurTaskId = 0;
219
Craig Mautner2420ead2013-04-01 17:13:20 -0700220 /** The current user */
221 private int mCurrentUser;
222
Craig Mautnere0a38842013-12-16 16:14:02 -0800223 /** The stack containing the launcher app. Assumed to always be attached to
224 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700225 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700226
Craig Mautnere0a38842013-12-16 16:14:02 -0800227 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700228 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700229
Craig Mautner4a1cb222013-12-04 16:14:06 -0800230 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
231 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800232 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800233 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700234
235 /** List of activities that are waiting for a new activity to become visible before completing
236 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800237 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700238
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700239 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800240 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700241
242 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800243 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700244
Craig Mautnerde4ef022013-04-07 19:01:33 -0700245 /** List of activities that are ready to be stopped, but waiting for the next activity to
246 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800247 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700248
Craig Mautnerf3333272013-04-22 10:55:53 -0700249 /** List of activities that are ready to be finished, but waiting for the previous activity to
250 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800251 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700252
Craig Mautner0eea92c2013-05-16 13:35:39 -0700253 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800254 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700255
Craig Mautnerf3333272013-04-22 10:55:53 -0700256 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700257 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700258
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700259 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700260 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700261
Craig Mautnerde4ef022013-04-07 19:01:33 -0700262 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
263 * is being brought in front of us. */
264 boolean mUserLeaving = false;
265
Craig Mautner0eea92c2013-05-16 13:35:39 -0700266 /** Set when we have taken too long waiting to go to sleep. */
267 boolean mSleepTimeout = false;
268
Jose Lima58e66d62014-05-27 20:00:27 -0700269 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
270 * setWindowManager is called. **/
271 private boolean mLeanbackOnlyDevice;
272
Craig Mautner0eea92c2013-05-16 13:35:39 -0700273 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700274 * We don't want to allow the device to go to sleep while in the process
275 * of launching an activity. This is primarily to allow alarm intent
276 * receivers to launch an activity and get that to run before the device
277 * goes back to sleep.
278 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700279 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700280
281 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700282 * Set when the system is going to sleep, until we have
283 * successfully paused the current activity and released our wake lock.
284 * At that point the system is allowed to actually sleep.
285 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700286 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700287
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700288 /** Stack id of the front stack when user switched, indexed by userId. */
289 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700290
Craig Mautner4504de52013-12-20 09:06:56 -0800291 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800292 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700293 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800294
295 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700296 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800297
Jeff Brownca9bc702014-02-11 14:32:56 -0800298 InputManagerInternal mInputManagerInternal;
299
Craig Mautner15df08a2015-04-01 12:17:18 -0700300 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
301 * may be finished until there is only one entry left. If this is empty the system is not
302 * in lockTask mode. */
303 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000304 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700305 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
306 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000307 */
308 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400309 /**
310 * Notifies the user when entering/exiting lock-task.
311 */
312 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800313
Craig Mautner15df08a2015-04-01 12:17:18 -0700314 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700315
Craig Mautner42d04db2014-11-06 12:13:23 -0800316 /** Used to keep resumeTopActivityLocked() from being entered recursively */
317 boolean inResumeTopActivity;
318
Craig Mautneree36c772014-07-16 14:56:05 -0700319 /**
320 * Description of a request to start a new activity, which has been held
321 * due to app switches being disabled.
322 */
323 static class PendingActivityLaunch {
324 final ActivityRecord r;
325 final ActivityRecord sourceRecord;
326 final int startFlags;
327 final ActivityStack stack;
328
329 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
330 int _startFlags, ActivityStack _stack) {
331 r = _r;
332 sourceRecord = _sourceRecord;
333 startFlags = _startFlags;
334 stack = _stack;
335 }
336 }
337
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800338 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700339 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800340 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700341 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
342 }
343
344 /**
345 * At the time when the constructor runs, the power manager has not yet been
346 * initialized. So we initialize our wakelocks afterwards.
347 */
348 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800349 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700350 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700351 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700352 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700353 }
354
justinzhang5286d3f2014-05-12 17:06:01 -0400355 // This function returns a IStatusBarService. The value is from ServiceManager.
356 // getService and is cached.
357 private IStatusBarService getStatusBarService() {
358 synchronized (mService) {
359 if (mStatusBarService == null) {
360 mStatusBarService = IStatusBarService.Stub.asInterface(
361 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
362 if (mStatusBarService == null) {
363 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
364 }
365 }
366 return mStatusBarService;
367 }
368 }
369
Jason Monk35c62a42014-06-17 10:24:47 -0400370 private IDevicePolicyManager getDevicePolicyManager() {
371 synchronized (mService) {
372 if (mDevicePolicyManager == null) {
373 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
374 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
375 if (mDevicePolicyManager == null) {
376 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
377 }
378 }
379 return mDevicePolicyManager;
380 }
381 }
382
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700383 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800384 synchronized (mService) {
385 mWindowManager = wm;
386
387 mDisplayManager =
388 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
389 mDisplayManager.registerDisplayListener(this, null);
390
391 Display[] displays = mDisplayManager.getDisplays();
392 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
393 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800394 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700395 if (activityDisplay.mDisplay == null) {
396 throw new IllegalStateException("Default Display does not exist");
397 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800398 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800399 }
400
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700401 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800402 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800403
404 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700405
406 // Initialize this here, now that we can get a valid reference to PackageManager.
407 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800408 }
Craig Mautner27084302013-03-25 08:05:25 -0700409 }
410
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200411 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700412 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200413 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700414 }
415
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700416 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800417 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700418 }
419
Craig Mautnerde4ef022013-04-07 19:01:33 -0700420 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800421 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700422 }
423
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800424 /** Top of all visible stacks is/should always be equal to the focused stack.
425 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
426 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700427 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700428 if (stack == null) {
429 return false;
430 }
431
Craig Mautnerdf88d732014-01-27 09:21:32 -0800432 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
433 if (parent != null) {
434 stack = parent.task.stack;
435 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800436 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700437 }
438
Craig Mautner299f9602015-01-26 09:47:33 -0800439 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800440 moveHomeStack(toFront, reason, null);
441 }
442
443 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800444 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800445 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800446 if (topNdx <= 0) {
447 return;
448 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700449
450 // The home stack should either be at the top or bottom of the stack list.
451 if ((toFront && (stacks.get(topNdx) != mHomeStack))
452 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700453 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700454 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
455 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800456 stacks.remove(mHomeStack);
457 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700458 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800459
460 if (lastFocusedStack != null) {
461 mLastFocusedStack = lastFocusedStack;
462 }
463 mFocusedStack = stacks.get(topNdx);
464
Craig Mautnerde313752015-01-22 14:28:03 -0800465 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
466 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800467 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800468
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800469 if (mService.mBooting || !mService.mBooted) {
470 final ActivityRecord r = topRunningActivityLocked();
471 if (r != null && r.idle) {
472 checkFinishBootingLocked();
473 }
474 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700475 }
476
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700477 /** Returns true if the focus activity was adjusted to the home stack top activity. */
478 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700479 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
480 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700481 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700482 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700483
Craig Mautner84984fa2014-06-19 11:19:20 -0700484 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700485
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700486 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700487 if (top == null) {
488 return false;
489 }
490 mService.setFocusedActivityLocked(top, reason);
491 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700492 }
493
Craig Mautner299f9602015-01-26 09:47:33 -0800494 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700495 if (!mService.mBooting && !mService.mBooted) {
496 // Not ready yet!
497 return false;
498 }
499
Craig Mautner84984fa2014-06-19 11:19:20 -0700500 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
501 mWindowManager.showRecentApps();
502 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700503 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700504
Craig Mautner84984fa2014-06-19 11:19:20 -0700505 if (prev != null) {
506 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
507 }
508
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700509 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
510 ActivityRecord r = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700511 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800512 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700513 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700514 }
Craig Mautner299f9602015-01-26 09:47:33 -0800515 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700516 }
517
Craig Mautner8d341ef2013-03-26 09:03:27 -0700518 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700519 return anyTaskForIdLocked(id, true);
520 }
521
522 /**
523 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
524 * @param id Id of the task we would like returned.
525 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
526 * restore the task from recents to the active list.
527 */
528 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800529 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800530 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800531 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800532 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
533 ActivityStack stack = stacks.get(stackNdx);
534 TaskRecord task = stack.taskForIdLocked(id);
535 if (task != null) {
536 return task;
537 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700538 }
539 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800540
541 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700542 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800543 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800544 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700545 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800546 return null;
547 }
548
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700549 if (!restoreFromRecents) {
550 return task;
551 }
552
Wale Ogunwale7de05352014-12-12 15:21:33 -0800553 if (!restoreRecentTaskLocked(task)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700554 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
555 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800556 return null;
557 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700558 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800559 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700560 }
561
Craig Mautner6170f732013-04-02 13:05:23 -0700562 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800563 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800564 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800565 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800566 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
567 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
568 if (r != null) {
569 return r;
570 }
Craig Mautner6170f732013-04-02 13:05:23 -0700571 }
572 }
573 return null;
574 }
575
Craig Mautneref73ee12014-04-23 11:45:37 -0700576 void setNextTaskId(int taskId) {
577 if (taskId > mCurTaskId) {
578 mCurTaskId = taskId;
579 }
580 }
581
Craig Mautner8d341ef2013-03-26 09:03:27 -0700582 int getNextTaskId() {
583 do {
584 mCurTaskId++;
585 if (mCurTaskId <= 0) {
586 mCurTaskId = 1;
587 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700588 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700589 return mCurTaskId;
590 }
591
Craig Mautnerde4ef022013-04-07 19:01:33 -0700592 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800593 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700594 if (stack == null) {
595 return null;
596 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700597 ActivityRecord resumedActivity = stack.mResumedActivity;
598 if (resumedActivity == null || resumedActivity.app == null) {
599 resumedActivity = stack.mPausingActivity;
600 if (resumedActivity == null || resumedActivity.app == null) {
601 resumedActivity = stack.topRunningActivityLocked(null);
602 }
603 }
604 return resumedActivity;
605 }
606
Dianne Hackbornff072722014-09-24 10:56:28 -0700607 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700608 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800609 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800610 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
611 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800612 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
613 final ActivityStack stack = stacks.get(stackNdx);
614 if (!isFrontStack(stack)) {
615 continue;
616 }
617 ActivityRecord hr = stack.topRunningActivityLocked(null);
618 if (hr != null) {
619 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
620 && processName.equals(hr.processName)) {
621 try {
George Mount2c92c972014-03-20 09:38:23 -0700622 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800623 didSomething = true;
624 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700625 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800626 Slog.w(TAG, "Exception in new application when starting activity "
627 + hr.intent.getComponent().flattenToShortString(), e);
628 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700629 }
Craig Mautner20e72272013-04-01 13:45:53 -0700630 }
Craig Mautner20e72272013-04-01 13:45:53 -0700631 }
632 }
633 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700634 if (!didSomething) {
635 ensureActivitiesVisibleLocked(null, 0);
636 }
Craig Mautner20e72272013-04-01 13:45:53 -0700637 return didSomething;
638 }
639
640 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800641 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
642 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800643 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
644 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800645 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800646 continue;
647 }
648 final ActivityRecord resumedActivity = stack.mResumedActivity;
649 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700650 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800651 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800652 return false;
653 }
Craig Mautner20e72272013-04-01 13:45:53 -0700654 }
655 }
656 return true;
657 }
658
Craig Mautnerde4ef022013-04-07 19:01:33 -0700659 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800660 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
661 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800662 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
663 final ActivityStack stack = stacks.get(stackNdx);
664 if (isFrontStack(stack)) {
665 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700666 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800667 return false;
668 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700669 }
670 }
671 }
672 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700673 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800674 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
675 mLastFocusedStack + " to=" + mFocusedStack);
676 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700677 return true;
678 }
679
680 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800681 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800682 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
683 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800684 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
685 final ActivityStack stack = stacks.get(stackNdx);
686 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800687 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700688 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800689 return false;
690 }
691 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800692 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700693 }
694 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800695 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700696 }
697
Craig Mautner2acc3892013-09-23 10:28:14 -0700698 /**
699 * Pause all activities in either all of the stacks or just the back stacks.
700 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700701 * @return true if any activity was paused as a result of this call.
702 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700703 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700704 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800705 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
706 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800707 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
708 final ActivityStack stack = stacks.get(stackNdx);
709 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700710 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800711 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700712 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
713 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800714 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700715 }
716 }
717 return someActivityPaused;
718 }
719
Craig Mautnerde4ef022013-04-07 19:01:33 -0700720 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700721 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800722 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
723 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800724 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
725 final ActivityStack stack = stacks.get(stackNdx);
726 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700727 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800728 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700729 Slog.d(TAG_STATES,
730 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800731 pausing = false;
732 } else {
733 return false;
734 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700735 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700736 }
737 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700738 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700739 }
740
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700741 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
742 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500743 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800744 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
745 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
746 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
747 final ActivityStack stack = stacks.get(stackNdx);
748 if (stack.mResumedActivity != null &&
749 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700750 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800751 }
752 }
753 }
754 }
755
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700756 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700757 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700758 }
759
760 void sendWaitingVisibleReportLocked(ActivityRecord r) {
761 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700762 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700763 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700764 if (w.who == null) {
765 changed = true;
766 w.timeout = false;
767 if (r != null) {
768 w.who = new ComponentName(r.info.packageName, r.info.name);
769 }
770 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
771 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700772 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700773 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700774 if (changed) {
775 mService.notifyAll();
776 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700777 }
778
779 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
780 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700781 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700782 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700783 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700784 if (w.who == null) {
785 changed = true;
786 w.timeout = timeout;
787 if (r != null) {
788 w.who = new ComponentName(r.info.packageName, r.info.name);
789 }
790 w.thisTime = thisTime;
791 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700792 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700793 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700794 if (changed) {
795 mService.notifyAll();
796 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700797 }
798
Craig Mautner29219d92013-04-16 20:19:12 -0700799 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800800 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700801 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
802 if (r != null) {
803 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700804 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700805
Craig Mautner4a1cb222013-12-04 16:14:06 -0800806 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800807 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800808 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
809 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700810 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700811 r = stack.topRunningActivityLocked(null);
812 if (r != null) {
813 return r;
814 }
815 }
816 }
817 return null;
818 }
819
Dianne Hackborn09233282014-04-30 11:33:59 -0700820 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700821 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800822 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
823 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800824 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800825 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800826 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800827 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
828 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700829 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800830 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700831 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700832 }
833 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700834
835 // The lists are already sorted from most recent to oldest. Just pull the most recent off
836 // each list and add it to list. Stop when all lists are empty or maxNum reached.
837 while (maxNum > 0) {
838 long mostRecentActiveTime = Long.MIN_VALUE;
839 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800840 final int numTaskLists = runningTaskLists.size();
841 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
842 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700843 if (!stackTaskList.isEmpty()) {
844 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
845 if (lastActiveTime > mostRecentActiveTime) {
846 mostRecentActiveTime = lastActiveTime;
847 selectedStackList = stackTaskList;
848 }
849 }
850 }
851 if (selectedStackList != null) {
852 list.add(selectedStackList.remove(0));
853 --maxNum;
854 } else {
855 break;
856 }
857 }
Craig Mautner20e72272013-04-01 13:45:53 -0700858 }
859
Craig Mautner23ac33b2013-04-01 16:26:35 -0700860 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700861 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700862 // Collect information about the target of the Intent.
863 ActivityInfo aInfo;
864 try {
865 ResolveInfo rInfo =
866 AppGlobals.getPackageManager().resolveIntent(
867 intent, resolvedType,
868 PackageManager.MATCH_DEFAULT_ONLY
869 | ActivityManagerService.STOCK_PM_FLAGS, userId);
870 aInfo = rInfo != null ? rInfo.activityInfo : null;
871 } catch (RemoteException e) {
872 aInfo = null;
873 }
874
875 if (aInfo != null) {
876 // Store the found target back into the intent, because now that
877 // we have it we never want to do this again. For example, if the
878 // user navigates back to this point in the history, we should
879 // always restart the exact same activity.
880 intent.setComponent(new ComponentName(
881 aInfo.applicationInfo.packageName, aInfo.name));
882
883 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700884 if (!aInfo.processName.equals("system")) {
885 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700886 mService.setDebugApp(aInfo.processName, true, false);
887 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700888
Man Caocfa78b22015-06-11 20:14:34 -0700889 if ((startFlags & ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700890 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
891 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700892
Man Caocfa78b22015-06-11 20:14:34 -0700893 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
894 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
895 }
896
897 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700898 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700899 }
900 }
901 }
902 return aInfo;
903 }
904
Craig Mautner299f9602015-01-26 09:47:33 -0800905 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
906 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700907 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
908 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
909 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
910 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700911 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
912 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700913 null /* outActivity */, null /* container */, null /* inTask */);
914 if (inResumeTopActivity) {
915 // If we are in resume section already, home activity will be initialized, but not
916 // resumed (to avoid recursive resume) and will stay that way until something pokes it
917 // again. We need to schedule another resume.
918 scheduleResumeTopActivities();
919 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700920 }
921
Craig Mautner23ac33b2013-04-01 16:26:35 -0700922 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700923 String callingPackage, Intent intent, String resolvedType,
924 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700925 IBinder resultTo, String resultWho, int requestCode, int startFlags,
926 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700927 Bundle options, boolean ignoreTargetSecurity, int userId,
928 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700929 // Refuse possible leaked file descriptors
930 if (intent != null && intent.hasFileDescriptors()) {
931 throw new IllegalArgumentException("File descriptors passed in Intent");
932 }
933 boolean componentSpecified = intent.getComponent() != null;
934
935 // Don't modify the client's object!
936 intent = new Intent(intent);
937
938 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700939 ActivityInfo aInfo =
940 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700941
Craig Mautnere0a38842013-12-16 16:14:02 -0800942 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700943 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800944 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700945 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800946 // Cannot start a child activity if the parent is not resumed.
947 return ActivityManager.START_CANCELED;
948 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700949 final int realCallingPid = Binder.getCallingPid();
950 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700951 int callingPid;
952 if (callingUid >= 0) {
953 callingPid = -1;
954 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700955 callingPid = realCallingPid;
956 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700957 } else {
958 callingPid = callingUid = -1;
959 }
960
Craig Mautnere0a38842013-12-16 16:14:02 -0800961 final ActivityStack stack;
962 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800963 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800964 } else {
965 stack = container.mStack;
966 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800967 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700968 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700969 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700970
971 final long origId = Binder.clearCallingIdentity();
972
973 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800974 (aInfo.applicationInfo.privateFlags
975 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700976 // This may be a heavy-weight process! Check to see if we already
977 // have another, different heavy-weight process running.
978 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
979 if (mService.mHeavyWeightProcess != null &&
980 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
981 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700982 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700983 if (caller != null) {
984 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
985 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700986 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700987 } else {
988 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700989 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700990 + intent.toString());
991 ActivityOptions.abort(options);
992 return ActivityManager.START_PERMISSION_DENIED;
993 }
994 }
995
996 IIntentSender target = mService.getIntentSenderLocked(
997 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700998 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700999 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1000 | PendingIntent.FLAG_ONE_SHOT, null);
1001
1002 Intent newIntent = new Intent();
1003 if (requestCode >= 0) {
1004 // Caller is requesting a result.
1005 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1006 }
1007 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1008 new IntentSender(target));
1009 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1010 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1011 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1012 hist.packageName);
1013 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1014 hist.task.taskId);
1015 }
1016 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1017 aInfo.packageName);
1018 newIntent.setFlags(intent.getFlags());
1019 newIntent.setClassName("android",
1020 HeavyWeightSwitcherActivity.class.getName());
1021 intent = newIntent;
1022 resolvedType = null;
1023 caller = null;
1024 callingUid = Binder.getCallingUid();
1025 callingPid = Binder.getCallingPid();
1026 componentSpecified = true;
1027 try {
1028 ResolveInfo rInfo =
1029 AppGlobals.getPackageManager().resolveIntent(
1030 intent, null,
1031 PackageManager.MATCH_DEFAULT_ONLY
1032 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1033 aInfo = rInfo != null ? rInfo.activityInfo : null;
1034 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1035 } catch (RemoteException e) {
1036 aInfo = null;
1037 }
1038 }
1039 }
1040 }
1041
Dianne Hackborn91097de2014-04-04 18:02:06 -07001042 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1043 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001044 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001045 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001046 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001047
Craig Mautner85c11a82014-07-17 12:38:18 -07001048 Binder.restoreCallingIdentity(origId);
1049
Craig Mautnerde4ef022013-04-07 19:01:33 -07001050 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001051 // If the caller also wants to switch to a new configuration,
1052 // do so now. This allows a clean switch, as we are waiting
1053 // for the current activity to pause (so we will not destroy
1054 // it), and have not yet started the next activity.
1055 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1056 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001057 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001058 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001059 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001060 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001061 }
1062
Craig Mautner23ac33b2013-04-01 16:26:35 -07001063 if (outResult != null) {
1064 outResult.result = res;
1065 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001066 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001067 do {
1068 try {
1069 mService.wait();
1070 } catch (InterruptedException e) {
1071 }
1072 } while (!outResult.timeout && outResult.who == null);
1073 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001074 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001075 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001076 outResult.timeout = false;
1077 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1078 outResult.totalTime = 0;
1079 outResult.thisTime = 0;
1080 } else {
1081 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001082 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001083 do {
1084 try {
1085 mService.wait();
1086 } catch (InterruptedException e) {
1087 }
1088 } while (!outResult.timeout && outResult.who == null);
1089 }
1090 }
1091 }
1092
1093 return res;
1094 }
1095 }
1096
1097 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1098 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1099 Bundle options, int userId) {
1100 if (intents == null) {
1101 throw new NullPointerException("intents is null");
1102 }
1103 if (resolvedTypes == null) {
1104 throw new NullPointerException("resolvedTypes is null");
1105 }
1106 if (intents.length != resolvedTypes.length) {
1107 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1108 }
1109
Craig Mautner23ac33b2013-04-01 16:26:35 -07001110
1111 int callingPid;
1112 if (callingUid >= 0) {
1113 callingPid = -1;
1114 } else if (caller == null) {
1115 callingPid = Binder.getCallingPid();
1116 callingUid = Binder.getCallingUid();
1117 } else {
1118 callingPid = callingUid = -1;
1119 }
1120 final long origId = Binder.clearCallingIdentity();
1121 try {
1122 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001123 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001124 for (int i=0; i<intents.length; i++) {
1125 Intent intent = intents[i];
1126 if (intent == null) {
1127 continue;
1128 }
1129
1130 // Refuse possible leaked file descriptors
1131 if (intent != null && intent.hasFileDescriptors()) {
1132 throw new IllegalArgumentException("File descriptors passed in Intent");
1133 }
1134
1135 boolean componentSpecified = intent.getComponent() != null;
1136
1137 // Don't modify the client's object!
1138 intent = new Intent(intent);
1139
1140 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001141 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001142 // TODO: New, check if this is correct
1143 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1144
1145 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001146 (aInfo.applicationInfo.privateFlags
1147 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001148 throw new IllegalArgumentException(
1149 "FLAG_CANT_SAVE_STATE not supported here");
1150 }
1151
1152 Bundle theseOptions;
1153 if (options != null && i == intents.length-1) {
1154 theseOptions = options;
1155 } else {
1156 theseOptions = null;
1157 }
Craig Mautner6170f732013-04-02 13:05:23 -07001158 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001159 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1160 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001161 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001162 if (res < 0) {
1163 return res;
1164 }
1165
1166 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1167 }
1168 }
1169 } finally {
1170 Binder.restoreCallingIdentity(origId);
1171 }
1172
1173 return ActivityManager.START_SUCCESS;
1174 }
1175
Craig Mautner2420ead2013-04-01 17:13:20 -07001176 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001177 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001178 throws RemoteException {
1179
Craig Mautner2568c3a2015-03-26 14:22:34 -07001180 if (andResume) {
1181 r.startFreezingScreenLocked(app, 0);
1182 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001183
Craig Mautner2568c3a2015-03-26 14:22:34 -07001184 // schedule launch ticks to collect information about slow apps.
1185 r.startLaunchTickingLocked();
1186 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001187
1188 // Have the window manager re-evaluate the orientation of
1189 // the screen based on the new activity order. Note that
1190 // as a result of this, it can call back into the activity
1191 // manager with a new orientation. We don't care about that,
1192 // because the activity is not currently running so we are
1193 // just restarting it anyway.
1194 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001195 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001196 mService.mConfiguration,
1197 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001198 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001199 }
1200
1201 r.app = app;
1202 app.waitingToKill = null;
1203 r.launchCount++;
1204 r.lastLaunchTime = SystemClock.uptimeMillis();
1205
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001206 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001207
1208 int idx = app.activities.indexOf(r);
1209 if (idx < 0) {
1210 app.activities.add(r);
1211 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001212 mService.updateLruProcessLocked(app, true, null);
1213 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001214
Craig Mautner15df08a2015-04-01 12:17:18 -07001215 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001216 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1217 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001218 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001219 }
1220
1221 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001222 try {
1223 if (app.thread == null) {
1224 throw new RemoteException();
1225 }
1226 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001227 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001228 if (andResume) {
1229 results = r.results;
1230 newIntents = r.newIntents;
1231 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001232 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1233 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1234 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001235 if (andResume) {
1236 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1237 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001238 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001239 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001240 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001241 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001242 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001243 }
1244 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1245 r.sleeping = false;
1246 r.forceNewConfig = false;
1247 mService.showAskCompatModeDialogLocked(r);
1248 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001249 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001250 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1251 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1252 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001253 final String profileFile = mService.mProfileFile;
1254 if (profileFile != null) {
1255 ParcelFileDescriptor profileFd = mService.mProfileFd;
1256 if (profileFd != null) {
1257 try {
1258 profileFd = profileFd.dup();
1259 } catch (IOException e) {
1260 if (profileFd != null) {
1261 try {
1262 profileFd.close();
1263 } catch (IOException o) {
1264 }
1265 profileFd = null;
1266 }
1267 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001268 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001269
1270 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1271 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001272 }
1273 }
1274 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001275
Craig Mautner2568c3a2015-03-26 14:22:34 -07001276 if (andResume) {
1277 app.hasShownUi = true;
1278 app.pendingUiClean = true;
1279 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001280 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001281 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001282 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001283 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001284 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001285 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001286
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001287 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001288 // This may be a heavy-weight process! Note that the package
1289 // manager will ensure that only activity can run in the main
1290 // process of the .apk, which is the only thing that will be
1291 // considered heavy-weight.
1292 if (app.processName.equals(app.info.packageName)) {
1293 if (mService.mHeavyWeightProcess != null
1294 && mService.mHeavyWeightProcess != app) {
1295 Slog.w(TAG, "Starting new heavy weight process " + app
1296 + " when already running "
1297 + mService.mHeavyWeightProcess);
1298 }
1299 mService.mHeavyWeightProcess = app;
1300 Message msg = mService.mHandler.obtainMessage(
1301 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1302 msg.obj = r;
1303 mService.mHandler.sendMessage(msg);
1304 }
1305 }
1306
1307 } catch (RemoteException e) {
1308 if (r.launchFailed) {
1309 // This is the second time we failed -- finish activity
1310 // and give up.
1311 Slog.e(TAG, "Second failure launching "
1312 + r.intent.getComponent().flattenToShortString()
1313 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001314 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001315 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1316 "2nd-crash", false);
1317 return false;
1318 }
1319
1320 // This is the first time we failed -- restart process and
1321 // retry.
1322 app.activities.remove(r);
1323 throw e;
1324 }
1325
1326 r.launchFailed = false;
1327 if (stack.updateLRUListLocked(r)) {
1328 Slog.w(TAG, "Activity " + r
1329 + " being launched, but already in LRU list");
1330 }
1331
1332 if (andResume) {
1333 // As part of the process of launching, ActivityThread also performs
1334 // a resume.
1335 stack.minimalResumeActivityLocked(r);
1336 } else {
1337 // This activity is not starting in the resumed state... which
1338 // should look like we asked it to pause+stop (but remain visible),
1339 // and it has done so and reported back the current icicle and
1340 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001341 if (DEBUG_STATES) Slog.v(TAG_STATES,
1342 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001343 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001344 r.stopped = true;
1345 }
1346
1347 // Launch the new version setup screen if needed. We do this -after-
1348 // launching the initial activity (that is, home), so that it can have
1349 // a chance to initialize itself while in the background, making the
1350 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001351 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001352 mService.startSetupActivityLocked();
1353 }
1354
Dianne Hackborn465fa392014-09-14 14:21:18 -07001355 // Update any services we are bound to that might care about whether
1356 // their client may have activities.
1357 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1358
Craig Mautner2420ead2013-04-01 17:13:20 -07001359 return true;
1360 }
1361
Craig Mautnere79d42682013-04-01 19:01:53 -07001362 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001363 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001364 // Is this activity's application already running?
1365 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001366 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001367
1368 r.task.stack.setLaunchTime(r);
1369
1370 if (app != null && app.thread != null) {
1371 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001372 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1373 || !"android".equals(r.info.packageName)) {
1374 // Don't add this if it is a platform component that is marked
1375 // to run in multiple processes, because this is actually
1376 // part of the framework so doesn't make sense to track as a
1377 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001378 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1379 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001380 }
George Mount2c92c972014-03-20 09:38:23 -07001381 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001382 return;
1383 } catch (RemoteException e) {
1384 Slog.w(TAG, "Exception when starting activity "
1385 + r.intent.getComponent().flattenToShortString(), e);
1386 }
1387
1388 // If a dead object exception was thrown -- fall through to
1389 // restart the application.
1390 }
1391
1392 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001393 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001394 }
1395
Craig Mautner6170f732013-04-02 13:05:23 -07001396 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001397 Intent intent, String resolvedType, ActivityInfo aInfo,
1398 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1399 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001400 int callingPid, int callingUid, String callingPackage,
1401 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001402 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1403 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001404 int err = ActivityManager.START_SUCCESS;
1405
1406 ProcessRecord callerApp = null;
1407 if (caller != null) {
1408 callerApp = mService.getRecordForAppLocked(caller);
1409 if (callerApp != null) {
1410 callingPid = callerApp.pid;
1411 callingUid = callerApp.info.uid;
1412 } else {
1413 Slog.w(TAG, "Unable to find app for caller " + caller
1414 + " (pid=" + callingPid + ") when starting: "
1415 + intent.toString());
1416 err = ActivityManager.START_PERMISSION_DENIED;
1417 }
1418 }
1419
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001420 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1421
Craig Mautner6170f732013-04-02 13:05:23 -07001422 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001423 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001424 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001425 + " on display " + (container == null ? (mFocusedStack == null ?
1426 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1427 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1428 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001429 }
1430
1431 ActivityRecord sourceRecord = null;
1432 ActivityRecord resultRecord = null;
1433 if (resultTo != null) {
1434 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001435 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1436 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001437 if (sourceRecord != null) {
1438 if (requestCode >= 0 && !sourceRecord.finishing) {
1439 resultRecord = sourceRecord;
1440 }
1441 }
1442 }
Craig Mautner6170f732013-04-02 13:05:23 -07001443
Dianne Hackborn91097de2014-04-04 18:02:06 -07001444 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001445
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001446 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001447 // Transfer the result target from the source activity to the new
1448 // one being started, including any failures.
1449 if (requestCode >= 0) {
1450 ActivityOptions.abort(options);
1451 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1452 }
1453 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001454 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1455 resultRecord = null;
1456 }
Craig Mautner6170f732013-04-02 13:05:23 -07001457 resultWho = sourceRecord.resultWho;
1458 requestCode = sourceRecord.requestCode;
1459 sourceRecord.resultTo = null;
1460 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001461 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001462 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001463 if (sourceRecord.launchedFromUid == callingUid) {
1464 // The new activity is being launched from the same uid as the previous
1465 // activity in the flow, and asking to forward its result back to the
1466 // previous. In this case the activity is serving as a trampoline between
1467 // the two, so we also want to update its launchedFromPackage to be the
1468 // same as the previous activity. Note that this is safe, since we know
1469 // these two packages come from the same uid; the caller could just as
1470 // well have supplied that same package name itself. This specifially
1471 // deals with the case of an intent picker/chooser being launched in the app
1472 // flow to redirect to an activity picked by the user, where we want the final
1473 // activity to consider it to have been launched by the previous app activity.
1474 callingPackage = sourceRecord.launchedFromPackage;
1475 }
Craig Mautner6170f732013-04-02 13:05:23 -07001476 }
1477
1478 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1479 // We couldn't find a class that can handle the given Intent.
1480 // That's the end of that!
1481 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1482 }
1483
1484 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1485 // We couldn't find the specific class specified in the Intent.
1486 // Also the end of the line.
1487 err = ActivityManager.START_CLASS_NOT_FOUND;
1488 }
1489
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001490 if (err == ActivityManager.START_SUCCESS
1491 && !isCurrentProfileLocked(userId)
1492 && (aInfo.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
1493 // Trying to launch a background activity that doesn't show for all users.
1494 err = ActivityManager.START_NOT_CURRENT_USER_ACTIVITY;
1495 }
1496
Dianne Hackborn91097de2014-04-04 18:02:06 -07001497 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1498 && sourceRecord.task.voiceSession != null) {
1499 // If this activity is being launched as part of a voice session, we need
1500 // to ensure that it is safe to do so. If the upcoming activity will also
1501 // be part of the voice session, we can only launch it if it has explicitly
1502 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001503 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001504 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1505 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001506 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001507 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1508 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001509 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1510 }
1511 } catch (RemoteException e) {
1512 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1513 }
1514 }
1515 }
1516
1517 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1518 // If the caller is starting a new voice session, just make sure the target
1519 // is actually allowing it to run this way.
1520 try {
1521 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1522 intent, resolvedType)) {
1523 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1524 }
1525 } catch (RemoteException e) {
1526 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1527 }
1528 }
1529
louis_changcd5d1982014-08-01 10:09:08 +08001530 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1531
Craig Mautner6170f732013-04-02 13:05:23 -07001532 if (err != ActivityManager.START_SUCCESS) {
1533 if (resultRecord != null) {
1534 resultStack.sendActivityResultLocked(-1,
1535 resultRecord, resultWho, requestCode,
1536 Activity.RESULT_CANCELED, null);
1537 }
Craig Mautner6170f732013-04-02 13:05:23 -07001538 ActivityOptions.abort(options);
1539 return err;
1540 }
1541
Svetoslav7008b512015-06-24 18:47:07 -07001542 boolean abort = false;
1543
Svet Ganov99b60432015-06-27 13:15:22 -07001544 final int startAnyPerm = mService.checkPermission(
1545 START_ANY_ACTIVITY, callingPid, callingUid);
1546
1547 if (startAnyPerm != PERMISSION_GRANTED) {
1548 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001549 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001550 final int actionRestriction = getActionRestrictionForCallingPackage(
1551 intent.getAction(), callingPackage, callingPid, callingUid);
1552
1553 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1554 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1555 if (resultRecord != null) {
1556 resultStack.sendActivityResultLocked(-1,
1557 resultRecord, resultWho, requestCode,
1558 Activity.RESULT_CANCELED, null);
1559 }
1560 String msg;
1561 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1562 msg = "Permission Denial: starting " + intent.toString()
1563 + " from " + callerApp + " (pid=" + callingPid
1564 + ", uid=" + callingUid + ")" + " with revoked permission "
1565 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1566 } else if (!aInfo.exported) {
1567 msg = "Permission Denial: starting " + intent.toString()
1568 + " from " + callerApp + " (pid=" + callingPid
1569 + ", uid=" + callingUid + ")"
1570 + " not exported from uid " + aInfo.applicationInfo.uid;
1571 } else {
1572 msg = "Permission Denial: starting " + intent.toString()
1573 + " from " + callerApp + " (pid=" + callingPid
1574 + ", uid=" + callingUid + ")"
1575 + " requires " + aInfo.permission;
1576 }
1577 Slog.w(TAG, msg);
1578 throw new SecurityException(msg);
1579 }
1580
1581 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1582 String message = "Appop Denial: starting " + intent.toString()
1583 + " from " + callerApp + " (pid=" + callingPid
1584 + ", uid=" + callingUid + ")"
1585 + " requires " + AppOpsManager.permissionToOp(
1586 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1587 Slog.w(TAG, message);
1588 abort = true;
1589 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1590 String message = "Appop Denial: starting " + intent.toString()
1591 + " from " + callerApp + " (pid=" + callingPid
1592 + ", uid=" + callingUid + ")"
1593 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1594 Slog.w(TAG, message);
1595 abort = true;
1596 }
Svetoslav7008b512015-06-24 18:47:07 -07001597 }
1598
1599 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001600 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001601
Craig Mautner6170f732013-04-02 13:05:23 -07001602 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001603 try {
1604 // The Intent we give to the watcher has the extra data
1605 // stripped off, since it can contain private information.
1606 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001607 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001608 aInfo.applicationInfo.packageName);
1609 } catch (RemoteException e) {
1610 mService.mController = null;
1611 }
Ben Gruver5e207332013-04-03 17:41:37 -07001612 }
Craig Mautner6170f732013-04-02 13:05:23 -07001613
Ben Gruver5e207332013-04-03 17:41:37 -07001614 if (abort) {
1615 if (resultRecord != null) {
1616 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001617 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001618 }
Ben Gruver5e207332013-04-03 17:41:37 -07001619 // We pretend to the caller that it was really started, but
1620 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001621 ActivityOptions.abort(options);
1622 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001623 }
1624
1625 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001626 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001627 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001628 if (outActivity != null) {
1629 outActivity[0] = r;
1630 }
1631
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001632 if (r.appTimeTracker == null && sourceRecord != null) {
1633 // If the caller didn't specify an explicit time tracker, we want to continue
1634 // tracking under any it has.
1635 r.appTimeTracker = sourceRecord.appTimeTracker;
1636 }
1637
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001638 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001639 if (voiceSession == null && (stack.mResumedActivity == null
1640 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001641 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1642 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001643 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001644 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001645 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001646 ActivityOptions.abort(options);
1647 return ActivityManager.START_SWITCHES_CANCELED;
1648 }
1649 }
1650
1651 if (mService.mDidAppSwitch) {
1652 // This is the second allowed switch since we stopped switches,
1653 // so now just generally allow switches. Use case: user presses
1654 // home (switches disabled, switch to home, mDidAppSwitch now true);
1655 // user taps a home icon (coming from home so allowed, we hit here
1656 // and now allow anyone to switch again).
1657 mService.mAppSwitchesAllowedTime = 0;
1658 } else {
1659 mService.mDidAppSwitch = true;
1660 }
1661
Craig Mautneree36c772014-07-16 14:56:05 -07001662 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001663
Dianne Hackborn91097de2014-04-04 18:02:06 -07001664 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001665 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001666
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001667 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001668 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001669 // activity start, but we are not actually doing an activity
1670 // switch... just dismiss the keyguard now, because we
1671 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001672 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001673 }
1674 return err;
1675 }
1676
Svet Ganov99b60432015-06-27 13:15:22 -07001677 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001678 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Svet Ganov99b60432015-06-27 13:15:22 -07001679 if (activityInfo.permission == null) {
1680 return ACTIVITY_RESTRICTION_NONE;
1681 }
1682
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001683 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1684 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001685 == PackageManager.PERMISSION_DENIED) {
1686 return ACTIVITY_RESTRICTION_PERMISSION;
1687 }
1688
1689 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1690 if (opCode == AppOpsManager.OP_NONE) {
1691 return ACTIVITY_RESTRICTION_NONE;
1692 }
1693
1694 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1695 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001696 if (!ignoreTargetSecurity) {
1697 return ACTIVITY_RESTRICTION_APPOP;
1698 }
Svet Ganov99b60432015-06-27 13:15:22 -07001699 }
1700
1701 return ACTIVITY_RESTRICTION_NONE;
1702 }
1703
Svetoslav7008b512015-06-24 18:47:07 -07001704 private int getActionRestrictionForCallingPackage(String action,
1705 String callingPackage, int callingPid, int callingUid) {
1706 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001707 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001708 }
1709
1710 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1711 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001712 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001713 }
1714
1715 final PackageInfo packageInfo;
1716 try {
1717 packageInfo = mService.mContext.getPackageManager()
1718 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1719 } catch (PackageManager.NameNotFoundException e) {
1720 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001721 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001722 }
1723
1724 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001725 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001726 }
1727
1728 if (mService.checkPermission(permission, callingPid, callingUid) ==
1729 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001730 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001731 }
1732
1733 final int opCode = AppOpsManager.permissionToOpCode(permission);
1734 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001735 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001736 }
1737
1738 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1739 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001740 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001741 }
1742
Svet Ganov99b60432015-06-27 13:15:22 -07001743 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001744 }
1745
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001746 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001747 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001748
1749 // On leanback only devices we should keep all activities in the same stack.
1750 if (!mLeanbackOnlyDevice &&
1751 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001752
1753 ActivityStack stack;
1754
Wale Ogunwale7d701172015-03-11 15:36:30 -07001755 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001756 stack = task.stack;
1757 if (stack.isOnHomeDisplay()) {
1758 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001759 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1760 "computeStackFocus: Setting " + "focused stack to r=" + r
1761 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001762 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001763 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001764 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001765 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001766 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001767 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001768 }
1769
Craig Mautnere0a38842013-12-16 16:14:02 -08001770 final ActivityContainer container = r.mInitialActivityContainer;
1771 if (container != null) {
1772 // The first time put it on the desired stack, after this put on task stack.
1773 r.mInitialActivityContainer = null;
1774 return container.mStack;
1775 }
1776
Craig Mautner1b4bf852014-05-26 15:06:32 -07001777 if (mFocusedStack != mHomeStack && (!newTask ||
1778 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001779 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001780 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001781 return mFocusedStack;
1782 }
1783
Wale Ogunwale706ed792015-08-02 10:29:44 -07001784 // We first try to put the task in the first dynamic stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001785 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1786 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001787 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001788 final boolean isDynamicStack = stack.mStackId >= FIRST_DYNAMIC_STACK_ID;
1789 if (isDynamicStack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001790 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001791 "computeStackFocus: Setting focused stack=" + stack);
1792 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001793 }
1794 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001795
Wale Ogunwale706ed792015-08-02 10:29:44 -07001796 // If there is no suitable dynamic stack then we figure out which static stack to use.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001797 stack = getStack(
Wale Ogunwale706ed792015-08-02 10:29:44 -07001798 task != null
1799 ? task.getLaunchStackId(mFocusedStack) : FULLSCREEN_WORKSPACE_STACK_ID,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001800 true /*createStaticStackIfNeeded*/,
1801 true /*createOnTop*/);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001802 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1803 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001804 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001805 }
1806 return mHomeStack;
1807 }
1808
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001809 boolean setFocusedStack(ActivityRecord r, String reason) {
1810 if (r == null) {
1811 // Not sure what you are trying to do, but it is not going to work...
1812 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001813 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001814 final TaskRecord task = r.task;
1815 if (task == null || task.stack == null) {
1816 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1817 return false;
1818 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001819 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001820 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001821 }
1822
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001823 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001824 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001825 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001826 final Intent intent = r.intent;
1827 final int callingUid = r.launchedFromUid;
1828
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001829 // In some flows in to this function, we retrieve the task record and hold on to it
1830 // without a lock before calling back in to here... so the task at this point may
1831 // not actually be in recents. Check for that, and if it isn't in recents just
1832 // consider it invalid.
1833 if (inTask != null && !inTask.inRecents) {
1834 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1835 inTask = null;
1836 }
1837
Craig Mautnerad400af2014-08-13 16:41:36 -07001838 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001839 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1840 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001841
Craig Mautnera228ae92014-07-09 05:44:55 -07001842 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001843 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001844 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001845 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1846 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1847 "\"singleInstance\" or \"singleTask\"");
1848 launchFlags &=
1849 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1850 } else {
1851 switch (r.info.documentLaunchMode) {
1852 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1853 break;
1854 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1855 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1856 break;
1857 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1858 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1859 break;
1860 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1861 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1862 break;
1863 }
1864 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001865
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001866 final boolean launchTaskBehind = r.mLaunchTaskBehind
1867 && !launchSingleTask && !launchSingleInstance
1868 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001869
Wale Ogunwale7d701172015-03-11 15:36:30 -07001870 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1871 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001872 // For whatever reason this activity is being launched into a new
1873 // task... yet the caller has requested a result back. Well, that
1874 // is pretty messed up, so instead immediately send back a cancel
1875 // and let the new task continue launched as normal without a
1876 // dependency on its originator.
1877 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1878 r.resultTo.task.stack.sendActivityResultLocked(-1,
1879 r.resultTo, r.resultWho, r.requestCode,
1880 Activity.RESULT_CANCELED, null);
1881 r.resultTo = null;
1882 }
1883
1884 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1885 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1886 }
1887
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001888 // If we are actually going to launch in to a new task, there are some cases where
1889 // we further want to do multiple task.
1890 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1891 if (launchTaskBehind
1892 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1893 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1894 }
1895 }
1896
Craig Mautner8849a5e2013-04-02 16:41:03 -07001897 // We'll invoke onUserLeaving before onPause only if the launching
1898 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001899 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001900 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1901 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001902
1903 // If the caller has asked not to resume at this point, we make note
1904 // of this in the record so that we can skip it when trying to find
1905 // the top running activity.
1906 if (!doResume) {
1907 r.delayedResume = true;
1908 }
1909
Craig Mautnera254cd72014-05-25 16:47:39 -07001910 ActivityRecord notTop =
1911 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001912
1913 // If the onlyIfNeeded flag is set, then we can do this if the activity
1914 // being launched is the same as the one making the call... or, as
1915 // a special case, if we do not know the caller then we count the
1916 // current top activity as the caller.
1917 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1918 ActivityRecord checkedCaller = sourceRecord;
1919 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001920 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001921 }
1922 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1923 // Caller is not the same as launcher, so always needed.
1924 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1925 }
1926 }
1927
Craig Mautner8849a5e2013-04-02 16:41:03 -07001928 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001929 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001930
1931 // If the caller is not coming from another activity, but has given us an
1932 // explicit task into which they would like us to launch the new activity,
1933 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001934 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1935 final Intent baseIntent = inTask.getBaseIntent();
1936 final ActivityRecord root = inTask.getRootActivity();
1937 if (baseIntent == null) {
1938 ActivityOptions.abort(options);
1939 throw new IllegalArgumentException("Launching into task without base intent: "
1940 + inTask);
1941 }
1942
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001943 // If this task is empty, then we are adding the first activity -- it
1944 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001945 if (launchSingleInstance || launchSingleTask) {
1946 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1947 ActivityOptions.abort(options);
1948 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1949 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001950 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001951 if (root != null) {
1952 ActivityOptions.abort(options);
1953 throw new IllegalArgumentException("Caller with inTask " + inTask
1954 + " has root " + root + " but target is singleInstance/Task");
1955 }
1956 }
1957
1958 // If task is empty, then adopt the interesting intent launch flags in to the
1959 // activity being started.
1960 if (root == null) {
1961 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1962 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1963 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1964 launchFlags = (launchFlags&~flagsOfInterest)
1965 | (baseIntent.getFlags()&flagsOfInterest);
1966 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001967 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001968 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001969
Dianne Hackborn962d5352014-08-29 16:27:40 -07001970 // If the task is not empty and the caller is asking to start it as the root
1971 // of a new task, then we don't actually want to start this on the task. We
1972 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001973 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001974 addingToTask = false;
1975
1976 } else {
1977 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001978 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001979
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001980 reuseTask = inTask;
1981 } else {
1982 inTask = null;
1983 }
1984
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001985 if (inTask == null) {
1986 if (sourceRecord == null) {
1987 // This activity is not being started from another... in this
1988 // case we -always- start a new task.
1989 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1990 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1991 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1992 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1993 }
1994 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1995 // The original activity who is starting us is running as a single
1996 // instance... this new activity it is starting must go on its
1997 // own task.
1998 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1999 } else if (launchSingleInstance || launchSingleTask) {
2000 // The activity being started is a single instance... it always
2001 // gets launched into its own task.
2002 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2003 }
2004 }
2005
2006 ActivityInfo newTaskInfo = null;
2007 Intent newTaskIntent = null;
2008 ActivityStack sourceStack;
2009 if (sourceRecord != null) {
2010 if (sourceRecord.finishing) {
2011 // If the source is finishing, we can't further count it as our source. This
2012 // is because the task it is associated with may now be empty and on its way out,
2013 // so we don't want to blindly throw it in to that task. Instead we will take
2014 // the NEW_TASK flow and try to find a task for it. But save the task information
2015 // so it can be used when creating the new task.
2016 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2017 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2018 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2019 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2020 newTaskInfo = sourceRecord.info;
2021 newTaskIntent = sourceRecord.task.intent;
2022 }
2023 sourceRecord = null;
2024 sourceStack = null;
2025 } else {
2026 sourceStack = sourceRecord.task.stack;
2027 }
2028 } else {
2029 sourceStack = null;
2030 }
2031
2032 boolean movedHome = false;
2033 ActivityStack targetStack;
2034
2035 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002036 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002037
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002038 // We may want to try to place the new activity in to an existing task. We always
2039 // do this if the target activity is singleTask or singleInstance; we will also do
2040 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2041 // us to still place it in a new task: multi task, always doc mode, or being asked to
2042 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002043 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2044 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002045 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002046 // If bring to front is requested, and no result is requested and we have not
2047 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002048 // we can find a task that was started with this same
2049 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002050 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002051 // See if there is a task to bring to the front. If this is
2052 // a SINGLE_INSTANCE activity, there can be one and only one
2053 // instance of it in the history, and it is always in its own
2054 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002055 ActivityRecord intentActivity = !launchSingleInstance ?
2056 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002057 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002058 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2059 // but still needs to be a lock task mode violation since the task gets
2060 // cleared out and the device would otherwise leave the locked task.
2061 if (isLockTaskModeViolation(intentActivity.task,
2062 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2063 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002064 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002065 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002066 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2067 }
Craig Mautner29219d92013-04-16 20:19:12 -07002068 if (r.task == null) {
2069 r.task = intentActivity.task;
2070 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002071 if (intentActivity.task.intent == null) {
2072 // This task was started because of movement of
2073 // the activity based on affinity... now that we
2074 // are actually launching it, we can assign the
2075 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002076 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002077 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002078 targetStack = intentActivity.task.stack;
2079 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002080 // If the target task is not in the front, then we need
2081 // to bring it to the front... except... well, with
2082 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2083 // to have the same behavior as if a new instance was
2084 // being started, which means not bringing it to the front
2085 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002086 final ActivityStack focusStack = getFocusedStack();
2087 ActivityRecord curTop = (focusStack == null)
2088 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002089 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002090 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002091 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002092 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002093 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2094 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002095 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002096 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002097 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2098 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002099 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002100 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002101 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002102 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002103 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002104 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2105 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002106 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002107 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002108 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002109 options = null;
2110 }
2111 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002112 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002113 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002114 + " from " + intentActivity);
2115 targetStack.moveToFront("intentActivityFound");
2116 }
2117
Craig Mautner8849a5e2013-04-02 16:41:03 -07002118 // If the caller has requested that the target task be
2119 // reset, then do so.
2120 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2121 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2122 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002123 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002124 // We don't need to start a new activity, and
2125 // the client said not to do anything if that
2126 // is the case, so this is it! And for paranoia, make
2127 // sure we have correctly resumed the top activity.
2128 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002129 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002130
2131 // Make sure to notify Keyguard as well if we are not running an app
2132 // transition later.
2133 if (!movedToFront) {
2134 notifyActivityDrawnForKeyguard();
2135 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002136 } else {
2137 ActivityOptions.abort(options);
2138 }
2139 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2140 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002141 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002142 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002143 // The caller has requested to completely replace any
2144 // existing task with its new activity. Well that should
2145 // not be too hard...
2146 reuseTask = intentActivity.task;
2147 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002148 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002149 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002150 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002151 // In this situation we want to remove all activities
2152 // from the task up to the one being started. In most
2153 // cases this means we are resetting the task to its
2154 // initial state.
2155 ActivityRecord top =
2156 intentActivity.task.performClearTaskLocked(r, launchFlags);
2157 if (top != null) {
2158 if (top.frontOfTask) {
2159 // Activity aliases may mean we use different
2160 // intents for the top activity, so make sure
2161 // the task now has the identity of the new
2162 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002163 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002164 }
2165 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2166 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002167 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002168 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002169 // A special case: we need to start the activity because it is not
2170 // currently running, and the caller has asked to clear the current
2171 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002172 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002173 // Now pretend like this activity is being started by the top of its
2174 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002175 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002176 TaskRecord task = sourceRecord.task;
2177 if (task != null && task.stack == null) {
2178 // Target stack got cleared when we all activities were removed
2179 // above. Go ahead and reset it.
2180 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2181 targetStack.addTask(
2182 task, !launchTaskBehind /* toTop */, false /* moving */);
2183 }
2184
Craig Mautner8849a5e2013-04-02 16:41:03 -07002185 }
2186 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2187 // In this case the top activity on the task is the
2188 // same as the one being launched, so we take that
2189 // as a request to bring the task to the foreground.
2190 // If the top activity in the task is the root
2191 // activity, deliver this new intent to it if it
2192 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002193 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002194 && intentActivity.realActivity.equals(r.realActivity)) {
2195 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2196 intentActivity.task);
2197 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002198 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002199 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002200 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2201 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002202 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2203 // In this case we are launching the root activity
2204 // of the task, but with a different intent. We
2205 // should start a new instance on top.
2206 addingToTask = true;
2207 sourceRecord = intentActivity;
2208 }
2209 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2210 // In this case an activity is being launched in to an
2211 // existing task, without resetting that task. This
2212 // is typically the situation of launching an activity
2213 // from a notification or shortcut. We want to place
2214 // the new activity on top of the current task.
2215 addingToTask = true;
2216 sourceRecord = intentActivity;
2217 } else if (!intentActivity.task.rootWasReset) {
2218 // In this case we are launching in to an existing task
2219 // that has not yet been started from its front door.
2220 // The current task has been brought to the front.
2221 // Ideally, we'd probably like to place this new task
2222 // at the bottom of its stack, but that's a little hard
2223 // to do with the current organization of the code so
2224 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002225 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002226 }
2227 if (!addingToTask && reuseTask == null) {
2228 // We didn't do anything... but it was needed (a.k.a., client
2229 // don't use that intent!) And for paranoia, make
2230 // sure we have correctly resumed the top activity.
2231 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002232 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002233 if (!movedToFront) {
2234 // Make sure to notify Keyguard as well if we are not running an app
2235 // transition later.
2236 notifyActivityDrawnForKeyguard();
2237 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002238 } else {
2239 ActivityOptions.abort(options);
2240 }
2241 return ActivityManager.START_TASK_TO_FRONT;
2242 }
2243 }
2244 }
2245 }
2246
2247 //String uri = r.intent.toURI();
2248 //Intent intent2 = new Intent(uri);
2249 //Slog.i(TAG, "Given intent: " + r.intent);
2250 //Slog.i(TAG, "URI is: " + uri);
2251 //Slog.i(TAG, "To intent: " + intent2);
2252
2253 if (r.packageName != null) {
2254 // If the activity being launched is the same as the one currently
2255 // at the top, then we need to check if it should only be launched
2256 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002257 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002258 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002259 if (top != null && r.resultTo == null) {
2260 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2261 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002262 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002263 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002264 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2265 top.task);
2266 // For paranoia, make sure we have correctly
2267 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002268 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002269 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002270 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002271 }
2272 ActivityOptions.abort(options);
2273 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2274 // We don't need to start a new activity, and
2275 // the client said not to do anything if that
2276 // is the case, so this is it!
2277 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2278 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002279 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002280 return ActivityManager.START_DELIVERED_TO_TOP;
2281 }
2282 }
2283 }
2284 }
2285
2286 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002287 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002288 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2289 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002290 }
2291 ActivityOptions.abort(options);
2292 return ActivityManager.START_CLASS_NOT_FOUND;
2293 }
2294
2295 boolean newTask = false;
2296 boolean keepCurTransition = false;
2297
Craig Mautnerbb742462014-07-07 15:28:55 -07002298 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002299 sourceRecord.task : null;
2300
Craig Mautner8849a5e2013-04-02 16:41:03 -07002301 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002302 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002303 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002304 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002305 targetStack = computeStackFocus(r, newTask);
2306 targetStack.moveToFront("startingNewTask");
2307
Craig Mautner8849a5e2013-04-02 16:41:03 -07002308 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002309 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2310 newTaskInfo != null ? newTaskInfo : r.info,
2311 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002312 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2313 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002314 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2315 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002316 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002317 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002318 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002319 if (isLockTaskModeViolation(r.task)) {
2320 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2321 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2322 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002323 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002324 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002325 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2326 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002327 // Caller wants to appear on home activity, so before starting
2328 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002329 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002330 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002331 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002332 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002333 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002334 if (isLockTaskModeViolation(sourceTask)) {
2335 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2336 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2337 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002338 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002339 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002340 final TaskRecord topTask = targetStack.topTask();
2341 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002342 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002343 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002344 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002345 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002346 // In this case, we are adding the activity to an existing
2347 // task, but the caller has asked to clear that task if the
2348 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002349 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002350 keepCurTransition = true;
2351 if (top != null) {
2352 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002353 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002354 // For paranoia, make sure we have correctly
2355 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002356 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002357 if (doResume) {
2358 targetStack.resumeTopActivityLocked(null);
2359 }
2360 ActivityOptions.abort(options);
2361 return ActivityManager.START_DELIVERED_TO_TOP;
2362 }
2363 } else if (!addingToTask &&
2364 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2365 // In this case, we are launching an activity in our own task
2366 // that may already be running somewhere in the history, and
2367 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002368 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002369 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002370 final TaskRecord task = top.task;
2371 task.moveActivityToFrontLocked(top);
2372 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002373 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002374 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002375 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002376 if (doResume) {
2377 targetStack.resumeTopActivityLocked(null);
2378 }
2379 return ActivityManager.START_DELIVERED_TO_TOP;
2380 }
2381 }
2382 // An existing activity is starting this new activity, so we want
2383 // to keep the new one in the same task as the one that is starting
2384 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002385 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002386 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002387 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002388
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002389 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002390 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002391 // task it has provided to us.
2392 if (isLockTaskModeViolation(inTask)) {
2393 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2394 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2395 }
2396 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002397 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2398 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002399
2400 // Check whether we should actually launch the new activity in to the task,
2401 // or just reuse the current activity on top.
2402 ActivityRecord top = inTask.getTopActivity();
2403 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2404 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2405 || launchSingleTop || launchSingleTask) {
2406 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2407 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2408 // We don't need to start a new activity, and
2409 // the client said not to do anything if that
2410 // is the case, so this is it!
2411 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2412 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002413 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002414 return ActivityManager.START_DELIVERED_TO_TOP;
2415 }
2416 }
2417
Dianne Hackborn962d5352014-08-29 16:27:40 -07002418 if (!addingToTask) {
2419 // We don't actually want to have this activity added to the task, so just
2420 // stop here but still tell the caller that we consumed the intent.
2421 ActivityOptions.abort(options);
2422 return ActivityManager.START_TASK_TO_FRONT;
2423 }
2424
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002425 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002426 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002427 + " in explicit task " + r.task);
2428
Craig Mautner8849a5e2013-04-02 16:41:03 -07002429 } else {
2430 // This not being started from an existing activity, and not part
2431 // of a new task... just put it in the top task, though these days
2432 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002433 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002434 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002435 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002436 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002437 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002438 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002439 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002440 + " in new guessed " + r.task);
2441 }
2442
2443 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002444 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002445
Craig Mautner76e2a762014-06-24 09:05:43 -07002446 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2447 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2448 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002449 if (newTask) {
2450 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2451 }
2452 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002453 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002454 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002455 if (!launchTaskBehind) {
2456 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002457 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002458 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002459 return ActivityManager.START_SUCCESS;
2460 }
2461
Craig Mautneree36c772014-07-16 14:56:05 -07002462 final void doPendingActivityLaunchesLocked(boolean doResume) {
2463 while (!mPendingActivityLaunches.isEmpty()) {
2464 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002465 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002466 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002467 }
2468 }
2469
Craig Mautner7f13ed32014-07-28 14:00:35 -07002470 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002471 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2472 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002473 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002474 mPendingActivityLaunches.remove(palNdx);
2475 }
2476 }
2477 }
2478
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002479 void setLaunchSource(int uid) {
2480 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2481 }
2482
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002483 void acquireLaunchWakelock() {
2484 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2485 throw new IllegalStateException("Calling must be system uid");
2486 }
2487 mLaunchingActivity.acquire();
2488 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2489 // To be safe, don't allow the wake lock to be held for too long.
2490 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2491 }
2492 }
2493
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002494 /**
2495 * Called when the frontmost task is idle.
2496 * @return the state of mService.mBooting before this was called.
2497 */
2498 private boolean checkFinishBootingLocked() {
2499 final boolean booting = mService.mBooting;
2500 boolean enableScreen = false;
2501 mService.mBooting = false;
2502 if (!mService.mBooted) {
2503 mService.mBooted = true;
2504 enableScreen = true;
2505 }
2506 if (booting || enableScreen) {
2507 mService.postFinishBooting(booting, enableScreen);
2508 }
2509 return booting;
2510 }
2511
Craig Mautnerf3333272013-04-22 10:55:53 -07002512 // Checked.
2513 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2514 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002515 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002516
Craig Mautnerf3333272013-04-22 10:55:53 -07002517 ArrayList<ActivityRecord> stops = null;
2518 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002519 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002520 int NS = 0;
2521 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002522 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002523 boolean activityRemoved = false;
2524
Wale Ogunwale7d701172015-03-11 15:36:30 -07002525 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002526 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002527 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2528 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002529 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2530 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002531 if (fromTimeout) {
2532 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002533 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002534
2535 // This is a hack to semi-deal with a race condition
2536 // in the client where it can be constructed with a
2537 // newer configuration from when we asked it to launch.
2538 // We'll update with whatever configuration it now says
2539 // it used to launch.
2540 if (config != null) {
2541 r.configuration = config;
2542 }
2543
2544 // We are now idle. If someone is waiting for a thumbnail from
2545 // us, we can now deliver.
2546 r.idle = true;
2547
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002548 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002549 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002550 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002551 }
2552 }
2553
2554 if (allResumedActivitiesIdle()) {
2555 if (r != null) {
2556 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002557 }
2558
2559 if (mLaunchingActivity.isHeld()) {
2560 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2561 if (VALIDATE_WAKE_LOCK_CALLER &&
2562 Binder.getCallingUid() != Process.myUid()) {
2563 throw new IllegalStateException("Calling must be system uid");
2564 }
2565 mLaunchingActivity.release();
2566 }
2567 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002568 }
2569
2570 // Atomically retrieve all of the other things to do.
2571 stops = processStoppingActivitiesLocked(true);
2572 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002573 if ((NF = mFinishingActivities.size()) > 0) {
2574 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002575 mFinishingActivities.clear();
2576 }
2577
Craig Mautnerf3333272013-04-22 10:55:53 -07002578 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002579 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002580 mStartingUsers.clear();
2581 }
2582
Craig Mautnerf3333272013-04-22 10:55:53 -07002583 // Stop any activities that are scheduled to do so but have been
2584 // waiting for the next one to start.
2585 for (int i = 0; i < NS; i++) {
2586 r = stops.get(i);
2587 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002588 if (stack != null) {
2589 if (r.finishing) {
2590 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2591 } else {
2592 stack.stopActivityLocked(r);
2593 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002594 }
2595 }
2596
2597 // Finish any activities that are scheduled to do so but have been
2598 // waiting for the next one to start.
2599 for (int i = 0; i < NF; i++) {
2600 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002601 final ActivityStack stack = r.task.stack;
2602 if (stack != null) {
2603 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2604 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002605 }
2606
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002607 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002608 // Complete user switch
2609 if (startingUsers != null) {
2610 for (int i = 0; i < startingUsers.size(); i++) {
2611 mService.finishUserSwitch(startingUsers.get(i));
2612 }
2613 }
2614 // Complete starting up of background users
2615 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002616 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002617 mStartingBackgroundUsers.clear();
2618 for (int i = 0; i < startingUsers.size(); i++) {
2619 mService.finishUserBoot(startingUsers.get(i));
2620 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002621 }
2622 }
2623
2624 mService.trimApplications();
2625 //dump();
2626 //mWindowManager.dump();
2627
Craig Mautnerf3333272013-04-22 10:55:53 -07002628 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002629 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002630 }
2631
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002632 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002633 }
2634
Craig Mautner8e569572013-10-11 17:36:59 -07002635 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002636 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002637 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2638 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002639 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2640 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2641 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002642 }
Craig Mautner19091252013-10-05 00:03:53 -07002643 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002644 }
2645
2646 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002647 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2648 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002649 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2650 stacks.get(stackNdx).closeSystemDialogsLocked();
2651 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002652 }
2653 }
2654
Craig Mautner93529a42013-10-04 15:03:13 -07002655 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002656 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002657 }
2658
Craig Mautner8d341ef2013-03-26 09:03:27 -07002659 /**
2660 * @return true if some activity was finished (or would have finished if doit were true).
2661 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002662 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2663 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002664 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002665 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2666 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002667 final int numStacks = stacks.size();
2668 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2669 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002670 if (stack.finishDisabledPackageActivitiesLocked(
2671 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002672 didSomething = true;
2673 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002674 }
2675 }
2676 return didSomething;
2677 }
2678
Dianne Hackborna413dc02013-07-12 12:02:55 -07002679 void updatePreviousProcessLocked(ActivityRecord r) {
2680 // Now that this process has stopped, we may want to consider
2681 // it to be the previous app to try to keep around in case
2682 // the user wants to return to it.
2683
2684 // First, found out what is currently the foreground app, so that
2685 // we don't blow away the previous app if this activity is being
2686 // hosted by the process that is actually still the foreground.
2687 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002688 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2689 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002690 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2691 final ActivityStack stack = stacks.get(stackNdx);
2692 if (isFrontStack(stack)) {
2693 if (stack.mResumedActivity != null) {
2694 fgApp = stack.mResumedActivity.app;
2695 } else if (stack.mPausingActivity != null) {
2696 fgApp = stack.mPausingActivity.app;
2697 }
2698 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002699 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002700 }
2701 }
2702
2703 // Now set this one as the previous process, only if that really
2704 // makes sense to.
2705 if (r.app != null && fgApp != null && r.app != fgApp
2706 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002707 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002708 mService.mPreviousProcess = r.app;
2709 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2710 }
2711 }
2712
Craig Mautner05d29032013-05-03 13:40:13 -07002713 boolean resumeTopActivitiesLocked() {
2714 return resumeTopActivitiesLocked(null, null, null);
2715 }
2716
2717 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2718 Bundle targetOptions) {
2719 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002720 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002721 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002722 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002723 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002724 if (isFrontStack(targetStack)) {
2725 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2726 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002727
Craig Mautnere0a38842013-12-16 16:14:02 -08002728 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2729 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002730 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2731 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002732 if (stack == targetStack) {
2733 // Already started above.
2734 continue;
2735 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002736 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002737 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002738 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002739 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002740 }
Craig Mautner05d29032013-05-03 13:40:13 -07002741 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002742 }
2743
Todd Kennedy539db512014-12-15 09:57:55 -08002744 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002745 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2746 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002747 final int numStacks = stacks.size();
2748 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2749 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002750 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002751 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002752 }
2753 }
2754
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002755 void finishVoiceTask(IVoiceInteractionSession session) {
2756 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2757 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2758 final int numStacks = stacks.size();
2759 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2760 final ActivityStack stack = stacks.get(stackNdx);
2761 stack.finishVoiceTask(session);
2762 }
2763 }
2764 }
2765
Craig Mautner299f9602015-01-26 09:47:33 -08002766 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002767 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2768 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002769 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002770 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2771 // Caller wants the home activity moved with it. To accomplish this,
2772 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002773 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002774 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002775 if (task.stack == null) {
2776 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2777 + task + " to front. Stack is null");
2778 return;
2779 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002780 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2781 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2782 reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002783 if (DEBUG_STACK) Slog.d(TAG_STACK,
2784 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002785 }
2786
Craig Mautner967212c2013-04-13 21:10:58 -07002787 ActivityStack getStack(int stackId) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002788 return getStack(stackId, false /*createStaticStackIfNeeded*/, false /*createOnTop*/);
2789 }
2790
2791 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002792 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2793 if (activityContainer != null) {
2794 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002795 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002796 if (!createStaticStackIfNeeded
2797 || (stackId < FIRST_STATIC_STACK_ID || stackId > LAST_STATIC_STACK_ID)) {
2798 return null;
2799 }
2800 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002801 }
2802
Craig Mautner967212c2013-04-13 21:10:58 -07002803 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002804 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002805 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2806 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002807 }
2808 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002809 }
2810
Craig Mautner4a1cb222013-12-04 16:14:06 -08002811 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002812 ActivityRecord homeActivity = getHomeActivity();
2813 if (homeActivity != null) {
2814 return homeActivity.appToken;
2815 }
2816 return null;
2817 }
2818
2819 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002820 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002821 }
2822
2823 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002824 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2825 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2826 final TaskRecord task = tasks.get(taskNdx);
2827 if (task.isHomeTask()) {
2828 final ArrayList<ActivityRecord> activities = task.mActivities;
2829 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2830 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002831 if (r.isHomeActivity()
2832 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002833 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002834 }
2835 }
2836 }
2837 }
2838 return null;
2839 }
2840
Todd Kennedyca4d8422015-01-15 15:19:22 -08002841 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002842 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002843 ActivityContainer activityContainer =
2844 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002845 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002846 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2847 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002848 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002849 return activityContainer;
2850 }
2851
Craig Mautner34b73df2014-01-12 21:11:08 -08002852 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002853 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2854 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2855 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002856 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2857 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002858 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002859 }
2860 }
2861
Craig Mautner95da1082014-02-24 17:54:35 -08002862 void deleteActivityContainer(IActivityContainer container) {
2863 ActivityContainer activityContainer = (ActivityContainer)container;
2864 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002865 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2866 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002867 final int stackId = activityContainer.mStackId;
2868 mActivityContainers.remove(stackId);
2869 mWindowManager.removeStack(stackId);
2870 }
2871 }
2872
Wale Ogunwale60454db2015-01-23 16:05:07 -08002873 void resizeStackLocked(int stackId, Rect bounds) {
2874 final ActivityStack stack = getStack(stackId);
2875 if (stack == null) {
2876 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2877 return;
2878 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002879
2880 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002881 if (r != null && !r.task.mResizeable) {
2882 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002883 return;
2884 }
2885
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002886 final IntArray changedTaskIds = new IntArray(stack.numTasks());
2887 final List<Configuration> newTaskConfigs = new ArrayList<>(stack.numTasks());
2888 stack.mFullscreen =
2889 mWindowManager.resizeStack(stackId, bounds, changedTaskIds, newTaskConfigs);
2890 for (int i = changedTaskIds.size() - 1; i >= 0; i--) {
2891 final TaskRecord task = anyTaskForIdLocked(changedTaskIds.get(i), false);
2892 if (task == null) {
2893 Slog.wtf(TAG, "Task in WindowManager, but not in ActivityManager???");
2894 continue;
2895 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07002896 task.updateOverrideConfiguration(newTaskConfigs.get(i), bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002897 }
2898
2899 if (r != null) {
2900 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2901 // And we need to make sure at this point that all other activities
2902 // are made visible with the correct configuration.
2903 ensureActivitiesVisibleLocked(r, 0);
2904 if (!updated) {
2905 resumeTopActivitiesLocked(stack, null, null);
2906 }
2907 }
2908 }
2909
2910 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2911 if (!task.mResizeable) {
2912 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
2913 return;
2914 }
2915
2916 if (task.mBounds != null && task.mBounds.equals(bounds)) {
2917 // Nothing to do here...
2918 return;
2919 }
2920
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002921 if (!mWindowManager.isValidTaskId(task.taskId)) {
2922 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002923 // All we can do for now is update the bounds so it can be used when the task is
2924 // added to window manager.
2925 task.mBounds = task.mLastNonFullscreenBounds = new Rect(bounds);
2926 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2927 // re-restore the task so it can have the proper stack association.
2928 restoreRecentTaskLocked(task);
2929 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002930 return;
2931 }
2932
2933 final Configuration overrideConfig = mWindowManager.resizeTask(task.taskId, bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002934 if (task.updateOverrideConfiguration(overrideConfig, bounds)) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002935 ActivityRecord r = task.topRunningActivityLocked(null);
Wale Ogunwale60454db2015-01-23 16:05:07 -08002936 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002937 final ActivityStack stack = task.stack;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002938 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2939 // And we need to make sure at this point that all other activities
2940 // are made visible with the correct configuration.
2941 ensureActivitiesVisibleLocked(r, 0);
2942 if (!updated) {
2943 resumeTopActivitiesLocked(stack, null, null);
2944 }
2945 }
2946 }
2947 }
2948
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002949 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002950 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2951 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002952 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002953 }
2954
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002955 ActivityContainer activityContainer = new ActivityContainer(stackId);
2956 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002957 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002958 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002959 }
2960
2961 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002962 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002963 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2964 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002965 break;
2966 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002967 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002968 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002969 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002970 }
2971
Wale Ogunwale7de05352014-12-12 15:21:33 -08002972 private boolean restoreRecentTaskLocked(TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002973 final int stackId =
2974 mLeanbackOnlyDevice ? mHomeStack.mStackId : task.getLaunchStackId(mFocusedStack);
2975 if (task.stack != null) {
2976 // Task has already been restored once. See if we need to do anything more
2977 if (task.stack.mStackId == stackId) {
2978 // Nothing else to do since it is already restored in the right stack.
2979 return true;
2980 }
2981 // Remove current stack association, so we can re-associate the task with the
2982 // right stack below.
2983 task.stack.removeTask(task, "restoreRecentTaskLocked", false /*notMoving*/);
2984 }
2985
2986 ActivityStack stack =
2987 getStack(stackId, true /*createStaticStackIfNeeded*/, false /*createOnTop*/);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002988
2989 if (stack == null) {
2990 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002991 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2992 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002993 return false;
2994 }
2995
2996 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002997 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2998 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002999 final ArrayList<ActivityRecord> activities = task.mActivities;
3000 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003001 stack.addAppToken(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003002 }
3003 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003004 }
3005
Craig Mautner299f9602015-01-26 09:47:33 -08003006 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003007 final TaskRecord task = anyTaskForIdLocked(taskId);
3008 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003009 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003010 return;
3011 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003012 ActivityStack stack =
3013 getStack(stackId, true /*createStaticStackIfNeeded*/, toTop /*createOnTop*/);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003014 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
3015 if (task.stack != null) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07003016 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003017 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003018 stack.addTask(task, toTop, true);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003019
3020 // Make sure the task has the appropriate bounds/size for the stack it is in.
3021 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
3022 resizeTaskLocked(task, null);
3023 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3024 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
3025 resizeTaskLocked(task, task.mLastNonFullscreenBounds);
3026 }
3027
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003028 // The task might have already been running and its visibility needs to be synchronized with
3029 // the visibility of the stack / windows.
3030 stack.ensureActivitiesVisibleLocked(null, 0);
Craig Mautner05d29032013-05-03 13:40:13 -07003031 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003032 }
3033
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003034 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3035 final TaskRecord task = anyTaskForIdLocked(taskId);
3036 if (task == null) {
3037 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3038 return;
3039 }
3040 ActivityStack stack =
3041 getStack(stackId, true /*createStaticStackIfNeeded*/, false /*createOnTop*/);
3042 mWindowManager.positionTaskInStack(taskId, stackId, position);
3043 final boolean stackChanged = task.stack != null && task.stack != stack;
3044 if (stackChanged) {
3045 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
3046 }
3047 stack.positionTask(task, position, stackChanged);
3048 // The task might have already been running and its visibility needs to be synchronized with
3049 // the visibility of the stack / windows.
3050 stack.ensureActivitiesVisibleLocked(null, 0);
3051 resumeTopActivitiesLocked();
3052 }
3053
Craig Mautnerac6f8432013-07-17 13:24:59 -07003054 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003055 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003056 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3057 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003058 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3059 final ActivityStack stack = stacks.get(stackNdx);
3060 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003061 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003062 continue;
3063 }
3064 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003065 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3066 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003067 continue;
3068 }
3069 final ActivityRecord ar = stack.findTaskLocked(r);
3070 if (ar != null) {
3071 return ar;
3072 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003073 }
3074 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003075 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003076 return null;
3077 }
3078
3079 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003080 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3081 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003082 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3083 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3084 if (ar != null) {
3085 return ar;
3086 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003087 }
3088 }
3089 return null;
3090 }
3091
Craig Mautner8d341ef2013-03-26 09:03:27 -07003092 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003093 scheduleSleepTimeout();
3094 if (!mGoingToSleep.isHeld()) {
3095 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003096 if (mLaunchingActivity.isHeld()) {
3097 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3098 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003099 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003100 mLaunchingActivity.release();
3101 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003102 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003103 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003104 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003105 }
3106
3107 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003108 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003109
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003110 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003111 final long endTime = System.currentTimeMillis() + timeout;
3112 while (true) {
3113 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003114 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3115 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003116 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3117 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3118 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003119 }
3120 if (cantShutdown) {
3121 long timeRemaining = endTime - System.currentTimeMillis();
3122 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003123 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003124 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003125 } catch (InterruptedException e) {
3126 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003127 } else {
3128 Slog.w(TAG, "Activity manager shutdown timed out");
3129 timedout = true;
3130 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003131 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003132 } else {
3133 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003134 }
3135 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003136
3137 // Force checkReadyForSleep to complete.
3138 mSleepTimeout = true;
3139 checkReadyForSleepLocked();
3140
Craig Mautner8d341ef2013-03-26 09:03:27 -07003141 return timedout;
3142 }
3143
3144 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003145 removeSleepTimeouts();
3146 if (mGoingToSleep.isHeld()) {
3147 mGoingToSleep.release();
3148 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003149 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3150 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003151 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3152 final ActivityStack stack = stacks.get(stackNdx);
3153 stack.awakeFromSleepingLocked();
3154 if (isFrontStack(stack)) {
3155 resumeTopActivitiesLocked();
3156 }
Craig Mautner5314a402013-09-26 12:40:16 -07003157 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003158 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003159 mGoingToSleepActivities.clear();
3160 }
3161
3162 void activitySleptLocked(ActivityRecord r) {
3163 mGoingToSleepActivities.remove(r);
3164 checkReadyForSleepLocked();
3165 }
3166
3167 void checkReadyForSleepLocked() {
3168 if (!mService.isSleepingOrShuttingDown()) {
3169 // Do not care.
3170 return;
3171 }
3172
3173 if (!mSleepTimeout) {
3174 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003175 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3176 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003177 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3178 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3179 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003180 }
3181
3182 if (mStoppingActivities.size() > 0) {
3183 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003184 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003185 + mStoppingActivities.size() + " activities");
3186 scheduleIdleLocked();
3187 dontSleep = true;
3188 }
3189
3190 if (mGoingToSleepActivities.size() > 0) {
3191 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003192 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003193 + mGoingToSleepActivities.size() + " activities");
3194 dontSleep = true;
3195 }
3196
3197 if (dontSleep) {
3198 return;
3199 }
3200 }
3201
Craig Mautnere0a38842013-12-16 16:14:02 -08003202 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3203 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003204 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3205 stacks.get(stackNdx).goToSleep();
3206 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003207 }
3208
3209 removeSleepTimeouts();
3210
3211 if (mGoingToSleep.isHeld()) {
3212 mGoingToSleep.release();
3213 }
3214 if (mService.mShuttingDown) {
3215 mService.notifyAll();
3216 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003217 }
3218
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003219 boolean reportResumedActivityLocked(ActivityRecord r) {
3220 final ActivityStack stack = r.task.stack;
3221 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003222 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003223 }
3224 if (allResumedActivitiesComplete()) {
3225 ensureActivitiesVisibleLocked(null, 0);
3226 mWindowManager.executeAppTransition();
3227 return true;
3228 }
3229 return false;
3230 }
3231
Craig Mautner8d341ef2013-03-26 09:03:27 -07003232 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003233 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3234 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003235 int stackNdx = stacks.size() - 1;
3236 while (stackNdx >= 0) {
3237 stacks.get(stackNdx).handleAppCrashLocked(app);
3238 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003239 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003240 }
3241 }
3242
Jose Lima4b6c6692014-08-12 17:41:12 -07003243 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003244 final ActivityStack stack = r.task.stack;
3245 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003246 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3247 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003248 return false;
3249 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003250 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003251 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3252 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003253
3254 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003255 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003256 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003257 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003258 return true;
3259 }
3260
3261 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003262 if (visible && top.fullscreen) {
3263 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003264 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3265 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3266 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3267 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003268 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003269 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3270 // Only the activity set as currently visible behind should actively reset its
3271 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003272 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3273 "requestVisibleBehind: returning visible=" + visible
3274 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3275 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003276 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003277 }
3278
Jose Lima4b6c6692014-08-12 17:41:12 -07003279 stack.setVisibleBehindActivity(visible ? r : null);
3280 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003281 // Make the activity immediately above r opaque.
3282 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3283 if (next != null) {
3284 mService.convertFromTranslucent(next.appToken);
3285 }
3286 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003287 if (top.app != null && top.app.thread != null) {
3288 // Notify the top app of the change.
3289 try {
3290 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3291 } catch (RemoteException e) {
3292 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003293 }
3294 return true;
3295 }
3296
Craig Mautnerbb742462014-07-07 15:28:55 -07003297 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3298 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3299 r.mLaunchTaskBehind = false;
3300 final TaskRecord task = r.task;
3301 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003302 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003303 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003304 mWindowManager.setAppVisibility(r.appToken, false);
3305 }
3306
3307 void scheduleLaunchTaskBehindComplete(IBinder token) {
3308 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3309 }
3310
Craig Mautnerde4ef022013-04-07 19:01:33 -07003311 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003312 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003313 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3314 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003315 final int topStackNdx = stacks.size() - 1;
3316 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3317 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003318 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003319 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003320 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003321 }
3322
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003323 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3324 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3325 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3326 final int topStackNdx = stacks.size() - 1;
3327 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3328 final ActivityStack stack = stacks.get(stackNdx);
3329 stack.clearOtherAppTimeTrackers(except);
3330 }
3331 }
3332 }
3333
Craig Mautner8d341ef2013-03-26 09:03:27 -07003334 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003335 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3336 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003337 final int numStacks = stacks.size();
3338 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3339 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003340 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003341 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003342 }
3343 }
3344
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003345 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3346 // Examine all activities currently running in the process.
3347 TaskRecord firstTask = null;
3348 // Tasks is non-null only if two or more tasks are found.
3349 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003350 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3351 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003352 ActivityRecord r = app.activities.get(i);
3353 // First, if we find an activity that is in the process of being destroyed,
3354 // then we just aren't going to do anything for now; we want things to settle
3355 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003356 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003357 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003358 return;
3359 }
3360 // Don't consider any activies that are currently not in a state where they
3361 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003362 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3363 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003364 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003365 continue;
3366 }
3367 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003368 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003369 + " from " + r);
3370 if (firstTask == null) {
3371 firstTask = r.task;
3372 } else if (firstTask != r.task) {
3373 if (tasks == null) {
3374 tasks = new ArraySet<>();
3375 tasks.add(firstTask);
3376 }
3377 tasks.add(r.task);
3378 }
3379 }
3380 }
3381 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003382 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003383 return;
3384 }
3385 // If we have activities in multiple tasks that are in a position to be destroyed,
3386 // let's iterate through the tasks and release the oldest one.
3387 final int numDisplays = mActivityDisplays.size();
3388 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3389 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3390 // Step through all stacks starting from behind, to hit the oldest things first.
3391 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3392 final ActivityStack stack = stacks.get(stackNdx);
3393 // Try to release activities in this stack; if we manage to, we are done.
3394 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3395 return;
3396 }
3397 }
3398 }
3399 }
3400
Amith Yamasani37a40c22015-06-17 13:25:42 -07003401 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003402 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003403 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003404 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003405
Craig Mautner858d8a62013-04-23 17:08:34 -07003406 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003407 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3408 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003409 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003410 final ActivityStack stack = stacks.get(stackNdx);
3411 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003412 TaskRecord task = stack.topTask();
3413 if (task != null) {
3414 mWindowManager.moveTaskToTop(task.taskId);
3415 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003416 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003417 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003418
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003419 ActivityStack stack = getStack(restoreStackId);
3420 if (stack == null) {
3421 stack = mHomeStack;
3422 }
3423 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003424 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003425 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003426 TaskRecord task = stack.topTask();
3427 if (task != null) {
3428 mWindowManager.moveTaskToTop(task.taskId);
3429 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003430 } else {
3431 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003432 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003433 }
Craig Mautner93529a42013-10-04 15:03:13 -07003434 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003435 }
3436
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003437 /**
3438 * Add background users to send boot completed events to.
3439 * @param userId The user being started in the background
3440 * @param uss The state object for the user.
3441 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003442 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003443 mStartingBackgroundUsers.add(uss);
3444 }
3445
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003446 /** Checks whether the userid is a profile of the current user. */
3447 boolean isCurrentProfileLocked(int userId) {
3448 if (userId == mCurrentUser) return true;
3449 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3450 if (mService.mCurrentProfileIds[i] == userId) return true;
3451 }
3452 return false;
3453 }
3454
Craig Mautnerde4ef022013-04-07 19:01:33 -07003455 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003456 ArrayList<ActivityRecord> stops = null;
3457
3458 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003459 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3460 ActivityRecord s = mStoppingActivities.get(activityNdx);
3461 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003462 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003463 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3464 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003465 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003466 if (s.finishing) {
3467 // If this activity is finishing, it is sitting on top of
3468 // everyone else but we now know it is no longer needed...
3469 // so get rid of it. Otherwise, we need to go through the
3470 // normal flow and hide it once we determine that it is
3471 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003472 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003473 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003474 }
3475 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003476 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003477 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003478 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003479 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003480 }
3481 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003482 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003483 }
3484 }
3485
3486 return stops;
3487 }
3488
Craig Mautnercf910b02013-04-23 11:23:27 -07003489 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003490 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3491 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3492 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3493 final ActivityStack stack = stacks.get(stackNdx);
3494 final ActivityRecord r = stack.topRunningActivityLocked(null);
3495 final ActivityState state = r == null ? DESTROYED : r.state;
3496 if (isFrontStack(stack)) {
3497 if (r == null) Slog.e(TAG,
3498 "validateTop...: null top activity, stack=" + stack);
3499 else {
3500 final ActivityRecord pausing = stack.mPausingActivity;
3501 if (pausing != null && pausing == r) Slog.e(TAG,
3502 "validateTop...: top stack has pausing activity r=" + r
3503 + " state=" + state);
3504 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3505 "validateTop...: activity in front not resumed r=" + r
3506 + " state=" + state);
3507 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003508 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003509 final ActivityRecord resumed = stack.mResumedActivity;
3510 if (resumed != null && resumed == r) Slog.e(TAG,
3511 "validateTop...: back stack has resumed activity r=" + r
3512 + " state=" + state);
3513 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3514 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003515 }
3516 }
3517 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003518 }
3519
Craig Mautnere0570202015-05-13 13:06:11 -07003520 private String lockTaskModeToString() {
3521 switch (mLockTaskModeState) {
3522 case LOCK_TASK_MODE_LOCKED:
3523 return "LOCKED";
3524 case LOCK_TASK_MODE_PINNED:
3525 return "PINNED";
3526 case LOCK_TASK_MODE_NONE:
3527 return "NONE";
3528 default: return "unknown=" + mLockTaskModeState;
3529 }
3530 }
3531
Craig Mautner27084302013-03-25 08:05:25 -07003532 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003533 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003534 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003535 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3536 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3537 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003538 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003539 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3540 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3541 if (packages.size() > 0) {
3542 pw.println(" mLockTaskPackages (userId:packages)=");
3543 for (int i = 0; i < packages.size(); ++i) {
3544 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3545 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3546 }
3547 }
3548 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003549 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003550
Craig Mautner20e72272013-04-01 13:45:53 -07003551 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003552 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003553 }
3554
Dianne Hackborn390517b2013-05-30 15:03:32 -07003555 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3556 boolean needSep, String prefix) {
3557 if (activity != null) {
3558 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3559 if (needSep) {
3560 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003561 }
3562 pw.print(prefix);
3563 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003564 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003565 }
3566 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003567 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003568 }
3569
Craig Mautner8d341ef2013-03-26 09:03:27 -07003570 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3571 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003572 boolean printed = false;
3573 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003574 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3575 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003576 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003577 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003578 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003579 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003580 final ActivityStack stack = stacks.get(stackNdx);
3581 StringBuilder stackHeader = new StringBuilder(128);
3582 stackHeader.append(" Stack #");
3583 stackHeader.append(stack.mStackId);
3584 stackHeader.append(":");
3585 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3586 needSep, stackHeader.toString());
3587 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3588 !dumpAll, false, dumpPackage, true,
3589 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003590
Craig Mautner4a1cb222013-12-04 16:14:06 -08003591 needSep = printed;
3592 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3593 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003594 if (pr) {
3595 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003596 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003597 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003598 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3599 " mResumedActivity: ");
3600 if (pr) {
3601 printed = true;
3602 needSep = false;
3603 }
3604 if (dumpAll) {
3605 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3606 " mLastPausedActivity: ");
3607 if (pr) {
3608 printed = true;
3609 needSep = true;
3610 }
3611 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3612 needSep, " mLastNoHistoryActivity: ");
3613 }
3614 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003615 }
3616 }
3617
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003618 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3619 false, dumpPackage, true, " Activities waiting to finish:", null);
3620 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3621 false, dumpPackage, true, " Activities waiting to stop:", null);
3622 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3623 false, dumpPackage, true, " Activities waiting for another to become visible:",
3624 null);
3625 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3626 false, dumpPackage, true, " Activities waiting to sleep:", null);
3627 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3628 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003629
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003630 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003631 }
3632
Dianne Hackborn390517b2013-05-30 15:03:32 -07003633 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003634 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003635 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003636 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003637 String innerPrefix = null;
3638 String[] args = null;
3639 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003640 for (int i=list.size()-1; i>=0; i--) {
3641 final ActivityRecord r = list.get(i);
3642 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3643 continue;
3644 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003645 if (innerPrefix == null) {
3646 innerPrefix = prefix + " ";
3647 args = new String[0];
3648 }
3649 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003650 final boolean full = !brief && (complete || !r.isInHistory());
3651 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003652 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003653 needNL = false;
3654 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003655 if (header1 != null) {
3656 pw.println(header1);
3657 header1 = null;
3658 }
3659 if (header2 != null) {
3660 pw.println(header2);
3661 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003662 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003663 if (lastTask != r.task) {
3664 lastTask = r.task;
3665 pw.print(prefix);
3666 pw.print(full ? "* " : " ");
3667 pw.println(lastTask);
3668 if (full) {
3669 lastTask.dump(pw, prefix + " ");
3670 } else if (complete) {
3671 // Complete + brief == give a summary. Isn't that obvious?!?
3672 if (lastTask.intent != null) {
3673 pw.print(prefix); pw.print(" ");
3674 pw.println(lastTask.intent.toInsecureStringWithClip());
3675 }
3676 }
3677 }
3678 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3679 pw.print(" #"); pw.print(i); pw.print(": ");
3680 pw.println(r);
3681 if (full) {
3682 r.dump(pw, innerPrefix);
3683 } else if (complete) {
3684 // Complete + brief == give a summary. Isn't that obvious?!?
3685 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3686 if (r.app != null) {
3687 pw.print(innerPrefix); pw.println(r.app);
3688 }
3689 }
3690 if (client && r.app != null && r.app.thread != null) {
3691 // flush anything that is already in the PrintWriter since the thread is going
3692 // to write to the file descriptor directly
3693 pw.flush();
3694 try {
3695 TransferPipe tp = new TransferPipe();
3696 try {
3697 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3698 r.appToken, innerPrefix, args);
3699 // Short timeout, since blocking here can
3700 // deadlock with the application.
3701 tp.go(fd, 2000);
3702 } finally {
3703 tp.kill();
3704 }
3705 } catch (IOException e) {
3706 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3707 } catch (RemoteException e) {
3708 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3709 }
3710 needNL = true;
3711 }
3712 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003713 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003714 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003715
Craig Mautnerf3333272013-04-22 10:55:53 -07003716 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003717 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3718 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003719 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3720 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003721 }
3722
3723 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003724 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003725 }
3726
3727 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003728 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3729 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003730 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3731 }
3732
Craig Mautner05d29032013-05-03 13:40:13 -07003733 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003734 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3735 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3736 }
Craig Mautner05d29032013-05-03 13:40:13 -07003737 }
3738
Craig Mautner0eea92c2013-05-16 13:35:39 -07003739 void removeSleepTimeouts() {
3740 mSleepTimeout = false;
3741 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3742 }
3743
3744 final void scheduleSleepTimeout() {
3745 removeSleepTimeouts();
3746 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3747 }
3748
Craig Mautner4a1cb222013-12-04 16:14:06 -08003749 @Override
3750 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003751 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003752 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3753 }
3754
3755 @Override
3756 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003757 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003758 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3759 }
3760
3761 @Override
3762 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003763 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003764 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3765 }
3766
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003767 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003768 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003769 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003770 newDisplay = mActivityDisplays.get(displayId) == null;
3771 if (newDisplay) {
3772 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003773 if (activityDisplay.mDisplay == null) {
3774 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3775 return;
3776 }
Craig Mautner4504de52013-12-20 09:06:56 -08003777 mActivityDisplays.put(displayId, activityDisplay);
3778 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003779 }
Craig Mautner4504de52013-12-20 09:06:56 -08003780 if (newDisplay) {
3781 mWindowManager.onDisplayAdded(displayId);
3782 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003783 }
3784
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003785 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003786 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003787 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3788 if (activityDisplay != null) {
3789 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003790 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003791 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003792 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003793 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003794 }
3795 }
3796 mWindowManager.onDisplayRemoved(displayId);
3797 }
3798
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003799 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003800 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003801 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3802 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003803 // TODO: Update the bounds.
3804 }
3805 }
3806 mWindowManager.onDisplayChanged(displayId);
3807 }
3808
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003809 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003810 StackInfo info = new StackInfo();
3811 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3812 info.displayId = Display.DEFAULT_DISPLAY;
3813 info.stackId = stack.mStackId;
3814
3815 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3816 final int numTasks = tasks.size();
3817 int[] taskIds = new int[numTasks];
3818 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003819 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003820 for (int i = 0; i < numTasks; ++i) {
3821 final TaskRecord task = tasks.get(i);
3822 taskIds[i] = task.taskId;
3823 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3824 : task.realActivity != null ? task.realActivity.flattenToString()
3825 : task.getTopActivity() != null ? task.getTopActivity().packageName
3826 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003827 taskBounds[i] = new Rect();
3828 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003829 }
3830 info.taskIds = taskIds;
3831 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003832 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003833 return info;
3834 }
3835
3836 StackInfo getStackInfoLocked(int stackId) {
3837 ActivityStack stack = getStack(stackId);
3838 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003839 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003840 }
3841 return null;
3842 }
3843
3844 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003845 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003846 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3847 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003848 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003849 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003850 }
3851 }
3852 return list;
3853 }
3854
Craig Mautner15df08a2015-04-01 12:17:18 -07003855 TaskRecord getLockedTaskLocked() {
3856 final int top = mLockTaskModeTasks.size() - 1;
3857 if (top >= 0) {
3858 return mLockTaskModeTasks.get(top);
3859 }
3860 return null;
3861 }
3862
3863 boolean isLockedTask(TaskRecord task) {
3864 return mLockTaskModeTasks.contains(task);
3865 }
3866
3867 boolean isLastLockedTask(TaskRecord task) {
3868 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3869 }
3870
3871 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003872 if (!mLockTaskModeTasks.remove(task)) {
3873 return;
3874 }
3875 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3876 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003877 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003878 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3879 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003880 final Message lockTaskMsg = Message.obtain();
3881 lockTaskMsg.arg1 = task.userId;
3882 lockTaskMsg.what = LOCK_TASK_END_MSG;
3883 mHandler.sendMessage(lockTaskMsg);
3884 }
3885 }
3886
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003887 void showLockTaskToast() {
3888 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003889 }
3890
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003891 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3892 if (mLockTaskModeTasks.contains(task)) {
3893 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3894 }
3895 }
3896
Craig Mautner432f64e2015-05-20 14:59:57 -07003897 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3898 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003899 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003900 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003901 final TaskRecord lockedTask = getLockedTaskLocked();
3902 if (lockedTask != null) {
3903 removeLockedTaskLocked(lockedTask);
3904 if (!mLockTaskModeTasks.isEmpty()) {
3905 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003906 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3907 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003908 lockedTask.performClearTaskLocked();
3909 resumeTopActivitiesLocked();
3910 return;
3911 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003912 }
Craig Mautnere0570202015-05-13 13:06:11 -07003913 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3914 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003915 return;
3916 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003917
3918 // Should have already been checked, but do it again.
3919 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003920 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3921 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003922 return;
3923 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003924 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003925 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003926 return;
3927 }
3928
3929 if (mLockTaskModeTasks.isEmpty()) {
3930 // First locktask.
3931 final Message lockTaskMsg = Message.obtain();
3932 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3933 lockTaskMsg.arg1 = task.userId;
3934 lockTaskMsg.what = LOCK_TASK_START_MSG;
3935 lockTaskMsg.arg2 = lockTaskModeState;
3936 mHandler.sendMessage(lockTaskMsg);
3937 }
3938 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003939 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3940 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003941 mLockTaskModeTasks.remove(task);
3942 mLockTaskModeTasks.add(task);
3943
3944 if (task.mLockTaskUid == -1) {
3945 task.mLockTaskUid = task.mCallingUid;
3946 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003947
3948 if (andResume) {
3949 findTaskToMoveToFrontLocked(task, 0, null, reason);
3950 resumeTopActivitiesLocked();
3951 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003952 }
3953
3954 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003955 return isLockTaskModeViolation(task, false);
3956 }
3957
3958 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3959 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003960 return false;
3961 }
3962 final int lockTaskAuth = task.mLockTaskAuth;
3963 switch (lockTaskAuth) {
3964 case LOCK_TASK_AUTH_DONT_LOCK:
3965 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003966 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003967 case LOCK_TASK_AUTH_LAUNCHABLE:
3968 case LOCK_TASK_AUTH_WHITELISTED:
3969 return false;
3970 case LOCK_TASK_AUTH_PINNABLE:
3971 // Pinnable tasks can't be launched on top of locktask tasks.
3972 return !mLockTaskModeTasks.isEmpty();
3973 default:
3974 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3975 return true;
3976 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003977 }
3978
Craig Mautner15df08a2015-04-01 12:17:18 -07003979 void onLockTaskPackagesUpdatedLocked() {
3980 boolean didSomething = false;
3981 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3982 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003983 final boolean wasWhitelisted =
3984 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3985 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003986 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003987 final boolean isWhitelisted =
3988 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3989 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3990 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003991 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003992 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3993 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003994 removeLockedTaskLocked(lockedTask);
3995 lockedTask.performClearTaskLocked();
3996 didSomething = true;
3997 }
3998 }
3999 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4000 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4001 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4002 final ActivityStack stack = stacks.get(stackNdx);
4003 stack.onLockTaskPackagesUpdatedLocked();
4004 }
4005 }
Craig Mautnere0570202015-05-13 13:06:11 -07004006 final ActivityRecord r = topRunningActivityLocked();
4007 final TaskRecord task = r != null ? r.task : null;
4008 if (mLockTaskModeTasks.isEmpty() && task != null
4009 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4010 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004011 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4012 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4013 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4014 false);
4015 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004016 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004017 if (didSomething) {
4018 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004019 }
4020 }
4021
Benjamin Franz43261142015-02-11 15:59:44 +00004022 int getLockTaskModeState() {
4023 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004024 }
4025
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004026 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004027
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004028 public ActivityStackSupervisorHandler(Looper looper) {
4029 super(looper);
4030 }
4031
Craig Mautnerf3333272013-04-22 10:55:53 -07004032 void activityIdleInternal(ActivityRecord r) {
4033 synchronized (mService) {
4034 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4035 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004036 }
4037
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004038 @Override
4039 public void handleMessage(Message msg) {
4040 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004041 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004042 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4043 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004044 if (mService.mDidDexOpt) {
4045 mService.mDidDexOpt = false;
4046 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4047 nmsg.obj = msg.obj;
4048 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4049 return;
4050 }
4051 // We don't at this point know if the activity is fullscreen,
4052 // so we need to be conservative and assume it isn't.
4053 activityIdleInternal((ActivityRecord)msg.obj);
4054 } break;
4055 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004056 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004057 activityIdleInternal((ActivityRecord)msg.obj);
4058 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004059 case RESUME_TOP_ACTIVITY_MSG: {
4060 synchronized (mService) {
4061 resumeTopActivitiesLocked();
4062 }
4063 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004064 case SLEEP_TIMEOUT_MSG: {
4065 synchronized (mService) {
4066 if (mService.isSleepingOrShuttingDown()) {
4067 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4068 mSleepTimeout = true;
4069 checkReadyForSleepLocked();
4070 }
4071 }
4072 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004073 case LAUNCH_TIMEOUT_MSG: {
4074 if (mService.mDidDexOpt) {
4075 mService.mDidDexOpt = false;
4076 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4077 return;
4078 }
4079 synchronized (mService) {
4080 if (mLaunchingActivity.isHeld()) {
4081 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4082 if (VALIDATE_WAKE_LOCK_CALLER
4083 && Binder.getCallingUid() != Process.myUid()) {
4084 throw new IllegalStateException("Calling must be system uid");
4085 }
4086 mLaunchingActivity.release();
4087 }
4088 }
4089 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004090 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004091 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004092 } break;
4093 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004094 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004095 } break;
4096 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004097 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004098 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004099 case CONTAINER_CALLBACK_VISIBILITY: {
4100 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004101 final IActivityContainerCallback callback = container.mCallback;
4102 if (callback != null) {
4103 try {
4104 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4105 } catch (RemoteException e) {
4106 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004107 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004108 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004109 case LOCK_TASK_START_MSG: {
4110 // When lock task starts, we disable the status bars.
4111 try {
Jason Monk62515be2014-05-21 16:06:19 -04004112 if (mLockTaskNotify == null) {
4113 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004114 }
Jason Monk62515be2014-05-21 16:06:19 -04004115 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004116 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004117 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004118 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004119 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004120 flags = StatusBarManager.DISABLE_MASK
4121 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004122 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004123 flags = StatusBarManager.DISABLE_MASK
4124 & (~StatusBarManager.DISABLE_BACK)
4125 & (~StatusBarManager.DISABLE_HOME)
4126 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004127 }
4128 getStatusBarService().disable(flags, mToken,
4129 mService.mContext.getPackageName());
4130 }
4131 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004132 if (getDevicePolicyManager() != null) {
4133 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004134 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004135 }
justinzhang5286d3f2014-05-12 17:06:01 -04004136 } catch (RemoteException ex) {
4137 throw new RuntimeException(ex);
4138 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004139 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004140 case LOCK_TASK_END_MSG: {
4141 // When lock task ends, we enable the status bars.
4142 try {
Jason Monk62515be2014-05-21 16:06:19 -04004143 if (getStatusBarService() != null) {
4144 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4145 mService.mContext.getPackageName());
4146 }
4147 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004148 if (getDevicePolicyManager() != null) {
4149 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4150 msg.arg1);
4151 }
Jason Monk62515be2014-05-21 16:06:19 -04004152 if (mLockTaskNotify == null) {
4153 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4154 }
4155 mLockTaskNotify.show(false);
4156 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004157 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004158 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004159 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004160 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004161 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004162 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004163 new LockPatternUtils(mService.mContext)
4164 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004165 }
4166 } catch (SettingNotFoundException e) {
4167 // No setting, don't lock.
4168 }
justinzhang5286d3f2014-05-12 17:06:01 -04004169 } catch (RemoteException ex) {
4170 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004171 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004172 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004173 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004174 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004175 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4176 if (mLockTaskNotify == null) {
4177 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4178 }
4179 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4180 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004181 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4182 final ActivityContainer container = (ActivityContainer) msg.obj;
4183 final IActivityContainerCallback callback = container.mCallback;
4184 if (callback != null) {
4185 try {
4186 callback.onAllActivitiesComplete(container.asBinder());
4187 } catch (RemoteException e) {
4188 }
4189 }
4190 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004191 case LAUNCH_TASK_BEHIND_COMPLETE: {
4192 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004193 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004194 if (r != null) {
4195 handleLaunchTaskBehindCompleteLocked(r);
4196 }
4197 }
4198 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004199 }
4200 }
4201 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004202
Ying Wangb081a592014-04-22 15:20:16 -07004203 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004204 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004205 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004206 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004207 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004208 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004209 ActivityRecord mParentActivity = null;
4210 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004211
Craig Mautnere3a00d72014-04-16 08:31:19 -07004212 boolean mVisible = true;
4213
Craig Mautner4a1cb222013-12-04 16:14:06 -08004214 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004215 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004216
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004217 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4218 final static int CONTAINER_STATE_NO_SURFACE = 1;
4219 final static int CONTAINER_STATE_FINISHING = 2;
4220 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4221
4222 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004223 synchronized (mService) {
4224 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004225 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004226 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004227 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004228 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004229 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004230
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004231 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004232 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004233 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004234 mActivityDisplay = activityDisplay;
4235 mStack.mDisplayId = activityDisplay.mDisplayId;
4236 mStack.mStacks = activityDisplay.mStacks;
4237
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004238 activityDisplay.attachActivities(mStack, onTop);
4239 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004240 }
4241
4242 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004243 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004244 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004245 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4246 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004247 return;
4248 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004249 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004250 }
4251 }
4252
4253 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004254 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004255 synchronized (mService) {
4256 if (mActivityDisplay != null) {
4257 return mActivityDisplay.mDisplayId;
4258 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004259 }
4260 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004261 }
4262
Jeff Brownca9bc702014-02-11 14:32:56 -08004263 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004264 public int getStackId() {
4265 synchronized (mService) {
4266 return mStackId;
4267 }
4268 }
4269
4270 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004271 public boolean injectEvent(InputEvent event) {
4272 final long origId = Binder.clearCallingIdentity();
4273 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004274 synchronized (mService) {
4275 if (mActivityDisplay != null) {
4276 return mInputManagerInternal.injectInputEvent(event,
4277 mActivityDisplay.mDisplayId,
4278 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4279 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004280 }
4281 return false;
4282 } finally {
4283 Binder.restoreCallingIdentity(origId);
4284 }
4285 }
4286
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004287 @Override
4288 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004289 synchronized (mService) {
4290 if (mContainerState == CONTAINER_STATE_FINISHING) {
4291 return;
4292 }
4293 mContainerState = CONTAINER_STATE_FINISHING;
4294
Craig Mautnerd163e752014-06-13 17:18:47 -07004295 long origId = Binder.clearCallingIdentity();
4296 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004297 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004298 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004299 } finally {
4300 Binder.restoreCallingIdentity(origId);
4301 }
4302 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004303 }
4304
Craig Mautner60257702014-09-17 15:02:33 -07004305 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004306 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004307 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004308 if (mActivityDisplay != null) {
4309 mActivityDisplay.detachActivitiesLocked(mStack);
4310 mActivityDisplay = null;
4311 mStack.mDisplayId = -1;
4312 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08004313 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004314 }
4315 }
4316
4317 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004318 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004319 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004320 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004321 Binder.getCallingUid(), mCurrentUser, false,
4322 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004323
Craig Mautnere0a38842013-12-16 16:14:02 -08004324 // TODO: Switch to user app stacks here.
4325 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004326 final Uri data = intent.getData();
4327 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4328 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004329 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004330 checkEmbeddedAllowedInner(userId, intent, mimeType);
4331
4332 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4333 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004334 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004335 }
4336
4337 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004338 public final int startActivityIntentSender(IIntentSender intentSender)
4339 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004340 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4341
4342 if (!(intentSender instanceof PendingIntentRecord)) {
4343 throw new IllegalArgumentException("Bad PendingIntent object");
4344 }
4345
Craig Mautnerb9168362015-02-26 20:40:19 -08004346 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004347 Binder.getCallingUid(), mCurrentUser, false,
4348 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004349
4350 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4351 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4352 pendingIntent.key.requestResolvedType);
4353
4354 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4355 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4356 }
4357
4358 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004359 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004360 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004361 throw new SecurityException(
4362 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4363 }
4364 }
4365
Craig Mautnerdf88d732014-01-27 09:21:32 -08004366 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004367 public IBinder asBinder() {
4368 return this;
4369 }
4370
Craig Mautner4504de52013-12-20 09:06:56 -08004371 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004372 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004373 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004374 }
4375
Craig Mautner4a1cb222013-12-04 16:14:06 -08004376 ActivityStackSupervisor getOuter() {
4377 return ActivityStackSupervisor.this;
4378 }
4379
Craig Mautnerd163e752014-06-13 17:18:47 -07004380 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004381 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004382 }
4383
4384 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004385 synchronized (mService) {
4386 if (mActivityDisplay != null) {
4387 mActivityDisplay.getBounds(outBounds);
4388 } else {
4389 outBounds.set(0, 0);
4390 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004391 }
4392 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004393
Craig Mautner6985bad2014-04-21 15:22:06 -07004394 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004395 void setVisible(boolean visible) {
4396 if (mVisible != visible) {
4397 mVisible = visible;
4398 if (mCallback != null) {
4399 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4400 0 /* unused */, this).sendToTarget();
4401 }
4402 }
4403 }
4404
Craig Mautner6985bad2014-04-21 15:22:06 -07004405 void setDrawn() {
4406 }
4407
Craig Mautner1b4bf852014-05-26 15:06:32 -07004408 // You can always start a new task on a regular ActivityStack.
4409 boolean isEligibleForNewTasks() {
4410 return true;
4411 }
4412
Craig Mautnerd163e752014-06-13 17:18:47 -07004413 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004414 detachLocked();
4415 deleteActivityContainer(this);
4416 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004417 }
4418
Craig Mautner34b73df2014-01-12 21:11:08 -08004419 @Override
4420 public String toString() {
4421 return mIdString + (mActivityDisplay == null ? "N" : "A");
4422 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004423 }
4424
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004425 private class VirtualActivityContainer extends ActivityContainer {
4426 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004427 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004428
4429 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4430 super(getNextStackId());
4431 mParentActivity = parent;
4432 mCallback = callback;
4433 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004434 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004435 }
4436
4437 @Override
4438 public void setSurface(Surface surface, int width, int height, int density) {
4439 super.setSurface(surface, width, height, density);
4440
4441 synchronized (mService) {
4442 final long origId = Binder.clearCallingIdentity();
4443 try {
4444 setSurfaceLocked(surface, width, height, density);
4445 } finally {
4446 Binder.restoreCallingIdentity(origId);
4447 }
4448 }
4449 }
4450
4451 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4452 if (mContainerState == CONTAINER_STATE_FINISHING) {
4453 return;
4454 }
4455 VirtualActivityDisplay virtualActivityDisplay =
4456 (VirtualActivityDisplay) mActivityDisplay;
4457 if (virtualActivityDisplay == null) {
4458 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004459 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004460 mActivityDisplay = virtualActivityDisplay;
4461 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004462 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004463 }
4464
4465 if (mSurface != null) {
4466 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004467 }
4468
Craig Mautner6985bad2014-04-21 15:22:06 -07004469 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004470 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004471 mStack.resumeTopActivityLocked(null);
4472 } else {
4473 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004474 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004475 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004476 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004477 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004478 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004479
Craig Mautnerd163e752014-06-13 17:18:47 -07004480 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004481
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004482 if (DEBUG_STACK) Slog.d(TAG_STACK,
4483 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004484 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004485
Craig Mautner6985bad2014-04-21 15:22:06 -07004486 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004487 boolean isAttachedLocked() {
4488 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004489 }
4490
4491 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004492 void setDrawn() {
4493 synchronized (mService) {
4494 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004495 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004496 }
4497 }
4498
Craig Mautner1b4bf852014-05-26 15:06:32 -07004499 // Never start a new task on an ActivityView if it isn't explicitly specified.
4500 @Override
4501 boolean isEligibleForNewTasks() {
4502 return false;
4503 }
4504
Craig Mautnerd163e752014-06-13 17:18:47 -07004505 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004506 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004507 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4508 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4509 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4510 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4511 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004512 }
4513 }
4514
Craig Mautner4a1cb222013-12-04 16:14:06 -08004515 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4516 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004517 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004518 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004519 int mDisplayId;
4520 Display mDisplay;
4521 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004522
Craig Mautner4a1cb222013-12-04 16:14:06 -08004523 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4524 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004525 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004526
Jose Lima4b6c6692014-08-12 17:41:12 -07004527 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004528
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004529 ActivityDisplay() {
4530 }
Craig Mautner4504de52013-12-20 09:06:56 -08004531
Craig Mautner1a70a162014-09-13 12:09:31 -07004532 // After instantiation, check that mDisplay is not null before using this. The alternative
4533 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004534 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004535 final Display display = mDisplayManager.getDisplay(displayId);
4536 if (display == null) {
4537 return;
4538 }
4539 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004540 }
4541
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004542 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004543 mDisplay = display;
4544 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004545 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004546 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004547
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004548 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004549 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004550 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4551 + " onTop=" + onTop);
4552 if (onTop) {
4553 mStacks.add(stack);
4554 } else {
4555 mStacks.add(0, stack);
4556 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004557 }
4558
Craig Mautnere0a38842013-12-16 16:14:02 -08004559 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004560 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004561 + " from displayId=" + mDisplayId);
4562 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004563 }
4564
4565 void getBounds(Point bounds) {
4566 mDisplay.getDisplayInfo(mDisplayInfo);
4567 bounds.x = mDisplayInfo.appWidth;
4568 bounds.y = mDisplayInfo.appHeight;
4569 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004570
Jose Lima4b6c6692014-08-12 17:41:12 -07004571 void setVisibleBehindActivity(ActivityRecord r) {
4572 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004573 }
4574
Jose Lima4b6c6692014-08-12 17:41:12 -07004575 boolean hasVisibleBehindActivity() {
4576 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004577 }
4578
Craig Mautner34b73df2014-01-12 21:11:08 -08004579 @Override
4580 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004581 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4582 }
4583 }
4584
4585 class VirtualActivityDisplay extends ActivityDisplay {
4586 VirtualDisplay mVirtualDisplay;
4587
Craig Mautner6985bad2014-04-21 15:22:06 -07004588 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004589 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004590 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4591 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4592 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4593 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004594
4595 init(mVirtualDisplay.getDisplay());
4596
4597 mWindowManager.handleDisplayAdded(mDisplayId);
4598 }
4599
4600 void setSurface(Surface surface) {
4601 if (mVirtualDisplay != null) {
4602 mVirtualDisplay.setSurface(surface);
4603 }
4604 }
4605
4606 @Override
4607 void detachActivitiesLocked(ActivityStack stack) {
4608 super.detachActivitiesLocked(stack);
4609 if (mVirtualDisplay != null) {
4610 mVirtualDisplay.release();
4611 mVirtualDisplay = null;
4612 }
4613 }
4614
4615 @Override
4616 public String toString() {
4617 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004618 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004619 }
Jose Lima58e66d62014-05-27 20:00:27 -07004620
4621 private boolean isLeanbackOnlyDevice() {
4622 boolean onLeanbackOnly = false;
4623 try {
4624 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4625 PackageManager.FEATURE_LEANBACK_ONLY);
4626 } catch (RemoteException e) {
4627 // noop
4628 }
4629
4630 return onLeanbackOnly;
4631 }
Craig Mautner27084302013-03-25 08:05:25 -07004632}