2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server.am;
19 import static android.Manifest.permission.START_ANY_ACTIVITY;
20 import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
21 import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
22 import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
23 import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
24 import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
25 import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
26 import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
27 import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
28 import static android.content.pm.PackageManager.PERMISSION_GRANTED;
29 import static com.android.server.am.ActivityManagerDebugConfig.*;
30 import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
31 import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
32 import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
33 import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
34 import static com.android.server.am.ActivityStack.ActivityState.*;
35 import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
36 import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
37 import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
38 import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
39 import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
41 import android.Manifest;
42 import android.app.Activity;
43 import android.app.ActivityManager;
44 import android.app.ActivityManager.StackInfo;
45 import android.app.ActivityOptions;
46 import android.app.AppGlobals;
47 import android.app.AppOpsManager;
48 import android.app.IActivityContainer;
49 import android.app.IActivityContainerCallback;
50 import android.app.IActivityManager;
51 import android.app.IApplicationThread;
52 import android.app.PendingIntent;
53 import android.app.ProfilerInfo;
54 import android.app.ActivityManager.RunningTaskInfo;
55 import android.app.IActivityManager.WaitResult;
56 import android.app.ResultInfo;
57 import android.app.StatusBarManager;
58 import android.app.admin.IDevicePolicyManager;
59 import android.content.ComponentName;
60 import android.content.Context;
61 import android.content.IIntentSender;
62 import android.content.Intent;
63 import android.content.IntentSender;
64 import android.content.pm.ActivityInfo;
65 import android.content.pm.ApplicationInfo;
66 import android.content.pm.PackageInfo;
67 import android.content.pm.PackageManager;
68 import android.content.pm.ResolveInfo;
69 import android.content.res.Configuration;
70 import android.graphics.Point;
71 import android.graphics.Rect;
72 import android.hardware.display.DisplayManager;
73 import android.hardware.display.DisplayManager.DisplayListener;
74 import android.hardware.display.DisplayManagerGlobal;
75 import android.hardware.display.VirtualDisplay;
76 import android.hardware.input.InputManager;
77 import android.hardware.input.InputManagerInternal;
78 import android.net.Uri;
79 import android.os.Binder;
80 import android.os.Bundle;
81 import android.os.Debug;
82 import android.os.Handler;
83 import android.os.IBinder;
84 import android.os.Looper;
85 import android.os.Message;
86 import android.os.ParcelFileDescriptor;
87 import android.os.PowerManager;
88 import android.os.Process;
89 import android.os.RemoteException;
90 import android.os.ServiceManager;
91 import android.os.SystemClock;
92 import android.os.TransactionTooLargeException;
93 import android.os.UserHandle;
94 import android.os.WorkSource;
95 import android.provider.MediaStore;
96 import android.provider.Settings;
97 import android.provider.Settings.SettingNotFoundException;
98 import android.service.voice.IVoiceInteractionSession;
99 import android.util.ArrayMap;
100 import android.util.ArraySet;
101 import android.util.EventLog;
102 import android.util.Slog;
103 import android.util.SparseArray;
105 import android.util.SparseIntArray;
106 import android.view.Display;
107 import android.view.DisplayInfo;
108 import android.view.InputEvent;
109 import android.view.Surface;
110 import com.android.internal.app.HeavyWeightSwitcherActivity;
111 import com.android.internal.app.IVoiceInteractor;
112 import com.android.internal.content.ReferrerIntent;
113 import com.android.internal.os.TransferPipe;
114 import com.android.internal.statusbar.IStatusBarService;
115 import com.android.internal.util.ArrayUtils;
116 import com.android.internal.widget.LockPatternUtils;
117 import com.android.server.LocalServices;
118 import com.android.server.am.ActivityStack.ActivityState;
119 import com.android.server.wm.WindowManagerService;
122 import java.io.FileDescriptor;
123 import java.io.IOException;
124 import java.io.PrintWriter;
125 import java.util.ArrayList;
126 import java.util.Arrays;
127 import java.util.List;
128 import java.util.Set;
130 public final class ActivityStackSupervisor implements DisplayListener {
131 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
132 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
133 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
134 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
135 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
136 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
137 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
138 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
139 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
140 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
141 private static final String TAG_STACK = TAG + POSTFIX_STACK;
142 private static final String TAG_STATES = TAG + POSTFIX_STATES;
143 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
144 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
145 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
146 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
148 public static final int HOME_STACK_ID = 0;
150 /** How long we wait until giving up on the last activity telling us it is idle. */
151 static final int IDLE_TIMEOUT = 10 * 1000;
153 /** How long we can hold the sleep wake lock before giving up. */
154 static final int SLEEP_TIMEOUT = 5 * 1000;
156 // How long we can hold the launch wake lock before giving up.
157 static final int LAUNCH_TIMEOUT = 10 * 1000;
159 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
160 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
161 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
162 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
163 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
164 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
165 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
166 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
167 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
168 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
169 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
170 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
171 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
172 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
174 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
176 private static final String LOCK_TASK_TAG = "Lock-to-App";
178 // Activity actions an app cannot start if it uses a permission which is not granted.
179 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
182 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
183 Manifest.permission.CAMERA);
184 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
185 Manifest.permission.CAMERA);
186 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
187 Manifest.permission.CALL_PHONE);
190 /** Action restriction: launching the activity is not restricted. */
191 private static final int ACTIVITY_RESTRICTION_NONE = 0;
192 /** Action restriction: launching the activity is restricted by a permission. */
193 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
194 /** Action restriction: launching the activity is restricted by an app op. */
195 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
197 /** Status Bar Service **/
198 private IBinder mToken = new Binder();
199 private IStatusBarService mStatusBarService;
200 private IDevicePolicyManager mDevicePolicyManager;
202 // For debugging to make sure the caller when acquiring/releasing our
203 // wake lock is the system process.
204 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
206 final ActivityManagerService mService;
208 private final RecentTasks mRecentTasks;
210 final ActivityStackSupervisorHandler mHandler;
213 WindowManagerService mWindowManager;
214 DisplayManager mDisplayManager;
216 /** Identifier counter for all ActivityStacks */
217 private int mLastStackId = HOME_STACK_ID;
219 /** Task identifier that activities are currently being started in. Incremented each time a
220 * new task is created. */
221 private int mCurTaskId = 0;
223 /** The current user */
224 private int mCurrentUser;
226 /** The stack containing the launcher app. Assumed to always be attached to
227 * Display.DEFAULT_DISPLAY. */
228 private ActivityStack mHomeStack;
230 /** The stack currently receiving input or launching the next activity. */
231 private ActivityStack mFocusedStack;
233 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
234 * been resumed. If stacks are changing position this will hold the old stack until the new
235 * stack becomes resumed after which it will be set to mFocusedStack. */
236 private ActivityStack mLastFocusedStack;
238 /** List of activities that are waiting for a new activity to become visible before completing
239 * whatever operation they are supposed to do. */
240 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
242 /** List of processes waiting to find out about the next visible activity. */
243 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
245 /** List of processes waiting to find out about the next launched activity. */
246 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
248 /** List of activities that are ready to be stopped, but waiting for the next activity to
249 * settle down before doing so. */
250 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
252 /** List of activities that are ready to be finished, but waiting for the previous activity to
253 * settle down before doing so. It contains ActivityRecord objects. */
254 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
256 /** List of activities that are in the process of going to sleep. */
257 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
259 /** Used on user changes */
260 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
262 /** Used to queue up any background users being started */
263 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
265 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
266 * is being brought in front of us. */
267 boolean mUserLeaving = false;
269 /** Set when we have taken too long waiting to go to sleep. */
270 boolean mSleepTimeout = false;
272 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
273 * setWindowManager is called. **/
274 private boolean mLeanbackOnlyDevice;
277 * We don't want to allow the device to go to sleep while in the process
278 * of launching an activity. This is primarily to allow alarm intent
279 * receivers to launch an activity and get that to run before the device
280 * goes back to sleep.
282 PowerManager.WakeLock mLaunchingActivity;
285 * Set when the system is going to sleep, until we have
286 * successfully paused the current activity and released our wake lock.
287 * At that point the system is allowed to actually sleep.
289 PowerManager.WakeLock mGoingToSleep;
291 /** Stack id of the front stack when user switched, indexed by userId. */
292 SparseIntArray mUserStackInFront = new SparseIntArray(2);
294 // TODO: Add listener for removal of references.
295 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
296 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
298 /** Mapping from displayId to display current state */
299 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
301 InputManagerInternal mInputManagerInternal;
303 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
304 * may be finished until there is only one entry left. If this is empty the system is not
305 * in lockTask mode. */
306 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
307 /** Store the current lock task mode. Possible values:
308 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
309 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
311 private int mLockTaskModeState;
313 * Notifies the user when entering/exiting lock-task.
315 private LockTaskNotify mLockTaskNotify;
317 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
319 /** Used to keep resumeTopActivityLocked() from being entered recursively */
320 boolean inResumeTopActivity;
323 * Description of a request to start a new activity, which has been held
324 * due to app switches being disabled.
326 static class PendingActivityLaunch {
327 final ActivityRecord r;
328 final ActivityRecord sourceRecord;
329 final int startFlags;
330 final ActivityStack stack;
332 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
333 int _startFlags, ActivityStack _stack) {
335 sourceRecord = _sourceRecord;
336 startFlags = _startFlags;
341 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
343 mRecentTasks = recentTasks;
344 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
348 * At the time when the constructor runs, the power manager has not yet been
349 * initialized. So we initialize our wakelocks afterwards.
351 void initPowerManagement() {
352 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
353 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
354 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
355 mLaunchingActivity.setReferenceCounted(false);
358 // This function returns a IStatusBarService. The value is from ServiceManager.
359 // getService and is cached.
360 private IStatusBarService getStatusBarService() {
361 synchronized (mService) {
362 if (mStatusBarService == null) {
363 mStatusBarService = IStatusBarService.Stub.asInterface(
364 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
365 if (mStatusBarService == null) {
366 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
369 return mStatusBarService;
373 private IDevicePolicyManager getDevicePolicyManager() {
374 synchronized (mService) {
375 if (mDevicePolicyManager == null) {
376 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
377 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
378 if (mDevicePolicyManager == null) {
379 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
382 return mDevicePolicyManager;
386 void setWindowManager(WindowManagerService wm) {
387 synchronized (mService) {
391 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
392 mDisplayManager.registerDisplayListener(this, null);
394 Display[] displays = mDisplayManager.getDisplays();
395 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
396 final int displayId = displays[displayNdx].getDisplayId();
397 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
398 if (activityDisplay.mDisplay == null) {
399 throw new IllegalStateException("Default Display does not exist");
401 mActivityDisplays.put(displayId, activityDisplay);
404 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
405 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
407 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
409 // Initialize this here, now that we can get a valid reference to PackageManager.
410 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
414 void notifyActivityDrawnForKeyguard() {
415 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
416 mWindowManager.notifyActivityDrawnForKeyguard();
419 ActivityStack getFocusedStack() {
420 return mFocusedStack;
423 ActivityStack getLastStack() {
424 return mLastFocusedStack;
427 /** Top of all visible stacks is/should always be equal to the focused stack.
428 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
429 * stack is visible or not. */
430 boolean isFrontStack(ActivityStack stack) {
435 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
436 if (parent != null) {
437 stack = parent.task.stack;
439 return stack == mFocusedStack;
442 void moveHomeStack(boolean toFront, String reason) {
443 moveHomeStack(toFront, reason, null);
446 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
447 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
448 final int topNdx = stacks.size() - 1;
453 // The home stack should either be at the top or bottom of the stack list.
454 if ((toFront && (stacks.get(topNdx) != mHomeStack))
455 || (!toFront && (stacks.get(0) != mHomeStack))) {
456 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
457 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
458 + " new=" + mFocusedStack);
459 stacks.remove(mHomeStack);
460 stacks.add(toFront ? topNdx : 0, mHomeStack);
463 if (lastFocusedStack != null) {
464 mLastFocusedStack = lastFocusedStack;
466 mFocusedStack = stacks.get(topNdx);
468 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
469 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
470 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
472 if (mService.mBooting || !mService.mBooted) {
473 final ActivityRecord r = topRunningActivityLocked();
474 if (r != null && r.idle) {
475 checkFinishBootingLocked();
480 /** Returns true if the focus activity was adjusted to the home stack top activity. */
481 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
482 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
483 mWindowManager.showRecentApps();
487 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
489 final ActivityRecord top = getHomeActivity();
493 mService.setFocusedActivityLocked(top, reason);
497 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
498 if (!mService.mBooting && !mService.mBooted) {
503 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
504 mWindowManager.showRecentApps();
509 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
512 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
513 ActivityRecord r = getHomeActivity();
515 mService.setFocusedActivityLocked(r, reason);
516 return resumeTopActivitiesLocked(mHomeStack, prev, null);
518 return mService.startHomeActivityLocked(mCurrentUser, reason);
521 TaskRecord anyTaskForIdLocked(int id) {
522 return anyTaskForIdLocked(id, true);
526 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
527 * @param id Id of the task we would like returned.
528 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
529 * restore the task from recents to the active list.
531 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
532 int numDisplays = mActivityDisplays.size();
533 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
534 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
535 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
536 ActivityStack stack = stacks.get(stackNdx);
537 TaskRecord task = stack.taskForIdLocked(id);
544 // Don't give up! Look in recents.
545 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
546 TaskRecord task = mRecentTasks.taskForIdLocked(id);
548 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
552 if (!restoreFromRecents) {
556 if (!restoreRecentTaskLocked(task)) {
557 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
558 "Couldn't restore task id=" + id + " found in recents");
561 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
565 ActivityRecord isInAnyStackLocked(IBinder token) {
566 int numDisplays = mActivityDisplays.size();
567 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
568 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
569 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
570 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
579 void setNextTaskId(int taskId) {
580 if (taskId > mCurTaskId) {
585 int getNextTaskId() {
588 if (mCurTaskId <= 0) {
591 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
595 ActivityRecord resumedAppLocked() {
596 ActivityStack stack = mFocusedStack;
600 ActivityRecord resumedActivity = stack.mResumedActivity;
601 if (resumedActivity == null || resumedActivity.app == null) {
602 resumedActivity = stack.mPausingActivity;
603 if (resumedActivity == null || resumedActivity.app == null) {
604 resumedActivity = stack.topRunningActivityLocked(null);
607 return resumedActivity;
610 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
611 final String processName = app.processName;
612 boolean didSomething = false;
613 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
614 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
615 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
616 final ActivityStack stack = stacks.get(stackNdx);
617 if (!isFrontStack(stack)) {
620 ActivityRecord hr = stack.topRunningActivityLocked(null);
622 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
623 && processName.equals(hr.processName)) {
625 if (realStartActivityLocked(hr, app, true, true)) {
628 } catch (RemoteException e) {
629 Slog.w(TAG, "Exception in new application when starting activity "
630 + hr.intent.getComponent().flattenToShortString(), e);
638 ensureActivitiesVisibleLocked(null, 0);
643 boolean allResumedActivitiesIdle() {
644 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
645 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
646 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
647 final ActivityStack stack = stacks.get(stackNdx);
648 if (!isFrontStack(stack) || stack.numActivities() == 0) {
651 final ActivityRecord resumedActivity = stack.mResumedActivity;
652 if (resumedActivity == null || !resumedActivity.idle) {
653 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
654 + stack.mStackId + " " + resumedActivity + " not idle");
662 boolean allResumedActivitiesComplete() {
663 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
664 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
665 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
666 final ActivityStack stack = stacks.get(stackNdx);
667 if (isFrontStack(stack)) {
668 final ActivityRecord r = stack.mResumedActivity;
669 if (r != null && r.state != RESUMED) {
675 // TODO: Not sure if this should check if all Paused are complete too.
676 if (DEBUG_STACK) Slog.d(TAG_STACK,
677 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
678 mLastFocusedStack + " to=" + mFocusedStack);
679 mLastFocusedStack = mFocusedStack;
683 boolean allResumedActivitiesVisible() {
684 boolean foundResumed = false;
685 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
686 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
687 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
688 final ActivityStack stack = stacks.get(stackNdx);
689 final ActivityRecord r = stack.mResumedActivity;
691 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
702 * Pause all activities in either all of the stacks or just the back stacks.
703 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
704 * @return true if any activity was paused as a result of this call.
706 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
707 boolean someActivityPaused = false;
708 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
709 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
710 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
711 final ActivityStack stack = stacks.get(stackNdx);
712 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
713 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
714 " mResumedActivity=" + stack.mResumedActivity);
715 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
720 return someActivityPaused;
723 boolean allPausedActivitiesComplete() {
724 boolean pausing = true;
725 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
726 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
727 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
728 final ActivityStack stack = stacks.get(stackNdx);
729 final ActivityRecord r = stack.mPausingActivity;
730 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
733 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
744 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
745 boolean resuming, boolean dontWait) {
746 // TODO: Put all stacks in supervisor and iterate through them instead.
747 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
748 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
749 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
750 final ActivityStack stack = stacks.get(stackNdx);
751 if (stack.mResumedActivity != null &&
752 stack.mActivityContainer.mParentActivity == parent) {
753 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
759 void reportActivityVisibleLocked(ActivityRecord r) {
760 sendWaitingVisibleReportLocked(r);
763 void sendWaitingVisibleReportLocked(ActivityRecord r) {
764 boolean changed = false;
765 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
766 WaitResult w = mWaitingActivityVisible.get(i);
771 w.who = new ComponentName(r.info.packageName, r.info.name);
773 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
774 w.thisTime = w.totalTime;
778 mService.notifyAll();
782 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
783 long thisTime, long totalTime) {
784 boolean changed = false;
785 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
786 WaitResult w = mWaitingActivityLaunched.remove(i);
791 w.who = new ComponentName(r.info.packageName, r.info.name);
793 w.thisTime = thisTime;
794 w.totalTime = totalTime;
798 mService.notifyAll();
802 ActivityRecord topRunningActivityLocked() {
803 final ActivityStack focusedStack = mFocusedStack;
804 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
809 // Return to the home stack.
810 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
811 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
812 final ActivityStack stack = stacks.get(stackNdx);
813 if (stack != focusedStack && isFrontStack(stack)) {
814 r = stack.topRunningActivityLocked(null);
823 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
824 // Gather all of the running tasks for each stack into runningTaskLists.
825 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
826 new ArrayList<ArrayList<RunningTaskInfo>>();
827 final int numDisplays = mActivityDisplays.size();
828 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
829 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
830 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
831 final ActivityStack stack = stacks.get(stackNdx);
832 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
833 runningTaskLists.add(stackTaskList);
834 stack.getTasksLocked(stackTaskList, callingUid, allowed);
838 // The lists are already sorted from most recent to oldest. Just pull the most recent off
839 // each list and add it to list. Stop when all lists are empty or maxNum reached.
841 long mostRecentActiveTime = Long.MIN_VALUE;
842 ArrayList<RunningTaskInfo> selectedStackList = null;
843 final int numTaskLists = runningTaskLists.size();
844 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
845 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
846 if (!stackTaskList.isEmpty()) {
847 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
848 if (lastActiveTime > mostRecentActiveTime) {
849 mostRecentActiveTime = lastActiveTime;
850 selectedStackList = stackTaskList;
854 if (selectedStackList != null) {
855 list.add(selectedStackList.remove(0));
863 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
864 ProfilerInfo profilerInfo, int userId) {
865 // Collect information about the target of the Intent.
869 AppGlobals.getPackageManager().resolveIntent(
870 intent, resolvedType,
871 PackageManager.MATCH_DEFAULT_ONLY
872 | ActivityManagerService.STOCK_PM_FLAGS, userId);
873 aInfo = rInfo != null ? rInfo.activityInfo : null;
874 } catch (RemoteException e) {
879 // Store the found target back into the intent, because now that
880 // we have it we never want to do this again. For example, if the
881 // user navigates back to this point in the history, we should
882 // always restart the exact same activity.
883 intent.setComponent(new ComponentName(
884 aInfo.applicationInfo.packageName, aInfo.name));
886 // Don't debug things in the system process
887 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
888 if (!aInfo.processName.equals("system")) {
889 mService.setDebugApp(aInfo.processName, true, false);
893 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
894 if (!aInfo.processName.equals("system")) {
895 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
899 if (profilerInfo != null) {
900 if (!aInfo.processName.equals("system")) {
901 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
908 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
909 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
910 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
911 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
912 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
913 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
914 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
915 false /* componentSpecified */,
916 null /* outActivity */, null /* container */, null /* inTask */);
917 if (inResumeTopActivity) {
918 // If we are in resume section already, home activity will be initialized, but not
919 // resumed (to avoid recursive resume) and will stay that way until something pokes it
920 // again. We need to schedule another resume.
921 scheduleResumeTopActivities();
925 final int startActivityMayWait(IApplicationThread caller, int callingUid,
926 String callingPackage, Intent intent, String resolvedType,
927 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
928 IBinder resultTo, String resultWho, int requestCode, int startFlags,
929 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
930 Bundle options, boolean ignoreTargetSecurity, int userId,
931 IActivityContainer iContainer, TaskRecord inTask) {
932 // Refuse possible leaked file descriptors
933 if (intent != null && intent.hasFileDescriptors()) {
934 throw new IllegalArgumentException("File descriptors passed in Intent");
936 boolean componentSpecified = intent.getComponent() != null;
938 // Don't modify the client's object!
939 intent = new Intent(intent);
941 // Collect information about the target of the Intent.
943 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
945 ActivityContainer container = (ActivityContainer)iContainer;
946 synchronized (mService) {
947 if (container != null && container.mParentActivity != null &&
948 container.mParentActivity.state != RESUMED) {
949 // Cannot start a child activity if the parent is not resumed.
950 return ActivityManager.START_CANCELED;
952 final int realCallingPid = Binder.getCallingPid();
953 final int realCallingUid = Binder.getCallingUid();
955 if (callingUid >= 0) {
957 } else if (caller == null) {
958 callingPid = realCallingPid;
959 callingUid = realCallingUid;
961 callingPid = callingUid = -1;
964 final ActivityStack stack;
965 if (container == null || container.mStack.isOnHomeDisplay()) {
966 stack = mFocusedStack;
968 stack = container.mStack;
970 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
971 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
972 "Starting activity when config will change = " + stack.mConfigWillChange);
974 final long origId = Binder.clearCallingIdentity();
977 (aInfo.applicationInfo.privateFlags
978 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
979 // This may be a heavy-weight process! Check to see if we already
980 // have another, different heavy-weight process running.
981 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
982 if (mService.mHeavyWeightProcess != null &&
983 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
984 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
985 int appCallingUid = callingUid;
986 if (caller != null) {
987 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
988 if (callerApp != null) {
989 appCallingUid = callerApp.info.uid;
991 Slog.w(TAG, "Unable to find app for caller " + caller
992 + " (pid=" + callingPid + ") when starting: "
993 + intent.toString());
994 ActivityOptions.abort(options);
995 return ActivityManager.START_PERMISSION_DENIED;
999 IIntentSender target = mService.getIntentSenderLocked(
1000 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
1001 appCallingUid, userId, null, null, 0, new Intent[] { intent },
1002 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1003 | PendingIntent.FLAG_ONE_SHOT, null);
1005 Intent newIntent = new Intent();
1006 if (requestCode >= 0) {
1007 // Caller is requesting a result.
1008 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1010 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1011 new IntentSender(target));
1012 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1013 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1014 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1016 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1019 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1021 newIntent.setFlags(intent.getFlags());
1022 newIntent.setClassName("android",
1023 HeavyWeightSwitcherActivity.class.getName());
1025 resolvedType = null;
1027 callingUid = Binder.getCallingUid();
1028 callingPid = Binder.getCallingPid();
1029 componentSpecified = true;
1032 AppGlobals.getPackageManager().resolveIntent(
1034 PackageManager.MATCH_DEFAULT_ONLY
1035 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1036 aInfo = rInfo != null ? rInfo.activityInfo : null;
1037 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1038 } catch (RemoteException e) {
1045 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1046 voiceSession, voiceInteractor, resultTo, resultWho,
1047 requestCode, callingPid, callingUid, callingPackage,
1048 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
1049 componentSpecified, null, container, inTask);
1051 Binder.restoreCallingIdentity(origId);
1053 if (stack.mConfigWillChange) {
1054 // If the caller also wants to switch to a new configuration,
1055 // do so now. This allows a clean switch, as we are waiting
1056 // for the current activity to pause (so we will not destroy
1057 // it), and have not yet started the next activity.
1058 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1059 "updateConfiguration()");
1060 stack.mConfigWillChange = false;
1061 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1062 "Updating to new configuration after starting activity.");
1063 mService.updateConfigurationLocked(config, null, false, false);
1066 if (outResult != null) {
1067 outResult.result = res;
1068 if (res == ActivityManager.START_SUCCESS) {
1069 mWaitingActivityLaunched.add(outResult);
1073 } catch (InterruptedException e) {
1075 } while (!outResult.timeout && outResult.who == null);
1076 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
1077 ActivityRecord r = stack.topRunningActivityLocked(null);
1078 if (r.nowVisible && r.state == RESUMED) {
1079 outResult.timeout = false;
1080 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1081 outResult.totalTime = 0;
1082 outResult.thisTime = 0;
1084 outResult.thisTime = SystemClock.uptimeMillis();
1085 mWaitingActivityVisible.add(outResult);
1089 } catch (InterruptedException e) {
1091 } while (!outResult.timeout && outResult.who == null);
1100 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1101 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1102 Bundle options, int userId) {
1103 if (intents == null) {
1104 throw new NullPointerException("intents is null");
1106 if (resolvedTypes == null) {
1107 throw new NullPointerException("resolvedTypes is null");
1109 if (intents.length != resolvedTypes.length) {
1110 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1115 if (callingUid >= 0) {
1117 } else if (caller == null) {
1118 callingPid = Binder.getCallingPid();
1119 callingUid = Binder.getCallingUid();
1121 callingPid = callingUid = -1;
1123 final long origId = Binder.clearCallingIdentity();
1125 synchronized (mService) {
1126 ActivityRecord[] outActivity = new ActivityRecord[1];
1127 for (int i=0; i<intents.length; i++) {
1128 Intent intent = intents[i];
1129 if (intent == null) {
1133 // Refuse possible leaked file descriptors
1134 if (intent != null && intent.hasFileDescriptors()) {
1135 throw new IllegalArgumentException("File descriptors passed in Intent");
1138 boolean componentSpecified = intent.getComponent() != null;
1140 // Don't modify the client's object!
1141 intent = new Intent(intent);
1143 // Collect information about the target of the Intent.
1144 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
1145 // TODO: New, check if this is correct
1146 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1148 if (aInfo != null &&
1149 (aInfo.applicationInfo.privateFlags
1150 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1151 throw new IllegalArgumentException(
1152 "FLAG_CANT_SAVE_STATE not supported here");
1155 Bundle theseOptions;
1156 if (options != null && i == intents.length-1) {
1157 theseOptions = options;
1159 theseOptions = null;
1161 int res = startActivityLocked(caller, intent, resolvedTypes[i],
1162 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1163 callingPackage, callingPid, callingUid,
1164 0, theseOptions, false, componentSpecified, outActivity, null, null);
1169 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1173 Binder.restoreCallingIdentity(origId);
1176 return ActivityManager.START_SUCCESS;
1179 final boolean realStartActivityLocked(ActivityRecord r,
1180 ProcessRecord app, boolean andResume, boolean checkConfig)
1181 throws RemoteException {
1184 r.startFreezingScreenLocked(app, 0);
1185 mWindowManager.setAppVisibility(r.appToken, true);
1187 // schedule launch ticks to collect information about slow apps.
1188 r.startLaunchTickingLocked();
1191 // Have the window manager re-evaluate the orientation of
1192 // the screen based on the new activity order. Note that
1193 // as a result of this, it can call back into the activity
1194 // manager with a new orientation. We don't care about that,
1195 // because the activity is not currently running so we are
1196 // just restarting it anyway.
1198 Configuration config = mWindowManager.updateOrientationFromAppTokens(
1199 mService.mConfiguration,
1200 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1201 mService.updateConfigurationLocked(config, r, false, false);
1205 app.waitingToKill = null;
1207 r.lastLaunchTime = SystemClock.uptimeMillis();
1209 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1211 int idx = app.activities.indexOf(r);
1213 app.activities.add(r);
1215 mService.updateLruProcessLocked(app, true, null);
1216 mService.updateOomAdjLocked();
1218 final TaskRecord task = r.task;
1219 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1220 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
1221 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
1224 final ActivityStack stack = task.stack;
1226 if (app.thread == null) {
1227 throw new RemoteException();
1229 List<ResultInfo> results = null;
1230 List<ReferrerIntent> newIntents = null;
1232 results = r.results;
1233 newIntents = r.newIntents;
1235 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1236 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1237 + " newIntents=" + newIntents + " andResume=" + andResume);
1239 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1240 r.userId, System.identityHashCode(r),
1241 task.taskId, r.shortComponentName);
1243 if (r.isHomeActivity() && r.isNotResolverActivity()) {
1244 // Home process is the root process of the task.
1245 mService.mHomeProcess = task.mActivities.get(0).app;
1247 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1249 r.forceNewConfig = false;
1250 mService.showAskCompatModeDialogLocked(r);
1251 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1252 ProfilerInfo profilerInfo = null;
1253 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1254 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1255 mService.mProfileProc = app;
1256 final String profileFile = mService.mProfileFile;
1257 if (profileFile != null) {
1258 ParcelFileDescriptor profileFd = mService.mProfileFd;
1259 if (profileFd != null) {
1261 profileFd = profileFd.dup();
1262 } catch (IOException e) {
1263 if (profileFd != null) {
1266 } catch (IOException o) {
1273 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1274 mService.mSamplingInterval, mService.mAutoStopProfiler);
1280 app.hasShownUi = true;
1281 app.pendingUiClean = true;
1283 app.forceProcessStateUpTo(mService.mTopProcessState);
1284 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1285 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
1286 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
1287 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
1288 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
1290 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1291 // This may be a heavy-weight process! Note that the package
1292 // manager will ensure that only activity can run in the main
1293 // process of the .apk, which is the only thing that will be
1294 // considered heavy-weight.
1295 if (app.processName.equals(app.info.packageName)) {
1296 if (mService.mHeavyWeightProcess != null
1297 && mService.mHeavyWeightProcess != app) {
1298 Slog.w(TAG, "Starting new heavy weight process " + app
1299 + " when already running "
1300 + mService.mHeavyWeightProcess);
1302 mService.mHeavyWeightProcess = app;
1303 Message msg = mService.mHandler.obtainMessage(
1304 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1306 mService.mHandler.sendMessage(msg);
1310 } catch (RemoteException e) {
1311 if (r.launchFailed) {
1312 // This is the second time we failed -- finish activity
1314 Slog.e(TAG, "Second failure launching "
1315 + r.intent.getComponent().flattenToShortString()
1316 + ", giving up", e);
1317 mService.appDiedLocked(app);
1318 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1319 "2nd-crash", false);
1323 // This is the first time we failed -- restart process and
1325 app.activities.remove(r);
1329 r.launchFailed = false;
1330 if (stack.updateLRUListLocked(r)) {
1331 Slog.w(TAG, "Activity " + r
1332 + " being launched, but already in LRU list");
1336 // As part of the process of launching, ActivityThread also performs
1338 stack.minimalResumeActivityLocked(r);
1340 // This activity is not starting in the resumed state... which
1341 // should look like we asked it to pause+stop (but remain visible),
1342 // and it has done so and reported back the current icicle and
1344 if (DEBUG_STATES) Slog.v(TAG_STATES,
1345 "Moving to STOPPED: " + r + " (starting in stopped state)");
1350 // Launch the new version setup screen if needed. We do this -after-
1351 // launching the initial activity (that is, home), so that it can have
1352 // a chance to initialize itself while in the background, making the
1353 // switch back to it faster and look better.
1354 if (isFrontStack(stack)) {
1355 mService.startSetupActivityLocked();
1358 // Update any services we are bound to that might care about whether
1359 // their client may have activities.
1360 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1365 void startSpecificActivityLocked(ActivityRecord r,
1366 boolean andResume, boolean checkConfig) {
1367 // Is this activity's application already running?
1368 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
1369 r.info.applicationInfo.uid, true);
1371 r.task.stack.setLaunchTime(r);
1373 if (app != null && app.thread != null) {
1375 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1376 || !"android".equals(r.info.packageName)) {
1377 // Don't add this if it is a platform component that is marked
1378 // to run in multiple processes, because this is actually
1379 // part of the framework so doesn't make sense to track as a
1380 // separate apk in the process.
1381 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1382 mService.mProcessStats);
1384 realStartActivityLocked(r, app, andResume, checkConfig);
1386 } catch (RemoteException e) {
1387 Slog.w(TAG, "Exception when starting activity "
1388 + r.intent.getComponent().flattenToShortString(), e);
1391 // If a dead object exception was thrown -- fall through to
1392 // restart the application.
1395 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
1396 "activity", r.intent.getComponent(), false, false, true);
1399 final int startActivityLocked(IApplicationThread caller,
1400 Intent intent, String resolvedType, ActivityInfo aInfo,
1401 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1402 IBinder resultTo, String resultWho, int requestCode,
1403 int callingPid, int callingUid, String callingPackage,
1404 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
1405 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1406 ActivityContainer container, TaskRecord inTask) {
1407 int err = ActivityManager.START_SUCCESS;
1409 ProcessRecord callerApp = null;
1410 if (caller != null) {
1411 callerApp = mService.getRecordForAppLocked(caller);
1412 if (callerApp != null) {
1413 callingPid = callerApp.pid;
1414 callingUid = callerApp.info.uid;
1416 Slog.w(TAG, "Unable to find app for caller " + caller
1417 + " (pid=" + callingPid + ") when starting: "
1418 + intent.toString());
1419 err = ActivityManager.START_PERMISSION_DENIED;
1423 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1425 if (err == ActivityManager.START_SUCCESS) {
1426 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
1427 + "} from uid " + callingUid
1428 + " on display " + (container == null ? (mFocusedStack == null ?
1429 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1430 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1431 container.mActivityDisplay.mDisplayId)));
1434 ActivityRecord sourceRecord = null;
1435 ActivityRecord resultRecord = null;
1436 if (resultTo != null) {
1437 sourceRecord = isInAnyStackLocked(resultTo);
1438 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1439 "Will send result to " + resultTo + " " + sourceRecord);
1440 if (sourceRecord != null) {
1441 if (requestCode >= 0 && !sourceRecord.finishing) {
1442 resultRecord = sourceRecord;
1447 final int launchFlags = intent.getFlags();
1449 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
1450 // Transfer the result target from the source activity to the new
1451 // one being started, including any failures.
1452 if (requestCode >= 0) {
1453 ActivityOptions.abort(options);
1454 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1456 resultRecord = sourceRecord.resultTo;
1457 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1458 resultRecord = null;
1460 resultWho = sourceRecord.resultWho;
1461 requestCode = sourceRecord.requestCode;
1462 sourceRecord.resultTo = null;
1463 if (resultRecord != null) {
1464 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
1466 if (sourceRecord.launchedFromUid == callingUid) {
1467 // The new activity is being launched from the same uid as the previous
1468 // activity in the flow, and asking to forward its result back to the
1469 // previous. In this case the activity is serving as a trampoline between
1470 // the two, so we also want to update its launchedFromPackage to be the
1471 // same as the previous activity. Note that this is safe, since we know
1472 // these two packages come from the same uid; the caller could just as
1473 // well have supplied that same package name itself. This specifially
1474 // deals with the case of an intent picker/chooser being launched in the app
1475 // flow to redirect to an activity picked by the user, where we want the final
1476 // activity to consider it to have been launched by the previous app activity.
1477 callingPackage = sourceRecord.launchedFromPackage;
1481 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1482 // We couldn't find a class that can handle the given Intent.
1483 // That's the end of that!
1484 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1487 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1488 // We couldn't find the specific class specified in the Intent.
1489 // Also the end of the line.
1490 err = ActivityManager.START_CLASS_NOT_FOUND;
1493 if (err == ActivityManager.START_SUCCESS
1494 && !isCurrentProfileLocked(userId)
1495 && (aInfo.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
1496 // Trying to launch a background activity that doesn't show for all users.
1497 err = ActivityManager.START_NOT_CURRENT_USER_ACTIVITY;
1500 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1501 && sourceRecord.task.voiceSession != null) {
1502 // If this activity is being launched as part of a voice session, we need
1503 // to ensure that it is safe to do so. If the upcoming activity will also
1504 // be part of the voice session, we can only launch it if it has explicitly
1505 // said it supports the VOICE category, or it is a part of the calling app.
1506 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
1507 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1509 intent.addCategory(Intent.CATEGORY_VOICE);
1510 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1511 intent.getComponent(), intent, resolvedType)) {
1513 "Activity being started in current voice task does not support voice: "
1515 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1517 } catch (RemoteException e) {
1518 Slog.w(TAG, "Failure checking voice capabilities", e);
1519 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1524 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1525 // If the caller is starting a new voice session, just make sure the target
1526 // is actually allowing it to run this way.
1528 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1529 intent, resolvedType)) {
1531 "Activity being started in new voice task does not support: "
1533 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1535 } catch (RemoteException e) {
1536 Slog.w(TAG, "Failure checking voice capabilities", e);
1537 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1541 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1543 if (err != ActivityManager.START_SUCCESS) {
1544 if (resultRecord != null) {
1545 resultStack.sendActivityResultLocked(-1,
1546 resultRecord, resultWho, requestCode,
1547 Activity.RESULT_CANCELED, null);
1549 ActivityOptions.abort(options);
1553 boolean abort = false;
1555 final int startAnyPerm = mService.checkPermission(
1556 START_ANY_ACTIVITY, callingPid, callingUid);
1558 if (startAnyPerm != PERMISSION_GRANTED) {
1559 final int componentRestriction = getComponentRestrictionForCallingPackage(
1560 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1561 final int actionRestriction = getActionRestrictionForCallingPackage(
1562 intent.getAction(), callingPackage, callingPid, callingUid);
1564 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1565 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1566 if (resultRecord != null) {
1567 resultStack.sendActivityResultLocked(-1,
1568 resultRecord, resultWho, requestCode,
1569 Activity.RESULT_CANCELED, null);
1572 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1573 msg = "Permission Denial: starting " + intent.toString()
1574 + " from " + callerApp + " (pid=" + callingPid
1575 + ", uid=" + callingUid + ")" + " with revoked permission "
1576 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1577 } else if (!aInfo.exported) {
1578 msg = "Permission Denial: starting " + intent.toString()
1579 + " from " + callerApp + " (pid=" + callingPid
1580 + ", uid=" + callingUid + ")"
1581 + " not exported from uid " + aInfo.applicationInfo.uid;
1583 msg = "Permission Denial: starting " + intent.toString()
1584 + " from " + callerApp + " (pid=" + callingPid
1585 + ", uid=" + callingUid + ")"
1586 + " requires " + aInfo.permission;
1589 throw new SecurityException(msg);
1592 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1593 String message = "Appop Denial: starting " + intent.toString()
1594 + " from " + callerApp + " (pid=" + callingPid
1595 + ", uid=" + callingUid + ")"
1596 + " requires " + AppOpsManager.permissionToOp(
1597 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1598 Slog.w(TAG, message);
1600 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1601 String message = "Appop Denial: starting " + intent.toString()
1602 + " from " + callerApp + " (pid=" + callingPid
1603 + ", uid=" + callingUid + ")"
1604 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1605 Slog.w(TAG, message);
1610 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
1611 callingPid, resolvedType, aInfo.applicationInfo);
1613 if (mService.mController != null) {
1615 // The Intent we give to the watcher has the extra data
1616 // stripped off, since it can contain private information.
1617 Intent watchIntent = intent.cloneFilter();
1618 abort |= !mService.mController.activityStarting(watchIntent,
1619 aInfo.applicationInfo.packageName);
1620 } catch (RemoteException e) {
1621 mService.mController = null;
1626 if (resultRecord != null) {
1627 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
1628 Activity.RESULT_CANCELED, null);
1630 // We pretend to the caller that it was really started, but
1631 // they will just get a cancel result.
1632 ActivityOptions.abort(options);
1633 return ActivityManager.START_SUCCESS;
1636 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
1637 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
1638 requestCode, componentSpecified, voiceSession != null, this, container, options);
1639 if (outActivity != null) {
1643 if (r.appTimeTracker == null && sourceRecord != null) {
1644 // If the caller didn't specify an explicit time tracker, we want to continue
1645 // tracking under any it has.
1646 r.appTimeTracker = sourceRecord.appTimeTracker;
1649 final ActivityStack stack = mFocusedStack;
1650 if (voiceSession == null && (stack.mResumedActivity == null
1651 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
1652 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1653 realCallingPid, realCallingUid, "Activity start")) {
1654 PendingActivityLaunch pal =
1655 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
1656 mPendingActivityLaunches.add(pal);
1657 ActivityOptions.abort(options);
1658 return ActivityManager.START_SWITCHES_CANCELED;
1662 if (mService.mDidAppSwitch) {
1663 // This is the second allowed switch since we stopped switches,
1664 // so now just generally allow switches. Use case: user presses
1665 // home (switches disabled, switch to home, mDidAppSwitch now true);
1666 // user taps a home icon (coming from home so allowed, we hit here
1667 // and now allow anyone to switch again).
1668 mService.mAppSwitchesAllowedTime = 0;
1670 mService.mDidAppSwitch = true;
1673 doPendingActivityLaunchesLocked(false);
1675 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
1676 startFlags, true, options, inTask);
1679 // If someone asked to have the keyguard dismissed on the next
1680 // activity start, but we are not actually doing an activity
1681 // switch... just dismiss the keyguard now, because we
1682 // probably want to see whatever is behind it.
1683 notifyActivityDrawnForKeyguard();
1688 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
1689 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
1690 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1691 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
1692 == PackageManager.PERMISSION_DENIED) {
1693 return ACTIVITY_RESTRICTION_PERMISSION;
1696 if (activityInfo.permission == null) {
1697 return ACTIVITY_RESTRICTION_NONE;
1700 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1701 if (opCode == AppOpsManager.OP_NONE) {
1702 return ACTIVITY_RESTRICTION_NONE;
1705 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1706 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1707 if (!ignoreTargetSecurity) {
1708 return ACTIVITY_RESTRICTION_APPOP;
1712 return ACTIVITY_RESTRICTION_NONE;
1715 private int getActionRestrictionForCallingPackage(String action,
1716 String callingPackage, int callingPid, int callingUid) {
1717 if (action == null) {
1718 return ACTIVITY_RESTRICTION_NONE;
1721 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1722 if (permission == null) {
1723 return ACTIVITY_RESTRICTION_NONE;
1726 final PackageInfo packageInfo;
1728 packageInfo = mService.mContext.getPackageManager()
1729 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1730 } catch (PackageManager.NameNotFoundException e) {
1731 Slog.i(TAG, "Cannot find package info for " + callingPackage);
1732 return ACTIVITY_RESTRICTION_NONE;
1735 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
1736 return ACTIVITY_RESTRICTION_NONE;
1739 if (mService.checkPermission(permission, callingPid, callingUid) ==
1740 PackageManager.PERMISSION_DENIED) {
1741 return ACTIVITY_RESTRICTION_PERMISSION;
1744 final int opCode = AppOpsManager.permissionToOpCode(permission);
1745 if (opCode == AppOpsManager.OP_NONE) {
1746 return ACTIVITY_RESTRICTION_NONE;
1749 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1750 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1751 return ACTIVITY_RESTRICTION_APPOP;
1754 return ACTIVITY_RESTRICTION_NONE;
1757 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
1758 final TaskRecord task = r.task;
1760 // On leanback only devices we should keep all activities in the same stack.
1761 if (!mLeanbackOnlyDevice &&
1762 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
1764 ActivityStack stack;
1766 if (task != null && task.stack != null) {
1768 if (stack.isOnHomeDisplay()) {
1769 if (mFocusedStack != stack) {
1770 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1771 "computeStackFocus: Setting " + "focused stack to r=" + r
1774 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1775 "computeStackFocus: Focused stack already=" + mFocusedStack);
1781 final ActivityContainer container = r.mInitialActivityContainer;
1782 if (container != null) {
1783 // The first time put it on the desired stack, after this put on task stack.
1784 r.mInitialActivityContainer = null;
1785 return container.mStack;
1788 if (mFocusedStack != mHomeStack && (!newTask ||
1789 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
1790 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1791 "computeStackFocus: Have a focused stack=" + mFocusedStack);
1792 return mFocusedStack;
1795 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1796 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1797 stack = homeDisplayStacks.get(stackNdx);
1798 if (!stack.isHomeStack()) {
1799 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1800 "computeStackFocus: Setting focused stack=" + stack);
1805 // Need to create an app stack for this user.
1806 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
1807 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1808 + r + " stackId=" + stack.mStackId);
1814 boolean setFocusedStack(ActivityRecord r, String reason) {
1816 // Not sure what you are trying to do, but it is not going to work...
1819 final TaskRecord task = r.task;
1820 if (task == null || task.stack == null) {
1821 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1824 task.stack.moveToFront(reason);
1828 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
1829 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
1830 boolean doResume, Bundle options, TaskRecord inTask) {
1831 final Intent intent = r.intent;
1832 final int callingUid = r.launchedFromUid;
1834 // In some flows in to this function, we retrieve the task record and hold on to it
1835 // without a lock before calling back in to here... so the task at this point may
1836 // not actually be in recents. Check for that, and if it isn't in recents just
1837 // consider it invalid.
1838 if (inTask != null && !inTask.inRecents) {
1839 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1843 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
1844 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1845 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
1847 int launchFlags = intent.getFlags();
1848 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
1849 (launchSingleInstance || launchSingleTask)) {
1850 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1851 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1852 "\"singleInstance\" or \"singleTask\"");
1854 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1856 switch (r.info.documentLaunchMode) {
1857 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1859 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1860 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1862 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1863 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1865 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1866 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1871 final boolean launchTaskBehind = r.mLaunchTaskBehind
1872 && !launchSingleTask && !launchSingleInstance
1873 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
1875 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1876 && r.resultTo.task.stack != null) {
1877 // For whatever reason this activity is being launched into a new
1878 // task... yet the caller has requested a result back. Well, that
1879 // is pretty messed up, so instead immediately send back a cancel
1880 // and let the new task continue launched as normal without a
1881 // dependency on its originator.
1882 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1883 r.resultTo.task.stack.sendActivityResultLocked(-1,
1884 r.resultTo, r.resultWho, r.requestCode,
1885 Activity.RESULT_CANCELED, null);
1889 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1890 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1893 // If we are actually going to launch in to a new task, there are some cases where
1894 // we further want to do multiple task.
1895 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1896 if (launchTaskBehind
1897 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1898 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1902 // We'll invoke onUserLeaving before onPause only if the launching
1903 // activity did not explicitly state that this is an automated launch.
1904 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
1905 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1906 "startActivity() => mUserLeaving=" + mUserLeaving);
1908 // If the caller has asked not to resume at this point, we make note
1909 // of this in the record so that we can skip it when trying to find
1910 // the top running activity.
1912 r.delayedResume = true;
1915 ActivityRecord notTop =
1916 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
1918 // If the onlyIfNeeded flag is set, then we can do this if the activity
1919 // being launched is the same as the one making the call... or, as
1920 // a special case, if we do not know the caller then we count the
1921 // current top activity as the caller.
1922 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1923 ActivityRecord checkedCaller = sourceRecord;
1924 if (checkedCaller == null) {
1925 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
1927 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1928 // Caller is not the same as launcher, so always needed.
1929 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1933 boolean addingToTask = false;
1934 TaskRecord reuseTask = null;
1936 // If the caller is not coming from another activity, but has given us an
1937 // explicit task into which they would like us to launch the new activity,
1938 // then let's see about doing that.
1939 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1940 final Intent baseIntent = inTask.getBaseIntent();
1941 final ActivityRecord root = inTask.getRootActivity();
1942 if (baseIntent == null) {
1943 ActivityOptions.abort(options);
1944 throw new IllegalArgumentException("Launching into task without base intent: "
1948 // If this task is empty, then we are adding the first activity -- it
1949 // determines the root, and must be launching as a NEW_TASK.
1950 if (launchSingleInstance || launchSingleTask) {
1951 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1952 ActivityOptions.abort(options);
1953 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1954 + r + " into different task " + inTask);
1957 ActivityOptions.abort(options);
1958 throw new IllegalArgumentException("Caller with inTask " + inTask
1959 + " has root " + root + " but target is singleInstance/Task");
1963 // If task is empty, then adopt the interesting intent launch flags in to the
1964 // activity being started.
1966 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1967 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1968 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1969 launchFlags = (launchFlags&~flagsOfInterest)
1970 | (baseIntent.getFlags()&flagsOfInterest);
1971 intent.setFlags(launchFlags);
1972 inTask.setIntent(r);
1973 addingToTask = true;
1975 // If the task is not empty and the caller is asking to start it as the root
1976 // of a new task, then we don't actually want to start this on the task. We
1977 // will bring the task to the front, and possibly give it a new intent.
1978 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1979 addingToTask = false;
1982 addingToTask = true;
1990 if (inTask == null) {
1991 if (sourceRecord == null) {
1992 // This activity is not being started from another... in this
1993 // case we -always- start a new task.
1994 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1995 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1996 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1997 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1999 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2000 // The original activity who is starting us is running as a single
2001 // instance... this new activity it is starting must go on its
2003 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2004 } else if (launchSingleInstance || launchSingleTask) {
2005 // The activity being started is a single instance... it always
2006 // gets launched into its own task.
2007 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2011 ActivityInfo newTaskInfo = null;
2012 Intent newTaskIntent = null;
2013 ActivityStack sourceStack;
2014 if (sourceRecord != null) {
2015 if (sourceRecord.finishing) {
2016 // If the source is finishing, we can't further count it as our source. This
2017 // is because the task it is associated with may now be empty and on its way out,
2018 // so we don't want to blindly throw it in to that task. Instead we will take
2019 // the NEW_TASK flow and try to find a task for it. But save the task information
2020 // so it can be used when creating the new task.
2021 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2022 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2023 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2024 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2025 newTaskInfo = sourceRecord.info;
2026 newTaskIntent = sourceRecord.task.intent;
2028 sourceRecord = null;
2031 sourceStack = sourceRecord.task.stack;
2037 boolean movedHome = false;
2038 ActivityStack targetStack;
2040 intent.setFlags(launchFlags);
2041 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
2043 // We may want to try to place the new activity in to an existing task. We always
2044 // do this if the target activity is singleTask or singleInstance; we will also do
2045 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2046 // us to still place it in a new task: multi task, always doc mode, or being asked to
2047 // launch this as a new task behind the current one.
2048 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2049 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
2050 || launchSingleInstance || launchSingleTask) {
2051 // If bring to front is requested, and no result is requested and we have not
2052 // been given an explicit task to launch in to, and
2053 // we can find a task that was started with this same
2054 // component, then instead of launching bring that one to the front.
2055 if (inTask == null && r.resultTo == null) {
2056 // See if there is a task to bring to the front. If this is
2057 // a SINGLE_INSTANCE activity, there can be one and only one
2058 // instance of it in the history, and it is always in its own
2059 // unique task, so we do a special search.
2060 ActivityRecord intentActivity = !launchSingleInstance ?
2061 findTaskLocked(r) : findActivityLocked(intent, r.info);
2062 if (intentActivity != null) {
2063 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2064 // but still needs to be a lock task mode violation since the task gets
2065 // cleared out and the device would otherwise leave the locked task.
2066 if (isLockTaskModeViolation(intentActivity.task,
2067 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2068 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
2069 showLockTaskToast();
2070 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
2071 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2073 if (r.task == null) {
2074 r.task = intentActivity.task;
2076 if (intentActivity.task.intent == null) {
2077 // This task was started because of movement of
2078 // the activity based on affinity... now that we
2079 // are actually launching it, we can assign the
2081 intentActivity.task.setIntent(r);
2083 targetStack = intentActivity.task.stack;
2084 targetStack.mLastPausedActivity = null;
2085 // If the target task is not in the front, then we need
2086 // to bring it to the front... except... well, with
2087 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2088 // to have the same behavior as if a new instance was
2089 // being started, which means not bringing it to the front
2090 // if the caller is not itself in the front.
2091 final ActivityStack focusStack = getFocusedStack();
2092 ActivityRecord curTop = (focusStack == null)
2093 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
2094 boolean movedToFront = false;
2095 if (curTop != null && (curTop.task != intentActivity.task ||
2096 curTop.task != focusStack.topTask())) {
2097 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
2098 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2099 sourceStack.topActivity().task == sourceRecord.task)) {
2100 // We really do want to push this one into the user's face, right now.
2101 if (launchTaskBehind && sourceRecord != null) {
2102 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2105 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
2106 options, r.appTimeTracker, "bringingFoundTaskToFront");
2107 movedToFront = true;
2109 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2110 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
2111 // Caller wants to appear on home activity.
2112 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
2117 if (!movedToFront) {
2118 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
2119 + " from " + intentActivity);
2120 targetStack.moveToFront("intentActivityFound");
2123 // If the caller has requested that the target task be
2124 // reset, then do so.
2125 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2126 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2128 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2129 // We don't need to start a new activity, and
2130 // the client said not to do anything if that
2131 // is the case, so this is it! And for paranoia, make
2132 // sure we have correctly resumed the top activity.
2134 resumeTopActivitiesLocked(targetStack, null, options);
2136 // Make sure to notify Keyguard as well if we are not running an app
2137 // transition later.
2138 if (!movedToFront) {
2139 notifyActivityDrawnForKeyguard();
2142 ActivityOptions.abort(options);
2144 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2146 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2147 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
2148 // The caller has requested to completely replace any
2149 // existing task with its new activity. Well that should
2150 // not be too hard...
2151 reuseTask = intentActivity.task;
2152 reuseTask.performClearTaskLocked();
2153 reuseTask.setIntent(r);
2154 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
2155 || launchSingleInstance || launchSingleTask) {
2156 // In this situation we want to remove all activities
2157 // from the task up to the one being started. In most
2158 // cases this means we are resetting the task to its
2160 ActivityRecord top =
2161 intentActivity.task.performClearTaskLocked(r, launchFlags);
2163 if (top.frontOfTask) {
2164 // Activity aliases may mean we use different
2165 // intents for the top activity, so make sure
2166 // the task now has the identity of the new
2168 top.task.setIntent(r);
2170 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2172 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
2174 // A special case: we need to start the activity because it is not
2175 // currently running, and the caller has asked to clear the current
2176 // task to have this activity at the top.
2177 addingToTask = true;
2178 // Now pretend like this activity is being started by the top of its
2179 // task, so it is put in the right place.
2180 sourceRecord = intentActivity;
2181 TaskRecord task = sourceRecord.task;
2182 if (task != null && task.stack == null) {
2183 // Target stack got cleared when we all activities were removed
2184 // above. Go ahead and reset it.
2185 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2186 targetStack.addTask(
2187 task, !launchTaskBehind /* toTop */, false /* moving */);
2191 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2192 // In this case the top activity on the task is the
2193 // same as the one being launched, so we take that
2194 // as a request to bring the task to the foreground.
2195 // If the top activity in the task is the root
2196 // activity, deliver this new intent to it if it
2198 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
2199 && intentActivity.realActivity.equals(r.realActivity)) {
2200 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2201 intentActivity.task);
2202 if (intentActivity.frontOfTask) {
2203 intentActivity.task.setIntent(r);
2205 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2206 r.launchedFromPackage);
2207 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2208 // In this case we are launching the root activity
2209 // of the task, but with a different intent. We
2210 // should start a new instance on top.
2211 addingToTask = true;
2212 sourceRecord = intentActivity;
2214 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2215 // In this case an activity is being launched in to an
2216 // existing task, without resetting that task. This
2217 // is typically the situation of launching an activity
2218 // from a notification or shortcut. We want to place
2219 // the new activity on top of the current task.
2220 addingToTask = true;
2221 sourceRecord = intentActivity;
2222 } else if (!intentActivity.task.rootWasReset) {
2223 // In this case we are launching in to an existing task
2224 // that has not yet been started from its front door.
2225 // The current task has been brought to the front.
2226 // Ideally, we'd probably like to place this new task
2227 // at the bottom of its stack, but that's a little hard
2228 // to do with the current organization of the code so
2229 // for now we'll just drop it.
2230 intentActivity.task.setIntent(r);
2232 if (!addingToTask && reuseTask == null) {
2233 // We didn't do anything... but it was needed (a.k.a., client
2234 // don't use that intent!) And for paranoia, make
2235 // sure we have correctly resumed the top activity.
2237 targetStack.resumeTopActivityLocked(null, options);
2238 if (!movedToFront) {
2239 // Make sure to notify Keyguard as well if we are not running an app
2240 // transition later.
2241 notifyActivityDrawnForKeyguard();
2244 ActivityOptions.abort(options);
2246 return ActivityManager.START_TASK_TO_FRONT;
2252 //String uri = r.intent.toURI();
2253 //Intent intent2 = new Intent(uri);
2254 //Slog.i(TAG, "Given intent: " + r.intent);
2255 //Slog.i(TAG, "URI is: " + uri);
2256 //Slog.i(TAG, "To intent: " + intent2);
2258 if (r.packageName != null) {
2259 // If the activity being launched is the same as the one currently
2260 // at the top, then we need to check if it should only be launched
2262 ActivityStack topStack = mFocusedStack;
2263 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
2264 if (top != null && r.resultTo == null) {
2265 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2266 if (top.app != null && top.app.thread != null) {
2267 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2268 || launchSingleTop || launchSingleTask) {
2269 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2271 // For paranoia, make sure we have correctly
2272 // resumed the top activity.
2273 topStack.mLastPausedActivity = null;
2275 resumeTopActivitiesLocked();
2277 ActivityOptions.abort(options);
2278 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2279 // We don't need to start a new activity, and
2280 // the client said not to do anything if that
2281 // is the case, so this is it!
2282 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2284 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
2285 return ActivityManager.START_DELIVERED_TO_TOP;
2292 if (r.resultTo != null && r.resultTo.task.stack != null) {
2293 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2294 r.requestCode, Activity.RESULT_CANCELED, null);
2296 ActivityOptions.abort(options);
2297 return ActivityManager.START_CLASS_NOT_FOUND;
2300 boolean newTask = false;
2301 boolean keepCurTransition = false;
2303 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
2304 sourceRecord.task : null;
2306 // Should this be considered a new task?
2307 if (r.resultTo == null && inTask == null && !addingToTask
2308 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
2310 targetStack = computeStackFocus(r, newTask);
2311 targetStack.moveToFront("startingNewTask");
2313 if (reuseTask == null) {
2314 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2315 newTaskInfo != null ? newTaskInfo : r.info,
2316 newTaskIntent != null ? newTaskIntent : intent,
2317 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2319 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2320 "Starting new activity " + r + " in new task " + r.task);
2322 r.setTask(reuseTask, taskToAffiliate);
2324 if (isLockTaskModeViolation(r.task)) {
2325 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2326 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2330 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2331 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
2332 // Caller wants to appear on home activity, so before starting
2333 // their own activity we will bring home to the front.
2334 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
2337 } else if (sourceRecord != null) {
2338 final TaskRecord sourceTask = sourceRecord.task;
2339 if (isLockTaskModeViolation(sourceTask)) {
2340 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2341 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2343 targetStack = sourceTask.stack;
2344 targetStack.moveToFront("sourceStackToFront");
2345 final TaskRecord topTask = targetStack.topTask();
2346 if (topTask != sourceTask) {
2347 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2348 r.appTimeTracker, "sourceTaskToFront");
2350 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2351 // In this case, we are adding the activity to an existing
2352 // task, but the caller has asked to clear that task if the
2353 // activity is already running.
2354 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
2355 keepCurTransition = true;
2357 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
2358 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
2359 // For paranoia, make sure we have correctly
2360 // resumed the top activity.
2361 targetStack.mLastPausedActivity = null;
2363 targetStack.resumeTopActivityLocked(null);
2365 ActivityOptions.abort(options);
2366 return ActivityManager.START_DELIVERED_TO_TOP;
2368 } else if (!addingToTask &&
2369 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2370 // In this case, we are launching an activity in our own task
2371 // that may already be running somewhere in the history, and
2372 // we want to shuffle it to the front of the stack if so.
2373 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
2375 final TaskRecord task = top.task;
2376 task.moveActivityToFrontLocked(top);
2377 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
2378 top.updateOptionsLocked(options);
2379 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
2380 targetStack.mLastPausedActivity = null;
2382 targetStack.resumeTopActivityLocked(null);
2384 return ActivityManager.START_DELIVERED_TO_TOP;
2387 // An existing activity is starting this new activity, so we want
2388 // to keep the new one in the same task as the one that is starting
2390 r.setTask(sourceTask, null);
2391 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
2392 + " in existing task " + r.task + " from source " + sourceRecord);
2394 } else if (inTask != null) {
2395 // The caller is asking that the new activity be started in an explicit
2396 // task it has provided to us.
2397 if (isLockTaskModeViolation(inTask)) {
2398 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2399 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2401 targetStack = inTask.stack;
2402 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2405 // Check whether we should actually launch the new activity in to the task,
2406 // or just reuse the current activity on top.
2407 ActivityRecord top = inTask.getTopActivity();
2408 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2409 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2410 || launchSingleTop || launchSingleTask) {
2411 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2412 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2413 // We don't need to start a new activity, and
2414 // the client said not to do anything if that
2415 // is the case, so this is it!
2416 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2418 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
2419 return ActivityManager.START_DELIVERED_TO_TOP;
2423 if (!addingToTask) {
2424 // We don't actually want to have this activity added to the task, so just
2425 // stop here but still tell the caller that we consumed the intent.
2426 ActivityOptions.abort(options);
2427 return ActivityManager.START_TASK_TO_FRONT;
2430 r.setTask(inTask, null);
2431 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
2432 + " in explicit task " + r.task);
2435 // This not being started from an existing activity, and not part
2436 // of a new task... just put it in the top task, though these days
2437 // this case should never happen.
2438 targetStack = computeStackFocus(r, newTask);
2439 targetStack.moveToFront("addingToTopTask");
2440 ActivityRecord prev = targetStack.topActivity();
2441 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
2442 r.info, intent, null, null, true), null);
2443 mWindowManager.moveTaskToTop(r.task.taskId);
2444 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
2445 + " in new guessed " + r.task);
2448 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
2449 intent, r.getUriPermissionsLocked(), r.userId);
2451 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2452 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2455 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2457 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
2458 targetStack.mLastPausedActivity = null;
2459 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
2460 if (!launchTaskBehind) {
2461 // Don't set focus on an activity that's going to the back.
2462 mService.setFocusedActivityLocked(r, "startedActivity");
2464 return ActivityManager.START_SUCCESS;
2467 final void doPendingActivityLaunchesLocked(boolean doResume) {
2468 while (!mPendingActivityLaunches.isEmpty()) {
2469 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
2470 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
2471 doResume && mPendingActivityLaunches.isEmpty(), null, null);
2475 void removePendingActivityLaunchesLocked(ActivityStack stack) {
2476 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2477 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
2478 if (pal.stack == stack) {
2479 mPendingActivityLaunches.remove(palNdx);
2484 void setLaunchSource(int uid) {
2485 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2488 void acquireLaunchWakelock() {
2489 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2490 throw new IllegalStateException("Calling must be system uid");
2492 mLaunchingActivity.acquire();
2493 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2494 // To be safe, don't allow the wake lock to be held for too long.
2495 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2500 * Called when the frontmost task is idle.
2501 * @return the state of mService.mBooting before this was called.
2503 private boolean checkFinishBootingLocked() {
2504 final boolean booting = mService.mBooting;
2505 boolean enableScreen = false;
2506 mService.mBooting = false;
2507 if (!mService.mBooted) {
2508 mService.mBooted = true;
2509 enableScreen = true;
2511 if (booting || enableScreen) {
2512 mService.postFinishBooting(booting, enableScreen);
2518 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2519 Configuration config) {
2520 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
2522 ArrayList<ActivityRecord> stops = null;
2523 ArrayList<ActivityRecord> finishes = null;
2524 ArrayList<UserState> startingUsers = null;
2527 boolean booting = false;
2528 boolean activityRemoved = false;
2530 ActivityRecord r = ActivityRecord.forTokenLocked(token);
2532 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2533 + Debug.getCallers(4));
2534 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2535 r.finishLaunchTickingLocked();
2537 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
2540 // This is a hack to semi-deal with a race condition
2541 // in the client where it can be constructed with a
2542 // newer configuration from when we asked it to launch.
2543 // We'll update with whatever configuration it now says
2544 // it used to launch.
2545 if (config != null) {
2546 r.configuration = config;
2549 // We are now idle. If someone is waiting for a thumbnail from
2550 // us, we can now deliver.
2553 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
2554 if (isFrontStack(r.task.stack) || fromTimeout) {
2555 booting = checkFinishBootingLocked();
2559 if (allResumedActivitiesIdle()) {
2561 mService.scheduleAppGcsLocked();
2564 if (mLaunchingActivity.isHeld()) {
2565 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2566 if (VALIDATE_WAKE_LOCK_CALLER &&
2567 Binder.getCallingUid() != Process.myUid()) {
2568 throw new IllegalStateException("Calling must be system uid");
2570 mLaunchingActivity.release();
2572 ensureActivitiesVisibleLocked(null, 0);
2575 // Atomically retrieve all of the other things to do.
2576 stops = processStoppingActivitiesLocked(true);
2577 NS = stops != null ? stops.size() : 0;
2578 if ((NF = mFinishingActivities.size()) > 0) {
2579 finishes = new ArrayList<>(mFinishingActivities);
2580 mFinishingActivities.clear();
2583 if (mStartingUsers.size() > 0) {
2584 startingUsers = new ArrayList<>(mStartingUsers);
2585 mStartingUsers.clear();
2588 // Stop any activities that are scheduled to do so but have been
2589 // waiting for the next one to start.
2590 for (int i = 0; i < NS; i++) {
2592 final ActivityStack stack = r.task.stack;
2593 if (stack != null) {
2595 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2597 stack.stopActivityLocked(r);
2602 // Finish any activities that are scheduled to do so but have been
2603 // waiting for the next one to start.
2604 for (int i = 0; i < NF; i++) {
2605 r = finishes.get(i);
2606 final ActivityStack stack = r.task.stack;
2607 if (stack != null) {
2608 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2613 // Complete user switch
2614 if (startingUsers != null) {
2615 for (int i = 0; i < startingUsers.size(); i++) {
2616 mService.finishUserSwitch(startingUsers.get(i));
2619 // Complete starting up of background users
2620 if (mStartingBackgroundUsers.size() > 0) {
2621 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
2622 mStartingBackgroundUsers.clear();
2623 for (int i = 0; i < startingUsers.size(); i++) {
2624 mService.finishUserBoot(startingUsers.get(i));
2629 mService.trimApplications();
2631 //mWindowManager.dump();
2633 if (activityRemoved) {
2634 resumeTopActivitiesLocked();
2640 boolean handleAppDiedLocked(ProcessRecord app) {
2641 boolean hasVisibleActivities = false;
2642 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2643 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2644 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2645 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2648 return hasVisibleActivities;
2651 void closeSystemDialogsLocked() {
2652 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2653 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2654 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2655 stacks.get(stackNdx).closeSystemDialogsLocked();
2660 void removeUserLocked(int userId) {
2661 mUserStackInFront.delete(userId);
2665 * @return true if some activity was finished (or would have finished if doit were true).
2667 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2668 boolean doit, boolean evenPersistent, int userId) {
2669 boolean didSomething = false;
2670 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2671 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2672 final int numStacks = stacks.size();
2673 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2674 final ActivityStack stack = stacks.get(stackNdx);
2675 if (stack.finishDisabledPackageActivitiesLocked(
2676 packageName, filterByClasses, doit, evenPersistent, userId)) {
2677 didSomething = true;
2681 return didSomething;
2684 void updatePreviousProcessLocked(ActivityRecord r) {
2685 // Now that this process has stopped, we may want to consider
2686 // it to be the previous app to try to keep around in case
2687 // the user wants to return to it.
2689 // First, found out what is currently the foreground app, so that
2690 // we don't blow away the previous app if this activity is being
2691 // hosted by the process that is actually still the foreground.
2692 ProcessRecord fgApp = null;
2693 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2694 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2695 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2696 final ActivityStack stack = stacks.get(stackNdx);
2697 if (isFrontStack(stack)) {
2698 if (stack.mResumedActivity != null) {
2699 fgApp = stack.mResumedActivity.app;
2700 } else if (stack.mPausingActivity != null) {
2701 fgApp = stack.mPausingActivity.app;
2708 // Now set this one as the previous process, only if that really
2710 if (r.app != null && fgApp != null && r.app != fgApp
2711 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
2712 && r.app != mService.mHomeProcess) {
2713 mService.mPreviousProcess = r.app;
2714 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2718 boolean resumeTopActivitiesLocked() {
2719 return resumeTopActivitiesLocked(null, null, null);
2722 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2723 Bundle targetOptions) {
2724 if (targetStack == null) {
2725 targetStack = mFocusedStack;
2727 // Do targetStack first.
2728 boolean result = false;
2729 if (isFrontStack(targetStack)) {
2730 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2733 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2734 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2735 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2736 final ActivityStack stack = stacks.get(stackNdx);
2737 if (stack == targetStack) {
2738 // Already started above.
2741 if (isFrontStack(stack)) {
2742 stack.resumeTopActivityLocked(null);
2749 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2750 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2751 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2752 final int numStacks = stacks.size();
2753 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2754 final ActivityStack stack = stacks.get(stackNdx);
2755 stack.finishTopRunningActivityLocked(app, reason);
2760 void finishVoiceTask(IVoiceInteractionSession session) {
2761 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2762 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2763 final int numStacks = stacks.size();
2764 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2765 final ActivityStack stack = stacks.get(stackNdx);
2766 stack.finishVoiceTask(session);
2771 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
2772 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2773 mUserLeaving = true;
2775 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2776 // Caller wants the home activity moved with it. To accomplish this,
2777 // we'll just indicate that this task returns to the home task.
2778 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
2780 if (task.stack == null) {
2781 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2782 + task + " to front. Stack is null");
2785 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2786 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2788 if (DEBUG_STACK) Slog.d(TAG_STACK,
2789 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
2792 ActivityStack getStack(int stackId) {
2793 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2794 if (activityContainer != null) {
2795 return activityContainer.mStack;
2800 ArrayList<ActivityStack> getStacks() {
2801 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
2802 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2803 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
2808 IBinder getHomeActivityToken() {
2809 ActivityRecord homeActivity = getHomeActivity();
2810 if (homeActivity != null) {
2811 return homeActivity.appToken;
2816 ActivityRecord getHomeActivity() {
2817 return getHomeActivityForUser(mCurrentUser);
2820 ActivityRecord getHomeActivityForUser(int userId) {
2821 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2822 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2823 final TaskRecord task = tasks.get(taskNdx);
2824 if (task.isHomeTask()) {
2825 final ArrayList<ActivityRecord> activities = task.mActivities;
2826 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2827 final ActivityRecord r = activities.get(activityNdx);
2828 if (r.isHomeActivity()
2829 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
2838 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
2839 IActivityContainerCallback callback) {
2840 ActivityContainer activityContainer =
2841 new VirtualActivityContainer(parentActivity, callback);
2842 mActivityContainers.put(activityContainer.mStackId, activityContainer);
2843 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2844 "createActivityContainer: " + activityContainer);
2845 parentActivity.mChildContainers.add(activityContainer);
2846 return activityContainer;
2849 void removeChildActivityContainers(ActivityRecord parentActivity) {
2850 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2851 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2852 ActivityContainer container = childStacks.remove(containerNdx);
2853 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2855 container.release();
2859 void deleteActivityContainer(IActivityContainer container) {
2860 ActivityContainer activityContainer = (ActivityContainer)container;
2861 if (activityContainer != null) {
2862 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2863 "deleteActivityContainer: callers=" + Debug.getCallers(4));
2864 final int stackId = activityContainer.mStackId;
2865 mActivityContainers.remove(stackId);
2866 mWindowManager.removeStack(stackId);
2870 void resizeStackLocked(int stackId, Rect bounds) {
2871 final ActivityStack stack = getStack(stackId);
2872 if (stack == null) {
2873 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2877 final ActivityRecord r = stack.topRunningActivityLocked(null);
2878 if (r != null && !r.task.mResizeable) {
2879 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
2883 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2884 if (stack.updateOverrideConfiguration(overrideConfig)) {
2886 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2887 // And we need to make sure at this point that all other activities
2888 // are made visible with the correct configuration.
2889 ensureActivitiesVisibleLocked(r, 0);
2891 resumeTopActivitiesLocked(stack, null, null);
2897 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2898 * current task stack if it only has one entry, moving the task to a stack that matches the
2899 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2900 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2901 task.mResizeable = true;
2902 final ActivityStack currentStack = task.stack;
2903 if (currentStack.isHomeStack()) {
2904 // Can't move task off the home stack. Sorry!
2908 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2909 if (matchingStackId != -1) {
2910 // There is already a stack with the right bounds!
2911 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2912 // Nothing to do here. Already in the right stack...
2915 // Move task to stack with matching bounds.
2916 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2920 if (currentStack != null && currentStack.numTasks() == 1) {
2921 // Just resize the current stack since this is the task in it.
2922 resizeStackLocked(currentStack.mStackId, bounds);
2926 // Create new stack and move the task to it.
2927 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2928 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2929 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2931 if (newStack == null) {
2932 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2935 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2936 resizeStackLocked(newStack.mStackId, bounds);
2939 ActivityStack createStackOnDisplay(int stackId, int displayId) {
2940 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2941 if (activityDisplay == null) {
2945 ActivityContainer activityContainer = new ActivityContainer(stackId);
2946 mActivityContainers.put(stackId, activityContainer);
2947 activityContainer.attachToDisplayLocked(activityDisplay);
2948 return activityContainer.mStack;
2951 int getNextStackId() {
2953 if (++mLastStackId <= HOME_STACK_ID) {
2954 mLastStackId = HOME_STACK_ID + 1;
2956 if (getStack(mLastStackId) == null) {
2960 return mLastStackId;
2963 private boolean restoreRecentTaskLocked(TaskRecord task) {
2964 ActivityStack stack = null;
2965 // Determine stack to restore task to.
2966 if (mLeanbackOnlyDevice) {
2967 // There is only one stack for lean back devices.
2970 // Look for the top stack on the home display that isn't the home stack.
2971 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2972 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2973 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
2974 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
2981 if (stack == null) {
2982 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2983 // before an application stack is created...Go ahead and create one on the default
2985 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
2986 // Restore home stack to top.
2987 moveHomeStack(true, "restoreRecentTask");
2988 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2989 "Created stack=" + stack + " for recents restoration.");
2992 if (stack == null) {
2993 // What does this mean??? Not sure how we would get here...
2994 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2995 "Unable to find/create stack to restore recent task=" + task);
2999 stack.addTask(task, false, false);
3000 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3001 "Added restored task=" + task + " to stack=" + stack);
3002 final ArrayList<ActivityRecord> activities = task.mActivities;
3003 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3004 final ActivityRecord r = activities.get(activityNdx);
3005 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
3006 r.info.screenOrientation, r.fullscreen,
3007 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0,
3008 r.userId, r.info.configChanges, task.voiceSession != null,
3009 r.mLaunchTaskBehind);
3014 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
3015 final TaskRecord task = anyTaskForIdLocked(taskId);
3017 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
3020 final ActivityStack stack = getStack(stackId);
3021 if (stack == null) {
3022 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
3025 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
3026 if (task.stack != null) {
3027 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
3029 stack.addTask(task, toTop, true);
3030 // The task might have already been running and its visibility needs to be synchronized with
3031 // the visibility of the stack / windows.
3032 stack.ensureActivitiesVisibleLocked(null, 0);
3033 resumeTopActivitiesLocked();
3036 ActivityRecord findTaskLocked(ActivityRecord r) {
3037 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
3038 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3039 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3040 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3041 final ActivityStack stack = stacks.get(stackNdx);
3042 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
3043 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
3046 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
3047 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3048 "Skipping stack: (new task not allowed) " + stack);
3051 final ActivityRecord ar = stack.findTaskLocked(r);
3057 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
3061 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
3062 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3063 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3064 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3065 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3074 void goingToSleepLocked() {
3075 scheduleSleepTimeout();
3076 if (!mGoingToSleep.isHeld()) {
3077 mGoingToSleep.acquire();
3078 if (mLaunchingActivity.isHeld()) {
3079 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3080 throw new IllegalStateException("Calling must be system uid");
3082 mLaunchingActivity.release();
3083 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
3086 checkReadyForSleepLocked();
3089 boolean shutdownLocked(int timeout) {
3090 goingToSleepLocked();
3092 boolean timedout = false;
3093 final long endTime = System.currentTimeMillis() + timeout;
3095 boolean cantShutdown = false;
3096 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3097 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3098 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3099 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3103 long timeRemaining = endTime - System.currentTimeMillis();
3104 if (timeRemaining > 0) {
3106 mService.wait(timeRemaining);
3107 } catch (InterruptedException e) {
3110 Slog.w(TAG, "Activity manager shutdown timed out");
3119 // Force checkReadyForSleep to complete.
3120 mSleepTimeout = true;
3121 checkReadyForSleepLocked();
3126 void comeOutOfSleepIfNeededLocked() {
3127 removeSleepTimeouts();
3128 if (mGoingToSleep.isHeld()) {
3129 mGoingToSleep.release();
3131 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3132 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3133 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3134 final ActivityStack stack = stacks.get(stackNdx);
3135 stack.awakeFromSleepingLocked();
3136 if (isFrontStack(stack)) {
3137 resumeTopActivitiesLocked();
3141 mGoingToSleepActivities.clear();
3144 void activitySleptLocked(ActivityRecord r) {
3145 mGoingToSleepActivities.remove(r);
3146 checkReadyForSleepLocked();
3149 void checkReadyForSleepLocked() {
3150 if (!mService.isSleepingOrShuttingDown()) {
3155 if (!mSleepTimeout) {
3156 boolean dontSleep = false;
3157 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3158 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3159 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3160 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3164 if (mStoppingActivities.size() > 0) {
3165 // Still need to tell some activities to stop; can't sleep yet.
3166 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
3167 + mStoppingActivities.size() + " activities");
3168 scheduleIdleLocked();
3172 if (mGoingToSleepActivities.size() > 0) {
3173 // Still need to tell some activities to sleep; can't sleep yet.
3174 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
3175 + mGoingToSleepActivities.size() + " activities");
3184 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3185 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3186 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3187 stacks.get(stackNdx).goToSleep();
3191 removeSleepTimeouts();
3193 if (mGoingToSleep.isHeld()) {
3194 mGoingToSleep.release();
3196 if (mService.mShuttingDown) {
3197 mService.notifyAll();
3201 boolean reportResumedActivityLocked(ActivityRecord r) {
3202 final ActivityStack stack = r.task.stack;
3203 if (isFrontStack(stack)) {
3204 mService.updateUsageStats(r, true);
3206 if (allResumedActivitiesComplete()) {
3207 ensureActivitiesVisibleLocked(null, 0);
3208 mWindowManager.executeAppTransition();
3214 void handleAppCrashLocked(ProcessRecord app) {
3215 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3216 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3217 int stackNdx = stacks.size() - 1;
3218 while (stackNdx >= 0) {
3219 stacks.get(stackNdx).handleAppCrashLocked(app);
3225 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
3226 final ActivityStack stack = r.task.stack;
3227 if (stack == null) {
3228 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3229 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
3232 final boolean isVisible = stack.hasVisibleBehindActivity();
3233 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3234 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
3236 final ActivityRecord top = topRunningActivityLocked();
3237 if (top == null || top == r || (visible == isVisible)) {
3238 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
3239 stack.setVisibleBehindActivity(visible ? r : null);
3243 // A non-top activity is reporting a visibility change.
3244 if (visible && top.fullscreen) {
3245 // Let the caller know that it can't be seen.
3246 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3247 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3248 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3251 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3252 // Only the activity set as currently visible behind should actively reset its
3253 // visible behind state.
3254 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3255 "requestVisibleBehind: returning visible=" + visible
3256 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3261 stack.setVisibleBehindActivity(visible ? r : null);
3263 // Make the activity immediately above r opaque.
3264 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3266 mService.convertFromTranslucent(next.appToken);
3269 if (top.app != null && top.app.thread != null) {
3270 // Notify the top app of the change.
3272 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3273 } catch (RemoteException e) {
3279 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3280 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3281 r.mLaunchTaskBehind = false;
3282 final TaskRecord task = r.task;
3283 task.setLastThumbnail(task.stack.screenshotActivities(r));
3284 mRecentTasks.addLocked(task);
3285 mService.notifyTaskStackChangedLocked();
3286 mWindowManager.setAppVisibility(r.appToken, false);
3289 void scheduleLaunchTaskBehindComplete(IBinder token) {
3290 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3293 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
3294 // First the front stacks. In case any are not fullscreen and are in front of home.
3295 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3296 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3297 final int topStackNdx = stacks.size() - 1;
3298 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3299 final ActivityStack stack = stacks.get(stackNdx);
3300 stack.ensureActivitiesVisibleLocked(starting, configChanges);
3305 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3306 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3307 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3308 final int topStackNdx = stacks.size() - 1;
3309 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3310 final ActivityStack stack = stacks.get(stackNdx);
3311 stack.clearOtherAppTimeTrackers(except);
3316 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
3317 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3318 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3319 final int numStacks = stacks.size();
3320 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3321 final ActivityStack stack = stacks.get(stackNdx);
3322 stack.scheduleDestroyActivities(app, reason);
3327 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3328 // Examine all activities currently running in the process.
3329 TaskRecord firstTask = null;
3330 // Tasks is non-null only if two or more tasks are found.
3331 ArraySet<TaskRecord> tasks = null;
3332 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3333 for (int i = 0; i < app.activities.size(); i++) {
3334 ActivityRecord r = app.activities.get(i);
3335 // First, if we find an activity that is in the process of being destroyed,
3336 // then we just aren't going to do anything for now; we want things to settle
3337 // down before we try to prune more activities.
3338 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
3339 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
3342 // Don't consider any activies that are currently not in a state where they
3343 // can be destroyed.
3344 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3345 || r.state == PAUSED || r.state == STOPPING) {
3346 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
3349 if (r.task != null) {
3350 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
3352 if (firstTask == null) {
3354 } else if (firstTask != r.task) {
3355 if (tasks == null) {
3356 tasks = new ArraySet<>();
3357 tasks.add(firstTask);
3363 if (tasks == null) {
3364 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
3367 // If we have activities in multiple tasks that are in a position to be destroyed,
3368 // let's iterate through the tasks and release the oldest one.
3369 final int numDisplays = mActivityDisplays.size();
3370 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3371 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3372 // Step through all stacks starting from behind, to hit the oldest things first.
3373 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3374 final ActivityStack stack = stacks.get(stackNdx);
3375 // Try to release activities in this stack; if we manage to, we are done.
3376 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3383 boolean switchUserLocked(int userId, UserState uss) {
3384 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
3385 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
3386 mCurrentUser = userId;
3388 mStartingUsers.add(uss);
3389 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3390 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3391 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3392 final ActivityStack stack = stacks.get(stackNdx);
3393 stack.switchUserLocked(userId);
3394 TaskRecord task = stack.topTask();
3396 mWindowManager.moveTaskToTop(task.taskId);
3401 ActivityStack stack = getStack(restoreStackId);
3402 if (stack == null) {
3405 final boolean homeInFront = stack.isHomeStack();
3406 if (stack.isOnHomeDisplay()) {
3407 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
3408 TaskRecord task = stack.topTask();
3410 mWindowManager.moveTaskToTop(task.taskId);
3413 // Stack was moved to another display while user was swapped out.
3414 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
3420 * Add background users to send boot completed events to.
3421 * @param userId The user being started in the background
3422 * @param uss The state object for the user.
3424 public void startBackgroundUserLocked(int userId, UserState uss) {
3425 mStartingBackgroundUsers.add(uss);
3428 /** Checks whether the userid is a profile of the current user. */
3429 boolean isCurrentProfileLocked(int userId) {
3430 if (userId == mCurrentUser) return true;
3431 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3432 if (mService.mCurrentProfileIds[i] == userId) return true;
3437 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
3438 ArrayList<ActivityRecord> stops = null;
3440 final boolean nowVisible = allResumedActivitiesVisible();
3441 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3442 ActivityRecord s = mStoppingActivities.get(activityNdx);
3443 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
3444 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
3445 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3446 if (waitingVisible && nowVisible) {
3447 mWaitingVisibleActivities.remove(s);
3449 // If this activity is finishing, it is sitting on top of
3450 // everyone else but we now know it is no longer needed...
3451 // so get rid of it. Otherwise, we need to go through the
3452 // normal flow and hide it once we determine that it is
3453 // hidden by the activities in front of it.
3454 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
3455 mWindowManager.setAppVisibility(s.appToken, false);
3458 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
3459 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
3460 if (stops == null) {
3461 stops = new ArrayList<>();
3464 mStoppingActivities.remove(activityNdx);
3471 void validateTopActivitiesLocked() {
3472 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3473 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3474 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3475 final ActivityStack stack = stacks.get(stackNdx);
3476 final ActivityRecord r = stack.topRunningActivityLocked(null);
3477 final ActivityState state = r == null ? DESTROYED : r.state;
3478 if (isFrontStack(stack)) {
3479 if (r == null) Slog.e(TAG,
3480 "validateTop...: null top activity, stack=" + stack);
3482 final ActivityRecord pausing = stack.mPausingActivity;
3483 if (pausing != null && pausing == r) Slog.e(TAG,
3484 "validateTop...: top stack has pausing activity r=" + r
3485 + " state=" + state);
3486 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3487 "validateTop...: activity in front not resumed r=" + r
3488 + " state=" + state);
3491 final ActivityRecord resumed = stack.mResumedActivity;
3492 if (resumed != null && resumed == r) Slog.e(TAG,
3493 "validateTop...: back stack has resumed activity r=" + r
3494 + " state=" + state);
3495 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3496 "validateTop...: activity in back resumed r=" + r + " state=" + state);
3502 private String lockTaskModeToString() {
3503 switch (mLockTaskModeState) {
3504 case LOCK_TASK_MODE_LOCKED:
3506 case LOCK_TASK_MODE_PINNED:
3508 case LOCK_TASK_MODE_NONE:
3510 default: return "unknown=" + mLockTaskModeState;
3514 public void dump(PrintWriter pw, String prefix) {
3515 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
3516 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
3517 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3518 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3519 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
3520 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
3521 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3522 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3523 if (packages.size() > 0) {
3524 pw.println(" mLockTaskPackages (userId:packages)=");
3525 for (int i = 0; i < packages.size(); ++i) {
3526 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3527 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3530 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3533 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3534 return mFocusedStack.getDumpActivitiesLocked(name);
3537 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3538 boolean needSep, String prefix) {
3539 if (activity != null) {
3540 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3545 pw.println(activity);
3552 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3553 boolean dumpClient, String dumpPackage) {
3554 boolean printed = false;
3555 boolean needSep = false;
3556 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3557 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
3558 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
3559 pw.println(" (activities from top to bottom):");
3560 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3561 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3562 final ActivityStack stack = stacks.get(stackNdx);
3563 StringBuilder stackHeader = new StringBuilder(128);
3564 stackHeader.append(" Stack #");
3565 stackHeader.append(stack.mStackId);
3566 stackHeader.append(":");
3567 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3568 needSep, stackHeader.toString());
3569 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3570 !dumpAll, false, dumpPackage, true,
3571 " Running activities (most recent first):", null);
3574 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3575 " mPausingActivity: ");
3580 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3581 " mResumedActivity: ");
3587 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3588 " mLastPausedActivity: ");
3593 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3594 needSep, " mLastNoHistoryActivity: ");
3600 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3601 false, dumpPackage, true, " Activities waiting to finish:", null);
3602 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3603 false, dumpPackage, true, " Activities waiting to stop:", null);
3604 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3605 false, dumpPackage, true, " Activities waiting for another to become visible:",
3607 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3608 false, dumpPackage, true, " Activities waiting to sleep:", null);
3609 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3610 false, dumpPackage, true, " Activities waiting to sleep:", null);
3615 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
3616 String prefix, String label, boolean complete, boolean brief, boolean client,
3617 String dumpPackage, boolean needNL, String header1, String header2) {
3618 TaskRecord lastTask = null;
3619 String innerPrefix = null;
3620 String[] args = null;
3621 boolean printed = false;
3622 for (int i=list.size()-1; i>=0; i--) {
3623 final ActivityRecord r = list.get(i);
3624 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3627 if (innerPrefix == null) {
3628 innerPrefix = prefix + " ";
3629 args = new String[0];
3632 final boolean full = !brief && (complete || !r.isInHistory());
3637 if (header1 != null) {
3638 pw.println(header1);
3641 if (header2 != null) {
3642 pw.println(header2);
3645 if (lastTask != r.task) {
3648 pw.print(full ? "* " : " ");
3649 pw.println(lastTask);
3651 lastTask.dump(pw, prefix + " ");
3652 } else if (complete) {
3653 // Complete + brief == give a summary. Isn't that obvious?!?
3654 if (lastTask.intent != null) {
3655 pw.print(prefix); pw.print(" ");
3656 pw.println(lastTask.intent.toInsecureStringWithClip());
3660 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3661 pw.print(" #"); pw.print(i); pw.print(": ");
3664 r.dump(pw, innerPrefix);
3665 } else if (complete) {
3666 // Complete + brief == give a summary. Isn't that obvious?!?
3667 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3668 if (r.app != null) {
3669 pw.print(innerPrefix); pw.println(r.app);
3672 if (client && r.app != null && r.app.thread != null) {
3673 // flush anything that is already in the PrintWriter since the thread is going
3674 // to write to the file descriptor directly
3677 TransferPipe tp = new TransferPipe();
3679 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3680 r.appToken, innerPrefix, args);
3681 // Short timeout, since blocking here can
3682 // deadlock with the application.
3687 } catch (IOException e) {
3688 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3689 } catch (RemoteException e) {
3690 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3698 void scheduleIdleTimeoutLocked(ActivityRecord next) {
3699 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3700 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
3701 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3702 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
3705 final void scheduleIdleLocked() {
3706 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
3709 void removeTimeoutsForActivityLocked(ActivityRecord r) {
3710 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3711 + Debug.getCallers(4));
3712 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3715 final void scheduleResumeTopActivities() {
3716 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3717 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3721 void removeSleepTimeouts() {
3722 mSleepTimeout = false;
3723 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3726 final void scheduleSleepTimeout() {
3727 removeSleepTimeouts();
3728 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3732 public void onDisplayAdded(int displayId) {
3733 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
3734 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3738 public void onDisplayRemoved(int displayId) {
3739 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
3740 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3744 public void onDisplayChanged(int displayId) {
3745 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
3746 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3749 private void handleDisplayAdded(int displayId) {
3751 synchronized (mService) {
3752 newDisplay = mActivityDisplays.get(displayId) == null;
3754 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
3755 if (activityDisplay.mDisplay == null) {
3756 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3759 mActivityDisplays.put(displayId, activityDisplay);
3763 mWindowManager.onDisplayAdded(displayId);
3767 private void handleDisplayRemoved(int displayId) {
3768 synchronized (mService) {
3769 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3770 if (activityDisplay != null) {
3771 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3772 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3773 stacks.get(stackNdx).mActivityContainer.detachLocked();
3775 mActivityDisplays.remove(displayId);
3778 mWindowManager.onDisplayRemoved(displayId);
3781 private void handleDisplayChanged(int displayId) {
3782 synchronized (mService) {
3783 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3784 if (activityDisplay != null) {
3785 // TODO: Update the bounds.
3788 mWindowManager.onDisplayChanged(displayId);
3791 private StackInfo getStackInfoLocked(ActivityStack stack) {
3792 StackInfo info = new StackInfo();
3793 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3794 info.displayId = Display.DEFAULT_DISPLAY;
3795 info.stackId = stack.mStackId;
3797 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3798 final int numTasks = tasks.size();
3799 int[] taskIds = new int[numTasks];
3800 String[] taskNames = new String[numTasks];
3801 for (int i = 0; i < numTasks; ++i) {
3802 final TaskRecord task = tasks.get(i);
3803 taskIds[i] = task.taskId;
3804 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3805 : task.realActivity != null ? task.realActivity.flattenToString()
3806 : task.getTopActivity() != null ? task.getTopActivity().packageName
3809 info.taskIds = taskIds;
3810 info.taskNames = taskNames;
3814 StackInfo getStackInfoLocked(int stackId) {
3815 ActivityStack stack = getStack(stackId);
3816 if (stack != null) {
3817 return getStackInfoLocked(stack);
3822 ArrayList<StackInfo> getAllStackInfosLocked() {
3823 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
3824 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3825 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3826 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3827 list.add(getStackInfoLocked(stacks.get(ndx)));
3833 TaskRecord getLockedTaskLocked() {
3834 final int top = mLockTaskModeTasks.size() - 1;
3836 return mLockTaskModeTasks.get(top);
3841 boolean isLockedTask(TaskRecord task) {
3842 return mLockTaskModeTasks.contains(task);
3845 boolean isLastLockedTask(TaskRecord task) {
3846 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3849 void removeLockedTaskLocked(final TaskRecord task) {
3850 if (!mLockTaskModeTasks.remove(task)) {
3853 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3854 if (mLockTaskModeTasks.isEmpty()) {
3856 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3857 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
3858 final Message lockTaskMsg = Message.obtain();
3859 lockTaskMsg.arg1 = task.userId;
3860 lockTaskMsg.what = LOCK_TASK_END_MSG;
3861 mHandler.sendMessage(lockTaskMsg);
3865 void showLockTaskToast() {
3866 if (mLockTaskNotify != null) {
3867 mLockTaskNotify.showToast(mLockTaskModeState);
3871 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3872 if (mLockTaskModeTasks.contains(task)) {
3873 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3877 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3878 boolean andResume) {
3880 // Take out of lock task mode if necessary
3881 final TaskRecord lockedTask = getLockedTaskLocked();
3882 if (lockedTask != null) {
3883 removeLockedTaskLocked(lockedTask);
3884 if (!mLockTaskModeTasks.isEmpty()) {
3885 // There are locked tasks remaining, can only finish this task, not unlock it.
3886 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3887 "setLockTaskModeLocked: Tasks remaining, can't unlock");
3888 lockedTask.performClearTaskLocked();
3889 resumeTopActivitiesLocked();
3893 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3894 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
3898 // Should have already been checked, but do it again.
3899 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
3900 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3901 "setLockTaskModeLocked: Can't lock due to auth");
3904 if (isLockTaskModeViolation(task)) {
3905 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
3909 if (mLockTaskModeTasks.isEmpty()) {
3911 final Message lockTaskMsg = Message.obtain();
3912 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3913 lockTaskMsg.arg1 = task.userId;
3914 lockTaskMsg.what = LOCK_TASK_START_MSG;
3915 lockTaskMsg.arg2 = lockTaskModeState;
3916 mHandler.sendMessage(lockTaskMsg);
3918 // Add it or move it to the top.
3919 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3920 " Callers=" + Debug.getCallers(4));
3921 mLockTaskModeTasks.remove(task);
3922 mLockTaskModeTasks.add(task);
3924 if (task.mLockTaskUid == -1) {
3925 task.mLockTaskUid = task.mCallingUid;
3929 findTaskToMoveToFrontLocked(task, 0, null, reason);
3930 resumeTopActivitiesLocked();
3934 boolean isLockTaskModeViolation(TaskRecord task) {
3935 return isLockTaskModeViolation(task, false);
3938 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3939 if (getLockedTaskLocked() == task && !isNewClearTask) {
3942 final int lockTaskAuth = task.mLockTaskAuth;
3943 switch (lockTaskAuth) {
3944 case LOCK_TASK_AUTH_DONT_LOCK:
3945 return !mLockTaskModeTasks.isEmpty();
3946 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
3947 case LOCK_TASK_AUTH_LAUNCHABLE:
3948 case LOCK_TASK_AUTH_WHITELISTED:
3950 case LOCK_TASK_AUTH_PINNABLE:
3951 // Pinnable tasks can't be launched on top of locktask tasks.
3952 return !mLockTaskModeTasks.isEmpty();
3954 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3959 void onLockTaskPackagesUpdatedLocked() {
3960 boolean didSomething = false;
3961 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3962 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
3963 final boolean wasWhitelisted =
3964 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3965 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3966 lockedTask.setLockTaskAuth();
3967 final boolean isWhitelisted =
3968 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3969 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3970 if (wasWhitelisted && !isWhitelisted) {
3971 // Lost whitelisting authorization. End it now.
3972 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3973 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
3974 removeLockedTaskLocked(lockedTask);
3975 lockedTask.performClearTaskLocked();
3976 didSomething = true;
3979 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3980 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3981 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3982 final ActivityStack stack = stacks.get(stackNdx);
3983 stack.onLockTaskPackagesUpdatedLocked();
3986 final ActivityRecord r = topRunningActivityLocked();
3987 final TaskRecord task = r != null ? r.task : null;
3988 if (mLockTaskModeTasks.isEmpty() && task != null
3989 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3990 // This task must have just been authorized.
3991 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3992 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3993 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3995 didSomething = true;
3998 resumeTopActivitiesLocked();
4002 int getLockTaskModeState() {
4003 return mLockTaskModeState;
4006 private final class ActivityStackSupervisorHandler extends Handler {
4008 public ActivityStackSupervisorHandler(Looper looper) {
4012 void activityIdleInternal(ActivityRecord r) {
4013 synchronized (mService) {
4014 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4019 public void handleMessage(Message msg) {
4021 case IDLE_TIMEOUT_MSG: {
4022 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4023 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
4024 if (mService.mDidDexOpt) {
4025 mService.mDidDexOpt = false;
4026 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4028 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4031 // We don't at this point know if the activity is fullscreen,
4032 // so we need to be conservative and assume it isn't.
4033 activityIdleInternal((ActivityRecord)msg.obj);
4035 case IDLE_NOW_MSG: {
4036 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
4037 activityIdleInternal((ActivityRecord)msg.obj);
4039 case RESUME_TOP_ACTIVITY_MSG: {
4040 synchronized (mService) {
4041 resumeTopActivitiesLocked();
4044 case SLEEP_TIMEOUT_MSG: {
4045 synchronized (mService) {
4046 if (mService.isSleepingOrShuttingDown()) {
4047 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4048 mSleepTimeout = true;
4049 checkReadyForSleepLocked();
4053 case LAUNCH_TIMEOUT_MSG: {
4054 if (mService.mDidDexOpt) {
4055 mService.mDidDexOpt = false;
4056 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4059 synchronized (mService) {
4060 if (mLaunchingActivity.isHeld()) {
4061 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4062 if (VALIDATE_WAKE_LOCK_CALLER
4063 && Binder.getCallingUid() != Process.myUid()) {
4064 throw new IllegalStateException("Calling must be system uid");
4066 mLaunchingActivity.release();
4070 case HANDLE_DISPLAY_ADDED: {
4071 handleDisplayAdded(msg.arg1);
4073 case HANDLE_DISPLAY_CHANGED: {
4074 handleDisplayChanged(msg.arg1);
4076 case HANDLE_DISPLAY_REMOVED: {
4077 handleDisplayRemoved(msg.arg1);
4079 case CONTAINER_CALLBACK_VISIBILITY: {
4080 final ActivityContainer container = (ActivityContainer) msg.obj;
4081 final IActivityContainerCallback callback = container.mCallback;
4082 if (callback != null) {
4084 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4085 } catch (RemoteException e) {
4089 case LOCK_TASK_START_MSG: {
4090 // When lock task starts, we disable the status bars.
4092 if (mLockTaskNotify == null) {
4093 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4095 mLockTaskNotify.show(true);
4096 mLockTaskModeState = msg.arg2;
4097 if (getStatusBarService() != null) {
4099 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
4100 flags = StatusBarManager.DISABLE_MASK
4101 & (~StatusBarManager.DISABLE_BACK);
4102 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
4103 flags = StatusBarManager.DISABLE_MASK
4104 & (~StatusBarManager.DISABLE_BACK)
4105 & (~StatusBarManager.DISABLE_HOME)
4106 & (~StatusBarManager.DISABLE_RECENT);
4108 getStatusBarService().disable(flags, mToken,
4109 mService.mContext.getPackageName());
4111 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
4112 if (getDevicePolicyManager() != null) {
4113 getDevicePolicyManager().notifyLockTaskModeChanged(true,
4114 (String)msg.obj, msg.arg1);
4116 } catch (RemoteException ex) {
4117 throw new RuntimeException(ex);
4120 case LOCK_TASK_END_MSG: {
4121 // When lock task ends, we enable the status bars.
4123 if (getStatusBarService() != null) {
4124 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4125 mService.mContext.getPackageName());
4127 mWindowManager.reenableKeyguard(mToken);
4128 if (getDevicePolicyManager() != null) {
4129 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4132 if (mLockTaskNotify == null) {
4133 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4135 mLockTaskNotify.show(false);
4137 boolean shouldLockKeyguard = Settings.Secure.getInt(
4138 mService.mContext.getContentResolver(),
4139 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
4140 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
4141 mWindowManager.lockNow(null);
4142 mWindowManager.dismissKeyguard();
4143 new LockPatternUtils(mService.mContext)
4144 .requireCredentialEntry(UserHandle.USER_ALL);
4146 } catch (SettingNotFoundException e) {
4147 // No setting, don't lock.
4149 } catch (RemoteException ex) {
4150 throw new RuntimeException(ex);
4152 mLockTaskModeState = LOCK_TASK_MODE_NONE;
4155 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4156 if (mLockTaskNotify == null) {
4157 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4159 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4161 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4162 final ActivityContainer container = (ActivityContainer) msg.obj;
4163 final IActivityContainerCallback callback = container.mCallback;
4164 if (callback != null) {
4166 callback.onAllActivitiesComplete(container.asBinder());
4167 } catch (RemoteException e) {
4171 case LAUNCH_TASK_BEHIND_COMPLETE: {
4172 synchronized (mService) {
4173 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
4175 handleLaunchTaskBehindCompleteLocked(r);
4183 class ActivityContainer extends android.app.IActivityContainer.Stub {
4184 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
4185 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
4187 IActivityContainerCallback mCallback = null;
4188 final ActivityStack mStack;
4189 ActivityRecord mParentActivity = null;
4192 boolean mVisible = true;
4194 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
4195 ActivityDisplay mActivityDisplay;
4197 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4198 final static int CONTAINER_STATE_NO_SURFACE = 1;
4199 final static int CONTAINER_STATE_FINISHING = 2;
4200 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4202 ActivityContainer(int stackId) {
4203 synchronized (mService) {
4205 mStack = new ActivityStack(this, mRecentTasks);
4206 mIdString = "ActivtyContainer{" + mStackId + "}";
4207 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
4211 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
4212 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
4213 + " to display=" + activityDisplay);
4214 mActivityDisplay = activityDisplay;
4215 mStack.mDisplayId = activityDisplay.mDisplayId;
4216 mStack.mStacks = activityDisplay.mStacks;
4218 activityDisplay.attachActivities(mStack);
4219 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
4223 public void attachToDisplay(int displayId) {
4224 synchronized (mService) {
4225 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4226 if (activityDisplay == null) {
4229 attachToDisplayLocked(activityDisplay);
4234 public int getDisplayId() {
4235 synchronized (mService) {
4236 if (mActivityDisplay != null) {
4237 return mActivityDisplay.mDisplayId;
4244 public int getStackId() {
4245 synchronized (mService) {
4251 public boolean injectEvent(InputEvent event) {
4252 final long origId = Binder.clearCallingIdentity();
4254 synchronized (mService) {
4255 if (mActivityDisplay != null) {
4256 return mInputManagerInternal.injectInputEvent(event,
4257 mActivityDisplay.mDisplayId,
4258 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4263 Binder.restoreCallingIdentity(origId);
4268 public void release() {
4269 synchronized (mService) {
4270 if (mContainerState == CONTAINER_STATE_FINISHING) {
4273 mContainerState = CONTAINER_STATE_FINISHING;
4275 long origId = Binder.clearCallingIdentity();
4277 mStack.finishAllActivitiesLocked(false);
4278 removePendingActivityLaunchesLocked(mStack);
4280 Binder.restoreCallingIdentity(origId);
4285 protected void detachLocked() {
4286 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
4287 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
4288 if (mActivityDisplay != null) {
4289 mActivityDisplay.detachActivitiesLocked(mStack);
4290 mActivityDisplay = null;
4291 mStack.mDisplayId = -1;
4292 mStack.mStacks = null;
4293 mWindowManager.detachStack(mStackId);
4298 public final int startActivity(Intent intent) {
4299 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
4300 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
4301 Binder.getCallingUid(), mCurrentUser, false,
4302 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
4304 // TODO: Switch to user app stacks here.
4305 String mimeType = intent.getType();
4306 final Uri data = intent.getData();
4307 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4308 mimeType = mService.getProviderMimeType(data, userId);
4310 checkEmbeddedAllowedInner(userId, intent, mimeType);
4312 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4313 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
4314 0, 0, null, null, null, null, false, userId, this, null);
4318 public final int startActivityIntentSender(IIntentSender intentSender)
4319 throws TransactionTooLargeException {
4320 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4322 if (!(intentSender instanceof PendingIntentRecord)) {
4323 throw new IllegalArgumentException("Bad PendingIntent object");
4326 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
4327 Binder.getCallingUid(), mCurrentUser, false,
4328 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
4330 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4331 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4332 pendingIntent.key.requestResolvedType);
4334 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4335 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4338 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
4339 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
4340 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
4341 throw new SecurityException(
4342 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4347 public IBinder asBinder() {
4352 public void setSurface(Surface surface, int width, int height, int density) {
4353 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
4356 ActivityStackSupervisor getOuter() {
4357 return ActivityStackSupervisor.this;
4360 boolean isAttachedLocked() {
4361 return mActivityDisplay != null;
4364 void getBounds(Point outBounds) {
4365 synchronized (mService) {
4366 if (mActivityDisplay != null) {
4367 mActivityDisplay.getBounds(outBounds);
4369 outBounds.set(0, 0);
4374 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
4375 void setVisible(boolean visible) {
4376 if (mVisible != visible) {
4378 if (mCallback != null) {
4379 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4380 0 /* unused */, this).sendToTarget();
4388 // You can always start a new task on a regular ActivityStack.
4389 boolean isEligibleForNewTasks() {
4393 void onTaskListEmptyLocked() {
4395 deleteActivityContainer(this);
4396 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
4400 public String toString() {
4401 return mIdString + (mActivityDisplay == null ? "N" : "A");
4405 private class VirtualActivityContainer extends ActivityContainer {
4407 boolean mDrawn = false;
4409 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4410 super(getNextStackId());
4411 mParentActivity = parent;
4412 mCallback = callback;
4413 mContainerState = CONTAINER_STATE_NO_SURFACE;
4414 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
4418 public void setSurface(Surface surface, int width, int height, int density) {
4419 super.setSurface(surface, width, height, density);
4421 synchronized (mService) {
4422 final long origId = Binder.clearCallingIdentity();
4424 setSurfaceLocked(surface, width, height, density);
4426 Binder.restoreCallingIdentity(origId);
4431 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4432 if (mContainerState == CONTAINER_STATE_FINISHING) {
4435 VirtualActivityDisplay virtualActivityDisplay =
4436 (VirtualActivityDisplay) mActivityDisplay;
4437 if (virtualActivityDisplay == null) {
4438 virtualActivityDisplay =
4439 new VirtualActivityDisplay(width, height, density);
4440 mActivityDisplay = virtualActivityDisplay;
4441 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4442 attachToDisplayLocked(virtualActivityDisplay);
4445 if (mSurface != null) {
4450 if (surface != null) {
4451 mStack.resumeTopActivityLocked(null);
4453 mContainerState = CONTAINER_STATE_NO_SURFACE;
4454 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
4455 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
4456 mStack.startPausingLocked(false, true, false, false);
4460 setSurfaceIfReadyLocked();
4462 if (DEBUG_STACK) Slog.d(TAG_STACK,
4463 "setSurface: " + this + " to display=" + virtualActivityDisplay);
4467 boolean isAttachedLocked() {
4468 return mSurface != null && super.isAttachedLocked();
4473 synchronized (mService) {
4475 setSurfaceIfReadyLocked();
4479 // Never start a new task on an ActivityView if it isn't explicitly specified.
4481 boolean isEligibleForNewTasks() {
4485 private void setSurfaceIfReadyLocked() {
4486 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
4487 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4488 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4489 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4490 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4495 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4496 * attached {@link ActivityStack}s */
4497 class ActivityDisplay {
4498 /** Actual Display this object tracks. */
4501 DisplayInfo mDisplayInfo = new DisplayInfo();
4503 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4504 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
4505 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
4507 ActivityRecord mVisibleBehindActivity;
4512 // After instantiation, check that mDisplay is not null before using this. The alternative
4513 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
4514 ActivityDisplay(int displayId) {
4515 final Display display = mDisplayManager.getDisplay(displayId);
4516 if (display == null) {
4522 void init(Display display) {
4524 mDisplayId = display.getDisplayId();
4525 mDisplay.getDisplayInfo(mDisplayInfo);
4528 void attachActivities(ActivityStack stack) {
4529 if (DEBUG_STACK) Slog.v(TAG_STACK,
4530 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId);
4534 void detachActivitiesLocked(ActivityStack stack) {
4535 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
4536 + " from displayId=" + mDisplayId);
4537 mStacks.remove(stack);
4540 void getBounds(Point bounds) {
4541 mDisplay.getDisplayInfo(mDisplayInfo);
4542 bounds.x = mDisplayInfo.appWidth;
4543 bounds.y = mDisplayInfo.appHeight;
4546 void setVisibleBehindActivity(ActivityRecord r) {
4547 mVisibleBehindActivity = r;
4550 boolean hasVisibleBehindActivity() {
4551 return mVisibleBehindActivity != null;
4555 public String toString() {
4556 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4560 class VirtualActivityDisplay extends ActivityDisplay {
4561 VirtualDisplay mVirtualDisplay;
4563 VirtualActivityDisplay(int width, int height, int density) {
4564 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
4565 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4566 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4567 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4568 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
4570 init(mVirtualDisplay.getDisplay());
4572 mWindowManager.handleDisplayAdded(mDisplayId);
4575 void setSurface(Surface surface) {
4576 if (mVirtualDisplay != null) {
4577 mVirtualDisplay.setSurface(surface);
4582 void detachActivitiesLocked(ActivityStack stack) {
4583 super.detachActivitiesLocked(stack);
4584 if (mVirtualDisplay != null) {
4585 mVirtualDisplay.release();
4586 mVirtualDisplay = null;
4591 public String toString() {
4592 return "VirtualActivityDisplay={" + mDisplayId + "}";
4596 private boolean isLeanbackOnlyDevice() {
4597 boolean onLeanbackOnly = false;
4599 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4600 PackageManager.FEATURE_LEANBACK_ONLY);
4601 } catch (RemoteException e) {
4605 return onLeanbackOnly;