blob: b68b98bd5895ad3ba31ae5bf1d95830520a9bb67 [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;
37import android.app.ActivityOptions;
38import android.app.AppGlobals;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070039import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070040import android.app.IApplicationThread;
Craig Mautner20e72272013-04-01 13:45:53 -070041import android.app.IThumbnailReceiver;
Craig Mautner23ac33b2013-04-01 16:26:35 -070042import android.app.PendingIntent;
Craig Mautner20e72272013-04-01 13:45:53 -070043import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070044import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070045import android.app.ResultInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070046import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070047import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070048import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070049import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070051import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070052import android.content.pm.ApplicationInfo;
53import android.content.pm.PackageManager;
54import android.content.pm.ResolveInfo;
55import android.content.res.Configuration;
56import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070057import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070058import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070059import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070061import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070062import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070063import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070064import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070065import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070066import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -070067import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070068import android.os.UserHandle;
Craig Mautner2420ead2013-04-01 17:13:20 -070069import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070070import android.util.Slog;
Craig Mautner4f1df4f2013-10-15 15:44:14 -070071import android.util.SparseIntArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070072
Craig Mautner23ac33b2013-04-01 16:26:35 -070073import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070074import com.android.internal.os.TransferPipe;
Craig Mautner6170f732013-04-02 13:05:23 -070075import com.android.server.am.ActivityManagerService.PendingActivityLaunch;
Craig Mautner2420ead2013-04-01 17:13:20 -070076import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerde4ef022013-04-07 19:01:33 -070077import com.android.server.wm.StackBox;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070078import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -070079
Craig Mautner8d341ef2013-03-26 09:03:27 -070080import java.io.FileDescriptor;
81import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -070082import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -070083import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -070084import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -070085
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070086public final class ActivityStackSupervisor {
Craig Mautnerde4ef022013-04-07 19:01:33 -070087 static final boolean DEBUG = ActivityManagerService.DEBUG || false;
88 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
89 static final boolean DEBUG_APP = DEBUG || false;
90 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
Craig Mautnera7f2bd42013-10-15 16:13:50 -070091 static final boolean DEBUG_STATES = DEBUG || false;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070092 static final boolean DEBUG_IDLE = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -070093
Craig Mautner2219a1b2013-03-25 09:44:30 -070094 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -070095
Craig Mautnerf3333272013-04-22 10:55:53 -070096 /** How long we wait until giving up on the last activity telling us it is idle. */
97 static final int IDLE_TIMEOUT = 10*1000;
98
Craig Mautner0eea92c2013-05-16 13:35:39 -070099 /** How long we can hold the sleep wake lock before giving up. */
100 static final int SLEEP_TIMEOUT = 5*1000;
101
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700102 // How long we can hold the launch wake lock before giving up.
103 static final int LAUNCH_TIMEOUT = 10*1000;
104
Craig Mautner05d29032013-05-03 13:40:13 -0700105 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
106 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
107 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700108 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700109 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
110
111 // For debugging to make sure the caller when acquiring/releasing our
112 // wake lock is the system process.
113 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700114
Craig Mautner27084302013-03-25 08:05:25 -0700115 final ActivityManagerService mService;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700116 final Context mContext;
117 final Looper mLooper;
Craig Mautner27084302013-03-25 08:05:25 -0700118
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700119 final ActivityStackSupervisorHandler mHandler;
120
121 /** Short cut */
122 WindowManagerService mWindowManager;
123
Craig Mautner27084302013-03-25 08:05:25 -0700124 /** Dismiss the keyguard after the next activity is displayed? */
Craig Mautner5314a402013-09-26 12:40:16 -0700125 boolean mDismissKeyguardOnNextActivity = false;
Craig Mautner27084302013-03-25 08:05:25 -0700126
Craig Mautner8d341ef2013-03-26 09:03:27 -0700127 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700128 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700129
130 /** Task identifier that activities are currently being started in. Incremented each time a
131 * new task is created. */
132 private int mCurTaskId = 0;
133
Craig Mautner2420ead2013-04-01 17:13:20 -0700134 /** The current user */
135 private int mCurrentUser;
136
Craig Mautner8d341ef2013-03-26 09:03:27 -0700137 /** The stack containing the launcher app */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700138 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700139
Craig Mautnerde4ef022013-04-07 19:01:33 -0700140 /** The non-home stack currently receiving input or launching the next activity. If home is
Craig Mautner41c0f352013-05-28 08:39:25 -0700141 * in front then mHomeStack overrides mFocusedStack.
142 * DO NOT ACCESS DIRECTLY - It may be null, use getFocusedStack() */
Craig Mautner29219d92013-04-16 20:19:12 -0700143 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700144
145 /** All the non-launcher stacks */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700146 private ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
147
Craig Mautnerde4ef022013-04-07 19:01:33 -0700148 private static final int STACK_STATE_HOME_IN_FRONT = 0;
149 private static final int STACK_STATE_HOME_TO_BACK = 1;
150 private static final int STACK_STATE_HOME_IN_BACK = 2;
151 private static final int STACK_STATE_HOME_TO_FRONT = 3;
152 private int mStackState = STACK_STATE_HOME_IN_FRONT;
153
154 /** List of activities that are waiting for a new activity to become visible before completing
155 * whatever operation they are supposed to do. */
156 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();
157
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700158 /** List of processes waiting to find out about the next visible activity. */
159 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
160 new ArrayList<IActivityManager.WaitResult>();
161
162 /** List of processes waiting to find out about the next launched activity. */
163 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
164 new ArrayList<IActivityManager.WaitResult>();
165
Craig Mautnerde4ef022013-04-07 19:01:33 -0700166 /** List of activities that are ready to be stopped, but waiting for the next activity to
167 * settle down before doing so. */
168 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>();
169
Craig Mautnerf3333272013-04-22 10:55:53 -0700170 /** List of activities that are ready to be finished, but waiting for the previous activity to
171 * settle down before doing so. It contains ActivityRecord objects. */
172 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>();
173
Craig Mautner0eea92c2013-05-16 13:35:39 -0700174 /** List of activities that are in the process of going to sleep. */
175 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
176
Craig Mautnerf3333272013-04-22 10:55:53 -0700177 /** List of ActivityRecord objects that have been finished and must still report back to a
178 * pending thumbnail receiver. */
179 final ArrayList<ActivityRecord> mCancelledThumbnails = new ArrayList<ActivityRecord>();
180
181 /** Used on user changes */
182 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<UserStartedState>();
183
Craig Mautnerde4ef022013-04-07 19:01:33 -0700184 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
185 * is being brought in front of us. */
186 boolean mUserLeaving = false;
187
Craig Mautner0eea92c2013-05-16 13:35:39 -0700188 /** Set when we have taken too long waiting to go to sleep. */
189 boolean mSleepTimeout = false;
190
191 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700192 * We don't want to allow the device to go to sleep while in the process
193 * of launching an activity. This is primarily to allow alarm intent
194 * receivers to launch an activity and get that to run before the device
195 * goes back to sleep.
196 */
197 final PowerManager.WakeLock mLaunchingActivity;
198
199 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700200 * Set when the system is going to sleep, until we have
201 * successfully paused the current activity and released our wake lock.
202 * At that point the system is allowed to actually sleep.
203 */
204 final PowerManager.WakeLock mGoingToSleep;
205
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700206 /** Stack id of the front stack when user switched, indexed by userId. */
207 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700208
Craig Mautner2219a1b2013-03-25 09:44:30 -0700209 public ActivityStackSupervisor(ActivityManagerService service, Context context,
210 Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700211 mService = service;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700212 mContext = context;
213 mLooper = looper;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700214 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
215 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700216 mHandler = new ActivityStackSupervisorHandler(looper);
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700217 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
218 throw new IllegalStateException("Calling must be system uid");
219 }
220 mLaunchingActivity =
221 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
222 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700223 }
224
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700225 void setWindowManager(WindowManagerService wm) {
226 mWindowManager = wm;
227 mHomeStack = new ActivityStack(mService, mContext, mLooper, HOME_STACK_ID);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700228 mStacks.add(mHomeStack);
Craig Mautner27084302013-03-25 08:05:25 -0700229 }
230
231 void dismissKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700232 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner27084302013-03-25 08:05:25 -0700233 if (mDismissKeyguardOnNextActivity) {
234 mDismissKeyguardOnNextActivity = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700235 mWindowManager.dismissKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700236 }
237 }
238
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700239 ActivityStack getFocusedStack() {
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700240 if (mFocusedStack == null) {
241 return mHomeStack;
242 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700243 switch (mStackState) {
244 case STACK_STATE_HOME_IN_FRONT:
245 case STACK_STATE_HOME_TO_FRONT:
246 return mHomeStack;
247 case STACK_STATE_HOME_IN_BACK:
248 case STACK_STATE_HOME_TO_BACK:
249 default:
Craig Mautner29219d92013-04-16 20:19:12 -0700250 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700251 }
Craig Mautner20e72272013-04-01 13:45:53 -0700252 }
253
Craig Mautnerde4ef022013-04-07 19:01:33 -0700254 ActivityStack getLastStack() {
255 switch (mStackState) {
256 case STACK_STATE_HOME_IN_FRONT:
257 case STACK_STATE_HOME_TO_BACK:
258 return mHomeStack;
259 case STACK_STATE_HOME_TO_FRONT:
260 case STACK_STATE_HOME_IN_BACK:
261 default:
Craig Mautner29219d92013-04-16 20:19:12 -0700262 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700263 }
Craig Mautner2219a1b2013-03-25 09:44:30 -0700264 }
265
Craig Mautnerde4ef022013-04-07 19:01:33 -0700266 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700267 return !(stack.isHomeStack() ^ getFocusedStack().isHomeStack());
Craig Mautner20e72272013-04-01 13:45:53 -0700268 }
269
Craig Mautnerde4ef022013-04-07 19:01:33 -0700270 void moveHomeStack(boolean toFront) {
271 final boolean homeInFront = isFrontStack(mHomeStack);
272 if (homeInFront ^ toFront) {
Craig Mautner76ea2242013-05-15 11:40:05 -0700273 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: mStackState old=" +
274 stackStateToString(mStackState) + " new=" + stackStateToString(homeInFront ?
275 STACK_STATE_HOME_TO_BACK : STACK_STATE_HOME_TO_FRONT));
Craig Mautnerde4ef022013-04-07 19:01:33 -0700276 mStackState = homeInFront ? STACK_STATE_HOME_TO_BACK : STACK_STATE_HOME_TO_FRONT;
277 }
278 }
279
Craig Mautner8e569572013-10-11 17:36:59 -0700280 void moveHomeToTop() {
Craig Mautner69ada552013-04-18 13:51:51 -0700281 moveHomeStack(true);
Craig Mautner8e569572013-10-11 17:36:59 -0700282 mHomeStack.moveHomeTaskToTop();
283 }
284
285 boolean resumeHomeActivity(ActivityRecord prev) {
286 moveHomeToTop();
Craig Mautner69ada552013-04-18 13:51:51 -0700287 if (prev != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700288 prev.task.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -0700289 }
Craig Mautnera8a90e02013-06-28 15:24:50 -0700290 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner760b2312013-10-11 11:57:07 -0700291 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700292 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700293 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700294 }
295 return mService.startHomeActivityLocked(mCurrentUser);
296 }
297
Craig Mautner27084302013-03-25 08:05:25 -0700298 void setDismissKeyguard(boolean dismiss) {
Craig Mautner5314a402013-09-26 12:40:16 -0700299 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen(" dismiss=" + dismiss);
Craig Mautner27084302013-03-25 08:05:25 -0700300 mDismissKeyguardOnNextActivity = dismiss;
301 }
302
Craig Mautner8d341ef2013-03-26 09:03:27 -0700303 TaskRecord anyTaskForIdLocked(int id) {
304 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
305 ActivityStack stack = mStacks.get(stackNdx);
306 TaskRecord task = stack.taskForIdLocked(id);
307 if (task != null) {
308 return task;
309 }
310 }
311 return null;
312 }
313
Craig Mautner6170f732013-04-02 13:05:23 -0700314 ActivityRecord isInAnyStackLocked(IBinder token) {
315 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
316 final ActivityRecord r = mStacks.get(stackNdx).isInStackLocked(token);
317 if (r != null) {
318 return r;
319 }
320 }
321 return null;
322 }
323
Craig Mautner8d341ef2013-03-26 09:03:27 -0700324 int getNextTaskId() {
325 do {
326 mCurTaskId++;
327 if (mCurTaskId <= 0) {
328 mCurTaskId = 1;
329 }
330 } while (anyTaskForIdLocked(mCurTaskId) != null);
331 return mCurTaskId;
332 }
333
Craig Mautnerde4ef022013-04-07 19:01:33 -0700334 void removeTask(TaskRecord task) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -0700335 mWindowManager.removeTask(task.taskId);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700336 final ActivityStack stack = task.stack;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -0700337 final ActivityRecord r = stack.mResumedActivity;
338 if (r != null && r.task == task) {
339 stack.mResumedActivity = null;
340 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700341 if (stack.removeTask(task) && !stack.isHomeStack()) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -0700342 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing stack " + stack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700343 mStacks.remove(stack);
Craig Mautner4cd0c13f2013-04-16 15:55:52 -0700344 final int stackId = stack.mStackId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700345 final int nextStackId = mWindowManager.removeStack(stackId);
Craig Mautnera9a3fb12013-04-18 10:01:00 -0700346 // TODO: Perhaps we need to let the ActivityManager determine the next focus...
Craig Mautnerac6f8432013-07-17 13:24:59 -0700347 if (mFocusedStack == null || mFocusedStack.mStackId == stackId) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700348 // If this is the last app stack, set mFocusedStack to null.
Craig Mautner29219d92013-04-16 20:19:12 -0700349 mFocusedStack = nextStackId == HOME_STACK_ID ? null : getStack(nextStackId);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700350 }
351 }
352 }
353
354 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700355 ActivityStack stack = getFocusedStack();
356 if (stack == null) {
357 return null;
358 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700359 ActivityRecord resumedActivity = stack.mResumedActivity;
360 if (resumedActivity == null || resumedActivity.app == null) {
361 resumedActivity = stack.mPausingActivity;
362 if (resumedActivity == null || resumedActivity.app == null) {
363 resumedActivity = stack.topRunningActivityLocked(null);
364 }
365 }
366 return resumedActivity;
367 }
368
Craig Mautner20e72272013-04-01 13:45:53 -0700369 boolean attachApplicationLocked(ProcessRecord app, boolean headless) throws Exception {
370 boolean didSomething = false;
371 final String processName = app.processName;
372 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
373 final ActivityStack stack = mStacks.get(stackNdx);
Craig Mautner858d8a62013-04-23 17:08:34 -0700374 if (!isFrontStack(stack)) {
375 continue;
376 }
Craig Mautner20e72272013-04-01 13:45:53 -0700377 ActivityRecord hr = stack.topRunningActivityLocked(null);
378 if (hr != null) {
379 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
380 && processName.equals(hr.processName)) {
381 try {
382 if (headless) {
383 Slog.e(TAG, "Starting activities not supported on headless device: "
384 + hr);
Craig Mautner2420ead2013-04-01 17:13:20 -0700385 } else if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner20e72272013-04-01 13:45:53 -0700386 didSomething = true;
387 }
388 } catch (Exception e) {
389 Slog.w(TAG, "Exception in new application when starting activity "
390 + hr.intent.getComponent().flattenToShortString(), e);
391 throw e;
392 }
Craig Mautner20e72272013-04-01 13:45:53 -0700393 }
394 }
395 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700396 if (!didSomething) {
397 ensureActivitiesVisibleLocked(null, 0);
398 }
Craig Mautner20e72272013-04-01 13:45:53 -0700399 return didSomething;
400 }
401
402 boolean allResumedActivitiesIdle() {
403 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700404 final ActivityStack stack = mStacks.get(stackNdx);
405 if (!isFrontStack(stack)) {
406 continue;
407 }
408 final ActivityRecord resumedActivity = stack.mResumedActivity;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -0700409 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner20e72272013-04-01 13:45:53 -0700410 return false;
411 }
412 }
413 return true;
414 }
415
Craig Mautnerde4ef022013-04-07 19:01:33 -0700416 boolean allResumedActivitiesComplete() {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700417 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
418 final ActivityStack stack = mStacks.get(stackNdx);
Craig Mautner967212c2013-04-13 21:10:58 -0700419 if (isFrontStack(stack)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700420 final ActivityRecord r = stack.mResumedActivity;
421 if (r != null && r.state != ActivityState.RESUMED) {
422 return false;
423 }
424 }
425 }
426 // TODO: Not sure if this should check if all Paused are complete too.
427 switch (mStackState) {
428 case STACK_STATE_HOME_TO_BACK:
Craig Mautner76ea2242013-05-15 11:40:05 -0700429 if (DEBUG_STACK) Slog.d(TAG, "allResumedActivitiesComplete: mStackState old=" +
430 stackStateToString(STACK_STATE_HOME_TO_BACK) + " new=" +
431 stackStateToString(STACK_STATE_HOME_IN_BACK));
Craig Mautnerde4ef022013-04-07 19:01:33 -0700432 mStackState = STACK_STATE_HOME_IN_BACK;
433 break;
434 case STACK_STATE_HOME_TO_FRONT:
Craig Mautner76ea2242013-05-15 11:40:05 -0700435 if (DEBUG_STACK) Slog.d(TAG, "allResumedActivitiesComplete: mStackState old=" +
436 stackStateToString(STACK_STATE_HOME_TO_FRONT) + " new=" +
437 stackStateToString(STACK_STATE_HOME_IN_FRONT));
Craig Mautnerde4ef022013-04-07 19:01:33 -0700438 mStackState = STACK_STATE_HOME_IN_FRONT;
439 break;
440 }
441 return true;
442 }
443
444 boolean allResumedActivitiesVisible() {
445 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
446 final ActivityStack stack = mStacks.get(stackNdx);
447 final ActivityRecord r = stack.mResumedActivity;
448 if (r != null && (!r.nowVisible || r.waitingVisible)) {
449 return false;
450 }
451 }
452 return true;
453 }
454
Craig Mautner2acc3892013-09-23 10:28:14 -0700455 /**
456 * Pause all activities in either all of the stacks or just the back stacks.
457 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700458 * @return true if any activity was paused as a result of this call.
459 */
Craig Mautner5314a402013-09-26 12:40:16 -0700460 boolean pauseBackStacks(boolean userLeaving) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700461 boolean someActivityPaused = false;
462 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
463 final ActivityStack stack = mStacks.get(stackNdx);
Craig Mautner5314a402013-09-26 12:40:16 -0700464 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
465 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
Craig Mautnerac6f8432013-07-17 13:24:59 -0700466 " mResumedActivity=" + stack.mResumedActivity);
Craig Mautnercf910b02013-04-23 11:23:27 -0700467 stack.startPausingLocked(userLeaving, false);
468 someActivityPaused = true;
469 }
470 }
471 return someActivityPaused;
472 }
473
Craig Mautnerde4ef022013-04-07 19:01:33 -0700474 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700475 boolean pausing = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700476 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
477 final ActivityStack stack = mStacks.get(stackNdx);
Craig Mautner69ada552013-04-18 13:51:51 -0700478 final ActivityRecord r = stack.mPausingActivity;
479 if (r != null && r.state != ActivityState.PAUSED
480 && r.state != ActivityState.STOPPED
481 && r.state != ActivityState.STOPPING) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700482 if (DEBUG_STATES) {
483 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
484 pausing = false;
485 } else {
486 return false;
487 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700488 }
489 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700490 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700491 }
492
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700493 void reportActivityVisibleLocked(ActivityRecord r) {
Craig Mautner858d8a62013-04-23 17:08:34 -0700494 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700495 WaitResult w = mWaitingActivityVisible.get(i);
496 w.timeout = false;
497 if (r != null) {
498 w.who = new ComponentName(r.info.packageName, r.info.name);
499 }
500 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
501 w.thisTime = w.totalTime;
502 }
503 mService.notifyAll();
504 dismissKeyguard();
505 }
506
507 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
508 long thisTime, long totalTime) {
509 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700510 WaitResult w = mWaitingActivityLaunched.remove(i);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700511 w.timeout = timeout;
512 if (r != null) {
513 w.who = new ComponentName(r.info.packageName, r.info.name);
514 }
515 w.thisTime = thisTime;
516 w.totalTime = totalTime;
517 }
518 mService.notifyAll();
519 }
520
Craig Mautner29219d92013-04-16 20:19:12 -0700521 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700522 final ActivityStack focusedStack = getFocusedStack();
523 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
524 if (r != null) {
525 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700526 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700527
Craig Mautner29219d92013-04-16 20:19:12 -0700528 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
529 final ActivityStack stack = mStacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700530 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700531 r = stack.topRunningActivityLocked(null);
532 if (r != null) {
533 return r;
534 }
535 }
536 }
537 return null;
538 }
539
Craig Mautner20e72272013-04-01 13:45:53 -0700540 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
541 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
542 ActivityRecord r = null;
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700543
544 // Gather all of the running tasks for each stack into runningTaskLists.
545 final int numStacks = mStacks.size();
546 ArrayList<RunningTaskInfo>[] runningTaskLists = new ArrayList[numStacks];
547 for (int stackNdx = numStacks - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner20e72272013-04-01 13:45:53 -0700548 final ActivityStack stack = mStacks.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700549 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
550 runningTaskLists[stackNdx] = stackTaskList;
551 final ActivityRecord ar = stack.getTasksLocked(receiver, pending, stackTaskList);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700552 if (isFrontStack(stack)) {
Craig Mautner20e72272013-04-01 13:45:53 -0700553 r = ar;
554 }
555 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700556
557 // The lists are already sorted from most recent to oldest. Just pull the most recent off
558 // each list and add it to list. Stop when all lists are empty or maxNum reached.
559 while (maxNum > 0) {
560 long mostRecentActiveTime = Long.MIN_VALUE;
561 ArrayList<RunningTaskInfo> selectedStackList = null;
562 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
563 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists[stackNdx];
564 if (!stackTaskList.isEmpty()) {
565 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
566 if (lastActiveTime > mostRecentActiveTime) {
567 mostRecentActiveTime = lastActiveTime;
568 selectedStackList = stackTaskList;
569 }
570 }
571 }
572 if (selectedStackList != null) {
573 list.add(selectedStackList.remove(0));
574 --maxNum;
575 } else {
576 break;
577 }
578 }
579
Craig Mautner20e72272013-04-01 13:45:53 -0700580 return r;
581 }
582
Craig Mautner23ac33b2013-04-01 16:26:35 -0700583 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
584 String profileFile, ParcelFileDescriptor profileFd, int userId) {
585 // Collect information about the target of the Intent.
586 ActivityInfo aInfo;
587 try {
588 ResolveInfo rInfo =
589 AppGlobals.getPackageManager().resolveIntent(
590 intent, resolvedType,
591 PackageManager.MATCH_DEFAULT_ONLY
592 | ActivityManagerService.STOCK_PM_FLAGS, userId);
593 aInfo = rInfo != null ? rInfo.activityInfo : null;
594 } catch (RemoteException e) {
595 aInfo = null;
596 }
597
598 if (aInfo != null) {
599 // Store the found target back into the intent, because now that
600 // we have it we never want to do this again. For example, if the
601 // user navigates back to this point in the history, we should
602 // always restart the exact same activity.
603 intent.setComponent(new ComponentName(
604 aInfo.applicationInfo.packageName, aInfo.name));
605
606 // Don't debug things in the system process
607 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
608 if (!aInfo.processName.equals("system")) {
609 mService.setDebugApp(aInfo.processName, true, false);
610 }
611 }
612
613 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
614 if (!aInfo.processName.equals("system")) {
615 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
616 }
617 }
618
619 if (profileFile != null) {
620 if (!aInfo.processName.equals("system")) {
621 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName,
622 profileFile, profileFd,
623 (startFlags&ActivityManager.START_FLAG_AUTO_STOP_PROFILER) != 0);
624 }
625 }
626 }
627 return aInfo;
628 }
629
Craig Mautner2219a1b2013-03-25 09:44:30 -0700630 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautner8e569572013-10-11 17:36:59 -0700631 moveHomeToTop();
Craig Mautner6170f732013-04-02 13:05:23 -0700632 startActivityLocked(null, intent, null, aInfo, null, null, 0, 0, 0, null, 0,
Craig Mautner2219a1b2013-03-25 09:44:30 -0700633 null, false, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700634 }
635
Craig Mautner23ac33b2013-04-01 16:26:35 -0700636 final int startActivityMayWait(IApplicationThread caller, int callingUid,
637 String callingPackage, Intent intent, String resolvedType, IBinder resultTo,
638 String resultWho, int requestCode, int startFlags, String profileFile,
639 ParcelFileDescriptor profileFd, WaitResult outResult, Configuration config,
640 Bundle options, int userId) {
641 // Refuse possible leaked file descriptors
642 if (intent != null && intent.hasFileDescriptors()) {
643 throw new IllegalArgumentException("File descriptors passed in Intent");
644 }
645 boolean componentSpecified = intent.getComponent() != null;
646
647 // Don't modify the client's object!
648 intent = new Intent(intent);
649
650 // Collect information about the target of the Intent.
651 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
652 profileFile, profileFd, userId);
653
654 synchronized (mService) {
655 int callingPid;
656 if (callingUid >= 0) {
657 callingPid = -1;
658 } else if (caller == null) {
659 callingPid = Binder.getCallingPid();
660 callingUid = Binder.getCallingUid();
661 } else {
662 callingPid = callingUid = -1;
663 }
664
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700665 final ActivityStack stack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700666 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700667 && mService.mConfiguration.diff(config) != 0;
668 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700669 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700670
671 final long origId = Binder.clearCallingIdentity();
672
673 if (aInfo != null &&
674 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
675 // This may be a heavy-weight process! Check to see if we already
676 // have another, different heavy-weight process running.
677 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
678 if (mService.mHeavyWeightProcess != null &&
679 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
680 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700681 int realCallingUid = callingUid;
682 if (caller != null) {
683 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
684 if (callerApp != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700685 realCallingUid = callerApp.info.uid;
686 } else {
687 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700688 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700689 + intent.toString());
690 ActivityOptions.abort(options);
691 return ActivityManager.START_PERMISSION_DENIED;
692 }
693 }
694
695 IIntentSender target = mService.getIntentSenderLocked(
696 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
697 realCallingUid, userId, null, null, 0, new Intent[] { intent },
698 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
699 | PendingIntent.FLAG_ONE_SHOT, null);
700
701 Intent newIntent = new Intent();
702 if (requestCode >= 0) {
703 // Caller is requesting a result.
704 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
705 }
706 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
707 new IntentSender(target));
708 if (mService.mHeavyWeightProcess.activities.size() > 0) {
709 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
710 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
711 hist.packageName);
712 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
713 hist.task.taskId);
714 }
715 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
716 aInfo.packageName);
717 newIntent.setFlags(intent.getFlags());
718 newIntent.setClassName("android",
719 HeavyWeightSwitcherActivity.class.getName());
720 intent = newIntent;
721 resolvedType = null;
722 caller = null;
723 callingUid = Binder.getCallingUid();
724 callingPid = Binder.getCallingPid();
725 componentSpecified = true;
726 try {
727 ResolveInfo rInfo =
728 AppGlobals.getPackageManager().resolveIntent(
729 intent, null,
730 PackageManager.MATCH_DEFAULT_ONLY
731 | ActivityManagerService.STOCK_PM_FLAGS, userId);
732 aInfo = rInfo != null ? rInfo.activityInfo : null;
733 aInfo = mService.getActivityInfoForUser(aInfo, userId);
734 } catch (RemoteException e) {
735 aInfo = null;
736 }
737 }
738 }
739 }
740
Craig Mautner6170f732013-04-02 13:05:23 -0700741 int res = startActivityLocked(caller, intent, resolvedType,
Craig Mautner23ac33b2013-04-01 16:26:35 -0700742 aInfo, resultTo, resultWho, requestCode, callingPid, callingUid,
743 callingPackage, startFlags, options, componentSpecified, null);
744
Craig Mautnerde4ef022013-04-07 19:01:33 -0700745 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700746 // If the caller also wants to switch to a new configuration,
747 // do so now. This allows a clean switch, as we are waiting
748 // for the current activity to pause (so we will not destroy
749 // it), and have not yet started the next activity.
750 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
751 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700752 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700753 if (DEBUG_CONFIGURATION) Slog.v(TAG,
754 "Updating to new configuration after starting activity.");
755 mService.updateConfigurationLocked(config, null, false, false);
756 }
757
758 Binder.restoreCallingIdentity(origId);
759
760 if (outResult != null) {
761 outResult.result = res;
762 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700763 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700764 do {
765 try {
766 mService.wait();
767 } catch (InterruptedException e) {
768 }
769 } while (!outResult.timeout && outResult.who == null);
770 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700771 ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700772 if (r.nowVisible) {
773 outResult.timeout = false;
774 outResult.who = new ComponentName(r.info.packageName, r.info.name);
775 outResult.totalTime = 0;
776 outResult.thisTime = 0;
777 } else {
778 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700779 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700780 do {
781 try {
782 mService.wait();
783 } catch (InterruptedException e) {
784 }
785 } while (!outResult.timeout && outResult.who == null);
786 }
787 }
788 }
789
790 return res;
791 }
792 }
793
794 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
795 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
796 Bundle options, int userId) {
797 if (intents == null) {
798 throw new NullPointerException("intents is null");
799 }
800 if (resolvedTypes == null) {
801 throw new NullPointerException("resolvedTypes is null");
802 }
803 if (intents.length != resolvedTypes.length) {
804 throw new IllegalArgumentException("intents are length different than resolvedTypes");
805 }
806
Craig Mautner23ac33b2013-04-01 16:26:35 -0700807
808 int callingPid;
809 if (callingUid >= 0) {
810 callingPid = -1;
811 } else if (caller == null) {
812 callingPid = Binder.getCallingPid();
813 callingUid = Binder.getCallingUid();
814 } else {
815 callingPid = callingUid = -1;
816 }
817 final long origId = Binder.clearCallingIdentity();
818 try {
819 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -0700820 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -0700821 for (int i=0; i<intents.length; i++) {
822 Intent intent = intents[i];
823 if (intent == null) {
824 continue;
825 }
826
827 // Refuse possible leaked file descriptors
828 if (intent != null && intent.hasFileDescriptors()) {
829 throw new IllegalArgumentException("File descriptors passed in Intent");
830 }
831
832 boolean componentSpecified = intent.getComponent() != null;
833
834 // Don't modify the client's object!
835 intent = new Intent(intent);
836
837 // Collect information about the target of the Intent.
838 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i],
839 0, null, null, userId);
840 // TODO: New, check if this is correct
841 aInfo = mService.getActivityInfoForUser(aInfo, userId);
842
843 if (aInfo != null &&
844 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
845 != 0) {
846 throw new IllegalArgumentException(
847 "FLAG_CANT_SAVE_STATE not supported here");
848 }
849
850 Bundle theseOptions;
851 if (options != null && i == intents.length-1) {
852 theseOptions = options;
853 } else {
854 theseOptions = null;
855 }
Craig Mautner6170f732013-04-02 13:05:23 -0700856 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Craig Mautner23ac33b2013-04-01 16:26:35 -0700857 aInfo, resultTo, null, -1, callingPid, callingUid, callingPackage,
858 0, theseOptions, componentSpecified, outActivity);
859 if (res < 0) {
860 return res;
861 }
862
863 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
864 }
865 }
866 } finally {
867 Binder.restoreCallingIdentity(origId);
868 }
869
870 return ActivityManager.START_SUCCESS;
871 }
872
Craig Mautner2420ead2013-04-01 17:13:20 -0700873 final boolean realStartActivityLocked(ActivityRecord r,
874 ProcessRecord app, boolean andResume, boolean checkConfig)
875 throws RemoteException {
876
877 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -0700878 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700879 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -0700880
881 // schedule launch ticks to collect information about slow apps.
882 r.startLaunchTickingLocked();
883
884 // Have the window manager re-evaluate the orientation of
885 // the screen based on the new activity order. Note that
886 // as a result of this, it can call back into the activity
887 // manager with a new orientation. We don't care about that,
888 // because the activity is not currently running so we are
889 // just restarting it anyway.
890 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700891 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -0700892 mService.mConfiguration,
893 r.mayFreezeScreenLocked(app) ? r.appToken : null);
894 mService.updateConfigurationLocked(config, r, false, false);
895 }
896
897 r.app = app;
898 app.waitingToKill = null;
899 r.launchCount++;
900 r.lastLaunchTime = SystemClock.uptimeMillis();
901
902 if (localLOGV) Slog.v(TAG, "Launching: " + r);
903
904 int idx = app.activities.indexOf(r);
905 if (idx < 0) {
906 app.activities.add(r);
907 }
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700908 mService.updateLruProcessLocked(app, true, true);
Craig Mautner2420ead2013-04-01 17:13:20 -0700909
910 final ActivityStack stack = r.task.stack;
911 try {
912 if (app.thread == null) {
913 throw new RemoteException();
914 }
915 List<ResultInfo> results = null;
916 List<Intent> newIntents = null;
917 if (andResume) {
918 results = r.results;
919 newIntents = r.newIntents;
920 }
921 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
922 + " icicle=" + r.icicle
923 + " with results=" + results + " newIntents=" + newIntents
924 + " andResume=" + andResume);
925 if (andResume) {
926 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
927 r.userId, System.identityHashCode(r),
928 r.task.taskId, r.shortComponentName);
929 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700930 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -0700931 // Home process is the root process of the task.
932 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -0700933 }
934 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
935 r.sleeping = false;
936 r.forceNewConfig = false;
937 mService.showAskCompatModeDialogLocked(r);
938 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
939 String profileFile = null;
940 ParcelFileDescriptor profileFd = null;
941 boolean profileAutoStop = false;
942 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
943 if (mService.mProfileProc == null || mService.mProfileProc == app) {
944 mService.mProfileProc = app;
945 profileFile = mService.mProfileFile;
946 profileFd = mService.mProfileFd;
947 profileAutoStop = mService.mAutoStopProfiler;
948 }
949 }
950 app.hasShownUi = true;
951 app.pendingUiClean = true;
952 if (profileFd != null) {
953 try {
954 profileFd = profileFd.dup();
955 } catch (IOException e) {
956 if (profileFd != null) {
957 try {
958 profileFd.close();
959 } catch (IOException o) {
960 }
961 profileFd = null;
962 }
963 }
964 }
Dianne Hackborna413dc02013-07-12 12:02:55 -0700965 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -0700966 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
967 System.identityHashCode(r), r.info,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700968 new Configuration(mService.mConfiguration), r.compat,
969 app.repProcState, r.icicle, results, newIntents, !andResume,
Craig Mautner2420ead2013-04-01 17:13:20 -0700970 mService.isNextTransitionForward(), profileFile, profileFd,
971 profileAutoStop);
972
973 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
974 // This may be a heavy-weight process! Note that the package
975 // manager will ensure that only activity can run in the main
976 // process of the .apk, which is the only thing that will be
977 // considered heavy-weight.
978 if (app.processName.equals(app.info.packageName)) {
979 if (mService.mHeavyWeightProcess != null
980 && mService.mHeavyWeightProcess != app) {
981 Slog.w(TAG, "Starting new heavy weight process " + app
982 + " when already running "
983 + mService.mHeavyWeightProcess);
984 }
985 mService.mHeavyWeightProcess = app;
986 Message msg = mService.mHandler.obtainMessage(
987 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
988 msg.obj = r;
989 mService.mHandler.sendMessage(msg);
990 }
991 }
992
993 } catch (RemoteException e) {
994 if (r.launchFailed) {
995 // This is the second time we failed -- finish activity
996 // and give up.
997 Slog.e(TAG, "Second failure launching "
998 + r.intent.getComponent().flattenToShortString()
999 + ", giving up", e);
1000 mService.appDiedLocked(app, app.pid, app.thread);
1001 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1002 "2nd-crash", false);
1003 return false;
1004 }
1005
1006 // This is the first time we failed -- restart process and
1007 // retry.
1008 app.activities.remove(r);
1009 throw e;
1010 }
1011
1012 r.launchFailed = false;
1013 if (stack.updateLRUListLocked(r)) {
1014 Slog.w(TAG, "Activity " + r
1015 + " being launched, but already in LRU list");
1016 }
1017
1018 if (andResume) {
1019 // As part of the process of launching, ActivityThread also performs
1020 // a resume.
1021 stack.minimalResumeActivityLocked(r);
1022 } else {
1023 // This activity is not starting in the resumed state... which
1024 // should look like we asked it to pause+stop (but remain visible),
1025 // and it has done so and reported back the current icicle and
1026 // other state.
1027 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1028 + " (starting in stopped state)");
1029 r.state = ActivityState.STOPPED;
1030 r.stopped = true;
1031 }
1032
1033 // Launch the new version setup screen if needed. We do this -after-
1034 // launching the initial activity (that is, home), so that it can have
1035 // a chance to initialize itself while in the background, making the
1036 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001037 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001038 mService.startSetupActivityLocked();
1039 }
1040
1041 return true;
1042 }
1043
Craig Mautnere79d42682013-04-01 19:01:53 -07001044 void startSpecificActivityLocked(ActivityRecord r,
1045 boolean andResume, boolean checkConfig) {
1046 // Is this activity's application already running?
1047 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001048 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001049
1050 r.task.stack.setLaunchTime(r);
1051
1052 if (app != null && app.thread != null) {
1053 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001054 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1055 || !"android".equals(r.info.packageName)) {
1056 // Don't add this if it is a platform component that is marked
1057 // to run in multiple processes, because this is actually
1058 // part of the framework so doesn't make sense to track as a
1059 // separate apk in the process.
1060 app.addPackage(r.info.packageName, mService.mProcessStats);
1061 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001062 realStartActivityLocked(r, app, andResume, checkConfig);
1063 return;
1064 } catch (RemoteException e) {
1065 Slog.w(TAG, "Exception when starting activity "
1066 + r.intent.getComponent().flattenToShortString(), e);
1067 }
1068
1069 // If a dead object exception was thrown -- fall through to
1070 // restart the application.
1071 }
1072
1073 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001074 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001075 }
1076
Craig Mautner6170f732013-04-02 13:05:23 -07001077 final int startActivityLocked(IApplicationThread caller,
1078 Intent intent, String resolvedType, ActivityInfo aInfo, IBinder resultTo,
1079 String resultWho, int requestCode,
1080 int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
1081 boolean componentSpecified, ActivityRecord[] outActivity) {
1082 int err = ActivityManager.START_SUCCESS;
1083
1084 ProcessRecord callerApp = null;
1085 if (caller != null) {
1086 callerApp = mService.getRecordForAppLocked(caller);
1087 if (callerApp != null) {
1088 callingPid = callerApp.pid;
1089 callingUid = callerApp.info.uid;
1090 } else {
1091 Slog.w(TAG, "Unable to find app for caller " + caller
1092 + " (pid=" + callingPid + ") when starting: "
1093 + intent.toString());
1094 err = ActivityManager.START_PERMISSION_DENIED;
1095 }
1096 }
1097
1098 if (err == ActivityManager.START_SUCCESS) {
1099 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1100 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
1101 + "} from pid " + (callerApp != null ? callerApp.pid : callingPid));
1102 }
1103
1104 ActivityRecord sourceRecord = null;
1105 ActivityRecord resultRecord = null;
1106 if (resultTo != null) {
1107 sourceRecord = isInAnyStackLocked(resultTo);
1108 if (DEBUG_RESULTS) Slog.v(
1109 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1110 if (sourceRecord != null) {
1111 if (requestCode >= 0 && !sourceRecord.finishing) {
1112 resultRecord = sourceRecord;
1113 }
1114 }
1115 }
1116 ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1117
1118 int launchFlags = intent.getFlags();
1119
1120 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
1121 && sourceRecord != null) {
1122 // Transfer the result target from the source activity to the new
1123 // one being started, including any failures.
1124 if (requestCode >= 0) {
1125 ActivityOptions.abort(options);
1126 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1127 }
1128 resultRecord = sourceRecord.resultTo;
1129 resultWho = sourceRecord.resultWho;
1130 requestCode = sourceRecord.requestCode;
1131 sourceRecord.resultTo = null;
1132 if (resultRecord != null) {
1133 resultRecord.removeResultsLocked(
1134 sourceRecord, resultWho, requestCode);
1135 }
1136 }
1137
1138 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1139 // We couldn't find a class that can handle the given Intent.
1140 // That's the end of that!
1141 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1142 }
1143
1144 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1145 // We couldn't find the specific class specified in the Intent.
1146 // Also the end of the line.
1147 err = ActivityManager.START_CLASS_NOT_FOUND;
1148 }
1149
1150 if (err != ActivityManager.START_SUCCESS) {
1151 if (resultRecord != null) {
1152 resultStack.sendActivityResultLocked(-1,
1153 resultRecord, resultWho, requestCode,
1154 Activity.RESULT_CANCELED, null);
1155 }
1156 setDismissKeyguard(false);
1157 ActivityOptions.abort(options);
1158 return err;
1159 }
1160
1161 final int startAnyPerm = mService.checkPermission(
1162 START_ANY_ACTIVITY, callingPid, callingUid);
1163 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1164 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1165 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1166 if (resultRecord != null) {
1167 resultStack.sendActivityResultLocked(-1,
1168 resultRecord, resultWho, requestCode,
1169 Activity.RESULT_CANCELED, null);
1170 }
1171 setDismissKeyguard(false);
1172 String msg;
1173 if (!aInfo.exported) {
1174 msg = "Permission Denial: starting " + intent.toString()
1175 + " from " + callerApp + " (pid=" + callingPid
1176 + ", uid=" + callingUid + ")"
1177 + " not exported from uid " + aInfo.applicationInfo.uid;
1178 } else {
1179 msg = "Permission Denial: starting " + intent.toString()
1180 + " from " + callerApp + " (pid=" + callingPid
1181 + ", uid=" + callingUid + ")"
1182 + " requires " + aInfo.permission;
1183 }
1184 Slog.w(TAG, msg);
1185 throw new SecurityException(msg);
1186 }
1187
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001188 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001189 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001190
Craig Mautner6170f732013-04-02 13:05:23 -07001191 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001192 try {
1193 // The Intent we give to the watcher has the extra data
1194 // stripped off, since it can contain private information.
1195 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001196 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001197 aInfo.applicationInfo.packageName);
1198 } catch (RemoteException e) {
1199 mService.mController = null;
1200 }
Ben Gruver5e207332013-04-03 17:41:37 -07001201 }
Craig Mautner6170f732013-04-02 13:05:23 -07001202
Ben Gruver5e207332013-04-03 17:41:37 -07001203 if (abort) {
1204 if (resultRecord != null) {
1205 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001206 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001207 }
Ben Gruver5e207332013-04-03 17:41:37 -07001208 // We pretend to the caller that it was really started, but
1209 // they will just get a cancel result.
1210 setDismissKeyguard(false);
1211 ActivityOptions.abort(options);
1212 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001213 }
1214
1215 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
1216 intent, resolvedType, aInfo, mService.mConfiguration,
Craig Mautnerde4ef022013-04-07 19:01:33 -07001217 resultRecord, resultWho, requestCode, componentSpecified, this);
Craig Mautner6170f732013-04-02 13:05:23 -07001218 if (outActivity != null) {
1219 outActivity[0] = r;
1220 }
1221
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001222 final ActivityStack stack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001223 if (stack.mResumedActivity == null
1224 || stack.mResumedActivity.info.applicationInfo.uid != callingUid) {
Craig Mautner6170f732013-04-02 13:05:23 -07001225 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
1226 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001227 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautner6170f732013-04-02 13:05:23 -07001228 mService.mPendingActivityLaunches.add(pal);
1229 setDismissKeyguard(false);
1230 ActivityOptions.abort(options);
1231 return ActivityManager.START_SWITCHES_CANCELED;
1232 }
1233 }
1234
1235 if (mService.mDidAppSwitch) {
1236 // This is the second allowed switch since we stopped switches,
1237 // so now just generally allow switches. Use case: user presses
1238 // home (switches disabled, switch to home, mDidAppSwitch now true);
1239 // user taps a home icon (coming from home so allowed, we hit here
1240 // and now allow anyone to switch again).
1241 mService.mAppSwitchesAllowedTime = 0;
1242 } else {
1243 mService.mDidAppSwitch = true;
1244 }
1245
1246 mService.doPendingActivityLaunchesLocked(false);
1247
Craig Mautner8849a5e2013-04-02 16:41:03 -07001248 err = startActivityUncheckedLocked(r, sourceRecord, startFlags, true, options);
Craig Mautner10385a12013-09-22 21:08:32 -07001249
1250 if (allPausedActivitiesComplete()) {
1251 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001252 // activity start, but we are not actually doing an activity
1253 // switch... just dismiss the keyguard now, because we
1254 // probably want to see whatever is behind it.
1255 dismissKeyguard();
1256 }
1257 return err;
1258 }
1259
Craig Mautnerac6f8432013-07-17 13:24:59 -07001260 ActivityStack adjustStackFocus(ActivityRecord r) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001261 final TaskRecord task = r.task;
1262 if (r.isApplicationActivity() || (task != null && task.isApplicationTask())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001263 if (task != null) {
1264 if (mFocusedStack != task.stack) {
1265 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1266 "adjustStackFocus: Setting focused stack to r=" + r + " task=" + task);
1267 mFocusedStack = task.stack;
1268 } else {
1269 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1270 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1271 }
1272 return mFocusedStack;
1273 }
1274
1275 if (mFocusedStack != null) {
1276 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1277 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1278 return mFocusedStack;
1279 }
1280
1281 for (int stackNdx = mStacks.size() - 1; stackNdx > 0; --stackNdx) {
1282 ActivityStack stack = mStacks.get(stackNdx);
1283 if (!stack.isHomeStack()) {
1284 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1285 "adjustStackFocus: Setting focused stack=" + stack);
1286 mFocusedStack = stack;
1287 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001288 }
1289 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001290
1291 // Time to create the first app stack for this user.
1292 int stackId = mService.createStack(-1, HOME_STACK_ID,
1293 StackBox.TASK_STACK_GOES_OVER, 1.0f);
1294 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1295 " stackId=" + stackId);
1296 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001297 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001298 }
1299 return mHomeStack;
1300 }
1301
Craig Mautner29219d92013-04-16 20:19:12 -07001302 void setFocusedStack(ActivityRecord r) {
1303 if (r == null) {
1304 return;
1305 }
Craig Mautner86d67a42013-05-14 10:34:38 -07001306 if (!r.isApplicationActivity() || (r.task != null && !r.task.isApplicationTask())) {
Craig Mautner29219d92013-04-16 20:19:12 -07001307 if (mStackState != STACK_STATE_HOME_IN_FRONT) {
Craig Mautnere7c58b62013-06-12 20:19:00 -07001308 if (DEBUG_STACK || DEBUG_FOCUS) Slog.d(TAG, "setFocusedStack: mStackState old=" +
Craig Mautner76ea2242013-05-15 11:40:05 -07001309 stackStateToString(mStackState) + " new=" +
1310 stackStateToString(STACK_STATE_HOME_TO_FRONT) +
1311 " Callers=" + Debug.getCallers(3));
Craig Mautner29219d92013-04-16 20:19:12 -07001312 mStackState = STACK_STATE_HOME_TO_FRONT;
1313 }
1314 } else {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001315 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1316 "setFocusedStack: Setting focused stack to r=" + r + " task=" + r.task +
1317 " Callers=" + Debug.getCallers(3));
Craig Mautner29219d92013-04-16 20:19:12 -07001318 mFocusedStack = r.task.stack;
1319 if (mStackState != STACK_STATE_HOME_IN_BACK) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001320 if (DEBUG_STACK) Slog.d(TAG, "setFocusedStack: mStackState old=" +
1321 stackStateToString(mStackState) + " new=" +
1322 stackStateToString(STACK_STATE_HOME_TO_BACK) +
1323 " Callers=" + Debug.getCallers(3));
Craig Mautner29219d92013-04-16 20:19:12 -07001324 mStackState = STACK_STATE_HOME_TO_BACK;
1325 }
1326 }
1327 }
1328
Craig Mautner8849a5e2013-04-02 16:41:03 -07001329 final int startActivityUncheckedLocked(ActivityRecord r,
1330 ActivityRecord sourceRecord, int startFlags, boolean doResume,
1331 Bundle options) {
1332 final Intent intent = r.intent;
1333 final int callingUid = r.launchedFromUid;
1334
1335 int launchFlags = intent.getFlags();
1336
Craig Mautner8849a5e2013-04-02 16:41:03 -07001337 // We'll invoke onUserLeaving before onPause only if the launching
1338 // activity did not explicitly state that this is an automated launch.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001339 mUserLeaving = (launchFlags&Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
1340 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001341
1342 // If the caller has asked not to resume at this point, we make note
1343 // of this in the record so that we can skip it when trying to find
1344 // the top running activity.
1345 if (!doResume) {
1346 r.delayedResume = true;
1347 }
1348
1349 ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
1350
1351 // If the onlyIfNeeded flag is set, then we can do this if the activity
1352 // being launched is the same as the one making the call... or, as
1353 // a special case, if we do not know the caller then we count the
1354 // current top activity as the caller.
1355 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1356 ActivityRecord checkedCaller = sourceRecord;
1357 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001358 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001359 }
1360 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1361 // Caller is not the same as launcher, so always needed.
1362 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1363 }
1364 }
1365
1366 if (sourceRecord == null) {
1367 // This activity is not being started from another... in this
1368 // case we -always- start a new task.
1369 if ((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Craig Mautner29219d92013-04-16 20:19:12 -07001370 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1371 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001372 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1373 }
1374 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1375 // The original activity who is starting us is running as a single
1376 // instance... this new activity it is starting must go on its
1377 // own task.
1378 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1379 } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
1380 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
1381 // The activity being started is a single instance... it always
1382 // gets launched into its own task.
1383 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1384 }
1385
Craig Mautnerde4ef022013-04-07 19:01:33 -07001386 final ActivityStack sourceStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001387 if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001388 if (sourceRecord.finishing) {
1389 // If the source is finishing, we can't further count it as our source. This
1390 // is because the task it is associated with may now be empty and on its way out,
1391 // so we don't want to blindly throw it in to that task. Instead we will take
1392 // the NEW_TASK flow and try to find a task for it.
1393 if ((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1394 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1395 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1396 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1397 }
1398 sourceRecord = null;
1399 sourceStack = null;
1400 } else {
1401 sourceStack = sourceRecord.task.stack;
1402 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001403 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001404 sourceStack = null;
1405 }
1406
Craig Mautner8849a5e2013-04-02 16:41:03 -07001407 if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1408 // For whatever reason this activity is being launched into a new
1409 // task... yet the caller has requested a result back. Well, that
1410 // is pretty messed up, so instead immediately send back a cancel
1411 // and let the new task continue launched as normal without a
1412 // dependency on its originator.
1413 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1414 r.resultTo.task.stack.sendActivityResultLocked(-1,
1415 r.resultTo, r.resultWho, r.requestCode,
1416 Activity.RESULT_CANCELED, null);
1417 r.resultTo = null;
1418 }
1419
1420 boolean addingToTask = false;
1421 boolean movedHome = false;
1422 TaskRecord reuseTask = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001423 ActivityStack targetStack;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001424 if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1425 (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
1426 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
1427 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1428 // If bring to front is requested, and no result is requested, and
1429 // we can find a task that was started with this same
1430 // component, then instead of launching bring that one to the front.
1431 if (r.resultTo == null) {
1432 // See if there is a task to bring to the front. If this is
1433 // a SINGLE_INSTANCE activity, there can be one and only one
1434 // instance of it in the history, and it is always in its own
1435 // unique task, so we do a special search.
1436 ActivityRecord intentActivity = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
Craig Mautnerac6f8432013-07-17 13:24:59 -07001437 ? findTaskLocked(r)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001438 : findActivityLocked(intent, r.info);
1439 if (intentActivity != null) {
Craig Mautner29219d92013-04-16 20:19:12 -07001440 if (r.task == null) {
1441 r.task = intentActivity.task;
1442 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001443 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001444 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001445 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1446 + " from " + intentActivity);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001447 moveHomeStack(targetStack.isHomeStack());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001448 if (intentActivity.task.intent == null) {
1449 // This task was started because of movement of
1450 // the activity based on affinity... now that we
1451 // are actually launching it, we can assign the
1452 // base intent.
1453 intentActivity.task.setIntent(intent, r.info);
1454 }
1455 // If the target task is not in the front, then we need
1456 // to bring it to the front... except... well, with
1457 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1458 // to have the same behavior as if a new instance was
1459 // being started, which means not bringing it to the front
1460 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001461 final ActivityStack lastStack = getLastStack();
1462 ActivityRecord curTop = lastStack == null?
1463 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner7504d7b2013-09-17 10:50:53 -07001464 if (curTop != null && (curTop.task != intentActivity.task ||
1465 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001466 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001467 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1468 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001469 // We really do want to push this one into the
1470 // user's face, right now.
1471 movedHome = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001472 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001473 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1474 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001475 // Caller wants to appear on home activity.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001476 intentActivity.task.mOnTopOfHome = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001477 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001478 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
1479 options = null;
1480 }
1481 }
1482 // If the caller has requested that the target task be
1483 // reset, then do so.
1484 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1485 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1486 }
1487 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1488 // We don't need to start a new activity, and
1489 // the client said not to do anything if that
1490 // is the case, so this is it! And for paranoia, make
1491 // sure we have correctly resumed the top activity.
1492 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001493 resumeTopActivitiesLocked(targetStack, null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001494 } else {
1495 ActivityOptions.abort(options);
1496 }
Craig Mautner29219d92013-04-16 20:19:12 -07001497 if (r.task == null) Slog.v(TAG,
1498 "startActivityUncheckedLocked: task left null",
1499 new RuntimeException("here").fillInStackTrace());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001500 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1501 }
1502 if ((launchFlags &
1503 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1504 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1505 // The caller has requested to completely replace any
1506 // existing task with its new activity. Well that should
1507 // not be too hard...
1508 reuseTask = intentActivity.task;
1509 reuseTask.performClearTaskLocked();
1510 reuseTask.setIntent(r.intent, r.info);
1511 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
1512 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
1513 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1514 // In this situation we want to remove all activities
1515 // from the task up to the one being started. In most
1516 // cases this means we are resetting the task to its
1517 // initial state.
1518 ActivityRecord top =
1519 intentActivity.task.performClearTaskLocked(r, launchFlags);
1520 if (top != null) {
1521 if (top.frontOfTask) {
1522 // Activity aliases may mean we use different
1523 // intents for the top activity, so make sure
1524 // the task now has the identity of the new
1525 // intent.
1526 top.task.setIntent(r.intent, r.info);
1527 }
1528 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1529 r, top.task);
1530 top.deliverNewIntentLocked(callingUid, r.intent);
1531 } else {
1532 // A special case: we need to
1533 // start the activity because it is not currently
1534 // running, and the caller has asked to clear the
1535 // current task to have this activity at the top.
1536 addingToTask = true;
1537 // Now pretend like this activity is being started
1538 // by the top of its task, so it is put in the
1539 // right place.
1540 sourceRecord = intentActivity;
1541 }
1542 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1543 // In this case the top activity on the task is the
1544 // same as the one being launched, so we take that
1545 // as a request to bring the task to the foreground.
1546 // If the top activity in the task is the root
1547 // activity, deliver this new intent to it if it
1548 // desires.
1549 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
1550 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
1551 && intentActivity.realActivity.equals(r.realActivity)) {
1552 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1553 intentActivity.task);
1554 if (intentActivity.frontOfTask) {
1555 intentActivity.task.setIntent(r.intent, r.info);
1556 }
1557 intentActivity.deliverNewIntentLocked(callingUid, r.intent);
1558 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1559 // In this case we are launching the root activity
1560 // of the task, but with a different intent. We
1561 // should start a new instance on top.
1562 addingToTask = true;
1563 sourceRecord = intentActivity;
1564 }
1565 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1566 // In this case an activity is being launched in to an
1567 // existing task, without resetting that task. This
1568 // is typically the situation of launching an activity
1569 // from a notification or shortcut. We want to place
1570 // the new activity on top of the current task.
1571 addingToTask = true;
1572 sourceRecord = intentActivity;
1573 } else if (!intentActivity.task.rootWasReset) {
1574 // In this case we are launching in to an existing task
1575 // that has not yet been started from its front door.
1576 // The current task has been brought to the front.
1577 // Ideally, we'd probably like to place this new task
1578 // at the bottom of its stack, but that's a little hard
1579 // to do with the current organization of the code so
1580 // for now we'll just drop it.
1581 intentActivity.task.setIntent(r.intent, r.info);
1582 }
1583 if (!addingToTask && reuseTask == null) {
1584 // We didn't do anything... but it was needed (a.k.a., client
1585 // don't use that intent!) And for paranoia, make
1586 // sure we have correctly resumed the top activity.
1587 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001588 targetStack.resumeTopActivityLocked(null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001589 } else {
1590 ActivityOptions.abort(options);
1591 }
Craig Mautner29219d92013-04-16 20:19:12 -07001592 if (r.task == null) Slog.v(TAG,
1593 "startActivityUncheckedLocked: task left null",
1594 new RuntimeException("here").fillInStackTrace());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001595 return ActivityManager.START_TASK_TO_FRONT;
1596 }
1597 }
1598 }
1599 }
1600
1601 //String uri = r.intent.toURI();
1602 //Intent intent2 = new Intent(uri);
1603 //Slog.i(TAG, "Given intent: " + r.intent);
1604 //Slog.i(TAG, "URI is: " + uri);
1605 //Slog.i(TAG, "To intent: " + intent2);
1606
1607 if (r.packageName != null) {
1608 // If the activity being launched is the same as the one currently
1609 // at the top, then we need to check if it should only be launched
1610 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001611 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001612 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001613 if (top != null && r.resultTo == null) {
1614 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
1615 if (top.app != null && top.app.thread != null) {
1616 if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
1617 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP
1618 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
1619 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
1620 top.task);
1621 // For paranoia, make sure we have correctly
1622 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001623 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001624 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001625 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001626 }
1627 ActivityOptions.abort(options);
1628 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1629 // We don't need to start a new activity, and
1630 // the client said not to do anything if that
1631 // is the case, so this is it!
Craig Mautner29219d92013-04-16 20:19:12 -07001632 if (r.task == null) Slog.v(TAG,
1633 "startActivityUncheckedLocked: task left null",
1634 new RuntimeException("here").fillInStackTrace());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001635 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1636 }
1637 top.deliverNewIntentLocked(callingUid, r.intent);
Craig Mautner29219d92013-04-16 20:19:12 -07001638 if (r.task == null) Slog.v(TAG,
1639 "startActivityUncheckedLocked: task left null",
1640 new RuntimeException("here").fillInStackTrace());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001641 return ActivityManager.START_DELIVERED_TO_TOP;
1642 }
1643 }
1644 }
1645 }
1646
1647 } else {
1648 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001649 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
1650 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001651 }
1652 ActivityOptions.abort(options);
Craig Mautner29219d92013-04-16 20:19:12 -07001653 if (r.task == null) Slog.v(TAG,
1654 "startActivityUncheckedLocked: task left null",
1655 new RuntimeException("here").fillInStackTrace());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001656 return ActivityManager.START_CLASS_NOT_FOUND;
1657 }
1658
1659 boolean newTask = false;
1660 boolean keepCurTransition = false;
1661
1662 // Should this be considered a new task?
1663 if (r.resultTo == null && !addingToTask
1664 && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001665 targetStack = adjustStackFocus(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001666 moveHomeStack(targetStack.isHomeStack());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001667 if (reuseTask == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001668 r.setTask(targetStack.createTaskRecord(getNextTaskId(), r.info, intent, true),
1669 null, true);
1670 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
1671 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001672 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001673 r.setTask(reuseTask, reuseTask, true);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001674 }
1675 newTask = true;
1676 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001677 if ((launchFlags &
1678 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
1679 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
1680 // Caller wants to appear on home activity, so before starting
1681 // their own activity we will bring home to the front.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001682 r.task.mOnTopOfHome = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001683 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001684 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001685 } else if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001686 TaskRecord sourceTask = sourceRecord.task;
Craig Mautner525f3d92013-05-07 14:01:50 -07001687 targetStack = sourceTask.stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001688 moveHomeStack(targetStack.isHomeStack());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001689 if (!addingToTask &&
1690 (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
1691 // In this case, we are adding the activity to an existing
1692 // task, but the caller has asked to clear that task if the
1693 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07001694 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001695 keepCurTransition = true;
1696 if (top != null) {
1697 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
1698 top.deliverNewIntentLocked(callingUid, r.intent);
1699 // For paranoia, make sure we have correctly
1700 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001701 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001702 if (doResume) {
1703 targetStack.resumeTopActivityLocked(null);
1704 }
1705 ActivityOptions.abort(options);
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001706 if (r.task == null) Slog.w(TAG,
Craig Mautner29219d92013-04-16 20:19:12 -07001707 "startActivityUncheckedLocked: task left null",
1708 new RuntimeException("here").fillInStackTrace());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001709 return ActivityManager.START_DELIVERED_TO_TOP;
1710 }
1711 } else if (!addingToTask &&
1712 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
1713 // In this case, we are launching an activity in our own task
1714 // that may already be running somewhere in the history, and
1715 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07001716 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001717 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001718 final TaskRecord task = top.task;
1719 task.moveActivityToFrontLocked(top);
1720 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001721 top.updateOptionsLocked(options);
1722 top.deliverNewIntentLocked(callingUid, r.intent);
Craig Mautner0f922742013-08-06 08:44:42 -07001723 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001724 if (doResume) {
1725 targetStack.resumeTopActivityLocked(null);
1726 }
1727 return ActivityManager.START_DELIVERED_TO_TOP;
1728 }
1729 }
1730 // An existing activity is starting this new activity, so we want
1731 // to keep the new one in the same task as the one that is starting
1732 // it.
Craig Mautner525f3d92013-05-07 14:01:50 -07001733 r.setTask(sourceTask, sourceRecord.thumbHolder, false);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001734 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001735 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001736
1737 } else {
1738 // This not being started from an existing activity, and not part
1739 // of a new task... just put it in the top task, though these days
1740 // this case should never happen.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001741 targetStack = adjustStackFocus(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001742 moveHomeStack(targetStack.isHomeStack());
Craig Mautner1602ec22013-05-12 10:24:27 -07001743 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001744 r.setTask(prev != null ? prev.task
1745 : targetStack.createTaskRecord(getNextTaskId(), r.info, intent, true),
1746 null, true);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001747 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
1748 + " in new guessed " + r.task);
1749 }
1750
1751 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
1752 intent, r.getUriPermissionsLocked());
1753
1754 if (newTask) {
1755 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
1756 }
1757 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07001758 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001759 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautner1d001b62013-06-18 16:52:43 -07001760 mService.setFocusedActivityLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001761 return ActivityManager.START_SUCCESS;
1762 }
1763
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001764 void acquireLaunchWakelock() {
1765 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1766 throw new IllegalStateException("Calling must be system uid");
1767 }
1768 mLaunchingActivity.acquire();
1769 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1770 // To be safe, don't allow the wake lock to be held for too long.
1771 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1772 }
1773 }
1774
Craig Mautnerf3333272013-04-22 10:55:53 -07001775 // Checked.
1776 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1777 Configuration config) {
1778 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
1779
Craig Mautnerf3333272013-04-22 10:55:53 -07001780 ArrayList<ActivityRecord> stops = null;
1781 ArrayList<ActivityRecord> finishes = null;
1782 ArrayList<UserStartedState> startingUsers = null;
1783 int NS = 0;
1784 int NF = 0;
1785 IApplicationThread sendThumbnail = null;
1786 boolean booting = false;
1787 boolean enableScreen = false;
1788 boolean activityRemoved = false;
1789
1790 ActivityRecord r = ActivityRecord.forToken(token);
1791 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001792 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
1793 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001794 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1795 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001796 if (fromTimeout) {
1797 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001798 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001799
1800 // This is a hack to semi-deal with a race condition
1801 // in the client where it can be constructed with a
1802 // newer configuration from when we asked it to launch.
1803 // We'll update with whatever configuration it now says
1804 // it used to launch.
1805 if (config != null) {
1806 r.configuration = config;
1807 }
1808
1809 // We are now idle. If someone is waiting for a thumbnail from
1810 // us, we can now deliver.
1811 r.idle = true;
1812
1813 if (r.thumbnailNeeded && r.app != null && r.app.thread != null) {
1814 sendThumbnail = r.app.thread;
1815 r.thumbnailNeeded = false;
1816 }
1817
1818 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
1819 if (!mService.mBooted && isFrontStack(r.task.stack)) {
1820 mService.mBooted = true;
1821 enableScreen = true;
1822 }
1823 }
1824
1825 if (allResumedActivitiesIdle()) {
1826 if (r != null) {
1827 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001828 }
1829
1830 if (mLaunchingActivity.isHeld()) {
1831 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1832 if (VALIDATE_WAKE_LOCK_CALLER &&
1833 Binder.getCallingUid() != Process.myUid()) {
1834 throw new IllegalStateException("Calling must be system uid");
1835 }
1836 mLaunchingActivity.release();
1837 }
1838 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07001839 }
1840
1841 // Atomically retrieve all of the other things to do.
1842 stops = processStoppingActivitiesLocked(true);
1843 NS = stops != null ? stops.size() : 0;
1844 if ((NF=mFinishingActivities.size()) > 0) {
1845 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
1846 mFinishingActivities.clear();
1847 }
1848
1849 final ArrayList<ActivityRecord> thumbnails;
1850 final int NT = mCancelledThumbnails.size();
1851 if (NT > 0) {
1852 thumbnails = new ArrayList<ActivityRecord>(mCancelledThumbnails);
1853 mCancelledThumbnails.clear();
1854 } else {
1855 thumbnails = null;
1856 }
1857
1858 if (isFrontStack(mHomeStack)) {
1859 booting = mService.mBooting;
1860 mService.mBooting = false;
1861 }
1862
1863 if (mStartingUsers.size() > 0) {
1864 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
1865 mStartingUsers.clear();
1866 }
1867
1868 // Perform the following actions from unsynchronized state.
1869 final IApplicationThread thumbnailThread = sendThumbnail;
1870 mHandler.post(new Runnable() {
1871 @Override
1872 public void run() {
1873 if (thumbnailThread != null) {
1874 try {
1875 thumbnailThread.requestThumbnail(token);
1876 } catch (Exception e) {
1877 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
1878 mService.sendPendingThumbnail(null, token, null, null, true);
1879 }
1880 }
1881
1882 // Report back to any thumbnail receivers.
1883 for (int i = 0; i < NT; i++) {
1884 ActivityRecord r = thumbnails.get(i);
1885 mService.sendPendingThumbnail(r, null, null, null, true);
1886 }
1887 }
1888 });
1889
1890 // Stop any activities that are scheduled to do so but have been
1891 // waiting for the next one to start.
1892 for (int i = 0; i < NS; i++) {
1893 r = stops.get(i);
1894 final ActivityStack stack = r.task.stack;
1895 if (r.finishing) {
1896 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1897 } else {
1898 stack.stopActivityLocked(r);
1899 }
1900 }
1901
1902 // Finish any activities that are scheduled to do so but have been
1903 // waiting for the next one to start.
1904 for (int i = 0; i < NF; i++) {
1905 r = finishes.get(i);
1906 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, false, "finish-idle");
1907 }
1908
1909 if (booting) {
1910 mService.finishBooting();
1911 } else if (startingUsers != null) {
1912 for (int i = 0; i < startingUsers.size(); i++) {
1913 mService.finishUserSwitch(startingUsers.get(i));
1914 }
1915 }
1916
1917 mService.trimApplications();
1918 //dump();
1919 //mWindowManager.dump();
1920
1921 if (enableScreen) {
1922 mService.enableScreenAfterBoot();
1923 }
1924
1925 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07001926 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001927 }
1928
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001929 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001930 }
1931
Craig Mautner8e569572013-10-11 17:36:59 -07001932 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001933 boolean hasVisibleActivities = false;
1934 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1935 hasVisibleActivities |= mStacks.get(stackNdx).handleAppDiedLocked(app);
Craig Mautner6b74cb52013-09-27 17:02:21 -07001936 }
Craig Mautner19091252013-10-05 00:03:53 -07001937 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001938 }
1939
1940 void closeSystemDialogsLocked() {
1941 final int numStacks = mStacks.size();
1942 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1943 final ActivityStack stack = mStacks.get(stackNdx);
1944 stack.closeSystemDialogsLocked();
1945 }
1946 }
1947
Craig Mautner93529a42013-10-04 15:03:13 -07001948 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001949 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001950 }
1951
Craig Mautner8d341ef2013-03-26 09:03:27 -07001952 /**
1953 * @return true if some activity was finished (or would have finished if doit were true).
1954 */
1955 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
1956 boolean didSomething = false;
1957 final int numStacks = mStacks.size();
1958 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1959 final ActivityStack stack = mStacks.get(stackNdx);
1960 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
1961 didSomething = true;
1962 }
1963 }
1964 return didSomething;
1965 }
1966
Dianne Hackborna413dc02013-07-12 12:02:55 -07001967 void updatePreviousProcessLocked(ActivityRecord r) {
1968 // Now that this process has stopped, we may want to consider
1969 // it to be the previous app to try to keep around in case
1970 // the user wants to return to it.
1971
1972 // First, found out what is currently the foreground app, so that
1973 // we don't blow away the previous app if this activity is being
1974 // hosted by the process that is actually still the foreground.
1975 ProcessRecord fgApp = null;
1976 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1977 final ActivityStack stack = mStacks.get(stackNdx);
1978 if (isFrontStack(stack)) {
1979 if (stack.mResumedActivity != null) {
1980 fgApp = stack.mResumedActivity.app;
1981 } else if (stack.mPausingActivity != null) {
1982 fgApp = stack.mPausingActivity.app;
1983 }
1984 break;
1985 }
1986 }
1987
1988 // Now set this one as the previous process, only if that really
1989 // makes sense to.
1990 if (r.app != null && fgApp != null && r.app != fgApp
1991 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001992 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001993 mService.mPreviousProcess = r.app;
1994 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1995 }
1996 }
1997
Craig Mautner05d29032013-05-03 13:40:13 -07001998 boolean resumeTopActivitiesLocked() {
1999 return resumeTopActivitiesLocked(null, null, null);
2000 }
2001
2002 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2003 Bundle targetOptions) {
2004 if (targetStack == null) {
2005 targetStack = getFocusedStack();
2006 }
2007 boolean result = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002008 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002009 final ActivityStack stack = mStacks.get(stackNdx);
2010 if (isFrontStack(stack)) {
Craig Mautner05d29032013-05-03 13:40:13 -07002011 if (stack == targetStack) {
2012 result = stack.resumeTopActivityLocked(target, targetOptions);
2013 } else {
2014 stack.resumeTopActivityLocked(null);
2015 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002016 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002017 }
Craig Mautner05d29032013-05-03 13:40:13 -07002018 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002019 }
2020
2021 void finishTopRunningActivityLocked(ProcessRecord app) {
2022 final int numStacks = mStacks.size();
2023 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2024 final ActivityStack stack = mStacks.get(stackNdx);
2025 stack.finishTopRunningActivityLocked(app);
2026 }
2027 }
2028
Craig Mautner8d341ef2013-03-26 09:03:27 -07002029 void findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
2030 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2031 if (mStacks.get(stackNdx).findTaskToMoveToFrontLocked(taskId, flags, options)) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002032 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack=" +
Craig Mautner1d001b62013-06-18 16:52:43 -07002033 mStacks.get(stackNdx));
Craig Mautner8d341ef2013-03-26 09:03:27 -07002034 return;
2035 }
2036 }
2037 }
2038
Craig Mautner967212c2013-04-13 21:10:58 -07002039 ActivityStack getStack(int stackId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002040 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2041 final ActivityStack stack = mStacks.get(stackNdx);
2042 if (stack.getStackId() == stackId) {
2043 return stack;
2044 }
2045 }
2046 return null;
2047 }
2048
Craig Mautner967212c2013-04-13 21:10:58 -07002049 ArrayList<ActivityStack> getStacks() {
2050 return new ArrayList<ActivityStack>(mStacks);
2051 }
2052
2053 int createStack() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002054 while (true) {
2055 if (++mLastStackId <= HOME_STACK_ID) {
2056 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002057 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002058 if (getStack(mLastStackId) == null) {
2059 break;
2060 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002061 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002062 mStacks.add(new ActivityStack(mService, mContext, mLooper, mLastStackId));
2063 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002064 }
2065
2066 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002067 final TaskRecord task = anyTaskForIdLocked(taskId);
2068 if (task == null) {
2069 return;
2070 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002071 final ActivityStack stack = getStack(stackId);
2072 if (stack == null) {
2073 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2074 return;
2075 }
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002076 removeTask(task);
2077 stack.addTask(task, toTop);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002078 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002079 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002080 }
2081
Craig Mautnerac6f8432013-07-17 13:24:59 -07002082 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002083 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002084 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002085 final ActivityStack stack = mStacks.get(stackNdx);
2086 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002087 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: " + stack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002088 continue;
2089 }
2090 final ActivityRecord ar = stack.findTaskLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002091 if (ar != null) {
2092 return ar;
2093 }
2094 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002095 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002096 return null;
2097 }
2098
2099 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
2100 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2101 final ActivityRecord ar = mStacks.get(stackNdx).findActivityLocked(intent, info);
2102 if (ar != null) {
2103 return ar;
2104 }
2105 }
2106 return null;
2107 }
2108
Craig Mautner8d341ef2013-03-26 09:03:27 -07002109 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002110 scheduleSleepTimeout();
2111 if (!mGoingToSleep.isHeld()) {
2112 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002113 if (mLaunchingActivity.isHeld()) {
2114 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2115 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002116 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002117 mLaunchingActivity.release();
2118 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002119 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002120 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002121 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002122 }
2123
2124 boolean shutdownLocked(int timeout) {
2125 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002126 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002127
2128 final long endTime = System.currentTimeMillis() + timeout;
2129 while (true) {
2130 boolean cantShutdown = false;
2131 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2132 cantShutdown |= mStacks.get(stackNdx).checkReadyForSleepLocked();
2133 }
2134 if (cantShutdown) {
2135 long timeRemaining = endTime - System.currentTimeMillis();
2136 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002137 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002138 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002139 } catch (InterruptedException e) {
2140 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002141 } else {
2142 Slog.w(TAG, "Activity manager shutdown timed out");
2143 timedout = true;
2144 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002145 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002146 } else {
2147 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002148 }
2149 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002150
2151 // Force checkReadyForSleep to complete.
2152 mSleepTimeout = true;
2153 checkReadyForSleepLocked();
2154
Craig Mautner8d341ef2013-03-26 09:03:27 -07002155 return timedout;
2156 }
2157
2158 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002159 removeSleepTimeouts();
2160 if (mGoingToSleep.isHeld()) {
2161 mGoingToSleep.release();
2162 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002163 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002164 final ActivityStack stack = mStacks.get(stackNdx);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002165 stack.awakeFromSleepingLocked();
Craig Mautner5314a402013-09-26 12:40:16 -07002166 if (isFrontStack(stack)) {
2167 resumeTopActivitiesLocked();
2168 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002169 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002170 mGoingToSleepActivities.clear();
2171 }
2172
2173 void activitySleptLocked(ActivityRecord r) {
2174 mGoingToSleepActivities.remove(r);
2175 checkReadyForSleepLocked();
2176 }
2177
2178 void checkReadyForSleepLocked() {
2179 if (!mService.isSleepingOrShuttingDown()) {
2180 // Do not care.
2181 return;
2182 }
2183
2184 if (!mSleepTimeout) {
2185 boolean dontSleep = false;
2186 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2187 dontSleep |= mStacks.get(stackNdx).checkReadyForSleepLocked();
2188 }
2189
2190 if (mStoppingActivities.size() > 0) {
2191 // Still need to tell some activities to stop; can't sleep yet.
2192 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2193 + mStoppingActivities.size() + " activities");
2194 scheduleIdleLocked();
2195 dontSleep = true;
2196 }
2197
2198 if (mGoingToSleepActivities.size() > 0) {
2199 // Still need to tell some activities to sleep; can't sleep yet.
2200 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2201 + mGoingToSleepActivities.size() + " activities");
2202 dontSleep = true;
2203 }
2204
2205 if (dontSleep) {
2206 return;
2207 }
2208 }
2209
2210 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2211 mStacks.get(stackNdx).goToSleep();
2212 }
2213
2214 removeSleepTimeouts();
2215
2216 if (mGoingToSleep.isHeld()) {
2217 mGoingToSleep.release();
2218 }
2219 if (mService.mShuttingDown) {
2220 mService.notifyAll();
2221 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002222 }
2223
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002224 boolean reportResumedActivityLocked(ActivityRecord r) {
2225 final ActivityStack stack = r.task.stack;
2226 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002227 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002228 }
2229 if (allResumedActivitiesComplete()) {
2230 ensureActivitiesVisibleLocked(null, 0);
2231 mWindowManager.executeAppTransition();
2232 return true;
2233 }
2234 return false;
2235 }
2236
Craig Mautner8d341ef2013-03-26 09:03:27 -07002237 void handleAppCrashLocked(ProcessRecord app) {
2238 final int numStacks = mStacks.size();
2239 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2240 final ActivityStack stack = mStacks.get(stackNdx);
2241 stack.handleAppCrashLocked(app);
2242 }
2243 }
2244
Craig Mautnerde4ef022013-04-07 19:01:33 -07002245 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002246 // First the front stacks. In case any are not fullscreen and are in front of home.
2247 boolean showHomeBehindStack = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002248 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner580ea812013-04-25 12:58:38 -07002249 final ActivityStack stack = mStacks.get(stackNdx);
2250 if (isFrontStack(stack)) {
2251 showHomeBehindStack =
2252 stack.ensureActivitiesVisibleLocked(starting, configChanges);
2253 }
2254 }
2255 // Now do back stacks.
2256 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2257 final ActivityStack stack = mStacks.get(stackNdx);
2258 if (!isFrontStack(stack)) {
2259 stack.ensureActivitiesVisibleLocked(starting, configChanges, showHomeBehindStack);
2260 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002261 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002262 }
2263
2264 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
2265 final int numStacks = mStacks.size();
2266 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2267 final ActivityStack stack = mStacks.get(stackNdx);
2268 stack.scheduleDestroyActivities(app, false, reason);
2269 }
2270 }
2271
2272 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002273 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
2274 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002275 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002276
Craig Mautner858d8a62013-04-23 17:08:34 -07002277 mStartingUsers.add(uss);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002278 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner93529a42013-10-04 15:03:13 -07002279 mStacks.get(stackNdx).switchUserLocked(userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002280 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002281
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002282 ActivityStack stack = getStack(restoreStackId);
2283 if (stack == null) {
2284 stack = mHomeStack;
2285 }
2286 final boolean homeInFront = stack.isHomeStack();
Craig Mautner93529a42013-10-04 15:03:13 -07002287 moveHomeStack(homeInFront);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002288 mWindowManager.moveTaskToTop(stack.topTask().taskId);
Craig Mautner93529a42013-10-04 15:03:13 -07002289 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002290 }
2291
Craig Mautnerde4ef022013-04-07 19:01:33 -07002292 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
2293 int N = mStoppingActivities.size();
2294 if (N <= 0) return null;
2295
2296 ArrayList<ActivityRecord> stops = null;
2297
2298 final boolean nowVisible = allResumedActivitiesVisible();
2299 for (int i=0; i<N; i++) {
2300 ActivityRecord s = mStoppingActivities.get(i);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07002301 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
Craig Mautnerde4ef022013-04-07 19:01:33 -07002302 + nowVisible + " waitingVisible=" + s.waitingVisible
2303 + " finishing=" + s.finishing);
2304 if (s.waitingVisible && nowVisible) {
2305 mWaitingVisibleActivities.remove(s);
2306 s.waitingVisible = false;
2307 if (s.finishing) {
2308 // If this activity is finishing, it is sitting on top of
2309 // everyone else but we now know it is no longer needed...
2310 // so get rid of it. Otherwise, we need to go through the
2311 // normal flow and hide it once we determine that it is
2312 // hidden by the activities in front of it.
2313 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002314 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002315 }
2316 }
2317 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
2318 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
2319 if (stops == null) {
2320 stops = new ArrayList<ActivityRecord>();
2321 }
2322 stops.add(s);
2323 mStoppingActivities.remove(i);
2324 N--;
2325 i--;
2326 }
2327 }
2328
2329 return stops;
2330 }
2331
Craig Mautnercf910b02013-04-23 11:23:27 -07002332 void validateTopActivitiesLocked() {
2333 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2334 final ActivityStack stack = mStacks.get(stackNdx);
2335 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002336 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07002337 if (isFrontStack(stack)) {
2338 if (r == null) {
2339 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
2340 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002341 final ActivityRecord pausing = stack.mPausingActivity;
2342 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002343 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002344 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002345 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002346 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002347 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002348 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002349 }
2350 }
2351 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002352 final ActivityRecord resumed = stack.mResumedActivity;
2353 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002354 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002355 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002356 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002357 if (r != null && (state == ActivityState.INITIALIZING
2358 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002359 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002360 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002361 }
2362 }
2363 }
2364 }
2365
Craig Mautner76ea2242013-05-15 11:40:05 -07002366 private static String stackStateToString(int stackState) {
2367 switch (stackState) {
2368 case STACK_STATE_HOME_IN_FRONT: return "STACK_STATE_HOME_IN_FRONT";
2369 case STACK_STATE_HOME_TO_BACK: return "STACK_STATE_HOME_TO_BACK";
2370 case STACK_STATE_HOME_IN_BACK: return "STACK_STATE_HOME_IN_BACK";
2371 case STACK_STATE_HOME_TO_FRONT: return "STACK_STATE_HOME_TO_FRONT";
2372 default: return "Unknown stackState=" + stackState;
2373 }
2374 }
2375
Craig Mautner27084302013-03-25 08:05:25 -07002376 public void dump(PrintWriter pw, String prefix) {
2377 pw.print(prefix); pw.print("mDismissKeyguardOnNextActivity:");
2378 pw.println(mDismissKeyguardOnNextActivity);
Craig Mautner76ea2242013-05-15 11:40:05 -07002379 pw.print(prefix); pw.print("mStackState="); pw.println(stackStateToString(mStackState));
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002380 pw.print(prefix); pw.println("mSleepTimeout: " + mSleepTimeout);
2381 pw.print(prefix); pw.println("mCurTaskId: " + mCurTaskId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002382 pw.print(prefix); pw.println("mUserStackInFront: " + mUserStackInFront);
Craig Mautner27084302013-03-25 08:05:25 -07002383 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002384
Craig Mautner20e72272013-04-01 13:45:53 -07002385 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002386 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002387 }
2388
Dianne Hackborn390517b2013-05-30 15:03:32 -07002389 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2390 boolean needSep, String prefix) {
2391 if (activity != null) {
2392 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2393 if (needSep) {
2394 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002395 }
2396 pw.print(prefix);
2397 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002398 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002399 }
2400 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002401 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002402 }
2403
Craig Mautner8d341ef2013-03-26 09:03:27 -07002404 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2405 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002406 boolean printed = false;
2407 boolean needSep = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002408 final int numStacks = mStacks.size();
2409 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2410 final ActivityStack stack = mStacks.get(stackNdx);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002411 StringBuilder stackHeader = new StringBuilder(128);
2412 stackHeader.append(" Stack #");
2413 stackHeader.append(mStacks.indexOf(stack));
2414 stackHeader.append(":");
2415 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage, needSep,
2416 stackHeader.toString());
2417 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false, !dumpAll,
2418 false, dumpPackage, true, " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002419
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002420 needSep = printed;
2421 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
Dianne Hackborn390517b2013-05-30 15:03:32 -07002422 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002423 if (pr) {
2424 printed = true;
2425 needSep = false;
2426 }
2427 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
Dianne Hackborn390517b2013-05-30 15:03:32 -07002428 " mResumedActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002429 if (pr) {
2430 printed = true;
2431 needSep = false;
2432 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002433 if (dumpAll) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002434 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
Dianne Hackborn390517b2013-05-30 15:03:32 -07002435 " mLastPausedActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002436 if (pr) {
2437 printed = true;
Craig Mautner0f922742013-08-06 08:44:42 -07002438 needSep = true;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002439 }
Craig Mautner0f922742013-08-06 08:44:42 -07002440 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2441 needSep, " mLastNoHistoryActivity: ");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002442 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002443 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002444 }
2445
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002446 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2447 false, dumpPackage, true, " Activities waiting to finish:", null);
2448 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2449 false, dumpPackage, true, " Activities waiting to stop:", null);
2450 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2451 false, dumpPackage, true, " Activities waiting for another to become visible:",
2452 null);
2453 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2454 false, dumpPackage, true, " Activities waiting to sleep:", null);
2455 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2456 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002457
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002458 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002459 }
2460
Dianne Hackborn390517b2013-05-30 15:03:32 -07002461 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002462 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002463 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002464 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002465 String innerPrefix = null;
2466 String[] args = null;
2467 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002468 for (int i=list.size()-1; i>=0; i--) {
2469 final ActivityRecord r = list.get(i);
2470 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2471 continue;
2472 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002473 if (innerPrefix == null) {
2474 innerPrefix = prefix + " ";
2475 args = new String[0];
2476 }
2477 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002478 final boolean full = !brief && (complete || !r.isInHistory());
2479 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002480 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002481 needNL = false;
2482 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002483 if (header1 != null) {
2484 pw.println(header1);
2485 header1 = null;
2486 }
2487 if (header2 != null) {
2488 pw.println(header2);
2489 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002490 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002491 if (lastTask != r.task) {
2492 lastTask = r.task;
2493 pw.print(prefix);
2494 pw.print(full ? "* " : " ");
2495 pw.println(lastTask);
2496 if (full) {
2497 lastTask.dump(pw, prefix + " ");
2498 } else if (complete) {
2499 // Complete + brief == give a summary. Isn't that obvious?!?
2500 if (lastTask.intent != null) {
2501 pw.print(prefix); pw.print(" ");
2502 pw.println(lastTask.intent.toInsecureStringWithClip());
2503 }
2504 }
2505 }
2506 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2507 pw.print(" #"); pw.print(i); pw.print(": ");
2508 pw.println(r);
2509 if (full) {
2510 r.dump(pw, innerPrefix);
2511 } else if (complete) {
2512 // Complete + brief == give a summary. Isn't that obvious?!?
2513 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2514 if (r.app != null) {
2515 pw.print(innerPrefix); pw.println(r.app);
2516 }
2517 }
2518 if (client && r.app != null && r.app.thread != null) {
2519 // flush anything that is already in the PrintWriter since the thread is going
2520 // to write to the file descriptor directly
2521 pw.flush();
2522 try {
2523 TransferPipe tp = new TransferPipe();
2524 try {
2525 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
2526 r.appToken, innerPrefix, args);
2527 // Short timeout, since blocking here can
2528 // deadlock with the application.
2529 tp.go(fd, 2000);
2530 } finally {
2531 tp.kill();
2532 }
2533 } catch (IOException e) {
2534 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2535 } catch (RemoteException e) {
2536 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2537 }
2538 needNL = true;
2539 }
2540 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002541 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002542 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002543
Craig Mautnerf3333272013-04-22 10:55:53 -07002544 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002545 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002546 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2547 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002548 }
2549
2550 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002551 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002552 }
2553
2554 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002555 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002556 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2557 }
2558
Craig Mautner05d29032013-05-03 13:40:13 -07002559 final void scheduleResumeTopActivities() {
2560 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2561 }
2562
Craig Mautner0eea92c2013-05-16 13:35:39 -07002563 void removeSleepTimeouts() {
2564 mSleepTimeout = false;
2565 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
2566 }
2567
2568 final void scheduleSleepTimeout() {
2569 removeSleepTimeouts();
2570 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
2571 }
2572
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002573 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07002574
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002575 public ActivityStackSupervisorHandler(Looper looper) {
2576 super(looper);
2577 }
2578
Craig Mautnerf3333272013-04-22 10:55:53 -07002579 void activityIdleInternal(ActivityRecord r) {
2580 synchronized (mService) {
2581 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
2582 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002583 }
2584
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002585 @Override
2586 public void handleMessage(Message msg) {
2587 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002588 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07002589 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07002590 if (mService.mDidDexOpt) {
2591 mService.mDidDexOpt = false;
2592 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
2593 nmsg.obj = msg.obj;
2594 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
2595 return;
2596 }
2597 // We don't at this point know if the activity is fullscreen,
2598 // so we need to be conservative and assume it isn't.
2599 activityIdleInternal((ActivityRecord)msg.obj);
2600 } break;
2601 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07002602 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07002603 activityIdleInternal((ActivityRecord)msg.obj);
2604 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07002605 case RESUME_TOP_ACTIVITY_MSG: {
2606 synchronized (mService) {
2607 resumeTopActivitiesLocked();
2608 }
2609 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002610 case SLEEP_TIMEOUT_MSG: {
2611 synchronized (mService) {
2612 if (mService.isSleepingOrShuttingDown()) {
2613 Slog.w(TAG, "Sleep timeout! Sleeping now.");
2614 mSleepTimeout = true;
2615 checkReadyForSleepLocked();
2616 }
2617 }
2618 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002619 case LAUNCH_TIMEOUT_MSG: {
2620 if (mService.mDidDexOpt) {
2621 mService.mDidDexOpt = false;
2622 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2623 return;
2624 }
2625 synchronized (mService) {
2626 if (mLaunchingActivity.isHeld()) {
2627 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
2628 if (VALIDATE_WAKE_LOCK_CALLER
2629 && Binder.getCallingUid() != Process.myUid()) {
2630 throw new IllegalStateException("Calling must be system uid");
2631 }
2632 mLaunchingActivity.release();
2633 }
2634 }
2635 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002636 }
2637 }
2638 }
Craig Mautner27084302013-03-25 08:05:25 -07002639}