blob: 1195e83915577258b98cbd392c2335062557a9cb [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Craig Mautner15df08a2015-04-01 12:17:18 -070020import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
21import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
22import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070023import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
24import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070025import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
26import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070027import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070028import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080029import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070030import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070031import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
32import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
33import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070034import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070035import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
36import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010037import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070038import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
39import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070040
Svetoslav7008b512015-06-24 18:47:07 -070041import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070042import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080044import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070045import android.app.ActivityOptions;
46import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070047import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080048import android.app.IActivityContainer;
49import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070050import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070051import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070052import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070053import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070054import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070055import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070056import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040057import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040058import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070059import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070060import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070062import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070063import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070064import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070066import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070067import android.content.pm.PackageManager;
68import android.content.pm.ResolveInfo;
69import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080070import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080071import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080072import android.hardware.display.DisplayManager;
73import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080074import android.hardware.display.DisplayManagerGlobal;
75import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080076import android.hardware.input.InputManager;
77import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080078import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070079import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070080import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070081import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070082import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070083import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070084import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070085import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070086import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070087import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070088import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070089import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040090import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070091import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070092import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070093import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070094import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070095import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040096import android.provider.Settings;
97import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070098import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070099import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700100import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700101import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700102import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800103import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700104
Craig Mautner4a1cb222013-12-04 16:14:06 -0800105import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800106import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800107import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800108import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800109import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700110import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700111import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800112import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700113import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400114import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700115import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400116import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800117import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700118import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700119import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700120
justinzhang5286d3f2014-05-12 17:06:01 -0400121
Craig Mautner8d341ef2013-03-26 09:03:27 -0700122import java.io.FileDescriptor;
123import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700124import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700125import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700126import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700127import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700128import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700129
Craig Mautner4a1cb222013-12-04 16:14:06 -0800130public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800131 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700132 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700133 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700134 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700135 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700136 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700137 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700138 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700139 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
140 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700141 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700142 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700143 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
144 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700145 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700146 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800147
Craig Mautner2219a1b2013-03-25 09:44:30 -0700148 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700149
Craig Mautnerf3333272013-04-22 10:55:53 -0700150 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700151 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700152
Craig Mautner0eea92c2013-05-16 13:35:39 -0700153 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700154 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700155
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700156 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700157 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700158
Craig Mautner05d29032013-05-03 13:40:13 -0700159 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
160 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
161 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700162 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700163 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800164 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
165 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
166 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700167 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400168 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
169 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700170 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700171 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700172 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800173
Craig Mautner4504de52013-12-20 09:06:56 -0800174 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700175
Jason Monk62515be2014-05-21 16:06:19 -0400176 private static final String LOCK_TASK_TAG = "Lock-to-App";
177
Svetoslav7008b512015-06-24 18:47:07 -0700178 // Activity actions an app cannot start if it uses a permission which is not granted.
179 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
180 new ArrayMap<>();
181 static {
182 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
183 Manifest.permission.CAMERA);
184 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
185 Manifest.permission.CAMERA);
186 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
187 Manifest.permission.CALL_PHONE);
188 }
189
Svet Ganov99b60432015-06-27 13:15:22 -0700190 /** Action restriction: launching the activity is not restricted. */
191 private static final int ACTIVITY_RESTRICTION_NONE = 0;
192 /** Action restriction: launching the activity is restricted by a permission. */
193 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
194 /** Action restriction: launching the activity is restricted by an app op. */
195 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700196
justinzhang5286d3f2014-05-12 17:06:01 -0400197 /** Status Bar Service **/
198 private IBinder mToken = new Binder();
199 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400200 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400201
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700202 // For debugging to make sure the caller when acquiring/releasing our
203 // wake lock is the system process.
204 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700205
Craig Mautner27084302013-03-25 08:05:25 -0700206 final ActivityManagerService mService;
207
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800208 private final RecentTasks mRecentTasks;
209
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700210 final ActivityStackSupervisorHandler mHandler;
211
212 /** Short cut */
213 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800214 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700215
Craig Mautner8d341ef2013-03-26 09:03:27 -0700216 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700217 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700218
219 /** Task identifier that activities are currently being started in. Incremented each time a
220 * new task is created. */
221 private int mCurTaskId = 0;
222
Craig Mautner2420ead2013-04-01 17:13:20 -0700223 /** The current user */
224 private int mCurrentUser;
225
Craig Mautnere0a38842013-12-16 16:14:02 -0800226 /** The stack containing the launcher app. Assumed to always be attached to
227 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700228 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700229
Craig Mautnere0a38842013-12-16 16:14:02 -0800230 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700231 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700232
Craig Mautner4a1cb222013-12-04 16:14:06 -0800233 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
234 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800235 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800236 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700237
238 /** List of activities that are waiting for a new activity to become visible before completing
239 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800240 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700241
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700242 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800243 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700244
245 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800246 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700247
Craig Mautnerde4ef022013-04-07 19:01:33 -0700248 /** List of activities that are ready to be stopped, but waiting for the next activity to
249 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800250 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700251
Craig Mautnerf3333272013-04-22 10:55:53 -0700252 /** List of activities that are ready to be finished, but waiting for the previous activity to
253 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800254 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700255
Craig Mautner0eea92c2013-05-16 13:35:39 -0700256 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800257 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700258
Craig Mautnerf3333272013-04-22 10:55:53 -0700259 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700260 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700261
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700262 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700263 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700264
Craig Mautnerde4ef022013-04-07 19:01:33 -0700265 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
266 * is being brought in front of us. */
267 boolean mUserLeaving = false;
268
Craig Mautner0eea92c2013-05-16 13:35:39 -0700269 /** Set when we have taken too long waiting to go to sleep. */
270 boolean mSleepTimeout = false;
271
Jose Lima58e66d62014-05-27 20:00:27 -0700272 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
273 * setWindowManager is called. **/
274 private boolean mLeanbackOnlyDevice;
275
Craig Mautner0eea92c2013-05-16 13:35:39 -0700276 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700277 * We don't want to allow the device to go to sleep while in the process
278 * of launching an activity. This is primarily to allow alarm intent
279 * receivers to launch an activity and get that to run before the device
280 * goes back to sleep.
281 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700282 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700283
284 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700285 * Set when the system is going to sleep, until we have
286 * successfully paused the current activity and released our wake lock.
287 * At that point the system is allowed to actually sleep.
288 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700289 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700290
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700291 /** Stack id of the front stack when user switched, indexed by userId. */
292 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700293
Craig Mautner4504de52013-12-20 09:06:56 -0800294 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800295 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700296 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800297
298 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700299 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800300
Jeff Brownca9bc702014-02-11 14:32:56 -0800301 InputManagerInternal mInputManagerInternal;
302
Craig Mautner15df08a2015-04-01 12:17:18 -0700303 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
304 * may be finished until there is only one entry left. If this is empty the system is not
305 * in lockTask mode. */
306 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000307 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700308 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
309 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000310 */
311 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400312 /**
313 * Notifies the user when entering/exiting lock-task.
314 */
315 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800316
Craig Mautner15df08a2015-04-01 12:17:18 -0700317 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700318
Craig Mautner42d04db2014-11-06 12:13:23 -0800319 /** Used to keep resumeTopActivityLocked() from being entered recursively */
320 boolean inResumeTopActivity;
321
Craig Mautneree36c772014-07-16 14:56:05 -0700322 /**
323 * Description of a request to start a new activity, which has been held
324 * due to app switches being disabled.
325 */
326 static class PendingActivityLaunch {
327 final ActivityRecord r;
328 final ActivityRecord sourceRecord;
329 final int startFlags;
330 final ActivityStack stack;
331
332 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
333 int _startFlags, ActivityStack _stack) {
334 r = _r;
335 sourceRecord = _sourceRecord;
336 startFlags = _startFlags;
337 stack = _stack;
338 }
339 }
340
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800341 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700342 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800343 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700344 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
345 }
346
347 /**
348 * At the time when the constructor runs, the power manager has not yet been
349 * initialized. So we initialize our wakelocks afterwards.
350 */
351 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800352 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700353 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700354 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700355 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700356 }
357
justinzhang5286d3f2014-05-12 17:06:01 -0400358 // This function returns a IStatusBarService. The value is from ServiceManager.
359 // getService and is cached.
360 private IStatusBarService getStatusBarService() {
361 synchronized (mService) {
362 if (mStatusBarService == null) {
363 mStatusBarService = IStatusBarService.Stub.asInterface(
364 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
365 if (mStatusBarService == null) {
366 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
367 }
368 }
369 return mStatusBarService;
370 }
371 }
372
Jason Monk35c62a42014-06-17 10:24:47 -0400373 private IDevicePolicyManager getDevicePolicyManager() {
374 synchronized (mService) {
375 if (mDevicePolicyManager == null) {
376 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
377 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
378 if (mDevicePolicyManager == null) {
379 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
380 }
381 }
382 return mDevicePolicyManager;
383 }
384 }
385
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700386 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800387 synchronized (mService) {
388 mWindowManager = wm;
389
390 mDisplayManager =
391 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
392 mDisplayManager.registerDisplayListener(this, null);
393
394 Display[] displays = mDisplayManager.getDisplays();
395 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
396 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800397 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700398 if (activityDisplay.mDisplay == null) {
399 throw new IllegalStateException("Default Display does not exist");
400 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800401 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800402 }
403
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700404 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800405 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800406
407 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700408
409 // Initialize this here, now that we can get a valid reference to PackageManager.
410 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800411 }
Craig Mautner27084302013-03-25 08:05:25 -0700412 }
413
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200414 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700415 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200416 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700417 }
418
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700419 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800420 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700421 }
422
Craig Mautnerde4ef022013-04-07 19:01:33 -0700423 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800424 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700425 }
426
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800427 /** Top of all visible stacks is/should always be equal to the focused stack.
428 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
429 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700430 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700431 if (stack == null) {
432 return false;
433 }
434
Craig Mautnerdf88d732014-01-27 09:21:32 -0800435 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
436 if (parent != null) {
437 stack = parent.task.stack;
438 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800439 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700440 }
441
Craig Mautner299f9602015-01-26 09:47:33 -0800442 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800443 moveHomeStack(toFront, reason, null);
444 }
445
446 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800447 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800448 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800449 if (topNdx <= 0) {
450 return;
451 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700452
453 // The home stack should either be at the top or bottom of the stack list.
454 if ((toFront && (stacks.get(topNdx) != mHomeStack))
455 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700456 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700457 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
458 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800459 stacks.remove(mHomeStack);
460 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700461 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800462
463 if (lastFocusedStack != null) {
464 mLastFocusedStack = lastFocusedStack;
465 }
466 mFocusedStack = stacks.get(topNdx);
467
Craig Mautnerde313752015-01-22 14:28:03 -0800468 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
469 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800470 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800471
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800472 if (mService.mBooting || !mService.mBooted) {
473 final ActivityRecord r = topRunningActivityLocked();
474 if (r != null && r.idle) {
475 checkFinishBootingLocked();
476 }
477 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700478 }
479
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700480 /** Returns true if the focus activity was adjusted to the home stack top activity. */
481 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700482 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
483 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700484 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700485 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700486
Craig Mautner84984fa2014-06-19 11:19:20 -0700487 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700488
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700489 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700490 if (top == null) {
491 return false;
492 }
493 mService.setFocusedActivityLocked(top, reason);
494 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700495 }
496
Craig Mautner299f9602015-01-26 09:47:33 -0800497 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700498 if (!mService.mBooting && !mService.mBooted) {
499 // Not ready yet!
500 return false;
501 }
502
Craig Mautner84984fa2014-06-19 11:19:20 -0700503 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
504 mWindowManager.showRecentApps();
505 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700506 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700507
Craig Mautner84984fa2014-06-19 11:19:20 -0700508 if (prev != null) {
509 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
510 }
511
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700512 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
513 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800514 // Only resume home activity if isn't finishing.
515 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800516 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700517 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700518 }
Craig Mautner299f9602015-01-26 09:47:33 -0800519 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700520 }
521
Craig Mautner8d341ef2013-03-26 09:03:27 -0700522 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700523 return anyTaskForIdLocked(id, true);
524 }
525
526 /**
527 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
528 * @param id Id of the task we would like returned.
529 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
530 * restore the task from recents to the active list.
531 */
532 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800533 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800534 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800535 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800536 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
537 ActivityStack stack = stacks.get(stackNdx);
538 TaskRecord task = stack.taskForIdLocked(id);
539 if (task != null) {
540 return task;
541 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700542 }
543 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800544
545 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700546 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800547 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800548 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700549 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800550 return null;
551 }
552
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700553 if (!restoreFromRecents) {
554 return task;
555 }
556
Wale Ogunwale7de05352014-12-12 15:21:33 -0800557 if (!restoreRecentTaskLocked(task)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700558 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
559 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800560 return null;
561 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700562 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800563 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700564 }
565
Craig Mautner6170f732013-04-02 13:05:23 -0700566 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800567 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800568 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800569 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800570 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
571 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
572 if (r != null) {
573 return r;
574 }
Craig Mautner6170f732013-04-02 13:05:23 -0700575 }
576 }
577 return null;
578 }
579
Craig Mautneref73ee12014-04-23 11:45:37 -0700580 void setNextTaskId(int taskId) {
581 if (taskId > mCurTaskId) {
582 mCurTaskId = taskId;
583 }
584 }
585
Craig Mautner8d341ef2013-03-26 09:03:27 -0700586 int getNextTaskId() {
587 do {
588 mCurTaskId++;
589 if (mCurTaskId <= 0) {
590 mCurTaskId = 1;
591 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700592 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700593 return mCurTaskId;
594 }
595
Craig Mautnerde4ef022013-04-07 19:01:33 -0700596 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800597 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700598 if (stack == null) {
599 return null;
600 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700601 ActivityRecord resumedActivity = stack.mResumedActivity;
602 if (resumedActivity == null || resumedActivity.app == null) {
603 resumedActivity = stack.mPausingActivity;
604 if (resumedActivity == null || resumedActivity.app == null) {
605 resumedActivity = stack.topRunningActivityLocked(null);
606 }
607 }
608 return resumedActivity;
609 }
610
Dianne Hackbornff072722014-09-24 10:56:28 -0700611 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700612 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800613 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800614 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
615 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800616 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
617 final ActivityStack stack = stacks.get(stackNdx);
618 if (!isFrontStack(stack)) {
619 continue;
620 }
621 ActivityRecord hr = stack.topRunningActivityLocked(null);
622 if (hr != null) {
623 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
624 && processName.equals(hr.processName)) {
625 try {
George Mount2c92c972014-03-20 09:38:23 -0700626 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800627 didSomething = true;
628 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700629 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800630 Slog.w(TAG, "Exception in new application when starting activity "
631 + hr.intent.getComponent().flattenToShortString(), e);
632 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700633 }
Craig Mautner20e72272013-04-01 13:45:53 -0700634 }
Craig Mautner20e72272013-04-01 13:45:53 -0700635 }
636 }
637 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700638 if (!didSomething) {
639 ensureActivitiesVisibleLocked(null, 0);
640 }
Craig Mautner20e72272013-04-01 13:45:53 -0700641 return didSomething;
642 }
643
644 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800645 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
646 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800647 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
648 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800649 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800650 continue;
651 }
652 final ActivityRecord resumedActivity = stack.mResumedActivity;
653 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700654 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800655 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800656 return false;
657 }
Craig Mautner20e72272013-04-01 13:45:53 -0700658 }
659 }
660 return true;
661 }
662
Craig Mautnerde4ef022013-04-07 19:01:33 -0700663 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800664 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
665 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800666 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
667 final ActivityStack stack = stacks.get(stackNdx);
668 if (isFrontStack(stack)) {
669 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700670 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 return false;
672 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700673 }
674 }
675 }
676 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700677 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800678 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
679 mLastFocusedStack + " to=" + mFocusedStack);
680 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700681 return true;
682 }
683
684 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800685 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800686 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
687 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800688 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
689 final ActivityStack stack = stacks.get(stackNdx);
690 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800691 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700692 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800693 return false;
694 }
695 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800696 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700697 }
698 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800699 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700700 }
701
Craig Mautner2acc3892013-09-23 10:28:14 -0700702 /**
703 * Pause all activities in either all of the stacks or just the back stacks.
704 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700705 * @return true if any activity was paused as a result of this call.
706 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700707 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700708 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800709 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
710 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800711 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
712 final ActivityStack stack = stacks.get(stackNdx);
713 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700714 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800715 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700716 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
717 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800718 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700719 }
720 }
721 return someActivityPaused;
722 }
723
Craig Mautnerde4ef022013-04-07 19:01:33 -0700724 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700725 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800726 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
727 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800728 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
729 final ActivityStack stack = stacks.get(stackNdx);
730 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700731 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800732 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700733 Slog.d(TAG_STATES,
734 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800735 pausing = false;
736 } else {
737 return false;
738 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700739 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700740 }
741 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700742 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700743 }
744
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700745 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
746 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500747 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800748 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
749 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
750 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
751 final ActivityStack stack = stacks.get(stackNdx);
752 if (stack.mResumedActivity != null &&
753 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700754 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800755 }
756 }
757 }
758 }
759
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700760 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700761 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700762 }
763
764 void sendWaitingVisibleReportLocked(ActivityRecord r) {
765 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700766 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700767 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700768 if (w.who == null) {
769 changed = true;
770 w.timeout = false;
771 if (r != null) {
772 w.who = new ComponentName(r.info.packageName, r.info.name);
773 }
774 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
775 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700776 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700777 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700778 if (changed) {
779 mService.notifyAll();
780 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700781 }
782
783 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
784 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700785 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700786 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700787 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700788 if (w.who == null) {
789 changed = true;
790 w.timeout = timeout;
791 if (r != null) {
792 w.who = new ComponentName(r.info.packageName, r.info.name);
793 }
794 w.thisTime = thisTime;
795 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700796 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700797 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700798 if (changed) {
799 mService.notifyAll();
800 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700801 }
802
Craig Mautner29219d92013-04-16 20:19:12 -0700803 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800804 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700805 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
806 if (r != null) {
807 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700808 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700809
Craig Mautner4a1cb222013-12-04 16:14:06 -0800810 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800811 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800812 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
813 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700814 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700815 r = stack.topRunningActivityLocked(null);
816 if (r != null) {
817 return r;
818 }
819 }
820 }
821 return null;
822 }
823
Dianne Hackborn09233282014-04-30 11:33:59 -0700824 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700825 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800826 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
827 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800828 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800829 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800830 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800831 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
832 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700833 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800834 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700835 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700836 }
837 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700838
839 // The lists are already sorted from most recent to oldest. Just pull the most recent off
840 // each list and add it to list. Stop when all lists are empty or maxNum reached.
841 while (maxNum > 0) {
842 long mostRecentActiveTime = Long.MIN_VALUE;
843 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800844 final int numTaskLists = runningTaskLists.size();
845 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
846 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700847 if (!stackTaskList.isEmpty()) {
848 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
849 if (lastActiveTime > mostRecentActiveTime) {
850 mostRecentActiveTime = lastActiveTime;
851 selectedStackList = stackTaskList;
852 }
853 }
854 }
855 if (selectedStackList != null) {
856 list.add(selectedStackList.remove(0));
857 --maxNum;
858 } else {
859 break;
860 }
861 }
Craig Mautner20e72272013-04-01 13:45:53 -0700862 }
863
Craig Mautner23ac33b2013-04-01 16:26:35 -0700864 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700865 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700866 // Collect information about the target of the Intent.
867 ActivityInfo aInfo;
868 try {
869 ResolveInfo rInfo =
870 AppGlobals.getPackageManager().resolveIntent(
871 intent, resolvedType,
872 PackageManager.MATCH_DEFAULT_ONLY
873 | ActivityManagerService.STOCK_PM_FLAGS, userId);
874 aInfo = rInfo != null ? rInfo.activityInfo : null;
875 } catch (RemoteException e) {
876 aInfo = null;
877 }
878
879 if (aInfo != null) {
880 // Store the found target back into the intent, because now that
881 // we have it we never want to do this again. For example, if the
882 // user navigates back to this point in the history, we should
883 // always restart the exact same activity.
884 intent.setComponent(new ComponentName(
885 aInfo.applicationInfo.packageName, aInfo.name));
886
887 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700888 if (!aInfo.processName.equals("system")) {
889 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700890 mService.setDebugApp(aInfo.processName, true, false);
891 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700892
Man Caocfa78b22015-06-11 20:14:34 -0700893 if ((startFlags & ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700894 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
895 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700896
Man Caocfa78b22015-06-11 20:14:34 -0700897 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
898 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
899 }
900
901 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700902 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700903 }
904 }
905 }
906 return aInfo;
907 }
908
Craig Mautner299f9602015-01-26 09:47:33 -0800909 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
910 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700911 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
912 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
913 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
914 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700915 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
916 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700917 null /* outActivity */, null /* container */, null /* inTask */);
918 if (inResumeTopActivity) {
919 // If we are in resume section already, home activity will be initialized, but not
920 // resumed (to avoid recursive resume) and will stay that way until something pokes it
921 // again. We need to schedule another resume.
922 scheduleResumeTopActivities();
923 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700924 }
925
Craig Mautner23ac33b2013-04-01 16:26:35 -0700926 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700927 String callingPackage, Intent intent, String resolvedType,
928 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700929 IBinder resultTo, String resultWho, int requestCode, int startFlags,
930 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700931 Bundle options, boolean ignoreTargetSecurity, int userId,
932 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700933 // Refuse possible leaked file descriptors
934 if (intent != null && intent.hasFileDescriptors()) {
935 throw new IllegalArgumentException("File descriptors passed in Intent");
936 }
937 boolean componentSpecified = intent.getComponent() != null;
938
939 // Don't modify the client's object!
940 intent = new Intent(intent);
941
942 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700943 ActivityInfo aInfo =
944 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700945
Craig Mautnere0a38842013-12-16 16:14:02 -0800946 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700947 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800948 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700949 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800950 // Cannot start a child activity if the parent is not resumed.
951 return ActivityManager.START_CANCELED;
952 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700953 final int realCallingPid = Binder.getCallingPid();
954 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700955 int callingPid;
956 if (callingUid >= 0) {
957 callingPid = -1;
958 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700959 callingPid = realCallingPid;
960 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700961 } else {
962 callingPid = callingUid = -1;
963 }
964
Craig Mautnere0a38842013-12-16 16:14:02 -0800965 final ActivityStack stack;
966 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800967 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800968 } else {
969 stack = container.mStack;
970 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800971 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700972 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700973 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700974
975 final long origId = Binder.clearCallingIdentity();
976
977 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800978 (aInfo.applicationInfo.privateFlags
979 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700980 // This may be a heavy-weight process! Check to see if we already
981 // have another, different heavy-weight process running.
982 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
983 if (mService.mHeavyWeightProcess != null &&
984 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
985 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700986 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700987 if (caller != null) {
988 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
989 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700990 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700991 } else {
992 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700993 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700994 + intent.toString());
995 ActivityOptions.abort(options);
996 return ActivityManager.START_PERMISSION_DENIED;
997 }
998 }
999
1000 IIntentSender target = mService.getIntentSenderLocked(
1001 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001002 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001003 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1004 | PendingIntent.FLAG_ONE_SHOT, null);
1005
1006 Intent newIntent = new Intent();
1007 if (requestCode >= 0) {
1008 // Caller is requesting a result.
1009 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1010 }
1011 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1012 new IntentSender(target));
1013 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1014 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1015 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1016 hist.packageName);
1017 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1018 hist.task.taskId);
1019 }
1020 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1021 aInfo.packageName);
1022 newIntent.setFlags(intent.getFlags());
1023 newIntent.setClassName("android",
1024 HeavyWeightSwitcherActivity.class.getName());
1025 intent = newIntent;
1026 resolvedType = null;
1027 caller = null;
1028 callingUid = Binder.getCallingUid();
1029 callingPid = Binder.getCallingPid();
1030 componentSpecified = true;
1031 try {
1032 ResolveInfo rInfo =
1033 AppGlobals.getPackageManager().resolveIntent(
1034 intent, null,
1035 PackageManager.MATCH_DEFAULT_ONLY
1036 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1037 aInfo = rInfo != null ? rInfo.activityInfo : null;
1038 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1039 } catch (RemoteException e) {
1040 aInfo = null;
1041 }
1042 }
1043 }
1044 }
1045
Dianne Hackborn91097de2014-04-04 18:02:06 -07001046 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1047 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001048 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001049 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001050 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001051
Craig Mautner85c11a82014-07-17 12:38:18 -07001052 Binder.restoreCallingIdentity(origId);
1053
Craig Mautnerde4ef022013-04-07 19:01:33 -07001054 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001055 // If the caller also wants to switch to a new configuration,
1056 // do so now. This allows a clean switch, as we are waiting
1057 // for the current activity to pause (so we will not destroy
1058 // it), and have not yet started the next activity.
1059 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1060 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001061 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001062 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001063 "Updating to new configuration after starting activity.");
1064 mService.updateConfigurationLocked(config, null, false, false);
1065 }
1066
Craig Mautner23ac33b2013-04-01 16:26:35 -07001067 if (outResult != null) {
1068 outResult.result = res;
1069 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001070 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001071 do {
1072 try {
1073 mService.wait();
1074 } catch (InterruptedException e) {
1075 }
1076 } while (!outResult.timeout && outResult.who == null);
1077 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001078 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001079 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001080 outResult.timeout = false;
1081 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1082 outResult.totalTime = 0;
1083 outResult.thisTime = 0;
1084 } else {
1085 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001086 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001087 do {
1088 try {
1089 mService.wait();
1090 } catch (InterruptedException e) {
1091 }
1092 } while (!outResult.timeout && outResult.who == null);
1093 }
1094 }
1095 }
1096
1097 return res;
1098 }
1099 }
1100
1101 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1102 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1103 Bundle options, int userId) {
1104 if (intents == null) {
1105 throw new NullPointerException("intents is null");
1106 }
1107 if (resolvedTypes == null) {
1108 throw new NullPointerException("resolvedTypes is null");
1109 }
1110 if (intents.length != resolvedTypes.length) {
1111 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1112 }
1113
Craig Mautner23ac33b2013-04-01 16:26:35 -07001114
1115 int callingPid;
1116 if (callingUid >= 0) {
1117 callingPid = -1;
1118 } else if (caller == null) {
1119 callingPid = Binder.getCallingPid();
1120 callingUid = Binder.getCallingUid();
1121 } else {
1122 callingPid = callingUid = -1;
1123 }
1124 final long origId = Binder.clearCallingIdentity();
1125 try {
1126 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001127 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001128 for (int i=0; i<intents.length; i++) {
1129 Intent intent = intents[i];
1130 if (intent == null) {
1131 continue;
1132 }
1133
1134 // Refuse possible leaked file descriptors
1135 if (intent != null && intent.hasFileDescriptors()) {
1136 throw new IllegalArgumentException("File descriptors passed in Intent");
1137 }
1138
1139 boolean componentSpecified = intent.getComponent() != null;
1140
1141 // Don't modify the client's object!
1142 intent = new Intent(intent);
1143
1144 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001145 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001146 // TODO: New, check if this is correct
1147 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1148
1149 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001150 (aInfo.applicationInfo.privateFlags
1151 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001152 throw new IllegalArgumentException(
1153 "FLAG_CANT_SAVE_STATE not supported here");
1154 }
1155
1156 Bundle theseOptions;
1157 if (options != null && i == intents.length-1) {
1158 theseOptions = options;
1159 } else {
1160 theseOptions = null;
1161 }
Craig Mautner6170f732013-04-02 13:05:23 -07001162 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001163 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1164 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001165 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001166 if (res < 0) {
1167 return res;
1168 }
1169
1170 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1171 }
1172 }
1173 } finally {
1174 Binder.restoreCallingIdentity(origId);
1175 }
1176
1177 return ActivityManager.START_SUCCESS;
1178 }
1179
Craig Mautner2420ead2013-04-01 17:13:20 -07001180 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001181 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001182 throws RemoteException {
1183
Craig Mautner2568c3a2015-03-26 14:22:34 -07001184 if (andResume) {
1185 r.startFreezingScreenLocked(app, 0);
1186 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001187
Craig Mautner2568c3a2015-03-26 14:22:34 -07001188 // schedule launch ticks to collect information about slow apps.
1189 r.startLaunchTickingLocked();
1190 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001191
1192 // Have the window manager re-evaluate the orientation of
1193 // the screen based on the new activity order. Note that
1194 // as a result of this, it can call back into the activity
1195 // manager with a new orientation. We don't care about that,
1196 // because the activity is not currently running so we are
1197 // just restarting it anyway.
1198 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001199 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001200 mService.mConfiguration,
1201 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1202 mService.updateConfigurationLocked(config, r, false, false);
1203 }
1204
1205 r.app = app;
1206 app.waitingToKill = null;
1207 r.launchCount++;
1208 r.lastLaunchTime = SystemClock.uptimeMillis();
1209
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001210 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001211
1212 int idx = app.activities.indexOf(r);
1213 if (idx < 0) {
1214 app.activities.add(r);
1215 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001216 mService.updateLruProcessLocked(app, true, null);
1217 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001218
Craig Mautner15df08a2015-04-01 12:17:18 -07001219 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001220 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1221 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001222 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001223 }
1224
1225 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001226 try {
1227 if (app.thread == null) {
1228 throw new RemoteException();
1229 }
1230 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001231 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001232 if (andResume) {
1233 results = r.results;
1234 newIntents = r.newIntents;
1235 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001236 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1237 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1238 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001239 if (andResume) {
1240 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1241 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001242 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001243 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001244 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001245 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001246 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001247 }
1248 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1249 r.sleeping = false;
1250 r.forceNewConfig = false;
1251 mService.showAskCompatModeDialogLocked(r);
1252 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001253 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001254 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1255 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1256 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001257 final String profileFile = mService.mProfileFile;
1258 if (profileFile != null) {
1259 ParcelFileDescriptor profileFd = mService.mProfileFd;
1260 if (profileFd != null) {
1261 try {
1262 profileFd = profileFd.dup();
1263 } catch (IOException e) {
1264 if (profileFd != null) {
1265 try {
1266 profileFd.close();
1267 } catch (IOException o) {
1268 }
1269 profileFd = null;
1270 }
1271 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001272 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001273
1274 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1275 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001276 }
1277 }
1278 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001279
Craig Mautner2568c3a2015-03-26 14:22:34 -07001280 if (andResume) {
1281 app.hasShownUi = true;
1282 app.pendingUiClean = true;
1283 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001284 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001285 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001286 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001287 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001288 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001289 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001290
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001291 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001292 // This may be a heavy-weight process! Note that the package
1293 // manager will ensure that only activity can run in the main
1294 // process of the .apk, which is the only thing that will be
1295 // considered heavy-weight.
1296 if (app.processName.equals(app.info.packageName)) {
1297 if (mService.mHeavyWeightProcess != null
1298 && mService.mHeavyWeightProcess != app) {
1299 Slog.w(TAG, "Starting new heavy weight process " + app
1300 + " when already running "
1301 + mService.mHeavyWeightProcess);
1302 }
1303 mService.mHeavyWeightProcess = app;
1304 Message msg = mService.mHandler.obtainMessage(
1305 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1306 msg.obj = r;
1307 mService.mHandler.sendMessage(msg);
1308 }
1309 }
1310
1311 } catch (RemoteException e) {
1312 if (r.launchFailed) {
1313 // This is the second time we failed -- finish activity
1314 // and give up.
1315 Slog.e(TAG, "Second failure launching "
1316 + r.intent.getComponent().flattenToShortString()
1317 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001318 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001319 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1320 "2nd-crash", false);
1321 return false;
1322 }
1323
1324 // This is the first time we failed -- restart process and
1325 // retry.
1326 app.activities.remove(r);
1327 throw e;
1328 }
1329
1330 r.launchFailed = false;
1331 if (stack.updateLRUListLocked(r)) {
1332 Slog.w(TAG, "Activity " + r
1333 + " being launched, but already in LRU list");
1334 }
1335
1336 if (andResume) {
1337 // As part of the process of launching, ActivityThread also performs
1338 // a resume.
1339 stack.minimalResumeActivityLocked(r);
1340 } else {
1341 // This activity is not starting in the resumed state... which
1342 // should look like we asked it to pause+stop (but remain visible),
1343 // and it has done so and reported back the current icicle and
1344 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001345 if (DEBUG_STATES) Slog.v(TAG_STATES,
1346 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001347 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001348 r.stopped = true;
1349 }
1350
1351 // Launch the new version setup screen if needed. We do this -after-
1352 // launching the initial activity (that is, home), so that it can have
1353 // a chance to initialize itself while in the background, making the
1354 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001355 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001356 mService.startSetupActivityLocked();
1357 }
1358
Dianne Hackborn465fa392014-09-14 14:21:18 -07001359 // Update any services we are bound to that might care about whether
1360 // their client may have activities.
1361 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1362
Craig Mautner2420ead2013-04-01 17:13:20 -07001363 return true;
1364 }
1365
Craig Mautnere79d42682013-04-01 19:01:53 -07001366 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001367 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001368 // Is this activity's application already running?
1369 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001370 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001371
1372 r.task.stack.setLaunchTime(r);
1373
1374 if (app != null && app.thread != null) {
1375 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001376 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1377 || !"android".equals(r.info.packageName)) {
1378 // Don't add this if it is a platform component that is marked
1379 // to run in multiple processes, because this is actually
1380 // part of the framework so doesn't make sense to track as a
1381 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001382 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1383 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001384 }
George Mount2c92c972014-03-20 09:38:23 -07001385 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001386 return;
1387 } catch (RemoteException e) {
1388 Slog.w(TAG, "Exception when starting activity "
1389 + r.intent.getComponent().flattenToShortString(), e);
1390 }
1391
1392 // If a dead object exception was thrown -- fall through to
1393 // restart the application.
1394 }
1395
1396 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001397 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001398 }
1399
Craig Mautner6170f732013-04-02 13:05:23 -07001400 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001401 Intent intent, String resolvedType, ActivityInfo aInfo,
1402 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1403 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001404 int callingPid, int callingUid, String callingPackage,
1405 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001406 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1407 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001408 int err = ActivityManager.START_SUCCESS;
1409
1410 ProcessRecord callerApp = null;
1411 if (caller != null) {
1412 callerApp = mService.getRecordForAppLocked(caller);
1413 if (callerApp != null) {
1414 callingPid = callerApp.pid;
1415 callingUid = callerApp.info.uid;
1416 } else {
1417 Slog.w(TAG, "Unable to find app for caller " + caller
1418 + " (pid=" + callingPid + ") when starting: "
1419 + intent.toString());
1420 err = ActivityManager.START_PERMISSION_DENIED;
1421 }
1422 }
1423
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001424 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1425
Craig Mautner6170f732013-04-02 13:05:23 -07001426 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001427 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001428 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001429 + " on display " + (container == null ? (mFocusedStack == null ?
1430 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1431 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1432 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001433 }
1434
1435 ActivityRecord sourceRecord = null;
1436 ActivityRecord resultRecord = null;
1437 if (resultTo != null) {
1438 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001439 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1440 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001441 if (sourceRecord != null) {
1442 if (requestCode >= 0 && !sourceRecord.finishing) {
1443 resultRecord = sourceRecord;
1444 }
1445 }
1446 }
Craig Mautner6170f732013-04-02 13:05:23 -07001447
Dianne Hackborn91097de2014-04-04 18:02:06 -07001448 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001449
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001450 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001451 // Transfer the result target from the source activity to the new
1452 // one being started, including any failures.
1453 if (requestCode >= 0) {
1454 ActivityOptions.abort(options);
1455 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1456 }
1457 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001458 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1459 resultRecord = null;
1460 }
Craig Mautner6170f732013-04-02 13:05:23 -07001461 resultWho = sourceRecord.resultWho;
1462 requestCode = sourceRecord.requestCode;
1463 sourceRecord.resultTo = null;
1464 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001465 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001466 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001467 if (sourceRecord.launchedFromUid == callingUid) {
1468 // The new activity is being launched from the same uid as the previous
1469 // activity in the flow, and asking to forward its result back to the
1470 // previous. In this case the activity is serving as a trampoline between
1471 // the two, so we also want to update its launchedFromPackage to be the
1472 // same as the previous activity. Note that this is safe, since we know
1473 // these two packages come from the same uid; the caller could just as
1474 // well have supplied that same package name itself. This specifially
1475 // deals with the case of an intent picker/chooser being launched in the app
1476 // flow to redirect to an activity picked by the user, where we want the final
1477 // activity to consider it to have been launched by the previous app activity.
1478 callingPackage = sourceRecord.launchedFromPackage;
1479 }
Craig Mautner6170f732013-04-02 13:05:23 -07001480 }
1481
1482 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1483 // We couldn't find a class that can handle the given Intent.
1484 // That's the end of that!
1485 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1486 }
1487
1488 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1489 // We couldn't find the specific class specified in the Intent.
1490 // Also the end of the line.
1491 err = ActivityManager.START_CLASS_NOT_FOUND;
1492 }
1493
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001494 if (err == ActivityManager.START_SUCCESS
1495 && !isCurrentProfileLocked(userId)
1496 && (aInfo.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
1497 // Trying to launch a background activity that doesn't show for all users.
1498 err = ActivityManager.START_NOT_CURRENT_USER_ACTIVITY;
1499 }
1500
Dianne Hackborn91097de2014-04-04 18:02:06 -07001501 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1502 && sourceRecord.task.voiceSession != null) {
1503 // If this activity is being launched as part of a voice session, we need
1504 // to ensure that it is safe to do so. If the upcoming activity will also
1505 // be part of the voice session, we can only launch it if it has explicitly
1506 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001507 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001508 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1509 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001510 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001511 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1512 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001513 Slog.w(TAG,
1514 "Activity being started in current voice task does not support voice: "
1515 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001516 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1517 }
1518 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001519 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001520 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1521 }
1522 }
1523 }
1524
1525 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1526 // If the caller is starting a new voice session, just make sure the target
1527 // is actually allowing it to run this way.
1528 try {
1529 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1530 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001531 Slog.w(TAG,
1532 "Activity being started in new voice task does not support: "
1533 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001534 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1535 }
1536 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001537 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001538 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1539 }
1540 }
1541
louis_changcd5d1982014-08-01 10:09:08 +08001542 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1543
Craig Mautner6170f732013-04-02 13:05:23 -07001544 if (err != ActivityManager.START_SUCCESS) {
1545 if (resultRecord != null) {
1546 resultStack.sendActivityResultLocked(-1,
1547 resultRecord, resultWho, requestCode,
1548 Activity.RESULT_CANCELED, null);
1549 }
Craig Mautner6170f732013-04-02 13:05:23 -07001550 ActivityOptions.abort(options);
1551 return err;
1552 }
1553
Svetoslav7008b512015-06-24 18:47:07 -07001554 boolean abort = false;
1555
Svet Ganov99b60432015-06-27 13:15:22 -07001556 final int startAnyPerm = mService.checkPermission(
1557 START_ANY_ACTIVITY, callingPid, callingUid);
1558
1559 if (startAnyPerm != PERMISSION_GRANTED) {
1560 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001561 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001562 final int actionRestriction = getActionRestrictionForCallingPackage(
1563 intent.getAction(), callingPackage, callingPid, callingUid);
1564
1565 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1566 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1567 if (resultRecord != null) {
1568 resultStack.sendActivityResultLocked(-1,
1569 resultRecord, resultWho, requestCode,
1570 Activity.RESULT_CANCELED, null);
1571 }
1572 String msg;
1573 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1574 msg = "Permission Denial: starting " + intent.toString()
1575 + " from " + callerApp + " (pid=" + callingPid
1576 + ", uid=" + callingUid + ")" + " with revoked permission "
1577 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1578 } else if (!aInfo.exported) {
1579 msg = "Permission Denial: starting " + intent.toString()
1580 + " from " + callerApp + " (pid=" + callingPid
1581 + ", uid=" + callingUid + ")"
1582 + " not exported from uid " + aInfo.applicationInfo.uid;
1583 } else {
1584 msg = "Permission Denial: starting " + intent.toString()
1585 + " from " + callerApp + " (pid=" + callingPid
1586 + ", uid=" + callingUid + ")"
1587 + " requires " + aInfo.permission;
1588 }
1589 Slog.w(TAG, msg);
1590 throw new SecurityException(msg);
1591 }
1592
1593 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1594 String message = "Appop Denial: starting " + intent.toString()
1595 + " from " + callerApp + " (pid=" + callingPid
1596 + ", uid=" + callingUid + ")"
1597 + " requires " + AppOpsManager.permissionToOp(
1598 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1599 Slog.w(TAG, message);
1600 abort = true;
1601 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1602 String message = "Appop Denial: starting " + intent.toString()
1603 + " from " + callerApp + " (pid=" + callingPid
1604 + ", uid=" + callingUid + ")"
1605 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1606 Slog.w(TAG, message);
1607 abort = true;
1608 }
Svetoslav7008b512015-06-24 18:47:07 -07001609 }
1610
1611 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001612 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001613
Craig Mautner6170f732013-04-02 13:05:23 -07001614 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001615 try {
1616 // The Intent we give to the watcher has the extra data
1617 // stripped off, since it can contain private information.
1618 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001619 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001620 aInfo.applicationInfo.packageName);
1621 } catch (RemoteException e) {
1622 mService.mController = null;
1623 }
Ben Gruver5e207332013-04-03 17:41:37 -07001624 }
Craig Mautner6170f732013-04-02 13:05:23 -07001625
Ben Gruver5e207332013-04-03 17:41:37 -07001626 if (abort) {
1627 if (resultRecord != null) {
1628 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001629 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001630 }
Ben Gruver5e207332013-04-03 17:41:37 -07001631 // We pretend to the caller that it was really started, but
1632 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001633 ActivityOptions.abort(options);
1634 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001635 }
1636
1637 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001638 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001639 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001640 if (outActivity != null) {
1641 outActivity[0] = r;
1642 }
1643
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001644 if (r.appTimeTracker == null && sourceRecord != null) {
1645 // If the caller didn't specify an explicit time tracker, we want to continue
1646 // tracking under any it has.
1647 r.appTimeTracker = sourceRecord.appTimeTracker;
1648 }
1649
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001650 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001651 if (voiceSession == null && (stack.mResumedActivity == null
1652 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001653 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1654 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001655 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001656 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001657 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001658 ActivityOptions.abort(options);
1659 return ActivityManager.START_SWITCHES_CANCELED;
1660 }
1661 }
1662
1663 if (mService.mDidAppSwitch) {
1664 // This is the second allowed switch since we stopped switches,
1665 // so now just generally allow switches. Use case: user presses
1666 // home (switches disabled, switch to home, mDidAppSwitch now true);
1667 // user taps a home icon (coming from home so allowed, we hit here
1668 // and now allow anyone to switch again).
1669 mService.mAppSwitchesAllowedTime = 0;
1670 } else {
1671 mService.mDidAppSwitch = true;
1672 }
1673
Craig Mautneree36c772014-07-16 14:56:05 -07001674 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001675
Dianne Hackborn91097de2014-04-04 18:02:06 -07001676 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001677 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001678
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001679 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001680 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001681 // activity start, but we are not actually doing an activity
1682 // switch... just dismiss the keyguard now, because we
1683 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001684 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001685 }
1686 return err;
1687 }
1688
Svet Ganov99b60432015-06-27 13:15:22 -07001689 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001690 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001691 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1692 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001693 == PackageManager.PERMISSION_DENIED) {
1694 return ACTIVITY_RESTRICTION_PERMISSION;
1695 }
1696
Christopher Tateff7add02015-08-17 10:23:22 -07001697 if (activityInfo.permission == null) {
1698 return ACTIVITY_RESTRICTION_NONE;
1699 }
1700
Svet Ganov99b60432015-06-27 13:15:22 -07001701 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1702 if (opCode == AppOpsManager.OP_NONE) {
1703 return ACTIVITY_RESTRICTION_NONE;
1704 }
1705
1706 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1707 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001708 if (!ignoreTargetSecurity) {
1709 return ACTIVITY_RESTRICTION_APPOP;
1710 }
Svet Ganov99b60432015-06-27 13:15:22 -07001711 }
1712
1713 return ACTIVITY_RESTRICTION_NONE;
1714 }
1715
Svetoslav7008b512015-06-24 18:47:07 -07001716 private int getActionRestrictionForCallingPackage(String action,
1717 String callingPackage, int callingPid, int callingUid) {
1718 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001719 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001720 }
1721
1722 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1723 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001724 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001725 }
1726
1727 final PackageInfo packageInfo;
1728 try {
1729 packageInfo = mService.mContext.getPackageManager()
1730 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1731 } catch (PackageManager.NameNotFoundException e) {
1732 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001733 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001734 }
1735
1736 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001737 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001738 }
1739
1740 if (mService.checkPermission(permission, callingPid, callingUid) ==
1741 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001742 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001743 }
1744
1745 final int opCode = AppOpsManager.permissionToOpCode(permission);
1746 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001747 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001748 }
1749
1750 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1751 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001752 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001753 }
1754
Svet Ganov99b60432015-06-27 13:15:22 -07001755 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001756 }
1757
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001758 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001759 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001760
1761 // On leanback only devices we should keep all activities in the same stack.
1762 if (!mLeanbackOnlyDevice &&
1763 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001764
1765 ActivityStack stack;
1766
Wale Ogunwale7d701172015-03-11 15:36:30 -07001767 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001768 stack = task.stack;
1769 if (stack.isOnHomeDisplay()) {
1770 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001771 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1772 "computeStackFocus: Setting " + "focused stack to r=" + r
1773 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001774 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001775 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001776 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001777 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001778 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001779 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001780 }
1781
Craig Mautnere0a38842013-12-16 16:14:02 -08001782 final ActivityContainer container = r.mInitialActivityContainer;
1783 if (container != null) {
1784 // The first time put it on the desired stack, after this put on task stack.
1785 r.mInitialActivityContainer = null;
1786 return container.mStack;
1787 }
1788
Craig Mautner1b4bf852014-05-26 15:06:32 -07001789 if (mFocusedStack != mHomeStack && (!newTask ||
1790 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001791 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001792 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001793 return mFocusedStack;
1794 }
1795
Craig Mautnere0a38842013-12-16 16:14:02 -08001796 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1797 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001798 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001799 if (!stack.isHomeStack()) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001800 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001801 "computeStackFocus: Setting focused stack=" + stack);
1802 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001803 }
1804 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001805
Craig Mautner4a1cb222013-12-04 16:14:06 -08001806 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001807 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001808 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1809 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001810 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001811 }
1812 return mHomeStack;
1813 }
1814
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001815 boolean setFocusedStack(ActivityRecord r, String reason) {
1816 if (r == null) {
1817 // Not sure what you are trying to do, but it is not going to work...
1818 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001819 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001820 final TaskRecord task = r.task;
1821 if (task == null || task.stack == null) {
1822 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1823 return false;
1824 }
1825 task.stack.moveToFront(reason);
1826 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001827 }
1828
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001829 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001830 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001831 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001832 final Intent intent = r.intent;
1833 final int callingUid = r.launchedFromUid;
1834
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001835 // In some flows in to this function, we retrieve the task record and hold on to it
1836 // without a lock before calling back in to here... so the task at this point may
1837 // not actually be in recents. Check for that, and if it isn't in recents just
1838 // consider it invalid.
1839 if (inTask != null && !inTask.inRecents) {
1840 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1841 inTask = null;
1842 }
1843
Craig Mautnerad400af2014-08-13 16:41:36 -07001844 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001845 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1846 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001847
Craig Mautnera228ae92014-07-09 05:44:55 -07001848 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001849 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001850 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001851 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1852 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1853 "\"singleInstance\" or \"singleTask\"");
1854 launchFlags &=
1855 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1856 } else {
1857 switch (r.info.documentLaunchMode) {
1858 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1859 break;
1860 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1861 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1862 break;
1863 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1864 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1865 break;
1866 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1867 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1868 break;
1869 }
1870 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001871
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001872 final boolean launchTaskBehind = r.mLaunchTaskBehind
1873 && !launchSingleTask && !launchSingleInstance
1874 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001875
Wale Ogunwale7d701172015-03-11 15:36:30 -07001876 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1877 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001878 // For whatever reason this activity is being launched into a new
1879 // task... yet the caller has requested a result back. Well, that
1880 // is pretty messed up, so instead immediately send back a cancel
1881 // and let the new task continue launched as normal without a
1882 // dependency on its originator.
1883 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1884 r.resultTo.task.stack.sendActivityResultLocked(-1,
1885 r.resultTo, r.resultWho, r.requestCode,
1886 Activity.RESULT_CANCELED, null);
1887 r.resultTo = null;
1888 }
1889
1890 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1891 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1892 }
1893
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001894 // If we are actually going to launch in to a new task, there are some cases where
1895 // we further want to do multiple task.
1896 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1897 if (launchTaskBehind
1898 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1899 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1900 }
1901 }
1902
Craig Mautner8849a5e2013-04-02 16:41:03 -07001903 // We'll invoke onUserLeaving before onPause only if the launching
1904 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001905 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001906 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1907 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001908
1909 // If the caller has asked not to resume at this point, we make note
1910 // of this in the record so that we can skip it when trying to find
1911 // the top running activity.
1912 if (!doResume) {
1913 r.delayedResume = true;
1914 }
1915
Craig Mautnera254cd72014-05-25 16:47:39 -07001916 ActivityRecord notTop =
1917 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001918
1919 // If the onlyIfNeeded flag is set, then we can do this if the activity
1920 // being launched is the same as the one making the call... or, as
1921 // a special case, if we do not know the caller then we count the
1922 // current top activity as the caller.
1923 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1924 ActivityRecord checkedCaller = sourceRecord;
1925 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001926 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001927 }
1928 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1929 // Caller is not the same as launcher, so always needed.
1930 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1931 }
1932 }
1933
Craig Mautner8849a5e2013-04-02 16:41:03 -07001934 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001935 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001936
1937 // If the caller is not coming from another activity, but has given us an
1938 // explicit task into which they would like us to launch the new activity,
1939 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001940 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1941 final Intent baseIntent = inTask.getBaseIntent();
1942 final ActivityRecord root = inTask.getRootActivity();
1943 if (baseIntent == null) {
1944 ActivityOptions.abort(options);
1945 throw new IllegalArgumentException("Launching into task without base intent: "
1946 + inTask);
1947 }
1948
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001949 // If this task is empty, then we are adding the first activity -- it
1950 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001951 if (launchSingleInstance || launchSingleTask) {
1952 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1953 ActivityOptions.abort(options);
1954 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1955 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001956 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001957 if (root != null) {
1958 ActivityOptions.abort(options);
1959 throw new IllegalArgumentException("Caller with inTask " + inTask
1960 + " has root " + root + " but target is singleInstance/Task");
1961 }
1962 }
1963
1964 // If task is empty, then adopt the interesting intent launch flags in to the
1965 // activity being started.
1966 if (root == null) {
1967 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1968 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1969 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1970 launchFlags = (launchFlags&~flagsOfInterest)
1971 | (baseIntent.getFlags()&flagsOfInterest);
1972 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001973 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001974 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001975
Dianne Hackborn962d5352014-08-29 16:27:40 -07001976 // If the task is not empty and the caller is asking to start it as the root
1977 // of a new task, then we don't actually want to start this on the task. We
1978 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001979 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001980 addingToTask = false;
1981
1982 } else {
1983 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001984 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001985
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001986 reuseTask = inTask;
1987 } else {
1988 inTask = null;
1989 }
1990
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001991 if (inTask == null) {
1992 if (sourceRecord == null) {
1993 // This activity is not being started from another... in this
1994 // case we -always- start a new task.
1995 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1996 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1997 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1998 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1999 }
2000 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2001 // The original activity who is starting us is running as a single
2002 // instance... this new activity it is starting must go on its
2003 // own task.
2004 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2005 } else if (launchSingleInstance || launchSingleTask) {
2006 // The activity being started is a single instance... it always
2007 // gets launched into its own task.
2008 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2009 }
2010 }
2011
2012 ActivityInfo newTaskInfo = null;
2013 Intent newTaskIntent = null;
2014 ActivityStack sourceStack;
2015 if (sourceRecord != null) {
2016 if (sourceRecord.finishing) {
2017 // If the source is finishing, we can't further count it as our source. This
2018 // is because the task it is associated with may now be empty and on its way out,
2019 // so we don't want to blindly throw it in to that task. Instead we will take
2020 // the NEW_TASK flow and try to find a task for it. But save the task information
2021 // so it can be used when creating the new task.
2022 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2023 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2024 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2025 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2026 newTaskInfo = sourceRecord.info;
2027 newTaskIntent = sourceRecord.task.intent;
2028 }
2029 sourceRecord = null;
2030 sourceStack = null;
2031 } else {
2032 sourceStack = sourceRecord.task.stack;
2033 }
2034 } else {
2035 sourceStack = null;
2036 }
2037
2038 boolean movedHome = false;
2039 ActivityStack targetStack;
2040
2041 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002042 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002043
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002044 // We may want to try to place the new activity in to an existing task. We always
2045 // do this if the target activity is singleTask or singleInstance; we will also do
2046 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2047 // us to still place it in a new task: multi task, always doc mode, or being asked to
2048 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002049 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2050 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002051 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002052 // If bring to front is requested, and no result is requested and we have not
2053 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002054 // we can find a task that was started with this same
2055 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002056 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002057 // See if there is a task to bring to the front. If this is
2058 // a SINGLE_INSTANCE activity, there can be one and only one
2059 // instance of it in the history, and it is always in its own
2060 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002061 ActivityRecord intentActivity = !launchSingleInstance ?
2062 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002063 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002064 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2065 // but still needs to be a lock task mode violation since the task gets
2066 // cleared out and the device would otherwise leave the locked task.
2067 if (isLockTaskModeViolation(intentActivity.task,
2068 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2069 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002070 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002071 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002072 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2073 }
Craig Mautner29219d92013-04-16 20:19:12 -07002074 if (r.task == null) {
2075 r.task = intentActivity.task;
2076 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002077 if (intentActivity.task.intent == null) {
2078 // This task was started because of movement of
2079 // the activity based on affinity... now that we
2080 // are actually launching it, we can assign the
2081 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002082 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002083 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002084 targetStack = intentActivity.task.stack;
2085 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002086 // If the target task is not in the front, then we need
2087 // to bring it to the front... except... well, with
2088 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2089 // to have the same behavior as if a new instance was
2090 // being started, which means not bringing it to the front
2091 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002092 final ActivityStack focusStack = getFocusedStack();
2093 ActivityRecord curTop = (focusStack == null)
2094 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002095 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002096 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002097 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002098 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002099 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2100 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002101 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002102 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002103 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2104 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002105 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002106 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002107 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002108 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002109 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002110 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2111 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002112 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002113 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002114 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002115 options = null;
2116 }
2117 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002118 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002119 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002120 + " from " + intentActivity);
2121 targetStack.moveToFront("intentActivityFound");
2122 }
2123
Craig Mautner8849a5e2013-04-02 16:41:03 -07002124 // If the caller has requested that the target task be
2125 // reset, then do so.
2126 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2127 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2128 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002129 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002130 // We don't need to start a new activity, and
2131 // the client said not to do anything if that
2132 // is the case, so this is it! And for paranoia, make
2133 // sure we have correctly resumed the top activity.
2134 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002135 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002136
2137 // Make sure to notify Keyguard as well if we are not running an app
2138 // transition later.
2139 if (!movedToFront) {
2140 notifyActivityDrawnForKeyguard();
2141 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002142 } else {
2143 ActivityOptions.abort(options);
2144 }
2145 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2146 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002147 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002148 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002149 // The caller has requested to completely replace any
2150 // existing task with its new activity. Well that should
2151 // not be too hard...
2152 reuseTask = intentActivity.task;
2153 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002154 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002155 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002156 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002157 // In this situation we want to remove all activities
2158 // from the task up to the one being started. In most
2159 // cases this means we are resetting the task to its
2160 // initial state.
2161 ActivityRecord top =
2162 intentActivity.task.performClearTaskLocked(r, launchFlags);
2163 if (top != null) {
2164 if (top.frontOfTask) {
2165 // Activity aliases may mean we use different
2166 // intents for the top activity, so make sure
2167 // the task now has the identity of the new
2168 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002169 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002170 }
2171 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2172 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002173 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002174 } else {
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002175 // A special case: we need to start the activity because it is not
2176 // currently running, and the caller has asked to clear the current
2177 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002178 addingToTask = true;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002179 // Now pretend like this activity is being started by the top of its
2180 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002181 sourceRecord = intentActivity;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002182 TaskRecord task = sourceRecord.task;
2183 if (task != null && task.stack == null) {
2184 // Target stack got cleared when we all activities were removed
2185 // above. Go ahead and reset it.
2186 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2187 targetStack.addTask(
2188 task, !launchTaskBehind /* toTop */, false /* moving */);
2189 }
2190
Craig Mautner8849a5e2013-04-02 16:41:03 -07002191 }
2192 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2193 // In this case the top activity on the task is the
2194 // same as the one being launched, so we take that
2195 // as a request to bring the task to the foreground.
2196 // If the top activity in the task is the root
2197 // activity, deliver this new intent to it if it
2198 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002199 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002200 && intentActivity.realActivity.equals(r.realActivity)) {
2201 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2202 intentActivity.task);
2203 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002204 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002205 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002206 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2207 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002208 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2209 // In this case we are launching the root activity
2210 // of the task, but with a different intent. We
2211 // should start a new instance on top.
2212 addingToTask = true;
2213 sourceRecord = intentActivity;
2214 }
2215 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2216 // In this case an activity is being launched in to an
2217 // existing task, without resetting that task. This
2218 // is typically the situation of launching an activity
2219 // from a notification or shortcut. We want to place
2220 // the new activity on top of the current task.
2221 addingToTask = true;
2222 sourceRecord = intentActivity;
2223 } else if (!intentActivity.task.rootWasReset) {
2224 // In this case we are launching in to an existing task
2225 // that has not yet been started from its front door.
2226 // The current task has been brought to the front.
2227 // Ideally, we'd probably like to place this new task
2228 // at the bottom of its stack, but that's a little hard
2229 // to do with the current organization of the code so
2230 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002231 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002232 }
2233 if (!addingToTask && reuseTask == null) {
2234 // We didn't do anything... but it was needed (a.k.a., client
2235 // don't use that intent!) And for paranoia, make
2236 // sure we have correctly resumed the top activity.
2237 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002238 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002239 if (!movedToFront) {
2240 // Make sure to notify Keyguard as well if we are not running an app
2241 // transition later.
2242 notifyActivityDrawnForKeyguard();
2243 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002244 } else {
2245 ActivityOptions.abort(options);
2246 }
2247 return ActivityManager.START_TASK_TO_FRONT;
2248 }
2249 }
2250 }
2251 }
2252
2253 //String uri = r.intent.toURI();
2254 //Intent intent2 = new Intent(uri);
2255 //Slog.i(TAG, "Given intent: " + r.intent);
2256 //Slog.i(TAG, "URI is: " + uri);
2257 //Slog.i(TAG, "To intent: " + intent2);
2258
2259 if (r.packageName != null) {
2260 // If the activity being launched is the same as the one currently
2261 // at the top, then we need to check if it should only be launched
2262 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002263 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002264 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002265 if (top != null && r.resultTo == null) {
2266 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2267 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002268 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002269 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002270 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2271 top.task);
2272 // For paranoia, make sure we have correctly
2273 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002274 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002275 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002276 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002277 }
2278 ActivityOptions.abort(options);
2279 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2280 // We don't need to start a new activity, and
2281 // the client said not to do anything if that
2282 // is the case, so this is it!
2283 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2284 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002285 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002286 return ActivityManager.START_DELIVERED_TO_TOP;
2287 }
2288 }
2289 }
2290 }
2291
2292 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002293 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002294 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2295 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002296 }
2297 ActivityOptions.abort(options);
2298 return ActivityManager.START_CLASS_NOT_FOUND;
2299 }
2300
2301 boolean newTask = false;
2302 boolean keepCurTransition = false;
2303
Craig Mautnerbb742462014-07-07 15:28:55 -07002304 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002305 sourceRecord.task : null;
2306
Craig Mautner8849a5e2013-04-02 16:41:03 -07002307 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002308 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002309 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002310 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002311 targetStack = computeStackFocus(r, newTask);
2312 targetStack.moveToFront("startingNewTask");
2313
Craig Mautner8849a5e2013-04-02 16:41:03 -07002314 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002315 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2316 newTaskInfo != null ? newTaskInfo : r.info,
2317 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002318 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2319 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002320 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2321 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002322 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002323 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002324 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002325 if (isLockTaskModeViolation(r.task)) {
2326 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2327 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2328 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002329 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002330 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002331 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2332 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002333 // Caller wants to appear on home activity, so before starting
2334 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002335 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002336 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002337 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002338 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002339 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002340 if (isLockTaskModeViolation(sourceTask)) {
2341 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2342 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2343 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002344 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002345 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002346 final TaskRecord topTask = targetStack.topTask();
2347 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002348 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002349 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002350 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002351 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002352 // In this case, we are adding the activity to an existing
2353 // task, but the caller has asked to clear that task if the
2354 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002355 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002356 keepCurTransition = true;
2357 if (top != null) {
2358 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002359 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002360 // For paranoia, make sure we have correctly
2361 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002362 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002363 if (doResume) {
2364 targetStack.resumeTopActivityLocked(null);
2365 }
2366 ActivityOptions.abort(options);
2367 return ActivityManager.START_DELIVERED_TO_TOP;
2368 }
2369 } else if (!addingToTask &&
2370 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2371 // In this case, we are launching an activity in our own task
2372 // that may already be running somewhere in the history, and
2373 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002374 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002375 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002376 final TaskRecord task = top.task;
2377 task.moveActivityToFrontLocked(top);
2378 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002379 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002380 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002381 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002382 if (doResume) {
2383 targetStack.resumeTopActivityLocked(null);
2384 }
2385 return ActivityManager.START_DELIVERED_TO_TOP;
2386 }
2387 }
2388 // An existing activity is starting this new activity, so we want
2389 // to keep the new one in the same task as the one that is starting
2390 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002391 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002392 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002393 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002394
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002395 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002396 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002397 // task it has provided to us.
2398 if (isLockTaskModeViolation(inTask)) {
2399 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2400 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2401 }
2402 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002403 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2404 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002405
2406 // Check whether we should actually launch the new activity in to the task,
2407 // or just reuse the current activity on top.
2408 ActivityRecord top = inTask.getTopActivity();
2409 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2410 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2411 || launchSingleTop || launchSingleTask) {
2412 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2413 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2414 // We don't need to start a new activity, and
2415 // the client said not to do anything if that
2416 // is the case, so this is it!
2417 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2418 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002419 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002420 return ActivityManager.START_DELIVERED_TO_TOP;
2421 }
2422 }
2423
Dianne Hackborn962d5352014-08-29 16:27:40 -07002424 if (!addingToTask) {
2425 // We don't actually want to have this activity added to the task, so just
2426 // stop here but still tell the caller that we consumed the intent.
2427 ActivityOptions.abort(options);
2428 return ActivityManager.START_TASK_TO_FRONT;
2429 }
2430
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002431 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002432 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002433 + " in explicit task " + r.task);
2434
Craig Mautner8849a5e2013-04-02 16:41:03 -07002435 } else {
2436 // This not being started from an existing activity, and not part
2437 // of a new task... just put it in the top task, though these days
2438 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002439 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002440 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002441 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002442 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002443 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002444 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002445 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002446 + " in new guessed " + r.task);
2447 }
2448
2449 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002450 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002451
Craig Mautner76e2a762014-06-24 09:05:43 -07002452 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2453 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2454 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002455 if (newTask) {
2456 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2457 }
2458 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002459 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002460 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002461 if (!launchTaskBehind) {
2462 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002463 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002464 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002465 return ActivityManager.START_SUCCESS;
2466 }
2467
Craig Mautneree36c772014-07-16 14:56:05 -07002468 final void doPendingActivityLaunchesLocked(boolean doResume) {
2469 while (!mPendingActivityLaunches.isEmpty()) {
2470 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002471 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002472 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002473 }
2474 }
2475
Craig Mautner7f13ed32014-07-28 14:00:35 -07002476 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002477 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2478 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002479 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002480 mPendingActivityLaunches.remove(palNdx);
2481 }
2482 }
2483 }
2484
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002485 void setLaunchSource(int uid) {
2486 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2487 }
2488
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002489 void acquireLaunchWakelock() {
2490 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2491 throw new IllegalStateException("Calling must be system uid");
2492 }
2493 mLaunchingActivity.acquire();
2494 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2495 // To be safe, don't allow the wake lock to be held for too long.
2496 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2497 }
2498 }
2499
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002500 /**
2501 * Called when the frontmost task is idle.
2502 * @return the state of mService.mBooting before this was called.
2503 */
2504 private boolean checkFinishBootingLocked() {
2505 final boolean booting = mService.mBooting;
2506 boolean enableScreen = false;
2507 mService.mBooting = false;
2508 if (!mService.mBooted) {
2509 mService.mBooted = true;
2510 enableScreen = true;
2511 }
2512 if (booting || enableScreen) {
2513 mService.postFinishBooting(booting, enableScreen);
2514 }
2515 return booting;
2516 }
2517
Craig Mautnerf3333272013-04-22 10:55:53 -07002518 // Checked.
2519 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2520 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002521 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002522
Craig Mautnerf3333272013-04-22 10:55:53 -07002523 ArrayList<ActivityRecord> stops = null;
2524 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002525 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002526 int NS = 0;
2527 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002528 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002529 boolean activityRemoved = false;
2530
Wale Ogunwale7d701172015-03-11 15:36:30 -07002531 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002532 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002533 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2534 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002535 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2536 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002537 if (fromTimeout) {
2538 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002539 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002540
2541 // This is a hack to semi-deal with a race condition
2542 // in the client where it can be constructed with a
2543 // newer configuration from when we asked it to launch.
2544 // We'll update with whatever configuration it now says
2545 // it used to launch.
2546 if (config != null) {
2547 r.configuration = config;
2548 }
2549
2550 // We are now idle. If someone is waiting for a thumbnail from
2551 // us, we can now deliver.
2552 r.idle = true;
2553
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002554 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002555 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002556 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002557 }
2558 }
2559
2560 if (allResumedActivitiesIdle()) {
2561 if (r != null) {
2562 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002563 }
2564
2565 if (mLaunchingActivity.isHeld()) {
2566 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2567 if (VALIDATE_WAKE_LOCK_CALLER &&
2568 Binder.getCallingUid() != Process.myUid()) {
2569 throw new IllegalStateException("Calling must be system uid");
2570 }
2571 mLaunchingActivity.release();
2572 }
2573 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002574 }
2575
2576 // Atomically retrieve all of the other things to do.
2577 stops = processStoppingActivitiesLocked(true);
2578 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002579 if ((NF = mFinishingActivities.size()) > 0) {
2580 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002581 mFinishingActivities.clear();
2582 }
2583
Craig Mautnerf3333272013-04-22 10:55:53 -07002584 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002585 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002586 mStartingUsers.clear();
2587 }
2588
Craig Mautnerf3333272013-04-22 10:55:53 -07002589 // Stop any activities that are scheduled to do so but have been
2590 // waiting for the next one to start.
2591 for (int i = 0; i < NS; i++) {
2592 r = stops.get(i);
2593 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002594 if (stack != null) {
2595 if (r.finishing) {
2596 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2597 } else {
2598 stack.stopActivityLocked(r);
2599 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002600 }
2601 }
2602
2603 // Finish any activities that are scheduled to do so but have been
2604 // waiting for the next one to start.
2605 for (int i = 0; i < NF; i++) {
2606 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002607 final ActivityStack stack = r.task.stack;
2608 if (stack != null) {
2609 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2610 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002611 }
2612
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002613 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002614 // Complete user switch
2615 if (startingUsers != null) {
2616 for (int i = 0; i < startingUsers.size(); i++) {
2617 mService.finishUserSwitch(startingUsers.get(i));
2618 }
2619 }
2620 // Complete starting up of background users
2621 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002622 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002623 mStartingBackgroundUsers.clear();
2624 for (int i = 0; i < startingUsers.size(); i++) {
2625 mService.finishUserBoot(startingUsers.get(i));
2626 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002627 }
2628 }
2629
2630 mService.trimApplications();
2631 //dump();
2632 //mWindowManager.dump();
2633
Craig Mautnerf3333272013-04-22 10:55:53 -07002634 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002635 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002636 }
2637
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002638 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002639 }
2640
Craig Mautner8e569572013-10-11 17:36:59 -07002641 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002642 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002643 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2644 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002645 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2646 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2647 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002648 }
Craig Mautner19091252013-10-05 00:03:53 -07002649 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002650 }
2651
2652 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002653 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2654 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002655 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2656 stacks.get(stackNdx).closeSystemDialogsLocked();
2657 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002658 }
2659 }
2660
Craig Mautner93529a42013-10-04 15:03:13 -07002661 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002662 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002663 }
2664
Craig Mautner8d341ef2013-03-26 09:03:27 -07002665 /**
2666 * @return true if some activity was finished (or would have finished if doit were true).
2667 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002668 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2669 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002670 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002671 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2672 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08002673 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002674 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002675 if (stack.finishDisabledPackageActivitiesLocked(
2676 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002677 didSomething = true;
2678 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002679 }
2680 }
2681 return didSomething;
2682 }
2683
Dianne Hackborna413dc02013-07-12 12:02:55 -07002684 void updatePreviousProcessLocked(ActivityRecord r) {
2685 // Now that this process has stopped, we may want to consider
2686 // it to be the previous app to try to keep around in case
2687 // the user wants to return to it.
2688
2689 // First, found out what is currently the foreground app, so that
2690 // we don't blow away the previous app if this activity is being
2691 // hosted by the process that is actually still the foreground.
2692 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002693 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2694 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002695 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2696 final ActivityStack stack = stacks.get(stackNdx);
2697 if (isFrontStack(stack)) {
2698 if (stack.mResumedActivity != null) {
2699 fgApp = stack.mResumedActivity.app;
2700 } else if (stack.mPausingActivity != null) {
2701 fgApp = stack.mPausingActivity.app;
2702 }
2703 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002704 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002705 }
2706 }
2707
2708 // Now set this one as the previous process, only if that really
2709 // makes sense to.
2710 if (r.app != null && fgApp != null && r.app != fgApp
2711 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002712 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002713 mService.mPreviousProcess = r.app;
2714 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2715 }
2716 }
2717
Craig Mautner05d29032013-05-03 13:40:13 -07002718 boolean resumeTopActivitiesLocked() {
2719 return resumeTopActivitiesLocked(null, null, null);
2720 }
2721
2722 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2723 Bundle targetOptions) {
2724 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002725 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002726 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002727 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002728 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002729 if (isFrontStack(targetStack)) {
2730 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2731 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002732
Craig Mautnere0a38842013-12-16 16:14:02 -08002733 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2734 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002735 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2736 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002737 if (stack == targetStack) {
2738 // Already started above.
2739 continue;
2740 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002741 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002742 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002743 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002744 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002745 }
Craig Mautner05d29032013-05-03 13:40:13 -07002746 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002747 }
2748
Todd Kennedy539db512014-12-15 09:57:55 -08002749 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002750 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2751 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002752 final int numStacks = stacks.size();
2753 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2754 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002755 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002756 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002757 }
2758 }
2759
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002760 void finishVoiceTask(IVoiceInteractionSession session) {
2761 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2762 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2763 final int numStacks = stacks.size();
2764 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2765 final ActivityStack stack = stacks.get(stackNdx);
2766 stack.finishVoiceTask(session);
2767 }
2768 }
2769 }
2770
Craig Mautner299f9602015-01-26 09:47:33 -08002771 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002772 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2773 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002774 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002775 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2776 // Caller wants the home activity moved with it. To accomplish this,
2777 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002778 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002779 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002780 if (task.stack == null) {
2781 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2782 + task + " to front. Stack is null");
2783 return;
2784 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002785 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2786 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2787 reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002788 if (DEBUG_STACK) Slog.d(TAG_STACK,
2789 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002790 }
2791
Craig Mautner967212c2013-04-13 21:10:58 -07002792 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002793 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2794 if (activityContainer != null) {
2795 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002796 }
2797 return null;
2798 }
2799
Craig Mautner967212c2013-04-13 21:10:58 -07002800 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002801 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002802 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2803 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002804 }
2805 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002806 }
2807
Craig Mautner4a1cb222013-12-04 16:14:06 -08002808 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002809 ActivityRecord homeActivity = getHomeActivity();
2810 if (homeActivity != null) {
2811 return homeActivity.appToken;
2812 }
2813 return null;
2814 }
2815
2816 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002817 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002818 }
2819
2820 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002821 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2822 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2823 final TaskRecord task = tasks.get(taskNdx);
2824 if (task.isHomeTask()) {
2825 final ArrayList<ActivityRecord> activities = task.mActivities;
2826 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2827 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002828 if (r.isHomeActivity()
2829 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002830 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002831 }
2832 }
2833 }
2834 }
2835 return null;
2836 }
2837
Todd Kennedyca4d8422015-01-15 15:19:22 -08002838 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002839 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002840 ActivityContainer activityContainer =
2841 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002842 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002843 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2844 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002845 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002846 return activityContainer;
2847 }
2848
Craig Mautner34b73df2014-01-12 21:11:08 -08002849 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002850 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2851 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2852 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002853 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2854 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002855 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002856 }
2857 }
2858
Craig Mautner95da1082014-02-24 17:54:35 -08002859 void deleteActivityContainer(IActivityContainer container) {
2860 ActivityContainer activityContainer = (ActivityContainer)container;
2861 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002862 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2863 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002864 final int stackId = activityContainer.mStackId;
2865 mActivityContainers.remove(stackId);
2866 mWindowManager.removeStack(stackId);
2867 }
2868 }
2869
Wale Ogunwale60454db2015-01-23 16:05:07 -08002870 void resizeStackLocked(int stackId, Rect bounds) {
2871 final ActivityStack stack = getStack(stackId);
2872 if (stack == null) {
2873 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2874 return;
2875 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002876
2877 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002878 if (r != null && !r.task.mResizeable) {
2879 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002880 return;
2881 }
2882
Wale Ogunwale60454db2015-01-23 16:05:07 -08002883 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2884 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002885 if (r != null) {
2886 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2887 // And we need to make sure at this point that all other activities
2888 // are made visible with the correct configuration.
2889 ensureActivitiesVisibleLocked(r, 0);
2890 if (!updated) {
2891 resumeTopActivitiesLocked(stack, null, null);
2892 }
2893 }
2894 }
2895 }
2896
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002897 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2898 * current task stack if it only has one entry, moving the task to a stack that matches the
2899 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2900 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2901 task.mResizeable = true;
2902 final ActivityStack currentStack = task.stack;
2903 if (currentStack.isHomeStack()) {
2904 // Can't move task off the home stack. Sorry!
2905 return;
2906 }
2907
2908 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2909 if (matchingStackId != -1) {
2910 // There is already a stack with the right bounds!
2911 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2912 // Nothing to do here. Already in the right stack...
2913 return;
2914 }
2915 // Move task to stack with matching bounds.
2916 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2917 return;
2918 }
2919
2920 if (currentStack != null && currentStack.numTasks() == 1) {
2921 // Just resize the current stack since this is the task in it.
2922 resizeStackLocked(currentStack.mStackId, bounds);
2923 return;
2924 }
2925
2926 // Create new stack and move the task to it.
2927 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2928 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2929 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2930
2931 if (newStack == null) {
2932 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2933 return;
2934 }
2935 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2936 resizeStackLocked(newStack.mStackId, bounds);
2937 }
2938
Todd Kennedy4900bf92015-01-16 16:05:14 -08002939 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002940 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2941 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002942 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002943 }
2944
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002945 ActivityContainer activityContainer = new ActivityContainer(stackId);
2946 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002947 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002948 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002949 }
2950
2951 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002952 while (true) {
2953 if (++mLastStackId <= HOME_STACK_ID) {
2954 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002955 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002956 if (getStack(mLastStackId) == null) {
2957 break;
2958 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002959 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002960 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002961 }
2962
Wale Ogunwale7de05352014-12-12 15:21:33 -08002963 private boolean restoreRecentTaskLocked(TaskRecord task) {
2964 ActivityStack stack = null;
2965 // Determine stack to restore task to.
2966 if (mLeanbackOnlyDevice) {
2967 // There is only one stack for lean back devices.
2968 stack = mHomeStack;
2969 } else {
2970 // Look for the top stack on the home display that isn't the home stack.
2971 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2972 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2973 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07002974 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002975 stack = tmpStack;
2976 break;
2977 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002978 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002979 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002980
2981 if (stack == null) {
2982 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2983 // before an application stack is created...Go ahead and create one on the default
2984 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002985 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002986 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002987 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002988 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2989 "Created stack=" + stack + " for recents restoration.");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002990 }
2991
2992 if (stack == null) {
2993 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002994 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2995 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002996 return false;
2997 }
2998
2999 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003000 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3001 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003002 final ArrayList<ActivityRecord> activities = task.mActivities;
3003 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3004 final ActivityRecord r = activities.get(activityNdx);
3005 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
3006 r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07003007 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0,
Wale Ogunwale7de05352014-12-12 15:21:33 -08003008 r.userId, r.info.configChanges, task.voiceSession != null,
3009 r.mLaunchTaskBehind);
3010 }
3011 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003012 }
3013
Craig Mautner299f9602015-01-26 09:47:33 -08003014 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003015 final TaskRecord task = anyTaskForIdLocked(taskId);
3016 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003017 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003018 return;
3019 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003020 final ActivityStack stack = getStack(stackId);
3021 if (stack == null) {
3022 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
3023 return;
3024 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003025 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
3026 if (task.stack != null) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07003027 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003028 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003029 stack.addTask(task, toTop, true);
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003030 // The task might have already been running and its visibility needs to be synchronized with
3031 // the visibility of the stack / windows.
3032 stack.ensureActivitiesVisibleLocked(null, 0);
Craig Mautner05d29032013-05-03 13:40:13 -07003033 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003034 }
3035
Craig Mautnerac6f8432013-07-17 13:24:59 -07003036 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003037 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003038 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3039 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003040 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3041 final ActivityStack stack = stacks.get(stackNdx);
3042 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003043 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003044 continue;
3045 }
3046 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003047 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3048 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003049 continue;
3050 }
3051 final ActivityRecord ar = stack.findTaskLocked(r);
3052 if (ar != null) {
3053 return ar;
3054 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003055 }
3056 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003057 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003058 return null;
3059 }
3060
3061 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003062 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3063 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003064 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3065 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3066 if (ar != null) {
3067 return ar;
3068 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003069 }
3070 }
3071 return null;
3072 }
3073
Craig Mautner8d341ef2013-03-26 09:03:27 -07003074 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003075 scheduleSleepTimeout();
3076 if (!mGoingToSleep.isHeld()) {
3077 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003078 if (mLaunchingActivity.isHeld()) {
3079 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3080 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003081 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003082 mLaunchingActivity.release();
3083 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003084 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003085 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003086 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003087 }
3088
3089 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003090 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003091
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003092 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003093 final long endTime = System.currentTimeMillis() + timeout;
3094 while (true) {
3095 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003096 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3097 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003098 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3099 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3100 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003101 }
3102 if (cantShutdown) {
3103 long timeRemaining = endTime - System.currentTimeMillis();
3104 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003105 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003106 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003107 } catch (InterruptedException e) {
3108 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003109 } else {
3110 Slog.w(TAG, "Activity manager shutdown timed out");
3111 timedout = true;
3112 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003113 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003114 } else {
3115 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003116 }
3117 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003118
3119 // Force checkReadyForSleep to complete.
3120 mSleepTimeout = true;
3121 checkReadyForSleepLocked();
3122
Craig Mautner8d341ef2013-03-26 09:03:27 -07003123 return timedout;
3124 }
3125
3126 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003127 removeSleepTimeouts();
3128 if (mGoingToSleep.isHeld()) {
3129 mGoingToSleep.release();
3130 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003131 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3132 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003133 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3134 final ActivityStack stack = stacks.get(stackNdx);
3135 stack.awakeFromSleepingLocked();
3136 if (isFrontStack(stack)) {
3137 resumeTopActivitiesLocked();
3138 }
Craig Mautner5314a402013-09-26 12:40:16 -07003139 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003140 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003141 mGoingToSleepActivities.clear();
3142 }
3143
3144 void activitySleptLocked(ActivityRecord r) {
3145 mGoingToSleepActivities.remove(r);
3146 checkReadyForSleepLocked();
3147 }
3148
3149 void checkReadyForSleepLocked() {
3150 if (!mService.isSleepingOrShuttingDown()) {
3151 // Do not care.
3152 return;
3153 }
3154
3155 if (!mSleepTimeout) {
3156 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003157 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3158 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003159 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3160 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3161 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003162 }
3163
3164 if (mStoppingActivities.size() > 0) {
3165 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003166 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003167 + mStoppingActivities.size() + " activities");
3168 scheduleIdleLocked();
3169 dontSleep = true;
3170 }
3171
3172 if (mGoingToSleepActivities.size() > 0) {
3173 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003174 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003175 + mGoingToSleepActivities.size() + " activities");
3176 dontSleep = true;
3177 }
3178
3179 if (dontSleep) {
3180 return;
3181 }
3182 }
3183
Craig Mautnere0a38842013-12-16 16:14:02 -08003184 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3185 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003186 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3187 stacks.get(stackNdx).goToSleep();
3188 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003189 }
3190
3191 removeSleepTimeouts();
3192
3193 if (mGoingToSleep.isHeld()) {
3194 mGoingToSleep.release();
3195 }
3196 if (mService.mShuttingDown) {
3197 mService.notifyAll();
3198 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003199 }
3200
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003201 boolean reportResumedActivityLocked(ActivityRecord r) {
3202 final ActivityStack stack = r.task.stack;
3203 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003204 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003205 }
3206 if (allResumedActivitiesComplete()) {
3207 ensureActivitiesVisibleLocked(null, 0);
3208 mWindowManager.executeAppTransition();
3209 return true;
3210 }
3211 return false;
3212 }
3213
Craig Mautner8d341ef2013-03-26 09:03:27 -07003214 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003215 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3216 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003217 int stackNdx = stacks.size() - 1;
3218 while (stackNdx >= 0) {
3219 stacks.get(stackNdx).handleAppCrashLocked(app);
3220 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003221 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003222 }
3223 }
3224
Jose Lima4b6c6692014-08-12 17:41:12 -07003225 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003226 final ActivityStack stack = r.task.stack;
3227 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003228 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3229 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003230 return false;
3231 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003232 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003233 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3234 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003235
3236 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003237 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003238 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003239 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003240 return true;
3241 }
3242
3243 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003244 if (visible && top.fullscreen) {
3245 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003246 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3247 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3248 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3249 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003250 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003251 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3252 // Only the activity set as currently visible behind should actively reset its
3253 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003254 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3255 "requestVisibleBehind: returning visible=" + visible
3256 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3257 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003258 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003259 }
3260
Jose Lima4b6c6692014-08-12 17:41:12 -07003261 stack.setVisibleBehindActivity(visible ? r : null);
3262 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003263 // Make the activity immediately above r opaque.
3264 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3265 if (next != null) {
3266 mService.convertFromTranslucent(next.appToken);
3267 }
3268 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003269 if (top.app != null && top.app.thread != null) {
3270 // Notify the top app of the change.
3271 try {
3272 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3273 } catch (RemoteException e) {
3274 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003275 }
3276 return true;
3277 }
3278
Craig Mautnerbb742462014-07-07 15:28:55 -07003279 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3280 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3281 r.mLaunchTaskBehind = false;
3282 final TaskRecord task = r.task;
3283 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003284 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003285 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003286 mWindowManager.setAppVisibility(r.appToken, false);
3287 }
3288
3289 void scheduleLaunchTaskBehindComplete(IBinder token) {
3290 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3291 }
3292
Craig Mautnerde4ef022013-04-07 19:01:33 -07003293 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003294 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003295 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3296 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003297 final int topStackNdx = stacks.size() - 1;
3298 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3299 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003300 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003301 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003302 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003303 }
3304
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003305 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3306 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3307 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3308 final int topStackNdx = stacks.size() - 1;
3309 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3310 final ActivityStack stack = stacks.get(stackNdx);
3311 stack.clearOtherAppTimeTrackers(except);
3312 }
3313 }
3314 }
3315
Craig Mautner8d341ef2013-03-26 09:03:27 -07003316 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003317 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3318 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003319 final int numStacks = stacks.size();
3320 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3321 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003322 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003323 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003324 }
3325 }
3326
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003327 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3328 // Examine all activities currently running in the process.
3329 TaskRecord firstTask = null;
3330 // Tasks is non-null only if two or more tasks are found.
3331 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003332 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3333 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003334 ActivityRecord r = app.activities.get(i);
3335 // First, if we find an activity that is in the process of being destroyed,
3336 // then we just aren't going to do anything for now; we want things to settle
3337 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003338 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003339 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003340 return;
3341 }
3342 // Don't consider any activies that are currently not in a state where they
3343 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003344 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3345 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003346 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003347 continue;
3348 }
3349 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003350 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003351 + " from " + r);
3352 if (firstTask == null) {
3353 firstTask = r.task;
3354 } else if (firstTask != r.task) {
3355 if (tasks == null) {
3356 tasks = new ArraySet<>();
3357 tasks.add(firstTask);
3358 }
3359 tasks.add(r.task);
3360 }
3361 }
3362 }
3363 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003364 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003365 return;
3366 }
3367 // If we have activities in multiple tasks that are in a position to be destroyed,
3368 // let's iterate through the tasks and release the oldest one.
3369 final int numDisplays = mActivityDisplays.size();
3370 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3371 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3372 // Step through all stacks starting from behind, to hit the oldest things first.
3373 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3374 final ActivityStack stack = stacks.get(stackNdx);
3375 // Try to release activities in this stack; if we manage to, we are done.
3376 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3377 return;
3378 }
3379 }
3380 }
3381 }
3382
Amith Yamasani37a40c22015-06-17 13:25:42 -07003383 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003384 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003385 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003386 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003387
Craig Mautner858d8a62013-04-23 17:08:34 -07003388 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003389 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3390 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003391 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003392 final ActivityStack stack = stacks.get(stackNdx);
3393 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003394 TaskRecord task = stack.topTask();
3395 if (task != null) {
3396 mWindowManager.moveTaskToTop(task.taskId);
3397 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003398 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003399 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003400
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003401 ActivityStack stack = getStack(restoreStackId);
3402 if (stack == null) {
3403 stack = mHomeStack;
3404 }
3405 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003406 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003407 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003408 TaskRecord task = stack.topTask();
3409 if (task != null) {
3410 mWindowManager.moveTaskToTop(task.taskId);
3411 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003412 } else {
3413 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003414 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003415 }
Craig Mautner93529a42013-10-04 15:03:13 -07003416 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003417 }
3418
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003419 /**
3420 * Add background users to send boot completed events to.
3421 * @param userId The user being started in the background
3422 * @param uss The state object for the user.
3423 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003424 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003425 mStartingBackgroundUsers.add(uss);
3426 }
3427
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003428 /** Checks whether the userid is a profile of the current user. */
3429 boolean isCurrentProfileLocked(int userId) {
3430 if (userId == mCurrentUser) return true;
3431 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3432 if (mService.mCurrentProfileIds[i] == userId) return true;
3433 }
3434 return false;
3435 }
3436
Craig Mautnerde4ef022013-04-07 19:01:33 -07003437 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003438 ArrayList<ActivityRecord> stops = null;
3439
3440 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003441 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3442 ActivityRecord s = mStoppingActivities.get(activityNdx);
3443 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003444 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003445 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3446 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003447 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003448 if (s.finishing) {
3449 // If this activity is finishing, it is sitting on top of
3450 // everyone else but we now know it is no longer needed...
3451 // so get rid of it. Otherwise, we need to go through the
3452 // normal flow and hide it once we determine that it is
3453 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003454 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003455 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003456 }
3457 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003458 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003459 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003460 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003461 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003462 }
3463 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003464 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003465 }
3466 }
3467
3468 return stops;
3469 }
3470
Craig Mautnercf910b02013-04-23 11:23:27 -07003471 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003472 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3473 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3474 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3475 final ActivityStack stack = stacks.get(stackNdx);
3476 final ActivityRecord r = stack.topRunningActivityLocked(null);
3477 final ActivityState state = r == null ? DESTROYED : r.state;
3478 if (isFrontStack(stack)) {
3479 if (r == null) Slog.e(TAG,
3480 "validateTop...: null top activity, stack=" + stack);
3481 else {
3482 final ActivityRecord pausing = stack.mPausingActivity;
3483 if (pausing != null && pausing == r) Slog.e(TAG,
3484 "validateTop...: top stack has pausing activity r=" + r
3485 + " state=" + state);
3486 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3487 "validateTop...: activity in front not resumed r=" + r
3488 + " state=" + state);
3489 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003490 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003491 final ActivityRecord resumed = stack.mResumedActivity;
3492 if (resumed != null && resumed == r) Slog.e(TAG,
3493 "validateTop...: back stack has resumed activity r=" + r
3494 + " state=" + state);
3495 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3496 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003497 }
3498 }
3499 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003500 }
3501
Craig Mautnere0570202015-05-13 13:06:11 -07003502 private String lockTaskModeToString() {
3503 switch (mLockTaskModeState) {
3504 case LOCK_TASK_MODE_LOCKED:
3505 return "LOCKED";
3506 case LOCK_TASK_MODE_PINNED:
3507 return "PINNED";
3508 case LOCK_TASK_MODE_NONE:
3509 return "NONE";
3510 default: return "unknown=" + mLockTaskModeState;
3511 }
3512 }
3513
Craig Mautner27084302013-03-25 08:05:25 -07003514 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003515 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003516 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003517 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3518 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3519 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003520 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003521 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3522 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3523 if (packages.size() > 0) {
3524 pw.println(" mLockTaskPackages (userId:packages)=");
3525 for (int i = 0; i < packages.size(); ++i) {
3526 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3527 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3528 }
3529 }
3530 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003531 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003532
Craig Mautner20e72272013-04-01 13:45:53 -07003533 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003534 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003535 }
3536
Dianne Hackborn390517b2013-05-30 15:03:32 -07003537 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3538 boolean needSep, String prefix) {
3539 if (activity != null) {
3540 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3541 if (needSep) {
3542 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003543 }
3544 pw.print(prefix);
3545 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003546 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003547 }
3548 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003549 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003550 }
3551
Craig Mautner8d341ef2013-03-26 09:03:27 -07003552 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3553 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003554 boolean printed = false;
3555 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003556 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3557 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003558 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003559 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003560 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003561 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003562 final ActivityStack stack = stacks.get(stackNdx);
3563 StringBuilder stackHeader = new StringBuilder(128);
3564 stackHeader.append(" Stack #");
3565 stackHeader.append(stack.mStackId);
3566 stackHeader.append(":");
3567 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3568 needSep, stackHeader.toString());
3569 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3570 !dumpAll, false, dumpPackage, true,
3571 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003572
Craig Mautner4a1cb222013-12-04 16:14:06 -08003573 needSep = printed;
3574 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3575 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003576 if (pr) {
3577 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003578 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003579 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003580 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3581 " mResumedActivity: ");
3582 if (pr) {
3583 printed = true;
3584 needSep = false;
3585 }
3586 if (dumpAll) {
3587 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3588 " mLastPausedActivity: ");
3589 if (pr) {
3590 printed = true;
3591 needSep = true;
3592 }
3593 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3594 needSep, " mLastNoHistoryActivity: ");
3595 }
3596 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003597 }
3598 }
3599
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003600 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3601 false, dumpPackage, true, " Activities waiting to finish:", null);
3602 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3603 false, dumpPackage, true, " Activities waiting to stop:", null);
3604 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3605 false, dumpPackage, true, " Activities waiting for another to become visible:",
3606 null);
3607 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3608 false, dumpPackage, true, " Activities waiting to sleep:", null);
3609 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3610 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003611
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003612 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003613 }
3614
Dianne Hackborn390517b2013-05-30 15:03:32 -07003615 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003616 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003617 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003618 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003619 String innerPrefix = null;
3620 String[] args = null;
3621 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003622 for (int i=list.size()-1; i>=0; i--) {
3623 final ActivityRecord r = list.get(i);
3624 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3625 continue;
3626 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003627 if (innerPrefix == null) {
3628 innerPrefix = prefix + " ";
3629 args = new String[0];
3630 }
3631 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003632 final boolean full = !brief && (complete || !r.isInHistory());
3633 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003634 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003635 needNL = false;
3636 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003637 if (header1 != null) {
3638 pw.println(header1);
3639 header1 = null;
3640 }
3641 if (header2 != null) {
3642 pw.println(header2);
3643 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003644 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003645 if (lastTask != r.task) {
3646 lastTask = r.task;
3647 pw.print(prefix);
3648 pw.print(full ? "* " : " ");
3649 pw.println(lastTask);
3650 if (full) {
3651 lastTask.dump(pw, prefix + " ");
3652 } else if (complete) {
3653 // Complete + brief == give a summary. Isn't that obvious?!?
3654 if (lastTask.intent != null) {
3655 pw.print(prefix); pw.print(" ");
3656 pw.println(lastTask.intent.toInsecureStringWithClip());
3657 }
3658 }
3659 }
3660 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3661 pw.print(" #"); pw.print(i); pw.print(": ");
3662 pw.println(r);
3663 if (full) {
3664 r.dump(pw, innerPrefix);
3665 } else if (complete) {
3666 // Complete + brief == give a summary. Isn't that obvious?!?
3667 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3668 if (r.app != null) {
3669 pw.print(innerPrefix); pw.println(r.app);
3670 }
3671 }
3672 if (client && r.app != null && r.app.thread != null) {
3673 // flush anything that is already in the PrintWriter since the thread is going
3674 // to write to the file descriptor directly
3675 pw.flush();
3676 try {
3677 TransferPipe tp = new TransferPipe();
3678 try {
3679 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3680 r.appToken, innerPrefix, args);
3681 // Short timeout, since blocking here can
3682 // deadlock with the application.
3683 tp.go(fd, 2000);
3684 } finally {
3685 tp.kill();
3686 }
3687 } catch (IOException e) {
3688 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3689 } catch (RemoteException e) {
3690 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3691 }
3692 needNL = true;
3693 }
3694 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003695 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003696 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003697
Craig Mautnerf3333272013-04-22 10:55:53 -07003698 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003699 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3700 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003701 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3702 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003703 }
3704
3705 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003706 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003707 }
3708
3709 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003710 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3711 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003712 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3713 }
3714
Craig Mautner05d29032013-05-03 13:40:13 -07003715 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003716 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3717 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3718 }
Craig Mautner05d29032013-05-03 13:40:13 -07003719 }
3720
Craig Mautner0eea92c2013-05-16 13:35:39 -07003721 void removeSleepTimeouts() {
3722 mSleepTimeout = false;
3723 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3724 }
3725
3726 final void scheduleSleepTimeout() {
3727 removeSleepTimeouts();
3728 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3729 }
3730
Craig Mautner4a1cb222013-12-04 16:14:06 -08003731 @Override
3732 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003733 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003734 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3735 }
3736
3737 @Override
3738 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003739 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003740 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3741 }
3742
3743 @Override
3744 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003745 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003746 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3747 }
3748
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003749 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003750 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003751 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003752 newDisplay = mActivityDisplays.get(displayId) == null;
3753 if (newDisplay) {
3754 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003755 if (activityDisplay.mDisplay == null) {
3756 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3757 return;
3758 }
Craig Mautner4504de52013-12-20 09:06:56 -08003759 mActivityDisplays.put(displayId, activityDisplay);
3760 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003761 }
Craig Mautner4504de52013-12-20 09:06:56 -08003762 if (newDisplay) {
3763 mWindowManager.onDisplayAdded(displayId);
3764 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003765 }
3766
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003767 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003768 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003769 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3770 if (activityDisplay != null) {
3771 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003772 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003773 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003774 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003775 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003776 }
3777 }
3778 mWindowManager.onDisplayRemoved(displayId);
3779 }
3780
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003781 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003782 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003783 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3784 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003785 // TODO: Update the bounds.
3786 }
3787 }
3788 mWindowManager.onDisplayChanged(displayId);
3789 }
3790
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003791 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003792 StackInfo info = new StackInfo();
3793 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3794 info.displayId = Display.DEFAULT_DISPLAY;
3795 info.stackId = stack.mStackId;
3796
3797 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3798 final int numTasks = tasks.size();
3799 int[] taskIds = new int[numTasks];
3800 String[] taskNames = new String[numTasks];
3801 for (int i = 0; i < numTasks; ++i) {
3802 final TaskRecord task = tasks.get(i);
3803 taskIds[i] = task.taskId;
3804 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3805 : task.realActivity != null ? task.realActivity.flattenToString()
3806 : task.getTopActivity() != null ? task.getTopActivity().packageName
3807 : "unknown";
3808 }
3809 info.taskIds = taskIds;
3810 info.taskNames = taskNames;
3811 return info;
3812 }
3813
3814 StackInfo getStackInfoLocked(int stackId) {
3815 ActivityStack stack = getStack(stackId);
3816 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003817 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003818 }
3819 return null;
3820 }
3821
3822 ArrayList<StackInfo> getAllStackInfosLocked() {
3823 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003824 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3825 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003826 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003827 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003828 }
3829 }
3830 return list;
3831 }
3832
Craig Mautner15df08a2015-04-01 12:17:18 -07003833 TaskRecord getLockedTaskLocked() {
3834 final int top = mLockTaskModeTasks.size() - 1;
3835 if (top >= 0) {
3836 return mLockTaskModeTasks.get(top);
3837 }
3838 return null;
3839 }
3840
3841 boolean isLockedTask(TaskRecord task) {
3842 return mLockTaskModeTasks.contains(task);
3843 }
3844
3845 boolean isLastLockedTask(TaskRecord task) {
3846 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3847 }
3848
3849 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003850 if (!mLockTaskModeTasks.remove(task)) {
3851 return;
3852 }
3853 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3854 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003855 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003856 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3857 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003858 final Message lockTaskMsg = Message.obtain();
3859 lockTaskMsg.arg1 = task.userId;
3860 lockTaskMsg.what = LOCK_TASK_END_MSG;
3861 mHandler.sendMessage(lockTaskMsg);
3862 }
3863 }
3864
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003865 void showLockTaskToast() {
3866 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003867 }
3868
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003869 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3870 if (mLockTaskModeTasks.contains(task)) {
3871 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3872 }
3873 }
3874
Craig Mautner432f64e2015-05-20 14:59:57 -07003875 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3876 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003877 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003878 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003879 final TaskRecord lockedTask = getLockedTaskLocked();
3880 if (lockedTask != null) {
3881 removeLockedTaskLocked(lockedTask);
3882 if (!mLockTaskModeTasks.isEmpty()) {
3883 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003884 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3885 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003886 lockedTask.performClearTaskLocked();
3887 resumeTopActivitiesLocked();
3888 return;
3889 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003890 }
Craig Mautnere0570202015-05-13 13:06:11 -07003891 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3892 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003893 return;
3894 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003895
3896 // Should have already been checked, but do it again.
3897 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003898 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3899 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003900 return;
3901 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003902 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003903 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003904 return;
3905 }
3906
3907 if (mLockTaskModeTasks.isEmpty()) {
3908 // First locktask.
3909 final Message lockTaskMsg = Message.obtain();
3910 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3911 lockTaskMsg.arg1 = task.userId;
3912 lockTaskMsg.what = LOCK_TASK_START_MSG;
3913 lockTaskMsg.arg2 = lockTaskModeState;
3914 mHandler.sendMessage(lockTaskMsg);
3915 }
3916 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003917 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3918 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003919 mLockTaskModeTasks.remove(task);
3920 mLockTaskModeTasks.add(task);
3921
3922 if (task.mLockTaskUid == -1) {
3923 task.mLockTaskUid = task.mCallingUid;
3924 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003925
3926 if (andResume) {
3927 findTaskToMoveToFrontLocked(task, 0, null, reason);
3928 resumeTopActivitiesLocked();
3929 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003930 }
3931
3932 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003933 return isLockTaskModeViolation(task, false);
3934 }
3935
3936 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3937 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003938 return false;
3939 }
3940 final int lockTaskAuth = task.mLockTaskAuth;
3941 switch (lockTaskAuth) {
3942 case LOCK_TASK_AUTH_DONT_LOCK:
3943 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003944 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003945 case LOCK_TASK_AUTH_LAUNCHABLE:
3946 case LOCK_TASK_AUTH_WHITELISTED:
3947 return false;
3948 case LOCK_TASK_AUTH_PINNABLE:
3949 // Pinnable tasks can't be launched on top of locktask tasks.
3950 return !mLockTaskModeTasks.isEmpty();
3951 default:
3952 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3953 return true;
3954 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003955 }
3956
Craig Mautner15df08a2015-04-01 12:17:18 -07003957 void onLockTaskPackagesUpdatedLocked() {
3958 boolean didSomething = false;
3959 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3960 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003961 final boolean wasWhitelisted =
3962 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3963 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003964 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003965 final boolean isWhitelisted =
3966 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3967 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3968 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003969 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003970 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3971 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003972 removeLockedTaskLocked(lockedTask);
3973 lockedTask.performClearTaskLocked();
3974 didSomething = true;
3975 }
3976 }
3977 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3978 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3979 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3980 final ActivityStack stack = stacks.get(stackNdx);
3981 stack.onLockTaskPackagesUpdatedLocked();
3982 }
3983 }
Craig Mautnere0570202015-05-13 13:06:11 -07003984 final ActivityRecord r = topRunningActivityLocked();
3985 final TaskRecord task = r != null ? r.task : null;
3986 if (mLockTaskModeTasks.isEmpty() && task != null
3987 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3988 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003989 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3990 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3991 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3992 false);
3993 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003994 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003995 if (didSomething) {
3996 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003997 }
3998 }
3999
Benjamin Franz43261142015-02-11 15:59:44 +00004000 int getLockTaskModeState() {
4001 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004002 }
4003
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004004 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004005
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004006 public ActivityStackSupervisorHandler(Looper looper) {
4007 super(looper);
4008 }
4009
Craig Mautnerf3333272013-04-22 10:55:53 -07004010 void activityIdleInternal(ActivityRecord r) {
4011 synchronized (mService) {
4012 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4013 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004014 }
4015
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004016 @Override
4017 public void handleMessage(Message msg) {
4018 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004019 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004020 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4021 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004022 if (mService.mDidDexOpt) {
4023 mService.mDidDexOpt = false;
4024 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4025 nmsg.obj = msg.obj;
4026 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4027 return;
4028 }
4029 // We don't at this point know if the activity is fullscreen,
4030 // so we need to be conservative and assume it isn't.
4031 activityIdleInternal((ActivityRecord)msg.obj);
4032 } break;
4033 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004034 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004035 activityIdleInternal((ActivityRecord)msg.obj);
4036 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004037 case RESUME_TOP_ACTIVITY_MSG: {
4038 synchronized (mService) {
4039 resumeTopActivitiesLocked();
4040 }
4041 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004042 case SLEEP_TIMEOUT_MSG: {
4043 synchronized (mService) {
4044 if (mService.isSleepingOrShuttingDown()) {
4045 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4046 mSleepTimeout = true;
4047 checkReadyForSleepLocked();
4048 }
4049 }
4050 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004051 case LAUNCH_TIMEOUT_MSG: {
4052 if (mService.mDidDexOpt) {
4053 mService.mDidDexOpt = false;
4054 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4055 return;
4056 }
4057 synchronized (mService) {
4058 if (mLaunchingActivity.isHeld()) {
4059 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4060 if (VALIDATE_WAKE_LOCK_CALLER
4061 && Binder.getCallingUid() != Process.myUid()) {
4062 throw new IllegalStateException("Calling must be system uid");
4063 }
4064 mLaunchingActivity.release();
4065 }
4066 }
4067 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004068 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004069 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004070 } break;
4071 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004072 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004073 } break;
4074 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004075 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004076 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004077 case CONTAINER_CALLBACK_VISIBILITY: {
4078 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004079 final IActivityContainerCallback callback = container.mCallback;
4080 if (callback != null) {
4081 try {
4082 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4083 } catch (RemoteException e) {
4084 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004085 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004086 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004087 case LOCK_TASK_START_MSG: {
4088 // When lock task starts, we disable the status bars.
4089 try {
Jason Monk62515be2014-05-21 16:06:19 -04004090 if (mLockTaskNotify == null) {
4091 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004092 }
Jason Monk62515be2014-05-21 16:06:19 -04004093 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004094 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004095 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004096 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004097 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004098 flags = StatusBarManager.DISABLE_MASK
4099 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004100 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004101 flags = StatusBarManager.DISABLE_MASK
4102 & (~StatusBarManager.DISABLE_BACK)
4103 & (~StatusBarManager.DISABLE_HOME)
4104 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004105 }
4106 getStatusBarService().disable(flags, mToken,
4107 mService.mContext.getPackageName());
4108 }
4109 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004110 if (getDevicePolicyManager() != null) {
4111 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004112 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004113 }
justinzhang5286d3f2014-05-12 17:06:01 -04004114 } catch (RemoteException ex) {
4115 throw new RuntimeException(ex);
4116 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004117 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004118 case LOCK_TASK_END_MSG: {
4119 // When lock task ends, we enable the status bars.
4120 try {
Jason Monk62515be2014-05-21 16:06:19 -04004121 if (getStatusBarService() != null) {
4122 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4123 mService.mContext.getPackageName());
4124 }
4125 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004126 if (getDevicePolicyManager() != null) {
4127 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4128 msg.arg1);
4129 }
Jason Monk62515be2014-05-21 16:06:19 -04004130 if (mLockTaskNotify == null) {
4131 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4132 }
4133 mLockTaskNotify.show(false);
4134 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004135 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004136 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004137 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004138 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004139 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004140 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004141 new LockPatternUtils(mService.mContext)
4142 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004143 }
4144 } catch (SettingNotFoundException e) {
4145 // No setting, don't lock.
4146 }
justinzhang5286d3f2014-05-12 17:06:01 -04004147 } catch (RemoteException ex) {
4148 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004149 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004150 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004151 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004152 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004153 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4154 if (mLockTaskNotify == null) {
4155 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4156 }
4157 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4158 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004159 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4160 final ActivityContainer container = (ActivityContainer) msg.obj;
4161 final IActivityContainerCallback callback = container.mCallback;
4162 if (callback != null) {
4163 try {
4164 callback.onAllActivitiesComplete(container.asBinder());
4165 } catch (RemoteException e) {
4166 }
4167 }
4168 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004169 case LAUNCH_TASK_BEHIND_COMPLETE: {
4170 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004171 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004172 if (r != null) {
4173 handleLaunchTaskBehindCompleteLocked(r);
4174 }
4175 }
4176 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004177 }
4178 }
4179 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004180
Ying Wangb081a592014-04-22 15:20:16 -07004181 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004182 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004183 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004184 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004185 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004186 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004187 ActivityRecord mParentActivity = null;
4188 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004189
Craig Mautnere3a00d72014-04-16 08:31:19 -07004190 boolean mVisible = true;
4191
Craig Mautner4a1cb222013-12-04 16:14:06 -08004192 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004193 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004194
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004195 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4196 final static int CONTAINER_STATE_NO_SURFACE = 1;
4197 final static int CONTAINER_STATE_FINISHING = 2;
4198 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4199
4200 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004201 synchronized (mService) {
4202 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004203 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004204 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004205 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004206 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004207 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004208
Craig Mautnere0a38842013-12-16 16:14:02 -08004209 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004210 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Craig Mautner34b73df2014-01-12 21:11:08 -08004211 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08004212 mActivityDisplay = activityDisplay;
4213 mStack.mDisplayId = activityDisplay.mDisplayId;
4214 mStack.mStacks = activityDisplay.mStacks;
4215
4216 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08004217 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004218 }
4219
4220 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004221 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004222 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004223 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4224 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004225 return;
4226 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004227 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004228 }
4229 }
4230
4231 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004232 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004233 synchronized (mService) {
4234 if (mActivityDisplay != null) {
4235 return mActivityDisplay.mDisplayId;
4236 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004237 }
4238 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004239 }
4240
Jeff Brownca9bc702014-02-11 14:32:56 -08004241 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004242 public int getStackId() {
4243 synchronized (mService) {
4244 return mStackId;
4245 }
4246 }
4247
4248 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004249 public boolean injectEvent(InputEvent event) {
4250 final long origId = Binder.clearCallingIdentity();
4251 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004252 synchronized (mService) {
4253 if (mActivityDisplay != null) {
4254 return mInputManagerInternal.injectInputEvent(event,
4255 mActivityDisplay.mDisplayId,
4256 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4257 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004258 }
4259 return false;
4260 } finally {
4261 Binder.restoreCallingIdentity(origId);
4262 }
4263 }
4264
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004265 @Override
4266 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004267 synchronized (mService) {
4268 if (mContainerState == CONTAINER_STATE_FINISHING) {
4269 return;
4270 }
4271 mContainerState = CONTAINER_STATE_FINISHING;
4272
Craig Mautnerd163e752014-06-13 17:18:47 -07004273 long origId = Binder.clearCallingIdentity();
4274 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004275 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004276 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004277 } finally {
4278 Binder.restoreCallingIdentity(origId);
4279 }
4280 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004281 }
4282
Craig Mautner60257702014-09-17 15:02:33 -07004283 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004284 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004285 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004286 if (mActivityDisplay != null) {
4287 mActivityDisplay.detachActivitiesLocked(mStack);
4288 mActivityDisplay = null;
4289 mStack.mDisplayId = -1;
4290 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08004291 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004292 }
4293 }
4294
4295 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004296 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004297 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004298 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004299 Binder.getCallingUid(), mCurrentUser, false,
4300 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004301
Craig Mautnere0a38842013-12-16 16:14:02 -08004302 // TODO: Switch to user app stacks here.
4303 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004304 final Uri data = intent.getData();
4305 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4306 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004307 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004308 checkEmbeddedAllowedInner(userId, intent, mimeType);
4309
4310 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4311 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004312 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004313 }
4314
4315 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004316 public final int startActivityIntentSender(IIntentSender intentSender)
4317 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004318 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4319
4320 if (!(intentSender instanceof PendingIntentRecord)) {
4321 throw new IllegalArgumentException("Bad PendingIntent object");
4322 }
4323
Craig Mautnerb9168362015-02-26 20:40:19 -08004324 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004325 Binder.getCallingUid(), mCurrentUser, false,
4326 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004327
4328 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4329 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4330 pendingIntent.key.requestResolvedType);
4331
4332 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4333 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4334 }
4335
4336 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004337 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004338 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004339 throw new SecurityException(
4340 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4341 }
4342 }
4343
Craig Mautnerdf88d732014-01-27 09:21:32 -08004344 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004345 public IBinder asBinder() {
4346 return this;
4347 }
4348
Craig Mautner4504de52013-12-20 09:06:56 -08004349 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004350 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004351 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004352 }
4353
Craig Mautner4a1cb222013-12-04 16:14:06 -08004354 ActivityStackSupervisor getOuter() {
4355 return ActivityStackSupervisor.this;
4356 }
4357
Craig Mautnerd163e752014-06-13 17:18:47 -07004358 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004359 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004360 }
4361
4362 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004363 synchronized (mService) {
4364 if (mActivityDisplay != null) {
4365 mActivityDisplay.getBounds(outBounds);
4366 } else {
4367 outBounds.set(0, 0);
4368 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004369 }
4370 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004371
Craig Mautner6985bad2014-04-21 15:22:06 -07004372 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004373 void setVisible(boolean visible) {
4374 if (mVisible != visible) {
4375 mVisible = visible;
4376 if (mCallback != null) {
4377 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4378 0 /* unused */, this).sendToTarget();
4379 }
4380 }
4381 }
4382
Craig Mautner6985bad2014-04-21 15:22:06 -07004383 void setDrawn() {
4384 }
4385
Craig Mautner1b4bf852014-05-26 15:06:32 -07004386 // You can always start a new task on a regular ActivityStack.
4387 boolean isEligibleForNewTasks() {
4388 return true;
4389 }
4390
Craig Mautnerd163e752014-06-13 17:18:47 -07004391 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004392 detachLocked();
4393 deleteActivityContainer(this);
4394 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004395 }
4396
Craig Mautner34b73df2014-01-12 21:11:08 -08004397 @Override
4398 public String toString() {
4399 return mIdString + (mActivityDisplay == null ? "N" : "A");
4400 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004401 }
4402
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004403 private class VirtualActivityContainer extends ActivityContainer {
4404 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004405 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004406
4407 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4408 super(getNextStackId());
4409 mParentActivity = parent;
4410 mCallback = callback;
4411 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004412 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004413 }
4414
4415 @Override
4416 public void setSurface(Surface surface, int width, int height, int density) {
4417 super.setSurface(surface, width, height, density);
4418
4419 synchronized (mService) {
4420 final long origId = Binder.clearCallingIdentity();
4421 try {
4422 setSurfaceLocked(surface, width, height, density);
4423 } finally {
4424 Binder.restoreCallingIdentity(origId);
4425 }
4426 }
4427 }
4428
4429 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4430 if (mContainerState == CONTAINER_STATE_FINISHING) {
4431 return;
4432 }
4433 VirtualActivityDisplay virtualActivityDisplay =
4434 (VirtualActivityDisplay) mActivityDisplay;
4435 if (virtualActivityDisplay == null) {
4436 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004437 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004438 mActivityDisplay = virtualActivityDisplay;
4439 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4440 attachToDisplayLocked(virtualActivityDisplay);
4441 }
4442
4443 if (mSurface != null) {
4444 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004445 }
4446
Craig Mautner6985bad2014-04-21 15:22:06 -07004447 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004448 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004449 mStack.resumeTopActivityLocked(null);
4450 } else {
4451 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004452 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004453 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004454 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004455 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004456 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004457
Craig Mautnerd163e752014-06-13 17:18:47 -07004458 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004459
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004460 if (DEBUG_STACK) Slog.d(TAG_STACK,
4461 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004462 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004463
Craig Mautner6985bad2014-04-21 15:22:06 -07004464 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004465 boolean isAttachedLocked() {
4466 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004467 }
4468
4469 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004470 void setDrawn() {
4471 synchronized (mService) {
4472 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004473 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004474 }
4475 }
4476
Craig Mautner1b4bf852014-05-26 15:06:32 -07004477 // Never start a new task on an ActivityView if it isn't explicitly specified.
4478 @Override
4479 boolean isEligibleForNewTasks() {
4480 return false;
4481 }
4482
Craig Mautnerd163e752014-06-13 17:18:47 -07004483 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004484 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004485 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4486 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4487 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4488 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4489 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004490 }
4491 }
4492
Craig Mautner4a1cb222013-12-04 16:14:06 -08004493 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4494 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004495 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004496 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004497 int mDisplayId;
4498 Display mDisplay;
4499 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004500
Craig Mautner4a1cb222013-12-04 16:14:06 -08004501 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4502 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004503 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004504
Jose Lima4b6c6692014-08-12 17:41:12 -07004505 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004506
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004507 ActivityDisplay() {
4508 }
Craig Mautner4504de52013-12-20 09:06:56 -08004509
Craig Mautner1a70a162014-09-13 12:09:31 -07004510 // After instantiation, check that mDisplay is not null before using this. The alternative
4511 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004512 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004513 final Display display = mDisplayManager.getDisplay(displayId);
4514 if (display == null) {
4515 return;
4516 }
4517 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004518 }
4519
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004520 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004521 mDisplay = display;
4522 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004523 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004524 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004525
4526 void attachActivities(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004527 if (DEBUG_STACK) Slog.v(TAG_STACK,
4528 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004529 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004530 }
4531
Craig Mautnere0a38842013-12-16 16:14:02 -08004532 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004533 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004534 + " from displayId=" + mDisplayId);
4535 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004536 }
4537
4538 void getBounds(Point bounds) {
4539 mDisplay.getDisplayInfo(mDisplayInfo);
4540 bounds.x = mDisplayInfo.appWidth;
4541 bounds.y = mDisplayInfo.appHeight;
4542 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004543
Jose Lima4b6c6692014-08-12 17:41:12 -07004544 void setVisibleBehindActivity(ActivityRecord r) {
4545 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004546 }
4547
Jose Lima4b6c6692014-08-12 17:41:12 -07004548 boolean hasVisibleBehindActivity() {
4549 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004550 }
4551
Craig Mautner34b73df2014-01-12 21:11:08 -08004552 @Override
4553 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004554 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4555 }
4556 }
4557
4558 class VirtualActivityDisplay extends ActivityDisplay {
4559 VirtualDisplay mVirtualDisplay;
4560
Craig Mautner6985bad2014-04-21 15:22:06 -07004561 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004562 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004563 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4564 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4565 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4566 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004567
4568 init(mVirtualDisplay.getDisplay());
4569
4570 mWindowManager.handleDisplayAdded(mDisplayId);
4571 }
4572
4573 void setSurface(Surface surface) {
4574 if (mVirtualDisplay != null) {
4575 mVirtualDisplay.setSurface(surface);
4576 }
4577 }
4578
4579 @Override
4580 void detachActivitiesLocked(ActivityStack stack) {
4581 super.detachActivitiesLocked(stack);
4582 if (mVirtualDisplay != null) {
4583 mVirtualDisplay.release();
4584 mVirtualDisplay = null;
4585 }
4586 }
4587
4588 @Override
4589 public String toString() {
4590 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004591 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004592 }
Jose Lima58e66d62014-05-27 20:00:27 -07004593
4594 private boolean isLeanbackOnlyDevice() {
4595 boolean onLeanbackOnly = false;
4596 try {
4597 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4598 PackageManager.FEATURE_LEANBACK_ONLY);
4599 } catch (RemoteException e) {
4600 // noop
4601 }
4602
4603 return onLeanbackOnly;
4604 }
Craig Mautner27084302013-03-25 08:05:25 -07004605}