blob: f11dca9f13f588e42aca986285af59a29d1298b5 [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 Mautner858d8a62013-04-23 17:08:34 -070071import android.util.SparseArray;
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 Mautnerb3370ce2013-09-19 12:02:09 -070091 static final boolean DEBUG_STATES = DEBUG || true;
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? */
125 private boolean mDismissKeyguardOnNextActivity = false;
126
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 Mautnerac6f8432013-07-17 13:24:59 -0700206 /**
207 * The name of the current home activity for each user.
208 * TODO: Remove entries when user is deleted.
209 */
210 final SparseArray<String> mHomePackageNames = new SparseArray<String>();
211
Craig Mautner2219a1b2013-03-25 09:44:30 -0700212 public ActivityStackSupervisor(ActivityManagerService service, Context context,
213 Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700214 mService = service;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700215 mContext = context;
216 mLooper = looper;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700217 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
218 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700219 mHandler = new ActivityStackSupervisorHandler(looper);
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700220 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
221 throw new IllegalStateException("Calling must be system uid");
222 }
223 mLaunchingActivity =
224 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
225 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700226 }
227
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700228 void setWindowManager(WindowManagerService wm) {
229 mWindowManager = wm;
230 mHomeStack = new ActivityStack(mService, mContext, mLooper, HOME_STACK_ID);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700231 mStacks.add(mHomeStack);
Craig Mautner27084302013-03-25 08:05:25 -0700232 }
233
234 void dismissKeyguard() {
235 if (mDismissKeyguardOnNextActivity) {
236 mDismissKeyguardOnNextActivity = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700237 mWindowManager.dismissKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700238 }
239 }
240
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700241 ActivityStack getFocusedStack() {
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700242 if (mFocusedStack == null) {
243 return mHomeStack;
244 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700245 switch (mStackState) {
246 case STACK_STATE_HOME_IN_FRONT:
247 case STACK_STATE_HOME_TO_FRONT:
248 return mHomeStack;
249 case STACK_STATE_HOME_IN_BACK:
250 case STACK_STATE_HOME_TO_BACK:
251 default:
Craig Mautner29219d92013-04-16 20:19:12 -0700252 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700253 }
Craig Mautner20e72272013-04-01 13:45:53 -0700254 }
255
Craig Mautnerde4ef022013-04-07 19:01:33 -0700256 ActivityStack getLastStack() {
257 switch (mStackState) {
258 case STACK_STATE_HOME_IN_FRONT:
259 case STACK_STATE_HOME_TO_BACK:
260 return mHomeStack;
261 case STACK_STATE_HOME_TO_FRONT:
262 case STACK_STATE_HOME_IN_BACK:
263 default:
Craig Mautner29219d92013-04-16 20:19:12 -0700264 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700265 }
Craig Mautner2219a1b2013-03-25 09:44:30 -0700266 }
267
Craig Mautnerde4ef022013-04-07 19:01:33 -0700268 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700269 return !(stack.isHomeStack() ^ getFocusedStack().isHomeStack());
Craig Mautner20e72272013-04-01 13:45:53 -0700270 }
271
Craig Mautnerde4ef022013-04-07 19:01:33 -0700272 void moveHomeStack(boolean toFront) {
273 final boolean homeInFront = isFrontStack(mHomeStack);
274 if (homeInFront ^ toFront) {
Craig Mautner76ea2242013-05-15 11:40:05 -0700275 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: mStackState old=" +
276 stackStateToString(mStackState) + " new=" + stackStateToString(homeInFront ?
277 STACK_STATE_HOME_TO_BACK : STACK_STATE_HOME_TO_FRONT));
Craig Mautnerde4ef022013-04-07 19:01:33 -0700278 mStackState = homeInFront ? STACK_STATE_HOME_TO_BACK : STACK_STATE_HOME_TO_FRONT;
279 }
280 }
281
Craig Mautner69ada552013-04-18 13:51:51 -0700282 boolean resumeHomeActivity(ActivityRecord prev) {
283 moveHomeStack(true);
284 if (prev != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700285 prev.task.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -0700286 }
Craig Mautnera82aa092013-09-13 15:34:08 -0700287 mHomeStack.moveHomeTaskToTop();
Craig Mautnera8a90e02013-06-28 15:24:50 -0700288 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
289 if (r != null) {
290 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700291 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700292 }
293 return mService.startHomeActivityLocked(mCurrentUser);
294 }
295
Craig Mautnerde4ef022013-04-07 19:01:33 -0700296 final void setLaunchHomeTaskNextFlag(ActivityRecord sourceRecord, ActivityRecord r,
297 ActivityStack stack) {
298 if (stack == mHomeStack) {
299 return;
300 }
301 if ((sourceRecord == null && getLastStack() == mHomeStack) ||
Craig Mautner86d67a42013-05-14 10:34:38 -0700302 (sourceRecord != null && sourceRecord.isHomeActivity())) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700303 if (r == null) {
304 r = stack.topRunningActivityLocked(null);
305 }
Craig Mautner86d67a42013-05-14 10:34:38 -0700306 if (r != null && !r.isHomeActivity() && r.isRootActivity()) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700307 r.task.mOnTopOfHome = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700308 }
309 }
Craig Mautner2219a1b2013-03-25 09:44:30 -0700310 }
311
Craig Mautner27084302013-03-25 08:05:25 -0700312 void setDismissKeyguard(boolean dismiss) {
313 mDismissKeyguardOnNextActivity = dismiss;
314 }
315
Craig Mautner8d341ef2013-03-26 09:03:27 -0700316 TaskRecord anyTaskForIdLocked(int id) {
317 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
318 ActivityStack stack = mStacks.get(stackNdx);
319 TaskRecord task = stack.taskForIdLocked(id);
320 if (task != null) {
321 return task;
322 }
323 }
324 return null;
325 }
326
Craig Mautner6170f732013-04-02 13:05:23 -0700327 ActivityRecord isInAnyStackLocked(IBinder token) {
328 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
329 final ActivityRecord r = mStacks.get(stackNdx).isInStackLocked(token);
330 if (r != null) {
331 return r;
332 }
333 }
334 return null;
335 }
336
Craig Mautner8d341ef2013-03-26 09:03:27 -0700337 int getNextTaskId() {
338 do {
339 mCurTaskId++;
340 if (mCurTaskId <= 0) {
341 mCurTaskId = 1;
342 }
343 } while (anyTaskForIdLocked(mCurTaskId) != null);
344 return mCurTaskId;
345 }
346
Craig Mautnerde4ef022013-04-07 19:01:33 -0700347 void removeTask(TaskRecord task) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -0700348 mWindowManager.removeTask(task.taskId);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700349 final ActivityStack stack = task.stack;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -0700350 final ActivityRecord r = stack.mResumedActivity;
351 if (r != null && r.task == task) {
352 stack.mResumedActivity = null;
353 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700354 if (stack.removeTask(task) && !stack.isHomeStack()) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -0700355 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing stack " + stack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700356 mStacks.remove(stack);
Craig Mautner4cd0c13f2013-04-16 15:55:52 -0700357 final int stackId = stack.mStackId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700358 final int nextStackId = mWindowManager.removeStack(stackId);
Craig Mautnera9a3fb12013-04-18 10:01:00 -0700359 // TODO: Perhaps we need to let the ActivityManager determine the next focus...
Craig Mautnerac6f8432013-07-17 13:24:59 -0700360 if (mFocusedStack == null || mFocusedStack.mStackId == stackId) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700361 // If this is the last app stack, set mFocusedStack to null.
Craig Mautner29219d92013-04-16 20:19:12 -0700362 mFocusedStack = nextStackId == HOME_STACK_ID ? null : getStack(nextStackId);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700363 }
364 }
365 }
366
367 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700368 ActivityStack stack = getFocusedStack();
369 if (stack == null) {
370 return null;
371 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700372 ActivityRecord resumedActivity = stack.mResumedActivity;
373 if (resumedActivity == null || resumedActivity.app == null) {
374 resumedActivity = stack.mPausingActivity;
375 if (resumedActivity == null || resumedActivity.app == null) {
376 resumedActivity = stack.topRunningActivityLocked(null);
377 }
378 }
379 return resumedActivity;
380 }
381
Craig Mautner20e72272013-04-01 13:45:53 -0700382 boolean attachApplicationLocked(ProcessRecord app, boolean headless) throws Exception {
383 boolean didSomething = false;
384 final String processName = app.processName;
385 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
386 final ActivityStack stack = mStacks.get(stackNdx);
Craig Mautner858d8a62013-04-23 17:08:34 -0700387 if (!isFrontStack(stack)) {
388 continue;
389 }
Craig Mautner20e72272013-04-01 13:45:53 -0700390 ActivityRecord hr = stack.topRunningActivityLocked(null);
391 if (hr != null) {
392 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
393 && processName.equals(hr.processName)) {
394 try {
395 if (headless) {
396 Slog.e(TAG, "Starting activities not supported on headless device: "
397 + hr);
Craig Mautner2420ead2013-04-01 17:13:20 -0700398 } else if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner20e72272013-04-01 13:45:53 -0700399 didSomething = true;
400 }
401 } catch (Exception e) {
402 Slog.w(TAG, "Exception in new application when starting activity "
403 + hr.intent.getComponent().flattenToShortString(), e);
404 throw e;
405 }
Craig Mautner20e72272013-04-01 13:45:53 -0700406 }
407 }
408 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700409 if (!didSomething) {
410 ensureActivitiesVisibleLocked(null, 0);
411 }
Craig Mautner20e72272013-04-01 13:45:53 -0700412 return didSomething;
413 }
414
415 boolean allResumedActivitiesIdle() {
416 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700417 final ActivityStack stack = mStacks.get(stackNdx);
418 if (!isFrontStack(stack)) {
419 continue;
420 }
421 final ActivityRecord resumedActivity = stack.mResumedActivity;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -0700422 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner20e72272013-04-01 13:45:53 -0700423 return false;
424 }
425 }
426 return true;
427 }
428
Craig Mautnerde4ef022013-04-07 19:01:33 -0700429 boolean allResumedActivitiesComplete() {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700430 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
431 final ActivityStack stack = mStacks.get(stackNdx);
Craig Mautner967212c2013-04-13 21:10:58 -0700432 if (isFrontStack(stack)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700433 final ActivityRecord r = stack.mResumedActivity;
434 if (r != null && r.state != ActivityState.RESUMED) {
435 return false;
436 }
437 }
438 }
439 // TODO: Not sure if this should check if all Paused are complete too.
440 switch (mStackState) {
441 case STACK_STATE_HOME_TO_BACK:
Craig Mautner76ea2242013-05-15 11:40:05 -0700442 if (DEBUG_STACK) Slog.d(TAG, "allResumedActivitiesComplete: mStackState old=" +
443 stackStateToString(STACK_STATE_HOME_TO_BACK) + " new=" +
444 stackStateToString(STACK_STATE_HOME_IN_BACK));
Craig Mautnerde4ef022013-04-07 19:01:33 -0700445 mStackState = STACK_STATE_HOME_IN_BACK;
446 break;
447 case STACK_STATE_HOME_TO_FRONT:
Craig Mautner76ea2242013-05-15 11:40:05 -0700448 if (DEBUG_STACK) Slog.d(TAG, "allResumedActivitiesComplete: mStackState old=" +
449 stackStateToString(STACK_STATE_HOME_TO_FRONT) + " new=" +
450 stackStateToString(STACK_STATE_HOME_IN_FRONT));
Craig Mautnerde4ef022013-04-07 19:01:33 -0700451 mStackState = STACK_STATE_HOME_IN_FRONT;
452 break;
453 }
454 return true;
455 }
456
457 boolean allResumedActivitiesVisible() {
458 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
459 final ActivityStack stack = mStacks.get(stackNdx);
460 final ActivityRecord r = stack.mResumedActivity;
461 if (r != null && (!r.nowVisible || r.waitingVisible)) {
462 return false;
463 }
464 }
465 return true;
466 }
467
Craig Mautner2acc3892013-09-23 10:28:14 -0700468 /**
469 * Pause all activities in either all of the stacks or just the back stacks.
470 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
471 * @param allStacks Whether to pause all the stacks (true), or just the back stacks (false).
472 * @return true if any activity was paused as a result of this call.
473 */
474 boolean pauseStacks(boolean userLeaving, boolean allStacks) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700475 boolean someActivityPaused = false;
476 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
477 final ActivityStack stack = mStacks.get(stackNdx);
Craig Mautner2acc3892013-09-23 10:28:14 -0700478 if ((allStacks || !isFrontStack(stack)) && stack.mResumedActivity != null) {
479 if (DEBUG_STATES) Slog.d(TAG, "pauseStacks: stack=" + stack +
Craig Mautnerac6f8432013-07-17 13:24:59 -0700480 " mResumedActivity=" + stack.mResumedActivity);
Craig Mautnercf910b02013-04-23 11:23:27 -0700481 stack.startPausingLocked(userLeaving, false);
482 someActivityPaused = true;
483 }
484 }
485 return someActivityPaused;
486 }
487
Craig Mautnerde4ef022013-04-07 19:01:33 -0700488 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700489 boolean pausing = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700490 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
491 final ActivityStack stack = mStacks.get(stackNdx);
Craig Mautner69ada552013-04-18 13:51:51 -0700492 final ActivityRecord r = stack.mPausingActivity;
493 if (r != null && r.state != ActivityState.PAUSED
494 && r.state != ActivityState.STOPPED
495 && r.state != ActivityState.STOPPING) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700496 if (DEBUG_STATES) {
497 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
498 pausing = false;
499 } else {
500 return false;
501 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700502 }
503 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700504 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700505 }
506
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700507 void reportActivityVisibleLocked(ActivityRecord r) {
Craig Mautner858d8a62013-04-23 17:08:34 -0700508 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700509 WaitResult w = mWaitingActivityVisible.get(i);
510 w.timeout = false;
511 if (r != null) {
512 w.who = new ComponentName(r.info.packageName, r.info.name);
513 }
514 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
515 w.thisTime = w.totalTime;
516 }
517 mService.notifyAll();
518 dismissKeyguard();
519 }
520
521 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
522 long thisTime, long totalTime) {
523 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700524 WaitResult w = mWaitingActivityLaunched.remove(i);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700525 w.timeout = timeout;
526 if (r != null) {
527 w.who = new ComponentName(r.info.packageName, r.info.name);
528 }
529 w.thisTime = thisTime;
530 w.totalTime = totalTime;
531 }
532 mService.notifyAll();
533 }
534
Craig Mautner29219d92013-04-16 20:19:12 -0700535 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700536 final ActivityStack focusedStack = getFocusedStack();
537 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
538 if (r != null) {
539 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700540 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700541
Craig Mautner29219d92013-04-16 20:19:12 -0700542 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
543 final ActivityStack stack = mStacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700544 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700545 r = stack.topRunningActivityLocked(null);
546 if (r != null) {
547 return r;
548 }
549 }
550 }
551 return null;
552 }
553
Craig Mautner20e72272013-04-01 13:45:53 -0700554 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
555 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
556 ActivityRecord r = null;
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700557
558 // Gather all of the running tasks for each stack into runningTaskLists.
559 final int numStacks = mStacks.size();
560 ArrayList<RunningTaskInfo>[] runningTaskLists = new ArrayList[numStacks];
561 for (int stackNdx = numStacks - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner20e72272013-04-01 13:45:53 -0700562 final ActivityStack stack = mStacks.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700563 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
564 runningTaskLists[stackNdx] = stackTaskList;
565 final ActivityRecord ar = stack.getTasksLocked(receiver, pending, stackTaskList);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700566 if (isFrontStack(stack)) {
Craig Mautner20e72272013-04-01 13:45:53 -0700567 r = ar;
568 }
569 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700570
571 // The lists are already sorted from most recent to oldest. Just pull the most recent off
572 // each list and add it to list. Stop when all lists are empty or maxNum reached.
573 while (maxNum > 0) {
574 long mostRecentActiveTime = Long.MIN_VALUE;
575 ArrayList<RunningTaskInfo> selectedStackList = null;
576 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
577 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists[stackNdx];
578 if (!stackTaskList.isEmpty()) {
579 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
580 if (lastActiveTime > mostRecentActiveTime) {
581 mostRecentActiveTime = lastActiveTime;
582 selectedStackList = stackTaskList;
583 }
584 }
585 }
586 if (selectedStackList != null) {
587 list.add(selectedStackList.remove(0));
588 --maxNum;
589 } else {
590 break;
591 }
592 }
593
Craig Mautner20e72272013-04-01 13:45:53 -0700594 return r;
595 }
596
Craig Mautner23ac33b2013-04-01 16:26:35 -0700597 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
598 String profileFile, ParcelFileDescriptor profileFd, int userId) {
599 // Collect information about the target of the Intent.
600 ActivityInfo aInfo;
601 try {
602 ResolveInfo rInfo =
603 AppGlobals.getPackageManager().resolveIntent(
604 intent, resolvedType,
605 PackageManager.MATCH_DEFAULT_ONLY
606 | ActivityManagerService.STOCK_PM_FLAGS, userId);
607 aInfo = rInfo != null ? rInfo.activityInfo : null;
608 } catch (RemoteException e) {
609 aInfo = null;
610 }
611
612 if (aInfo != null) {
613 // Store the found target back into the intent, because now that
614 // we have it we never want to do this again. For example, if the
615 // user navigates back to this point in the history, we should
616 // always restart the exact same activity.
617 intent.setComponent(new ComponentName(
618 aInfo.applicationInfo.packageName, aInfo.name));
619
620 // Don't debug things in the system process
621 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
622 if (!aInfo.processName.equals("system")) {
623 mService.setDebugApp(aInfo.processName, true, false);
624 }
625 }
626
627 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
628 if (!aInfo.processName.equals("system")) {
629 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
630 }
631 }
632
633 if (profileFile != null) {
634 if (!aInfo.processName.equals("system")) {
635 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName,
636 profileFile, profileFd,
637 (startFlags&ActivityManager.START_FLAG_AUTO_STOP_PROFILER) != 0);
638 }
639 }
640 }
641 return aInfo;
642 }
643
Craig Mautner2219a1b2013-03-25 09:44:30 -0700644 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700645 moveHomeStack(true);
Craig Mautner6170f732013-04-02 13:05:23 -0700646 startActivityLocked(null, intent, null, aInfo, null, null, 0, 0, 0, null, 0,
Craig Mautner2219a1b2013-03-25 09:44:30 -0700647 null, false, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700648 }
649
Craig Mautner23ac33b2013-04-01 16:26:35 -0700650 final int startActivityMayWait(IApplicationThread caller, int callingUid,
651 String callingPackage, Intent intent, String resolvedType, IBinder resultTo,
652 String resultWho, int requestCode, int startFlags, String profileFile,
653 ParcelFileDescriptor profileFd, WaitResult outResult, Configuration config,
654 Bundle options, int userId) {
655 // Refuse possible leaked file descriptors
656 if (intent != null && intent.hasFileDescriptors()) {
657 throw new IllegalArgumentException("File descriptors passed in Intent");
658 }
659 boolean componentSpecified = intent.getComponent() != null;
660
661 // Don't modify the client's object!
662 intent = new Intent(intent);
663
664 // Collect information about the target of the Intent.
665 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
666 profileFile, profileFd, userId);
667
668 synchronized (mService) {
669 int callingPid;
670 if (callingUid >= 0) {
671 callingPid = -1;
672 } else if (caller == null) {
673 callingPid = Binder.getCallingPid();
674 callingUid = Binder.getCallingUid();
675 } else {
676 callingPid = callingUid = -1;
677 }
678
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700679 final ActivityStack stack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700680 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700681 && mService.mConfiguration.diff(config) != 0;
682 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700683 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700684
685 final long origId = Binder.clearCallingIdentity();
686
687 if (aInfo != null &&
688 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
689 // This may be a heavy-weight process! Check to see if we already
690 // have another, different heavy-weight process running.
691 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
692 if (mService.mHeavyWeightProcess != null &&
693 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
694 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700695 int realCallingUid = callingUid;
696 if (caller != null) {
697 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
698 if (callerApp != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700699 realCallingUid = callerApp.info.uid;
700 } else {
701 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700702 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700703 + intent.toString());
704 ActivityOptions.abort(options);
705 return ActivityManager.START_PERMISSION_DENIED;
706 }
707 }
708
709 IIntentSender target = mService.getIntentSenderLocked(
710 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
711 realCallingUid, userId, null, null, 0, new Intent[] { intent },
712 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
713 | PendingIntent.FLAG_ONE_SHOT, null);
714
715 Intent newIntent = new Intent();
716 if (requestCode >= 0) {
717 // Caller is requesting a result.
718 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
719 }
720 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
721 new IntentSender(target));
722 if (mService.mHeavyWeightProcess.activities.size() > 0) {
723 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
724 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
725 hist.packageName);
726 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
727 hist.task.taskId);
728 }
729 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
730 aInfo.packageName);
731 newIntent.setFlags(intent.getFlags());
732 newIntent.setClassName("android",
733 HeavyWeightSwitcherActivity.class.getName());
734 intent = newIntent;
735 resolvedType = null;
736 caller = null;
737 callingUid = Binder.getCallingUid();
738 callingPid = Binder.getCallingPid();
739 componentSpecified = true;
740 try {
741 ResolveInfo rInfo =
742 AppGlobals.getPackageManager().resolveIntent(
743 intent, null,
744 PackageManager.MATCH_DEFAULT_ONLY
745 | ActivityManagerService.STOCK_PM_FLAGS, userId);
746 aInfo = rInfo != null ? rInfo.activityInfo : null;
747 aInfo = mService.getActivityInfoForUser(aInfo, userId);
748 } catch (RemoteException e) {
749 aInfo = null;
750 }
751 }
752 }
753 }
754
Craig Mautner6170f732013-04-02 13:05:23 -0700755 int res = startActivityLocked(caller, intent, resolvedType,
Craig Mautner23ac33b2013-04-01 16:26:35 -0700756 aInfo, resultTo, resultWho, requestCode, callingPid, callingUid,
757 callingPackage, startFlags, options, componentSpecified, null);
758
Craig Mautnerde4ef022013-04-07 19:01:33 -0700759 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700760 // If the caller also wants to switch to a new configuration,
761 // do so now. This allows a clean switch, as we are waiting
762 // for the current activity to pause (so we will not destroy
763 // it), and have not yet started the next activity.
764 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
765 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700766 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700767 if (DEBUG_CONFIGURATION) Slog.v(TAG,
768 "Updating to new configuration after starting activity.");
769 mService.updateConfigurationLocked(config, null, false, false);
770 }
771
772 Binder.restoreCallingIdentity(origId);
773
774 if (outResult != null) {
775 outResult.result = res;
776 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700777 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700778 do {
779 try {
780 mService.wait();
781 } catch (InterruptedException e) {
782 }
783 } while (!outResult.timeout && outResult.who == null);
784 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700785 ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700786 if (r.nowVisible) {
787 outResult.timeout = false;
788 outResult.who = new ComponentName(r.info.packageName, r.info.name);
789 outResult.totalTime = 0;
790 outResult.thisTime = 0;
791 } else {
792 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700793 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700794 do {
795 try {
796 mService.wait();
797 } catch (InterruptedException e) {
798 }
799 } while (!outResult.timeout && outResult.who == null);
800 }
801 }
802 }
803
804 return res;
805 }
806 }
807
808 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
809 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
810 Bundle options, int userId) {
811 if (intents == null) {
812 throw new NullPointerException("intents is null");
813 }
814 if (resolvedTypes == null) {
815 throw new NullPointerException("resolvedTypes is null");
816 }
817 if (intents.length != resolvedTypes.length) {
818 throw new IllegalArgumentException("intents are length different than resolvedTypes");
819 }
820
Craig Mautner23ac33b2013-04-01 16:26:35 -0700821
822 int callingPid;
823 if (callingUid >= 0) {
824 callingPid = -1;
825 } else if (caller == null) {
826 callingPid = Binder.getCallingPid();
827 callingUid = Binder.getCallingUid();
828 } else {
829 callingPid = callingUid = -1;
830 }
831 final long origId = Binder.clearCallingIdentity();
832 try {
833 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -0700834 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -0700835 for (int i=0; i<intents.length; i++) {
836 Intent intent = intents[i];
837 if (intent == null) {
838 continue;
839 }
840
841 // Refuse possible leaked file descriptors
842 if (intent != null && intent.hasFileDescriptors()) {
843 throw new IllegalArgumentException("File descriptors passed in Intent");
844 }
845
846 boolean componentSpecified = intent.getComponent() != null;
847
848 // Don't modify the client's object!
849 intent = new Intent(intent);
850
851 // Collect information about the target of the Intent.
852 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i],
853 0, null, null, userId);
854 // TODO: New, check if this is correct
855 aInfo = mService.getActivityInfoForUser(aInfo, userId);
856
857 if (aInfo != null &&
858 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
859 != 0) {
860 throw new IllegalArgumentException(
861 "FLAG_CANT_SAVE_STATE not supported here");
862 }
863
864 Bundle theseOptions;
865 if (options != null && i == intents.length-1) {
866 theseOptions = options;
867 } else {
868 theseOptions = null;
869 }
Craig Mautner6170f732013-04-02 13:05:23 -0700870 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Craig Mautner23ac33b2013-04-01 16:26:35 -0700871 aInfo, resultTo, null, -1, callingPid, callingUid, callingPackage,
872 0, theseOptions, componentSpecified, outActivity);
873 if (res < 0) {
874 return res;
875 }
876
877 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
878 }
879 }
880 } finally {
881 Binder.restoreCallingIdentity(origId);
882 }
883
884 return ActivityManager.START_SUCCESS;
885 }
886
Craig Mautner2420ead2013-04-01 17:13:20 -0700887 final boolean realStartActivityLocked(ActivityRecord r,
888 ProcessRecord app, boolean andResume, boolean checkConfig)
889 throws RemoteException {
890
891 r.startFreezingScreenLocked(app, 0);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700892 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -0700893
894 // schedule launch ticks to collect information about slow apps.
895 r.startLaunchTickingLocked();
896
897 // Have the window manager re-evaluate the orientation of
898 // the screen based on the new activity order. Note that
899 // as a result of this, it can call back into the activity
900 // manager with a new orientation. We don't care about that,
901 // because the activity is not currently running so we are
902 // just restarting it anyway.
903 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700904 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -0700905 mService.mConfiguration,
906 r.mayFreezeScreenLocked(app) ? r.appToken : null);
907 mService.updateConfigurationLocked(config, r, false, false);
908 }
909
910 r.app = app;
911 app.waitingToKill = null;
912 r.launchCount++;
913 r.lastLaunchTime = SystemClock.uptimeMillis();
914
915 if (localLOGV) Slog.v(TAG, "Launching: " + r);
916
917 int idx = app.activities.indexOf(r);
918 if (idx < 0) {
919 app.activities.add(r);
920 }
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700921 mService.updateLruProcessLocked(app, true, true);
Craig Mautner2420ead2013-04-01 17:13:20 -0700922
923 final ActivityStack stack = r.task.stack;
924 try {
925 if (app.thread == null) {
926 throw new RemoteException();
927 }
928 List<ResultInfo> results = null;
929 List<Intent> newIntents = null;
930 if (andResume) {
931 results = r.results;
932 newIntents = r.newIntents;
933 }
934 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
935 + " icicle=" + r.icicle
936 + " with results=" + results + " newIntents=" + newIntents
937 + " andResume=" + andResume);
938 if (andResume) {
939 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
940 r.userId, System.identityHashCode(r),
941 r.task.taskId, r.shortComponentName);
942 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700943 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -0700944 // Home process is the root process of the task.
945 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -0700946 }
947 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
948 r.sleeping = false;
949 r.forceNewConfig = false;
950 mService.showAskCompatModeDialogLocked(r);
951 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
952 String profileFile = null;
953 ParcelFileDescriptor profileFd = null;
954 boolean profileAutoStop = false;
955 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
956 if (mService.mProfileProc == null || mService.mProfileProc == app) {
957 mService.mProfileProc = app;
958 profileFile = mService.mProfileFile;
959 profileFd = mService.mProfileFd;
960 profileAutoStop = mService.mAutoStopProfiler;
961 }
962 }
963 app.hasShownUi = true;
964 app.pendingUiClean = true;
965 if (profileFd != null) {
966 try {
967 profileFd = profileFd.dup();
968 } catch (IOException e) {
969 if (profileFd != null) {
970 try {
971 profileFd.close();
972 } catch (IOException o) {
973 }
974 profileFd = null;
975 }
976 }
977 }
Dianne Hackborna413dc02013-07-12 12:02:55 -0700978 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -0700979 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
980 System.identityHashCode(r), r.info,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700981 new Configuration(mService.mConfiguration), r.compat,
982 app.repProcState, r.icicle, results, newIntents, !andResume,
Craig Mautner2420ead2013-04-01 17:13:20 -0700983 mService.isNextTransitionForward(), profileFile, profileFd,
984 profileAutoStop);
985
986 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
987 // This may be a heavy-weight process! Note that the package
988 // manager will ensure that only activity can run in the main
989 // process of the .apk, which is the only thing that will be
990 // considered heavy-weight.
991 if (app.processName.equals(app.info.packageName)) {
992 if (mService.mHeavyWeightProcess != null
993 && mService.mHeavyWeightProcess != app) {
994 Slog.w(TAG, "Starting new heavy weight process " + app
995 + " when already running "
996 + mService.mHeavyWeightProcess);
997 }
998 mService.mHeavyWeightProcess = app;
999 Message msg = mService.mHandler.obtainMessage(
1000 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1001 msg.obj = r;
1002 mService.mHandler.sendMessage(msg);
1003 }
1004 }
1005
1006 } catch (RemoteException e) {
1007 if (r.launchFailed) {
1008 // This is the second time we failed -- finish activity
1009 // and give up.
1010 Slog.e(TAG, "Second failure launching "
1011 + r.intent.getComponent().flattenToShortString()
1012 + ", giving up", e);
1013 mService.appDiedLocked(app, app.pid, app.thread);
1014 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1015 "2nd-crash", false);
1016 return false;
1017 }
1018
1019 // This is the first time we failed -- restart process and
1020 // retry.
1021 app.activities.remove(r);
1022 throw e;
1023 }
1024
1025 r.launchFailed = false;
1026 if (stack.updateLRUListLocked(r)) {
1027 Slog.w(TAG, "Activity " + r
1028 + " being launched, but already in LRU list");
1029 }
1030
1031 if (andResume) {
1032 // As part of the process of launching, ActivityThread also performs
1033 // a resume.
1034 stack.minimalResumeActivityLocked(r);
1035 } else {
1036 // This activity is not starting in the resumed state... which
1037 // should look like we asked it to pause+stop (but remain visible),
1038 // and it has done so and reported back the current icicle and
1039 // other state.
1040 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1041 + " (starting in stopped state)");
1042 r.state = ActivityState.STOPPED;
1043 r.stopped = true;
1044 }
1045
1046 // Launch the new version setup screen if needed. We do this -after-
1047 // launching the initial activity (that is, home), so that it can have
1048 // a chance to initialize itself while in the background, making the
1049 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001050 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001051 mService.startSetupActivityLocked();
1052 }
1053
1054 return true;
1055 }
1056
Craig Mautnere79d42682013-04-01 19:01:53 -07001057 void startSpecificActivityLocked(ActivityRecord r,
1058 boolean andResume, boolean checkConfig) {
1059 // Is this activity's application already running?
1060 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001061 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001062
1063 r.task.stack.setLaunchTime(r);
1064
1065 if (app != null && app.thread != null) {
1066 try {
Dianne Hackbornd2932242013-08-05 18:18:42 -07001067 app.addPackage(r.info.packageName, mService.mProcessStats);
Craig Mautnere79d42682013-04-01 19:01:53 -07001068 realStartActivityLocked(r, app, andResume, checkConfig);
1069 return;
1070 } catch (RemoteException e) {
1071 Slog.w(TAG, "Exception when starting activity "
1072 + r.intent.getComponent().flattenToShortString(), e);
1073 }
1074
1075 // If a dead object exception was thrown -- fall through to
1076 // restart the application.
1077 }
1078
1079 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001080 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001081 }
1082
Craig Mautner6170f732013-04-02 13:05:23 -07001083 final int startActivityLocked(IApplicationThread caller,
1084 Intent intent, String resolvedType, ActivityInfo aInfo, IBinder resultTo,
1085 String resultWho, int requestCode,
1086 int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
1087 boolean componentSpecified, ActivityRecord[] outActivity) {
1088 int err = ActivityManager.START_SUCCESS;
1089
1090 ProcessRecord callerApp = null;
1091 if (caller != null) {
1092 callerApp = mService.getRecordForAppLocked(caller);
1093 if (callerApp != null) {
1094 callingPid = callerApp.pid;
1095 callingUid = callerApp.info.uid;
1096 } else {
1097 Slog.w(TAG, "Unable to find app for caller " + caller
1098 + " (pid=" + callingPid + ") when starting: "
1099 + intent.toString());
1100 err = ActivityManager.START_PERMISSION_DENIED;
1101 }
1102 }
1103
1104 if (err == ActivityManager.START_SUCCESS) {
1105 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1106 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
1107 + "} from pid " + (callerApp != null ? callerApp.pid : callingPid));
1108 }
1109
1110 ActivityRecord sourceRecord = null;
1111 ActivityRecord resultRecord = null;
1112 if (resultTo != null) {
1113 sourceRecord = isInAnyStackLocked(resultTo);
1114 if (DEBUG_RESULTS) Slog.v(
1115 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1116 if (sourceRecord != null) {
1117 if (requestCode >= 0 && !sourceRecord.finishing) {
1118 resultRecord = sourceRecord;
1119 }
1120 }
1121 }
1122 ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1123
1124 int launchFlags = intent.getFlags();
1125
1126 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
1127 && sourceRecord != null) {
1128 // Transfer the result target from the source activity to the new
1129 // one being started, including any failures.
1130 if (requestCode >= 0) {
1131 ActivityOptions.abort(options);
1132 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1133 }
1134 resultRecord = sourceRecord.resultTo;
1135 resultWho = sourceRecord.resultWho;
1136 requestCode = sourceRecord.requestCode;
1137 sourceRecord.resultTo = null;
1138 if (resultRecord != null) {
1139 resultRecord.removeResultsLocked(
1140 sourceRecord, resultWho, requestCode);
1141 }
1142 }
1143
1144 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1145 // We couldn't find a class that can handle the given Intent.
1146 // That's the end of that!
1147 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1148 }
1149
1150 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1151 // We couldn't find the specific class specified in the Intent.
1152 // Also the end of the line.
1153 err = ActivityManager.START_CLASS_NOT_FOUND;
1154 }
1155
1156 if (err != ActivityManager.START_SUCCESS) {
1157 if (resultRecord != null) {
1158 resultStack.sendActivityResultLocked(-1,
1159 resultRecord, resultWho, requestCode,
1160 Activity.RESULT_CANCELED, null);
1161 }
1162 setDismissKeyguard(false);
1163 ActivityOptions.abort(options);
1164 return err;
1165 }
1166
1167 final int startAnyPerm = mService.checkPermission(
1168 START_ANY_ACTIVITY, callingPid, callingUid);
1169 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1170 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1171 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1172 if (resultRecord != null) {
1173 resultStack.sendActivityResultLocked(-1,
1174 resultRecord, resultWho, requestCode,
1175 Activity.RESULT_CANCELED, null);
1176 }
1177 setDismissKeyguard(false);
1178 String msg;
1179 if (!aInfo.exported) {
1180 msg = "Permission Denial: starting " + intent.toString()
1181 + " from " + callerApp + " (pid=" + callingPid
1182 + ", uid=" + callingUid + ")"
1183 + " not exported from uid " + aInfo.applicationInfo.uid;
1184 } else {
1185 msg = "Permission Denial: starting " + intent.toString()
1186 + " from " + callerApp + " (pid=" + callingPid
1187 + ", uid=" + callingUid + ")"
1188 + " requires " + aInfo.permission;
1189 }
1190 Slog.w(TAG, msg);
1191 throw new SecurityException(msg);
1192 }
1193
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001194 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001195 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001196
Craig Mautner6170f732013-04-02 13:05:23 -07001197 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001198 try {
1199 // The Intent we give to the watcher has the extra data
1200 // stripped off, since it can contain private information.
1201 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001202 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001203 aInfo.applicationInfo.packageName);
1204 } catch (RemoteException e) {
1205 mService.mController = null;
1206 }
Ben Gruver5e207332013-04-03 17:41:37 -07001207 }
Craig Mautner6170f732013-04-02 13:05:23 -07001208
Ben Gruver5e207332013-04-03 17:41:37 -07001209 if (abort) {
1210 if (resultRecord != null) {
1211 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001212 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001213 }
Ben Gruver5e207332013-04-03 17:41:37 -07001214 // We pretend to the caller that it was really started, but
1215 // they will just get a cancel result.
1216 setDismissKeyguard(false);
1217 ActivityOptions.abort(options);
1218 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001219 }
1220
1221 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
1222 intent, resolvedType, aInfo, mService.mConfiguration,
Craig Mautnerde4ef022013-04-07 19:01:33 -07001223 resultRecord, resultWho, requestCode, componentSpecified, this);
Craig Mautner6170f732013-04-02 13:05:23 -07001224 if (outActivity != null) {
1225 outActivity[0] = r;
1226 }
1227
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001228 final ActivityStack stack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001229 if (stack.mResumedActivity == null
1230 || stack.mResumedActivity.info.applicationInfo.uid != callingUid) {
Craig Mautner6170f732013-04-02 13:05:23 -07001231 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
1232 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001233 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautner6170f732013-04-02 13:05:23 -07001234 mService.mPendingActivityLaunches.add(pal);
1235 setDismissKeyguard(false);
1236 ActivityOptions.abort(options);
1237 return ActivityManager.START_SWITCHES_CANCELED;
1238 }
1239 }
1240
1241 if (mService.mDidAppSwitch) {
1242 // This is the second allowed switch since we stopped switches,
1243 // so now just generally allow switches. Use case: user presses
1244 // home (switches disabled, switch to home, mDidAppSwitch now true);
1245 // user taps a home icon (coming from home so allowed, we hit here
1246 // and now allow anyone to switch again).
1247 mService.mAppSwitchesAllowedTime = 0;
1248 } else {
1249 mService.mDidAppSwitch = true;
1250 }
1251
1252 mService.doPendingActivityLaunchesLocked(false);
1253
Craig Mautner8849a5e2013-04-02 16:41:03 -07001254 err = startActivityUncheckedLocked(r, sourceRecord, startFlags, true, options);
Craig Mautner10385a12013-09-22 21:08:32 -07001255
1256 if (allPausedActivitiesComplete()) {
1257 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001258 // activity start, but we are not actually doing an activity
1259 // switch... just dismiss the keyguard now, because we
1260 // probably want to see whatever is behind it.
1261 dismissKeyguard();
1262 }
1263 return err;
1264 }
1265
Craig Mautnerac6f8432013-07-17 13:24:59 -07001266 ActivityStack adjustStackFocus(ActivityRecord r) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001267 final TaskRecord task = r.task;
1268 if (r.isApplicationActivity() || (task != null && task.isApplicationTask())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001269 if (task != null) {
1270 if (mFocusedStack != task.stack) {
1271 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1272 "adjustStackFocus: Setting focused stack to r=" + r + " task=" + task);
1273 mFocusedStack = task.stack;
1274 } else {
1275 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1276 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1277 }
1278 return mFocusedStack;
1279 }
1280
1281 if (mFocusedStack != null) {
1282 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1283 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1284 return mFocusedStack;
1285 }
1286
1287 for (int stackNdx = mStacks.size() - 1; stackNdx > 0; --stackNdx) {
1288 ActivityStack stack = mStacks.get(stackNdx);
1289 if (!stack.isHomeStack()) {
1290 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1291 "adjustStackFocus: Setting focused stack=" + stack);
1292 mFocusedStack = stack;
1293 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001294 }
1295 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001296
1297 // Time to create the first app stack for this user.
1298 int stackId = mService.createStack(-1, HOME_STACK_ID,
1299 StackBox.TASK_STACK_GOES_OVER, 1.0f);
1300 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1301 " stackId=" + stackId);
1302 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001303 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001304 }
1305 return mHomeStack;
1306 }
1307
Craig Mautner29219d92013-04-16 20:19:12 -07001308 void setFocusedStack(ActivityRecord r) {
1309 if (r == null) {
1310 return;
1311 }
Craig Mautner86d67a42013-05-14 10:34:38 -07001312 if (!r.isApplicationActivity() || (r.task != null && !r.task.isApplicationTask())) {
Craig Mautner29219d92013-04-16 20:19:12 -07001313 if (mStackState != STACK_STATE_HOME_IN_FRONT) {
Craig Mautnere7c58b62013-06-12 20:19:00 -07001314 if (DEBUG_STACK || DEBUG_FOCUS) Slog.d(TAG, "setFocusedStack: mStackState old=" +
Craig Mautner76ea2242013-05-15 11:40:05 -07001315 stackStateToString(mStackState) + " new=" +
1316 stackStateToString(STACK_STATE_HOME_TO_FRONT) +
1317 " Callers=" + Debug.getCallers(3));
Craig Mautner29219d92013-04-16 20:19:12 -07001318 mStackState = STACK_STATE_HOME_TO_FRONT;
1319 }
1320 } else {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001321 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1322 "setFocusedStack: Setting focused stack to r=" + r + " task=" + r.task +
1323 " Callers=" + Debug.getCallers(3));
Craig Mautner29219d92013-04-16 20:19:12 -07001324 mFocusedStack = r.task.stack;
1325 if (mStackState != STACK_STATE_HOME_IN_BACK) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001326 if (DEBUG_STACK) Slog.d(TAG, "setFocusedStack: mStackState old=" +
1327 stackStateToString(mStackState) + " new=" +
1328 stackStateToString(STACK_STATE_HOME_TO_BACK) +
1329 " Callers=" + Debug.getCallers(3));
Craig Mautner29219d92013-04-16 20:19:12 -07001330 mStackState = STACK_STATE_HOME_TO_BACK;
1331 }
1332 }
1333 }
1334
Craig Mautner8849a5e2013-04-02 16:41:03 -07001335 final int startActivityUncheckedLocked(ActivityRecord r,
1336 ActivityRecord sourceRecord, int startFlags, boolean doResume,
1337 Bundle options) {
1338 final Intent intent = r.intent;
1339 final int callingUid = r.launchedFromUid;
1340
1341 int launchFlags = intent.getFlags();
1342
Craig Mautner8849a5e2013-04-02 16:41:03 -07001343 // We'll invoke onUserLeaving before onPause only if the launching
1344 // activity did not explicitly state that this is an automated launch.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001345 mUserLeaving = (launchFlags&Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
1346 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001347
1348 // If the caller has asked not to resume at this point, we make note
1349 // of this in the record so that we can skip it when trying to find
1350 // the top running activity.
1351 if (!doResume) {
1352 r.delayedResume = true;
1353 }
1354
1355 ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
1356
1357 // If the onlyIfNeeded flag is set, then we can do this if the activity
1358 // being launched is the same as the one making the call... or, as
1359 // a special case, if we do not know the caller then we count the
1360 // current top activity as the caller.
1361 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1362 ActivityRecord checkedCaller = sourceRecord;
1363 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001364 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001365 }
1366 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1367 // Caller is not the same as launcher, so always needed.
1368 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1369 }
1370 }
1371
1372 if (sourceRecord == null) {
1373 // This activity is not being started from another... in this
1374 // case we -always- start a new task.
1375 if ((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Craig Mautner29219d92013-04-16 20:19:12 -07001376 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1377 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001378 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1379 }
1380 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1381 // The original activity who is starting us is running as a single
1382 // instance... this new activity it is starting must go on its
1383 // own task.
1384 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1385 } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
1386 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
1387 // The activity being started is a single instance... it always
1388 // gets launched into its own task.
1389 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1390 }
1391
Craig Mautnerde4ef022013-04-07 19:01:33 -07001392 final ActivityStack sourceStack;
Craig Mautner525f3d92013-05-07 14:01:50 -07001393 TaskRecord sourceTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001394 if (sourceRecord != null) {
1395 sourceTask = sourceRecord.task;
1396 sourceStack = sourceTask.stack;
1397 } else {
1398 sourceTask = null;
1399 sourceStack = null;
1400 }
1401
Craig Mautner8849a5e2013-04-02 16:41:03 -07001402 if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1403 // For whatever reason this activity is being launched into a new
1404 // task... yet the caller has requested a result back. Well, that
1405 // is pretty messed up, so instead immediately send back a cancel
1406 // and let the new task continue launched as normal without a
1407 // dependency on its originator.
1408 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1409 r.resultTo.task.stack.sendActivityResultLocked(-1,
1410 r.resultTo, r.resultWho, r.requestCode,
1411 Activity.RESULT_CANCELED, null);
1412 r.resultTo = null;
1413 }
1414
1415 boolean addingToTask = false;
1416 boolean movedHome = false;
1417 TaskRecord reuseTask = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001418 ActivityStack targetStack;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001419 if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1420 (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
1421 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
1422 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1423 // If bring to front is requested, and no result is requested, and
1424 // we can find a task that was started with this same
1425 // component, then instead of launching bring that one to the front.
1426 if (r.resultTo == null) {
1427 // See if there is a task to bring to the front. If this is
1428 // a SINGLE_INSTANCE activity, there can be one and only one
1429 // instance of it in the history, and it is always in its own
1430 // unique task, so we do a special search.
1431 ActivityRecord intentActivity = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
Craig Mautnerac6f8432013-07-17 13:24:59 -07001432 ? findTaskLocked(r)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001433 : findActivityLocked(intent, r.info);
1434 if (intentActivity != null) {
Craig Mautner29219d92013-04-16 20:19:12 -07001435 if (r.task == null) {
1436 r.task = intentActivity.task;
1437 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001438 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001439 targetStack.mLastPausedActivity = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001440 moveHomeStack(targetStack.isHomeStack());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001441 if (intentActivity.task.intent == null) {
1442 // This task was started because of movement of
1443 // the activity based on affinity... now that we
1444 // are actually launching it, we can assign the
1445 // base intent.
1446 intentActivity.task.setIntent(intent, r.info);
1447 }
1448 // If the target task is not in the front, then we need
1449 // to bring it to the front... except... well, with
1450 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1451 // to have the same behavior as if a new instance was
1452 // being started, which means not bringing it to the front
1453 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001454 final ActivityStack lastStack = getLastStack();
1455 ActivityRecord curTop = lastStack == null?
1456 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner7504d7b2013-09-17 10:50:53 -07001457 if (curTop != null && (curTop.task != intentActivity.task ||
1458 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001459 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001460 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1461 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001462 // We really do want to push this one into the
1463 // user's face, right now.
1464 movedHome = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001465 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001466 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1467 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001468 // Caller wants to appear on home activity.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001469 intentActivity.task.mOnTopOfHome = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001470 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001471 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
1472 options = null;
1473 }
1474 }
1475 // If the caller has requested that the target task be
1476 // reset, then do so.
1477 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1478 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1479 }
1480 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1481 // We don't need to start a new activity, and
1482 // the client said not to do anything if that
1483 // is the case, so this is it! And for paranoia, make
1484 // sure we have correctly resumed the top activity.
1485 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001486 setLaunchHomeTaskNextFlag(sourceRecord, null, targetStack);
Craig Mautner05d29032013-05-03 13:40:13 -07001487 resumeTopActivitiesLocked(targetStack, null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001488 } else {
1489 ActivityOptions.abort(options);
1490 }
Craig Mautner29219d92013-04-16 20:19:12 -07001491 if (r.task == null) Slog.v(TAG,
1492 "startActivityUncheckedLocked: task left null",
1493 new RuntimeException("here").fillInStackTrace());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001494 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1495 }
1496 if ((launchFlags &
1497 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1498 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1499 // The caller has requested to completely replace any
1500 // existing task with its new activity. Well that should
1501 // not be too hard...
1502 reuseTask = intentActivity.task;
1503 reuseTask.performClearTaskLocked();
1504 reuseTask.setIntent(r.intent, r.info);
1505 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
1506 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
1507 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1508 // In this situation we want to remove all activities
1509 // from the task up to the one being started. In most
1510 // cases this means we are resetting the task to its
1511 // initial state.
1512 ActivityRecord top =
1513 intentActivity.task.performClearTaskLocked(r, launchFlags);
1514 if (top != null) {
1515 if (top.frontOfTask) {
1516 // Activity aliases may mean we use different
1517 // intents for the top activity, so make sure
1518 // the task now has the identity of the new
1519 // intent.
1520 top.task.setIntent(r.intent, r.info);
1521 }
1522 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1523 r, top.task);
1524 top.deliverNewIntentLocked(callingUid, r.intent);
1525 } else {
1526 // A special case: we need to
1527 // start the activity because it is not currently
1528 // running, and the caller has asked to clear the
1529 // current task to have this activity at the top.
1530 addingToTask = true;
1531 // Now pretend like this activity is being started
1532 // by the top of its task, so it is put in the
1533 // right place.
1534 sourceRecord = intentActivity;
1535 }
1536 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1537 // In this case the top activity on the task is the
1538 // same as the one being launched, so we take that
1539 // as a request to bring the task to the foreground.
1540 // If the top activity in the task is the root
1541 // activity, deliver this new intent to it if it
1542 // desires.
1543 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
1544 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
1545 && intentActivity.realActivity.equals(r.realActivity)) {
1546 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1547 intentActivity.task);
1548 if (intentActivity.frontOfTask) {
1549 intentActivity.task.setIntent(r.intent, r.info);
1550 }
1551 intentActivity.deliverNewIntentLocked(callingUid, r.intent);
1552 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1553 // In this case we are launching the root activity
1554 // of the task, but with a different intent. We
1555 // should start a new instance on top.
1556 addingToTask = true;
1557 sourceRecord = intentActivity;
1558 }
1559 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1560 // In this case an activity is being launched in to an
1561 // existing task, without resetting that task. This
1562 // is typically the situation of launching an activity
1563 // from a notification or shortcut. We want to place
1564 // the new activity on top of the current task.
1565 addingToTask = true;
1566 sourceRecord = intentActivity;
1567 } else if (!intentActivity.task.rootWasReset) {
1568 // In this case we are launching in to an existing task
1569 // that has not yet been started from its front door.
1570 // The current task has been brought to the front.
1571 // Ideally, we'd probably like to place this new task
1572 // at the bottom of its stack, but that's a little hard
1573 // to do with the current organization of the code so
1574 // for now we'll just drop it.
1575 intentActivity.task.setIntent(r.intent, r.info);
1576 }
1577 if (!addingToTask && reuseTask == null) {
1578 // We didn't do anything... but it was needed (a.k.a., client
1579 // don't use that intent!) And for paranoia, make
1580 // sure we have correctly resumed the top activity.
1581 if (doResume) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001582 // Reset flag so it gets correctly reevaluated.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001583 intentActivity.task.mOnTopOfHome = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001584 setLaunchHomeTaskNextFlag(sourceRecord, intentActivity, targetStack);
1585 targetStack.resumeTopActivityLocked(null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001586 } else {
1587 ActivityOptions.abort(options);
1588 }
Craig Mautner29219d92013-04-16 20:19:12 -07001589 if (r.task == null) Slog.v(TAG,
1590 "startActivityUncheckedLocked: task left null",
1591 new RuntimeException("here").fillInStackTrace());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001592 return ActivityManager.START_TASK_TO_FRONT;
1593 }
1594 }
1595 }
1596 }
1597
1598 //String uri = r.intent.toURI();
1599 //Intent intent2 = new Intent(uri);
1600 //Slog.i(TAG, "Given intent: " + r.intent);
1601 //Slog.i(TAG, "URI is: " + uri);
1602 //Slog.i(TAG, "To intent: " + intent2);
1603
1604 if (r.packageName != null) {
1605 // If the activity being launched is the same as the one currently
1606 // at the top, then we need to check if it should only be launched
1607 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001608 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001609 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001610 if (top != null && r.resultTo == null) {
1611 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
1612 if (top.app != null && top.app.thread != null) {
1613 if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
1614 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP
1615 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
1616 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
1617 top.task);
1618 // For paranoia, make sure we have correctly
1619 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001620 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001621 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001622 setLaunchHomeTaskNextFlag(sourceRecord, null, topStack);
Craig Mautner05d29032013-05-03 13:40:13 -07001623 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001624 }
1625 ActivityOptions.abort(options);
1626 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1627 // We don't need to start a new activity, and
1628 // the client said not to do anything if that
1629 // is the case, so this is it!
Craig Mautner29219d92013-04-16 20:19:12 -07001630 if (r.task == null) Slog.v(TAG,
1631 "startActivityUncheckedLocked: task left null",
1632 new RuntimeException("here").fillInStackTrace());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001633 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1634 }
1635 top.deliverNewIntentLocked(callingUid, r.intent);
Craig Mautner29219d92013-04-16 20:19:12 -07001636 if (r.task == null) Slog.v(TAG,
1637 "startActivityUncheckedLocked: task left null",
1638 new RuntimeException("here").fillInStackTrace());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001639 return ActivityManager.START_DELIVERED_TO_TOP;
1640 }
1641 }
1642 }
1643 }
1644
1645 } else {
1646 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001647 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
1648 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001649 }
1650 ActivityOptions.abort(options);
Craig Mautner29219d92013-04-16 20:19:12 -07001651 if (r.task == null) Slog.v(TAG,
1652 "startActivityUncheckedLocked: task left null",
1653 new RuntimeException("here").fillInStackTrace());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001654 return ActivityManager.START_CLASS_NOT_FOUND;
1655 }
1656
1657 boolean newTask = false;
1658 boolean keepCurTransition = false;
1659
1660 // Should this be considered a new task?
1661 if (r.resultTo == null && !addingToTask
1662 && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001663 targetStack = adjustStackFocus(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001664 moveHomeStack(targetStack.isHomeStack());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001665 if (reuseTask == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001666 r.setTask(targetStack.createTaskRecord(getNextTaskId(), r.info, intent, true),
1667 null, true);
1668 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
1669 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001670 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001671 r.setTask(reuseTask, reuseTask, true);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001672 }
1673 newTask = true;
1674 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001675 if ((launchFlags &
1676 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
1677 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
1678 // Caller wants to appear on home activity, so before starting
1679 // their own activity we will bring home to the front.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001680 r.task.mOnTopOfHome = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001681 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001682 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001683 } else if (sourceRecord != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001684 sourceTask = sourceRecord.task;
1685 targetStack = sourceTask.stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001686 moveHomeStack(targetStack.isHomeStack());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001687 if (!addingToTask &&
1688 (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
1689 // In this case, we are adding the activity to an existing
1690 // task, but the caller has asked to clear that task if the
1691 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07001692 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001693 keepCurTransition = true;
1694 if (top != null) {
1695 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
1696 top.deliverNewIntentLocked(callingUid, r.intent);
1697 // For paranoia, make sure we have correctly
1698 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001699 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001700 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001701 setLaunchHomeTaskNextFlag(sourceRecord, null, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001702 targetStack.resumeTopActivityLocked(null);
1703 }
1704 ActivityOptions.abort(options);
Craig Mautner29219d92013-04-16 20:19:12 -07001705 if (r.task == null) Slog.v(TAG,
1706 "startActivityUncheckedLocked: task left null",
1707 new RuntimeException("here").fillInStackTrace());
Craig Mautner8849a5e2013-04-02 16:41:03 -07001708 return ActivityManager.START_DELIVERED_TO_TOP;
1709 }
1710 } else if (!addingToTask &&
1711 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
1712 // In this case, we are launching an activity in our own task
1713 // that may already be running somewhere in the history, and
1714 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07001715 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001716 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001717 final TaskRecord task = top.task;
1718 task.moveActivityToFrontLocked(top);
1719 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001720 top.updateOptionsLocked(options);
1721 top.deliverNewIntentLocked(callingUid, r.intent);
Craig Mautner0f922742013-08-06 08:44:42 -07001722 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001723 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001724 setLaunchHomeTaskNextFlag(sourceRecord, null, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001725 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
1735 + " in existing task " + r.task);
1736
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 Mautnerde4ef022013-04-07 19:01:33 -07001758 setLaunchHomeTaskNextFlag(sourceRecord, r, targetStack);
Craig Mautner0f922742013-08-06 08:44:42 -07001759 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001760 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautner1d001b62013-06-18 16:52:43 -07001761 mService.setFocusedActivityLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001762 return ActivityManager.START_SUCCESS;
1763 }
1764
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001765 void acquireLaunchWakelock() {
1766 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1767 throw new IllegalStateException("Calling must be system uid");
1768 }
1769 mLaunchingActivity.acquire();
1770 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1771 // To be safe, don't allow the wake lock to be held for too long.
1772 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1773 }
1774 }
1775
Craig Mautnerf3333272013-04-22 10:55:53 -07001776 // Checked.
1777 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1778 Configuration config) {
1779 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
1780
Craig Mautnerf3333272013-04-22 10:55:53 -07001781 ArrayList<ActivityRecord> stops = null;
1782 ArrayList<ActivityRecord> finishes = null;
1783 ArrayList<UserStartedState> startingUsers = null;
1784 int NS = 0;
1785 int NF = 0;
1786 IApplicationThread sendThumbnail = null;
1787 boolean booting = false;
1788 boolean enableScreen = false;
1789 boolean activityRemoved = false;
1790
1791 ActivityRecord r = ActivityRecord.forToken(token);
1792 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001793 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
1794 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001795 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1796 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001797 if (fromTimeout) {
1798 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001799 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001800
1801 // This is a hack to semi-deal with a race condition
1802 // in the client where it can be constructed with a
1803 // newer configuration from when we asked it to launch.
1804 // We'll update with whatever configuration it now says
1805 // it used to launch.
1806 if (config != null) {
1807 r.configuration = config;
1808 }
1809
1810 // We are now idle. If someone is waiting for a thumbnail from
1811 // us, we can now deliver.
1812 r.idle = true;
1813
1814 if (r.thumbnailNeeded && r.app != null && r.app.thread != null) {
1815 sendThumbnail = r.app.thread;
1816 r.thumbnailNeeded = false;
1817 }
1818
1819 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
1820 if (!mService.mBooted && isFrontStack(r.task.stack)) {
1821 mService.mBooted = true;
1822 enableScreen = true;
1823 }
1824 }
1825
1826 if (allResumedActivitiesIdle()) {
1827 if (r != null) {
1828 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001829 }
1830
1831 if (mLaunchingActivity.isHeld()) {
1832 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1833 if (VALIDATE_WAKE_LOCK_CALLER &&
1834 Binder.getCallingUid() != Process.myUid()) {
1835 throw new IllegalStateException("Calling must be system uid");
1836 }
1837 mLaunchingActivity.release();
1838 }
1839 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07001840 }
1841
1842 // Atomically retrieve all of the other things to do.
1843 stops = processStoppingActivitiesLocked(true);
1844 NS = stops != null ? stops.size() : 0;
1845 if ((NF=mFinishingActivities.size()) > 0) {
1846 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
1847 mFinishingActivities.clear();
1848 }
1849
1850 final ArrayList<ActivityRecord> thumbnails;
1851 final int NT = mCancelledThumbnails.size();
1852 if (NT > 0) {
1853 thumbnails = new ArrayList<ActivityRecord>(mCancelledThumbnails);
1854 mCancelledThumbnails.clear();
1855 } else {
1856 thumbnails = null;
1857 }
1858
1859 if (isFrontStack(mHomeStack)) {
1860 booting = mService.mBooting;
1861 mService.mBooting = false;
1862 }
1863
1864 if (mStartingUsers.size() > 0) {
1865 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
1866 mStartingUsers.clear();
1867 }
1868
1869 // Perform the following actions from unsynchronized state.
1870 final IApplicationThread thumbnailThread = sendThumbnail;
1871 mHandler.post(new Runnable() {
1872 @Override
1873 public void run() {
1874 if (thumbnailThread != null) {
1875 try {
1876 thumbnailThread.requestThumbnail(token);
1877 } catch (Exception e) {
1878 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
1879 mService.sendPendingThumbnail(null, token, null, null, true);
1880 }
1881 }
1882
1883 // Report back to any thumbnail receivers.
1884 for (int i = 0; i < NT; i++) {
1885 ActivityRecord r = thumbnails.get(i);
1886 mService.sendPendingThumbnail(r, null, null, null, true);
1887 }
1888 }
1889 });
1890
1891 // Stop any activities that are scheduled to do so but have been
1892 // waiting for the next one to start.
1893 for (int i = 0; i < NS; i++) {
1894 r = stops.get(i);
1895 final ActivityStack stack = r.task.stack;
1896 if (r.finishing) {
1897 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1898 } else {
1899 stack.stopActivityLocked(r);
1900 }
1901 }
1902
1903 // Finish any activities that are scheduled to do so but have been
1904 // waiting for the next one to start.
1905 for (int i = 0; i < NF; i++) {
1906 r = finishes.get(i);
1907 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, false, "finish-idle");
1908 }
1909
1910 if (booting) {
1911 mService.finishBooting();
1912 } else if (startingUsers != null) {
1913 for (int i = 0; i < startingUsers.size(); i++) {
1914 mService.finishUserSwitch(startingUsers.get(i));
1915 }
1916 }
1917
1918 mService.trimApplications();
1919 //dump();
1920 //mWindowManager.dump();
1921
1922 if (enableScreen) {
1923 mService.enableScreenAfterBoot();
1924 }
1925
1926 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07001927 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001928 }
1929
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001930 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001931 }
1932
Craig Mautner8d341ef2013-03-26 09:03:27 -07001933 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
1934 // Just in case.
1935 final int numStacks = mStacks.size();
1936 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001937 mStacks.get(stackNdx).handleAppDiedLocked(app, restarting);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001938 }
1939 }
1940
1941 void closeSystemDialogsLocked() {
1942 final int numStacks = mStacks.size();
1943 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1944 final ActivityStack stack = mStacks.get(stackNdx);
1945 stack.closeSystemDialogsLocked();
1946 }
1947 }
1948
1949 /**
1950 * @return true if some activity was finished (or would have finished if doit were true).
1951 */
1952 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
1953 boolean didSomething = false;
1954 final int numStacks = mStacks.size();
1955 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1956 final ActivityStack stack = mStacks.get(stackNdx);
1957 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
1958 didSomething = true;
1959 }
1960 }
1961 return didSomething;
1962 }
1963
Dianne Hackborna413dc02013-07-12 12:02:55 -07001964 void updatePreviousProcessLocked(ActivityRecord r) {
1965 // Now that this process has stopped, we may want to consider
1966 // it to be the previous app to try to keep around in case
1967 // the user wants to return to it.
1968
1969 // First, found out what is currently the foreground app, so that
1970 // we don't blow away the previous app if this activity is being
1971 // hosted by the process that is actually still the foreground.
1972 ProcessRecord fgApp = null;
1973 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1974 final ActivityStack stack = mStacks.get(stackNdx);
1975 if (isFrontStack(stack)) {
1976 if (stack.mResumedActivity != null) {
1977 fgApp = stack.mResumedActivity.app;
1978 } else if (stack.mPausingActivity != null) {
1979 fgApp = stack.mPausingActivity.app;
1980 }
1981 break;
1982 }
1983 }
1984
1985 // Now set this one as the previous process, only if that really
1986 // makes sense to.
1987 if (r.app != null && fgApp != null && r.app != fgApp
1988 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001989 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001990 mService.mPreviousProcess = r.app;
1991 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1992 }
1993 }
1994
Craig Mautner05d29032013-05-03 13:40:13 -07001995 boolean resumeTopActivitiesLocked() {
1996 return resumeTopActivitiesLocked(null, null, null);
1997 }
1998
1999 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2000 Bundle targetOptions) {
2001 if (targetStack == null) {
2002 targetStack = getFocusedStack();
2003 }
2004 boolean result = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002005 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002006 final ActivityStack stack = mStacks.get(stackNdx);
2007 if (isFrontStack(stack)) {
Craig Mautner05d29032013-05-03 13:40:13 -07002008 if (stack == targetStack) {
2009 result = stack.resumeTopActivityLocked(target, targetOptions);
2010 } else {
2011 stack.resumeTopActivityLocked(null);
2012 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002013 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002014 }
Craig Mautner05d29032013-05-03 13:40:13 -07002015 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002016 }
2017
2018 void finishTopRunningActivityLocked(ProcessRecord app) {
2019 final int numStacks = mStacks.size();
2020 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2021 final ActivityStack stack = mStacks.get(stackNdx);
2022 stack.finishTopRunningActivityLocked(app);
2023 }
2024 }
2025
Craig Mautner8d341ef2013-03-26 09:03:27 -07002026 void findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
2027 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2028 if (mStacks.get(stackNdx).findTaskToMoveToFrontLocked(taskId, flags, options)) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002029 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack=" +
Craig Mautner1d001b62013-06-18 16:52:43 -07002030 mStacks.get(stackNdx));
Craig Mautner8d341ef2013-03-26 09:03:27 -07002031 return;
2032 }
2033 }
2034 }
2035
Craig Mautner967212c2013-04-13 21:10:58 -07002036 ActivityStack getStack(int stackId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002037 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2038 final ActivityStack stack = mStacks.get(stackNdx);
2039 if (stack.getStackId() == stackId) {
2040 return stack;
2041 }
2042 }
2043 return null;
2044 }
2045
Craig Mautner967212c2013-04-13 21:10:58 -07002046 ArrayList<ActivityStack> getStacks() {
2047 return new ArrayList<ActivityStack>(mStacks);
2048 }
2049
2050 int createStack() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002051 while (true) {
2052 if (++mLastStackId <= HOME_STACK_ID) {
2053 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002054 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002055 if (getStack(mLastStackId) == null) {
2056 break;
2057 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002058 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002059 mStacks.add(new ActivityStack(mService, mContext, mLooper, mLastStackId));
2060 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002061 }
2062
2063 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002064 final TaskRecord task = anyTaskForIdLocked(taskId);
2065 if (task == null) {
2066 return;
2067 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002068 final ActivityStack stack = getStack(stackId);
2069 if (stack == null) {
2070 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2071 return;
2072 }
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002073 removeTask(task);
2074 stack.addTask(task, toTop);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002075 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002076 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002077 }
2078
Craig Mautnerac6f8432013-07-17 13:24:59 -07002079 ActivityRecord findTaskLocked(ActivityRecord r) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002080 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002081 final ActivityStack stack = mStacks.get(stackNdx);
2082 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
2083 continue;
2084 }
2085 final ActivityRecord ar = stack.findTaskLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002086 if (ar != null) {
2087 return ar;
2088 }
2089 }
2090 return null;
2091 }
2092
2093 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
2094 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2095 final ActivityRecord ar = mStacks.get(stackNdx).findActivityLocked(intent, info);
2096 if (ar != null) {
2097 return ar;
2098 }
2099 }
2100 return null;
2101 }
2102
Craig Mautner8d341ef2013-03-26 09:03:27 -07002103 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002104 scheduleSleepTimeout();
2105 if (!mGoingToSleep.isHeld()) {
2106 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002107 if (mLaunchingActivity.isHeld()) {
2108 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2109 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002110 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002111 mLaunchingActivity.release();
2112 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002113 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002114 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002115 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002116 }
2117
2118 boolean shutdownLocked(int timeout) {
2119 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002120 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002121
2122 final long endTime = System.currentTimeMillis() + timeout;
2123 while (true) {
2124 boolean cantShutdown = false;
2125 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2126 cantShutdown |= mStacks.get(stackNdx).checkReadyForSleepLocked();
2127 }
2128 if (cantShutdown) {
2129 long timeRemaining = endTime - System.currentTimeMillis();
2130 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002131 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002132 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002133 } catch (InterruptedException e) {
2134 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002135 } else {
2136 Slog.w(TAG, "Activity manager shutdown timed out");
2137 timedout = true;
2138 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002139 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002140 } else {
2141 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002142 }
2143 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002144
2145 // Force checkReadyForSleep to complete.
2146 mSleepTimeout = true;
2147 checkReadyForSleepLocked();
2148
Craig Mautner8d341ef2013-03-26 09:03:27 -07002149 return timedout;
2150 }
2151
2152 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002153 removeSleepTimeouts();
2154 if (mGoingToSleep.isHeld()) {
2155 mGoingToSleep.release();
2156 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002157 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002158 final ActivityStack stack = mStacks.get(stackNdx);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002159 stack.awakeFromSleepingLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002160 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002161 mGoingToSleepActivities.clear();
2162 }
2163
2164 void activitySleptLocked(ActivityRecord r) {
2165 mGoingToSleepActivities.remove(r);
2166 checkReadyForSleepLocked();
2167 }
2168
2169 void checkReadyForSleepLocked() {
2170 if (!mService.isSleepingOrShuttingDown()) {
2171 // Do not care.
2172 return;
2173 }
2174
2175 if (!mSleepTimeout) {
2176 boolean dontSleep = false;
2177 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2178 dontSleep |= mStacks.get(stackNdx).checkReadyForSleepLocked();
2179 }
2180
2181 if (mStoppingActivities.size() > 0) {
2182 // Still need to tell some activities to stop; can't sleep yet.
2183 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2184 + mStoppingActivities.size() + " activities");
2185 scheduleIdleLocked();
2186 dontSleep = true;
2187 }
2188
2189 if (mGoingToSleepActivities.size() > 0) {
2190 // Still need to tell some activities to sleep; can't sleep yet.
2191 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2192 + mGoingToSleepActivities.size() + " activities");
2193 dontSleep = true;
2194 }
2195
2196 if (dontSleep) {
2197 return;
2198 }
2199 }
2200
2201 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2202 mStacks.get(stackNdx).goToSleep();
2203 }
2204
2205 removeSleepTimeouts();
2206
2207 if (mGoingToSleep.isHeld()) {
2208 mGoingToSleep.release();
2209 }
2210 if (mService.mShuttingDown) {
2211 mService.notifyAll();
2212 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002213 }
2214
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002215 boolean reportResumedActivityLocked(ActivityRecord r) {
2216 final ActivityStack stack = r.task.stack;
2217 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002218 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002219 }
2220 if (allResumedActivitiesComplete()) {
2221 ensureActivitiesVisibleLocked(null, 0);
2222 mWindowManager.executeAppTransition();
2223 return true;
2224 }
2225 return false;
2226 }
2227
Craig Mautner8d341ef2013-03-26 09:03:27 -07002228 void handleAppCrashLocked(ProcessRecord app) {
2229 final int numStacks = mStacks.size();
2230 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2231 final ActivityStack stack = mStacks.get(stackNdx);
2232 stack.handleAppCrashLocked(app);
2233 }
2234 }
2235
Craig Mautnerde4ef022013-04-07 19:01:33 -07002236 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002237 // First the front stacks. In case any are not fullscreen and are in front of home.
2238 boolean showHomeBehindStack = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002239 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner580ea812013-04-25 12:58:38 -07002240 final ActivityStack stack = mStacks.get(stackNdx);
2241 if (isFrontStack(stack)) {
2242 showHomeBehindStack =
2243 stack.ensureActivitiesVisibleLocked(starting, configChanges);
2244 }
2245 }
2246 // Now do back stacks.
2247 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2248 final ActivityStack stack = mStacks.get(stackNdx);
2249 if (!isFrontStack(stack)) {
2250 stack.ensureActivitiesVisibleLocked(starting, configChanges, showHomeBehindStack);
2251 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002252 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002253 }
2254
2255 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
2256 final int numStacks = mStacks.size();
2257 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2258 final ActivityStack stack = mStacks.get(stackNdx);
2259 stack.scheduleDestroyActivities(app, false, reason);
2260 }
2261 }
2262
2263 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002264 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002265
2266 final String homePackageName = mService.getHomePackageName();
2267 if (homePackageName != null) {
2268 setHomePackageName(mCurrentUser, homePackageName);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002269 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002270
2271 mStartingUsers.add(uss);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002272 boolean haveActivities = false;
2273 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2274 haveActivities |= mStacks.get(stackNdx).switchUserLocked(userId);
2275 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002276
2277 resumeTopActivitiesLocked();
2278
Craig Mautner8d341ef2013-03-26 09:03:27 -07002279 return haveActivities;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002280 }
2281
Craig Mautnerde4ef022013-04-07 19:01:33 -07002282 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
2283 int N = mStoppingActivities.size();
2284 if (N <= 0) return null;
2285
2286 ArrayList<ActivityRecord> stops = null;
2287
2288 final boolean nowVisible = allResumedActivitiesVisible();
2289 for (int i=0; i<N; i++) {
2290 ActivityRecord s = mStoppingActivities.get(i);
2291 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
2292 + nowVisible + " waitingVisible=" + s.waitingVisible
2293 + " finishing=" + s.finishing);
2294 if (s.waitingVisible && nowVisible) {
2295 mWaitingVisibleActivities.remove(s);
2296 s.waitingVisible = false;
2297 if (s.finishing) {
2298 // If this activity is finishing, it is sitting on top of
2299 // everyone else but we now know it is no longer needed...
2300 // so get rid of it. Otherwise, we need to go through the
2301 // normal flow and hide it once we determine that it is
2302 // hidden by the activities in front of it.
2303 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002304 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002305 }
2306 }
2307 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
2308 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
2309 if (stops == null) {
2310 stops = new ArrayList<ActivityRecord>();
2311 }
2312 stops.add(s);
2313 mStoppingActivities.remove(i);
2314 N--;
2315 i--;
2316 }
2317 }
2318
2319 return stops;
2320 }
2321
Craig Mautnercf910b02013-04-23 11:23:27 -07002322 void validateTopActivitiesLocked() {
2323 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2324 final ActivityStack stack = mStacks.get(stackNdx);
2325 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002326 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07002327 if (isFrontStack(stack)) {
2328 if (r == null) {
2329 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
2330 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002331 final ActivityRecord pausing = stack.mPausingActivity;
2332 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002333 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002334 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002335 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002336 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002337 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002338 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002339 }
2340 }
2341 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002342 final ActivityRecord resumed = stack.mResumedActivity;
2343 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002344 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002345 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002346 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002347 if (r != null && (state == ActivityState.INITIALIZING
2348 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002349 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002350 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002351 }
2352 }
2353 }
2354 }
2355
Craig Mautner76ea2242013-05-15 11:40:05 -07002356 private static String stackStateToString(int stackState) {
2357 switch (stackState) {
2358 case STACK_STATE_HOME_IN_FRONT: return "STACK_STATE_HOME_IN_FRONT";
2359 case STACK_STATE_HOME_TO_BACK: return "STACK_STATE_HOME_TO_BACK";
2360 case STACK_STATE_HOME_IN_BACK: return "STACK_STATE_HOME_IN_BACK";
2361 case STACK_STATE_HOME_TO_FRONT: return "STACK_STATE_HOME_TO_FRONT";
2362 default: return "Unknown stackState=" + stackState;
2363 }
2364 }
2365
Craig Mautner27084302013-03-25 08:05:25 -07002366 public void dump(PrintWriter pw, String prefix) {
2367 pw.print(prefix); pw.print("mDismissKeyguardOnNextActivity:");
2368 pw.println(mDismissKeyguardOnNextActivity);
Craig Mautner76ea2242013-05-15 11:40:05 -07002369 pw.print(prefix); pw.print("mStackState="); pw.println(stackStateToString(mStackState));
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002370 pw.print(prefix); pw.println("mSleepTimeout: " + mSleepTimeout);
2371 pw.print(prefix); pw.println("mCurTaskId: " + mCurTaskId);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002372 pw.print(prefix); pw.print("mHomePackageNames:");
2373 for (int i = 0; i < mHomePackageNames.size(); ++i) {
2374 pw.print(" ("); pw.print(mHomePackageNames.keyAt(i)); pw.print(",");
2375 pw.print(mHomePackageNames.valueAt(i)); pw.print(")");
2376 }
2377 pw.println();
Craig Mautner27084302013-03-25 08:05:25 -07002378 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002379
Craig Mautner20e72272013-04-01 13:45:53 -07002380 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002381 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002382 }
2383
Dianne Hackborn390517b2013-05-30 15:03:32 -07002384 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2385 boolean needSep, String prefix) {
2386 if (activity != null) {
2387 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2388 if (needSep) {
2389 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002390 }
2391 pw.print(prefix);
2392 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002393 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002394 }
2395 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002396 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002397 }
2398
Craig Mautner8d341ef2013-03-26 09:03:27 -07002399 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2400 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002401 boolean printed = false;
2402 boolean needSep = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002403 final int numStacks = mStacks.size();
2404 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2405 final ActivityStack stack = mStacks.get(stackNdx);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002406 StringBuilder stackHeader = new StringBuilder(128);
2407 stackHeader.append(" Stack #");
2408 stackHeader.append(mStacks.indexOf(stack));
2409 stackHeader.append(":");
2410 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage, needSep,
2411 stackHeader.toString());
2412 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false, !dumpAll,
2413 false, dumpPackage, true, " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002414
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002415 needSep = printed;
2416 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
Dianne Hackborn390517b2013-05-30 15:03:32 -07002417 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002418 if (pr) {
2419 printed = true;
2420 needSep = false;
2421 }
2422 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
Dianne Hackborn390517b2013-05-30 15:03:32 -07002423 " mResumedActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002424 if (pr) {
2425 printed = true;
2426 needSep = false;
2427 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002428 if (dumpAll) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002429 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
Dianne Hackborn390517b2013-05-30 15:03:32 -07002430 " mLastPausedActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002431 if (pr) {
2432 printed = true;
Craig Mautner0f922742013-08-06 08:44:42 -07002433 needSep = true;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002434 }
Craig Mautner0f922742013-08-06 08:44:42 -07002435 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2436 needSep, " mLastNoHistoryActivity: ");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002437 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002438 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002439 }
2440
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002441 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2442 false, dumpPackage, true, " Activities waiting to finish:", null);
2443 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2444 false, dumpPackage, true, " Activities waiting to stop:", null);
2445 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2446 false, dumpPackage, true, " Activities waiting for another to become visible:",
2447 null);
2448 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2449 false, dumpPackage, true, " Activities waiting to sleep:", null);
2450 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2451 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002452
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002453 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002454 }
2455
Dianne Hackborn390517b2013-05-30 15:03:32 -07002456 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002457 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002458 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002459 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002460 String innerPrefix = null;
2461 String[] args = null;
2462 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002463 for (int i=list.size()-1; i>=0; i--) {
2464 final ActivityRecord r = list.get(i);
2465 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2466 continue;
2467 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002468 if (innerPrefix == null) {
2469 innerPrefix = prefix + " ";
2470 args = new String[0];
2471 }
2472 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002473 final boolean full = !brief && (complete || !r.isInHistory());
2474 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002475 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002476 needNL = false;
2477 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002478 if (header1 != null) {
2479 pw.println(header1);
2480 header1 = null;
2481 }
2482 if (header2 != null) {
2483 pw.println(header2);
2484 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002485 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002486 if (lastTask != r.task) {
2487 lastTask = r.task;
2488 pw.print(prefix);
2489 pw.print(full ? "* " : " ");
2490 pw.println(lastTask);
2491 if (full) {
2492 lastTask.dump(pw, prefix + " ");
2493 } else if (complete) {
2494 // Complete + brief == give a summary. Isn't that obvious?!?
2495 if (lastTask.intent != null) {
2496 pw.print(prefix); pw.print(" ");
2497 pw.println(lastTask.intent.toInsecureStringWithClip());
2498 }
2499 }
2500 }
2501 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2502 pw.print(" #"); pw.print(i); pw.print(": ");
2503 pw.println(r);
2504 if (full) {
2505 r.dump(pw, innerPrefix);
2506 } else if (complete) {
2507 // Complete + brief == give a summary. Isn't that obvious?!?
2508 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2509 if (r.app != null) {
2510 pw.print(innerPrefix); pw.println(r.app);
2511 }
2512 }
2513 if (client && r.app != null && r.app.thread != null) {
2514 // flush anything that is already in the PrintWriter since the thread is going
2515 // to write to the file descriptor directly
2516 pw.flush();
2517 try {
2518 TransferPipe tp = new TransferPipe();
2519 try {
2520 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
2521 r.appToken, innerPrefix, args);
2522 // Short timeout, since blocking here can
2523 // deadlock with the application.
2524 tp.go(fd, 2000);
2525 } finally {
2526 tp.kill();
2527 }
2528 } catch (IOException e) {
2529 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2530 } catch (RemoteException e) {
2531 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2532 }
2533 needNL = true;
2534 }
2535 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002536 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002537 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002538
Craig Mautnerf3333272013-04-22 10:55:53 -07002539 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002540 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002541 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2542 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002543 }
2544
2545 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002546 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002547 }
2548
2549 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002550 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002551 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2552 }
2553
Craig Mautner05d29032013-05-03 13:40:13 -07002554 final void scheduleResumeTopActivities() {
2555 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2556 }
2557
Craig Mautner0eea92c2013-05-16 13:35:39 -07002558 void removeSleepTimeouts() {
2559 mSleepTimeout = false;
2560 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
2561 }
2562
2563 final void scheduleSleepTimeout() {
2564 removeSleepTimeouts();
2565 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
2566 }
2567
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002568 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07002569
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002570 public ActivityStackSupervisorHandler(Looper looper) {
2571 super(looper);
2572 }
2573
Craig Mautnerf3333272013-04-22 10:55:53 -07002574 void activityIdleInternal(ActivityRecord r) {
2575 synchronized (mService) {
2576 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
2577 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002578 }
2579
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002580 @Override
2581 public void handleMessage(Message msg) {
2582 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002583 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07002584 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07002585 if (mService.mDidDexOpt) {
2586 mService.mDidDexOpt = false;
2587 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
2588 nmsg.obj = msg.obj;
2589 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
2590 return;
2591 }
2592 // We don't at this point know if the activity is fullscreen,
2593 // so we need to be conservative and assume it isn't.
2594 activityIdleInternal((ActivityRecord)msg.obj);
2595 } break;
2596 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07002597 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07002598 activityIdleInternal((ActivityRecord)msg.obj);
2599 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07002600 case RESUME_TOP_ACTIVITY_MSG: {
2601 synchronized (mService) {
2602 resumeTopActivitiesLocked();
2603 }
2604 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002605 case SLEEP_TIMEOUT_MSG: {
2606 synchronized (mService) {
2607 if (mService.isSleepingOrShuttingDown()) {
2608 Slog.w(TAG, "Sleep timeout! Sleeping now.");
2609 mSleepTimeout = true;
2610 checkReadyForSleepLocked();
2611 }
2612 }
2613 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002614 case LAUNCH_TIMEOUT_MSG: {
2615 if (mService.mDidDexOpt) {
2616 mService.mDidDexOpt = false;
2617 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2618 return;
2619 }
2620 synchronized (mService) {
2621 if (mLaunchingActivity.isHeld()) {
2622 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
2623 if (VALIDATE_WAKE_LOCK_CALLER
2624 && Binder.getCallingUid() != Process.myUid()) {
2625 throw new IllegalStateException("Calling must be system uid");
2626 }
2627 mLaunchingActivity.release();
2628 }
2629 }
2630 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002631 }
2632 }
2633 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002634
Craig Mautnerac6f8432013-07-17 13:24:59 -07002635 String getHomePackageName() {
2636 return mHomePackageNames.get(mCurrentUser);
2637 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002638
Craig Mautnerac6f8432013-07-17 13:24:59 -07002639 void setHomePackageName(int userId, String homePackageName) {
2640 if (DEBUG_SWITCH) Slog.d(TAG, "setHomePackageName: user=" + userId + " package="
2641 + homePackageName);
2642 mHomePackageNames.put(userId, homePackageName);
Craig Mautner858d8a62013-04-23 17:08:34 -07002643 }
Craig Mautner27084302013-03-25 08:05:25 -07002644}