blob: 899efbb4ab06f6b57654a907fd2fd2dd113f124a [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 Mautner29219d92013-04-16 20:19:12 -070020import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
21import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Craig Mautner6170f732013-04-02 13:05:23 -070022import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Craig Mautner2420ead2013-04-01 17:13:20 -070023import static com.android.server.am.ActivityManagerService.localLOGV;
Craig Mautner23ac33b2013-04-01 16:26:35 -070024import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
Craig Mautnere7c58b62013-06-12 20:19:00 -070025import static com.android.server.am.ActivityManagerService.DEBUG_FOCUS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070026import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
Craig Mautner6170f732013-04-02 13:05:23 -070027import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070028import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
Craig Mautner2420ead2013-04-01 17:13:20 -070029import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
Craig Mautner8849a5e2013-04-02 16:41:03 -070030import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
31import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
Craig Mautner05d29032013-05-03 13:40:13 -070032import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner8d341ef2013-03-26 09:03:27 -070033import static com.android.server.am.ActivityManagerService.TAG;
34
Craig Mautner2420ead2013-04-01 17:13:20 -070035import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070036import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080037import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070038import android.app.ActivityOptions;
39import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080040import android.app.IActivityContainer;
41import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070042import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070044import android.app.PendingIntent;
Craig Mautner20e72272013-04-01 13:45:53 -070045import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070046import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070047import android.app.ResultInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070048import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070049import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070051import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070052import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070053import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.content.pm.ApplicationInfo;
55import android.content.pm.PackageManager;
56import android.content.pm.ResolveInfo;
57import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080058import android.graphics.Point;
59import android.hardware.display.DisplayManager;
60import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080061import android.hardware.display.DisplayManagerGlobal;
62import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080063import android.hardware.input.InputManager;
64import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070066import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070067import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070068import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070069import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070070import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070071import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070072import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070073import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070074import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070075import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -070076import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070077import android.os.UserHandle;
Dianne Hackborn91097de2014-04-04 18:02:06 -070078import android.service.voice.IVoiceInteractionSession;
Craig Mautner2420ead2013-04-01 17:13:20 -070079import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070080import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080081import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070082
Craig Mautner4a1cb222013-12-04 16:14:06 -080083import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080084import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080085import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080086import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080087import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -070088import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -070089import com.android.internal.app.IVoiceInteractor;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070090import com.android.internal.os.TransferPipe;
Jeff Brownca9bc702014-02-11 14:32:56 -080091import com.android.server.LocalServices;
Craig Mautner6170f732013-04-02 13:05:23 -070092import com.android.server.am.ActivityManagerService.PendingActivityLaunch;
Craig Mautner2420ead2013-04-01 17:13:20 -070093import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070094import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -070095
Craig Mautner8d341ef2013-03-26 09:03:27 -070096import java.io.FileDescriptor;
97import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -070098import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -070099import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700100import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700101
Craig Mautner4a1cb222013-12-04 16:14:06 -0800102public final class ActivityStackSupervisor implements DisplayListener {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700103 static final boolean DEBUG = ActivityManagerService.DEBUG || false;
104 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
105 static final boolean DEBUG_APP = DEBUG || false;
106 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
Craig Mautnera7f2bd42013-10-15 16:13:50 -0700107 static final boolean DEBUG_STATES = DEBUG || false;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700108 static final boolean DEBUG_IDLE = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700109
Craig Mautner2219a1b2013-03-25 09:44:30 -0700110 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700111
Craig Mautnerf3333272013-04-22 10:55:53 -0700112 /** How long we wait until giving up on the last activity telling us it is idle. */
113 static final int IDLE_TIMEOUT = 10*1000;
114
Craig Mautner0eea92c2013-05-16 13:35:39 -0700115 /** How long we can hold the sleep wake lock before giving up. */
116 static final int SLEEP_TIMEOUT = 5*1000;
117
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700118 // How long we can hold the launch wake lock before giving up.
119 static final int LAUNCH_TIMEOUT = 10*1000;
120
Craig Mautner05d29032013-05-03 13:40:13 -0700121 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
122 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
123 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700124 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700125 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800126 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
127 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
128 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700129 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800130
Craig Mautner4504de52013-12-20 09:06:56 -0800131 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700132
133 // For debugging to make sure the caller when acquiring/releasing our
134 // wake lock is the system process.
135 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700136
Craig Mautner27084302013-03-25 08:05:25 -0700137 final ActivityManagerService mService;
138
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700139 final ActivityStackSupervisorHandler mHandler;
140
141 /** Short cut */
142 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800143 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700144
Craig Mautner27084302013-03-25 08:05:25 -0700145 /** Dismiss the keyguard after the next activity is displayed? */
Craig Mautner5314a402013-09-26 12:40:16 -0700146 boolean mDismissKeyguardOnNextActivity = false;
Craig Mautner27084302013-03-25 08:05:25 -0700147
Craig Mautner8d341ef2013-03-26 09:03:27 -0700148 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700149 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700150
151 /** Task identifier that activities are currently being started in. Incremented each time a
152 * new task is created. */
153 private int mCurTaskId = 0;
154
Craig Mautner2420ead2013-04-01 17:13:20 -0700155 /** The current user */
156 private int mCurrentUser;
157
Craig Mautnere0a38842013-12-16 16:14:02 -0800158 /** The stack containing the launcher app. Assumed to always be attached to
159 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700160 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700161
Craig Mautnere0a38842013-12-16 16:14:02 -0800162 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700163 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700164
Craig Mautner4a1cb222013-12-04 16:14:06 -0800165 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
166 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800167 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800168 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700169
170 /** List of activities that are waiting for a new activity to become visible before completing
171 * whatever operation they are supposed to do. */
172 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();
173
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700174 /** List of processes waiting to find out about the next visible activity. */
175 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
176 new ArrayList<IActivityManager.WaitResult>();
177
178 /** List of processes waiting to find out about the next launched activity. */
179 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
180 new ArrayList<IActivityManager.WaitResult>();
181
Craig Mautnerde4ef022013-04-07 19:01:33 -0700182 /** List of activities that are ready to be stopped, but waiting for the next activity to
183 * settle down before doing so. */
184 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>();
185
Craig Mautnerf3333272013-04-22 10:55:53 -0700186 /** List of activities that are ready to be finished, but waiting for the previous activity to
187 * settle down before doing so. It contains ActivityRecord objects. */
188 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>();
189
Craig Mautner0eea92c2013-05-16 13:35:39 -0700190 /** List of activities that are in the process of going to sleep. */
191 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
192
Craig Mautnerf3333272013-04-22 10:55:53 -0700193 /** Used on user changes */
194 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<UserStartedState>();
195
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700196 /** Used to queue up any background users being started */
197 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<UserStartedState>();
198
Craig Mautnerde4ef022013-04-07 19:01:33 -0700199 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
200 * is being brought in front of us. */
201 boolean mUserLeaving = false;
202
Craig Mautner0eea92c2013-05-16 13:35:39 -0700203 /** Set when we have taken too long waiting to go to sleep. */
204 boolean mSleepTimeout = false;
205
206 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700207 * We don't want to allow the device to go to sleep while in the process
208 * of launching an activity. This is primarily to allow alarm intent
209 * receivers to launch an activity and get that to run before the device
210 * goes back to sleep.
211 */
212 final PowerManager.WakeLock mLaunchingActivity;
213
214 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700215 * Set when the system is going to sleep, until we have
216 * successfully paused the current activity and released our wake lock.
217 * At that point the system is allowed to actually sleep.
218 */
219 final PowerManager.WakeLock mGoingToSleep;
220
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700221 /** Stack id of the front stack when user switched, indexed by userId. */
222 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700223
Craig Mautner4504de52013-12-20 09:06:56 -0800224 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800225 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700226 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800227
228 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700229 private final SparseArray<ActivityDisplay> mActivityDisplays =
230 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800231
Jeff Brownca9bc702014-02-11 14:32:56 -0800232 InputManagerInternal mInputManagerInternal;
233
Craig Mautneraea74a52014-03-08 14:23:10 -0800234 /** If non-null then the task specified remains in front and no other tasks may be started
235 * until the task exits or #stopLockTaskMode() is called. */
236 private TaskRecord mLockTaskModeTask;
237
Craig Mautner4a1cb222013-12-04 16:14:06 -0800238 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700239 mService = service;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800240 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700241 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800242 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700243 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
244 throw new IllegalStateException("Calling must be system uid");
245 }
246 mLaunchingActivity =
247 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
248 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700249 }
250
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700251 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800252 synchronized (mService) {
253 mWindowManager = wm;
254
255 mDisplayManager =
256 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
257 mDisplayManager.registerDisplayListener(this, null);
258
259 Display[] displays = mDisplayManager.getDisplays();
260 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
261 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800262 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
263 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800264 }
265
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700266 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800267 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800268
269 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800270 }
Craig Mautner27084302013-03-25 08:05:25 -0700271 }
272
273 void dismissKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700274 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner27084302013-03-25 08:05:25 -0700275 if (mDismissKeyguardOnNextActivity) {
276 mDismissKeyguardOnNextActivity = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700277 mWindowManager.dismissKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700278 }
279 }
280
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700281 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800282 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700283 }
284
Craig Mautnerde4ef022013-04-07 19:01:33 -0700285 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800286 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700287 }
288
Craig Mautner4a1cb222013-12-04 16:14:06 -0800289 // TODO: Split into two methods isFrontStack for any visible stack and isFrontmostStack for the
290 // top of all visible stacks.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700291 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800292 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
293 if (parent != null) {
294 stack = parent.task.stack;
295 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800296 ArrayList<ActivityStack> stacks = stack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800297 if (stacks != null && !stacks.isEmpty()) {
298 return stack == stacks.get(stacks.size() - 1);
299 }
300 return false;
Craig Mautner20e72272013-04-01 13:45:53 -0700301 }
302
Craig Mautnerde4ef022013-04-07 19:01:33 -0700303 void moveHomeStack(boolean toFront) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800304 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800305 int topNdx = stacks.size() - 1;
306 if (topNdx <= 0) {
307 return;
308 }
309 ActivityStack topStack = stacks.get(topNdx);
310 final boolean homeInFront = topStack == mHomeStack;
311 if (homeInFront != toFront) {
312 mLastFocusedStack = topStack;
313 stacks.remove(mHomeStack);
314 stacks.add(toFront ? topNdx : 0, mHomeStack);
315 mFocusedStack = stacks.get(topNdx);
316 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
317 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700318 }
319 }
320
Craig Mautner8e569572013-10-11 17:36:59 -0700321 void moveHomeToTop() {
Craig Mautner69ada552013-04-18 13:51:51 -0700322 moveHomeStack(true);
Craig Mautner8e569572013-10-11 17:36:59 -0700323 mHomeStack.moveHomeTaskToTop();
324 }
325
326 boolean resumeHomeActivity(ActivityRecord prev) {
327 moveHomeToTop();
Craig Mautner69ada552013-04-18 13:51:51 -0700328 if (prev != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700329 prev.task.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -0700330 }
Craig Mautnera8a90e02013-06-28 15:24:50 -0700331 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner760b2312013-10-11 11:57:07 -0700332 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700333 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700334 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700335 }
336 return mService.startHomeActivityLocked(mCurrentUser);
337 }
338
Craig Mautner27084302013-03-25 08:05:25 -0700339 void setDismissKeyguard(boolean dismiss) {
Craig Mautner5314a402013-09-26 12:40:16 -0700340 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen(" dismiss=" + dismiss);
Craig Mautner27084302013-03-25 08:05:25 -0700341 mDismissKeyguardOnNextActivity = dismiss;
342 }
343
Craig Mautner8d341ef2013-03-26 09:03:27 -0700344 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800345 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800346 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800347 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800348 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
349 ActivityStack stack = stacks.get(stackNdx);
350 TaskRecord task = stack.taskForIdLocked(id);
351 if (task != null) {
352 return task;
353 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700354 }
355 }
356 return null;
357 }
358
Craig Mautner6170f732013-04-02 13:05:23 -0700359 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800360 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800361 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800362 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800363 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
364 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
365 if (r != null) {
366 return r;
367 }
Craig Mautner6170f732013-04-02 13:05:23 -0700368 }
369 }
370 return null;
371 }
372
Craig Mautner8d341ef2013-03-26 09:03:27 -0700373 int getNextTaskId() {
374 do {
375 mCurTaskId++;
376 if (mCurTaskId <= 0) {
377 mCurTaskId = 1;
378 }
379 } while (anyTaskForIdLocked(mCurTaskId) != null);
380 return mCurTaskId;
381 }
382
Craig Mautnerde4ef022013-04-07 19:01:33 -0700383 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700384 ActivityStack stack = getFocusedStack();
385 if (stack == null) {
386 return null;
387 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700388 ActivityRecord resumedActivity = stack.mResumedActivity;
389 if (resumedActivity == null || resumedActivity.app == null) {
390 resumedActivity = stack.mPausingActivity;
391 if (resumedActivity == null || resumedActivity.app == null) {
392 resumedActivity = stack.topRunningActivityLocked(null);
393 }
394 }
395 return resumedActivity;
396 }
397
Mike Lockwooded8902d2013-11-15 11:01:47 -0800398 boolean attachApplicationLocked(ProcessRecord app) throws Exception {
Craig Mautner20e72272013-04-01 13:45:53 -0700399 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800400 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800401 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
402 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800403 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
404 final ActivityStack stack = stacks.get(stackNdx);
405 if (!isFrontStack(stack)) {
406 continue;
407 }
408 ActivityRecord hr = stack.topRunningActivityLocked(null);
409 if (hr != null) {
410 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
411 && processName.equals(hr.processName)) {
412 try {
George Mount2c92c972014-03-20 09:38:23 -0700413 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800414 didSomething = true;
415 }
416 } catch (Exception e) {
417 Slog.w(TAG, "Exception in new application when starting activity "
418 + hr.intent.getComponent().flattenToShortString(), e);
419 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700420 }
Craig Mautner20e72272013-04-01 13:45:53 -0700421 }
Craig Mautner20e72272013-04-01 13:45:53 -0700422 }
423 }
424 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700425 if (!didSomething) {
426 ensureActivitiesVisibleLocked(null, 0);
427 }
Craig Mautner20e72272013-04-01 13:45:53 -0700428 return didSomething;
429 }
430
431 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800432 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
433 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800434 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
435 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800436 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800437 continue;
438 }
439 final ActivityRecord resumedActivity = stack.mResumedActivity;
440 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800441 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
442 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800443 return false;
444 }
Craig Mautner20e72272013-04-01 13:45:53 -0700445 }
446 }
447 return true;
448 }
449
Craig Mautnerde4ef022013-04-07 19:01:33 -0700450 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800451 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
452 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800453 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
454 final ActivityStack stack = stacks.get(stackNdx);
455 if (isFrontStack(stack)) {
456 final ActivityRecord r = stack.mResumedActivity;
457 if (r != null && r.state != ActivityState.RESUMED) {
458 return false;
459 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700460 }
461 }
462 }
463 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800464 if (DEBUG_STACK) Slog.d(TAG,
465 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
466 mLastFocusedStack + " to=" + mFocusedStack);
467 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700468 return true;
469 }
470
471 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800472 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
473 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800474 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
475 final ActivityStack stack = stacks.get(stackNdx);
476 final ActivityRecord r = stack.mResumedActivity;
477 if (r != null && (!r.nowVisible || r.waitingVisible)) {
478 return false;
479 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700480 }
481 }
482 return true;
483 }
484
Craig Mautner2acc3892013-09-23 10:28:14 -0700485 /**
486 * Pause all activities in either all of the stacks or just the back stacks.
487 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700488 * @return true if any activity was paused as a result of this call.
489 */
Craig Mautner5314a402013-09-26 12:40:16 -0700490 boolean pauseBackStacks(boolean userLeaving) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700491 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800492 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
493 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800494 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
495 final ActivityStack stack = stacks.get(stackNdx);
496 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
497 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
498 " mResumedActivity=" + stack.mResumedActivity);
499 stack.startPausingLocked(userLeaving, false);
500 someActivityPaused = true;
501 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700502 }
503 }
504 return someActivityPaused;
505 }
506
Craig Mautnerde4ef022013-04-07 19:01:33 -0700507 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700508 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800509 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
510 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800511 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
512 final ActivityStack stack = stacks.get(stackNdx);
513 final ActivityRecord r = stack.mPausingActivity;
514 if (r != null && r.state != ActivityState.PAUSED
515 && r.state != ActivityState.STOPPED
516 && r.state != ActivityState.STOPPING) {
517 if (DEBUG_STATES) {
518 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
519 pausing = false;
520 } else {
521 return false;
522 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700523 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700524 }
525 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700526 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700527 }
528
Craig Mautnerdf88d732014-01-27 09:21:32 -0800529 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping) {
John Spurlock8a985d22014-02-25 09:40:05 -0500530 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800531 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
532 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
533 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
534 final ActivityStack stack = stacks.get(stackNdx);
535 if (stack.mResumedActivity != null &&
536 stack.mActivityContainer.mParentActivity == parent) {
537 stack.startPausingLocked(userLeaving, uiSleeping);
538 }
539 }
540 }
541 }
542
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700543 void reportActivityVisibleLocked(ActivityRecord r) {
Craig Mautner858d8a62013-04-23 17:08:34 -0700544 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700545 WaitResult w = mWaitingActivityVisible.get(i);
546 w.timeout = false;
547 if (r != null) {
548 w.who = new ComponentName(r.info.packageName, r.info.name);
549 }
550 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
551 w.thisTime = w.totalTime;
552 }
553 mService.notifyAll();
554 dismissKeyguard();
555 }
556
557 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
558 long thisTime, long totalTime) {
559 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700560 WaitResult w = mWaitingActivityLaunched.remove(i);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700561 w.timeout = timeout;
562 if (r != null) {
563 w.who = new ComponentName(r.info.packageName, r.info.name);
564 }
565 w.thisTime = thisTime;
566 w.totalTime = totalTime;
567 }
568 mService.notifyAll();
569 }
570
Craig Mautner29219d92013-04-16 20:19:12 -0700571 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700572 final ActivityStack focusedStack = getFocusedStack();
573 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
574 if (r != null) {
575 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700576 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700577
Craig Mautner4a1cb222013-12-04 16:14:06 -0800578 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800579 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800580 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
581 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700582 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700583 r = stack.topRunningActivityLocked(null);
584 if (r != null) {
585 return r;
586 }
587 }
588 }
589 return null;
590 }
591
Dianne Hackborn09233282014-04-30 11:33:59 -0700592 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700593 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800594 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
595 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800596 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800597 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800598 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800599 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
600 final ActivityStack stack = stacks.get(stackNdx);
601 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
602 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700603 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700604 }
605 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700606
607 // The lists are already sorted from most recent to oldest. Just pull the most recent off
608 // each list and add it to list. Stop when all lists are empty or maxNum reached.
609 while (maxNum > 0) {
610 long mostRecentActiveTime = Long.MIN_VALUE;
611 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800612 final int numTaskLists = runningTaskLists.size();
613 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
614 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700615 if (!stackTaskList.isEmpty()) {
616 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
617 if (lastActiveTime > mostRecentActiveTime) {
618 mostRecentActiveTime = lastActiveTime;
619 selectedStackList = stackTaskList;
620 }
621 }
622 }
623 if (selectedStackList != null) {
624 list.add(selectedStackList.remove(0));
625 --maxNum;
626 } else {
627 break;
628 }
629 }
Craig Mautner20e72272013-04-01 13:45:53 -0700630 }
631
Craig Mautner23ac33b2013-04-01 16:26:35 -0700632 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
633 String profileFile, ParcelFileDescriptor profileFd, int userId) {
634 // Collect information about the target of the Intent.
635 ActivityInfo aInfo;
636 try {
637 ResolveInfo rInfo =
638 AppGlobals.getPackageManager().resolveIntent(
639 intent, resolvedType,
640 PackageManager.MATCH_DEFAULT_ONLY
641 | ActivityManagerService.STOCK_PM_FLAGS, userId);
642 aInfo = rInfo != null ? rInfo.activityInfo : null;
643 } catch (RemoteException e) {
644 aInfo = null;
645 }
646
647 if (aInfo != null) {
648 // Store the found target back into the intent, because now that
649 // we have it we never want to do this again. For example, if the
650 // user navigates back to this point in the history, we should
651 // always restart the exact same activity.
652 intent.setComponent(new ComponentName(
653 aInfo.applicationInfo.packageName, aInfo.name));
654
655 // Don't debug things in the system process
656 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
657 if (!aInfo.processName.equals("system")) {
658 mService.setDebugApp(aInfo.processName, true, false);
659 }
660 }
661
662 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
663 if (!aInfo.processName.equals("system")) {
664 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
665 }
666 }
667
668 if (profileFile != null) {
669 if (!aInfo.processName.equals("system")) {
670 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName,
671 profileFile, profileFd,
672 (startFlags&ActivityManager.START_FLAG_AUTO_STOP_PROFILER) != 0);
673 }
674 }
675 }
676 return aInfo;
677 }
678
Craig Mautner2219a1b2013-03-25 09:44:30 -0700679 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautner8e569572013-10-11 17:36:59 -0700680 moveHomeToTop();
Dianne Hackborn91097de2014-04-04 18:02:06 -0700681 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null, 0,
Craig Mautnere0a38842013-12-16 16:14:02 -0800682 null, false, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700683 }
684
Craig Mautner23ac33b2013-04-01 16:26:35 -0700685 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700686 String callingPackage, Intent intent, String resolvedType,
687 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
688 IBinder resultTo, String resultWho, int requestCode, int startFlags, String profileFile,
Craig Mautner23ac33b2013-04-01 16:26:35 -0700689 ParcelFileDescriptor profileFd, WaitResult outResult, Configuration config,
Craig Mautnere0a38842013-12-16 16:14:02 -0800690 Bundle options, int userId, IActivityContainer iContainer) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700691 // Refuse possible leaked file descriptors
692 if (intent != null && intent.hasFileDescriptors()) {
693 throw new IllegalArgumentException("File descriptors passed in Intent");
694 }
695 boolean componentSpecified = intent.getComponent() != null;
696
697 // Don't modify the client's object!
698 intent = new Intent(intent);
699
700 // Collect information about the target of the Intent.
701 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
702 profileFile, profileFd, userId);
703
Craig Mautnere0a38842013-12-16 16:14:02 -0800704 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700705 synchronized (mService) {
706 int callingPid;
707 if (callingUid >= 0) {
708 callingPid = -1;
709 } else if (caller == null) {
710 callingPid = Binder.getCallingPid();
711 callingUid = Binder.getCallingUid();
712 } else {
713 callingPid = callingUid = -1;
714 }
715
Craig Mautnere0a38842013-12-16 16:14:02 -0800716 final ActivityStack stack;
717 if (container == null || container.mStack.isOnHomeDisplay()) {
718 stack = getFocusedStack();
719 } else {
720 stack = container.mStack;
721 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700722 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700723 && mService.mConfiguration.diff(config) != 0;
724 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700725 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700726
727 final long origId = Binder.clearCallingIdentity();
728
729 if (aInfo != null &&
730 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
731 // This may be a heavy-weight process! Check to see if we already
732 // have another, different heavy-weight process running.
733 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
734 if (mService.mHeavyWeightProcess != null &&
735 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
736 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700737 int realCallingUid = callingUid;
738 if (caller != null) {
739 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
740 if (callerApp != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700741 realCallingUid = callerApp.info.uid;
742 } else {
743 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700744 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700745 + intent.toString());
746 ActivityOptions.abort(options);
747 return ActivityManager.START_PERMISSION_DENIED;
748 }
749 }
750
751 IIntentSender target = mService.getIntentSenderLocked(
752 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
753 realCallingUid, userId, null, null, 0, new Intent[] { intent },
754 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
755 | PendingIntent.FLAG_ONE_SHOT, null);
756
757 Intent newIntent = new Intent();
758 if (requestCode >= 0) {
759 // Caller is requesting a result.
760 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
761 }
762 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
763 new IntentSender(target));
764 if (mService.mHeavyWeightProcess.activities.size() > 0) {
765 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
766 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
767 hist.packageName);
768 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
769 hist.task.taskId);
770 }
771 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
772 aInfo.packageName);
773 newIntent.setFlags(intent.getFlags());
774 newIntent.setClassName("android",
775 HeavyWeightSwitcherActivity.class.getName());
776 intent = newIntent;
777 resolvedType = null;
778 caller = null;
779 callingUid = Binder.getCallingUid();
780 callingPid = Binder.getCallingPid();
781 componentSpecified = true;
782 try {
783 ResolveInfo rInfo =
784 AppGlobals.getPackageManager().resolveIntent(
785 intent, null,
786 PackageManager.MATCH_DEFAULT_ONLY
787 | ActivityManagerService.STOCK_PM_FLAGS, userId);
788 aInfo = rInfo != null ? rInfo.activityInfo : null;
789 aInfo = mService.getActivityInfoForUser(aInfo, userId);
790 } catch (RemoteException e) {
791 aInfo = null;
792 }
793 }
794 }
795 }
796
Dianne Hackborn91097de2014-04-04 18:02:06 -0700797 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
798 voiceSession, voiceInteractor, resultTo, resultWho,
Craig Mautnere0a38842013-12-16 16:14:02 -0800799 requestCode, callingPid, callingUid, callingPackage, startFlags, options,
800 componentSpecified, null, container);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700801
Craig Mautnerde4ef022013-04-07 19:01:33 -0700802 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700803 // If the caller also wants to switch to a new configuration,
804 // do so now. This allows a clean switch, as we are waiting
805 // for the current activity to pause (so we will not destroy
806 // it), and have not yet started the next activity.
807 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
808 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700809 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700810 if (DEBUG_CONFIGURATION) Slog.v(TAG,
811 "Updating to new configuration after starting activity.");
812 mService.updateConfigurationLocked(config, null, false, false);
813 }
814
815 Binder.restoreCallingIdentity(origId);
816
817 if (outResult != null) {
818 outResult.result = res;
819 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700820 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700821 do {
822 try {
823 mService.wait();
824 } catch (InterruptedException e) {
825 }
826 } while (!outResult.timeout && outResult.who == null);
827 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700828 ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700829 if (r.nowVisible) {
830 outResult.timeout = false;
831 outResult.who = new ComponentName(r.info.packageName, r.info.name);
832 outResult.totalTime = 0;
833 outResult.thisTime = 0;
834 } else {
835 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700836 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700837 do {
838 try {
839 mService.wait();
840 } catch (InterruptedException e) {
841 }
842 } while (!outResult.timeout && outResult.who == null);
843 }
844 }
845 }
846
847 return res;
848 }
849 }
850
851 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
852 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
853 Bundle options, int userId) {
854 if (intents == null) {
855 throw new NullPointerException("intents is null");
856 }
857 if (resolvedTypes == null) {
858 throw new NullPointerException("resolvedTypes is null");
859 }
860 if (intents.length != resolvedTypes.length) {
861 throw new IllegalArgumentException("intents are length different than resolvedTypes");
862 }
863
Craig Mautner23ac33b2013-04-01 16:26:35 -0700864
865 int callingPid;
866 if (callingUid >= 0) {
867 callingPid = -1;
868 } else if (caller == null) {
869 callingPid = Binder.getCallingPid();
870 callingUid = Binder.getCallingUid();
871 } else {
872 callingPid = callingUid = -1;
873 }
874 final long origId = Binder.clearCallingIdentity();
875 try {
876 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -0700877 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -0700878 for (int i=0; i<intents.length; i++) {
879 Intent intent = intents[i];
880 if (intent == null) {
881 continue;
882 }
883
884 // Refuse possible leaked file descriptors
885 if (intent != null && intent.hasFileDescriptors()) {
886 throw new IllegalArgumentException("File descriptors passed in Intent");
887 }
888
889 boolean componentSpecified = intent.getComponent() != null;
890
891 // Don't modify the client's object!
892 intent = new Intent(intent);
893
894 // Collect information about the target of the Intent.
895 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i],
896 0, null, null, userId);
897 // TODO: New, check if this is correct
898 aInfo = mService.getActivityInfoForUser(aInfo, userId);
899
900 if (aInfo != null &&
901 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
902 != 0) {
903 throw new IllegalArgumentException(
904 "FLAG_CANT_SAVE_STATE not supported here");
905 }
906
907 Bundle theseOptions;
908 if (options != null && i == intents.length-1) {
909 theseOptions = options;
910 } else {
911 theseOptions = null;
912 }
Craig Mautner6170f732013-04-02 13:05:23 -0700913 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn91097de2014-04-04 18:02:06 -0700914 aInfo, null, null, resultTo, null, -1, callingPid, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -0800915 0, theseOptions, componentSpecified, outActivity, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700916 if (res < 0) {
917 return res;
918 }
919
920 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
921 }
922 }
923 } finally {
924 Binder.restoreCallingIdentity(origId);
925 }
926
927 return ActivityManager.START_SUCCESS;
928 }
929
Craig Mautner2420ead2013-04-01 17:13:20 -0700930 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -0700931 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -0700932 throws RemoteException {
933
934 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -0700935 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700936 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -0700937
938 // schedule launch ticks to collect information about slow apps.
939 r.startLaunchTickingLocked();
940
941 // Have the window manager re-evaluate the orientation of
942 // the screen based on the new activity order. Note that
943 // as a result of this, it can call back into the activity
944 // manager with a new orientation. We don't care about that,
945 // because the activity is not currently running so we are
946 // just restarting it anyway.
947 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700948 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -0700949 mService.mConfiguration,
950 r.mayFreezeScreenLocked(app) ? r.appToken : null);
951 mService.updateConfigurationLocked(config, r, false, false);
952 }
953
954 r.app = app;
955 app.waitingToKill = null;
956 r.launchCount++;
957 r.lastLaunchTime = SystemClock.uptimeMillis();
958
959 if (localLOGV) Slog.v(TAG, "Launching: " + r);
960
961 int idx = app.activities.indexOf(r);
962 if (idx < 0) {
963 app.activities.add(r);
964 }
Dianne Hackborndb926082013-10-31 16:32:44 -0700965 mService.updateLruProcessLocked(app, true, null);
966 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -0700967
968 final ActivityStack stack = r.task.stack;
969 try {
970 if (app.thread == null) {
971 throw new RemoteException();
972 }
973 List<ResultInfo> results = null;
974 List<Intent> newIntents = null;
975 if (andResume) {
976 results = r.results;
977 newIntents = r.newIntents;
978 }
979 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
980 + " icicle=" + r.icicle
981 + " with results=" + results + " newIntents=" + newIntents
982 + " andResume=" + andResume);
983 if (andResume) {
984 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
985 r.userId, System.identityHashCode(r),
986 r.task.taskId, r.shortComponentName);
987 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700988 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -0700989 // Home process is the root process of the task.
990 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -0700991 }
992 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
993 r.sleeping = false;
994 r.forceNewConfig = false;
995 mService.showAskCompatModeDialogLocked(r);
996 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
997 String profileFile = null;
998 ParcelFileDescriptor profileFd = null;
999 boolean profileAutoStop = false;
1000 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1001 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1002 mService.mProfileProc = app;
1003 profileFile = mService.mProfileFile;
1004 profileFd = mService.mProfileFd;
1005 profileAutoStop = mService.mAutoStopProfiler;
1006 }
1007 }
1008 app.hasShownUi = true;
1009 app.pendingUiClean = true;
1010 if (profileFd != null) {
1011 try {
1012 profileFd = profileFd.dup();
1013 } catch (IOException e) {
1014 if (profileFd != null) {
1015 try {
1016 profileFd.close();
1017 } catch (IOException o) {
1018 }
1019 profileFd = null;
1020 }
1021 }
1022 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001023
Dianne Hackborna413dc02013-07-12 12:02:55 -07001024 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001025 Bundle options = (r.pendingOptions == null) ? null : r.pendingOptions.toBundle();
1026 r.clearOptionsLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001027 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1028 System.identityHashCode(r), r.info,
Dianne Hackborna413dc02013-07-12 12:02:55 -07001029 new Configuration(mService.mConfiguration), r.compat,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001030 r.task.voiceInteractor, app.repProcState, r.icicle, results, newIntents,
1031 !andResume, mService.isNextTransitionForward(), profileFile, profileFd,
George Mount2c92c972014-03-20 09:38:23 -07001032 profileAutoStop, options);
Craig Mautner2420ead2013-04-01 17:13:20 -07001033
1034 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1035 // This may be a heavy-weight process! Note that the package
1036 // manager will ensure that only activity can run in the main
1037 // process of the .apk, which is the only thing that will be
1038 // considered heavy-weight.
1039 if (app.processName.equals(app.info.packageName)) {
1040 if (mService.mHeavyWeightProcess != null
1041 && mService.mHeavyWeightProcess != app) {
1042 Slog.w(TAG, "Starting new heavy weight process " + app
1043 + " when already running "
1044 + mService.mHeavyWeightProcess);
1045 }
1046 mService.mHeavyWeightProcess = app;
1047 Message msg = mService.mHandler.obtainMessage(
1048 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1049 msg.obj = r;
1050 mService.mHandler.sendMessage(msg);
1051 }
1052 }
1053
1054 } catch (RemoteException e) {
1055 if (r.launchFailed) {
1056 // This is the second time we failed -- finish activity
1057 // and give up.
1058 Slog.e(TAG, "Second failure launching "
1059 + r.intent.getComponent().flattenToShortString()
1060 + ", giving up", e);
1061 mService.appDiedLocked(app, app.pid, app.thread);
1062 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1063 "2nd-crash", false);
1064 return false;
1065 }
1066
1067 // This is the first time we failed -- restart process and
1068 // retry.
1069 app.activities.remove(r);
1070 throw e;
1071 }
1072
1073 r.launchFailed = false;
1074 if (stack.updateLRUListLocked(r)) {
1075 Slog.w(TAG, "Activity " + r
1076 + " being launched, but already in LRU list");
1077 }
1078
1079 if (andResume) {
1080 // As part of the process of launching, ActivityThread also performs
1081 // a resume.
1082 stack.minimalResumeActivityLocked(r);
1083 } else {
1084 // This activity is not starting in the resumed state... which
1085 // should look like we asked it to pause+stop (but remain visible),
1086 // and it has done so and reported back the current icicle and
1087 // other state.
1088 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1089 + " (starting in stopped state)");
1090 r.state = ActivityState.STOPPED;
1091 r.stopped = true;
1092 }
1093
1094 // Launch the new version setup screen if needed. We do this -after-
1095 // launching the initial activity (that is, home), so that it can have
1096 // a chance to initialize itself while in the background, making the
1097 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001098 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001099 mService.startSetupActivityLocked();
1100 }
1101
1102 return true;
1103 }
1104
Craig Mautnere79d42682013-04-01 19:01:53 -07001105 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001106 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001107 // Is this activity's application already running?
1108 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001109 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001110
1111 r.task.stack.setLaunchTime(r);
1112
1113 if (app != null && app.thread != null) {
1114 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001115 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1116 || !"android".equals(r.info.packageName)) {
1117 // Don't add this if it is a platform component that is marked
1118 // to run in multiple processes, because this is actually
1119 // part of the framework so doesn't make sense to track as a
1120 // separate apk in the process.
1121 app.addPackage(r.info.packageName, mService.mProcessStats);
1122 }
George Mount2c92c972014-03-20 09:38:23 -07001123 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001124 return;
1125 } catch (RemoteException e) {
1126 Slog.w(TAG, "Exception when starting activity "
1127 + r.intent.getComponent().flattenToShortString(), e);
1128 }
1129
1130 // If a dead object exception was thrown -- fall through to
1131 // restart the application.
1132 }
1133
1134 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001135 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001136 }
1137
Craig Mautner6170f732013-04-02 13:05:23 -07001138 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001139 Intent intent, String resolvedType, ActivityInfo aInfo,
1140 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1141 IBinder resultTo, String resultWho, int requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001142 int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
Craig Mautnere0a38842013-12-16 16:14:02 -08001143 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container) {
Craig Mautner6170f732013-04-02 13:05:23 -07001144 int err = ActivityManager.START_SUCCESS;
1145
1146 ProcessRecord callerApp = null;
1147 if (caller != null) {
1148 callerApp = mService.getRecordForAppLocked(caller);
1149 if (callerApp != null) {
1150 callingPid = callerApp.pid;
1151 callingUid = callerApp.info.uid;
1152 } else {
1153 Slog.w(TAG, "Unable to find app for caller " + caller
1154 + " (pid=" + callingPid + ") when starting: "
1155 + intent.toString());
1156 err = ActivityManager.START_PERMISSION_DENIED;
1157 }
1158 }
1159
1160 if (err == ActivityManager.START_SUCCESS) {
1161 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1162 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner9ef471f2014-02-07 13:11:47 -08001163 + "} from pid " + (callerApp != null ? callerApp.pid : callingPid)
1164 + " on display " + (container == null ? (mFocusedStack == null ?
1165 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1166 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1167 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001168 }
1169
1170 ActivityRecord sourceRecord = null;
1171 ActivityRecord resultRecord = null;
1172 if (resultTo != null) {
1173 sourceRecord = isInAnyStackLocked(resultTo);
1174 if (DEBUG_RESULTS) Slog.v(
1175 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1176 if (sourceRecord != null) {
1177 if (requestCode >= 0 && !sourceRecord.finishing) {
1178 resultRecord = sourceRecord;
1179 }
1180 }
1181 }
1182 ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1183
Dianne Hackborn91097de2014-04-04 18:02:06 -07001184 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001185
1186 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
1187 && sourceRecord != null) {
1188 // Transfer the result target from the source activity to the new
1189 // one being started, including any failures.
1190 if (requestCode >= 0) {
1191 ActivityOptions.abort(options);
1192 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1193 }
1194 resultRecord = sourceRecord.resultTo;
1195 resultWho = sourceRecord.resultWho;
1196 requestCode = sourceRecord.requestCode;
1197 sourceRecord.resultTo = null;
1198 if (resultRecord != null) {
1199 resultRecord.removeResultsLocked(
1200 sourceRecord, resultWho, requestCode);
1201 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001202 if (sourceRecord.launchedFromUid == callingUid) {
1203 // The new activity is being launched from the same uid as the previous
1204 // activity in the flow, and asking to forward its result back to the
1205 // previous. In this case the activity is serving as a trampoline between
1206 // the two, so we also want to update its launchedFromPackage to be the
1207 // same as the previous activity. Note that this is safe, since we know
1208 // these two packages come from the same uid; the caller could just as
1209 // well have supplied that same package name itself. This specifially
1210 // deals with the case of an intent picker/chooser being launched in the app
1211 // flow to redirect to an activity picked by the user, where we want the final
1212 // activity to consider it to have been launched by the previous app activity.
1213 callingPackage = sourceRecord.launchedFromPackage;
1214 }
Craig Mautner6170f732013-04-02 13:05:23 -07001215 }
1216
1217 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1218 // We couldn't find a class that can handle the given Intent.
1219 // That's the end of that!
1220 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1221 }
1222
1223 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1224 // We couldn't find the specific class specified in the Intent.
1225 // Also the end of the line.
1226 err = ActivityManager.START_CLASS_NOT_FOUND;
1227 }
1228
Dianne Hackborn91097de2014-04-04 18:02:06 -07001229 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1230 && sourceRecord.task.voiceSession != null) {
1231 // If this activity is being launched as part of a voice session, we need
1232 // to ensure that it is safe to do so. If the upcoming activity will also
1233 // be part of the voice session, we can only launch it if it has explicitly
1234 // said it supports the VOICE category, or it is a part of the calling app.
1235 if ((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) == 0
1236 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1237 try {
1238 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1239 intent, resolvedType)) {
1240 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1241 }
1242 } catch (RemoteException e) {
1243 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1244 }
1245 }
1246 }
1247
1248 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1249 // If the caller is starting a new voice session, just make sure the target
1250 // is actually allowing it to run this way.
1251 try {
1252 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1253 intent, resolvedType)) {
1254 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1255 }
1256 } catch (RemoteException e) {
1257 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1258 }
1259 }
1260
Craig Mautner6170f732013-04-02 13:05:23 -07001261 if (err != ActivityManager.START_SUCCESS) {
1262 if (resultRecord != null) {
1263 resultStack.sendActivityResultLocked(-1,
1264 resultRecord, resultWho, requestCode,
1265 Activity.RESULT_CANCELED, null);
1266 }
1267 setDismissKeyguard(false);
1268 ActivityOptions.abort(options);
1269 return err;
1270 }
1271
1272 final int startAnyPerm = mService.checkPermission(
1273 START_ANY_ACTIVITY, callingPid, callingUid);
1274 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1275 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1276 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1277 if (resultRecord != null) {
1278 resultStack.sendActivityResultLocked(-1,
1279 resultRecord, resultWho, requestCode,
1280 Activity.RESULT_CANCELED, null);
1281 }
1282 setDismissKeyguard(false);
1283 String msg;
1284 if (!aInfo.exported) {
1285 msg = "Permission Denial: starting " + intent.toString()
1286 + " from " + callerApp + " (pid=" + callingPid
1287 + ", uid=" + callingUid + ")"
1288 + " not exported from uid " + aInfo.applicationInfo.uid;
1289 } else {
1290 msg = "Permission Denial: starting " + intent.toString()
1291 + " from " + callerApp + " (pid=" + callingPid
1292 + ", uid=" + callingUid + ")"
1293 + " requires " + aInfo.permission;
1294 }
1295 Slog.w(TAG, msg);
1296 throw new SecurityException(msg);
1297 }
1298
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001299 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001300 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001301
Craig Mautner6170f732013-04-02 13:05:23 -07001302 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001303 try {
1304 // The Intent we give to the watcher has the extra data
1305 // stripped off, since it can contain private information.
1306 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001307 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001308 aInfo.applicationInfo.packageName);
1309 } catch (RemoteException e) {
1310 mService.mController = null;
1311 }
Ben Gruver5e207332013-04-03 17:41:37 -07001312 }
Craig Mautner6170f732013-04-02 13:05:23 -07001313
Ben Gruver5e207332013-04-03 17:41:37 -07001314 if (abort) {
1315 if (resultRecord != null) {
1316 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001317 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001318 }
Ben Gruver5e207332013-04-03 17:41:37 -07001319 // We pretend to the caller that it was really started, but
1320 // they will just get a cancel result.
1321 setDismissKeyguard(false);
1322 ActivityOptions.abort(options);
1323 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001324 }
1325
1326 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001327 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
1328 requestCode, componentSpecified, this, container);
Craig Mautner6170f732013-04-02 13:05:23 -07001329 if (outActivity != null) {
1330 outActivity[0] = r;
1331 }
1332
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001333 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001334 if (voiceSession == null && (stack.mResumedActivity == null
1335 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Craig Mautner6170f732013-04-02 13:05:23 -07001336 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
1337 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001338 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautner6170f732013-04-02 13:05:23 -07001339 mService.mPendingActivityLaunches.add(pal);
1340 setDismissKeyguard(false);
1341 ActivityOptions.abort(options);
1342 return ActivityManager.START_SWITCHES_CANCELED;
1343 }
1344 }
1345
1346 if (mService.mDidAppSwitch) {
1347 // This is the second allowed switch since we stopped switches,
1348 // so now just generally allow switches. Use case: user presses
1349 // home (switches disabled, switch to home, mDidAppSwitch now true);
1350 // user taps a home icon (coming from home so allowed, we hit here
1351 // and now allow anyone to switch again).
1352 mService.mAppSwitchesAllowedTime = 0;
1353 } else {
1354 mService.mDidAppSwitch = true;
1355 }
1356
1357 mService.doPendingActivityLaunchesLocked(false);
1358
Dianne Hackborn91097de2014-04-04 18:02:06 -07001359 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
1360 startFlags, true, options);
Craig Mautner10385a12013-09-22 21:08:32 -07001361
1362 if (allPausedActivitiesComplete()) {
1363 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001364 // activity start, but we are not actually doing an activity
1365 // switch... just dismiss the keyguard now, because we
1366 // probably want to see whatever is behind it.
1367 dismissKeyguard();
1368 }
1369 return err;
1370 }
1371
Craig Mautnerac6f8432013-07-17 13:24:59 -07001372 ActivityStack adjustStackFocus(ActivityRecord r) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001373 final TaskRecord task = r.task;
1374 if (r.isApplicationActivity() || (task != null && task.isApplicationTask())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001375 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001376 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001377 if (taskStack.isOnHomeDisplay()) {
1378 if (mFocusedStack != taskStack) {
1379 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1380 "focused stack to r=" + r + " task=" + task);
1381 mFocusedStack = taskStack;
1382 } else {
1383 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1384 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1385 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001386 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001387 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001388 }
1389
Craig Mautnere0a38842013-12-16 16:14:02 -08001390 final ActivityContainer container = r.mInitialActivityContainer;
1391 if (container != null) {
1392 // The first time put it on the desired stack, after this put on task stack.
1393 r.mInitialActivityContainer = null;
1394 return container.mStack;
1395 }
1396
Craig Mautner4a1cb222013-12-04 16:14:06 -08001397 if (mFocusedStack != mHomeStack) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001398 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1399 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1400 return mFocusedStack;
1401 }
1402
Craig Mautnere0a38842013-12-16 16:14:02 -08001403 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1404 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1405 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1406 if (!stack.isHomeStack()) {
1407 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1408 "adjustStackFocus: Setting focused stack=" + stack);
1409 mFocusedStack = stack;
1410 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001411 }
1412 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001413
Craig Mautner4a1cb222013-12-04 16:14:06 -08001414 // Need to create an app stack for this user.
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001415 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001416 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1417 " stackId=" + stackId);
1418 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001419 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001420 }
1421 return mHomeStack;
1422 }
1423
Craig Mautner29219d92013-04-16 20:19:12 -07001424 void setFocusedStack(ActivityRecord r) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001425 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001426 final TaskRecord task = r.task;
1427 boolean isHomeActivity = !r.isApplicationActivity();
1428 if (!isHomeActivity && task != null) {
1429 isHomeActivity = !task.isApplicationTask();
1430 }
1431 if (!isHomeActivity && task != null) {
1432 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1433 isHomeActivity = parent != null && parent.isHomeActivity();
1434 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001435 moveHomeStack(isHomeActivity);
Craig Mautner29219d92013-04-16 20:19:12 -07001436 }
1437 }
1438
Craig Mautner8849a5e2013-04-02 16:41:03 -07001439 final int startActivityUncheckedLocked(ActivityRecord r,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001440 ActivityRecord sourceRecord,
1441 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
1442 boolean doResume, Bundle options) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001443 final Intent intent = r.intent;
1444 final int callingUid = r.launchedFromUid;
1445
1446 int launchFlags = intent.getFlags();
1447
Craig Mautner8849a5e2013-04-02 16:41:03 -07001448 // We'll invoke onUserLeaving before onPause only if the launching
1449 // activity did not explicitly state that this is an automated launch.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001450 mUserLeaving = (launchFlags&Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
1451 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001452
1453 // If the caller has asked not to resume at this point, we make note
1454 // of this in the record so that we can skip it when trying to find
1455 // the top running activity.
1456 if (!doResume) {
1457 r.delayedResume = true;
1458 }
1459
1460 ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
1461
1462 // If the onlyIfNeeded flag is set, then we can do this if the activity
1463 // being launched is the same as the one making the call... or, as
1464 // a special case, if we do not know the caller then we count the
1465 // current top activity as the caller.
1466 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1467 ActivityRecord checkedCaller = sourceRecord;
1468 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001469 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001470 }
1471 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1472 // Caller is not the same as launcher, so always needed.
1473 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1474 }
1475 }
1476
Craig Mautnerd00f4742014-03-12 14:17:26 -07001477 final boolean newDocument = intent.isDocument();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001478 if (sourceRecord == null) {
1479 // This activity is not being started from another... in this
1480 // case we -always- start a new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001481 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Craig Mautner29219d92013-04-16 20:19:12 -07001482 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1483 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001484 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1485 }
Craig Mautnerd00f4742014-03-12 14:17:26 -07001486 } else if (newDocument) {
1487 if (r.launchMode != ActivityInfo.LAUNCH_MULTIPLE) {
1488 Slog.w(TAG, "FLAG_ACTIVITY_NEW_DOCUMENT and launchMode != \"standard\"");
1489 r.launchMode = ActivityInfo.LAUNCH_MULTIPLE;
1490 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001491 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1492 // The original activity who is starting us is running as a single
1493 // instance... this new activity it is starting must go on its
1494 // own task.
1495 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1496 } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
1497 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
1498 // The activity being started is a single instance... it always
1499 // gets launched into its own task.
1500 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1501 }
1502
Craig Mautner88629292013-11-10 20:39:05 -08001503 ActivityInfo newTaskInfo = null;
1504 Intent newTaskIntent = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001505 final ActivityStack sourceStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001506 if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001507 if (sourceRecord.finishing) {
1508 // If the source is finishing, we can't further count it as our source. This
1509 // is because the task it is associated with may now be empty and on its way out,
1510 // so we don't want to blindly throw it in to that task. Instead we will take
Craig Mautner88629292013-11-10 20:39:05 -08001511 // the NEW_TASK flow and try to find a task for it. But save the task information
1512 // so it can be used when creating the new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001513 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001514 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1515 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1516 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
Craig Mautner88629292013-11-10 20:39:05 -08001517 newTaskInfo = sourceRecord.info;
1518 newTaskIntent = sourceRecord.task.intent;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001519 }
1520 sourceRecord = null;
1521 sourceStack = null;
1522 } else {
1523 sourceStack = sourceRecord.task.stack;
1524 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001525 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001526 sourceStack = null;
1527 }
1528
Craig Mautnerd00f4742014-03-12 14:17:26 -07001529 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001530 // For whatever reason this activity is being launched into a new
1531 // task... yet the caller has requested a result back. Well, that
1532 // is pretty messed up, so instead immediately send back a cancel
1533 // and let the new task continue launched as normal without a
1534 // dependency on its originator.
1535 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1536 r.resultTo.task.stack.sendActivityResultLocked(-1,
1537 r.resultTo, r.resultWho, r.requestCode,
1538 Activity.RESULT_CANCELED, null);
1539 r.resultTo = null;
1540 }
1541
1542 boolean addingToTask = false;
1543 boolean movedHome = false;
1544 TaskRecord reuseTask = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001545 ActivityStack targetStack;
Craig Mautnerd00f4742014-03-12 14:17:26 -07001546 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1547 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001548 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
1549 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1550 // If bring to front is requested, and no result is requested, and
1551 // we can find a task that was started with this same
1552 // component, then instead of launching bring that one to the front.
1553 if (r.resultTo == null) {
1554 // See if there is a task to bring to the front. If this is
1555 // a SINGLE_INSTANCE activity, there can be one and only one
1556 // instance of it in the history, and it is always in its own
1557 // unique task, so we do a special search.
1558 ActivityRecord intentActivity = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
Craig Mautnerac6f8432013-07-17 13:24:59 -07001559 ? findTaskLocked(r)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001560 : findActivityLocked(intent, r.info);
1561 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001562 if (isLockTaskModeViolation(intentActivity.task)) {
1563 Slog.e(TAG, "moveTaskToFront: Attempt to violate Lock Task Mode");
1564 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1565 }
Craig Mautner29219d92013-04-16 20:19:12 -07001566 if (r.task == null) {
1567 r.task = intentActivity.task;
1568 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001569 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001570 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001571 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1572 + " from " + intentActivity);
Craig Mautnere0a38842013-12-16 16:14:02 -08001573 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001574 if (intentActivity.task.intent == null) {
1575 // This task was started because of movement of
1576 // the activity based on affinity... now that we
1577 // are actually launching it, we can assign the
1578 // base intent.
1579 intentActivity.task.setIntent(intent, r.info);
1580 }
1581 // If the target task is not in the front, then we need
1582 // to bring it to the front... except... well, with
1583 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1584 // to have the same behavior as if a new instance was
1585 // being started, which means not bringing it to the front
1586 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001587 final ActivityStack lastStack = getLastStack();
1588 ActivityRecord curTop = lastStack == null?
1589 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner7504d7b2013-09-17 10:50:53 -07001590 if (curTop != null && (curTop.task != intentActivity.task ||
1591 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001592 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001593 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1594 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001595 // We really do want to push this one into the
1596 // user's face, right now.
1597 movedHome = true;
Craig Mautnerb53d97c2013-10-25 11:54:37 -07001598 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001599 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001600 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1601 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001602 // Caller wants to appear on home activity.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001603 intentActivity.task.mOnTopOfHome = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001604 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001605 options = null;
1606 }
1607 }
1608 // If the caller has requested that the target task be
1609 // reset, then do so.
1610 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1611 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1612 }
1613 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1614 // We don't need to start a new activity, and
1615 // the client said not to do anything if that
1616 // is the case, so this is it! And for paranoia, make
1617 // sure we have correctly resumed the top activity.
1618 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001619 resumeTopActivitiesLocked(targetStack, null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001620 } else {
1621 ActivityOptions.abort(options);
1622 }
1623 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1624 }
1625 if ((launchFlags &
1626 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1627 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1628 // The caller has requested to completely replace any
1629 // existing task with its new activity. Well that should
1630 // not be too hard...
1631 reuseTask = intentActivity.task;
1632 reuseTask.performClearTaskLocked();
1633 reuseTask.setIntent(r.intent, r.info);
1634 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
1635 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
1636 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1637 // In this situation we want to remove all activities
1638 // from the task up to the one being started. In most
1639 // cases this means we are resetting the task to its
1640 // initial state.
1641 ActivityRecord top =
1642 intentActivity.task.performClearTaskLocked(r, launchFlags);
1643 if (top != null) {
1644 if (top.frontOfTask) {
1645 // Activity aliases may mean we use different
1646 // intents for the top activity, so make sure
1647 // the task now has the identity of the new
1648 // intent.
1649 top.task.setIntent(r.intent, r.info);
1650 }
1651 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1652 r, top.task);
1653 top.deliverNewIntentLocked(callingUid, r.intent);
1654 } else {
1655 // A special case: we need to
1656 // start the activity because it is not currently
1657 // running, and the caller has asked to clear the
1658 // current task to have this activity at the top.
1659 addingToTask = true;
1660 // Now pretend like this activity is being started
1661 // by the top of its task, so it is put in the
1662 // right place.
1663 sourceRecord = intentActivity;
1664 }
1665 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1666 // In this case the top activity on the task is the
1667 // same as the one being launched, so we take that
1668 // as a request to bring the task to the foreground.
1669 // If the top activity in the task is the root
1670 // activity, deliver this new intent to it if it
1671 // desires.
1672 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
1673 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
1674 && intentActivity.realActivity.equals(r.realActivity)) {
1675 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1676 intentActivity.task);
1677 if (intentActivity.frontOfTask) {
1678 intentActivity.task.setIntent(r.intent, r.info);
1679 }
1680 intentActivity.deliverNewIntentLocked(callingUid, r.intent);
1681 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1682 // In this case we are launching the root activity
1683 // of the task, but with a different intent. We
1684 // should start a new instance on top.
1685 addingToTask = true;
1686 sourceRecord = intentActivity;
1687 }
1688 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1689 // In this case an activity is being launched in to an
1690 // existing task, without resetting that task. This
1691 // is typically the situation of launching an activity
1692 // from a notification or shortcut. We want to place
1693 // the new activity on top of the current task.
1694 addingToTask = true;
1695 sourceRecord = intentActivity;
1696 } else if (!intentActivity.task.rootWasReset) {
1697 // In this case we are launching in to an existing task
1698 // that has not yet been started from its front door.
1699 // The current task has been brought to the front.
1700 // Ideally, we'd probably like to place this new task
1701 // at the bottom of its stack, but that's a little hard
1702 // to do with the current organization of the code so
1703 // for now we'll just drop it.
1704 intentActivity.task.setIntent(r.intent, r.info);
1705 }
1706 if (!addingToTask && reuseTask == null) {
1707 // We didn't do anything... but it was needed (a.k.a., client
1708 // don't use that intent!) And for paranoia, make
1709 // sure we have correctly resumed the top activity.
1710 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001711 targetStack.resumeTopActivityLocked(null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001712 } else {
1713 ActivityOptions.abort(options);
1714 }
1715 return ActivityManager.START_TASK_TO_FRONT;
1716 }
1717 }
1718 }
1719 }
1720
1721 //String uri = r.intent.toURI();
1722 //Intent intent2 = new Intent(uri);
1723 //Slog.i(TAG, "Given intent: " + r.intent);
1724 //Slog.i(TAG, "URI is: " + uri);
1725 //Slog.i(TAG, "To intent: " + intent2);
1726
1727 if (r.packageName != null) {
1728 // If the activity being launched is the same as the one currently
1729 // at the top, then we need to check if it should only be launched
1730 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001731 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001732 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001733 if (top != null && r.resultTo == null) {
1734 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
1735 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07001736 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautner8849a5e2013-04-02 16:41:03 -07001737 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP
1738 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
1739 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
1740 top.task);
1741 // For paranoia, make sure we have correctly
1742 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001743 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001744 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001745 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001746 }
1747 ActivityOptions.abort(options);
1748 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1749 // We don't need to start a new activity, and
1750 // the client said not to do anything if that
1751 // is the case, so this is it!
1752 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1753 }
1754 top.deliverNewIntentLocked(callingUid, r.intent);
1755 return ActivityManager.START_DELIVERED_TO_TOP;
1756 }
1757 }
1758 }
1759 }
1760
1761 } else {
1762 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001763 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
1764 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001765 }
1766 ActivityOptions.abort(options);
1767 return ActivityManager.START_CLASS_NOT_FOUND;
1768 }
1769
1770 boolean newTask = false;
1771 boolean keepCurTransition = false;
1772
1773 // Should this be considered a new task?
1774 if (r.resultTo == null && !addingToTask
1775 && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001776 if (isLockTaskModeViolation(reuseTask)) {
1777 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1778 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1779 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001780 targetStack = adjustStackFocus(r);
Craig Mautnere0a38842013-12-16 16:14:02 -08001781 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001782 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08001783 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
1784 newTaskInfo != null ? newTaskInfo : r.info,
1785 newTaskIntent != null ? newTaskIntent : intent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001786 voiceSession, voiceInteractor, true), null, true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001787 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
1788 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001789 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001790 r.setTask(reuseTask, reuseTask, true);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001791 }
1792 newTask = true;
1793 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001794 if ((launchFlags &
1795 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
1796 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
1797 // Caller wants to appear on home activity, so before starting
1798 // their own activity we will bring home to the front.
Craig Mautnere0a38842013-12-16 16:14:02 -08001799 r.task.mOnTopOfHome = r.task.stack.isOnHomeDisplay();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001800 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001801 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001802 } else if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001803 TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08001804 if (isLockTaskModeViolation(sourceTask)) {
1805 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1806 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1807 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001808 targetStack = sourceTask.stack;
Craig Mautnerdd221b32014-03-19 19:53:45 -07001809 targetStack.moveToFront();
Craig Mautner95e9daa2014-03-17 15:57:20 -07001810 mWindowManager.moveTaskToTop(sourceTask.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001811 if (!addingToTask &&
1812 (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
1813 // In this case, we are adding the activity to an existing
1814 // task, but the caller has asked to clear that task if the
1815 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07001816 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001817 keepCurTransition = true;
1818 if (top != null) {
1819 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
1820 top.deliverNewIntentLocked(callingUid, r.intent);
1821 // For paranoia, make sure we have correctly
1822 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001823 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001824 if (doResume) {
1825 targetStack.resumeTopActivityLocked(null);
1826 }
1827 ActivityOptions.abort(options);
1828 return ActivityManager.START_DELIVERED_TO_TOP;
1829 }
1830 } else if (!addingToTask &&
1831 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
1832 // In this case, we are launching an activity in our own task
1833 // that may already be running somewhere in the history, and
1834 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07001835 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001836 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001837 final TaskRecord task = top.task;
1838 task.moveActivityToFrontLocked(top);
1839 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001840 top.updateOptionsLocked(options);
1841 top.deliverNewIntentLocked(callingUid, r.intent);
Craig Mautner0f922742013-08-06 08:44:42 -07001842 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001843 if (doResume) {
1844 targetStack.resumeTopActivityLocked(null);
1845 }
1846 return ActivityManager.START_DELIVERED_TO_TOP;
1847 }
1848 }
1849 // An existing activity is starting this new activity, so we want
1850 // to keep the new one in the same task as the one that is starting
1851 // it.
Craig Mautner525f3d92013-05-07 14:01:50 -07001852 r.setTask(sourceTask, sourceRecord.thumbHolder, false);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001853 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001854 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001855
1856 } else {
1857 // This not being started from an existing activity, and not part
1858 // of a new task... just put it in the top task, though these days
1859 // this case should never happen.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001860 targetStack = adjustStackFocus(r);
Craig Mautnerdd221b32014-03-19 19:53:45 -07001861 targetStack.moveToFront();
Craig Mautner1602ec22013-05-12 10:24:27 -07001862 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001863 r.setTask(prev != null ? prev.task
Dianne Hackborn91097de2014-04-04 18:02:06 -07001864 : targetStack.createTaskRecord(getNextTaskId(), r.info, intent, null, null, true),
Craig Mautnerde4ef022013-04-07 19:01:33 -07001865 null, true);
Craig Mautner95e9daa2014-03-17 15:57:20 -07001866 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001867 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
1868 + " in new guessed " + r.task);
1869 }
1870
1871 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
1872 intent, r.getUriPermissionsLocked());
1873
1874 if (newTask) {
1875 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
1876 }
1877 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07001878 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001879 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautner1d001b62013-06-18 16:52:43 -07001880 mService.setFocusedActivityLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001881 return ActivityManager.START_SUCCESS;
1882 }
1883
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001884 void acquireLaunchWakelock() {
1885 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1886 throw new IllegalStateException("Calling must be system uid");
1887 }
1888 mLaunchingActivity.acquire();
1889 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1890 // To be safe, don't allow the wake lock to be held for too long.
1891 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1892 }
1893 }
1894
Craig Mautnerf3333272013-04-22 10:55:53 -07001895 // Checked.
1896 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1897 Configuration config) {
1898 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
1899
Craig Mautnerf3333272013-04-22 10:55:53 -07001900 ArrayList<ActivityRecord> stops = null;
1901 ArrayList<ActivityRecord> finishes = null;
1902 ArrayList<UserStartedState> startingUsers = null;
1903 int NS = 0;
1904 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001905 boolean booting = false;
1906 boolean enableScreen = false;
1907 boolean activityRemoved = false;
1908
1909 ActivityRecord r = ActivityRecord.forToken(token);
1910 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001911 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
1912 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001913 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1914 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001915 if (fromTimeout) {
1916 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001917 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001918
1919 // This is a hack to semi-deal with a race condition
1920 // in the client where it can be constructed with a
1921 // newer configuration from when we asked it to launch.
1922 // We'll update with whatever configuration it now says
1923 // it used to launch.
1924 if (config != null) {
1925 r.configuration = config;
1926 }
1927
1928 // We are now idle. If someone is waiting for a thumbnail from
1929 // us, we can now deliver.
1930 r.idle = true;
1931
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001932 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
1933 if (!mService.mBooted && isFrontStack(r.task.stack)) {
1934 mService.mBooted = true;
1935 enableScreen = true;
1936 }
1937 }
1938
1939 if (allResumedActivitiesIdle()) {
1940 if (r != null) {
1941 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001942 }
1943
1944 if (mLaunchingActivity.isHeld()) {
1945 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1946 if (VALIDATE_WAKE_LOCK_CALLER &&
1947 Binder.getCallingUid() != Process.myUid()) {
1948 throw new IllegalStateException("Calling must be system uid");
1949 }
1950 mLaunchingActivity.release();
1951 }
1952 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07001953 }
1954
1955 // Atomically retrieve all of the other things to do.
1956 stops = processStoppingActivitiesLocked(true);
1957 NS = stops != null ? stops.size() : 0;
1958 if ((NF=mFinishingActivities.size()) > 0) {
1959 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
1960 mFinishingActivities.clear();
1961 }
1962
Craig Mautnerf3333272013-04-22 10:55:53 -07001963 if (isFrontStack(mHomeStack)) {
1964 booting = mService.mBooting;
1965 mService.mBooting = false;
1966 }
1967
1968 if (mStartingUsers.size() > 0) {
1969 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
1970 mStartingUsers.clear();
1971 }
1972
Craig Mautnerf3333272013-04-22 10:55:53 -07001973 // Stop any activities that are scheduled to do so but have been
1974 // waiting for the next one to start.
1975 for (int i = 0; i < NS; i++) {
1976 r = stops.get(i);
1977 final ActivityStack stack = r.task.stack;
1978 if (r.finishing) {
1979 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1980 } else {
1981 stack.stopActivityLocked(r);
1982 }
1983 }
1984
1985 // Finish any activities that are scheduled to do so but have been
1986 // waiting for the next one to start.
1987 for (int i = 0; i < NF; i++) {
1988 r = finishes.get(i);
1989 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, false, "finish-idle");
1990 }
1991
1992 if (booting) {
1993 mService.finishBooting();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001994 } else {
1995 // Complete user switch
1996 if (startingUsers != null) {
1997 for (int i = 0; i < startingUsers.size(); i++) {
1998 mService.finishUserSwitch(startingUsers.get(i));
1999 }
2000 }
2001 // Complete starting up of background users
2002 if (mStartingBackgroundUsers.size() > 0) {
2003 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2004 mStartingBackgroundUsers.clear();
2005 for (int i = 0; i < startingUsers.size(); i++) {
2006 mService.finishUserBoot(startingUsers.get(i));
2007 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002008 }
2009 }
2010
2011 mService.trimApplications();
2012 //dump();
2013 //mWindowManager.dump();
2014
2015 if (enableScreen) {
2016 mService.enableScreenAfterBoot();
2017 }
2018
2019 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002020 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002021 }
2022
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002023 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002024 }
2025
Craig Mautner8e569572013-10-11 17:36:59 -07002026 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002027 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002028 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2029 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002030 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2031 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2032 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002033 }
Craig Mautner19091252013-10-05 00:03:53 -07002034 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002035 }
2036
2037 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002038 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2039 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002040 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2041 stacks.get(stackNdx).closeSystemDialogsLocked();
2042 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002043 }
2044 }
2045
Craig Mautner93529a42013-10-04 15:03:13 -07002046 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002047 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002048 }
2049
Craig Mautner8d341ef2013-03-26 09:03:27 -07002050 /**
2051 * @return true if some activity was finished (or would have finished if doit were true).
2052 */
2053 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2054 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002055 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2056 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002057 final int numStacks = stacks.size();
2058 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2059 final ActivityStack stack = stacks.get(stackNdx);
2060 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2061 didSomething = true;
2062 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002063 }
2064 }
2065 return didSomething;
2066 }
2067
Dianne Hackborna413dc02013-07-12 12:02:55 -07002068 void updatePreviousProcessLocked(ActivityRecord r) {
2069 // Now that this process has stopped, we may want to consider
2070 // it to be the previous app to try to keep around in case
2071 // the user wants to return to it.
2072
2073 // First, found out what is currently the foreground app, so that
2074 // we don't blow away the previous app if this activity is being
2075 // hosted by the process that is actually still the foreground.
2076 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002077 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2078 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002079 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2080 final ActivityStack stack = stacks.get(stackNdx);
2081 if (isFrontStack(stack)) {
2082 if (stack.mResumedActivity != null) {
2083 fgApp = stack.mResumedActivity.app;
2084 } else if (stack.mPausingActivity != null) {
2085 fgApp = stack.mPausingActivity.app;
2086 }
2087 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002088 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002089 }
2090 }
2091
2092 // Now set this one as the previous process, only if that really
2093 // makes sense to.
2094 if (r.app != null && fgApp != null && r.app != fgApp
2095 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002096 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002097 mService.mPreviousProcess = r.app;
2098 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2099 }
2100 }
2101
Craig Mautner05d29032013-05-03 13:40:13 -07002102 boolean resumeTopActivitiesLocked() {
2103 return resumeTopActivitiesLocked(null, null, null);
2104 }
2105
2106 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2107 Bundle targetOptions) {
2108 if (targetStack == null) {
2109 targetStack = getFocusedStack();
2110 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002111 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002112 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002113 if (isFrontStack(targetStack)) {
2114 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2115 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002116 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2117 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002118 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2119 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002120 if (stack == targetStack) {
2121 // Already started above.
2122 continue;
2123 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002124 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002125 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002126 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002127 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002128 }
Craig Mautner05d29032013-05-03 13:40:13 -07002129 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002130 }
2131
2132 void finishTopRunningActivityLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002133 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2134 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002135 final int numStacks = stacks.size();
2136 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2137 final ActivityStack stack = stacks.get(stackNdx);
2138 stack.finishTopRunningActivityLocked(app);
2139 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002140 }
2141 }
2142
Craig Mautneraea74a52014-03-08 14:23:10 -08002143 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options) {
2144 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2145 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002146 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002147 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2148 // Caller wants the home activity moved with it. To accomplish this,
2149 // we'll just indicate that this task returns to the home task.
2150 task.mOnTopOfHome = true;
2151 }
2152 task.stack.moveTaskToFrontLocked(task, null, options);
2153 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2154 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002155 }
2156
Craig Mautner967212c2013-04-13 21:10:58 -07002157 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002158 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2159 if (activityContainer != null) {
2160 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002161 }
2162 return null;
2163 }
2164
Craig Mautner967212c2013-04-13 21:10:58 -07002165 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002166 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002167 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2168 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002169 }
2170 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002171 }
2172
Craig Mautner4a1cb222013-12-04 16:14:06 -08002173 IBinder getHomeActivityToken() {
2174 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2175 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2176 final TaskRecord task = tasks.get(taskNdx);
2177 if (task.isHomeTask()) {
2178 final ArrayList<ActivityRecord> activities = task.mActivities;
2179 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2180 final ActivityRecord r = activities.get(activityNdx);
2181 if (r.isHomeActivity()) {
2182 return r.appToken;
2183 }
2184 }
2185 }
2186 }
2187 return null;
2188 }
2189
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002190 ActivityContainer createActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002191 IActivityContainerCallback callback) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002192 ActivityContainer activityContainer = new VirtualActivityContainer(parentActivity, callback);
2193 mActivityContainers.put(activityContainer.mStackId, activityContainer);
2194 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002195 return activityContainer;
2196 }
2197
Craig Mautner34b73df2014-01-12 21:11:08 -08002198 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002199 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2200 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2201 ActivityContainer container = childStacks.remove(containerNdx);
2202 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002203 }
2204 }
2205
Craig Mautner95da1082014-02-24 17:54:35 -08002206 void deleteActivityContainer(IActivityContainer container) {
2207 ActivityContainer activityContainer = (ActivityContainer)container;
2208 if (activityContainer != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002209 activityContainer.mStack.finishAllActivitiesLocked();
Craig Mautner95da1082014-02-24 17:54:35 -08002210 final ActivityRecord parent = activityContainer.mParentActivity;
2211 if (parent != null) {
2212 parent.mChildContainers.remove(activityContainer);
2213 }
2214 final int stackId = activityContainer.mStackId;
2215 mActivityContainers.remove(stackId);
2216 mWindowManager.removeStack(stackId);
2217 }
2218 }
2219
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002220 private int createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002221 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2222 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002223 return -1;
2224 }
2225
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002226 ActivityContainer activityContainer = new ActivityContainer(stackId);
2227 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002228 activityContainer.attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002229 return stackId;
2230 }
2231
2232 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002233 while (true) {
2234 if (++mLastStackId <= HOME_STACK_ID) {
2235 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002236 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002237 if (getStack(mLastStackId) == null) {
2238 break;
2239 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002240 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002241 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002242 }
2243
2244 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002245 final TaskRecord task = anyTaskForIdLocked(taskId);
2246 if (task == null) {
2247 return;
2248 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002249 final ActivityStack stack = getStack(stackId);
2250 if (stack == null) {
2251 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2252 return;
2253 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002254 task.stack.removeTask(task, true);
2255 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002256 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002257 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002258 }
2259
Craig Mautnerac6f8432013-07-17 13:24:59 -07002260 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002261 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002262 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2263 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002264 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2265 final ActivityStack stack = stacks.get(stackNdx);
2266 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
2267 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: " + stack);
2268 continue;
2269 }
2270 final ActivityRecord ar = stack.findTaskLocked(r);
2271 if (ar != null) {
2272 return ar;
2273 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002274 }
2275 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002276 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002277 return null;
2278 }
2279
2280 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002281 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2282 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002283 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2284 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2285 if (ar != null) {
2286 return ar;
2287 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002288 }
2289 }
2290 return null;
2291 }
2292
Craig Mautner8d341ef2013-03-26 09:03:27 -07002293 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002294 scheduleSleepTimeout();
2295 if (!mGoingToSleep.isHeld()) {
2296 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002297 if (mLaunchingActivity.isHeld()) {
2298 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2299 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002300 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002301 mLaunchingActivity.release();
2302 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002303 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002304 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002305 checkReadyForSleepLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08002306 setLockTaskModeLocked(null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002307 }
2308
2309 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002310 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002311
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002312 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002313 final long endTime = System.currentTimeMillis() + timeout;
2314 while (true) {
2315 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002316 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2317 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002318 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2319 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2320 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002321 }
2322 if (cantShutdown) {
2323 long timeRemaining = endTime - System.currentTimeMillis();
2324 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002325 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002326 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002327 } catch (InterruptedException e) {
2328 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002329 } else {
2330 Slog.w(TAG, "Activity manager shutdown timed out");
2331 timedout = true;
2332 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002333 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002334 } else {
2335 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002336 }
2337 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002338
2339 // Force checkReadyForSleep to complete.
2340 mSleepTimeout = true;
2341 checkReadyForSleepLocked();
2342
Craig Mautner8d341ef2013-03-26 09:03:27 -07002343 return timedout;
2344 }
2345
2346 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002347 removeSleepTimeouts();
2348 if (mGoingToSleep.isHeld()) {
2349 mGoingToSleep.release();
2350 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002351 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2352 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002353 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2354 final ActivityStack stack = stacks.get(stackNdx);
2355 stack.awakeFromSleepingLocked();
2356 if (isFrontStack(stack)) {
2357 resumeTopActivitiesLocked();
2358 }
Craig Mautner5314a402013-09-26 12:40:16 -07002359 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002360 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002361 mGoingToSleepActivities.clear();
2362 }
2363
2364 void activitySleptLocked(ActivityRecord r) {
2365 mGoingToSleepActivities.remove(r);
2366 checkReadyForSleepLocked();
2367 }
2368
2369 void checkReadyForSleepLocked() {
2370 if (!mService.isSleepingOrShuttingDown()) {
2371 // Do not care.
2372 return;
2373 }
2374
2375 if (!mSleepTimeout) {
2376 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002377 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2378 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002379 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2380 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2381 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002382 }
2383
2384 if (mStoppingActivities.size() > 0) {
2385 // Still need to tell some activities to stop; can't sleep yet.
2386 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2387 + mStoppingActivities.size() + " activities");
2388 scheduleIdleLocked();
2389 dontSleep = true;
2390 }
2391
2392 if (mGoingToSleepActivities.size() > 0) {
2393 // Still need to tell some activities to sleep; can't sleep yet.
2394 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2395 + mGoingToSleepActivities.size() + " activities");
2396 dontSleep = true;
2397 }
2398
2399 if (dontSleep) {
2400 return;
2401 }
2402 }
2403
Craig Mautnere0a38842013-12-16 16:14:02 -08002404 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2405 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002406 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2407 stacks.get(stackNdx).goToSleep();
2408 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002409 }
2410
2411 removeSleepTimeouts();
2412
2413 if (mGoingToSleep.isHeld()) {
2414 mGoingToSleep.release();
2415 }
2416 if (mService.mShuttingDown) {
2417 mService.notifyAll();
2418 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002419 }
2420
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002421 boolean reportResumedActivityLocked(ActivityRecord r) {
2422 final ActivityStack stack = r.task.stack;
2423 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002424 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002425 }
2426 if (allResumedActivitiesComplete()) {
2427 ensureActivitiesVisibleLocked(null, 0);
2428 mWindowManager.executeAppTransition();
2429 return true;
2430 }
2431 return false;
2432 }
2433
Craig Mautner8d341ef2013-03-26 09:03:27 -07002434 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002435 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2436 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002437 final int numStacks = stacks.size();
2438 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2439 final ActivityStack stack = stacks.get(stackNdx);
2440 stack.handleAppCrashLocked(app);
2441 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002442 }
2443 }
2444
Craig Mautnerde4ef022013-04-07 19:01:33 -07002445 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002446 // First the front stacks. In case any are not fullscreen and are in front of home.
2447 boolean showHomeBehindStack = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002448 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2449 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002450 final int topStackNdx = stacks.size() - 1;
2451 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2452 final ActivityStack stack = stacks.get(stackNdx);
2453 if (stackNdx == topStackNdx) {
2454 // Top stack.
2455 showHomeBehindStack =
2456 stack.ensureActivitiesVisibleLocked(starting, configChanges);
2457 } else {
2458 // Back stack.
2459 stack.ensureActivitiesVisibleLocked(starting, configChanges,
2460 showHomeBehindStack);
2461 }
Craig Mautner580ea812013-04-25 12:58:38 -07002462 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002463 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002464 }
2465
2466 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002467 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2468 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002469 final int numStacks = stacks.size();
2470 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2471 final ActivityStack stack = stacks.get(stackNdx);
2472 stack.scheduleDestroyActivities(app, false, reason);
2473 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002474 }
2475 }
2476
2477 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002478 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
2479 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002480 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002481
Craig Mautner858d8a62013-04-23 17:08:34 -07002482 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002483 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2484 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002485 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002486 final ActivityStack stack = stacks.get(stackNdx);
2487 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002488 TaskRecord task = stack.topTask();
2489 if (task != null) {
2490 mWindowManager.moveTaskToTop(task.taskId);
2491 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002492 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002493 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002494
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002495 ActivityStack stack = getStack(restoreStackId);
2496 if (stack == null) {
2497 stack = mHomeStack;
2498 }
2499 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002500 if (stack.isOnHomeDisplay()) {
2501 moveHomeStack(homeInFront);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002502 TaskRecord task = stack.topTask();
2503 if (task != null) {
2504 mWindowManager.moveTaskToTop(task.taskId);
2505 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002506 } else {
2507 // Stack was moved to another display while user was swapped out.
2508 resumeHomeActivity(null);
2509 }
Craig Mautner93529a42013-10-04 15:03:13 -07002510 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002511 }
2512
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002513 /**
2514 * Add background users to send boot completed events to.
2515 * @param userId The user being started in the background
2516 * @param uss The state object for the user.
2517 */
2518 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
2519 mStartingBackgroundUsers.add(uss);
2520 }
2521
Craig Mautnerde4ef022013-04-07 19:01:33 -07002522 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
2523 int N = mStoppingActivities.size();
2524 if (N <= 0) return null;
2525
2526 ArrayList<ActivityRecord> stops = null;
2527
2528 final boolean nowVisible = allResumedActivitiesVisible();
2529 for (int i=0; i<N; i++) {
2530 ActivityRecord s = mStoppingActivities.get(i);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07002531 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
Craig Mautnerde4ef022013-04-07 19:01:33 -07002532 + nowVisible + " waitingVisible=" + s.waitingVisible
2533 + " finishing=" + s.finishing);
2534 if (s.waitingVisible && nowVisible) {
2535 mWaitingVisibleActivities.remove(s);
2536 s.waitingVisible = false;
2537 if (s.finishing) {
2538 // If this activity is finishing, it is sitting on top of
2539 // everyone else but we now know it is no longer needed...
2540 // so get rid of it. Otherwise, we need to go through the
2541 // normal flow and hide it once we determine that it is
2542 // hidden by the activities in front of it.
2543 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002544 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002545 }
2546 }
2547 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
2548 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
2549 if (stops == null) {
2550 stops = new ArrayList<ActivityRecord>();
2551 }
2552 stops.add(s);
2553 mStoppingActivities.remove(i);
2554 N--;
2555 i--;
2556 }
2557 }
2558
2559 return stops;
2560 }
2561
Craig Mautnercf910b02013-04-23 11:23:27 -07002562 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002563 // FIXME
2564/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2565 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07002566 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002567 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07002568 if (isFrontStack(stack)) {
2569 if (r == null) {
2570 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
2571 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002572 final ActivityRecord pausing = stack.mPausingActivity;
2573 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002574 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002575 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002576 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002577 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002578 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002579 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002580 }
2581 }
2582 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002583 final ActivityRecord resumed = stack.mResumedActivity;
2584 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002585 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002586 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002587 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002588 if (r != null && (state == ActivityState.INITIALIZING
2589 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002590 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002591 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002592 }
2593 }
2594 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002595*/
Craig Mautner76ea2242013-05-15 11:40:05 -07002596 }
2597
Craig Mautner27084302013-03-25 08:05:25 -07002598 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002599 pw.print(prefix); pw.print("mDismissKeyguardOnNextActivity=");
Craig Mautner27084302013-03-25 08:05:25 -07002600 pw.println(mDismissKeyguardOnNextActivity);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002601 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002602 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002603 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
2604 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
2605 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002606 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07002607 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002608
Craig Mautner20e72272013-04-01 13:45:53 -07002609 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002610 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002611 }
2612
Dianne Hackborn390517b2013-05-30 15:03:32 -07002613 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2614 boolean needSep, String prefix) {
2615 if (activity != null) {
2616 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2617 if (needSep) {
2618 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002619 }
2620 pw.print(prefix);
2621 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002622 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002623 }
2624 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002625 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002626 }
2627
Craig Mautner8d341ef2013-03-26 09:03:27 -07002628 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2629 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002630 boolean printed = false;
2631 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002632 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2633 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
2634 pw.print("Display #"); pw.println(activityDisplay.mDisplayId);
2635 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002636 final int numStacks = stacks.size();
2637 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2638 final ActivityStack stack = stacks.get(stackNdx);
2639 StringBuilder stackHeader = new StringBuilder(128);
2640 stackHeader.append(" Stack #");
2641 stackHeader.append(stack.mStackId);
2642 stackHeader.append(":");
2643 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2644 needSep, stackHeader.toString());
2645 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2646 !dumpAll, false, dumpPackage, true,
2647 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002648
Craig Mautner4a1cb222013-12-04 16:14:06 -08002649 needSep = printed;
2650 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2651 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002652 if (pr) {
2653 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002654 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002655 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002656 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2657 " mResumedActivity: ");
2658 if (pr) {
2659 printed = true;
2660 needSep = false;
2661 }
2662 if (dumpAll) {
2663 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2664 " mLastPausedActivity: ");
2665 if (pr) {
2666 printed = true;
2667 needSep = true;
2668 }
2669 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2670 needSep, " mLastNoHistoryActivity: ");
2671 }
2672 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002673 }
2674 }
2675
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002676 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2677 false, dumpPackage, true, " Activities waiting to finish:", null);
2678 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2679 false, dumpPackage, true, " Activities waiting to stop:", null);
2680 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2681 false, dumpPackage, true, " Activities waiting for another to become visible:",
2682 null);
2683 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2684 false, dumpPackage, true, " Activities waiting to sleep:", null);
2685 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2686 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002687
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002688 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002689 }
2690
Dianne Hackborn390517b2013-05-30 15:03:32 -07002691 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002692 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002693 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002694 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002695 String innerPrefix = null;
2696 String[] args = null;
2697 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002698 for (int i=list.size()-1; i>=0; i--) {
2699 final ActivityRecord r = list.get(i);
2700 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2701 continue;
2702 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002703 if (innerPrefix == null) {
2704 innerPrefix = prefix + " ";
2705 args = new String[0];
2706 }
2707 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002708 final boolean full = !brief && (complete || !r.isInHistory());
2709 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002710 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002711 needNL = false;
2712 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002713 if (header1 != null) {
2714 pw.println(header1);
2715 header1 = null;
2716 }
2717 if (header2 != null) {
2718 pw.println(header2);
2719 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002720 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002721 if (lastTask != r.task) {
2722 lastTask = r.task;
2723 pw.print(prefix);
2724 pw.print(full ? "* " : " ");
2725 pw.println(lastTask);
2726 if (full) {
2727 lastTask.dump(pw, prefix + " ");
2728 } else if (complete) {
2729 // Complete + brief == give a summary. Isn't that obvious?!?
2730 if (lastTask.intent != null) {
2731 pw.print(prefix); pw.print(" ");
2732 pw.println(lastTask.intent.toInsecureStringWithClip());
2733 }
2734 }
2735 }
2736 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2737 pw.print(" #"); pw.print(i); pw.print(": ");
2738 pw.println(r);
2739 if (full) {
2740 r.dump(pw, innerPrefix);
2741 } else if (complete) {
2742 // Complete + brief == give a summary. Isn't that obvious?!?
2743 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2744 if (r.app != null) {
2745 pw.print(innerPrefix); pw.println(r.app);
2746 }
2747 }
2748 if (client && r.app != null && r.app.thread != null) {
2749 // flush anything that is already in the PrintWriter since the thread is going
2750 // to write to the file descriptor directly
2751 pw.flush();
2752 try {
2753 TransferPipe tp = new TransferPipe();
2754 try {
2755 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
2756 r.appToken, innerPrefix, args);
2757 // Short timeout, since blocking here can
2758 // deadlock with the application.
2759 tp.go(fd, 2000);
2760 } finally {
2761 tp.kill();
2762 }
2763 } catch (IOException e) {
2764 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2765 } catch (RemoteException e) {
2766 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2767 }
2768 needNL = true;
2769 }
2770 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002771 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002772 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002773
Craig Mautnerf3333272013-04-22 10:55:53 -07002774 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002775 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002776 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2777 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002778 }
2779
2780 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002781 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002782 }
2783
2784 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002785 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002786 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2787 }
2788
Craig Mautner05d29032013-05-03 13:40:13 -07002789 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08002790 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
2791 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2792 }
Craig Mautner05d29032013-05-03 13:40:13 -07002793 }
2794
Craig Mautner0eea92c2013-05-16 13:35:39 -07002795 void removeSleepTimeouts() {
2796 mSleepTimeout = false;
2797 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
2798 }
2799
2800 final void scheduleSleepTimeout() {
2801 removeSleepTimeouts();
2802 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
2803 }
2804
Craig Mautner4a1cb222013-12-04 16:14:06 -08002805 @Override
2806 public void onDisplayAdded(int displayId) {
2807 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
2808 }
2809
2810 @Override
2811 public void onDisplayRemoved(int displayId) {
2812 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
2813 }
2814
2815 @Override
2816 public void onDisplayChanged(int displayId) {
2817 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
2818 }
2819
2820 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08002821 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002822 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08002823 newDisplay = mActivityDisplays.get(displayId) == null;
2824 if (newDisplay) {
2825 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
2826 mActivityDisplays.put(displayId, activityDisplay);
2827 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002828 }
Craig Mautner4504de52013-12-20 09:06:56 -08002829 if (newDisplay) {
2830 mWindowManager.onDisplayAdded(displayId);
2831 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002832 }
2833
2834 public void handleDisplayRemovedLocked(int displayId) {
2835 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002836 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2837 if (activityDisplay != null) {
2838 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002839 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002840 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002841 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002842 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002843 }
2844 }
2845 mWindowManager.onDisplayRemoved(displayId);
2846 }
2847
2848 public void handleDisplayChangedLocked(int displayId) {
2849 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002850 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2851 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002852 // TODO: Update the bounds.
2853 }
2854 }
2855 mWindowManager.onDisplayChanged(displayId);
2856 }
2857
2858 StackInfo getStackInfo(ActivityStack stack) {
2859 StackInfo info = new StackInfo();
2860 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
2861 info.displayId = Display.DEFAULT_DISPLAY;
2862 info.stackId = stack.mStackId;
2863
2864 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2865 final int numTasks = tasks.size();
2866 int[] taskIds = new int[numTasks];
2867 String[] taskNames = new String[numTasks];
2868 for (int i = 0; i < numTasks; ++i) {
2869 final TaskRecord task = tasks.get(i);
2870 taskIds[i] = task.taskId;
2871 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
2872 : task.realActivity != null ? task.realActivity.flattenToString()
2873 : task.getTopActivity() != null ? task.getTopActivity().packageName
2874 : "unknown";
2875 }
2876 info.taskIds = taskIds;
2877 info.taskNames = taskNames;
2878 return info;
2879 }
2880
2881 StackInfo getStackInfoLocked(int stackId) {
2882 ActivityStack stack = getStack(stackId);
2883 if (stack != null) {
2884 return getStackInfo(stack);
2885 }
2886 return null;
2887 }
2888
2889 ArrayList<StackInfo> getAllStackInfosLocked() {
2890 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002891 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2892 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002893 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
2894 list.add(getStackInfo(stacks.get(ndx)));
2895 }
2896 }
2897 return list;
2898 }
2899
Craig Mautneraea74a52014-03-08 14:23:10 -08002900 void setLockTaskModeLocked(TaskRecord task) {
2901 if (task == null) {
2902 // Take out of lock task mode.
2903 mLockTaskModeTask = null;
2904 return;
2905 }
2906 if (isLockTaskModeViolation(task)) {
2907 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
2908 return;
2909 }
2910 mLockTaskModeTask = task;
2911 findTaskToMoveToFrontLocked(task, 0, null);
2912 resumeTopActivitiesLocked();
2913 }
2914
2915 boolean isLockTaskModeViolation(TaskRecord task) {
2916 return mLockTaskModeTask != null && mLockTaskModeTask != task;
2917 }
2918
2919 void endLockTaskModeIfTaskEnding(TaskRecord task) {
2920 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
2921 mLockTaskModeTask = null;
2922 }
2923 }
2924
2925 boolean isInLockTaskMode() {
2926 return mLockTaskModeTask != null;
2927 }
2928
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002929 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07002930
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002931 public ActivityStackSupervisorHandler(Looper looper) {
2932 super(looper);
2933 }
2934
Craig Mautnerf3333272013-04-22 10:55:53 -07002935 void activityIdleInternal(ActivityRecord r) {
2936 synchronized (mService) {
2937 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
2938 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002939 }
2940
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002941 @Override
2942 public void handleMessage(Message msg) {
2943 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002944 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07002945 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07002946 if (mService.mDidDexOpt) {
2947 mService.mDidDexOpt = false;
2948 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
2949 nmsg.obj = msg.obj;
2950 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
2951 return;
2952 }
2953 // We don't at this point know if the activity is fullscreen,
2954 // so we need to be conservative and assume it isn't.
2955 activityIdleInternal((ActivityRecord)msg.obj);
2956 } break;
2957 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07002958 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07002959 activityIdleInternal((ActivityRecord)msg.obj);
2960 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07002961 case RESUME_TOP_ACTIVITY_MSG: {
2962 synchronized (mService) {
2963 resumeTopActivitiesLocked();
2964 }
2965 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002966 case SLEEP_TIMEOUT_MSG: {
2967 synchronized (mService) {
2968 if (mService.isSleepingOrShuttingDown()) {
2969 Slog.w(TAG, "Sleep timeout! Sleeping now.");
2970 mSleepTimeout = true;
2971 checkReadyForSleepLocked();
2972 }
2973 }
2974 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002975 case LAUNCH_TIMEOUT_MSG: {
2976 if (mService.mDidDexOpt) {
2977 mService.mDidDexOpt = false;
2978 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2979 return;
2980 }
2981 synchronized (mService) {
2982 if (mLaunchingActivity.isHeld()) {
2983 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
2984 if (VALIDATE_WAKE_LOCK_CALLER
2985 && Binder.getCallingUid() != Process.myUid()) {
2986 throw new IllegalStateException("Calling must be system uid");
2987 }
2988 mLaunchingActivity.release();
2989 }
2990 }
2991 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002992 case HANDLE_DISPLAY_ADDED: {
2993 handleDisplayAddedLocked(msg.arg1);
2994 } break;
2995 case HANDLE_DISPLAY_CHANGED: {
2996 handleDisplayChangedLocked(msg.arg1);
2997 } break;
2998 case HANDLE_DISPLAY_REMOVED: {
2999 handleDisplayRemovedLocked(msg.arg1);
3000 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003001 case CONTAINER_CALLBACK_VISIBILITY: {
3002 final ActivityContainer container = (ActivityContainer) msg.obj;
3003 try {
3004 // We only send this message if mCallback is non-null.
3005 container.mCallback.setVisible(container.asBinder(), msg.arg1 == 1);
3006 } catch (RemoteException e) {
3007 }
3008 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003009 }
3010 }
3011 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003012
Ying Wangb081a592014-04-22 15:20:16 -07003013 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003014 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
3015 Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003016 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003017 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003018 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003019 ActivityRecord mParentActivity = null;
3020 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003021
Craig Mautnere3a00d72014-04-16 08:31:19 -07003022 boolean mVisible = true;
3023
Craig Mautner4a1cb222013-12-04 16:14:06 -08003024 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003025 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003026
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003027 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3028 final static int CONTAINER_STATE_NO_SURFACE = 1;
3029 final static int CONTAINER_STATE_FINISHING = 2;
3030 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3031
3032 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003033 synchronized (mService) {
3034 mStackId = stackId;
3035 mStack = new ActivityStack(this);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003036 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003037 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003038 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003039 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003040
Craig Mautnere0a38842013-12-16 16:14:02 -08003041 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003042 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3043 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003044 mActivityDisplay = activityDisplay;
3045 mStack.mDisplayId = activityDisplay.mDisplayId;
3046 mStack.mStacks = activityDisplay.mStacks;
3047
3048 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003049 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003050 }
3051
3052 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003053 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003054 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003055 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3056 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003057 return;
3058 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003059 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003060 }
3061 }
3062
3063 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003064 public int getDisplayId() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003065 if (mActivityDisplay != null) {
3066 return mActivityDisplay.mDisplayId;
3067 }
3068 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003069 }
3070
Jeff Brownca9bc702014-02-11 14:32:56 -08003071 @Override
3072 public boolean injectEvent(InputEvent event) {
3073 final long origId = Binder.clearCallingIdentity();
3074 try {
3075 if (mActivityDisplay != null) {
3076 return mInputManagerInternal.injectInputEvent(event,
3077 mActivityDisplay.mDisplayId,
3078 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3079 }
3080 return false;
3081 } finally {
3082 Binder.restoreCallingIdentity(origId);
3083 }
3084 }
3085
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003086 @Override
3087 public void release() {
3088 mContainerState = CONTAINER_STATE_FINISHING;
3089 mStack.finishAllActivitiesLocked();
3090 detachLocked();
3091 mWindowManager.removeStack(mStackId);
3092 }
3093
Craig Mautner34b73df2014-01-12 21:11:08 -08003094 private void detachLocked() {
3095 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3096 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003097 if (mActivityDisplay != null) {
3098 mActivityDisplay.detachActivitiesLocked(mStack);
3099 mActivityDisplay = null;
3100 mStack.mDisplayId = -1;
3101 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003102 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003103 }
3104 }
3105
3106 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003107 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003108 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003109 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
3110 Binder.getCallingUid(), mCurrentUser, false, true, "ActivityContainer", null);
3111 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003112 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003113 String mimeType = intent.getType();
3114 if (mimeType == null && intent.getData() != null
3115 && "content".equals(intent.getData().getScheme())) {
3116 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3117 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003118 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0, 0, null,
Craig Mautnere0a38842013-12-16 16:14:02 -08003119 null, null, null, null, userId, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003120 }
3121
3122 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003123 public final int startActivityIntentSender(IIntentSender intentSender) {
3124 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3125
3126 if (!(intentSender instanceof PendingIntentRecord)) {
3127 throw new IllegalArgumentException("Bad PendingIntent object");
3128 }
3129
3130 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003131 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003132 }
3133
Craig Mautner247ab652014-04-25 10:09:00 -07003134 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3135 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
3136 Binder.getCallingUid(), mCurrentUser, false, true, "ActivityContainer", null);
3137 if (resolvedType == null) {
3138 resolvedType = intent.getType();
3139 if (resolvedType == null && intent.getData() != null
3140 && "content".equals(intent.getData().getScheme())) {
3141 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3142 }
3143 }
3144 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003145 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003146 throw new SecurityException(
3147 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3148 }
3149 }
3150
3151 /** Throw a SecurityException if allowEmbedded is not true */
3152 @Override
3153 public final void checkEmbeddedAllowed(Intent intent) {
3154 checkEmbeddedAllowedInner(intent, null);
3155 }
3156
3157 /** Throw a SecurityException if allowEmbedded is not true */
3158 @Override
3159 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3160 if (!(intentSender instanceof PendingIntentRecord)) {
3161 throw new IllegalArgumentException("Bad PendingIntent object");
3162 }
3163 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3164 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3165 pendingIntent.key.requestResolvedType);
3166 }
3167
Craig Mautnerdf88d732014-01-27 09:21:32 -08003168 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003169 public IBinder asBinder() {
3170 return this;
3171 }
3172
Craig Mautner4504de52013-12-20 09:06:56 -08003173 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003174 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003175 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003176 }
3177
Craig Mautner4a1cb222013-12-04 16:14:06 -08003178 ActivityStackSupervisor getOuter() {
3179 return ActivityStackSupervisor.this;
3180 }
3181
3182 boolean isAttached() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003183 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003184 }
3185
3186 void getBounds(Point outBounds) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003187 if (mActivityDisplay != null) {
3188 mActivityDisplay.getBounds(outBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003189 } else {
3190 outBounds.set(0, 0);
3191 }
3192 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003193
Craig Mautner6985bad2014-04-21 15:22:06 -07003194 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003195 void setVisible(boolean visible) {
3196 if (mVisible != visible) {
3197 mVisible = visible;
3198 if (mCallback != null) {
3199 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3200 0 /* unused */, this).sendToTarget();
3201 }
3202 }
3203 }
3204
Craig Mautner6985bad2014-04-21 15:22:06 -07003205 void setDrawn() {
3206 }
3207
Craig Mautner34b73df2014-01-12 21:11:08 -08003208 @Override
3209 public String toString() {
3210 return mIdString + (mActivityDisplay == null ? "N" : "A");
3211 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003212 }
3213
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003214 private class VirtualActivityContainer extends ActivityContainer {
3215 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003216 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003217
3218 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3219 super(getNextStackId());
3220 mParentActivity = parent;
3221 mCallback = callback;
3222 mContainerState = CONTAINER_STATE_NO_SURFACE;
3223 mIdString = "VirtualActivtyContainer{" + mStackId + ", parent=" + mParentActivity + "}";
3224 }
3225
3226 @Override
3227 public void setSurface(Surface surface, int width, int height, int density) {
3228 super.setSurface(surface, width, height, density);
3229
3230 synchronized (mService) {
3231 final long origId = Binder.clearCallingIdentity();
3232 try {
3233 setSurfaceLocked(surface, width, height, density);
3234 } finally {
3235 Binder.restoreCallingIdentity(origId);
3236 }
3237 }
3238 }
3239
3240 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3241 if (mContainerState == CONTAINER_STATE_FINISHING) {
3242 return;
3243 }
3244 VirtualActivityDisplay virtualActivityDisplay =
3245 (VirtualActivityDisplay) mActivityDisplay;
3246 if (virtualActivityDisplay == null) {
3247 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003248 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003249 mActivityDisplay = virtualActivityDisplay;
3250 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3251 attachToDisplayLocked(virtualActivityDisplay);
3252 }
3253
3254 if (mSurface != null) {
3255 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003256 }
3257
Craig Mautner6985bad2014-04-21 15:22:06 -07003258 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003259 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003260 mStack.resumeTopActivityLocked(null);
3261 } else {
3262 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003263 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003264 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
3265 mStack.startPausingLocked(false, true);
3266 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003267 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003268
3269 setSurfaceIfReady();
3270
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003271 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3272 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003273 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003274
Craig Mautner6985bad2014-04-21 15:22:06 -07003275 @Override
Craig Mautnerd13a5582014-05-05 12:07:40 -07003276 boolean isAttached() {
3277 return mSurface != null && super.isAttached();
3278 }
3279
3280 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003281 void setDrawn() {
3282 synchronized (mService) {
3283 mDrawn = true;
3284 setSurfaceIfReady();
3285 }
3286 }
3287
3288 private void setSurfaceIfReady() {
3289 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReady: mDrawn=" + mDrawn +
3290 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3291 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3292 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3293 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3294 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003295 }
3296 }
3297
Craig Mautner4a1cb222013-12-04 16:14:06 -08003298 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3299 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003300 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003301 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003302 int mDisplayId;
3303 Display mDisplay;
3304 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003305
Craig Mautner4a1cb222013-12-04 16:14:06 -08003306 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3307 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08003308 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003309
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003310 ActivityDisplay() {
3311 }
Craig Mautner4504de52013-12-20 09:06:56 -08003312
Craig Mautnere0a38842013-12-16 16:14:02 -08003313 ActivityDisplay(int displayId) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003314 init(mDisplayManager.getDisplay(displayId));
Craig Mautner4504de52013-12-20 09:06:56 -08003315 }
3316
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003317 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003318 mDisplay = display;
3319 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003320 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003321 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003322
3323 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003324 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
3325 + mDisplayId);
3326 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003327 }
3328
Craig Mautnere0a38842013-12-16 16:14:02 -08003329 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003330 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003331 + " from displayId=" + mDisplayId);
3332 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003333 }
3334
3335 void getBounds(Point bounds) {
3336 mDisplay.getDisplayInfo(mDisplayInfo);
3337 bounds.x = mDisplayInfo.appWidth;
3338 bounds.y = mDisplayInfo.appHeight;
3339 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003340
3341 @Override
3342 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003343 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3344 }
3345 }
3346
3347 class VirtualActivityDisplay extends ActivityDisplay {
3348 VirtualDisplay mVirtualDisplay;
3349
Craig Mautner6985bad2014-04-21 15:22:06 -07003350 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003351 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
3352 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, VIRTUAL_DISPLAY_BASE_NAME,
Craig Mautner6985bad2014-04-21 15:22:06 -07003353 width, height, density, null, DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003354 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY);
3355
3356 init(mVirtualDisplay.getDisplay());
3357
3358 mWindowManager.handleDisplayAdded(mDisplayId);
3359 }
3360
3361 void setSurface(Surface surface) {
3362 if (mVirtualDisplay != null) {
3363 mVirtualDisplay.setSurface(surface);
3364 }
3365 }
3366
3367 @Override
3368 void detachActivitiesLocked(ActivityStack stack) {
3369 super.detachActivitiesLocked(stack);
3370 if (mVirtualDisplay != null) {
3371 mVirtualDisplay.release();
3372 mVirtualDisplay = null;
3373 }
3374 }
3375
3376 @Override
3377 public String toString() {
3378 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003379 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003380 }
Craig Mautner27084302013-03-25 08:05:25 -07003381}