blob: c12aff6b626d6586b46f5a6b61b1784f08b6281f [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();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700514 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800515 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700516 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700517 }
Craig Mautner299f9602015-01-26 09:47:33 -0800518 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700519 }
520
Craig Mautner8d341ef2013-03-26 09:03:27 -0700521 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700522 return anyTaskForIdLocked(id, true);
523 }
524
525 /**
526 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
527 * @param id Id of the task we would like returned.
528 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
529 * restore the task from recents to the active list.
530 */
531 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800532 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800533 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800534 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800535 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
536 ActivityStack stack = stacks.get(stackNdx);
537 TaskRecord task = stack.taskForIdLocked(id);
538 if (task != null) {
539 return task;
540 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700541 }
542 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800543
544 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700545 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800546 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800547 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700548 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800549 return null;
550 }
551
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700552 if (!restoreFromRecents) {
553 return task;
554 }
555
Wale Ogunwale7de05352014-12-12 15:21:33 -0800556 if (!restoreRecentTaskLocked(task)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700557 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
558 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800559 return null;
560 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700561 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800562 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700563 }
564
Craig Mautner6170f732013-04-02 13:05:23 -0700565 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800566 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800567 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800568 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800569 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
570 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
571 if (r != null) {
572 return r;
573 }
Craig Mautner6170f732013-04-02 13:05:23 -0700574 }
575 }
576 return null;
577 }
578
Craig Mautneref73ee12014-04-23 11:45:37 -0700579 void setNextTaskId(int taskId) {
580 if (taskId > mCurTaskId) {
581 mCurTaskId = taskId;
582 }
583 }
584
Craig Mautner8d341ef2013-03-26 09:03:27 -0700585 int getNextTaskId() {
586 do {
587 mCurTaskId++;
588 if (mCurTaskId <= 0) {
589 mCurTaskId = 1;
590 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700591 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700592 return mCurTaskId;
593 }
594
Craig Mautnerde4ef022013-04-07 19:01:33 -0700595 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800596 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700597 if (stack == null) {
598 return null;
599 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700600 ActivityRecord resumedActivity = stack.mResumedActivity;
601 if (resumedActivity == null || resumedActivity.app == null) {
602 resumedActivity = stack.mPausingActivity;
603 if (resumedActivity == null || resumedActivity.app == null) {
604 resumedActivity = stack.topRunningActivityLocked(null);
605 }
606 }
607 return resumedActivity;
608 }
609
Dianne Hackbornff072722014-09-24 10:56:28 -0700610 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700611 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800612 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800613 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
614 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800615 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
616 final ActivityStack stack = stacks.get(stackNdx);
617 if (!isFrontStack(stack)) {
618 continue;
619 }
620 ActivityRecord hr = stack.topRunningActivityLocked(null);
621 if (hr != null) {
622 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
623 && processName.equals(hr.processName)) {
624 try {
George Mount2c92c972014-03-20 09:38:23 -0700625 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800626 didSomething = true;
627 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700628 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800629 Slog.w(TAG, "Exception in new application when starting activity "
630 + hr.intent.getComponent().flattenToShortString(), e);
631 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700632 }
Craig Mautner20e72272013-04-01 13:45:53 -0700633 }
Craig Mautner20e72272013-04-01 13:45:53 -0700634 }
635 }
636 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700637 if (!didSomething) {
638 ensureActivitiesVisibleLocked(null, 0);
639 }
Craig Mautner20e72272013-04-01 13:45:53 -0700640 return didSomething;
641 }
642
643 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800644 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
645 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800646 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
647 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800648 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800649 continue;
650 }
651 final ActivityRecord resumedActivity = stack.mResumedActivity;
652 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700653 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800654 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800655 return false;
656 }
Craig Mautner20e72272013-04-01 13:45:53 -0700657 }
658 }
659 return true;
660 }
661
Craig Mautnerde4ef022013-04-07 19:01:33 -0700662 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800663 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
664 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800665 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
666 final ActivityStack stack = stacks.get(stackNdx);
667 if (isFrontStack(stack)) {
668 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700669 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800670 return false;
671 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700672 }
673 }
674 }
675 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700676 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800677 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
678 mLastFocusedStack + " to=" + mFocusedStack);
679 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700680 return true;
681 }
682
683 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800684 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800685 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
686 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800687 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
688 final ActivityStack stack = stacks.get(stackNdx);
689 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800690 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700691 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800692 return false;
693 }
694 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800695 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700696 }
697 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800698 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700699 }
700
Craig Mautner2acc3892013-09-23 10:28:14 -0700701 /**
702 * Pause all activities in either all of the stacks or just the back stacks.
703 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700704 * @return true if any activity was paused as a result of this call.
705 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700706 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700707 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800708 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
709 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800710 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
711 final ActivityStack stack = stacks.get(stackNdx);
712 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700713 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800714 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700715 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
716 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800717 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700718 }
719 }
720 return someActivityPaused;
721 }
722
Craig Mautnerde4ef022013-04-07 19:01:33 -0700723 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700724 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800725 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
726 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800727 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
728 final ActivityStack stack = stacks.get(stackNdx);
729 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700730 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800731 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700732 Slog.d(TAG_STATES,
733 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800734 pausing = false;
735 } else {
736 return false;
737 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700738 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700739 }
740 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700741 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700742 }
743
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700744 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
745 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500746 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800747 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
748 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
749 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
750 final ActivityStack stack = stacks.get(stackNdx);
751 if (stack.mResumedActivity != null &&
752 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700753 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800754 }
755 }
756 }
757 }
758
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700759 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700760 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700761 }
762
763 void sendWaitingVisibleReportLocked(ActivityRecord r) {
764 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700765 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700766 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700767 if (w.who == null) {
768 changed = true;
769 w.timeout = false;
770 if (r != null) {
771 w.who = new ComponentName(r.info.packageName, r.info.name);
772 }
773 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
774 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700775 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700776 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700777 if (changed) {
778 mService.notifyAll();
779 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700780 }
781
782 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
783 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700784 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700785 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700786 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700787 if (w.who == null) {
788 changed = true;
789 w.timeout = timeout;
790 if (r != null) {
791 w.who = new ComponentName(r.info.packageName, r.info.name);
792 }
793 w.thisTime = thisTime;
794 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700795 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700796 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700797 if (changed) {
798 mService.notifyAll();
799 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700800 }
801
Craig Mautner29219d92013-04-16 20:19:12 -0700802 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800803 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700804 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
805 if (r != null) {
806 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700807 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700808
Craig Mautner4a1cb222013-12-04 16:14:06 -0800809 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800810 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800811 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
812 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700813 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700814 r = stack.topRunningActivityLocked(null);
815 if (r != null) {
816 return r;
817 }
818 }
819 }
820 return null;
821 }
822
Dianne Hackborn09233282014-04-30 11:33:59 -0700823 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700824 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800825 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
826 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800827 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800828 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800829 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800830 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
831 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700832 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800833 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700834 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700835 }
836 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700837
838 // The lists are already sorted from most recent to oldest. Just pull the most recent off
839 // each list and add it to list. Stop when all lists are empty or maxNum reached.
840 while (maxNum > 0) {
841 long mostRecentActiveTime = Long.MIN_VALUE;
842 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800843 final int numTaskLists = runningTaskLists.size();
844 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
845 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700846 if (!stackTaskList.isEmpty()) {
847 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
848 if (lastActiveTime > mostRecentActiveTime) {
849 mostRecentActiveTime = lastActiveTime;
850 selectedStackList = stackTaskList;
851 }
852 }
853 }
854 if (selectedStackList != null) {
855 list.add(selectedStackList.remove(0));
856 --maxNum;
857 } else {
858 break;
859 }
860 }
Craig Mautner20e72272013-04-01 13:45:53 -0700861 }
862
Craig Mautner23ac33b2013-04-01 16:26:35 -0700863 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700864 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700865 // Collect information about the target of the Intent.
866 ActivityInfo aInfo;
867 try {
868 ResolveInfo rInfo =
869 AppGlobals.getPackageManager().resolveIntent(
870 intent, resolvedType,
871 PackageManager.MATCH_DEFAULT_ONLY
872 | ActivityManagerService.STOCK_PM_FLAGS, userId);
873 aInfo = rInfo != null ? rInfo.activityInfo : null;
874 } catch (RemoteException e) {
875 aInfo = null;
876 }
877
878 if (aInfo != null) {
879 // Store the found target back into the intent, because now that
880 // we have it we never want to do this again. For example, if the
881 // user navigates back to this point in the history, we should
882 // always restart the exact same activity.
883 intent.setComponent(new ComponentName(
884 aInfo.applicationInfo.packageName, aInfo.name));
885
886 // Don't debug things in the system process
887 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
888 if (!aInfo.processName.equals("system")) {
889 mService.setDebugApp(aInfo.processName, true, false);
890 }
891 }
892
893 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
894 if (!aInfo.processName.equals("system")) {
895 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
896 }
897 }
898
Jeff Hao1b012d32014-08-20 10:35:34 -0700899 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700900 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700901 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700902 }
903 }
904 }
905 return aInfo;
906 }
907
Craig Mautner299f9602015-01-26 09:47:33 -0800908 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
909 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700910 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
911 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
912 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
913 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
914 0 /* startFlags */, null /* options */, false /* componentSpecified */,
915 null /* outActivity */, null /* container */, null /* inTask */);
916 if (inResumeTopActivity) {
917 // If we are in resume section already, home activity will be initialized, but not
918 // resumed (to avoid recursive resume) and will stay that way until something pokes it
919 // again. We need to schedule another resume.
920 scheduleResumeTopActivities();
921 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700922 }
923
Craig Mautner23ac33b2013-04-01 16:26:35 -0700924 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700925 String callingPackage, Intent intent, String resolvedType,
926 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700927 IBinder resultTo, String resultWho, int requestCode, int startFlags,
928 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700929 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700930 // Refuse possible leaked file descriptors
931 if (intent != null && intent.hasFileDescriptors()) {
932 throw new IllegalArgumentException("File descriptors passed in Intent");
933 }
934 boolean componentSpecified = intent.getComponent() != null;
935
936 // Don't modify the client's object!
937 intent = new Intent(intent);
938
939 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700940 ActivityInfo aInfo =
941 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700942
Craig Mautnere0a38842013-12-16 16:14:02 -0800943 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700944 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800945 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700946 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800947 // Cannot start a child activity if the parent is not resumed.
948 return ActivityManager.START_CANCELED;
949 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700950 final int realCallingPid = Binder.getCallingPid();
951 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700952 int callingPid;
953 if (callingUid >= 0) {
954 callingPid = -1;
955 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700956 callingPid = realCallingPid;
957 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700958 } else {
959 callingPid = callingUid = -1;
960 }
961
Craig Mautnere0a38842013-12-16 16:14:02 -0800962 final ActivityStack stack;
963 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800964 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800965 } else {
966 stack = container.mStack;
967 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800968 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700969 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700970 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700971
972 final long origId = Binder.clearCallingIdentity();
973
974 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800975 (aInfo.applicationInfo.privateFlags
976 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700977 // This may be a heavy-weight process! Check to see if we already
978 // have another, different heavy-weight process running.
979 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
980 if (mService.mHeavyWeightProcess != null &&
981 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
982 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700983 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700984 if (caller != null) {
985 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
986 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700987 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700988 } else {
989 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700990 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700991 + intent.toString());
992 ActivityOptions.abort(options);
993 return ActivityManager.START_PERMISSION_DENIED;
994 }
995 }
996
997 IIntentSender target = mService.getIntentSenderLocked(
998 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700999 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001000 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1001 | PendingIntent.FLAG_ONE_SHOT, null);
1002
1003 Intent newIntent = new Intent();
1004 if (requestCode >= 0) {
1005 // Caller is requesting a result.
1006 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1007 }
1008 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1009 new IntentSender(target));
1010 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1011 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1012 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1013 hist.packageName);
1014 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1015 hist.task.taskId);
1016 }
1017 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1018 aInfo.packageName);
1019 newIntent.setFlags(intent.getFlags());
1020 newIntent.setClassName("android",
1021 HeavyWeightSwitcherActivity.class.getName());
1022 intent = newIntent;
1023 resolvedType = null;
1024 caller = null;
1025 callingUid = Binder.getCallingUid();
1026 callingPid = Binder.getCallingPid();
1027 componentSpecified = true;
1028 try {
1029 ResolveInfo rInfo =
1030 AppGlobals.getPackageManager().resolveIntent(
1031 intent, null,
1032 PackageManager.MATCH_DEFAULT_ONLY
1033 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1034 aInfo = rInfo != null ? rInfo.activityInfo : null;
1035 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1036 } catch (RemoteException e) {
1037 aInfo = null;
1038 }
1039 }
1040 }
1041 }
1042
Dianne Hackborn91097de2014-04-04 18:02:06 -07001043 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1044 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001045 requestCode, callingPid, callingUid, callingPackage,
1046 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001047 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001048
Craig Mautner85c11a82014-07-17 12:38:18 -07001049 Binder.restoreCallingIdentity(origId);
1050
Craig Mautnerde4ef022013-04-07 19:01:33 -07001051 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001052 // If the caller also wants to switch to a new configuration,
1053 // do so now. This allows a clean switch, as we are waiting
1054 // for the current activity to pause (so we will not destroy
1055 // it), and have not yet started the next activity.
1056 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1057 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001058 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001059 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001060 "Updating to new configuration after starting activity.");
1061 mService.updateConfigurationLocked(config, null, false, false);
1062 }
1063
Craig Mautner23ac33b2013-04-01 16:26:35 -07001064 if (outResult != null) {
1065 outResult.result = res;
1066 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001067 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001068 do {
1069 try {
1070 mService.wait();
1071 } catch (InterruptedException e) {
1072 }
1073 } while (!outResult.timeout && outResult.who == null);
1074 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001075 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001076 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001077 outResult.timeout = false;
1078 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1079 outResult.totalTime = 0;
1080 outResult.thisTime = 0;
1081 } else {
1082 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001083 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001084 do {
1085 try {
1086 mService.wait();
1087 } catch (InterruptedException e) {
1088 }
1089 } while (!outResult.timeout && outResult.who == null);
1090 }
1091 }
1092 }
1093
1094 return res;
1095 }
1096 }
1097
1098 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1099 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1100 Bundle options, int userId) {
1101 if (intents == null) {
1102 throw new NullPointerException("intents is null");
1103 }
1104 if (resolvedTypes == null) {
1105 throw new NullPointerException("resolvedTypes is null");
1106 }
1107 if (intents.length != resolvedTypes.length) {
1108 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1109 }
1110
Craig Mautner23ac33b2013-04-01 16:26:35 -07001111
1112 int callingPid;
1113 if (callingUid >= 0) {
1114 callingPid = -1;
1115 } else if (caller == null) {
1116 callingPid = Binder.getCallingPid();
1117 callingUid = Binder.getCallingUid();
1118 } else {
1119 callingPid = callingUid = -1;
1120 }
1121 final long origId = Binder.clearCallingIdentity();
1122 try {
1123 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001124 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001125 for (int i=0; i<intents.length; i++) {
1126 Intent intent = intents[i];
1127 if (intent == null) {
1128 continue;
1129 }
1130
1131 // Refuse possible leaked file descriptors
1132 if (intent != null && intent.hasFileDescriptors()) {
1133 throw new IllegalArgumentException("File descriptors passed in Intent");
1134 }
1135
1136 boolean componentSpecified = intent.getComponent() != null;
1137
1138 // Don't modify the client's object!
1139 intent = new Intent(intent);
1140
1141 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001142 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001143 // TODO: New, check if this is correct
1144 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1145
1146 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001147 (aInfo.applicationInfo.privateFlags
1148 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001149 throw new IllegalArgumentException(
1150 "FLAG_CANT_SAVE_STATE not supported here");
1151 }
1152
1153 Bundle theseOptions;
1154 if (options != null && i == intents.length-1) {
1155 theseOptions = options;
1156 } else {
1157 theseOptions = null;
1158 }
Craig Mautner6170f732013-04-02 13:05:23 -07001159 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001160 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1161 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001162 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001163 if (res < 0) {
1164 return res;
1165 }
1166
1167 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1168 }
1169 }
1170 } finally {
1171 Binder.restoreCallingIdentity(origId);
1172 }
1173
1174 return ActivityManager.START_SUCCESS;
1175 }
1176
Craig Mautner2420ead2013-04-01 17:13:20 -07001177 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001178 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001179 throws RemoteException {
1180
Craig Mautner2568c3a2015-03-26 14:22:34 -07001181 if (andResume) {
1182 r.startFreezingScreenLocked(app, 0);
1183 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001184
Craig Mautner2568c3a2015-03-26 14:22:34 -07001185 // schedule launch ticks to collect information about slow apps.
1186 r.startLaunchTickingLocked();
1187 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001188
1189 // Have the window manager re-evaluate the orientation of
1190 // the screen based on the new activity order. Note that
1191 // as a result of this, it can call back into the activity
1192 // manager with a new orientation. We don't care about that,
1193 // because the activity is not currently running so we are
1194 // just restarting it anyway.
1195 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001196 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001197 mService.mConfiguration,
1198 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1199 mService.updateConfigurationLocked(config, r, false, false);
1200 }
1201
1202 r.app = app;
1203 app.waitingToKill = null;
1204 r.launchCount++;
1205 r.lastLaunchTime = SystemClock.uptimeMillis();
1206
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001207 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001208
1209 int idx = app.activities.indexOf(r);
1210 if (idx < 0) {
1211 app.activities.add(r);
1212 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001213 mService.updateLruProcessLocked(app, true, null);
1214 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001215
Craig Mautner15df08a2015-04-01 12:17:18 -07001216 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001217 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1218 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001219 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001220 }
1221
1222 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001223 try {
1224 if (app.thread == null) {
1225 throw new RemoteException();
1226 }
1227 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001228 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001229 if (andResume) {
1230 results = r.results;
1231 newIntents = r.newIntents;
1232 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001233 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1234 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1235 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001236 if (andResume) {
1237 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1238 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001239 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001240 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001241 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001242 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001243 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001244 }
1245 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1246 r.sleeping = false;
1247 r.forceNewConfig = false;
1248 mService.showAskCompatModeDialogLocked(r);
1249 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001250 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001251 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1252 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1253 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001254 final String profileFile = mService.mProfileFile;
1255 if (profileFile != null) {
1256 ParcelFileDescriptor profileFd = mService.mProfileFd;
1257 if (profileFd != null) {
1258 try {
1259 profileFd = profileFd.dup();
1260 } catch (IOException e) {
1261 if (profileFd != null) {
1262 try {
1263 profileFd.close();
1264 } catch (IOException o) {
1265 }
1266 profileFd = null;
1267 }
1268 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001269 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001270
1271 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1272 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001273 }
1274 }
1275 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001276
Craig Mautner2568c3a2015-03-26 14:22:34 -07001277 if (andResume) {
1278 app.hasShownUi = true;
1279 app.pendingUiClean = true;
1280 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001281 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001282 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001283 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001284 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001285 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001286 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001287
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001288 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001289 // This may be a heavy-weight process! Note that the package
1290 // manager will ensure that only activity can run in the main
1291 // process of the .apk, which is the only thing that will be
1292 // considered heavy-weight.
1293 if (app.processName.equals(app.info.packageName)) {
1294 if (mService.mHeavyWeightProcess != null
1295 && mService.mHeavyWeightProcess != app) {
1296 Slog.w(TAG, "Starting new heavy weight process " + app
1297 + " when already running "
1298 + mService.mHeavyWeightProcess);
1299 }
1300 mService.mHeavyWeightProcess = app;
1301 Message msg = mService.mHandler.obtainMessage(
1302 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1303 msg.obj = r;
1304 mService.mHandler.sendMessage(msg);
1305 }
1306 }
1307
1308 } catch (RemoteException e) {
1309 if (r.launchFailed) {
1310 // This is the second time we failed -- finish activity
1311 // and give up.
1312 Slog.e(TAG, "Second failure launching "
1313 + r.intent.getComponent().flattenToShortString()
1314 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001315 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001316 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1317 "2nd-crash", false);
1318 return false;
1319 }
1320
1321 // This is the first time we failed -- restart process and
1322 // retry.
1323 app.activities.remove(r);
1324 throw e;
1325 }
1326
1327 r.launchFailed = false;
1328 if (stack.updateLRUListLocked(r)) {
1329 Slog.w(TAG, "Activity " + r
1330 + " being launched, but already in LRU list");
1331 }
1332
1333 if (andResume) {
1334 // As part of the process of launching, ActivityThread also performs
1335 // a resume.
1336 stack.minimalResumeActivityLocked(r);
1337 } else {
1338 // This activity is not starting in the resumed state... which
1339 // should look like we asked it to pause+stop (but remain visible),
1340 // and it has done so and reported back the current icicle and
1341 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001342 if (DEBUG_STATES) Slog.v(TAG_STATES,
1343 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001344 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001345 r.stopped = true;
1346 }
1347
1348 // Launch the new version setup screen if needed. We do this -after-
1349 // launching the initial activity (that is, home), so that it can have
1350 // a chance to initialize itself while in the background, making the
1351 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001352 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001353 mService.startSetupActivityLocked();
1354 }
1355
Dianne Hackborn465fa392014-09-14 14:21:18 -07001356 // Update any services we are bound to that might care about whether
1357 // their client may have activities.
1358 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1359
Craig Mautner2420ead2013-04-01 17:13:20 -07001360 return true;
1361 }
1362
Craig Mautnere79d42682013-04-01 19:01:53 -07001363 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001364 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001365 // Is this activity's application already running?
1366 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001367 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001368
1369 r.task.stack.setLaunchTime(r);
1370
1371 if (app != null && app.thread != null) {
1372 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001373 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1374 || !"android".equals(r.info.packageName)) {
1375 // Don't add this if it is a platform component that is marked
1376 // to run in multiple processes, because this is actually
1377 // part of the framework so doesn't make sense to track as a
1378 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001379 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1380 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001381 }
George Mount2c92c972014-03-20 09:38:23 -07001382 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001383 return;
1384 } catch (RemoteException e) {
1385 Slog.w(TAG, "Exception when starting activity "
1386 + r.intent.getComponent().flattenToShortString(), e);
1387 }
1388
1389 // If a dead object exception was thrown -- fall through to
1390 // restart the application.
1391 }
1392
1393 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001394 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001395 }
1396
Craig Mautner6170f732013-04-02 13:05:23 -07001397 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001398 Intent intent, String resolvedType, ActivityInfo aInfo,
1399 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1400 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001401 int callingPid, int callingUid, String callingPackage,
1402 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001403 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1404 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001405 int err = ActivityManager.START_SUCCESS;
1406
1407 ProcessRecord callerApp = null;
1408 if (caller != null) {
1409 callerApp = mService.getRecordForAppLocked(caller);
1410 if (callerApp != null) {
1411 callingPid = callerApp.pid;
1412 callingUid = callerApp.info.uid;
1413 } else {
1414 Slog.w(TAG, "Unable to find app for caller " + caller
1415 + " (pid=" + callingPid + ") when starting: "
1416 + intent.toString());
1417 err = ActivityManager.START_PERMISSION_DENIED;
1418 }
1419 }
1420
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001421 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1422
Craig Mautner6170f732013-04-02 13:05:23 -07001423 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001424 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001425 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001426 + " on display " + (container == null ? (mFocusedStack == null ?
1427 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1428 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1429 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001430 }
1431
1432 ActivityRecord sourceRecord = null;
1433 ActivityRecord resultRecord = null;
1434 if (resultTo != null) {
1435 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001436 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1437 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001438 if (sourceRecord != null) {
1439 if (requestCode >= 0 && !sourceRecord.finishing) {
1440 resultRecord = sourceRecord;
1441 }
1442 }
1443 }
Craig Mautner6170f732013-04-02 13:05:23 -07001444
Dianne Hackborn91097de2014-04-04 18:02:06 -07001445 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001446
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001447 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001448 // Transfer the result target from the source activity to the new
1449 // one being started, including any failures.
1450 if (requestCode >= 0) {
1451 ActivityOptions.abort(options);
1452 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1453 }
1454 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001455 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1456 resultRecord = null;
1457 }
Craig Mautner6170f732013-04-02 13:05:23 -07001458 resultWho = sourceRecord.resultWho;
1459 requestCode = sourceRecord.requestCode;
1460 sourceRecord.resultTo = null;
1461 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001462 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001463 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001464 if (sourceRecord.launchedFromUid == callingUid) {
1465 // The new activity is being launched from the same uid as the previous
1466 // activity in the flow, and asking to forward its result back to the
1467 // previous. In this case the activity is serving as a trampoline between
1468 // the two, so we also want to update its launchedFromPackage to be the
1469 // same as the previous activity. Note that this is safe, since we know
1470 // these two packages come from the same uid; the caller could just as
1471 // well have supplied that same package name itself. This specifially
1472 // deals with the case of an intent picker/chooser being launched in the app
1473 // flow to redirect to an activity picked by the user, where we want the final
1474 // activity to consider it to have been launched by the previous app activity.
1475 callingPackage = sourceRecord.launchedFromPackage;
1476 }
Craig Mautner6170f732013-04-02 13:05:23 -07001477 }
1478
1479 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1480 // We couldn't find a class that can handle the given Intent.
1481 // That's the end of that!
1482 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1483 }
1484
1485 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1486 // We couldn't find the specific class specified in the Intent.
1487 // Also the end of the line.
1488 err = ActivityManager.START_CLASS_NOT_FOUND;
1489 }
1490
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001491 if (err == ActivityManager.START_SUCCESS
1492 && !isCurrentProfileLocked(userId)
1493 && (aInfo.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
1494 // Trying to launch a background activity that doesn't show for all users.
1495 err = ActivityManager.START_NOT_CURRENT_USER_ACTIVITY;
1496 }
1497
Dianne Hackborn91097de2014-04-04 18:02:06 -07001498 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1499 && sourceRecord.task.voiceSession != null) {
1500 // If this activity is being launched as part of a voice session, we need
1501 // to ensure that it is safe to do so. If the upcoming activity will also
1502 // be part of the voice session, we can only launch it if it has explicitly
1503 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001504 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001505 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1506 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001507 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1508 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001509 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1510 }
1511 } catch (RemoteException e) {
1512 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1513 }
1514 }
1515 }
1516
1517 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1518 // If the caller is starting a new voice session, just make sure the target
1519 // is actually allowing it to run this way.
1520 try {
1521 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1522 intent, resolvedType)) {
1523 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1524 }
1525 } catch (RemoteException e) {
1526 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1527 }
1528 }
1529
louis_changcd5d1982014-08-01 10:09:08 +08001530 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1531
Craig Mautner6170f732013-04-02 13:05:23 -07001532 if (err != ActivityManager.START_SUCCESS) {
1533 if (resultRecord != null) {
1534 resultStack.sendActivityResultLocked(-1,
1535 resultRecord, resultWho, requestCode,
1536 Activity.RESULT_CANCELED, null);
1537 }
Craig Mautner6170f732013-04-02 13:05:23 -07001538 ActivityOptions.abort(options);
1539 return err;
1540 }
1541
Svetoslav7008b512015-06-24 18:47:07 -07001542 boolean abort = false;
1543
Svet Ganov99b60432015-06-27 13:15:22 -07001544 final int startAnyPerm = mService.checkPermission(
1545 START_ANY_ACTIVITY, callingPid, callingUid);
1546
1547 if (startAnyPerm != PERMISSION_GRANTED) {
1548 final int componentRestriction = getComponentRestrictionForCallingPackage(
1549 aInfo, callingPackage, callingPid, callingUid);
1550 final int actionRestriction = getActionRestrictionForCallingPackage(
1551 intent.getAction(), callingPackage, callingPid, callingUid);
1552
1553 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1554 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1555 if (resultRecord != null) {
1556 resultStack.sendActivityResultLocked(-1,
1557 resultRecord, resultWho, requestCode,
1558 Activity.RESULT_CANCELED, null);
1559 }
1560 String msg;
1561 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1562 msg = "Permission Denial: starting " + intent.toString()
1563 + " from " + callerApp + " (pid=" + callingPid
1564 + ", uid=" + callingUid + ")" + " with revoked permission "
1565 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1566 } else if (!aInfo.exported) {
1567 msg = "Permission Denial: starting " + intent.toString()
1568 + " from " + callerApp + " (pid=" + callingPid
1569 + ", uid=" + callingUid + ")"
1570 + " not exported from uid " + aInfo.applicationInfo.uid;
1571 } else {
1572 msg = "Permission Denial: starting " + intent.toString()
1573 + " from " + callerApp + " (pid=" + callingPid
1574 + ", uid=" + callingUid + ")"
1575 + " requires " + aInfo.permission;
1576 }
1577 Slog.w(TAG, msg);
1578 throw new SecurityException(msg);
1579 }
1580
1581 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1582 String message = "Appop Denial: starting " + intent.toString()
1583 + " from " + callerApp + " (pid=" + callingPid
1584 + ", uid=" + callingUid + ")"
1585 + " requires " + AppOpsManager.permissionToOp(
1586 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1587 Slog.w(TAG, message);
1588 abort = true;
1589 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1590 String message = "Appop Denial: starting " + intent.toString()
1591 + " from " + callerApp + " (pid=" + callingPid
1592 + ", uid=" + callingUid + ")"
1593 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1594 Slog.w(TAG, message);
1595 abort = true;
1596 }
Svetoslav7008b512015-06-24 18:47:07 -07001597 }
1598
1599 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001600 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001601
Craig Mautner6170f732013-04-02 13:05:23 -07001602 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001603 try {
1604 // The Intent we give to the watcher has the extra data
1605 // stripped off, since it can contain private information.
1606 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001607 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001608 aInfo.applicationInfo.packageName);
1609 } catch (RemoteException e) {
1610 mService.mController = null;
1611 }
Ben Gruver5e207332013-04-03 17:41:37 -07001612 }
Craig Mautner6170f732013-04-02 13:05:23 -07001613
Ben Gruver5e207332013-04-03 17:41:37 -07001614 if (abort) {
1615 if (resultRecord != null) {
1616 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001617 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001618 }
Ben Gruver5e207332013-04-03 17:41:37 -07001619 // We pretend to the caller that it was really started, but
1620 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001621 ActivityOptions.abort(options);
1622 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001623 }
1624
1625 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001626 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001627 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001628 if (outActivity != null) {
1629 outActivity[0] = r;
1630 }
1631
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001632 if (r.appTimeTracker == null && sourceRecord != null) {
1633 // If the caller didn't specify an explicit time tracker, we want to continue
1634 // tracking under any it has.
1635 r.appTimeTracker = sourceRecord.appTimeTracker;
1636 }
1637
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001638 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001639 if (voiceSession == null && (stack.mResumedActivity == null
1640 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001641 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1642 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001643 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001644 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001645 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001646 ActivityOptions.abort(options);
1647 return ActivityManager.START_SWITCHES_CANCELED;
1648 }
1649 }
1650
1651 if (mService.mDidAppSwitch) {
1652 // This is the second allowed switch since we stopped switches,
1653 // so now just generally allow switches. Use case: user presses
1654 // home (switches disabled, switch to home, mDidAppSwitch now true);
1655 // user taps a home icon (coming from home so allowed, we hit here
1656 // and now allow anyone to switch again).
1657 mService.mAppSwitchesAllowedTime = 0;
1658 } else {
1659 mService.mDidAppSwitch = true;
1660 }
1661
Craig Mautneree36c772014-07-16 14:56:05 -07001662 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001663
Dianne Hackborn91097de2014-04-04 18:02:06 -07001664 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001665 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001666
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001667 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001668 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001669 // activity start, but we are not actually doing an activity
1670 // switch... just dismiss the keyguard now, because we
1671 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001672 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001673 }
1674 return err;
1675 }
1676
Svet Ganov99b60432015-06-27 13:15:22 -07001677 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
1678 String callingPackage, int callingPid, int callingUid) {
1679 if (activityInfo.permission == null) {
1680 return ACTIVITY_RESTRICTION_NONE;
1681 }
1682
1683 if (mService.checkComponentPermission(activityInfo.permission, callingPid, callingUid,
1684 activityInfo.applicationInfo.uid, activityInfo.exported)
1685 == PackageManager.PERMISSION_DENIED) {
1686 return ACTIVITY_RESTRICTION_PERMISSION;
1687 }
1688
1689 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1690 if (opCode == AppOpsManager.OP_NONE) {
1691 return ACTIVITY_RESTRICTION_NONE;
1692 }
1693
1694 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1695 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1696 return ACTIVITY_RESTRICTION_APPOP;
1697 }
1698
1699 return ACTIVITY_RESTRICTION_NONE;
1700 }
1701
Svetoslav7008b512015-06-24 18:47:07 -07001702 private int getActionRestrictionForCallingPackage(String action,
1703 String callingPackage, int callingPid, int callingUid) {
1704 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001705 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001706 }
1707
1708 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1709 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001710 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001711 }
1712
1713 final PackageInfo packageInfo;
1714 try {
1715 packageInfo = mService.mContext.getPackageManager()
1716 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1717 } catch (PackageManager.NameNotFoundException e) {
1718 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001719 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001720 }
1721
1722 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001723 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001724 }
1725
1726 if (mService.checkPermission(permission, callingPid, callingUid) ==
1727 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001728 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001729 }
1730
1731 final int opCode = AppOpsManager.permissionToOpCode(permission);
1732 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001733 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001734 }
1735
1736 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1737 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001738 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001739 }
1740
Svet Ganov99b60432015-06-27 13:15:22 -07001741 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001742 }
1743
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001744 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001745 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001746
1747 // On leanback only devices we should keep all activities in the same stack.
1748 if (!mLeanbackOnlyDevice &&
1749 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001750
1751 ActivityStack stack;
1752
Wale Ogunwale7d701172015-03-11 15:36:30 -07001753 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001754 stack = task.stack;
1755 if (stack.isOnHomeDisplay()) {
1756 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001757 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1758 "computeStackFocus: Setting " + "focused stack to r=" + r
1759 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001760 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001761 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001762 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001763 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001764 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001765 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001766 }
1767
Craig Mautnere0a38842013-12-16 16:14:02 -08001768 final ActivityContainer container = r.mInitialActivityContainer;
1769 if (container != null) {
1770 // The first time put it on the desired stack, after this put on task stack.
1771 r.mInitialActivityContainer = null;
1772 return container.mStack;
1773 }
1774
Craig Mautner1b4bf852014-05-26 15:06:32 -07001775 if (mFocusedStack != mHomeStack && (!newTask ||
1776 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001777 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001778 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001779 return mFocusedStack;
1780 }
1781
Craig Mautnere0a38842013-12-16 16:14:02 -08001782 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1783 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001784 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001785 if (!stack.isHomeStack()) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001786 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001787 "computeStackFocus: Setting focused stack=" + stack);
1788 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001789 }
1790 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001791
Craig Mautner4a1cb222013-12-04 16:14:06 -08001792 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001793 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001794 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1795 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001796 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001797 }
1798 return mHomeStack;
1799 }
1800
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001801 boolean setFocusedStack(ActivityRecord r, String reason) {
1802 if (r == null) {
1803 // Not sure what you are trying to do, but it is not going to work...
1804 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001805 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001806 final TaskRecord task = r.task;
1807 if (task == null || task.stack == null) {
1808 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1809 return false;
1810 }
1811 task.stack.moveToFront(reason);
1812 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001813 }
1814
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001815 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001816 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001817 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001818 final Intent intent = r.intent;
1819 final int callingUid = r.launchedFromUid;
1820
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001821 // In some flows in to this function, we retrieve the task record and hold on to it
1822 // without a lock before calling back in to here... so the task at this point may
1823 // not actually be in recents. Check for that, and if it isn't in recents just
1824 // consider it invalid.
1825 if (inTask != null && !inTask.inRecents) {
1826 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1827 inTask = null;
1828 }
1829
Craig Mautnerad400af2014-08-13 16:41:36 -07001830 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001831 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1832 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001833
Craig Mautnera228ae92014-07-09 05:44:55 -07001834 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001835 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001836 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001837 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1838 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1839 "\"singleInstance\" or \"singleTask\"");
1840 launchFlags &=
1841 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1842 } else {
1843 switch (r.info.documentLaunchMode) {
1844 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1845 break;
1846 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1847 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1848 break;
1849 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1850 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1851 break;
1852 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1853 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1854 break;
1855 }
1856 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001857
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001858 final boolean launchTaskBehind = r.mLaunchTaskBehind
1859 && !launchSingleTask && !launchSingleInstance
1860 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001861
Wale Ogunwale7d701172015-03-11 15:36:30 -07001862 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1863 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001864 // For whatever reason this activity is being launched into a new
1865 // task... yet the caller has requested a result back. Well, that
1866 // is pretty messed up, so instead immediately send back a cancel
1867 // and let the new task continue launched as normal without a
1868 // dependency on its originator.
1869 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1870 r.resultTo.task.stack.sendActivityResultLocked(-1,
1871 r.resultTo, r.resultWho, r.requestCode,
1872 Activity.RESULT_CANCELED, null);
1873 r.resultTo = null;
1874 }
1875
1876 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1877 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1878 }
1879
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001880 // If we are actually going to launch in to a new task, there are some cases where
1881 // we further want to do multiple task.
1882 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1883 if (launchTaskBehind
1884 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1885 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1886 }
1887 }
1888
Craig Mautner8849a5e2013-04-02 16:41:03 -07001889 // We'll invoke onUserLeaving before onPause only if the launching
1890 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001891 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001892 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1893 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001894
1895 // If the caller has asked not to resume at this point, we make note
1896 // of this in the record so that we can skip it when trying to find
1897 // the top running activity.
1898 if (!doResume) {
1899 r.delayedResume = true;
1900 }
1901
Craig Mautnera254cd72014-05-25 16:47:39 -07001902 ActivityRecord notTop =
1903 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001904
1905 // If the onlyIfNeeded flag is set, then we can do this if the activity
1906 // being launched is the same as the one making the call... or, as
1907 // a special case, if we do not know the caller then we count the
1908 // current top activity as the caller.
1909 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1910 ActivityRecord checkedCaller = sourceRecord;
1911 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001912 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001913 }
1914 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1915 // Caller is not the same as launcher, so always needed.
1916 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1917 }
1918 }
1919
Craig Mautner8849a5e2013-04-02 16:41:03 -07001920 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001921 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001922
1923 // If the caller is not coming from another activity, but has given us an
1924 // explicit task into which they would like us to launch the new activity,
1925 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001926 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1927 final Intent baseIntent = inTask.getBaseIntent();
1928 final ActivityRecord root = inTask.getRootActivity();
1929 if (baseIntent == null) {
1930 ActivityOptions.abort(options);
1931 throw new IllegalArgumentException("Launching into task without base intent: "
1932 + inTask);
1933 }
1934
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001935 // If this task is empty, then we are adding the first activity -- it
1936 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001937 if (launchSingleInstance || launchSingleTask) {
1938 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1939 ActivityOptions.abort(options);
1940 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1941 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001942 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001943 if (root != null) {
1944 ActivityOptions.abort(options);
1945 throw new IllegalArgumentException("Caller with inTask " + inTask
1946 + " has root " + root + " but target is singleInstance/Task");
1947 }
1948 }
1949
1950 // If task is empty, then adopt the interesting intent launch flags in to the
1951 // activity being started.
1952 if (root == null) {
1953 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1954 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1955 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1956 launchFlags = (launchFlags&~flagsOfInterest)
1957 | (baseIntent.getFlags()&flagsOfInterest);
1958 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001959 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001960 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001961
Dianne Hackborn962d5352014-08-29 16:27:40 -07001962 // If the task is not empty and the caller is asking to start it as the root
1963 // of a new task, then we don't actually want to start this on the task. We
1964 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001965 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001966 addingToTask = false;
1967
1968 } else {
1969 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001970 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001971
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001972 reuseTask = inTask;
1973 } else {
1974 inTask = null;
1975 }
1976
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001977 if (inTask == null) {
1978 if (sourceRecord == null) {
1979 // This activity is not being started from another... in this
1980 // case we -always- start a new task.
1981 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1982 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1983 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1984 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1985 }
1986 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1987 // The original activity who is starting us is running as a single
1988 // instance... this new activity it is starting must go on its
1989 // own task.
1990 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1991 } else if (launchSingleInstance || launchSingleTask) {
1992 // The activity being started is a single instance... it always
1993 // gets launched into its own task.
1994 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1995 }
1996 }
1997
1998 ActivityInfo newTaskInfo = null;
1999 Intent newTaskIntent = null;
2000 ActivityStack sourceStack;
2001 if (sourceRecord != null) {
2002 if (sourceRecord.finishing) {
2003 // If the source is finishing, we can't further count it as our source. This
2004 // is because the task it is associated with may now be empty and on its way out,
2005 // so we don't want to blindly throw it in to that task. Instead we will take
2006 // the NEW_TASK flow and try to find a task for it. But save the task information
2007 // so it can be used when creating the new task.
2008 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2009 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2010 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2011 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2012 newTaskInfo = sourceRecord.info;
2013 newTaskIntent = sourceRecord.task.intent;
2014 }
2015 sourceRecord = null;
2016 sourceStack = null;
2017 } else {
2018 sourceStack = sourceRecord.task.stack;
2019 }
2020 } else {
2021 sourceStack = null;
2022 }
2023
2024 boolean movedHome = false;
2025 ActivityStack targetStack;
2026
2027 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002028 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002029
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002030 // We may want to try to place the new activity in to an existing task. We always
2031 // do this if the target activity is singleTask or singleInstance; we will also do
2032 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2033 // us to still place it in a new task: multi task, always doc mode, or being asked to
2034 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002035 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2036 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002037 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002038 // If bring to front is requested, and no result is requested and we have not
2039 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002040 // we can find a task that was started with this same
2041 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002042 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002043 // See if there is a task to bring to the front. If this is
2044 // a SINGLE_INSTANCE activity, there can be one and only one
2045 // instance of it in the history, and it is always in its own
2046 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002047 ActivityRecord intentActivity = !launchSingleInstance ?
2048 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002049 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002050 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2051 // but still needs to be a lock task mode violation since the task gets
2052 // cleared out and the device would otherwise leave the locked task.
2053 if (isLockTaskModeViolation(intentActivity.task,
2054 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2055 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002056 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002057 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002058 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2059 }
Craig Mautner29219d92013-04-16 20:19:12 -07002060 if (r.task == null) {
2061 r.task = intentActivity.task;
2062 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002063 if (intentActivity.task.intent == null) {
2064 // This task was started because of movement of
2065 // the activity based on affinity... now that we
2066 // are actually launching it, we can assign the
2067 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002068 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002069 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002070 targetStack = intentActivity.task.stack;
2071 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002072 // If the target task is not in the front, then we need
2073 // to bring it to the front... except... well, with
2074 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2075 // to have the same behavior as if a new instance was
2076 // being started, which means not bringing it to the front
2077 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002078 final ActivityStack focusStack = getFocusedStack();
2079 ActivityRecord curTop = (focusStack == null)
2080 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002081 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002082 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002083 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002084 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002085 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2086 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002087 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002088 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002089 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2090 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002091 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002092 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002093 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002094 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002095 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002096 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2097 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002098 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002099 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002100 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002101 options = null;
2102 }
2103 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002104 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002105 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002106 + " from " + intentActivity);
2107 targetStack.moveToFront("intentActivityFound");
2108 }
2109
Craig Mautner8849a5e2013-04-02 16:41:03 -07002110 // If the caller has requested that the target task be
2111 // reset, then do so.
2112 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2113 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2114 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002115 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002116 // We don't need to start a new activity, and
2117 // the client said not to do anything if that
2118 // is the case, so this is it! And for paranoia, make
2119 // sure we have correctly resumed the top activity.
2120 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002121 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002122
2123 // Make sure to notify Keyguard as well if we are not running an app
2124 // transition later.
2125 if (!movedToFront) {
2126 notifyActivityDrawnForKeyguard();
2127 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002128 } else {
2129 ActivityOptions.abort(options);
2130 }
2131 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2132 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002133 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002134 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002135 // The caller has requested to completely replace any
2136 // existing task with its new activity. Well that should
2137 // not be too hard...
2138 reuseTask = intentActivity.task;
2139 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002140 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002141 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002142 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002143 // In this situation we want to remove all activities
2144 // from the task up to the one being started. In most
2145 // cases this means we are resetting the task to its
2146 // initial state.
2147 ActivityRecord top =
2148 intentActivity.task.performClearTaskLocked(r, launchFlags);
2149 if (top != null) {
2150 if (top.frontOfTask) {
2151 // Activity aliases may mean we use different
2152 // intents for the top activity, so make sure
2153 // the task now has the identity of the new
2154 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002155 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002156 }
2157 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2158 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002159 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002160 } else {
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002161 // A special case: we need to start the activity because it is not
2162 // currently running, and the caller has asked to clear the current
2163 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002164 addingToTask = true;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002165 // Now pretend like this activity is being started by the top of its
2166 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002167 sourceRecord = intentActivity;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002168 TaskRecord task = sourceRecord.task;
2169 if (task != null && task.stack == null) {
2170 // Target stack got cleared when we all activities were removed
2171 // above. Go ahead and reset it.
2172 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2173 targetStack.addTask(
2174 task, !launchTaskBehind /* toTop */, false /* moving */);
2175 }
2176
Craig Mautner8849a5e2013-04-02 16:41:03 -07002177 }
2178 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2179 // In this case the top activity on the task is the
2180 // same as the one being launched, so we take that
2181 // as a request to bring the task to the foreground.
2182 // If the top activity in the task is the root
2183 // activity, deliver this new intent to it if it
2184 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002185 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002186 && intentActivity.realActivity.equals(r.realActivity)) {
2187 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2188 intentActivity.task);
2189 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002190 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002191 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002192 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2193 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002194 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2195 // In this case we are launching the root activity
2196 // of the task, but with a different intent. We
2197 // should start a new instance on top.
2198 addingToTask = true;
2199 sourceRecord = intentActivity;
2200 }
2201 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2202 // In this case an activity is being launched in to an
2203 // existing task, without resetting that task. This
2204 // is typically the situation of launching an activity
2205 // from a notification or shortcut. We want to place
2206 // the new activity on top of the current task.
2207 addingToTask = true;
2208 sourceRecord = intentActivity;
2209 } else if (!intentActivity.task.rootWasReset) {
2210 // In this case we are launching in to an existing task
2211 // that has not yet been started from its front door.
2212 // The current task has been brought to the front.
2213 // Ideally, we'd probably like to place this new task
2214 // at the bottom of its stack, but that's a little hard
2215 // to do with the current organization of the code so
2216 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002217 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002218 }
2219 if (!addingToTask && reuseTask == null) {
2220 // We didn't do anything... but it was needed (a.k.a., client
2221 // don't use that intent!) And for paranoia, make
2222 // sure we have correctly resumed the top activity.
2223 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002224 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002225 if (!movedToFront) {
2226 // Make sure to notify Keyguard as well if we are not running an app
2227 // transition later.
2228 notifyActivityDrawnForKeyguard();
2229 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002230 } else {
2231 ActivityOptions.abort(options);
2232 }
2233 return ActivityManager.START_TASK_TO_FRONT;
2234 }
2235 }
2236 }
2237 }
2238
2239 //String uri = r.intent.toURI();
2240 //Intent intent2 = new Intent(uri);
2241 //Slog.i(TAG, "Given intent: " + r.intent);
2242 //Slog.i(TAG, "URI is: " + uri);
2243 //Slog.i(TAG, "To intent: " + intent2);
2244
2245 if (r.packageName != null) {
2246 // If the activity being launched is the same as the one currently
2247 // at the top, then we need to check if it should only be launched
2248 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002249 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002250 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002251 if (top != null && r.resultTo == null) {
2252 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2253 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002254 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002255 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002256 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2257 top.task);
2258 // For paranoia, make sure we have correctly
2259 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002260 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002261 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002262 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002263 }
2264 ActivityOptions.abort(options);
2265 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2266 // We don't need to start a new activity, and
2267 // the client said not to do anything if that
2268 // is the case, so this is it!
2269 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2270 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002271 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002272 return ActivityManager.START_DELIVERED_TO_TOP;
2273 }
2274 }
2275 }
2276 }
2277
2278 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002279 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002280 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2281 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002282 }
2283 ActivityOptions.abort(options);
2284 return ActivityManager.START_CLASS_NOT_FOUND;
2285 }
2286
2287 boolean newTask = false;
2288 boolean keepCurTransition = false;
2289
Craig Mautnerbb742462014-07-07 15:28:55 -07002290 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002291 sourceRecord.task : null;
2292
Craig Mautner8849a5e2013-04-02 16:41:03 -07002293 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002294 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002295 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002296 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002297 targetStack = computeStackFocus(r, newTask);
2298 targetStack.moveToFront("startingNewTask");
2299
Craig Mautner8849a5e2013-04-02 16:41:03 -07002300 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002301 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2302 newTaskInfo != null ? newTaskInfo : r.info,
2303 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002304 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2305 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002306 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2307 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002308 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002309 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002310 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002311 if (isLockTaskModeViolation(r.task)) {
2312 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2313 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2314 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002315 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002316 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002317 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2318 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002319 // Caller wants to appear on home activity, so before starting
2320 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002321 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002322 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002323 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002324 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002325 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002326 if (isLockTaskModeViolation(sourceTask)) {
2327 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2328 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2329 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002330 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002331 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002332 final TaskRecord topTask = targetStack.topTask();
2333 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002334 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002335 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002336 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002337 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002338 // In this case, we are adding the activity to an existing
2339 // task, but the caller has asked to clear that task if the
2340 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002341 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002342 keepCurTransition = true;
2343 if (top != null) {
2344 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002345 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002346 // For paranoia, make sure we have correctly
2347 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002348 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002349 if (doResume) {
2350 targetStack.resumeTopActivityLocked(null);
2351 }
2352 ActivityOptions.abort(options);
2353 return ActivityManager.START_DELIVERED_TO_TOP;
2354 }
2355 } else if (!addingToTask &&
2356 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2357 // In this case, we are launching an activity in our own task
2358 // that may already be running somewhere in the history, and
2359 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002360 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002361 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002362 final TaskRecord task = top.task;
2363 task.moveActivityToFrontLocked(top);
2364 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002365 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002366 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002367 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002368 if (doResume) {
2369 targetStack.resumeTopActivityLocked(null);
2370 }
2371 return ActivityManager.START_DELIVERED_TO_TOP;
2372 }
2373 }
2374 // An existing activity is starting this new activity, so we want
2375 // to keep the new one in the same task as the one that is starting
2376 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002377 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002378 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002379 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002380
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002381 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002382 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002383 // task it has provided to us.
2384 if (isLockTaskModeViolation(inTask)) {
2385 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2386 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2387 }
2388 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002389 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2390 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002391
2392 // Check whether we should actually launch the new activity in to the task,
2393 // or just reuse the current activity on top.
2394 ActivityRecord top = inTask.getTopActivity();
2395 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2396 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2397 || launchSingleTop || launchSingleTask) {
2398 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2399 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2400 // We don't need to start a new activity, and
2401 // the client said not to do anything if that
2402 // is the case, so this is it!
2403 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2404 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002405 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002406 return ActivityManager.START_DELIVERED_TO_TOP;
2407 }
2408 }
2409
Dianne Hackborn962d5352014-08-29 16:27:40 -07002410 if (!addingToTask) {
2411 // We don't actually want to have this activity added to the task, so just
2412 // stop here but still tell the caller that we consumed the intent.
2413 ActivityOptions.abort(options);
2414 return ActivityManager.START_TASK_TO_FRONT;
2415 }
2416
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002417 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002418 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002419 + " in explicit task " + r.task);
2420
Craig Mautner8849a5e2013-04-02 16:41:03 -07002421 } else {
2422 // This not being started from an existing activity, and not part
2423 // of a new task... just put it in the top task, though these days
2424 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002425 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002426 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002427 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002428 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002429 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002430 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002431 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002432 + " in new guessed " + r.task);
2433 }
2434
2435 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002436 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002437
Craig Mautner76e2a762014-06-24 09:05:43 -07002438 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2439 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2440 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002441 if (newTask) {
2442 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2443 }
2444 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002445 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002446 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002447 if (!launchTaskBehind) {
2448 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002449 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002450 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002451 return ActivityManager.START_SUCCESS;
2452 }
2453
Craig Mautneree36c772014-07-16 14:56:05 -07002454 final void doPendingActivityLaunchesLocked(boolean doResume) {
2455 while (!mPendingActivityLaunches.isEmpty()) {
2456 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002457 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002458 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002459 }
2460 }
2461
Craig Mautner7f13ed32014-07-28 14:00:35 -07002462 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002463 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2464 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002465 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002466 mPendingActivityLaunches.remove(palNdx);
2467 }
2468 }
2469 }
2470
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002471 void setLaunchSource(int uid) {
2472 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2473 }
2474
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002475 void acquireLaunchWakelock() {
2476 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2477 throw new IllegalStateException("Calling must be system uid");
2478 }
2479 mLaunchingActivity.acquire();
2480 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2481 // To be safe, don't allow the wake lock to be held for too long.
2482 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2483 }
2484 }
2485
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002486 /**
2487 * Called when the frontmost task is idle.
2488 * @return the state of mService.mBooting before this was called.
2489 */
2490 private boolean checkFinishBootingLocked() {
2491 final boolean booting = mService.mBooting;
2492 boolean enableScreen = false;
2493 mService.mBooting = false;
2494 if (!mService.mBooted) {
2495 mService.mBooted = true;
2496 enableScreen = true;
2497 }
2498 if (booting || enableScreen) {
2499 mService.postFinishBooting(booting, enableScreen);
2500 }
2501 return booting;
2502 }
2503
Craig Mautnerf3333272013-04-22 10:55:53 -07002504 // Checked.
2505 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2506 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002507 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002508
Craig Mautnerf3333272013-04-22 10:55:53 -07002509 ArrayList<ActivityRecord> stops = null;
2510 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002511 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002512 int NS = 0;
2513 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002514 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002515 boolean activityRemoved = false;
2516
Wale Ogunwale7d701172015-03-11 15:36:30 -07002517 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002518 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002519 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2520 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002521 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2522 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002523 if (fromTimeout) {
2524 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002525 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002526
2527 // This is a hack to semi-deal with a race condition
2528 // in the client where it can be constructed with a
2529 // newer configuration from when we asked it to launch.
2530 // We'll update with whatever configuration it now says
2531 // it used to launch.
2532 if (config != null) {
2533 r.configuration = config;
2534 }
2535
2536 // We are now idle. If someone is waiting for a thumbnail from
2537 // us, we can now deliver.
2538 r.idle = true;
2539
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002540 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002541 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002542 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002543 }
2544 }
2545
2546 if (allResumedActivitiesIdle()) {
2547 if (r != null) {
2548 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002549 }
2550
2551 if (mLaunchingActivity.isHeld()) {
2552 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2553 if (VALIDATE_WAKE_LOCK_CALLER &&
2554 Binder.getCallingUid() != Process.myUid()) {
2555 throw new IllegalStateException("Calling must be system uid");
2556 }
2557 mLaunchingActivity.release();
2558 }
2559 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002560 }
2561
2562 // Atomically retrieve all of the other things to do.
2563 stops = processStoppingActivitiesLocked(true);
2564 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002565 if ((NF = mFinishingActivities.size()) > 0) {
2566 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002567 mFinishingActivities.clear();
2568 }
2569
Craig Mautnerf3333272013-04-22 10:55:53 -07002570 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002571 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002572 mStartingUsers.clear();
2573 }
2574
Craig Mautnerf3333272013-04-22 10:55:53 -07002575 // Stop any activities that are scheduled to do so but have been
2576 // waiting for the next one to start.
2577 for (int i = 0; i < NS; i++) {
2578 r = stops.get(i);
2579 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002580 if (stack != null) {
2581 if (r.finishing) {
2582 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2583 } else {
2584 stack.stopActivityLocked(r);
2585 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002586 }
2587 }
2588
2589 // Finish 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 < NF; i++) {
2592 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002593 final ActivityStack stack = r.task.stack;
2594 if (stack != null) {
2595 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2596 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002597 }
2598
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002599 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002600 // Complete user switch
2601 if (startingUsers != null) {
2602 for (int i = 0; i < startingUsers.size(); i++) {
2603 mService.finishUserSwitch(startingUsers.get(i));
2604 }
2605 }
2606 // Complete starting up of background users
2607 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002608 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002609 mStartingBackgroundUsers.clear();
2610 for (int i = 0; i < startingUsers.size(); i++) {
2611 mService.finishUserBoot(startingUsers.get(i));
2612 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002613 }
2614 }
2615
2616 mService.trimApplications();
2617 //dump();
2618 //mWindowManager.dump();
2619
Craig Mautnerf3333272013-04-22 10:55:53 -07002620 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002621 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002622 }
2623
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002624 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002625 }
2626
Craig Mautner8e569572013-10-11 17:36:59 -07002627 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002628 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002629 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2630 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002631 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2632 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2633 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002634 }
Craig Mautner19091252013-10-05 00:03:53 -07002635 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002636 }
2637
2638 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002639 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2640 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002641 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2642 stacks.get(stackNdx).closeSystemDialogsLocked();
2643 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002644 }
2645 }
2646
Craig Mautner93529a42013-10-04 15:03:13 -07002647 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002648 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002649 }
2650
Craig Mautner8d341ef2013-03-26 09:03:27 -07002651 /**
2652 * @return true if some activity was finished (or would have finished if doit were true).
2653 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002654 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2655 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002656 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002657 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2658 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002659 final int numStacks = stacks.size();
2660 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2661 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002662 if (stack.finishDisabledPackageActivitiesLocked(
2663 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002664 didSomething = true;
2665 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002666 }
2667 }
2668 return didSomething;
2669 }
2670
Dianne Hackborna413dc02013-07-12 12:02:55 -07002671 void updatePreviousProcessLocked(ActivityRecord r) {
2672 // Now that this process has stopped, we may want to consider
2673 // it to be the previous app to try to keep around in case
2674 // the user wants to return to it.
2675
2676 // First, found out what is currently the foreground app, so that
2677 // we don't blow away the previous app if this activity is being
2678 // hosted by the process that is actually still the foreground.
2679 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002680 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2681 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002682 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2683 final ActivityStack stack = stacks.get(stackNdx);
2684 if (isFrontStack(stack)) {
2685 if (stack.mResumedActivity != null) {
2686 fgApp = stack.mResumedActivity.app;
2687 } else if (stack.mPausingActivity != null) {
2688 fgApp = stack.mPausingActivity.app;
2689 }
2690 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002691 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002692 }
2693 }
2694
2695 // Now set this one as the previous process, only if that really
2696 // makes sense to.
2697 if (r.app != null && fgApp != null && r.app != fgApp
2698 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002699 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002700 mService.mPreviousProcess = r.app;
2701 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2702 }
2703 }
2704
Craig Mautner05d29032013-05-03 13:40:13 -07002705 boolean resumeTopActivitiesLocked() {
2706 return resumeTopActivitiesLocked(null, null, null);
2707 }
2708
2709 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2710 Bundle targetOptions) {
2711 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002712 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002713 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002714 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002715 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002716 if (isFrontStack(targetStack)) {
2717 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2718 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002719
Craig Mautnere0a38842013-12-16 16:14:02 -08002720 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2721 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002722 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2723 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002724 if (stack == targetStack) {
2725 // Already started above.
2726 continue;
2727 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002728 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002729 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002730 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002731 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002732 }
Craig Mautner05d29032013-05-03 13:40:13 -07002733 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002734 }
2735
Todd Kennedy539db512014-12-15 09:57:55 -08002736 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002737 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2738 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002739 final int numStacks = stacks.size();
2740 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2741 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002742 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002743 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002744 }
2745 }
2746
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002747 void finishVoiceTask(IVoiceInteractionSession session) {
2748 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2749 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2750 final int numStacks = stacks.size();
2751 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2752 final ActivityStack stack = stacks.get(stackNdx);
2753 stack.finishVoiceTask(session);
2754 }
2755 }
2756 }
2757
Craig Mautner299f9602015-01-26 09:47:33 -08002758 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002759 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2760 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002761 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002762 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2763 // Caller wants the home activity moved with it. To accomplish this,
2764 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002765 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002766 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002767 if (task.stack == null) {
2768 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2769 + task + " to front. Stack is null");
2770 return;
2771 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002772 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2773 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2774 reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002775 if (DEBUG_STACK) Slog.d(TAG_STACK,
2776 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002777 }
2778
Craig Mautner967212c2013-04-13 21:10:58 -07002779 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002780 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2781 if (activityContainer != null) {
2782 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002783 }
2784 return null;
2785 }
2786
Craig Mautner967212c2013-04-13 21:10:58 -07002787 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002788 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002789 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2790 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002791 }
2792 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002793 }
2794
Craig Mautner4a1cb222013-12-04 16:14:06 -08002795 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002796 ActivityRecord homeActivity = getHomeActivity();
2797 if (homeActivity != null) {
2798 return homeActivity.appToken;
2799 }
2800 return null;
2801 }
2802
2803 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002804 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002805 }
2806
2807 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002808 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2809 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2810 final TaskRecord task = tasks.get(taskNdx);
2811 if (task.isHomeTask()) {
2812 final ArrayList<ActivityRecord> activities = task.mActivities;
2813 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2814 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002815 if (r.isHomeActivity()
2816 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002817 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002818 }
2819 }
2820 }
2821 }
2822 return null;
2823 }
2824
Todd Kennedyca4d8422015-01-15 15:19:22 -08002825 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002826 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002827 ActivityContainer activityContainer =
2828 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002829 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002830 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2831 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002832 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002833 return activityContainer;
2834 }
2835
Craig Mautner34b73df2014-01-12 21:11:08 -08002836 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002837 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2838 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2839 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002840 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2841 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002842 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002843 }
2844 }
2845
Craig Mautner95da1082014-02-24 17:54:35 -08002846 void deleteActivityContainer(IActivityContainer container) {
2847 ActivityContainer activityContainer = (ActivityContainer)container;
2848 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002849 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2850 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002851 final int stackId = activityContainer.mStackId;
2852 mActivityContainers.remove(stackId);
2853 mWindowManager.removeStack(stackId);
2854 }
2855 }
2856
Wale Ogunwale60454db2015-01-23 16:05:07 -08002857 void resizeStackLocked(int stackId, Rect bounds) {
2858 final ActivityStack stack = getStack(stackId);
2859 if (stack == null) {
2860 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2861 return;
2862 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002863
2864 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002865 if (r != null && !r.task.mResizeable) {
2866 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002867 return;
2868 }
2869
Wale Ogunwale60454db2015-01-23 16:05:07 -08002870 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2871 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002872 if (r != null) {
2873 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2874 // And we need to make sure at this point that all other activities
2875 // are made visible with the correct configuration.
2876 ensureActivitiesVisibleLocked(r, 0);
2877 if (!updated) {
2878 resumeTopActivitiesLocked(stack, null, null);
2879 }
2880 }
2881 }
2882 }
2883
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002884 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2885 * current task stack if it only has one entry, moving the task to a stack that matches the
2886 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2887 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2888 task.mResizeable = true;
2889 final ActivityStack currentStack = task.stack;
2890 if (currentStack.isHomeStack()) {
2891 // Can't move task off the home stack. Sorry!
2892 return;
2893 }
2894
2895 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2896 if (matchingStackId != -1) {
2897 // There is already a stack with the right bounds!
2898 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2899 // Nothing to do here. Already in the right stack...
2900 return;
2901 }
2902 // Move task to stack with matching bounds.
2903 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2904 return;
2905 }
2906
2907 if (currentStack != null && currentStack.numTasks() == 1) {
2908 // Just resize the current stack since this is the task in it.
2909 resizeStackLocked(currentStack.mStackId, bounds);
2910 return;
2911 }
2912
2913 // Create new stack and move the task to it.
2914 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2915 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2916 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2917
2918 if (newStack == null) {
2919 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2920 return;
2921 }
2922 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2923 resizeStackLocked(newStack.mStackId, bounds);
2924 }
2925
Todd Kennedy4900bf92015-01-16 16:05:14 -08002926 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002927 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2928 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002929 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002930 }
2931
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002932 ActivityContainer activityContainer = new ActivityContainer(stackId);
2933 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002934 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002935 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002936 }
2937
2938 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002939 while (true) {
2940 if (++mLastStackId <= HOME_STACK_ID) {
2941 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002942 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002943 if (getStack(mLastStackId) == null) {
2944 break;
2945 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002946 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002947 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002948 }
2949
Wale Ogunwale7de05352014-12-12 15:21:33 -08002950 private boolean restoreRecentTaskLocked(TaskRecord task) {
2951 ActivityStack stack = null;
2952 // Determine stack to restore task to.
2953 if (mLeanbackOnlyDevice) {
2954 // There is only one stack for lean back devices.
2955 stack = mHomeStack;
2956 } else {
2957 // Look for the top stack on the home display that isn't the home stack.
2958 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2959 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2960 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07002961 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002962 stack = tmpStack;
2963 break;
2964 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002965 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002966 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002967
2968 if (stack == null) {
2969 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2970 // before an application stack is created...Go ahead and create one on the default
2971 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002972 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002973 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002974 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002975 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2976 "Created stack=" + stack + " for recents restoration.");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002977 }
2978
2979 if (stack == null) {
2980 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002981 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2982 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002983 return false;
2984 }
2985
2986 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002987 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2988 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002989 final ArrayList<ActivityRecord> activities = task.mActivities;
2990 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2991 final ActivityRecord r = activities.get(activityNdx);
2992 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2993 r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002994 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0,
Wale Ogunwale7de05352014-12-12 15:21:33 -08002995 r.userId, r.info.configChanges, task.voiceSession != null,
2996 r.mLaunchTaskBehind);
2997 }
2998 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002999 }
3000
Craig Mautner299f9602015-01-26 09:47:33 -08003001 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003002 final TaskRecord task = anyTaskForIdLocked(taskId);
3003 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003004 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003005 return;
3006 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003007 final ActivityStack stack = getStack(stackId);
3008 if (stack == null) {
3009 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
3010 return;
3011 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003012 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
3013 if (task.stack != null) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07003014 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003015 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003016 stack.addTask(task, toTop, true);
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003017 // The task might have already been running and its visibility needs to be synchronized with
3018 // the visibility of the stack / windows.
3019 stack.ensureActivitiesVisibleLocked(null, 0);
Craig Mautner05d29032013-05-03 13:40:13 -07003020 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003021 }
3022
Craig Mautnerac6f8432013-07-17 13:24:59 -07003023 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003024 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003025 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3026 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003027 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3028 final ActivityStack stack = stacks.get(stackNdx);
3029 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003030 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003031 continue;
3032 }
3033 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003034 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3035 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003036 continue;
3037 }
3038 final ActivityRecord ar = stack.findTaskLocked(r);
3039 if (ar != null) {
3040 return ar;
3041 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003042 }
3043 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003044 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003045 return null;
3046 }
3047
3048 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003049 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3050 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003051 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3052 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3053 if (ar != null) {
3054 return ar;
3055 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003056 }
3057 }
3058 return null;
3059 }
3060
Craig Mautner8d341ef2013-03-26 09:03:27 -07003061 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003062 scheduleSleepTimeout();
3063 if (!mGoingToSleep.isHeld()) {
3064 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003065 if (mLaunchingActivity.isHeld()) {
3066 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3067 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003068 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003069 mLaunchingActivity.release();
3070 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003071 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003072 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003073 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003074 }
3075
3076 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003077 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003078
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003079 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003080 final long endTime = System.currentTimeMillis() + timeout;
3081 while (true) {
3082 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003083 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3084 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003085 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3086 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3087 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003088 }
3089 if (cantShutdown) {
3090 long timeRemaining = endTime - System.currentTimeMillis();
3091 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003092 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003093 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003094 } catch (InterruptedException e) {
3095 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003096 } else {
3097 Slog.w(TAG, "Activity manager shutdown timed out");
3098 timedout = true;
3099 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003100 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003101 } else {
3102 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003103 }
3104 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003105
3106 // Force checkReadyForSleep to complete.
3107 mSleepTimeout = true;
3108 checkReadyForSleepLocked();
3109
Craig Mautner8d341ef2013-03-26 09:03:27 -07003110 return timedout;
3111 }
3112
3113 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003114 removeSleepTimeouts();
3115 if (mGoingToSleep.isHeld()) {
3116 mGoingToSleep.release();
3117 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003118 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3119 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003120 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3121 final ActivityStack stack = stacks.get(stackNdx);
3122 stack.awakeFromSleepingLocked();
3123 if (isFrontStack(stack)) {
3124 resumeTopActivitiesLocked();
3125 }
Craig Mautner5314a402013-09-26 12:40:16 -07003126 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003127 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003128 mGoingToSleepActivities.clear();
3129 }
3130
3131 void activitySleptLocked(ActivityRecord r) {
3132 mGoingToSleepActivities.remove(r);
3133 checkReadyForSleepLocked();
3134 }
3135
3136 void checkReadyForSleepLocked() {
3137 if (!mService.isSleepingOrShuttingDown()) {
3138 // Do not care.
3139 return;
3140 }
3141
3142 if (!mSleepTimeout) {
3143 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003144 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3145 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003146 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3147 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3148 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003149 }
3150
3151 if (mStoppingActivities.size() > 0) {
3152 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003153 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003154 + mStoppingActivities.size() + " activities");
3155 scheduleIdleLocked();
3156 dontSleep = true;
3157 }
3158
3159 if (mGoingToSleepActivities.size() > 0) {
3160 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003161 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003162 + mGoingToSleepActivities.size() + " activities");
3163 dontSleep = true;
3164 }
3165
3166 if (dontSleep) {
3167 return;
3168 }
3169 }
3170
Craig Mautnere0a38842013-12-16 16:14:02 -08003171 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3172 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003173 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3174 stacks.get(stackNdx).goToSleep();
3175 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003176 }
3177
3178 removeSleepTimeouts();
3179
3180 if (mGoingToSleep.isHeld()) {
3181 mGoingToSleep.release();
3182 }
3183 if (mService.mShuttingDown) {
3184 mService.notifyAll();
3185 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003186 }
3187
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003188 boolean reportResumedActivityLocked(ActivityRecord r) {
3189 final ActivityStack stack = r.task.stack;
3190 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003191 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003192 }
3193 if (allResumedActivitiesComplete()) {
3194 ensureActivitiesVisibleLocked(null, 0);
3195 mWindowManager.executeAppTransition();
3196 return true;
3197 }
3198 return false;
3199 }
3200
Craig Mautner8d341ef2013-03-26 09:03:27 -07003201 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003202 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3203 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003204 final int numStacks = stacks.size();
3205 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3206 final ActivityStack stack = stacks.get(stackNdx);
3207 stack.handleAppCrashLocked(app);
3208 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003209 }
3210 }
3211
Jose Lima4b6c6692014-08-12 17:41:12 -07003212 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003213 final ActivityStack stack = r.task.stack;
3214 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003215 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3216 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003217 return false;
3218 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003219 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003220 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3221 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003222
3223 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003224 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003225 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003226 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003227 return true;
3228 }
3229
3230 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003231 if (visible && top.fullscreen) {
3232 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003233 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3234 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3235 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3236 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003237 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003238 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3239 // Only the activity set as currently visible behind should actively reset its
3240 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003241 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3242 "requestVisibleBehind: returning visible=" + visible
3243 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3244 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003245 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003246 }
3247
Jose Lima4b6c6692014-08-12 17:41:12 -07003248 stack.setVisibleBehindActivity(visible ? r : null);
3249 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003250 // Make the activity immediately above r opaque.
3251 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3252 if (next != null) {
3253 mService.convertFromTranslucent(next.appToken);
3254 }
3255 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003256 if (top.app != null && top.app.thread != null) {
3257 // Notify the top app of the change.
3258 try {
3259 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3260 } catch (RemoteException e) {
3261 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003262 }
3263 return true;
3264 }
3265
Craig Mautnerbb742462014-07-07 15:28:55 -07003266 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3267 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3268 r.mLaunchTaskBehind = false;
3269 final TaskRecord task = r.task;
3270 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003271 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003272 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003273 mWindowManager.setAppVisibility(r.appToken, false);
3274 }
3275
3276 void scheduleLaunchTaskBehindComplete(IBinder token) {
3277 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3278 }
3279
Craig Mautnerde4ef022013-04-07 19:01:33 -07003280 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003281 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003282 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3283 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003284 final int topStackNdx = stacks.size() - 1;
3285 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3286 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003287 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003288 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003289 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003290 }
3291
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003292 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3293 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3294 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3295 final int topStackNdx = stacks.size() - 1;
3296 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3297 final ActivityStack stack = stacks.get(stackNdx);
3298 stack.clearOtherAppTimeTrackers(except);
3299 }
3300 }
3301 }
3302
Craig Mautner8d341ef2013-03-26 09:03:27 -07003303 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003304 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3305 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003306 final int numStacks = stacks.size();
3307 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3308 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003309 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003310 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003311 }
3312 }
3313
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003314 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3315 // Examine all activities currently running in the process.
3316 TaskRecord firstTask = null;
3317 // Tasks is non-null only if two or more tasks are found.
3318 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003319 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3320 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003321 ActivityRecord r = app.activities.get(i);
3322 // First, if we find an activity that is in the process of being destroyed,
3323 // then we just aren't going to do anything for now; we want things to settle
3324 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003325 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003326 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003327 return;
3328 }
3329 // Don't consider any activies that are currently not in a state where they
3330 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003331 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3332 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003333 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003334 continue;
3335 }
3336 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003337 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003338 + " from " + r);
3339 if (firstTask == null) {
3340 firstTask = r.task;
3341 } else if (firstTask != r.task) {
3342 if (tasks == null) {
3343 tasks = new ArraySet<>();
3344 tasks.add(firstTask);
3345 }
3346 tasks.add(r.task);
3347 }
3348 }
3349 }
3350 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003351 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003352 return;
3353 }
3354 // If we have activities in multiple tasks that are in a position to be destroyed,
3355 // let's iterate through the tasks and release the oldest one.
3356 final int numDisplays = mActivityDisplays.size();
3357 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3358 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3359 // Step through all stacks starting from behind, to hit the oldest things first.
3360 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3361 final ActivityStack stack = stacks.get(stackNdx);
3362 // Try to release activities in this stack; if we manage to, we are done.
3363 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3364 return;
3365 }
3366 }
3367 }
3368 }
3369
Amith Yamasani37a40c22015-06-17 13:25:42 -07003370 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003371 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003372 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003373 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003374
Craig Mautner858d8a62013-04-23 17:08:34 -07003375 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003376 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3377 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003378 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003379 final ActivityStack stack = stacks.get(stackNdx);
3380 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003381 TaskRecord task = stack.topTask();
3382 if (task != null) {
3383 mWindowManager.moveTaskToTop(task.taskId);
3384 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003385 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003386 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003387
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003388 ActivityStack stack = getStack(restoreStackId);
3389 if (stack == null) {
3390 stack = mHomeStack;
3391 }
3392 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003393 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003394 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003395 TaskRecord task = stack.topTask();
3396 if (task != null) {
3397 mWindowManager.moveTaskToTop(task.taskId);
3398 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003399 } else {
3400 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003401 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003402 }
Craig Mautner93529a42013-10-04 15:03:13 -07003403 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003404 }
3405
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003406 /**
3407 * Add background users to send boot completed events to.
3408 * @param userId The user being started in the background
3409 * @param uss The state object for the user.
3410 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003411 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003412 mStartingBackgroundUsers.add(uss);
3413 }
3414
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003415 /** Checks whether the userid is a profile of the current user. */
3416 boolean isCurrentProfileLocked(int userId) {
3417 if (userId == mCurrentUser) return true;
3418 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3419 if (mService.mCurrentProfileIds[i] == userId) return true;
3420 }
3421 return false;
3422 }
3423
Craig Mautnerde4ef022013-04-07 19:01:33 -07003424 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003425 ArrayList<ActivityRecord> stops = null;
3426
3427 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003428 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3429 ActivityRecord s = mStoppingActivities.get(activityNdx);
3430 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003431 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003432 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3433 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003434 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003435 if (s.finishing) {
3436 // If this activity is finishing, it is sitting on top of
3437 // everyone else but we now know it is no longer needed...
3438 // so get rid of it. Otherwise, we need to go through the
3439 // normal flow and hide it once we determine that it is
3440 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003441 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003442 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003443 }
3444 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003445 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003446 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003447 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003448 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003449 }
3450 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003451 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003452 }
3453 }
3454
3455 return stops;
3456 }
3457
Craig Mautnercf910b02013-04-23 11:23:27 -07003458 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003459 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3460 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3461 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3462 final ActivityStack stack = stacks.get(stackNdx);
3463 final ActivityRecord r = stack.topRunningActivityLocked(null);
3464 final ActivityState state = r == null ? DESTROYED : r.state;
3465 if (isFrontStack(stack)) {
3466 if (r == null) Slog.e(TAG,
3467 "validateTop...: null top activity, stack=" + stack);
3468 else {
3469 final ActivityRecord pausing = stack.mPausingActivity;
3470 if (pausing != null && pausing == r) Slog.e(TAG,
3471 "validateTop...: top stack has pausing activity r=" + r
3472 + " state=" + state);
3473 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3474 "validateTop...: activity in front not resumed r=" + r
3475 + " state=" + state);
3476 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003477 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003478 final ActivityRecord resumed = stack.mResumedActivity;
3479 if (resumed != null && resumed == r) Slog.e(TAG,
3480 "validateTop...: back stack has resumed activity r=" + r
3481 + " state=" + state);
3482 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3483 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003484 }
3485 }
3486 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003487 }
3488
Craig Mautnere0570202015-05-13 13:06:11 -07003489 private String lockTaskModeToString() {
3490 switch (mLockTaskModeState) {
3491 case LOCK_TASK_MODE_LOCKED:
3492 return "LOCKED";
3493 case LOCK_TASK_MODE_PINNED:
3494 return "PINNED";
3495 case LOCK_TASK_MODE_NONE:
3496 return "NONE";
3497 default: return "unknown=" + mLockTaskModeState;
3498 }
3499 }
3500
Craig Mautner27084302013-03-25 08:05:25 -07003501 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003502 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003503 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003504 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3505 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3506 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003507 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003508 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3509 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3510 if (packages.size() > 0) {
3511 pw.println(" mLockTaskPackages (userId:packages)=");
3512 for (int i = 0; i < packages.size(); ++i) {
3513 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3514 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3515 }
3516 }
3517 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003518 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003519
Craig Mautner20e72272013-04-01 13:45:53 -07003520 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003521 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003522 }
3523
Dianne Hackborn390517b2013-05-30 15:03:32 -07003524 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3525 boolean needSep, String prefix) {
3526 if (activity != null) {
3527 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3528 if (needSep) {
3529 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003530 }
3531 pw.print(prefix);
3532 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003533 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003534 }
3535 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003536 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003537 }
3538
Craig Mautner8d341ef2013-03-26 09:03:27 -07003539 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3540 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003541 boolean printed = false;
3542 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003543 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3544 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003545 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003546 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003547 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003548 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003549 final ActivityStack stack = stacks.get(stackNdx);
3550 StringBuilder stackHeader = new StringBuilder(128);
3551 stackHeader.append(" Stack #");
3552 stackHeader.append(stack.mStackId);
3553 stackHeader.append(":");
3554 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3555 needSep, stackHeader.toString());
3556 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3557 !dumpAll, false, dumpPackage, true,
3558 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003559
Craig Mautner4a1cb222013-12-04 16:14:06 -08003560 needSep = printed;
3561 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3562 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003563 if (pr) {
3564 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003565 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003566 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003567 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3568 " mResumedActivity: ");
3569 if (pr) {
3570 printed = true;
3571 needSep = false;
3572 }
3573 if (dumpAll) {
3574 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3575 " mLastPausedActivity: ");
3576 if (pr) {
3577 printed = true;
3578 needSep = true;
3579 }
3580 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3581 needSep, " mLastNoHistoryActivity: ");
3582 }
3583 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003584 }
3585 }
3586
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003587 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3588 false, dumpPackage, true, " Activities waiting to finish:", null);
3589 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3590 false, dumpPackage, true, " Activities waiting to stop:", null);
3591 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3592 false, dumpPackage, true, " Activities waiting for another to become visible:",
3593 null);
3594 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3595 false, dumpPackage, true, " Activities waiting to sleep:", null);
3596 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3597 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003598
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003599 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003600 }
3601
Dianne Hackborn390517b2013-05-30 15:03:32 -07003602 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003603 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003604 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003605 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003606 String innerPrefix = null;
3607 String[] args = null;
3608 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003609 for (int i=list.size()-1; i>=0; i--) {
3610 final ActivityRecord r = list.get(i);
3611 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3612 continue;
3613 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003614 if (innerPrefix == null) {
3615 innerPrefix = prefix + " ";
3616 args = new String[0];
3617 }
3618 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003619 final boolean full = !brief && (complete || !r.isInHistory());
3620 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003621 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003622 needNL = false;
3623 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003624 if (header1 != null) {
3625 pw.println(header1);
3626 header1 = null;
3627 }
3628 if (header2 != null) {
3629 pw.println(header2);
3630 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003631 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003632 if (lastTask != r.task) {
3633 lastTask = r.task;
3634 pw.print(prefix);
3635 pw.print(full ? "* " : " ");
3636 pw.println(lastTask);
3637 if (full) {
3638 lastTask.dump(pw, prefix + " ");
3639 } else if (complete) {
3640 // Complete + brief == give a summary. Isn't that obvious?!?
3641 if (lastTask.intent != null) {
3642 pw.print(prefix); pw.print(" ");
3643 pw.println(lastTask.intent.toInsecureStringWithClip());
3644 }
3645 }
3646 }
3647 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3648 pw.print(" #"); pw.print(i); pw.print(": ");
3649 pw.println(r);
3650 if (full) {
3651 r.dump(pw, innerPrefix);
3652 } else if (complete) {
3653 // Complete + brief == give a summary. Isn't that obvious?!?
3654 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3655 if (r.app != null) {
3656 pw.print(innerPrefix); pw.println(r.app);
3657 }
3658 }
3659 if (client && r.app != null && r.app.thread != null) {
3660 // flush anything that is already in the PrintWriter since the thread is going
3661 // to write to the file descriptor directly
3662 pw.flush();
3663 try {
3664 TransferPipe tp = new TransferPipe();
3665 try {
3666 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3667 r.appToken, innerPrefix, args);
3668 // Short timeout, since blocking here can
3669 // deadlock with the application.
3670 tp.go(fd, 2000);
3671 } finally {
3672 tp.kill();
3673 }
3674 } catch (IOException e) {
3675 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3676 } catch (RemoteException e) {
3677 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3678 }
3679 needNL = true;
3680 }
3681 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003682 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003683 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003684
Craig Mautnerf3333272013-04-22 10:55:53 -07003685 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003686 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3687 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003688 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3689 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003690 }
3691
3692 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003693 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003694 }
3695
3696 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003697 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3698 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003699 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3700 }
3701
Craig Mautner05d29032013-05-03 13:40:13 -07003702 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003703 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3704 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3705 }
Craig Mautner05d29032013-05-03 13:40:13 -07003706 }
3707
Craig Mautner0eea92c2013-05-16 13:35:39 -07003708 void removeSleepTimeouts() {
3709 mSleepTimeout = false;
3710 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3711 }
3712
3713 final void scheduleSleepTimeout() {
3714 removeSleepTimeouts();
3715 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3716 }
3717
Craig Mautner4a1cb222013-12-04 16:14:06 -08003718 @Override
3719 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003720 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003721 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3722 }
3723
3724 @Override
3725 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003726 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003727 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3728 }
3729
3730 @Override
3731 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003732 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003733 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3734 }
3735
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003736 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003737 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003738 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003739 newDisplay = mActivityDisplays.get(displayId) == null;
3740 if (newDisplay) {
3741 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003742 if (activityDisplay.mDisplay == null) {
3743 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3744 return;
3745 }
Craig Mautner4504de52013-12-20 09:06:56 -08003746 mActivityDisplays.put(displayId, activityDisplay);
3747 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003748 }
Craig Mautner4504de52013-12-20 09:06:56 -08003749 if (newDisplay) {
3750 mWindowManager.onDisplayAdded(displayId);
3751 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003752 }
3753
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003754 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003755 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003756 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3757 if (activityDisplay != null) {
3758 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003759 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003760 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003761 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003762 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003763 }
3764 }
3765 mWindowManager.onDisplayRemoved(displayId);
3766 }
3767
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003768 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003769 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003770 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3771 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003772 // TODO: Update the bounds.
3773 }
3774 }
3775 mWindowManager.onDisplayChanged(displayId);
3776 }
3777
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003778 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003779 StackInfo info = new StackInfo();
3780 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3781 info.displayId = Display.DEFAULT_DISPLAY;
3782 info.stackId = stack.mStackId;
3783
3784 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3785 final int numTasks = tasks.size();
3786 int[] taskIds = new int[numTasks];
3787 String[] taskNames = new String[numTasks];
3788 for (int i = 0; i < numTasks; ++i) {
3789 final TaskRecord task = tasks.get(i);
3790 taskIds[i] = task.taskId;
3791 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3792 : task.realActivity != null ? task.realActivity.flattenToString()
3793 : task.getTopActivity() != null ? task.getTopActivity().packageName
3794 : "unknown";
3795 }
3796 info.taskIds = taskIds;
3797 info.taskNames = taskNames;
3798 return info;
3799 }
3800
3801 StackInfo getStackInfoLocked(int stackId) {
3802 ActivityStack stack = getStack(stackId);
3803 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003804 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003805 }
3806 return null;
3807 }
3808
3809 ArrayList<StackInfo> getAllStackInfosLocked() {
3810 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003811 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3812 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003813 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003814 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003815 }
3816 }
3817 return list;
3818 }
3819
Craig Mautner15df08a2015-04-01 12:17:18 -07003820 TaskRecord getLockedTaskLocked() {
3821 final int top = mLockTaskModeTasks.size() - 1;
3822 if (top >= 0) {
3823 return mLockTaskModeTasks.get(top);
3824 }
3825 return null;
3826 }
3827
3828 boolean isLockedTask(TaskRecord task) {
3829 return mLockTaskModeTasks.contains(task);
3830 }
3831
3832 boolean isLastLockedTask(TaskRecord task) {
3833 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3834 }
3835
3836 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003837 if (!mLockTaskModeTasks.remove(task)) {
3838 return;
3839 }
3840 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3841 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003842 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003843 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3844 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003845 final Message lockTaskMsg = Message.obtain();
3846 lockTaskMsg.arg1 = task.userId;
3847 lockTaskMsg.what = LOCK_TASK_END_MSG;
3848 mHandler.sendMessage(lockTaskMsg);
3849 }
3850 }
3851
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003852 void showLockTaskToast() {
3853 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003854 }
3855
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003856 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3857 if (mLockTaskModeTasks.contains(task)) {
3858 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3859 }
3860 }
3861
Craig Mautner432f64e2015-05-20 14:59:57 -07003862 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3863 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003864 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003865 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003866 final TaskRecord lockedTask = getLockedTaskLocked();
3867 if (lockedTask != null) {
3868 removeLockedTaskLocked(lockedTask);
3869 if (!mLockTaskModeTasks.isEmpty()) {
3870 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003871 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3872 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003873 lockedTask.performClearTaskLocked();
3874 resumeTopActivitiesLocked();
3875 return;
3876 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003877 }
Craig Mautnere0570202015-05-13 13:06:11 -07003878 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3879 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003880 return;
3881 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003882
3883 // Should have already been checked, but do it again.
3884 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003885 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3886 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003887 return;
3888 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003889 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003890 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003891 return;
3892 }
3893
3894 if (mLockTaskModeTasks.isEmpty()) {
3895 // First locktask.
3896 final Message lockTaskMsg = Message.obtain();
3897 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3898 lockTaskMsg.arg1 = task.userId;
3899 lockTaskMsg.what = LOCK_TASK_START_MSG;
3900 lockTaskMsg.arg2 = lockTaskModeState;
3901 mHandler.sendMessage(lockTaskMsg);
3902 }
3903 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003904 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3905 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003906 mLockTaskModeTasks.remove(task);
3907 mLockTaskModeTasks.add(task);
3908
3909 if (task.mLockTaskUid == -1) {
3910 task.mLockTaskUid = task.mCallingUid;
3911 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003912
3913 if (andResume) {
3914 findTaskToMoveToFrontLocked(task, 0, null, reason);
3915 resumeTopActivitiesLocked();
3916 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003917 }
3918
3919 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003920 return isLockTaskModeViolation(task, false);
3921 }
3922
3923 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3924 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003925 return false;
3926 }
3927 final int lockTaskAuth = task.mLockTaskAuth;
3928 switch (lockTaskAuth) {
3929 case LOCK_TASK_AUTH_DONT_LOCK:
3930 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003931 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003932 case LOCK_TASK_AUTH_LAUNCHABLE:
3933 case LOCK_TASK_AUTH_WHITELISTED:
3934 return false;
3935 case LOCK_TASK_AUTH_PINNABLE:
3936 // Pinnable tasks can't be launched on top of locktask tasks.
3937 return !mLockTaskModeTasks.isEmpty();
3938 default:
3939 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3940 return true;
3941 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003942 }
3943
Craig Mautner15df08a2015-04-01 12:17:18 -07003944 void onLockTaskPackagesUpdatedLocked() {
3945 boolean didSomething = false;
3946 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3947 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003948 final boolean wasWhitelisted =
3949 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3950 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003951 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003952 final boolean isWhitelisted =
3953 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3954 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3955 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003956 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003957 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3958 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003959 removeLockedTaskLocked(lockedTask);
3960 lockedTask.performClearTaskLocked();
3961 didSomething = true;
3962 }
3963 }
3964 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3965 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3966 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3967 final ActivityStack stack = stacks.get(stackNdx);
3968 stack.onLockTaskPackagesUpdatedLocked();
3969 }
3970 }
Craig Mautnere0570202015-05-13 13:06:11 -07003971 final ActivityRecord r = topRunningActivityLocked();
3972 final TaskRecord task = r != null ? r.task : null;
3973 if (mLockTaskModeTasks.isEmpty() && task != null
3974 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3975 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003976 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3977 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3978 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3979 false);
3980 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003981 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003982 if (didSomething) {
3983 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003984 }
3985 }
3986
Benjamin Franz43261142015-02-11 15:59:44 +00003987 int getLockTaskModeState() {
3988 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003989 }
3990
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003991 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003992
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003993 public ActivityStackSupervisorHandler(Looper looper) {
3994 super(looper);
3995 }
3996
Craig Mautnerf3333272013-04-22 10:55:53 -07003997 void activityIdleInternal(ActivityRecord r) {
3998 synchronized (mService) {
3999 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4000 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004001 }
4002
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004003 @Override
4004 public void handleMessage(Message msg) {
4005 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004006 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004007 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4008 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004009 if (mService.mDidDexOpt) {
4010 mService.mDidDexOpt = false;
4011 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4012 nmsg.obj = msg.obj;
4013 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4014 return;
4015 }
4016 // We don't at this point know if the activity is fullscreen,
4017 // so we need to be conservative and assume it isn't.
4018 activityIdleInternal((ActivityRecord)msg.obj);
4019 } break;
4020 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004021 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004022 activityIdleInternal((ActivityRecord)msg.obj);
4023 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004024 case RESUME_TOP_ACTIVITY_MSG: {
4025 synchronized (mService) {
4026 resumeTopActivitiesLocked();
4027 }
4028 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004029 case SLEEP_TIMEOUT_MSG: {
4030 synchronized (mService) {
4031 if (mService.isSleepingOrShuttingDown()) {
4032 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4033 mSleepTimeout = true;
4034 checkReadyForSleepLocked();
4035 }
4036 }
4037 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004038 case LAUNCH_TIMEOUT_MSG: {
4039 if (mService.mDidDexOpt) {
4040 mService.mDidDexOpt = false;
4041 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4042 return;
4043 }
4044 synchronized (mService) {
4045 if (mLaunchingActivity.isHeld()) {
4046 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4047 if (VALIDATE_WAKE_LOCK_CALLER
4048 && Binder.getCallingUid() != Process.myUid()) {
4049 throw new IllegalStateException("Calling must be system uid");
4050 }
4051 mLaunchingActivity.release();
4052 }
4053 }
4054 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004055 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004056 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004057 } break;
4058 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004059 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004060 } break;
4061 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004062 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004063 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004064 case CONTAINER_CALLBACK_VISIBILITY: {
4065 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004066 final IActivityContainerCallback callback = container.mCallback;
4067 if (callback != null) {
4068 try {
4069 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4070 } catch (RemoteException e) {
4071 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004072 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004073 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004074 case LOCK_TASK_START_MSG: {
4075 // When lock task starts, we disable the status bars.
4076 try {
Jason Monk62515be2014-05-21 16:06:19 -04004077 if (mLockTaskNotify == null) {
4078 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004079 }
Jason Monk62515be2014-05-21 16:06:19 -04004080 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004081 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004082 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004083 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004084 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004085 flags = StatusBarManager.DISABLE_MASK
4086 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004087 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004088 flags = StatusBarManager.DISABLE_MASK
4089 & (~StatusBarManager.DISABLE_BACK)
4090 & (~StatusBarManager.DISABLE_HOME)
4091 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004092 }
4093 getStatusBarService().disable(flags, mToken,
4094 mService.mContext.getPackageName());
4095 }
4096 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004097 if (getDevicePolicyManager() != null) {
4098 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004099 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004100 }
justinzhang5286d3f2014-05-12 17:06:01 -04004101 } catch (RemoteException ex) {
4102 throw new RuntimeException(ex);
4103 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004104 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004105 case LOCK_TASK_END_MSG: {
4106 // When lock task ends, we enable the status bars.
4107 try {
Jason Monk62515be2014-05-21 16:06:19 -04004108 if (getStatusBarService() != null) {
4109 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4110 mService.mContext.getPackageName());
4111 }
4112 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004113 if (getDevicePolicyManager() != null) {
4114 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4115 msg.arg1);
4116 }
Jason Monk62515be2014-05-21 16:06:19 -04004117 if (mLockTaskNotify == null) {
4118 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4119 }
4120 mLockTaskNotify.show(false);
4121 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004122 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004123 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004124 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004125 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004126 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004127 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004128 new LockPatternUtils(mService.mContext)
4129 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004130 }
4131 } catch (SettingNotFoundException e) {
4132 // No setting, don't lock.
4133 }
justinzhang5286d3f2014-05-12 17:06:01 -04004134 } catch (RemoteException ex) {
4135 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004136 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004137 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004138 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004139 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004140 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4141 if (mLockTaskNotify == null) {
4142 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4143 }
4144 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4145 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004146 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4147 final ActivityContainer container = (ActivityContainer) msg.obj;
4148 final IActivityContainerCallback callback = container.mCallback;
4149 if (callback != null) {
4150 try {
4151 callback.onAllActivitiesComplete(container.asBinder());
4152 } catch (RemoteException e) {
4153 }
4154 }
4155 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004156 case LAUNCH_TASK_BEHIND_COMPLETE: {
4157 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004158 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004159 if (r != null) {
4160 handleLaunchTaskBehindCompleteLocked(r);
4161 }
4162 }
4163 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004164 }
4165 }
4166 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004167
Ying Wangb081a592014-04-22 15:20:16 -07004168 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004169 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004170 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004171 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004172 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004173 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004174 ActivityRecord mParentActivity = null;
4175 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004176
Craig Mautnere3a00d72014-04-16 08:31:19 -07004177 boolean mVisible = true;
4178
Craig Mautner4a1cb222013-12-04 16:14:06 -08004179 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004180 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004181
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004182 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4183 final static int CONTAINER_STATE_NO_SURFACE = 1;
4184 final static int CONTAINER_STATE_FINISHING = 2;
4185 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4186
4187 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004188 synchronized (mService) {
4189 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004190 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004191 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004192 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004193 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004194 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004195
Craig Mautnere0a38842013-12-16 16:14:02 -08004196 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004197 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Craig Mautner34b73df2014-01-12 21:11:08 -08004198 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08004199 mActivityDisplay = activityDisplay;
4200 mStack.mDisplayId = activityDisplay.mDisplayId;
4201 mStack.mStacks = activityDisplay.mStacks;
4202
4203 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08004204 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004205 }
4206
4207 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004208 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004209 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004210 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4211 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004212 return;
4213 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004214 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004215 }
4216 }
4217
4218 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004219 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004220 synchronized (mService) {
4221 if (mActivityDisplay != null) {
4222 return mActivityDisplay.mDisplayId;
4223 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004224 }
4225 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004226 }
4227
Jeff Brownca9bc702014-02-11 14:32:56 -08004228 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004229 public int getStackId() {
4230 synchronized (mService) {
4231 return mStackId;
4232 }
4233 }
4234
4235 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004236 public boolean injectEvent(InputEvent event) {
4237 final long origId = Binder.clearCallingIdentity();
4238 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004239 synchronized (mService) {
4240 if (mActivityDisplay != null) {
4241 return mInputManagerInternal.injectInputEvent(event,
4242 mActivityDisplay.mDisplayId,
4243 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4244 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004245 }
4246 return false;
4247 } finally {
4248 Binder.restoreCallingIdentity(origId);
4249 }
4250 }
4251
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004252 @Override
4253 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004254 synchronized (mService) {
4255 if (mContainerState == CONTAINER_STATE_FINISHING) {
4256 return;
4257 }
4258 mContainerState = CONTAINER_STATE_FINISHING;
4259
Craig Mautnerd163e752014-06-13 17:18:47 -07004260 long origId = Binder.clearCallingIdentity();
4261 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004262 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004263 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004264 } finally {
4265 Binder.restoreCallingIdentity(origId);
4266 }
4267 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004268 }
4269
Craig Mautner60257702014-09-17 15:02:33 -07004270 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004271 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004272 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004273 if (mActivityDisplay != null) {
4274 mActivityDisplay.detachActivitiesLocked(mStack);
4275 mActivityDisplay = null;
4276 mStack.mDisplayId = -1;
4277 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08004278 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004279 }
4280 }
4281
4282 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004283 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004284 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004285 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004286 Binder.getCallingUid(), mCurrentUser, false,
4287 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004288
Craig Mautnere0a38842013-12-16 16:14:02 -08004289 // TODO: Switch to user app stacks here.
4290 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004291 final Uri data = intent.getData();
4292 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4293 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004294 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004295 checkEmbeddedAllowedInner(userId, intent, mimeType);
4296
4297 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4298 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
4299 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004300 }
4301
4302 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004303 public final int startActivityIntentSender(IIntentSender intentSender)
4304 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004305 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4306
4307 if (!(intentSender instanceof PendingIntentRecord)) {
4308 throw new IllegalArgumentException("Bad PendingIntent object");
4309 }
4310
Craig Mautnerb9168362015-02-26 20:40:19 -08004311 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004312 Binder.getCallingUid(), mCurrentUser, false,
4313 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004314
4315 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4316 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4317 pendingIntent.key.requestResolvedType);
4318
4319 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4320 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4321 }
4322
4323 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004324 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004325 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004326 throw new SecurityException(
4327 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4328 }
4329 }
4330
Craig Mautnerdf88d732014-01-27 09:21:32 -08004331 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004332 public IBinder asBinder() {
4333 return this;
4334 }
4335
Craig Mautner4504de52013-12-20 09:06:56 -08004336 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004337 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004338 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004339 }
4340
Craig Mautner4a1cb222013-12-04 16:14:06 -08004341 ActivityStackSupervisor getOuter() {
4342 return ActivityStackSupervisor.this;
4343 }
4344
Craig Mautnerd163e752014-06-13 17:18:47 -07004345 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004346 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004347 }
4348
4349 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004350 synchronized (mService) {
4351 if (mActivityDisplay != null) {
4352 mActivityDisplay.getBounds(outBounds);
4353 } else {
4354 outBounds.set(0, 0);
4355 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004356 }
4357 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004358
Craig Mautner6985bad2014-04-21 15:22:06 -07004359 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004360 void setVisible(boolean visible) {
4361 if (mVisible != visible) {
4362 mVisible = visible;
4363 if (mCallback != null) {
4364 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4365 0 /* unused */, this).sendToTarget();
4366 }
4367 }
4368 }
4369
Craig Mautner6985bad2014-04-21 15:22:06 -07004370 void setDrawn() {
4371 }
4372
Craig Mautner1b4bf852014-05-26 15:06:32 -07004373 // You can always start a new task on a regular ActivityStack.
4374 boolean isEligibleForNewTasks() {
4375 return true;
4376 }
4377
Craig Mautnerd163e752014-06-13 17:18:47 -07004378 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004379 detachLocked();
4380 deleteActivityContainer(this);
4381 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004382 }
4383
Craig Mautner34b73df2014-01-12 21:11:08 -08004384 @Override
4385 public String toString() {
4386 return mIdString + (mActivityDisplay == null ? "N" : "A");
4387 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004388 }
4389
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004390 private class VirtualActivityContainer extends ActivityContainer {
4391 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004392 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004393
4394 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4395 super(getNextStackId());
4396 mParentActivity = parent;
4397 mCallback = callback;
4398 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004399 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004400 }
4401
4402 @Override
4403 public void setSurface(Surface surface, int width, int height, int density) {
4404 super.setSurface(surface, width, height, density);
4405
4406 synchronized (mService) {
4407 final long origId = Binder.clearCallingIdentity();
4408 try {
4409 setSurfaceLocked(surface, width, height, density);
4410 } finally {
4411 Binder.restoreCallingIdentity(origId);
4412 }
4413 }
4414 }
4415
4416 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4417 if (mContainerState == CONTAINER_STATE_FINISHING) {
4418 return;
4419 }
4420 VirtualActivityDisplay virtualActivityDisplay =
4421 (VirtualActivityDisplay) mActivityDisplay;
4422 if (virtualActivityDisplay == null) {
4423 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004424 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004425 mActivityDisplay = virtualActivityDisplay;
4426 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4427 attachToDisplayLocked(virtualActivityDisplay);
4428 }
4429
4430 if (mSurface != null) {
4431 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004432 }
4433
Craig Mautner6985bad2014-04-21 15:22:06 -07004434 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004435 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004436 mStack.resumeTopActivityLocked(null);
4437 } else {
4438 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004439 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004440 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004441 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004442 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004443 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004444
Craig Mautnerd163e752014-06-13 17:18:47 -07004445 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004446
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004447 if (DEBUG_STACK) Slog.d(TAG_STACK,
4448 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004449 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004450
Craig Mautner6985bad2014-04-21 15:22:06 -07004451 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004452 boolean isAttachedLocked() {
4453 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004454 }
4455
4456 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004457 void setDrawn() {
4458 synchronized (mService) {
4459 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004460 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004461 }
4462 }
4463
Craig Mautner1b4bf852014-05-26 15:06:32 -07004464 // Never start a new task on an ActivityView if it isn't explicitly specified.
4465 @Override
4466 boolean isEligibleForNewTasks() {
4467 return false;
4468 }
4469
Craig Mautnerd163e752014-06-13 17:18:47 -07004470 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004471 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004472 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4473 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4474 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4475 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4476 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004477 }
4478 }
4479
Craig Mautner4a1cb222013-12-04 16:14:06 -08004480 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4481 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004482 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004483 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004484 int mDisplayId;
4485 Display mDisplay;
4486 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004487
Craig Mautner4a1cb222013-12-04 16:14:06 -08004488 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4489 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004490 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004491
Jose Lima4b6c6692014-08-12 17:41:12 -07004492 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004493
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004494 ActivityDisplay() {
4495 }
Craig Mautner4504de52013-12-20 09:06:56 -08004496
Craig Mautner1a70a162014-09-13 12:09:31 -07004497 // After instantiation, check that mDisplay is not null before using this. The alternative
4498 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004499 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004500 final Display display = mDisplayManager.getDisplay(displayId);
4501 if (display == null) {
4502 return;
4503 }
4504 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004505 }
4506
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004507 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004508 mDisplay = display;
4509 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004510 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004511 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004512
4513 void attachActivities(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004514 if (DEBUG_STACK) Slog.v(TAG_STACK,
4515 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004516 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004517 }
4518
Craig Mautnere0a38842013-12-16 16:14:02 -08004519 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004520 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004521 + " from displayId=" + mDisplayId);
4522 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004523 }
4524
4525 void getBounds(Point bounds) {
4526 mDisplay.getDisplayInfo(mDisplayInfo);
4527 bounds.x = mDisplayInfo.appWidth;
4528 bounds.y = mDisplayInfo.appHeight;
4529 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004530
Jose Lima4b6c6692014-08-12 17:41:12 -07004531 void setVisibleBehindActivity(ActivityRecord r) {
4532 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004533 }
4534
Jose Lima4b6c6692014-08-12 17:41:12 -07004535 boolean hasVisibleBehindActivity() {
4536 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004537 }
4538
Craig Mautner34b73df2014-01-12 21:11:08 -08004539 @Override
4540 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004541 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4542 }
4543 }
4544
4545 class VirtualActivityDisplay extends ActivityDisplay {
4546 VirtualDisplay mVirtualDisplay;
4547
Craig Mautner6985bad2014-04-21 15:22:06 -07004548 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004549 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004550 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4551 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4552 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4553 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004554
4555 init(mVirtualDisplay.getDisplay());
4556
4557 mWindowManager.handleDisplayAdded(mDisplayId);
4558 }
4559
4560 void setSurface(Surface surface) {
4561 if (mVirtualDisplay != null) {
4562 mVirtualDisplay.setSurface(surface);
4563 }
4564 }
4565
4566 @Override
4567 void detachActivitiesLocked(ActivityStack stack) {
4568 super.detachActivitiesLocked(stack);
4569 if (mVirtualDisplay != null) {
4570 mVirtualDisplay.release();
4571 mVirtualDisplay = null;
4572 }
4573 }
4574
4575 @Override
4576 public String toString() {
4577 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004578 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004579 }
Jose Lima58e66d62014-05-27 20:00:27 -07004580
4581 private boolean isLeanbackOnlyDevice() {
4582 boolean onLeanbackOnly = false;
4583 try {
4584 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4585 PackageManager.FEATURE_LEANBACK_ONLY);
4586 } catch (RemoteException e) {
4587 // noop
4588 }
4589
4590 return onLeanbackOnly;
4591 }
Craig Mautner27084302013-03-25 08:05:25 -07004592}