ActivityStack.java revision b8bd069eacc2a888a1ff6e923094e4178ad531a3
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
19import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.HOME_STACK_ID;
23import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
24import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
25import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
26import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
27import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
28import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
29import static android.content.pm.ActivityInfo.FLAG_RESUME_WHILE_PAUSING;
30import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
31import static android.content.res.Configuration.SCREENLAYOUT_UNDEFINED;
32import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ADD_REMOVE;
33import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
34import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_APP;
35import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CLEANUP;
36import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONFIGURATION;
37import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
38import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
39import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
40import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
41import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RESULTS;
42import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SAVED_STATE;
43import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SCREENSHOTS;
44import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
45import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TRANSITION;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_USER_LEAVING;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBILITY;
51import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_ADD_REMOVE;
52import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_APP;
53import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CLEANUP;
54import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONFIGURATION;
55import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
56import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
57import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RESULTS;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SAVED_STATE;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SCREENSHOTS;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TRANSITION;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_USER_LEAVING;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBILITY;
68import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
69import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
70import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
71import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
72import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
73import static com.android.server.am.ActivityRecord.STARTING_WINDOW_REMOVED;
74import static com.android.server.am.ActivityRecord.STARTING_WINDOW_SHOWN;
75import static com.android.server.am.ActivityStackSupervisor.FindTaskResult;
76import static com.android.server.am.ActivityStackSupervisor.MOVING;
77import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
78import static com.android.server.wm.AppTransition.TRANSIT_ACTIVITY_CLOSE;
79import static com.android.server.wm.AppTransition.TRANSIT_ACTIVITY_OPEN;
80import static com.android.server.wm.AppTransition.TRANSIT_NONE;
81import static com.android.server.wm.AppTransition.TRANSIT_TASK_CLOSE;
82import static com.android.server.wm.AppTransition.TRANSIT_TASK_OPEN;
83import static com.android.server.wm.AppTransition.TRANSIT_TASK_OPEN_BEHIND;
84import static com.android.server.wm.AppTransition.TRANSIT_TASK_TO_BACK;
85import static com.android.server.wm.AppTransition.TRANSIT_TASK_TO_FRONT;
86
87import android.app.Activity;
88import android.app.ActivityManager;
89import android.app.ActivityManager.RunningTaskInfo;
90import android.app.ActivityManager.StackId;
91import android.app.ActivityOptions;
92import android.app.AppGlobals;
93import android.app.IActivityController;
94import android.app.ResultInfo;
95import android.content.ComponentName;
96import android.content.Intent;
97import android.content.pm.ActivityInfo;
98import android.content.pm.ApplicationInfo;
99import android.content.res.Configuration;
100import android.graphics.Bitmap;
101import android.graphics.Point;
102import android.graphics.Rect;
103import android.net.Uri;
104import android.os.Binder;
105import android.os.Bundle;
106import android.os.Debug;
107import android.os.Handler;
108import android.os.IBinder;
109import android.os.Looper;
110import android.os.Message;
111import android.os.PersistableBundle;
112import android.os.RemoteException;
113import android.os.SystemClock;
114import android.os.Trace;
115import android.os.UserHandle;
116import android.service.voice.IVoiceInteractionSession;
117import android.util.ArraySet;
118import android.util.EventLog;
119import android.util.Log;
120import android.util.Slog;
121import android.view.Display;
122
123import com.android.internal.app.IVoiceInteractor;
124import com.android.internal.content.ReferrerIntent;
125import com.android.internal.os.BatteryStatsImpl;
126import com.android.server.Watchdog;
127import com.android.server.am.ActivityManagerService.ItemMatcher;
128import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
129import com.android.server.wm.TaskGroup;
130import com.android.server.wm.WindowManagerService;
131
132import java.io.FileDescriptor;
133import java.io.PrintWriter;
134import java.lang.ref.WeakReference;
135import java.util.ArrayList;
136import java.util.Iterator;
137import java.util.List;
138import java.util.Objects;
139import java.util.Set;
140
141/**
142 * State and management of a single stack of activities.
143 */
144final class ActivityStack {
145
146    private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
147    private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
148    private static final String TAG_APP = TAG + POSTFIX_APP;
149    private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
150    private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
151    private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
152    private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
153    private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
154    private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
155    private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
156    private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
157    private static final String TAG_STACK = TAG + POSTFIX_STACK;
158    private static final String TAG_STATES = TAG + POSTFIX_STATES;
159    private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
160    private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
161    private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
162    private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
163    private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
164
165    private static final boolean VALIDATE_TOKENS = false;
166
167    // Ticks during which we check progress while waiting for an app to launch.
168    static final int LAUNCH_TICK = 500;
169
170    // How long we wait until giving up on the last activity to pause.  This
171    // is short because it directly impacts the responsiveness of starting the
172    // next activity.
173    static final int PAUSE_TIMEOUT = 500;
174
175    // How long we wait for the activity to tell us it has stopped before
176    // giving up.  This is a good amount of time because we really need this
177    // from the application in order to get its saved state.
178    static final int STOP_TIMEOUT = 10 * 1000;
179
180    // How long we wait until giving up on an activity telling us it has
181    // finished destroying itself.
182    static final int DESTROY_TIMEOUT = 10 * 1000;
183
184    // How long until we reset a task when the user returns to it.  Currently
185    // disabled.
186    static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
187
188    // How long between activity launches that we consider safe to not warn
189    // the user about an unexpected activity being launched on top.
190    static final long START_WARN_TIME = 5 * 1000;
191
192    // Set to false to disable the preview that is shown while a new activity
193    // is being started.
194    static final boolean SHOW_APP_STARTING_PREVIEW = true;
195
196    // How long to wait for all background Activities to redraw following a call to
197    // convertToTranslucent().
198    static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
199
200    // How many activities have to be scheduled to stop to force a stop pass.
201    private static final int MAX_STOPPING_TO_FORCE = 3;
202
203    enum ActivityState {
204        INITIALIZING,
205        RESUMED,
206        PAUSING,
207        PAUSED,
208        STOPPING,
209        STOPPED,
210        FINISHING,
211        DESTROYING,
212        DESTROYED
213    }
214
215    // Stack is not considered visible.
216    static final int STACK_INVISIBLE = 0;
217    // Stack is considered visible
218    static final int STACK_VISIBLE = 1;
219    // Stack is considered visible, but only becuase it has activity that is visible behind other
220    // activities and there is a specific combination of stacks.
221    static final int STACK_VISIBLE_ACTIVITY_BEHIND = 2;
222
223    final ActivityManagerService mService;
224    final WindowManagerService mWindowManager;
225    private final RecentTasks mRecentTasks;
226
227    /**
228     * The back history of all previous (and possibly still
229     * running) activities.  It contains #TaskRecord objects.
230     */
231    private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
232
233    /**
234     * Used for validating app tokens with window manager.
235     */
236    final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
237
238    /**
239     * List of running activities, sorted by recent usage.
240     * The first entry in the list is the least recently used.
241     * It contains HistoryRecord objects.
242     */
243    final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
244
245    /**
246     * Animations that for the current transition have requested not to
247     * be considered for the transition animation.
248     */
249    final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
250
251    /**
252     * When we are in the process of pausing an activity, before starting the
253     * next one, this variable holds the activity that is currently being paused.
254     */
255    ActivityRecord mPausingActivity = null;
256
257    /**
258     * This is the last activity that we put into the paused state.  This is
259     * used to determine if we need to do an activity transition while sleeping,
260     * when we normally hold the top activity paused.
261     */
262    ActivityRecord mLastPausedActivity = null;
263
264    /**
265     * Activities that specify No History must be removed once the user navigates away from them.
266     * If the device goes to sleep with such an activity in the paused state then we save it here
267     * and finish it later if another activity replaces it on wakeup.
268     */
269    ActivityRecord mLastNoHistoryActivity = null;
270
271    /**
272     * Current activity that is resumed, or null if there is none.
273     */
274    ActivityRecord mResumedActivity = null;
275
276    /**
277     * This is the last activity that has been started.  It is only used to
278     * identify when multiple activities are started at once so that the user
279     * can be warned they may not be in the activity they think they are.
280     */
281    ActivityRecord mLastStartedActivity = null;
282
283    // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
284    // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
285    // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
286    // Activity in mTranslucentActivityWaiting is notified via
287    // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
288    // background activity being drawn then the same call will be made with a true value.
289    ActivityRecord mTranslucentActivityWaiting = null;
290    private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
291
292    /**
293     * Set when we know we are going to be calling updateConfiguration()
294     * soon, so want to skip intermediate config checks.
295     */
296    boolean mConfigWillChange;
297
298    // Whether or not this stack covers the entire screen; by default stacks are fullscreen
299    boolean mFullscreen = true;
300    // Current bounds of the stack or null if fullscreen.
301    Rect mBounds = null;
302
303    boolean mUpdateBoundsDeferred;
304    boolean mUpdateBoundsDeferredCalled;
305    final Rect mDeferredBounds = new Rect();
306    final Rect mDeferredTaskBounds = new Rect();
307    final Rect mDeferredTaskInsetBounds = new Rect();
308
309    long mLaunchStartTime = 0;
310    long mFullyDrawnStartTime = 0;
311
312    int mCurrentUser;
313
314    final int mStackId;
315    final ActivityContainer mActivityContainer;
316    /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
317    ArrayList<ActivityStack> mStacks;
318    /** The attached Display's unique identifier, or -1 if detached */
319    int mDisplayId;
320
321    /** Run all ActivityStacks through this */
322    final ActivityStackSupervisor mStackSupervisor;
323
324    private final LaunchingTaskPositioner mTaskPositioner;
325
326    static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
327    static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
328    static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
329    static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
330    static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
331    static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
332    static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
333            ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
334
335    static class ScheduleDestroyArgs {
336        final ProcessRecord mOwner;
337        final String mReason;
338        ScheduleDestroyArgs(ProcessRecord owner, String reason) {
339            mOwner = owner;
340            mReason = reason;
341        }
342    }
343
344    final Handler mHandler;
345
346    final class ActivityStackHandler extends Handler {
347
348        ActivityStackHandler(Looper looper) {
349            super(looper);
350        }
351
352        @Override
353        public void handleMessage(Message msg) {
354            switch (msg.what) {
355                case PAUSE_TIMEOUT_MSG: {
356                    ActivityRecord r = (ActivityRecord)msg.obj;
357                    // We don't at this point know if the activity is fullscreen,
358                    // so we need to be conservative and assume it isn't.
359                    Slog.w(TAG, "Activity pause timeout for " + r);
360                    synchronized (mService) {
361                        if (r.app != null) {
362                            mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
363                        }
364                        activityPausedLocked(r.appToken, true);
365                    }
366                } break;
367                case LAUNCH_TICK_MSG: {
368                    ActivityRecord r = (ActivityRecord)msg.obj;
369                    synchronized (mService) {
370                        if (r.continueLaunchTickingLocked()) {
371                            mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
372                        }
373                    }
374                } break;
375                case DESTROY_TIMEOUT_MSG: {
376                    ActivityRecord r = (ActivityRecord)msg.obj;
377                    // We don't at this point know if the activity is fullscreen,
378                    // so we need to be conservative and assume it isn't.
379                    Slog.w(TAG, "Activity destroy timeout for " + r);
380                    synchronized (mService) {
381                        activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
382                    }
383                } break;
384                case STOP_TIMEOUT_MSG: {
385                    ActivityRecord r = (ActivityRecord)msg.obj;
386                    // We don't at this point know if the activity is fullscreen,
387                    // so we need to be conservative and assume it isn't.
388                    Slog.w(TAG, "Activity stop timeout for " + r);
389                    synchronized (mService) {
390                        if (r.isInHistory()) {
391                            activityStoppedLocked(r, null, null, null);
392                        }
393                    }
394                } break;
395                case DESTROY_ACTIVITIES_MSG: {
396                    ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
397                    synchronized (mService) {
398                        destroyActivitiesLocked(args.mOwner, args.mReason);
399                    }
400                } break;
401                case TRANSLUCENT_TIMEOUT_MSG: {
402                    synchronized (mService) {
403                        notifyActivityDrawnLocked(null);
404                    }
405                } break;
406                case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
407                    synchronized (mService) {
408                        final ActivityRecord r = getVisibleBehindActivity();
409                        Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
410                        if (r != null) {
411                            mService.killAppAtUsersRequest(r.app, null);
412                        }
413                    }
414                } break;
415            }
416        }
417    }
418
419    int numActivities() {
420        int count = 0;
421        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
422            count += mTaskHistory.get(taskNdx).mActivities.size();
423        }
424        return count;
425    }
426
427    ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
428            RecentTasks recentTasks) {
429        mActivityContainer = activityContainer;
430        mStackSupervisor = activityContainer.getOuter();
431        mService = mStackSupervisor.mService;
432        mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
433        mWindowManager = mService.mWindowManager;
434        mStackId = activityContainer.mStackId;
435        mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
436        mRecentTasks = recentTasks;
437        mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
438                ? new LaunchingTaskPositioner() : null;
439    }
440
441    void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
442        mDisplayId = activityDisplay.mDisplayId;
443        mStacks = activityDisplay.mStacks;
444        mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
445        mFullscreen = mBounds == null;
446        if (mTaskPositioner != null) {
447            mTaskPositioner.setDisplay(activityDisplay.mDisplay);
448            mTaskPositioner.configure(mBounds);
449        }
450
451        if (mStackId == DOCKED_STACK_ID) {
452            // If we created a docked stack we want to resize it so it resizes all other stacks
453            // in the system.
454            mStackSupervisor.resizeDockedStackLocked(
455                    mBounds, null, null, null, null, PRESERVE_WINDOWS);
456        }
457    }
458
459    void detachDisplay() {
460        mDisplayId = Display.INVALID_DISPLAY;
461        mStacks = null;
462        if (mTaskPositioner != null) {
463            mTaskPositioner.reset();
464        }
465        mWindowManager.detachStack(mStackId);
466        if (mStackId == DOCKED_STACK_ID) {
467            // If we removed a docked stack we want to resize it so it resizes all other stacks
468            // in the system to fullscreen.
469            mStackSupervisor.resizeDockedStackLocked(
470                    null, null, null, null, null, PRESERVE_WINDOWS);
471        }
472    }
473
474    public void getDisplaySize(Point out) {
475        mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
476    }
477
478    /**
479     * Defers updating the bounds of the stack. If the stack was resized/repositioned while
480     * deferring, the bounds will update in {@link #continueUpdateBounds()}.
481     */
482    void deferUpdateBounds() {
483        if (!mUpdateBoundsDeferred) {
484            mUpdateBoundsDeferred = true;
485            mUpdateBoundsDeferredCalled = false;
486        }
487    }
488
489    /**
490     * Continues updating bounds after updates have been deferred. If there was a resize attempt
491     * between {@link #deferUpdateBounds()} and {@link #continueUpdateBounds()}, the stack will
492     * be resized to that bounds.
493     */
494    void continueUpdateBounds() {
495        final boolean wasDeferred = mUpdateBoundsDeferred;
496        mUpdateBoundsDeferred = false;
497        if (wasDeferred && mUpdateBoundsDeferredCalled) {
498            mStackSupervisor.resizeStackUncheckedLocked(this,
499                    mDeferredBounds.isEmpty() ? null : mDeferredBounds,
500                    mDeferredTaskBounds.isEmpty() ? null : mDeferredTaskBounds,
501                    mDeferredTaskInsetBounds.isEmpty() ? null : mDeferredTaskInsetBounds);
502        }
503    }
504
505    boolean updateBoundsAllowed(Rect bounds, Rect tempTaskBounds,
506            Rect tempTaskInsetBounds) {
507        if (!mUpdateBoundsDeferred) {
508            return true;
509        }
510        if (bounds != null) {
511            mDeferredBounds.set(bounds);
512        } else {
513            mDeferredBounds.setEmpty();
514        }
515        if (tempTaskBounds != null) {
516            mDeferredTaskBounds.set(tempTaskBounds);
517        } else {
518            mDeferredTaskBounds.setEmpty();
519        }
520        if (tempTaskInsetBounds != null) {
521            mDeferredTaskInsetBounds.set(tempTaskInsetBounds);
522        } else {
523            mDeferredTaskInsetBounds.setEmpty();
524        }
525        mUpdateBoundsDeferredCalled = true;
526        return false;
527    }
528
529    void setBounds(Rect bounds) {
530        mBounds = mFullscreen ? null : new Rect(bounds);
531        if (mTaskPositioner != null) {
532            mTaskPositioner.configure(bounds);
533        }
534    }
535
536    boolean okToShowLocked(ActivityRecord r) {
537        return mStackSupervisor.okToShowLocked(r);
538    }
539
540    final ActivityRecord topRunningActivityLocked() {
541        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
542            ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
543            if (r != null) {
544                return r;
545            }
546        }
547        return null;
548    }
549
550    final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
551        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
552            final TaskRecord task = mTaskHistory.get(taskNdx);
553            final ArrayList<ActivityRecord> activities = task.mActivities;
554            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
555                ActivityRecord r = activities.get(activityNdx);
556                if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
557                    return r;
558                }
559            }
560        }
561        return null;
562    }
563
564    /**
565     * This is a simplified version of topRunningActivityLocked that provides a number of
566     * optional skip-over modes.  It is intended for use with the ActivityController hook only.
567     *
568     * @param token If non-null, any history records matching this token will be skipped.
569     * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
570     *
571     * @return Returns the HistoryRecord of the next activity on the stack.
572     */
573    final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
574        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
575            TaskRecord task = mTaskHistory.get(taskNdx);
576            if (task.taskId == taskId) {
577                continue;
578            }
579            ArrayList<ActivityRecord> activities = task.mActivities;
580            for (int i = activities.size() - 1; i >= 0; --i) {
581                final ActivityRecord r = activities.get(i);
582                // Note: the taskId check depends on real taskId fields being non-zero
583                if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
584                    return r;
585                }
586            }
587        }
588        return null;
589    }
590
591    final ActivityRecord topActivity() {
592        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
593            ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
594            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
595                final ActivityRecord r = activities.get(activityNdx);
596                if (!r.finishing) {
597                    return r;
598                }
599            }
600        }
601        return null;
602    }
603
604    final TaskRecord topTask() {
605        final int size = mTaskHistory.size();
606        if (size > 0) {
607            return mTaskHistory.get(size - 1);
608        }
609        return null;
610    }
611
612    TaskRecord taskForIdLocked(int id) {
613        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
614            final TaskRecord task = mTaskHistory.get(taskNdx);
615            if (task.taskId == id) {
616                return task;
617            }
618        }
619        return null;
620    }
621
622    ActivityRecord isInStackLocked(IBinder token) {
623        final ActivityRecord r = ActivityRecord.forTokenLocked(token);
624        return isInStackLocked(r);
625    }
626
627    ActivityRecord isInStackLocked(ActivityRecord r) {
628        if (r == null) {
629            return null;
630        }
631        final TaskRecord task = r.task;
632        if (task != null && task.stack != null
633                && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
634            if (task.stack != this) Slog.w(TAG,
635                    "Illegal state! task does not point to stack it is in.");
636            return r;
637        }
638        return null;
639    }
640
641    final boolean updateLRUListLocked(ActivityRecord r) {
642        final boolean hadit = mLRUActivities.remove(r);
643        mLRUActivities.add(r);
644        return hadit;
645    }
646
647    final boolean isHomeStack() {
648        return mStackId == HOME_STACK_ID;
649    }
650
651    final boolean isDockedStack() {
652        return mStackId == DOCKED_STACK_ID;
653    }
654
655    final boolean isPinnedStack() {
656        return mStackId == PINNED_STACK_ID;
657    }
658
659    final boolean isOnHomeDisplay() {
660        return isAttached() &&
661                mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
662    }
663
664    void moveToFront(String reason) {
665        moveToFront(reason, null);
666    }
667
668    /**
669     * @param reason The reason for moving the stack to the front.
670     * @param task If non-null, the task will be moved to the top of the stack.
671     * */
672    void moveToFront(String reason, TaskRecord task) {
673        if (!isAttached()) {
674            return;
675        }
676
677        mStacks.remove(this);
678        int addIndex = mStacks.size();
679
680        if (addIndex > 0) {
681            final ActivityStack topStack = mStacks.get(addIndex - 1);
682            if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
683                // If the top stack is always on top, we move this stack just below it.
684                addIndex--;
685            }
686        }
687
688        mStacks.add(addIndex, this);
689
690        // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
691        if (isOnHomeDisplay()) {
692            mStackSupervisor.setFocusStackUnchecked(reason, this);
693        }
694        if (task != null) {
695            insertTaskAtTop(task, null);
696        } else {
697            task = topTask();
698        }
699        if (task != null) {
700            mWindowManager.moveTaskToTop(task.taskId);
701        }
702    }
703
704    boolean isFocusable() {
705        if (StackId.canReceiveKeys(mStackId)) {
706            return true;
707        }
708        // The stack isn't focusable. See if its top activity is focusable to force focus on the
709        // stack.
710        final ActivityRecord r = topRunningActivityLocked();
711        return r != null && r.isFocusable();
712    }
713
714    final boolean isAttached() {
715        return mStacks != null;
716    }
717
718    /**
719     * Returns the top activity in any existing task matching the given Intent in the input result.
720     * Returns null if no such task is found.
721     */
722    void findTaskLocked(ActivityRecord target, FindTaskResult result) {
723        Intent intent = target.intent;
724        ActivityInfo info = target.info;
725        ComponentName cls = intent.getComponent();
726        if (info.targetActivity != null) {
727            cls = new ComponentName(info.packageName, info.targetActivity);
728        }
729        final int userId = UserHandle.getUserId(info.applicationInfo.uid);
730        boolean isDocument = intent != null & intent.isDocument();
731        // If documentData is non-null then it must match the existing task data.
732        Uri documentData = isDocument ? intent.getData() : null;
733
734        if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
735        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
736            final TaskRecord task = mTaskHistory.get(taskNdx);
737            if (task.voiceSession != null) {
738                // We never match voice sessions; those always run independently.
739                if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
740                continue;
741            }
742            if (task.userId != userId) {
743                // Looking for a different task.
744                if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
745                continue;
746            }
747            final ActivityRecord r = task.getTopActivity();
748            if (r == null || r.finishing || r.userId != userId ||
749                    r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
750                if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
751                continue;
752            }
753            if (r.mActivityType != target.mActivityType) {
754                if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
755                continue;
756            }
757
758            final Intent taskIntent = task.intent;
759            final Intent affinityIntent = task.affinityIntent;
760            final boolean taskIsDocument;
761            final Uri taskDocumentData;
762            if (taskIntent != null && taskIntent.isDocument()) {
763                taskIsDocument = true;
764                taskDocumentData = taskIntent.getData();
765            } else if (affinityIntent != null && affinityIntent.isDocument()) {
766                taskIsDocument = true;
767                taskDocumentData = affinityIntent.getData();
768            } else {
769                taskIsDocument = false;
770                taskDocumentData = null;
771            }
772
773            if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
774                    + taskIntent.getComponent().flattenToShortString()
775                    + "/aff=" + r.task.rootAffinity + " to new cls="
776                    + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
777            if (!isDocument && !taskIsDocument
778                    && result.r == null && task.canMatchRootAffinity()) {
779                if (task.rootAffinity.equals(target.taskAffinity)) {
780                    if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
781                    // It is possible for multiple tasks to have the same root affinity especially
782                    // if they are in separate stacks. We save off this candidate, but keep looking
783                    // to see if there is a better candidate.
784                    result.r = r;
785                    result.matchedByRootAffinity = true;
786                }
787            } else if (taskIntent != null && taskIntent.getComponent() != null &&
788                    taskIntent.getComponent().compareTo(cls) == 0 &&
789                    Objects.equals(documentData, taskDocumentData)) {
790                if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
791                //dump();
792                if (DEBUG_TASKS) Slog.d(TAG_TASKS,
793                        "For Intent " + intent + " bringing to top: " + r.intent);
794                result.r = r;
795                result.matchedByRootAffinity = false;
796                break;
797            } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
798                    affinityIntent.getComponent().compareTo(cls) == 0 &&
799                    Objects.equals(documentData, taskDocumentData)) {
800                if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
801                //dump();
802                if (DEBUG_TASKS) Slog.d(TAG_TASKS,
803                        "For Intent " + intent + " bringing to top: " + r.intent);
804                result.r = r;
805                result.matchedByRootAffinity = false;
806                break;
807            } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
808        }
809    }
810
811    /**
812     * Returns the first activity (starting from the top of the stack) that
813     * is the same as the given activity.  Returns null if no such activity
814     * is found.
815     */
816    ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
817        ComponentName cls = intent.getComponent();
818        if (info.targetActivity != null) {
819            cls = new ComponentName(info.packageName, info.targetActivity);
820        }
821        final int userId = UserHandle.getUserId(info.applicationInfo.uid);
822
823        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
824            final TaskRecord task = mTaskHistory.get(taskNdx);
825            final boolean notCurrentUserTask =
826                    !mStackSupervisor.isCurrentProfileLocked(task.userId);
827            final ArrayList<ActivityRecord> activities = task.mActivities;
828
829            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
830                ActivityRecord r = activities.get(activityNdx);
831                if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
832                    continue;
833                }
834                if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
835                    return r;
836                }
837            }
838        }
839
840        return null;
841    }
842
843    /*
844     * Move the activities around in the stack to bring a user to the foreground.
845     */
846    final void switchUserLocked(int userId) {
847        if (mCurrentUser == userId) {
848            return;
849        }
850        mCurrentUser = userId;
851
852        // Move userId's tasks to the top.
853        int index = mTaskHistory.size();
854        for (int i = 0; i < index; ) {
855            final TaskRecord task = mTaskHistory.get(i);
856
857            // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
858            // okay to show the activity when locked.
859            if (mStackSupervisor.isCurrentProfileLocked(task.userId)
860                    || task.topRunningActivityLocked() != null) {
861                if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
862                        " moving " + task + " to top");
863                mTaskHistory.remove(i);
864                mTaskHistory.add(task);
865                --index;
866                // Use same value for i.
867            } else {
868                ++i;
869            }
870        }
871        if (VALIDATE_TOKENS) {
872            validateAppTokensLocked();
873        }
874    }
875
876    void minimalResumeActivityLocked(ActivityRecord r) {
877        r.state = ActivityState.RESUMED;
878        if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
879                + " callers=" + Debug.getCallers(5));
880        mResumedActivity = r;
881        r.task.touchActiveTime();
882        mRecentTasks.addLocked(r.task);
883        completeResumeLocked(r);
884        mStackSupervisor.checkReadyForSleepLocked();
885        setLaunchTime(r);
886        if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
887                "Launch completed; removing icicle of " + r.icicle);
888    }
889
890    void addRecentActivityLocked(ActivityRecord r) {
891        if (r != null) {
892            mRecentTasks.addLocked(r.task);
893            r.task.touchActiveTime();
894        }
895    }
896
897    private void startLaunchTraces(String packageName) {
898        if (mFullyDrawnStartTime != 0)  {
899            Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
900        }
901        Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
902        Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
903    }
904
905    private void stopFullyDrawnTraceIfNeeded() {
906        if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
907            Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
908            mFullyDrawnStartTime = 0;
909        }
910    }
911
912    void setLaunchTime(ActivityRecord r) {
913        if (r.displayStartTime == 0) {
914            r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
915            if (r.task != null) {
916                r.task.isLaunching = true;
917            }
918            if (mLaunchStartTime == 0) {
919                startLaunchTraces(r.packageName);
920                mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
921            }
922        } else if (mLaunchStartTime == 0) {
923            startLaunchTraces(r.packageName);
924            mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
925        }
926    }
927
928    void clearLaunchTime(ActivityRecord r) {
929        // Make sure that there is no activity waiting for this to launch.
930        if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
931            r.displayStartTime = r.fullyDrawnStartTime = 0;
932            if (r.task != null) {
933                r.task.isLaunching = false;
934            }
935        } else {
936            mStackSupervisor.removeTimeoutsForActivityLocked(r);
937            mStackSupervisor.scheduleIdleTimeoutLocked(r);
938        }
939    }
940
941    void awakeFromSleepingLocked() {
942        // Ensure activities are no longer sleeping.
943        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
944            final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
945            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
946                activities.get(activityNdx).setSleeping(false);
947            }
948        }
949        if (mPausingActivity != null) {
950            Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
951            activityPausedLocked(mPausingActivity.appToken, true);
952        }
953    }
954
955    void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
956        final String packageName = aInfo.packageName;
957        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
958            final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
959            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
960                if (packageName.equals(activities.get(activityNdx).packageName)) {
961                    activities.get(activityNdx).info.applicationInfo = aInfo;
962                }
963            }
964        }
965    }
966
967    /**
968     * @return true if something must be done before going to sleep.
969     */
970    boolean checkReadyForSleepLocked() {
971        if (mResumedActivity != null) {
972            // Still have something resumed; can't sleep until it is paused.
973            if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
974            if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
975                    "Sleep => pause with userLeaving=false");
976            startPausingLocked(false, true, false, false);
977            return true;
978        }
979        if (mPausingActivity != null) {
980            // Still waiting for something to pause; can't sleep yet.
981            if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
982            return true;
983        }
984
985        if (hasVisibleBehindActivity()) {
986            // Stop visible behind activity before going to sleep.
987            final ActivityRecord r = getVisibleBehindActivity();
988            mStackSupervisor.mStoppingActivities.add(r);
989            if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
990            return true;
991        }
992
993        return false;
994    }
995
996    void goToSleep() {
997        ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
998
999        // Make sure any stopped but visible activities are now sleeping.
1000        // This ensures that the activity's onStop() is called.
1001        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1002            final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1003            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1004                final ActivityRecord r = activities.get(activityNdx);
1005                if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
1006                    r.setSleeping(true);
1007                }
1008            }
1009        }
1010    }
1011
1012    public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
1013        if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
1014        if (who.noDisplay) {
1015            if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
1016            return null;
1017        }
1018
1019        if (isHomeStack()) {
1020            // This is an optimization -- since we never show Home or Recents within Recents itself,
1021            // we can just go ahead and skip taking the screenshot if this is the home stack.
1022            if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
1023            return null;
1024        }
1025
1026        int w = mService.mThumbnailWidth;
1027        int h = mService.mThumbnailHeight;
1028        float scale = 1f;
1029        if (w > 0) {
1030            if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
1031
1032            // When this flag is set, we currently take the fullscreen screenshot of the activity
1033            // but scaled to half the size.  This gives us a "good-enough" fullscreen thumbnail to
1034            // use within SystemUI while keeping memory usage low.
1035            if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
1036                w = h = -1;
1037                scale = mService.mFullscreenThumbnailScale;
1038            }
1039            return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
1040                    w, h, scale);
1041        }
1042        Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
1043        return null;
1044    }
1045
1046    /**
1047     * Start pausing the currently resumed activity.  It is an error to call this if there
1048     * is already an activity being paused or there is no resumed activity.
1049     *
1050     * @param userLeaving True if this should result in an onUserLeaving to the current activity.
1051     * @param uiSleeping True if this is happening with the user interface going to sleep (the
1052     * screen turning off).
1053     * @param resuming True if this is being called as part of resuming the top activity, so
1054     * we shouldn't try to instigate a resume here.
1055     * @param dontWait True if the caller does not want to wait for the pause to complete.  If
1056     * set to true, we will immediately complete the pause here before returning.
1057     * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
1058     * it to tell us when it is done.
1059     */
1060    final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
1061            boolean dontWait) {
1062        if (mPausingActivity != null) {
1063            Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
1064                    + " state=" + mPausingActivity.state);
1065            if (!mService.isSleeping()) {
1066                // Avoid recursion among check for sleep and complete pause during sleeping.
1067                // Because activity will be paused immediately after resume, just let pause
1068                // be completed by the order of activity paused from clients.
1069                completePauseLocked(false);
1070            }
1071        }
1072        ActivityRecord prev = mResumedActivity;
1073        if (prev == null) {
1074            if (!resuming) {
1075                Slog.wtf(TAG, "Trying to pause when nothing is resumed");
1076                mStackSupervisor.resumeFocusedStackTopActivityLocked();
1077            }
1078            return false;
1079        }
1080
1081        if (mActivityContainer.mParentActivity == null) {
1082            // Top level stack, not a child. Look for child stacks.
1083            mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
1084        }
1085
1086        if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
1087        else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
1088        mResumedActivity = null;
1089        mPausingActivity = prev;
1090        mLastPausedActivity = prev;
1091        mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1092                || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
1093        prev.state = ActivityState.PAUSING;
1094        prev.task.touchActiveTime();
1095        clearLaunchTime(prev);
1096        final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
1097        if (mService.mHasRecents
1098                && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
1099            prev.mUpdateTaskThumbnailWhenHidden = true;
1100        }
1101        stopFullyDrawnTraceIfNeeded();
1102
1103        mService.updateCpuStats();
1104
1105        if (prev.app != null && prev.app.thread != null) {
1106            if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
1107            try {
1108                EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
1109                        prev.userId, System.identityHashCode(prev),
1110                        prev.shortComponentName);
1111                mService.updateUsageStats(prev, false);
1112                prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
1113                        userLeaving, prev.configChangeFlags, dontWait);
1114            } catch (Exception e) {
1115                // Ignore exception, if process died other code will cleanup.
1116                Slog.w(TAG, "Exception thrown during pause", e);
1117                mPausingActivity = null;
1118                mLastPausedActivity = null;
1119                mLastNoHistoryActivity = null;
1120            }
1121        } else {
1122            mPausingActivity = null;
1123            mLastPausedActivity = null;
1124            mLastNoHistoryActivity = null;
1125        }
1126
1127        // If we are not going to sleep, we want to ensure the device is
1128        // awake until the next activity is started.
1129        if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
1130            mStackSupervisor.acquireLaunchWakelock();
1131        }
1132
1133        if (mPausingActivity != null) {
1134            // Have the window manager pause its key dispatching until the new
1135            // activity has started.  If we're pausing the activity just because
1136            // the screen is being turned off and the UI is sleeping, don't interrupt
1137            // key dispatch; the same activity will pick it up again on wakeup.
1138            if (!uiSleeping) {
1139                prev.pauseKeyDispatchingLocked();
1140            } else if (DEBUG_PAUSE) {
1141                 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
1142            }
1143
1144            if (dontWait) {
1145                // If the caller said they don't want to wait for the pause, then complete
1146                // the pause now.
1147                completePauseLocked(false);
1148                return false;
1149
1150            } else {
1151                // Schedule a pause timeout in case the app doesn't respond.
1152                // We don't give it much time because this directly impacts the
1153                // responsiveness seen by the user.
1154                Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1155                msg.obj = prev;
1156                prev.pauseTime = SystemClock.uptimeMillis();
1157                mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
1158                if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
1159                return true;
1160            }
1161
1162        } else {
1163            // This activity failed to schedule the
1164            // pause, so just treat it as being paused now.
1165            if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
1166            if (!resuming) {
1167                mStackSupervisor.resumeFocusedStackTopActivityLocked();
1168            }
1169            return false;
1170        }
1171    }
1172
1173    final void activityPausedLocked(IBinder token, boolean timeout) {
1174        if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1175            "Activity paused: token=" + token + ", timeout=" + timeout);
1176
1177        final ActivityRecord r = isInStackLocked(token);
1178        if (r != null) {
1179            mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1180            if (mPausingActivity == r) {
1181                if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
1182                        + (timeout ? " (due to timeout)" : " (pause complete)"));
1183                completePauseLocked(true);
1184                return;
1185            } else {
1186                EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1187                        r.userId, System.identityHashCode(r), r.shortComponentName,
1188                        mPausingActivity != null
1189                            ? mPausingActivity.shortComponentName : "(none)");
1190                if (r.state == ActivityState.PAUSING) {
1191                    r.state = ActivityState.PAUSED;
1192                    if (r.finishing) {
1193                        if (DEBUG_PAUSE) Slog.v(TAG,
1194                                "Executing finish of failed to pause activity: " + r);
1195                        finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1196                    }
1197                }
1198            }
1199        }
1200        mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
1201    }
1202
1203    final void activityResumedLocked(IBinder token) {
1204        final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1205        if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1206        r.icicle = null;
1207        r.haveState = false;
1208    }
1209
1210    final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1211            PersistableBundle persistentState, CharSequence description) {
1212        if (r.state != ActivityState.STOPPING) {
1213            Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1214            mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1215            return;
1216        }
1217        if (persistentState != null) {
1218            r.persistentState = persistentState;
1219            mService.notifyTaskPersisterLocked(r.task, false);
1220        }
1221        if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
1222        if (icicle != null) {
1223            // If icicle is null, this is happening due to a timeout, so we
1224            // haven't really saved the state.
1225            r.icicle = icicle;
1226            r.haveState = true;
1227            r.launchCount = 0;
1228            r.updateThumbnailLocked(null, description);
1229        }
1230        if (!r.stopped) {
1231            if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
1232            mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1233            r.stopped = true;
1234            r.state = ActivityState.STOPPED;
1235
1236            mWindowManager.notifyAppStopped(r.appToken, true);
1237
1238            if (getVisibleBehindActivity() == r) {
1239                mStackSupervisor.requestVisibleBehindLocked(r, false);
1240            }
1241            if (r.finishing) {
1242                r.clearOptionsLocked();
1243            } else {
1244                if (r.deferRelaunchUntilPaused) {
1245                    destroyActivityLocked(r, true, "stop-config");
1246                    mStackSupervisor.resumeFocusedStackTopActivityLocked();
1247                } else {
1248                    mStackSupervisor.updatePreviousProcessLocked(r);
1249                }
1250            }
1251        }
1252    }
1253
1254    private void completePauseLocked(boolean resumeNext) {
1255        ActivityRecord prev = mPausingActivity;
1256        if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
1257
1258        if (prev != null) {
1259            final boolean wasStopping = prev.state == ActivityState.STOPPING;
1260            prev.state = ActivityState.PAUSED;
1261            if (prev.finishing) {
1262                if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
1263                prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
1264            } else if (prev.app != null) {
1265                if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1266                        + " wasStopping=" + wasStopping + " visible=" + prev.visible);
1267                if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
1268                    if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1269                            "Complete pause, no longer waiting: " + prev);
1270                }
1271                if (prev.deferRelaunchUntilPaused) {
1272                    // Complete the deferred relaunch that was waiting for pause to complete.
1273                    if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
1274                    relaunchActivityLocked(prev, prev.configChangeFlags, false,
1275                            prev.preserveWindowOnDeferredRelaunch);
1276                } else if (wasStopping) {
1277                    // We are also stopping, the stop request must have gone soon after the pause.
1278                    // We can't clobber it, because the stop confirmation will not be handled.
1279                    // We don't need to schedule another stop, we only need to let it happen.
1280                    prev.state = ActivityState.STOPPING;
1281                } else if ((!prev.visible && !hasVisibleBehindActivity())
1282                        || mService.isSleepingOrShuttingDown()) {
1283                    // If we were visible then resumeTopActivities will release resources before
1284                    // stopping.
1285                    addToStopping(prev, true /* immediate */);
1286                }
1287            } else {
1288                if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
1289                prev = null;
1290            }
1291            // It is possible the activity was freezing the screen before it was paused.
1292            // In that case go ahead and remove the freeze this activity has on the screen
1293            // since it is no longer visible.
1294            prev.stopFreezingScreenLocked(true /*force*/);
1295            mPausingActivity = null;
1296        }
1297
1298        if (resumeNext) {
1299            final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1300            if (!mService.isSleepingOrShuttingDown()) {
1301                mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
1302            } else {
1303                mStackSupervisor.checkReadyForSleepLocked();
1304                ActivityRecord top = topStack.topRunningActivityLocked();
1305                if (top == null || (prev != null && top != prev)) {
1306                    // If there are no more activities available to run, do resume anyway to start
1307                    // something. Also if the top activity on the stack is not the just paused
1308                    // activity, we need to go ahead and resume it to ensure we complete an
1309                    // in-flight app switch.
1310                    mStackSupervisor.resumeFocusedStackTopActivityLocked();
1311                }
1312            }
1313        }
1314
1315        if (prev != null) {
1316            prev.resumeKeyDispatchingLocked();
1317
1318            if (prev.app != null && prev.cpuTimeAtResume > 0
1319                    && mService.mBatteryStatsService.isOnBattery()) {
1320                long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1321                        - prev.cpuTimeAtResume;
1322                if (diff > 0) {
1323                    BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1324                    synchronized (bsi) {
1325                        BatteryStatsImpl.Uid.Proc ps =
1326                                bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
1327                                        prev.info.packageName);
1328                        if (ps != null) {
1329                            ps.addForegroundTimeLocked(diff);
1330                        }
1331                    }
1332                }
1333            }
1334            prev.cpuTimeAtResume = 0; // reset it
1335        }
1336
1337        // Notify when the task stack has changed, but only if visibilities changed (not just focus)
1338        if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause) {
1339            mService.notifyTaskStackChangedLocked();
1340            mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1341        }
1342
1343        mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
1344    }
1345
1346    private void addToStopping(ActivityRecord r, boolean immediate) {
1347        if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1348            mStackSupervisor.mStoppingActivities.add(r);
1349        }
1350
1351        // If we already have a few activities waiting to stop, then give up
1352        // on things going idle and start clearing them out. Or if r is the
1353        // last of activity of the last task the stack will be empty and must
1354        // be cleared immediately.
1355        boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1356                || (r.frontOfTask && mTaskHistory.size() <= 1);
1357
1358        if (immediate || forceIdle) {
1359            if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
1360                    + forceIdle + "immediate=" + immediate);
1361            mStackSupervisor.scheduleIdleLocked();
1362        } else {
1363            mStackSupervisor.checkReadyForSleepLocked();
1364        }
1365    }
1366
1367    /**
1368     * Once we know that we have asked an application to put an activity in
1369     * the resumed state (either by launching it or explicitly telling it),
1370     * this function updates the rest of our state to match that fact.
1371     */
1372    private void completeResumeLocked(ActivityRecord next) {
1373        next.visible = true;
1374        next.idle = false;
1375        next.results = null;
1376        next.newIntents = null;
1377        next.stopped = false;
1378
1379        if (next.isHomeActivity()) {
1380            ProcessRecord app = next.task.mActivities.get(0).app;
1381            if (app != null && app != mService.mHomeProcess) {
1382                mService.mHomeProcess = app;
1383            }
1384        }
1385
1386        if (next.nowVisible) {
1387            // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1388            mStackSupervisor.notifyActivityDrawnForKeyguard();
1389        }
1390
1391        // schedule an idle timeout in case the app doesn't do it for us.
1392        mStackSupervisor.scheduleIdleTimeoutLocked(next);
1393
1394        mStackSupervisor.reportResumedActivityLocked(next);
1395
1396        next.resumeKeyDispatchingLocked();
1397        mNoAnimActivities.clear();
1398
1399        // Mark the point when the activity is resuming
1400        // TODO: To be more accurate, the mark should be before the onCreate,
1401        //       not after the onResume. But for subsequent starts, onResume is fine.
1402        if (next.app != null) {
1403            next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
1404        } else {
1405            next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1406        }
1407
1408        next.returningOptions = null;
1409
1410        if (getVisibleBehindActivity() == next) {
1411            // When resuming an activity, require it to call requestVisibleBehind() again.
1412            setVisibleBehindActivity(null);
1413        }
1414    }
1415
1416    private void setVisible(ActivityRecord r, boolean visible) {
1417        r.visible = visible;
1418        if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1419            r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1420            r.mUpdateTaskThumbnailWhenHidden = false;
1421        }
1422        mWindowManager.setAppVisibility(r.appToken, visible);
1423        final ArrayList<ActivityContainer> containers = r.mChildContainers;
1424        for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
1425            ActivityContainer container = containers.get(containerNdx);
1426            container.setVisible(visible);
1427        }
1428        mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1429    }
1430
1431    // Find the first visible activity above the passed activity and if it is translucent return it
1432    // otherwise return null;
1433    ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1434        TaskRecord task = r.task;
1435        if (task == null) {
1436            return null;
1437        }
1438
1439        ActivityStack stack = task.stack;
1440        if (stack == null) {
1441            return null;
1442        }
1443
1444        int stackNdx = mStacks.indexOf(stack);
1445
1446        ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1447        int taskNdx = tasks.indexOf(task);
1448
1449        ArrayList<ActivityRecord> activities = task.mActivities;
1450        int activityNdx = activities.indexOf(r) + 1;
1451
1452        final int numStacks = mStacks.size();
1453        while (stackNdx < numStacks) {
1454            final ActivityStack historyStack = mStacks.get(stackNdx);
1455            tasks = historyStack.mTaskHistory;
1456            final int numTasks = tasks.size();
1457            while (taskNdx < numTasks) {
1458                final TaskRecord currentTask = tasks.get(taskNdx);
1459                activities = currentTask.mActivities;
1460                final int numActivities = activities.size();
1461                while (activityNdx < numActivities) {
1462                    final ActivityRecord activity = activities.get(activityNdx);
1463                    if (!activity.finishing) {
1464                        return historyStack.mFullscreen
1465                                && currentTask.mFullscreen && activity.fullscreen ? null : activity;
1466                    }
1467                    ++activityNdx;
1468                }
1469                activityNdx = 0;
1470                ++taskNdx;
1471            }
1472            taskNdx = 0;
1473            ++stackNdx;
1474        }
1475
1476        return null;
1477    }
1478
1479    ActivityStack getNextFocusableStackLocked() {
1480        ArrayList<ActivityStack> stacks = mStacks;
1481        final ActivityRecord parent = mActivityContainer.mParentActivity;
1482        if (parent != null) {
1483            stacks = parent.task.stack.mStacks;
1484        }
1485        if (stacks != null) {
1486            for (int i = stacks.size() - 1; i >= 0; --i) {
1487                ActivityStack stack = stacks.get(i);
1488                if (stack != this && stack.isFocusable()
1489                        && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
1490                    return stack;
1491                }
1492            }
1493        }
1494        return null;
1495    }
1496
1497    /** Returns true if the stack contains a fullscreen task. */
1498    private boolean hasFullscreenTask() {
1499        for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1500            final TaskRecord task = mTaskHistory.get(i);
1501            if (task.mFullscreen) {
1502                return true;
1503            }
1504        }
1505        return false;
1506    }
1507
1508    /**
1509     * Returns true if the stack is translucent and can have other contents visible behind it if
1510     * needed. A stack is considered translucent if it don't contain a visible or
1511     * starting (about to be visible) activity that is fullscreen (opaque).
1512     * @param starting The currently starting activity or null if there is none.
1513     * @param stackBehindId The id of the stack directly behind this one.
1514     */
1515    private boolean isStackTranslucent(ActivityRecord starting, int stackBehindId) {
1516        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1517            final TaskRecord task = mTaskHistory.get(taskNdx);
1518            final ArrayList<ActivityRecord> activities = task.mActivities;
1519            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1520                final ActivityRecord r = activities.get(activityNdx);
1521
1522                if (r.finishing) {
1523                    // We don't factor in finishing activities when determining translucency since
1524                    // they will be gone soon.
1525                    continue;
1526                }
1527
1528                if (!r.visible && r != starting) {
1529                    // Also ignore invisible activities that are not the currently starting
1530                    // activity (about to be visible).
1531                    continue;
1532                }
1533
1534                if (r.fullscreen) {
1535                    // Stack isn't translucent if it has at least one fullscreen activity
1536                    // that is visible.
1537                    return false;
1538                }
1539
1540                if (!isHomeStack() && r.frontOfTask
1541                        && task.isOverHomeStack() && stackBehindId != HOME_STACK_ID) {
1542                    // Stack isn't translucent if it's top activity should have the home stack
1543                    // behind it and the stack currently behind it isn't the home stack.
1544                    return false;
1545                }
1546            }
1547        }
1548        return true;
1549    }
1550
1551    /**
1552     * Returns stack's visibility: {@link #STACK_INVISIBLE}, {@link #STACK_VISIBLE} or
1553     * {@link #STACK_VISIBLE_ACTIVITY_BEHIND}.
1554     * @param starting The currently starting activity or null if there is none.
1555     */
1556    int getStackVisibilityLocked(ActivityRecord starting) {
1557        if (!isAttached()) {
1558            return STACK_INVISIBLE;
1559        }
1560
1561        if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
1562            return STACK_VISIBLE;
1563        }
1564
1565        final int stackIndex = mStacks.indexOf(this);
1566
1567        if (stackIndex == mStacks.size() - 1) {
1568            Slog.wtf(TAG,
1569                    "Stack=" + this + " isn't front stack but is at the top of the stack list");
1570            return STACK_INVISIBLE;
1571        }
1572
1573        final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1574        if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
1575            return STACK_INVISIBLE;
1576        }
1577
1578        final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1579        final int focusedStackId = focusedStack.mStackId;
1580
1581        if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1582                && hasVisibleBehindActivity() && focusedStackId == HOME_STACK_ID
1583                && !focusedStack.topActivity().fullscreen) {
1584            // The fullscreen stack should be visible if it has a visible behind activity behind
1585            // the home stack that is translucent.
1586            return STACK_VISIBLE_ACTIVITY_BEHIND;
1587        }
1588
1589        if (mStackId == DOCKED_STACK_ID) {
1590            // Docked stack is always visible, except in the case where the top running activity
1591            // task in the focus stack doesn't support any form of resizing but we show it for the
1592            // home task even though it's not resizable.
1593            final ActivityRecord r = focusedStack.topRunningActivityLocked();
1594            final TaskRecord task = r != null ? r.task : null;
1595            return task == null || task.canGoInDockedStack() || task.isHomeTask() ? STACK_VISIBLE
1596                    : STACK_INVISIBLE;
1597        }
1598
1599        // Find the first stack behind focused stack that actually got something visible.
1600        int stackBehindFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1601        while (stackBehindFocusedIndex >= 0 &&
1602                mStacks.get(stackBehindFocusedIndex).topRunningActivityLocked() == null) {
1603            stackBehindFocusedIndex--;
1604        }
1605        if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1606                && stackIndex == stackBehindFocusedIndex) {
1607            // Stacks directly behind the docked or pinned stack are always visible.
1608            return STACK_VISIBLE;
1609        }
1610
1611        final int stackBehindFocusedId = (stackBehindFocusedIndex >= 0)
1612                ? mStacks.get(stackBehindFocusedIndex).mStackId : INVALID_STACK_ID;
1613
1614        if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1615                && focusedStack.isStackTranslucent(starting, stackBehindFocusedId)) {
1616            // Stacks behind the fullscreen stack with a translucent activity are always
1617            // visible so they can act as a backdrop to the translucent activity.
1618            // For example, dialog activities
1619            if (stackIndex == stackBehindFocusedIndex) {
1620                return STACK_VISIBLE;
1621            }
1622            if (stackBehindFocusedIndex >= 0) {
1623                if ((stackBehindFocusedId == DOCKED_STACK_ID
1624                        || stackBehindFocusedId == PINNED_STACK_ID)
1625                        && stackIndex == (stackBehindFocusedIndex - 1)) {
1626                    // The stack behind the docked or pinned stack is also visible so we can have a
1627                    // complete backdrop to the translucent activity when the docked stack is up.
1628                    return STACK_VISIBLE;
1629                }
1630            }
1631        }
1632
1633        if (StackId.isStaticStack(mStackId)) {
1634            // Visibility of any static stack should have been determined by the conditions above.
1635            return STACK_INVISIBLE;
1636        }
1637
1638        for (int i = stackIndex + 1; i < mStacks.size(); i++) {
1639            final ActivityStack stack = mStacks.get(i);
1640
1641            if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1642                continue;
1643            }
1644
1645            if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
1646                // These stacks can't have any dynamic stacks visible behind them.
1647                return STACK_INVISIBLE;
1648            }
1649
1650            if (!stack.isStackTranslucent(starting, INVALID_STACK_ID)) {
1651                return STACK_INVISIBLE;
1652            }
1653        }
1654
1655        return STACK_VISIBLE;
1656    }
1657
1658    final int rankTaskLayers(int baseLayer) {
1659        int layer = 0;
1660        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1661            final TaskRecord task = mTaskHistory.get(taskNdx);
1662            ActivityRecord r = task.topRunningActivityLocked();
1663            if (r == null || r.finishing || !r.visible) {
1664                task.mLayerRank = -1;
1665            } else {
1666                task.mLayerRank = baseLayer + layer++;
1667            }
1668        }
1669        return layer;
1670    }
1671
1672    /**
1673     * Make sure that all activities that need to be visible (that is, they
1674     * currently can be seen by the user) actually are.
1675     */
1676    final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1677            boolean preserveWindows) {
1678        ActivityRecord top = topRunningActivityLocked();
1679        if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
1680                + " configChanges=0x" + Integer.toHexString(configChanges));
1681        if (top != null) {
1682            checkTranslucentActivityWaiting(top);
1683        }
1684
1685        // If the top activity is not fullscreen, then we need to
1686        // make sure any activities under it are now visible.
1687        boolean aboveTop = top != null;
1688        final int stackVisibility = getStackVisibilityLocked(starting);
1689        final boolean stackInvisible = stackVisibility != STACK_VISIBLE;
1690        final boolean stackVisibleBehind = stackVisibility == STACK_VISIBLE_ACTIVITY_BEHIND;
1691        boolean behindFullscreenActivity = stackInvisible;
1692        boolean resumeNextActivity = isFocusable() && (isInStackLocked(starting) == null);
1693        boolean behindTranslucentActivity = false;
1694        final ActivityRecord visibleBehind = getVisibleBehindActivity();
1695        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1696            final TaskRecord task = mTaskHistory.get(taskNdx);
1697            final ArrayList<ActivityRecord> activities = task.mActivities;
1698            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1699                final ActivityRecord r = activities.get(activityNdx);
1700                if (r.finishing) {
1701                    // Normally the screenshot will be taken in makeInvisible(). When an activity
1702                    // is finishing, we no longer change its visibility, but we still need to take
1703                    // the screenshots if startPausingLocked decided it should be taken.
1704                    if (r.mUpdateTaskThumbnailWhenHidden) {
1705                        r.updateThumbnailLocked(screenshotActivitiesLocked(r), null);
1706                        r.mUpdateTaskThumbnailWhenHidden = false;
1707                    }
1708                    continue;
1709                }
1710                final boolean isTop = r == top;
1711                if (aboveTop && !isTop) {
1712                    continue;
1713                }
1714                aboveTop = false;
1715                // mLaunchingBehind: Activities launching behind are at the back of the task stack
1716                // but must be drawn initially for the animation as though they were visible.
1717                final boolean activityVisibleBehind =
1718                        (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == r;
1719                final boolean isVisible = (!behindFullscreenActivity || r.mLaunchTaskBehind
1720                        || activityVisibleBehind) && okToShowLocked(r);
1721                if (isVisible) {
1722                    if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
1723                            + " finishing=" + r.finishing + " state=" + r.state);
1724                    // First: if this is not the current activity being started, make
1725                    // sure it matches the current configuration.
1726                    if (r != starting) {
1727                        ensureActivityConfigurationLocked(r, 0, preserveWindows);
1728                    }
1729
1730                    if (r.app == null || r.app.thread == null) {
1731                        if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
1732                                resumeNextActivity, r)) {
1733                            if (activityNdx >= activities.size()) {
1734                                // Record may be removed if its process needs to restart.
1735                                activityNdx = activities.size() - 1;
1736                            } else {
1737                                resumeNextActivity = false;
1738                            }
1739                        }
1740                    } else if (r.visible) {
1741                        // If this activity is already visible, then there is nothing to do here.
1742                        if (handleAlreadyVisible(r)) {
1743                            resumeNextActivity = false;
1744                        }
1745                    } else {
1746                        makeVisibleIfNeeded(starting, r);
1747                    }
1748                    // Aggregate current change flags.
1749                    configChanges |= r.configChangeFlags;
1750                    behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1751                            behindFullscreenActivity, task, r);
1752                    if (behindFullscreenActivity && !r.fullscreen) {
1753                        behindTranslucentActivity = true;
1754                    }
1755                } else {
1756                    if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
1757                            + " finishing=" + r.finishing + " state=" + r.state + " stackInvisible="
1758                            + stackInvisible + " behindFullscreenActivity="
1759                            + behindFullscreenActivity + " mLaunchTaskBehind="
1760                            + r.mLaunchTaskBehind);
1761                    makeInvisible(r, visibleBehind);
1762                }
1763            }
1764            if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1765                // The visibility of tasks and the activities they contain in freeform stack are
1766                // determined individually unlike other stacks where the visibility or fullscreen
1767                // status of an activity in a previous task affects other.
1768                behindFullscreenActivity = stackVisibility == STACK_INVISIBLE;
1769            } else if (mStackId == HOME_STACK_ID) {
1770                if (task.isHomeTask()) {
1771                    if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
1772                            + " stackInvisible=" + stackInvisible
1773                            + " behindFullscreenActivity=" + behindFullscreenActivity);
1774                    // No other task in the home stack should be visible behind the home activity.
1775                    // Home activities is usually a translucent activity with the wallpaper behind
1776                    // them. However, when they don't have the wallpaper behind them, we want to
1777                    // show activities in the next application stack behind them vs. another
1778                    // task in the home stack like recents.
1779                    behindFullscreenActivity = true;
1780                } else if (task.isRecentsTask()
1781                        && task.getTaskToReturnTo() == APPLICATION_ACTIVITY_TYPE) {
1782                    if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1783                            "Recents task returning to app: at " + task
1784                                    + " stackInvisible=" + stackInvisible
1785                                    + " behindFullscreenActivity=" + behindFullscreenActivity);
1786                    // We don't want any other tasks in the home stack visible if the recents
1787                    // activity is going to be returning to an application activity type.
1788                    // We do this to preserve the visible order the user used to get into the
1789                    // recents activity. The recents activity is normally translucent and if it
1790                    // doesn't have the wallpaper behind it the next activity in the home stack
1791                    // shouldn't be visible when the home stack is brought to the front to display
1792                    // the recents activity from an app.
1793                    behindFullscreenActivity = true;
1794                }
1795
1796            }
1797        }
1798
1799        if (mTranslucentActivityWaiting != null &&
1800                mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1801            // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1802            notifyActivityDrawnLocked(null);
1803        }
1804    }
1805
1806    private void checkTranslucentActivityWaiting(ActivityRecord top) {
1807        if (mTranslucentActivityWaiting != top) {
1808            mUndrawnActivitiesBelowTopTranslucent.clear();
1809            if (mTranslucentActivityWaiting != null) {
1810                // Call the callback with a timeout indication.
1811                notifyActivityDrawnLocked(null);
1812                mTranslucentActivityWaiting = null;
1813            }
1814            mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1815        }
1816    }
1817
1818    private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
1819            boolean isTop, boolean andResume, ActivityRecord r) {
1820        // We need to make sure the app is running if it's the top, or it is just made visible from
1821        // invisible. If the app is already visible, it must have died while it was visible. In this
1822        // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1823        // thrashing.
1824        if (isTop || !r.visible) {
1825            // This activity needs to be visible, but isn't even running...
1826            // get it started and resume if no other stack in this stack is resumed.
1827            if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1828            if (r != starting) {
1829                r.startFreezingScreenLocked(r.app, configChanges);
1830            }
1831            if (!r.visible || r.mLaunchTaskBehind) {
1832                if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1833                setVisible(r, true);
1834            }
1835            if (r != starting) {
1836                mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
1837                return true;
1838            }
1839        }
1840        return false;
1841    }
1842
1843    private void makeInvisible(ActivityRecord r, ActivityRecord visibleBehind) {
1844        if (!r.visible) {
1845            if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1846            return;
1847        }
1848        // Now for any activities that aren't visible to the user, make sure they no longer are
1849        // keeping the screen frozen.
1850        if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.state);
1851        try {
1852            setVisible(r, false);
1853            switch (r.state) {
1854                case STOPPING:
1855                case STOPPED:
1856                    if (r.app != null && r.app.thread != null) {
1857                        if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1858                                "Scheduling invisibility: " + r);
1859                        r.app.thread.scheduleWindowVisibility(r.appToken, false);
1860                    }
1861                    break;
1862
1863                case INITIALIZING:
1864                case RESUMED:
1865                case PAUSING:
1866                case PAUSED:
1867                    // This case created for transitioning activities from
1868                    // translucent to opaque {@link Activity#convertToOpaque}.
1869                    if (visibleBehind == r) {
1870                        releaseBackgroundResources(r);
1871                    } else {
1872                        addToStopping(r, true /* immediate */);
1873                    }
1874                    break;
1875
1876                default:
1877                    break;
1878            }
1879        } catch (Exception e) {
1880            // Just skip on any failure; we'll make it visible when it next restarts.
1881            Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1882        }
1883    }
1884
1885    private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1886            TaskRecord task, ActivityRecord r) {
1887        if (r.fullscreen) {
1888            if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
1889                        + " stackInvisible=" + stackInvisible
1890                        + " behindFullscreenActivity=" + behindFullscreenActivity);
1891            // At this point, nothing else needs to be shown in this task.
1892            behindFullscreenActivity = true;
1893        } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
1894            if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1895                    + " stackInvisible=" + stackInvisible
1896                    + " behindFullscreenActivity=" + behindFullscreenActivity);
1897            behindFullscreenActivity = true;
1898        }
1899        return behindFullscreenActivity;
1900    }
1901
1902    private void makeVisibleIfNeeded(ActivityRecord starting, ActivityRecord r) {
1903
1904        // This activity is not currently visible, but is running. Tell it to become visible.
1905        if (r.state == ActivityState.RESUMED || r == starting) {
1906            if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1907                    "Not making visible, r=" + r + " state=" + r.state + " starting=" + starting);
1908            return;
1909        }
1910
1911        // If this activity is paused, tell it to now show its window.
1912        if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1913                "Making visible and scheduling visibility: " + r);
1914        try {
1915            if (mTranslucentActivityWaiting != null) {
1916                r.updateOptionsLocked(r.returningOptions);
1917                mUndrawnActivitiesBelowTopTranslucent.add(r);
1918            }
1919            setVisible(r, true);
1920            r.sleeping = false;
1921            r.app.pendingUiClean = true;
1922            r.app.thread.scheduleWindowVisibility(r.appToken, true);
1923            r.stopFreezingScreenLocked(false);
1924
1925            // The activity may be waiting for stop, but that is no longer
1926            // appropriate for it.
1927            mStackSupervisor.mStoppingActivities.remove(r);
1928            mStackSupervisor.mGoingToSleepActivities.remove(r);
1929        } catch (Exception e) {
1930            // Just skip on any failure; we'll make it
1931            // visible when it next restarts.
1932            Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
1933        }
1934    }
1935
1936    private boolean handleAlreadyVisible(ActivityRecord r) {
1937        if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1938        r.stopFreezingScreenLocked(false);
1939        try {
1940            if (r.returningOptions != null) {
1941                r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1942            }
1943        } catch(RemoteException e) {
1944        }
1945        return r.state == ActivityState.RESUMED;
1946    }
1947
1948    void convertActivityToTranslucent(ActivityRecord r) {
1949        mTranslucentActivityWaiting = r;
1950        mUndrawnActivitiesBelowTopTranslucent.clear();
1951        mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1952    }
1953
1954    void clearOtherAppTimeTrackers(AppTimeTracker except) {
1955        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1956            final TaskRecord task = mTaskHistory.get(taskNdx);
1957            final ArrayList<ActivityRecord> activities = task.mActivities;
1958            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1959                final ActivityRecord r = activities.get(activityNdx);
1960                if ( r.appTimeTracker != except) {
1961                    r.appTimeTracker = null;
1962                }
1963            }
1964        }
1965    }
1966
1967    /**
1968     * Called as activities below the top translucent activity are redrawn. When the last one is
1969     * redrawn notify the top activity by calling
1970     * {@link Activity#onTranslucentConversionComplete}.
1971     *
1972     * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1973     * occurred and the activity will be notified immediately.
1974     */
1975    void notifyActivityDrawnLocked(ActivityRecord r) {
1976        mActivityContainer.setDrawn();
1977        if ((r == null)
1978                || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1979                        mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1980            // The last undrawn activity below the top has just been drawn. If there is an
1981            // opaque activity at the top, notify it that it can become translucent safely now.
1982            final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1983            mTranslucentActivityWaiting = null;
1984            mUndrawnActivitiesBelowTopTranslucent.clear();
1985            mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1986
1987            if (waitingActivity != null) {
1988                mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1989                if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1990                    try {
1991                        waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1992                                waitingActivity.appToken, r != null);
1993                    } catch (RemoteException e) {
1994                    }
1995                }
1996            }
1997        }
1998    }
1999
2000    /** If any activities below the top running one are in the INITIALIZING state and they have a
2001     * starting window displayed then remove that starting window. It is possible that the activity
2002     * in this state will never resumed in which case that starting window will be orphaned. */
2003    void cancelInitializingActivities() {
2004        final ActivityRecord topActivity = topRunningActivityLocked();
2005        boolean aboveTop = true;
2006        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2007            final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2008            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2009                final ActivityRecord r = activities.get(activityNdx);
2010                if (aboveTop) {
2011                    if (r == topActivity) {
2012                        aboveTop = false;
2013                    }
2014                    continue;
2015                }
2016
2017                if (r.state == ActivityState.INITIALIZING
2018                        && r.mStartingWindowState == STARTING_WINDOW_SHOWN) {
2019                    if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
2020                            "Found orphaned starting window " + r);
2021                    r.mStartingWindowState = STARTING_WINDOW_REMOVED;
2022                    mWindowManager.removeAppStartingWindow(r.appToken);
2023                }
2024            }
2025        }
2026    }
2027
2028    /**
2029     * Ensure that the top activity in the stack is resumed.
2030     *
2031     * @param prev The previously resumed activity, for when in the process
2032     * of pausing; can be null to call from elsewhere.
2033     * @param options Activity options.
2034     *
2035     * @return Returns true if something is being resumed, or false if
2036     * nothing happened.
2037     *
2038     * NOTE: It is not safe to call this method directly as it can cause an activity in a
2039     *       non-focused stack to be resumed.
2040     *       Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
2041     *       right activity for the current system state.
2042     */
2043    boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
2044        if (mStackSupervisor.inResumeTopActivity) {
2045            // Don't even start recursing.
2046            return false;
2047        }
2048
2049        boolean result = false;
2050        try {
2051            // Protect against recursion.
2052            mStackSupervisor.inResumeTopActivity = true;
2053            if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
2054                mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
2055                mService.updateSleepIfNeededLocked();
2056            }
2057            result = resumeTopActivityInnerLocked(prev, options);
2058        } finally {
2059            mStackSupervisor.inResumeTopActivity = false;
2060        }
2061        return result;
2062    }
2063
2064    private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
2065        if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
2066
2067        if (!mService.mBooting && !mService.mBooted) {
2068            // Not ready yet!
2069            return false;
2070        }
2071
2072        ActivityRecord parent = mActivityContainer.mParentActivity;
2073        if ((parent != null && parent.state != ActivityState.RESUMED) ||
2074                !mActivityContainer.isAttachedLocked()) {
2075            // Do not resume this stack if its parent is not resumed.
2076            // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
2077            return false;
2078        }
2079
2080        mStackSupervisor.cancelInitializingActivities();
2081
2082        // Find the first activity that is not finishing.
2083        final ActivityRecord next = topRunningActivityLocked();
2084
2085        // Remember how we'll process this pause/resume situation, and ensure
2086        // that the state is reset however we wind up proceeding.
2087        final boolean userLeaving = mStackSupervisor.mUserLeaving;
2088        mStackSupervisor.mUserLeaving = false;
2089
2090        final TaskRecord prevTask = prev != null ? prev.task : null;
2091        if (next == null) {
2092            // There are no more activities!
2093            final String reason = "noMoreActivities";
2094            final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack()
2095                    ? HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2096            if (!mFullscreen && adjustFocusToNextFocusableStackLocked(returnTaskType, reason)) {
2097                // Try to move focus to the next visible stack with a running activity if this
2098                // stack is not covering the entire screen.
2099                return mStackSupervisor.resumeFocusedStackTopActivityLocked(
2100                        mStackSupervisor.getFocusedStack(), prev, null);
2101            }
2102
2103            // Let's just start up the Launcher...
2104            ActivityOptions.abort(options);
2105            if (DEBUG_STATES) Slog.d(TAG_STATES,
2106                    "resumeTopActivityLocked: No more activities go home");
2107            if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2108            // Only resume home if on home display
2109            return isOnHomeDisplay() &&
2110                    mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
2111        }
2112
2113        next.delayedResume = false;
2114
2115        // If the top activity is the resumed one, nothing to do.
2116        if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
2117                    mStackSupervisor.allResumedActivitiesComplete()) {
2118            // Make sure we have executed any pending transitions, since there
2119            // should be nothing left to do at this point.
2120            mWindowManager.executeAppTransition();
2121            mNoAnimActivities.clear();
2122            ActivityOptions.abort(options);
2123            if (DEBUG_STATES) Slog.d(TAG_STATES,
2124                    "resumeTopActivityLocked: Top activity resumed " + next);
2125            if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2126            return false;
2127        }
2128
2129        final TaskRecord nextTask = next.task;
2130        if (prevTask != null && prevTask.stack == this &&
2131                prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
2132            if (DEBUG_STACK)  mStackSupervisor.validateTopActivitiesLocked();
2133            if (prevTask == nextTask) {
2134                prevTask.setFrontOfTask();
2135            } else if (prevTask != topTask()) {
2136                // This task is going away but it was supposed to return to the home stack.
2137                // Now the task above it has to return to the home task instead.
2138                final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
2139                mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
2140            } else if (!isOnHomeDisplay()) {
2141                return false;
2142            } else if (!isHomeStack()){
2143                if (DEBUG_STATES) Slog.d(TAG_STATES,
2144                        "resumeTopActivityLocked: Launching home next");
2145                final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
2146                        HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2147                return isOnHomeDisplay() &&
2148                        mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
2149            }
2150        }
2151
2152        // If we are sleeping, and there is no resumed activity, and the top
2153        // activity is paused, well that is the state we want.
2154        if (mService.isSleepingOrShuttingDown()
2155                && mLastPausedActivity == next
2156                && mStackSupervisor.allPausedActivitiesComplete()) {
2157            // Make sure we have executed any pending transitions, since there
2158            // should be nothing left to do at this point.
2159            mWindowManager.executeAppTransition();
2160            mNoAnimActivities.clear();
2161            ActivityOptions.abort(options);
2162            if (DEBUG_STATES) Slog.d(TAG_STATES,
2163                    "resumeTopActivityLocked: Going to sleep and all paused");
2164            if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2165            return false;
2166        }
2167
2168        // Make sure that the user who owns this activity is started.  If not,
2169        // we will just leave it as is because someone should be bringing
2170        // another user's activities to the top of the stack.
2171        if (!mService.mUserController.hasStartedUserState(next.userId)) {
2172            Slog.w(TAG, "Skipping resume of top activity " + next
2173                    + ": user " + next.userId + " is stopped");
2174            if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2175            return false;
2176        }
2177
2178        // The activity may be waiting for stop, but that is no longer
2179        // appropriate for it.
2180        mStackSupervisor.mStoppingActivities.remove(next);
2181        mStackSupervisor.mGoingToSleepActivities.remove(next);
2182        next.sleeping = false;
2183        mStackSupervisor.mWaitingVisibleActivities.remove(next);
2184
2185        if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
2186
2187        // If we are currently pausing an activity, then don't do anything until that is done.
2188        if (!mStackSupervisor.allPausedActivitiesComplete()) {
2189            if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
2190                    "resumeTopActivityLocked: Skip resume: some activity pausing.");
2191            if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2192            return false;
2193        }
2194
2195        mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2196
2197        // We need to start pausing the current activity so the top one can be resumed...
2198        final boolean dontWaitForPause = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0;
2199        boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
2200        if (mResumedActivity != null) {
2201            if (DEBUG_STATES) Slog.d(TAG_STATES,
2202                    "resumeTopActivityLocked: Pausing " + mResumedActivity);
2203            pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
2204        }
2205        if (pausing) {
2206            if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
2207                    "resumeTopActivityLocked: Skip resume: need to start pausing");
2208            // At this point we want to put the upcoming activity's process
2209            // at the top of the LRU list, since we know we will be needing it
2210            // very soon and it would be a waste to let it get killed if it
2211            // happens to be sitting towards the end.
2212            if (next.app != null && next.app.thread != null) {
2213                mService.updateLruProcessLocked(next.app, true, null);
2214            }
2215            if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2216            return true;
2217        }
2218
2219        // If the most recent activity was noHistory but was only stopped rather
2220        // than stopped+finished because the device went to sleep, we need to make
2221        // sure to finish it as we're making a new activity topmost.
2222        if (mService.isSleeping() && mLastNoHistoryActivity != null &&
2223                !mLastNoHistoryActivity.finishing) {
2224            if (DEBUG_STATES) Slog.d(TAG_STATES,
2225                    "no-history finish of " + mLastNoHistoryActivity + " on new resume");
2226            requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
2227                    null, "resume-no-history", false);
2228            mLastNoHistoryActivity = null;
2229        }
2230
2231        if (prev != null && prev != next) {
2232            if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2233                    && next != null && !next.nowVisible) {
2234                mStackSupervisor.mWaitingVisibleActivities.add(prev);
2235                if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2236                        "Resuming top, waiting visible to hide: " + prev);
2237            } else {
2238                // The next activity is already visible, so hide the previous
2239                // activity's windows right now so we can show the new one ASAP.
2240                // We only do this if the previous is finishing, which should mean
2241                // it is on top of the one being resumed so hiding it quickly
2242                // is good.  Otherwise, we want to do the normal route of allowing
2243                // the resumed activity to be shown so we can decide if the
2244                // previous should actually be hidden depending on whether the
2245                // new one is found to be full-screen or not.
2246                if (prev.finishing) {
2247                    mWindowManager.setAppVisibility(prev.appToken, false);
2248                    if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2249                            "Not waiting for visible to hide: " + prev + ", waitingVisible="
2250                            + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2251                            + ", nowVisible=" + next.nowVisible);
2252                } else {
2253                    if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2254                            "Previous already visible but still waiting to hide: " + prev
2255                            + ", waitingVisible="
2256                            + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2257                            + ", nowVisible=" + next.nowVisible);
2258                }
2259            }
2260        }
2261
2262        // Launching this app's activity, make sure the app is no longer
2263        // considered stopped.
2264        try {
2265            AppGlobals.getPackageManager().setPackageStoppedState(
2266                    next.packageName, false, next.userId); /* TODO: Verify if correct userid */
2267        } catch (RemoteException e1) {
2268        } catch (IllegalArgumentException e) {
2269            Slog.w(TAG, "Failed trying to unstop package "
2270                    + next.packageName + ": " + e);
2271        }
2272
2273        // We are starting up the next activity, so tell the window manager
2274        // that the previous one will be hidden soon.  This way it can know
2275        // to ignore it when computing the desired screen orientation.
2276        boolean anim = true;
2277        if (prev != null) {
2278            if (prev.finishing) {
2279                if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2280                        "Prepare close transition: prev=" + prev);
2281                if (mNoAnimActivities.contains(prev)) {
2282                    anim = false;
2283                    mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
2284                } else {
2285                    mWindowManager.prepareAppTransition(prev.task == next.task
2286                            ? TRANSIT_ACTIVITY_CLOSE
2287                            : TRANSIT_TASK_CLOSE, false);
2288                }
2289                mWindowManager.setAppVisibility(prev.appToken, false);
2290            } else {
2291                if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2292                        "Prepare open transition: prev=" + prev);
2293                if (mNoAnimActivities.contains(next)) {
2294                    anim = false;
2295                    mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
2296                } else {
2297                    mWindowManager.prepareAppTransition(prev.task == next.task
2298                            ? TRANSIT_ACTIVITY_OPEN
2299                            : next.mLaunchTaskBehind
2300                                    ? TRANSIT_TASK_OPEN_BEHIND
2301                                    : TRANSIT_TASK_OPEN, false);
2302                }
2303            }
2304        } else {
2305            if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
2306            if (mNoAnimActivities.contains(next)) {
2307                anim = false;
2308                mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
2309            } else {
2310                mWindowManager.prepareAppTransition(TRANSIT_ACTIVITY_OPEN, false);
2311            }
2312        }
2313
2314        Bundle resumeAnimOptions = null;
2315        if (anim) {
2316            ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2317            if (opts != null) {
2318                resumeAnimOptions = opts.toBundle();
2319            }
2320            next.applyOptionsLocked();
2321        } else {
2322            next.clearOptionsLocked();
2323        }
2324
2325        ActivityStack lastStack = mStackSupervisor.getLastStack();
2326        if (next.app != null && next.app.thread != null) {
2327            if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next
2328                    + " stopped=" + next.stopped + " visible=" + next.visible);
2329
2330            // This activity is now becoming visible.
2331            if (!next.visible || next.stopped) {
2332                mWindowManager.setAppVisibility(next.appToken, true);
2333            }
2334
2335            // schedule launch ticks to collect information about slow apps.
2336            next.startLaunchTickingLocked();
2337
2338            ActivityRecord lastResumedActivity =
2339                    lastStack == null ? null :lastStack.mResumedActivity;
2340            ActivityState lastState = next.state;
2341
2342            mService.updateCpuStats();
2343
2344            if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
2345            next.state = ActivityState.RESUMED;
2346            mResumedActivity = next;
2347            next.task.touchActiveTime();
2348            mRecentTasks.addLocked(next.task);
2349            mService.updateLruProcessLocked(next.app, true, null);
2350            updateLRUListLocked(next);
2351            mService.updateOomAdjLocked();
2352
2353            // Have the window manager re-evaluate the orientation of
2354            // the screen based on the new activity order.
2355            boolean notUpdated = true;
2356            if (mStackSupervisor.isFocusedStack(this)) {
2357                Configuration config = mWindowManager.updateOrientationFromAppTokens(
2358                        mService.mConfiguration,
2359                        next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2360                if (config != null) {
2361                    next.frozenBeforeDestroy = true;
2362                }
2363                notUpdated = !mService.updateConfigurationLocked(config, next, false);
2364            }
2365
2366            if (notUpdated) {
2367                // The configuration update wasn't able to keep the existing
2368                // instance of the activity, and instead started a new one.
2369                // We should be all done, but let's just make sure our activity
2370                // is still at the top and schedule another run if something
2371                // weird happened.
2372                ActivityRecord nextNext = topRunningActivityLocked();
2373                if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
2374                        "Activity config changed during resume: " + next
2375                        + ", new next: " + nextNext);
2376                if (nextNext != next) {
2377                    // Do over!
2378                    mStackSupervisor.scheduleResumeTopActivities();
2379                }
2380                if (mStackSupervisor.reportResumedActivityLocked(next)) {
2381                    mNoAnimActivities.clear();
2382                    if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2383                    return true;
2384                }
2385                if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2386                return false;
2387            }
2388
2389            try {
2390                // Deliver all pending results.
2391                ArrayList<ResultInfo> a = next.results;
2392                if (a != null) {
2393                    final int N = a.size();
2394                    if (!next.finishing && N > 0) {
2395                        if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2396                                "Delivering results to " + next + ": " + a);
2397                        next.app.thread.scheduleSendResult(next.appToken, a);
2398                    }
2399                }
2400
2401                if (next.newIntents != null) {
2402                    next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
2403                }
2404
2405                // Well the app will no longer be stopped.
2406                // Clear app token stopped state in window manager if needed.
2407                mWindowManager.notifyAppStopped(next.appToken, false);
2408
2409                EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2410                        System.identityHashCode(next), next.task.taskId, next.shortComponentName);
2411
2412                next.sleeping = false;
2413                mService.showAskCompatModeDialogLocked(next);
2414                next.app.pendingUiClean = true;
2415                next.app.forceProcessStateUpTo(mService.mTopProcessState);
2416                next.clearOptionsLocked();
2417                next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
2418                        mService.isNextTransitionForward(), resumeAnimOptions);
2419
2420                mStackSupervisor.checkReadyForSleepLocked();
2421
2422                if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
2423            } catch (Exception e) {
2424                // Whoops, need to restart this activity!
2425                if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
2426                        + lastState + ": " + next);
2427                next.state = lastState;
2428                if (lastStack != null) {
2429                    lastStack.mResumedActivity = lastResumedActivity;
2430                }
2431                Slog.i(TAG, "Restarting because process died: " + next);
2432                if (!next.hasBeenLaunched) {
2433                    next.hasBeenLaunched = true;
2434                } else  if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2435                        mStackSupervisor.isFrontStack(lastStack)) {
2436                    mWindowManager.setAppStartingWindow(
2437                            next.appToken, next.packageName, next.theme,
2438                            mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
2439                            next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2440                            next.windowFlags, null, true);
2441                    next.mStartingWindowState = STARTING_WINDOW_SHOWN;
2442                }
2443                mStackSupervisor.startSpecificActivityLocked(next, true, false);
2444                if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2445                return true;
2446            }
2447
2448            // From this point on, if something goes wrong there is no way
2449            // to recover the activity.
2450            try {
2451                completeResumeLocked(next);
2452            } catch (Exception e) {
2453                // If any exception gets thrown, toss away this
2454                // activity and try the next one.
2455                Slog.w(TAG, "Exception thrown during resume of " + next, e);
2456                requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
2457                        "resume-exception", true);
2458                if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2459                return true;
2460            }
2461        } else {
2462            // Whoops, need to restart this activity!
2463            if (!next.hasBeenLaunched) {
2464                next.hasBeenLaunched = true;
2465            } else {
2466                if (SHOW_APP_STARTING_PREVIEW) {
2467                    mWindowManager.setAppStartingWindow(
2468                            next.appToken, next.packageName, next.theme,
2469                            mService.compatibilityInfoForPackageLocked(
2470                                    next.info.applicationInfo),
2471                            next.nonLocalizedLabel,
2472                            next.labelRes, next.icon, next.logo, next.windowFlags,
2473                            null, true);
2474                    next.mStartingWindowState = STARTING_WINDOW_SHOWN;
2475                }
2476                if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
2477            }
2478            if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
2479            mStackSupervisor.startSpecificActivityLocked(next, true, true);
2480        }
2481
2482        if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2483        return true;
2484    }
2485
2486    private TaskRecord getNextTask(TaskRecord targetTask) {
2487        final int index = mTaskHistory.indexOf(targetTask);
2488        if (index >= 0) {
2489            final int numTasks = mTaskHistory.size();
2490            for (int i = index + 1; i < numTasks; ++i) {
2491                TaskRecord task = mTaskHistory.get(i);
2492                if (task.userId == targetTask.userId) {
2493                    return task;
2494                }
2495            }
2496        }
2497        return null;
2498    }
2499
2500    private void insertTaskAtPosition(TaskRecord task, int position) {
2501        if (position >= mTaskHistory.size()) {
2502            insertTaskAtTop(task, null);
2503            return;
2504        }
2505        // Calculate maximum possible position for this task.
2506        int maxPosition = mTaskHistory.size();
2507        if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
2508                && task.topRunningActivityLocked() == null) {
2509            // Put non-current user tasks below current user tasks.
2510            while (maxPosition > 0) {
2511                final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2512                if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
2513                        || tmpTask.topRunningActivityLocked() == null) {
2514                    break;
2515                }
2516                maxPosition--;
2517            }
2518        }
2519        position = Math.min(position, maxPosition);
2520        mTaskHistory.remove(task);
2521        mTaskHistory.add(position, task);
2522        updateTaskMovement(task, true);
2523    }
2524
2525    private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
2526        // If the moving task is over home stack, transfer its return type to next task
2527        if (task.isOverHomeStack()) {
2528            final TaskRecord nextTask = getNextTask(task);
2529            if (nextTask != null) {
2530                nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2531            }
2532        }
2533
2534        // If this is being moved to the top by another activity or being launched from the home
2535        // activity, set mTaskToReturnTo accordingly.
2536        if (isOnHomeDisplay()) {
2537            ActivityStack lastStack = mStackSupervisor.getLastStack();
2538            final boolean fromHome = lastStack.isHomeStack();
2539            if (!isHomeStack() && (fromHome || topTask() != task)) {
2540                int returnToType = APPLICATION_ACTIVITY_TYPE;
2541                if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
2542                    returnToType = lastStack.topTask() == null
2543                            ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
2544                }
2545                task.setTaskToReturnTo(returnToType);
2546            }
2547        } else {
2548            task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
2549        }
2550
2551        mTaskHistory.remove(task);
2552        // Now put task at top.
2553        int taskNdx = mTaskHistory.size();
2554        final boolean notShownWhenLocked =
2555                (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
2556                || (newActivity == null && task.topRunningActivityLocked() == null);
2557        if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
2558            // Put non-current user tasks below current user tasks.
2559            while (--taskNdx >= 0) {
2560                final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
2561                if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
2562                        || tmpTask.topRunningActivityLocked() == null) {
2563                    break;
2564                }
2565            }
2566            ++taskNdx;
2567        }
2568        mTaskHistory.add(taskNdx, task);
2569        updateTaskMovement(task, true);
2570    }
2571
2572    final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2573            ActivityOptions options) {
2574        TaskRecord rTask = r.task;
2575        final int taskId = rTask.taskId;
2576        // mLaunchTaskBehind tasks get placed at the back of the task stack.
2577        if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
2578            // Last activity in task had been removed or ActivityManagerService is reusing task.
2579            // Insert or replace.
2580            // Might not even be in.
2581            insertTaskAtTop(rTask, r);
2582            mWindowManager.moveTaskToTop(taskId);
2583        }
2584        TaskRecord task = null;
2585        if (!newTask) {
2586            // If starting in an existing task, find where that is...
2587            boolean startIt = true;
2588            for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2589                task = mTaskHistory.get(taskNdx);
2590                if (task.getTopActivity() == null) {
2591                    // All activities in task are finishing.
2592                    continue;
2593                }
2594                if (task == r.task) {
2595                    // Here it is!  Now, if this is not yet visible to the
2596                    // user, then just add it without starting; it will
2597                    // get started when the user navigates back to it.
2598                    if (!startIt) {
2599                        if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2600                                + task, new RuntimeException("here").fillInStackTrace());
2601                        task.addActivityToTop(r);
2602                        r.putInHistory();
2603                        addConfigOverride(r, task);
2604                        if (VALIDATE_TOKENS) {
2605                            validateAppTokensLocked();
2606                        }
2607                        ActivityOptions.abort(options);
2608                        return;
2609                    }
2610                    break;
2611                } else if (task.numFullscreen > 0) {
2612                    startIt = false;
2613                }
2614            }
2615        }
2616
2617        // Place a new activity at top of stack, so it is next to interact
2618        // with the user.
2619
2620        // If we are not placing the new activity frontmost, we do not want
2621        // to deliver the onUserLeaving callback to the actual frontmost
2622        // activity
2623        if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
2624            mStackSupervisor.mUserLeaving = false;
2625            if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2626                    "startActivity() behind front, mUserLeaving=false");
2627        }
2628
2629        task = r.task;
2630
2631        // Slot the activity into the history stack and proceed
2632        if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
2633                new RuntimeException("here").fillInStackTrace());
2634        task.addActivityToTop(r);
2635        task.setFrontOfTask();
2636
2637        r.putInHistory();
2638        if (!isHomeStack() || numActivities() > 0) {
2639            // We want to show the starting preview window if we are
2640            // switching to a new task, or the next activity's process is
2641            // not currently running.
2642            boolean showStartingIcon = newTask;
2643            ProcessRecord proc = r.app;
2644            if (proc == null) {
2645                proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2646            }
2647            if (proc == null || proc.thread == null) {
2648                showStartingIcon = true;
2649            }
2650            if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2651                    "Prepare open transition: starting " + r);
2652            if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2653                mWindowManager.prepareAppTransition(TRANSIT_NONE, keepCurTransition);
2654                mNoAnimActivities.add(r);
2655            } else {
2656                mWindowManager.prepareAppTransition(newTask
2657                        ? r.mLaunchTaskBehind
2658                                ? TRANSIT_TASK_OPEN_BEHIND
2659                                : TRANSIT_TASK_OPEN
2660                        : TRANSIT_ACTIVITY_OPEN, keepCurTransition);
2661                mNoAnimActivities.remove(r);
2662            }
2663            addConfigOverride(r, task);
2664            boolean doShow = true;
2665            if (newTask) {
2666                // Even though this activity is starting fresh, we still need
2667                // to reset it to make sure we apply affinities to move any
2668                // existing activities from other tasks in to it.
2669                // If the caller has requested that the target task be
2670                // reset, then do so.
2671                if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2672                    resetTaskIfNeededLocked(r, r);
2673                    doShow = topRunningNonDelayedActivityLocked(null) == r;
2674                }
2675            } else if (options != null && options.getAnimationType()
2676                    == ActivityOptions.ANIM_SCENE_TRANSITION) {
2677                doShow = false;
2678            }
2679            if (r.mLaunchTaskBehind) {
2680                // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2681                // tell WindowManager that r is visible even though it is at the back of the stack.
2682                mWindowManager.setAppVisibility(r.appToken, true);
2683                ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2684            } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
2685                // Figure out if we are transitioning from another activity that is
2686                // "has the same starting icon" as the next one.  This allows the
2687                // window manager to keep the previous window it had previously
2688                // created, if it still had one.
2689                ActivityRecord prev = mResumedActivity;
2690                if (prev != null) {
2691                    // We don't want to reuse the previous starting preview if:
2692                    // (1) The current activity is in a different task.
2693                    if (prev.task != r.task) {
2694                        prev = null;
2695                    }
2696                    // (2) The current activity is already displayed.
2697                    else if (prev.nowVisible) {
2698                        prev = null;
2699                    }
2700                }
2701                mWindowManager.setAppStartingWindow(
2702                        r.appToken, r.packageName, r.theme,
2703                        mService.compatibilityInfoForPackageLocked(
2704                                r.info.applicationInfo), r.nonLocalizedLabel,
2705                        r.labelRes, r.icon, r.logo, r.windowFlags,
2706                        prev != null ? prev.appToken : null, showStartingIcon);
2707                r.mStartingWindowState = STARTING_WINDOW_SHOWN;
2708            }
2709        } else {
2710            // If this is the first activity, don't do any fancy animations,
2711            // because there is nothing for it to animate on top of.
2712            addConfigOverride(r, task);
2713            ActivityOptions.abort(options);
2714            options = null;
2715        }
2716        if (VALIDATE_TOKENS) {
2717            validateAppTokensLocked();
2718        }
2719    }
2720
2721    final void validateAppTokensLocked() {
2722        mValidateAppTokens.clear();
2723        mValidateAppTokens.ensureCapacity(numActivities());
2724        final int numTasks = mTaskHistory.size();
2725        for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2726            TaskRecord task = mTaskHistory.get(taskNdx);
2727            final ArrayList<ActivityRecord> activities = task.mActivities;
2728            if (activities.isEmpty()) {
2729                continue;
2730            }
2731            TaskGroup group = new TaskGroup();
2732            group.taskId = task.taskId;
2733            mValidateAppTokens.add(group);
2734            final int numActivities = activities.size();
2735            for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2736                final ActivityRecord r = activities.get(activityNdx);
2737                group.tokens.add(r.appToken);
2738            }
2739        }
2740        mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
2741    }
2742
2743    /**
2744     * Perform a reset of the given task, if needed as part of launching it.
2745     * Returns the new HistoryRecord at the top of the task.
2746     */
2747    /**
2748     * Helper method for #resetTaskIfNeededLocked.
2749     * We are inside of the task being reset...  we'll either finish this activity, push it out
2750     * for another task, or leave it as-is.
2751     * @param task The task containing the Activity (taskTop) that might be reset.
2752     * @param forceReset
2753     * @return An ActivityOptions that needs to be processed.
2754     */
2755    final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
2756        ActivityOptions topOptions = null;
2757
2758        int replyChainEnd = -1;
2759        boolean canMoveOptions = true;
2760
2761        // We only do this for activities that are not the root of the task (since if we finish
2762        // the root, we may no longer have the task!).
2763        final ArrayList<ActivityRecord> activities = task.mActivities;
2764        final int numActivities = activities.size();
2765        final int rootActivityNdx = task.findEffectiveRootIndex();
2766        for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
2767            ActivityRecord target = activities.get(i);
2768            if (target.frontOfTask)
2769                break;
2770
2771            final int flags = target.info.flags;
2772            final boolean finishOnTaskLaunch =
2773                    (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2774            final boolean allowTaskReparenting =
2775                    (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2776            final boolean clearWhenTaskReset =
2777                    (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2778
2779            if (!finishOnTaskLaunch
2780                    && !clearWhenTaskReset
2781                    && target.resultTo != null) {
2782                // If this activity is sending a reply to a previous
2783                // activity, we can't do anything with it now until
2784                // we reach the start of the reply chain.
2785                // XXX note that we are assuming the result is always
2786                // to the previous activity, which is almost always
2787                // the case but we really shouldn't count on.
2788                if (replyChainEnd < 0) {
2789                    replyChainEnd = i;
2790                }
2791            } else if (!finishOnTaskLaunch
2792                    && !clearWhenTaskReset
2793                    && allowTaskReparenting
2794                    && target.taskAffinity != null
2795                    && !target.taskAffinity.equals(task.affinity)) {
2796                // If this activity has an affinity for another
2797                // task, then we need to move it out of here.  We will
2798                // move it as far out of the way as possible, to the
2799                // bottom of the activity stack.  This also keeps it
2800                // correctly ordered with any activities we previously
2801                // moved.
2802                final TaskRecord targetTask;
2803                final ActivityRecord bottom =
2804                        !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
2805                                mTaskHistory.get(0).mActivities.get(0) : null;
2806                if (bottom != null && target.taskAffinity != null
2807                        && target.taskAffinity.equals(bottom.task.affinity)) {
2808                    // If the activity currently at the bottom has the
2809                    // same task affinity as the one we are moving,
2810                    // then merge it into the same task.
2811                    targetTask = bottom.task;
2812                    if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
2813                            + " out to bottom task " + bottom.task);
2814                } else {
2815                    targetTask = createTaskRecord(
2816                            mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
2817                            target.info, null, null, null, false);
2818                    targetTask.affinityIntent = target.intent;
2819                    if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
2820                            + " out to new task " + target.task);
2821                }
2822
2823                setAppTask(target, targetTask);
2824
2825                boolean noOptions = canMoveOptions;
2826                final int start = replyChainEnd < 0 ? i : replyChainEnd;
2827                for (int srcPos = start; srcPos >= i; --srcPos) {
2828                    final ActivityRecord p = activities.get(srcPos);
2829                    if (p.finishing) {
2830                        continue;
2831                    }
2832
2833                    canMoveOptions = false;
2834                    if (noOptions && topOptions == null) {
2835                        topOptions = p.takeOptionsLocked();
2836                        if (topOptions != null) {
2837                            noOptions = false;
2838                        }
2839                    }
2840                    if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2841                            "Removing activity " + p + " from task=" + task + " adding to task="
2842                            + targetTask + " Callers=" + Debug.getCallers(4));
2843                    if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2844                            "Pushing next activity " + p + " out to target's task " + target.task);
2845                    p.setTask(targetTask, null);
2846                    targetTask.addActivityAtBottom(p);
2847
2848                    setAppTask(p, targetTask);
2849                }
2850
2851                mWindowManager.moveTaskToBottom(targetTask.taskId);
2852                if (VALIDATE_TOKENS) {
2853                    validateAppTokensLocked();
2854                }
2855
2856                replyChainEnd = -1;
2857            } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2858                // If the activity should just be removed -- either
2859                // because it asks for it, or the task should be
2860                // cleared -- then finish it and anything that is
2861                // part of its reply chain.
2862                int end;
2863                if (clearWhenTaskReset) {
2864                    // In this case, we want to finish this activity
2865                    // and everything above it, so be sneaky and pretend
2866                    // like these are all in the reply chain.
2867                    end = activities.size() - 1;
2868                } else if (replyChainEnd < 0) {
2869                    end = i;
2870                } else {
2871                    end = replyChainEnd;
2872                }
2873                boolean noOptions = canMoveOptions;
2874                for (int srcPos = i; srcPos <= end; srcPos++) {
2875                    ActivityRecord p = activities.get(srcPos);
2876                    if (p.finishing) {
2877                        continue;
2878                    }
2879                    canMoveOptions = false;
2880                    if (noOptions && topOptions == null) {
2881                        topOptions = p.takeOptionsLocked();
2882                        if (topOptions != null) {
2883                            noOptions = false;
2884                        }
2885                    }
2886                    if (DEBUG_TASKS) Slog.w(TAG_TASKS,
2887                            "resetTaskIntendedTask: calling finishActivity on " + p);
2888                    if (finishActivityLocked(
2889                            p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
2890                        end--;
2891                        srcPos--;
2892                    }
2893                }
2894                replyChainEnd = -1;
2895            } else {
2896                // If we were in the middle of a chain, well the
2897                // activity that started it all doesn't want anything
2898                // special, so leave it all as-is.
2899                replyChainEnd = -1;
2900            }
2901        }
2902
2903        return topOptions;
2904    }
2905
2906    /**
2907     * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2908     * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2909     * @param affinityTask The task we are looking for an affinity to.
2910     * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2911     * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2912     * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2913     */
2914    private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
2915            boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
2916        int replyChainEnd = -1;
2917        final int taskId = task.taskId;
2918        final String taskAffinity = task.affinity;
2919
2920        final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2921        final int numActivities = activities.size();
2922        final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2923
2924        // Do not operate on or below the effective root Activity.
2925        for (int i = numActivities - 1; i > rootActivityNdx; --i) {
2926            ActivityRecord target = activities.get(i);
2927            if (target.frontOfTask)
2928                break;
2929
2930            final int flags = target.info.flags;
2931            boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2932            boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2933
2934            if (target.resultTo != null) {
2935                // If this activity is sending a reply to a previous
2936                // activity, we can't do anything with it now until
2937                // we reach the start of the reply chain.
2938                // XXX note that we are assuming the result is always
2939                // to the previous activity, which is almost always
2940                // the case but we really shouldn't count on.
2941                if (replyChainEnd < 0) {
2942                    replyChainEnd = i;
2943                }
2944            } else if (topTaskIsHigher
2945                    && allowTaskReparenting
2946                    && taskAffinity != null
2947                    && taskAffinity.equals(target.taskAffinity)) {
2948                // This activity has an affinity for our task. Either remove it if we are
2949                // clearing or move it over to our task.  Note that
2950                // we currently punt on the case where we are resetting a
2951                // task that is not at the top but who has activities above
2952                // with an affinity to it...  this is really not a normal
2953                // case, and we will need to later pull that task to the front
2954                // and usually at that point we will do the reset and pick
2955                // up those remaining activities.  (This only happens if
2956                // someone starts an activity in a new task from an activity
2957                // in a task that is not currently on top.)
2958                if (forceReset || finishOnTaskLaunch) {
2959                    final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2960                    if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2961                            "Finishing task at index " + start + " to " + i);
2962                    for (int srcPos = start; srcPos >= i; --srcPos) {
2963                        final ActivityRecord p = activities.get(srcPos);
2964                        if (p.finishing) {
2965                            continue;
2966                        }
2967                        finishActivityLocked(
2968                                p, Activity.RESULT_CANCELED, null, "move-affinity", false);
2969                    }
2970                } else {
2971                    if (taskInsertionPoint < 0) {
2972                        taskInsertionPoint = task.mActivities.size();
2973
2974                    }
2975
2976                    final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2977                    if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2978                            "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2979                            + " to task=" + task + ":" + taskInsertionPoint);
2980                    for (int srcPos = start; srcPos >= i; --srcPos) {
2981                        final ActivityRecord p = activities.get(srcPos);
2982                        p.setTask(task, null);
2983                        task.addActivityAtIndex(taskInsertionPoint, p);
2984
2985                        if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2986                                "Removing and adding activity " + p + " to stack at " + task
2987                                + " callers=" + Debug.getCallers(3));
2988                        if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2989                                + " from " + srcPos + " in to resetting task " + task);
2990                        setAppTask(p, task);
2991                    }
2992                    mWindowManager.moveTaskToTop(taskId);
2993                    if (VALIDATE_TOKENS) {
2994                        validateAppTokensLocked();
2995                    }
2996
2997                    // Now we've moved it in to place...  but what if this is
2998                    // a singleTop activity and we have put it on top of another
2999                    // instance of the same activity?  Then we drop the instance
3000                    // below so it remains singleTop.
3001                    if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
3002                        ArrayList<ActivityRecord> taskActivities = task.mActivities;
3003                        int targetNdx = taskActivities.indexOf(target);
3004                        if (targetNdx > 0) {
3005                            ActivityRecord p = taskActivities.get(targetNdx - 1);
3006                            if (p.intent.getComponent().equals(target.intent.getComponent())) {
3007                                finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
3008                                        false);
3009                            }
3010                        }
3011                    }
3012                }
3013
3014                replyChainEnd = -1;
3015            }
3016        }
3017        return taskInsertionPoint;
3018    }
3019
3020    final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
3021            ActivityRecord newActivity) {
3022        boolean forceReset =
3023                (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
3024        if (ACTIVITY_INACTIVE_RESET_TIME > 0
3025                && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
3026            if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
3027                forceReset = true;
3028            }
3029        }
3030
3031        final TaskRecord task = taskTop.task;
3032
3033        /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
3034         * for remaining tasks. Used for later tasks to reparent to task. */
3035        boolean taskFound = false;
3036
3037        /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
3038        ActivityOptions topOptions = null;
3039
3040        // Preserve the location for reparenting in the new task.
3041        int reparentInsertionPoint = -1;
3042
3043        for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3044            final TaskRecord targetTask = mTaskHistory.get(i);
3045
3046            if (targetTask == task) {
3047                topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
3048                taskFound = true;
3049            } else {
3050                reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
3051                        taskFound, forceReset, reparentInsertionPoint);
3052            }
3053        }
3054
3055        int taskNdx = mTaskHistory.indexOf(task);
3056        if (taskNdx >= 0) {
3057            do {
3058                taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
3059            } while (taskTop == null && taskNdx >= 0);
3060        }
3061
3062        if (topOptions != null) {
3063            // If we got some ActivityOptions from an activity on top that
3064            // was removed from the task, propagate them to the new real top.
3065            if (taskTop != null) {
3066                taskTop.updateOptionsLocked(topOptions);
3067            } else {
3068                topOptions.abort();
3069            }
3070        }
3071
3072        return taskTop;
3073    }
3074
3075    void sendActivityResultLocked(int callingUid, ActivityRecord r,
3076            String resultWho, int requestCode, int resultCode, Intent data) {
3077
3078        if (callingUid > 0) {
3079            mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
3080                    data, r.getUriPermissionsLocked(), r.userId);
3081        }
3082
3083        if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
3084                + " : who=" + resultWho + " req=" + requestCode
3085                + " res=" + resultCode + " data=" + data);
3086        if (mResumedActivity == r && r.app != null && r.app.thread != null) {
3087            try {
3088                ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
3089                list.add(new ResultInfo(resultWho, requestCode,
3090                        resultCode, data));
3091                r.app.thread.scheduleSendResult(r.appToken, list);
3092                return;
3093            } catch (Exception e) {
3094                Slog.w(TAG, "Exception thrown sending result to " + r, e);
3095            }
3096        }
3097
3098        r.addResultLocked(null, resultWho, requestCode, resultCode, data);
3099    }
3100
3101    private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
3102        if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
3103            return;
3104        }
3105
3106        final ActivityRecord next = topRunningActivityLocked();
3107        final String myReason = reason + " adjustFocus";
3108        if (next != r) {
3109            if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
3110                // For freeform, docked, and pinned stacks we always keep the focus within the
3111                // stack as long as there is a running activity in the stack that we can adjust
3112                // focus to.
3113                mService.setFocusedActivityLocked(next, myReason);
3114                return;
3115            } else {
3116                final TaskRecord task = r.task;
3117                if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
3118                    final int taskToReturnTo = task.getTaskToReturnTo();
3119
3120                    // For non-fullscreen stack, we want to move the focus to the next visible
3121                    // stack to prevent the home screen from moving to the top and obscuring
3122                    // other visible stacks.
3123                    if (!mFullscreen
3124                            && adjustFocusToNextFocusableStackLocked(taskToReturnTo, myReason)) {
3125                        return;
3126                    }
3127                    // Move the home stack to the top if this stack is fullscreen or there is no
3128                    // other visible stack.
3129                    if (mStackSupervisor.moveHomeStackTaskToTop(taskToReturnTo, myReason)) {
3130                        // Activity focus was already adjusted. Nothing else to do...
3131                        return;
3132                    }
3133                }
3134            }
3135        }
3136
3137        mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked(), myReason);
3138    }
3139
3140    private boolean adjustFocusToNextFocusableStackLocked(int taskToReturnTo, String reason) {
3141        final ActivityStack stack = getNextFocusableStackLocked();
3142        final String myReason = reason + " adjustFocusToNextFocusableStack";
3143        if (stack == null) {
3144            return false;
3145        }
3146
3147        final ActivityRecord top = stack.topRunningActivityLocked();
3148
3149        if (stack.isHomeStack() && (top == null || !top.visible)) {
3150            // If we will be focusing on the home stack next and its current top activity isn't
3151            // visible, then use the task return to value to determine the home task to display next.
3152            return mStackSupervisor.moveHomeStackTaskToTop(taskToReturnTo, reason);
3153        }
3154        return mService.setFocusedActivityLocked(top, myReason);
3155    }
3156
3157    final void stopActivityLocked(ActivityRecord r) {
3158        if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
3159        if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3160                || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3161            if (!r.finishing) {
3162                if (!mService.isSleeping()) {
3163                    if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
3164                    if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3165                            "stop-no-history", false)) {
3166                        // Activity was finished, no need to continue trying to schedule stop.
3167                        adjustFocusedActivityLocked(r, "stopActivityFinished");
3168                        r.resumeKeyDispatchingLocked();
3169                        return;
3170                    }
3171                } else {
3172                    if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
3173                            + " on stop because we're just sleeping");
3174                }
3175            }
3176        }
3177
3178        if (r.app != null && r.app.thread != null) {
3179            adjustFocusedActivityLocked(r, "stopActivity");
3180            r.resumeKeyDispatchingLocked();
3181            try {
3182                r.stopped = false;
3183                if (DEBUG_STATES) Slog.v(TAG_STATES,
3184                        "Moving to STOPPING: " + r + " (stop requested)");
3185                r.state = ActivityState.STOPPING;
3186                if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3187                        "Stopping visible=" + r.visible + " for " + r);
3188                if (!r.visible) {
3189                    mWindowManager.setAppVisibility(r.appToken, false);
3190                }
3191                EventLogTags.writeAmStopActivity(
3192                        r.userId, System.identityHashCode(r), r.shortComponentName);
3193                r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
3194                if (mService.isSleepingOrShuttingDown()) {
3195                    r.setSleeping(true);
3196                }
3197                Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
3198                mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
3199            } catch (Exception e) {
3200                // Maybe just ignore exceptions here...  if the process
3201                // has crashed, our death notification will clean things
3202                // up.
3203                Slog.w(TAG, "Exception thrown during pause", e);
3204                // Just in case, assume it to be stopped.
3205                r.stopped = true;
3206                if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
3207                r.state = ActivityState.STOPPED;
3208                if (r.deferRelaunchUntilPaused) {
3209                    destroyActivityLocked(r, true, "stop-except");
3210                }
3211            }
3212        }
3213    }
3214
3215    /**
3216     * @return Returns true if the activity is being finished, false if for
3217     * some reason it is being left as-is.
3218     */
3219    final boolean requestFinishActivityLocked(IBinder token, int resultCode,
3220            Intent resultData, String reason, boolean oomAdj) {
3221        ActivityRecord r = isInStackLocked(token);
3222        if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3223                "Finishing activity token=" + token + " r="
3224                + ", result=" + resultCode + ", data=" + resultData
3225                + ", reason=" + reason);
3226        if (r == null) {
3227            return false;
3228        }
3229
3230        finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
3231        return true;
3232    }
3233
3234    final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
3235        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3236            ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3237            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3238                ActivityRecord r = activities.get(activityNdx);
3239                if (r.resultTo == self && r.requestCode == requestCode) {
3240                    if ((r.resultWho == null && resultWho == null) ||
3241                        (r.resultWho != null && r.resultWho.equals(resultWho))) {
3242                        finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3243                                false);
3244                    }
3245                }
3246            }
3247        }
3248        mService.updateOomAdjLocked();
3249    }
3250
3251    final TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
3252        ActivityRecord r = topRunningActivityLocked();
3253        TaskRecord finishedTask = null;
3254        if (r == null || r.app != app) {
3255            return null;
3256        }
3257        Slog.w(TAG, "  Force finishing activity "
3258                + r.intent.getComponent().flattenToShortString());
3259        int taskNdx = mTaskHistory.indexOf(r.task);
3260        int activityNdx = r.task.mActivities.indexOf(r);
3261        finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
3262        finishedTask = r.task;
3263        // Also terminate any activities below it that aren't yet
3264        // stopped, to avoid a situation where one will get
3265        // re-start our crashing activity once it gets resumed again.
3266        --activityNdx;
3267        if (activityNdx < 0) {
3268            do {
3269                --taskNdx;
3270                if (taskNdx < 0) {
3271                    break;
3272                }
3273                activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3274            } while (activityNdx < 0);
3275        }
3276        if (activityNdx >= 0) {
3277            r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
3278            if (r.state == ActivityState.RESUMED
3279                    || r.state == ActivityState.PAUSING
3280                    || r.state == ActivityState.PAUSED) {
3281                if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
3282                    Slog.w(TAG, "  Force finishing activity "
3283                            + r.intent.getComponent().flattenToShortString());
3284                    finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
3285                }
3286            }
3287        }
3288        return finishedTask;
3289    }
3290
3291    final void finishVoiceTask(IVoiceInteractionSession session) {
3292        IBinder sessionBinder = session.asBinder();
3293        boolean didOne = false;
3294        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3295            TaskRecord tr = mTaskHistory.get(taskNdx);
3296            if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3297                for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3298                    ActivityRecord r = tr.mActivities.get(activityNdx);
3299                    if (!r.finishing) {
3300                        finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3301                                false);
3302                        didOne = true;
3303                    }
3304                }
3305            } else {
3306                // Check if any of the activities are using voice
3307                for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3308                    ActivityRecord r = tr.mActivities.get(activityNdx);
3309                    if (r.voiceSession != null
3310                            && r.voiceSession.asBinder() == sessionBinder) {
3311                        // Inform of cancellation
3312                        r.clearVoiceSessionLocked();
3313                        try {
3314                            r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3315                                    null);
3316                        } catch (RemoteException re) {
3317                            // Ok
3318                        }
3319                        // TODO: VI This is redundant in some cases
3320                        mService.finishRunningVoiceLocked();
3321                        break;
3322                    }
3323                }
3324            }
3325        }
3326        Slog.d(TAG, "ActivityStack.finishVoiceTask()");
3327
3328        if (didOne) {
3329            mService.updateOomAdjLocked();
3330        }
3331    }
3332
3333    final boolean finishActivityAffinityLocked(ActivityRecord r) {
3334        ArrayList<ActivityRecord> activities = r.task.mActivities;
3335        for (int index = activities.indexOf(r); index >= 0; --index) {
3336            ActivityRecord cur = activities.get(index);
3337            if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
3338                break;
3339            }
3340            finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
3341        }
3342        return true;
3343    }
3344
3345    final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3346        // send the result
3347        ActivityRecord resultTo = r.resultTo;
3348        if (resultTo != null) {
3349            if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
3350                    + " who=" + r.resultWho + " req=" + r.requestCode
3351                    + " res=" + resultCode + " data=" + resultData);
3352            if (resultTo.userId != r.userId) {
3353                if (resultData != null) {
3354                    resultData.prepareToLeaveUser(r.userId);
3355                }
3356            }
3357            if (r.info.applicationInfo.uid > 0) {
3358                mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3359                        resultTo.packageName, resultData,
3360                        resultTo.getUriPermissionsLocked(), resultTo.userId);
3361            }
3362            resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3363                                     resultData);
3364            r.resultTo = null;
3365        }
3366        else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
3367
3368        // Make sure this HistoryRecord is not holding on to other resources,
3369        // because clients have remote IPC references to this object so we
3370        // can't assume that will go away and want to avoid circular IPC refs.
3371        r.results = null;
3372        r.pendingResults = null;
3373        r.newIntents = null;
3374        r.icicle = null;
3375    }
3376
3377    /**
3378     * @return Returns true if this activity has been removed from the history
3379     * list, or false if it is still in the list and will be removed later.
3380     */
3381    final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3382            String reason, boolean oomAdj) {
3383        if (r.finishing) {
3384            Slog.w(TAG, "Duplicate finish request for " + r);
3385            return false;
3386        }
3387
3388        r.makeFinishingLocked();
3389        final TaskRecord task = r.task;
3390        EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3391                r.userId, System.identityHashCode(r),
3392                task.taskId, r.shortComponentName, reason);
3393        final ArrayList<ActivityRecord> activities = task.mActivities;
3394        final int index = activities.indexOf(r);
3395        if (index < (activities.size() - 1)) {
3396            task.setFrontOfTask();
3397            if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
3398                // If the caller asked that this activity (and all above it)
3399                // be cleared when the task is reset, don't lose that information,
3400                // but propagate it up to the next activity.
3401                ActivityRecord next = activities.get(index+1);
3402                next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
3403            }
3404        }
3405
3406        r.pauseKeyDispatchingLocked();
3407
3408        adjustFocusedActivityLocked(r, "finishActivity");
3409
3410        finishActivityResultsLocked(r, resultCode, resultData);
3411
3412        final boolean endTask = index <= 0;
3413        final int transit = endTask ? TRANSIT_TASK_CLOSE : TRANSIT_ACTIVITY_CLOSE;
3414        if (mResumedActivity == r) {
3415
3416            if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
3417                    "Prepare close transition: finishing " + r);
3418            mWindowManager.prepareAppTransition(transit, false);
3419
3420            // Tell window manager to prepare for this one to be removed.
3421            mWindowManager.setAppVisibility(r.appToken, false);
3422
3423            if (mPausingActivity == null) {
3424                if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3425                if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3426                        "finish() => pause with userLeaving=false");
3427                startPausingLocked(false, false, false, false);
3428            }
3429
3430            if (endTask) {
3431                mStackSupervisor.removeLockedTaskLocked(task);
3432            }
3433        } else if (r.state != ActivityState.PAUSING) {
3434            // If the activity is PAUSING, we will complete the finish once
3435            // it is done pausing; else we can just directly finish it here.
3436            if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
3437            if (r.visible) {
3438                mWindowManager.prepareAppTransition(transit, false);
3439                mWindowManager.setAppVisibility(r.appToken, false);
3440                mWindowManager.executeAppTransition();
3441                if (!mStackSupervisor.mWaitingVisibleActivities.contains(r)) {
3442                    mStackSupervisor.mWaitingVisibleActivities.add(r);
3443                }
3444            }
3445            return finishCurrentActivityLocked(r, (r.visible || r.nowVisible) ?
3446                    FINISH_AFTER_VISIBLE : FINISH_AFTER_PAUSE, oomAdj) == null;
3447        } else {
3448            if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
3449        }
3450
3451        return false;
3452    }
3453
3454    static final int FINISH_IMMEDIATELY = 0;
3455    static final int FINISH_AFTER_PAUSE = 1;
3456    static final int FINISH_AFTER_VISIBLE = 2;
3457
3458    final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
3459        // First things first: if this activity is currently visible,
3460        // and the resumed activity is not yet visible, then hold off on
3461        // finishing until the resumed one becomes visible.
3462        if (mode == FINISH_AFTER_VISIBLE && (r.visible || r.nowVisible)
3463                && !mStackSupervisor.allResumedActivitiesVisible()) {
3464            if (!mStackSupervisor.mStoppingActivities.contains(r)) {
3465                addToStopping(r, false /* immediate */);
3466            }
3467            if (DEBUG_STATES) Slog.v(TAG_STATES,
3468                    "Moving to STOPPING: "+ r + " (finish requested)");
3469            r.state = ActivityState.STOPPING;
3470            if (oomAdj) {
3471                mService.updateOomAdjLocked();
3472            }
3473            return r;
3474        }
3475
3476        // make sure the record is cleaned out of other places.
3477        mStackSupervisor.mStoppingActivities.remove(r);
3478        mStackSupervisor.mGoingToSleepActivities.remove(r);
3479        mStackSupervisor.mWaitingVisibleActivities.remove(r);
3480        if (mResumedActivity == r) {
3481            mResumedActivity = null;
3482        }
3483        final ActivityState prevState = r.state;
3484        if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
3485        r.state = ActivityState.FINISHING;
3486
3487        if (mode == FINISH_IMMEDIATELY
3488                || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
3489                || prevState == ActivityState.STOPPED
3490                || prevState == ActivityState.INITIALIZING) {
3491            // If this activity is already stopped, we can just finish
3492            // it right now.
3493            r.makeFinishingLocked();
3494            boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
3495            if (activityRemoved) {
3496                mStackSupervisor.resumeFocusedStackTopActivityLocked();
3497            }
3498            if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3499                    "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3500                    " destroy returned removed=" + activityRemoved);
3501            return activityRemoved ? null : r;
3502        }
3503
3504        // Need to go through the full pause cycle to get this
3505        // activity into the stopped state and then finish it.
3506        if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
3507        mStackSupervisor.mFinishingActivities.add(r);
3508        r.resumeKeyDispatchingLocked();
3509        mStackSupervisor.resumeFocusedStackTopActivityLocked();
3510        return r;
3511    }
3512
3513    void finishAllActivitiesLocked(boolean immediately) {
3514        boolean noActivitiesInStack = true;
3515        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3516            final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3517            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3518                final ActivityRecord r = activities.get(activityNdx);
3519                noActivitiesInStack = false;
3520                if (r.finishing && !immediately) {
3521                    continue;
3522                }
3523                Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
3524                finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3525            }
3526        }
3527        if (noActivitiesInStack) {
3528            mActivityContainer.onTaskListEmptyLocked();
3529        }
3530    }
3531
3532    final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3533        // Basic case: for simple app-centric recents, we need to recreate
3534        // the task if the affinity has changed.
3535        if (srec == null || srec.task.affinity == null ||
3536                !srec.task.affinity.equals(destAffinity)) {
3537            return true;
3538        }
3539        // Document-centric case: an app may be split in to multiple documents;
3540        // they need to re-create their task if this current activity is the root
3541        // of a document, unless simply finishing it will return them to the the
3542        // correct app behind.
3543        if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3544                && srec.task.getBaseIntent().isDocument()) {
3545            // Okay, this activity is at the root of its task.  What to do, what to do...
3546            if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3547                // Finishing won't return to an application, so we need to recreate.
3548                return true;
3549            }
3550            // We now need to get the task below it to determine what to do.
3551            int taskIdx = mTaskHistory.indexOf(srec.task);
3552            if (taskIdx <= 0) {
3553                Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3554                return false;
3555            }
3556            if (taskIdx == 0) {
3557                // At the bottom of the stack, nothing to go back to.
3558                return true;
3559            }
3560            TaskRecord prevTask = mTaskHistory.get(taskIdx);
3561            if (!srec.task.affinity.equals(prevTask.affinity)) {
3562                // These are different apps, so need to recreate.
3563                return true;
3564            }
3565        }
3566        return false;
3567    }
3568
3569    final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
3570            Intent resultData) {
3571        final TaskRecord task = srec.task;
3572        final ArrayList<ActivityRecord> activities = task.mActivities;
3573        final int start = activities.indexOf(srec);
3574        if (!mTaskHistory.contains(task) || (start < 0)) {
3575            return false;
3576        }
3577        int finishTo = start - 1;
3578        ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
3579        boolean foundParentInTask = false;
3580        final ComponentName dest = destIntent.getComponent();
3581        if (start > 0 && dest != null) {
3582            for (int i = finishTo; i >= 0; i--) {
3583                ActivityRecord r = activities.get(i);
3584                if (r.info.packageName.equals(dest.getPackageName()) &&
3585                        r.info.name.equals(dest.getClassName())) {
3586                    finishTo = i;
3587                    parent = r;
3588                    foundParentInTask = true;
3589                    break;
3590                }
3591            }
3592        }
3593
3594        IActivityController controller = mService.mController;
3595        if (controller != null) {
3596            ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3597            if (next != null) {
3598                // ask watcher if this is allowed
3599                boolean resumeOK = true;
3600                try {
3601                    resumeOK = controller.activityResuming(next.packageName);
3602                } catch (RemoteException e) {
3603                    mService.mController = null;
3604                    Watchdog.getInstance().setActivityController(null);
3605                }
3606
3607                if (!resumeOK) {
3608                    return false;
3609                }
3610            }
3611        }
3612        final long origId = Binder.clearCallingIdentity();
3613        for (int i = start; i > finishTo; i--) {
3614            ActivityRecord r = activities.get(i);
3615            requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
3616            // Only return the supplied result for the first activity finished
3617            resultCode = Activity.RESULT_CANCELED;
3618            resultData = null;
3619        }
3620
3621        if (parent != null && foundParentInTask) {
3622            final int parentLaunchMode = parent.info.launchMode;
3623            final int destIntentFlags = destIntent.getFlags();
3624            if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3625                    parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3626                    parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3627                    (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
3628                parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3629                        srec.packageName);
3630            } else {
3631                try {
3632                    ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3633                            destIntent.getComponent(), 0, srec.userId);
3634                    int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3635                            destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3636                            null, parent.appToken, null, 0, -1, parent.launchedFromUid,
3637                            parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3638                            false, true, null, null, null);
3639                    foundParentInTask = res == ActivityManager.START_SUCCESS;
3640                } catch (RemoteException e) {
3641                    foundParentInTask = false;
3642                }
3643                requestFinishActivityLocked(parent.appToken, resultCode,
3644                        resultData, "navigate-top", true);
3645            }
3646        }
3647        Binder.restoreCallingIdentity(origId);
3648        return foundParentInTask;
3649    }
3650    /**
3651     * Perform the common clean-up of an activity record.  This is called both
3652     * as part of destroyActivityLocked() (when destroying the client-side
3653     * representation) and cleaning things up as a result of its hosting
3654     * processing going away, in which case there is no remaining client-side
3655     * state to destroy so only the cleanup here is needed.
3656     *
3657     * Note: Call before #removeActivityFromHistoryLocked.
3658     */
3659    final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3660            boolean setState) {
3661        if (mResumedActivity == r) {
3662            mResumedActivity = null;
3663        }
3664        if (mPausingActivity == r) {
3665            mPausingActivity = null;
3666        }
3667        mService.resetFocusedActivityIfNeededLocked(r);
3668
3669        r.deferRelaunchUntilPaused = false;
3670        r.frozenBeforeDestroy = false;
3671
3672        if (setState) {
3673            if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
3674            r.state = ActivityState.DESTROYED;
3675            if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
3676            r.app = null;
3677        }
3678
3679        // Make sure this record is no longer in the pending finishes list.
3680        // This could happen, for example, if we are trimming activities
3681        // down to the max limit while they are still waiting to finish.
3682        mStackSupervisor.mFinishingActivities.remove(r);
3683        mStackSupervisor.mWaitingVisibleActivities.remove(r);
3684
3685        // Remove any pending results.
3686        if (r.finishing && r.pendingResults != null) {
3687            for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3688                PendingIntentRecord rec = apr.get();
3689                if (rec != null) {
3690                    mService.cancelIntentSenderLocked(rec, false);
3691                }
3692            }
3693            r.pendingResults = null;
3694        }
3695
3696        if (cleanServices) {
3697            cleanUpActivityServicesLocked(r);
3698        }
3699
3700        // Get rid of any pending idle timeouts.
3701        removeTimeoutsForActivityLocked(r);
3702        if (getVisibleBehindActivity() == r) {
3703            mStackSupervisor.requestVisibleBehindLocked(r, false);
3704        }
3705    }
3706
3707    private void removeTimeoutsForActivityLocked(ActivityRecord r) {
3708        mStackSupervisor.removeTimeoutsForActivityLocked(r);
3709        mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3710        mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
3711        mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
3712        r.finishLaunchTickingLocked();
3713    }
3714
3715    private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
3716        mStackSupervisor.removeChildActivityContainers(r);
3717        finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
3718        r.makeFinishingLocked();
3719        if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3720                "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3721
3722        r.takeFromHistory();
3723        removeTimeoutsForActivityLocked(r);
3724        if (DEBUG_STATES) Slog.v(TAG_STATES,
3725                "Moving to DESTROYED: " + r + " (removed from history)");
3726        r.state = ActivityState.DESTROYED;
3727        if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
3728        r.app = null;
3729        mWindowManager.removeAppToken(r.appToken);
3730        if (VALIDATE_TOKENS) {
3731            validateAppTokensLocked();
3732        }
3733        final TaskRecord task = r.task;
3734        if (task != null && task.removeActivity(r)) {
3735            if (DEBUG_STACK) Slog.i(TAG_STACK,
3736                    "removeActivityFromHistoryLocked: last activity removed from " + this);
3737            if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
3738                    task.isOverHomeStack()) {
3739                mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
3740            }
3741            removeTask(task, reason);
3742        }
3743        cleanUpActivityServicesLocked(r);
3744        r.removeUriPermissionsLocked();
3745    }
3746
3747    /**
3748     * Perform clean-up of service connections in an activity record.
3749     */
3750    final void cleanUpActivityServicesLocked(ActivityRecord r) {
3751        // Throw away any services that have been bound by this activity.
3752        if (r.connections != null) {
3753            Iterator<ConnectionRecord> it = r.connections.iterator();
3754            while (it.hasNext()) {
3755                ConnectionRecord c = it.next();
3756                mService.mServices.removeConnectionLocked(c, null, r);
3757            }
3758            r.connections = null;
3759        }
3760    }
3761
3762    final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
3763        Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
3764        msg.obj = new ScheduleDestroyArgs(owner, reason);
3765        mHandler.sendMessage(msg);
3766    }
3767
3768    final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
3769        boolean lastIsOpaque = false;
3770        boolean activityRemoved = false;
3771        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3772            final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3773            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3774                final ActivityRecord r = activities.get(activityNdx);
3775                if (r.finishing) {
3776                    continue;
3777                }
3778                if (r.fullscreen) {
3779                    lastIsOpaque = true;
3780                }
3781                if (owner != null && r.app != owner) {
3782                    continue;
3783                }
3784                if (!lastIsOpaque) {
3785                    continue;
3786                }
3787                if (r.isDestroyable()) {
3788                    if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
3789                            + " resumed=" + mResumedActivity
3790                            + " pausing=" + mPausingActivity + " for reason " + reason);
3791                    if (destroyActivityLocked(r, true, reason)) {
3792                        activityRemoved = true;
3793                    }
3794                }
3795            }
3796        }
3797        if (activityRemoved) {
3798            mStackSupervisor.resumeFocusedStackTopActivityLocked();
3799        }
3800    }
3801
3802    final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3803        if (r.isDestroyable()) {
3804            if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3805                    "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
3806                    + " pausing=" + mPausingActivity + " for reason " + reason);
3807            return destroyActivityLocked(r, true, reason);
3808        }
3809        return false;
3810    }
3811
3812    final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3813            String reason) {
3814        // Iterate over tasks starting at the back (oldest) first.
3815        if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3816        int maxTasks = tasks.size() / 4;
3817        if (maxTasks < 1) {
3818            maxTasks = 1;
3819        }
3820        int numReleased = 0;
3821        for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3822            final TaskRecord task = mTaskHistory.get(taskNdx);
3823            if (!tasks.contains(task)) {
3824                continue;
3825            }
3826            if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
3827            int curNum = 0;
3828            final ArrayList<ActivityRecord> activities = task.mActivities;
3829            for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3830                final ActivityRecord activity = activities.get(actNdx);
3831                if (activity.app == app && activity.isDestroyable()) {
3832                    if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
3833                            + " in state " + activity.state + " resumed=" + mResumedActivity
3834                            + " pausing=" + mPausingActivity + " for reason " + reason);
3835                    destroyActivityLocked(activity, true, reason);
3836                    if (activities.get(actNdx) != activity) {
3837                        // Was removed from list, back up so we don't miss the next one.
3838                        actNdx--;
3839                    }
3840                    curNum++;
3841                }
3842            }
3843            if (curNum > 0) {
3844                numReleased += curNum;
3845                maxTasks--;
3846                if (mTaskHistory.get(taskNdx) != task) {
3847                    // The entire task got removed, back up so we don't miss the next one.
3848                    taskNdx--;
3849                }
3850            }
3851        }
3852        if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3853                "Done releasing: did " + numReleased + " activities");
3854        return numReleased;
3855    }
3856
3857    /**
3858     * Destroy the current CLIENT SIDE instance of an activity.  This may be
3859     * called both when actually finishing an activity, or when performing
3860     * a configuration switch where we destroy the current client-side object
3861     * but then create a new client-side object for this same HistoryRecord.
3862     */
3863    final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
3864        if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3865                "Removing activity from " + reason + ": token=" + r
3866                        + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3867        EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
3868                r.userId, System.identityHashCode(r),
3869                r.task.taskId, r.shortComponentName, reason);
3870
3871        boolean removedFromHistory = false;
3872
3873        cleanUpActivityLocked(r, false, false);
3874
3875        final boolean hadApp = r.app != null;
3876
3877        if (hadApp) {
3878            if (removeFromApp) {
3879                r.app.activities.remove(r);
3880                if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3881                    mService.mHeavyWeightProcess = null;
3882                    mService.mHandler.sendEmptyMessage(
3883                            ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3884                }
3885                if (r.app.activities.isEmpty()) {
3886                    // Update any services we are bound to that might care about whether
3887                    // their client may have activities.
3888                    mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
3889                    // No longer have activities, so update LRU list and oom adj.
3890                    mService.updateLruProcessLocked(r.app, false, null);
3891                    mService.updateOomAdjLocked();
3892                }
3893            }
3894
3895            boolean skipDestroy = false;
3896
3897            try {
3898                if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
3899                r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
3900                        r.configChangeFlags);
3901            } catch (Exception e) {
3902                // We can just ignore exceptions here...  if the process
3903                // has crashed, our death notification will clean things
3904                // up.
3905                //Slog.w(TAG, "Exception thrown during finish", e);
3906                if (r.finishing) {
3907                    removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
3908                    removedFromHistory = true;
3909                    skipDestroy = true;
3910                }
3911            }
3912
3913            r.nowVisible = false;
3914
3915            // If the activity is finishing, we need to wait on removing it
3916            // from the list to give it a chance to do its cleanup.  During
3917            // that time it may make calls back with its token so we need to
3918            // be able to find it on the list and so we don't want to remove
3919            // it from the list yet.  Otherwise, we can just immediately put
3920            // it in the destroyed state since we are not removing it from the
3921            // list.
3922            if (r.finishing && !skipDestroy) {
3923                if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
3924                        + " (destroy requested)");
3925                r.state = ActivityState.DESTROYING;
3926                Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
3927                mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3928            } else {
3929                if (DEBUG_STATES) Slog.v(TAG_STATES,
3930                        "Moving to DESTROYED: " + r + " (destroy skipped)");
3931                r.state = ActivityState.DESTROYED;
3932                if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
3933                r.app = null;
3934            }
3935        } else {
3936            // remove this record from the history.
3937            if (r.finishing) {
3938                removeActivityFromHistoryLocked(r, reason + " hadNoApp");
3939                removedFromHistory = true;
3940            } else {
3941                if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
3942                r.state = ActivityState.DESTROYED;
3943                if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
3944                r.app = null;
3945            }
3946        }
3947
3948        r.configChangeFlags = 0;
3949
3950        if (!mLRUActivities.remove(r) && hadApp) {
3951            Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3952        }
3953
3954        return removedFromHistory;
3955    }
3956
3957    final void activityDestroyedLocked(IBinder token, String reason) {
3958        final long origId = Binder.clearCallingIdentity();
3959        try {
3960            ActivityRecord r = ActivityRecord.forTokenLocked(token);
3961            if (r != null) {
3962                mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
3963            }
3964            if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
3965
3966            if (isInStackLocked(r) != null) {
3967                if (r.state == ActivityState.DESTROYING) {
3968                    cleanUpActivityLocked(r, true, false);
3969                    removeActivityFromHistoryLocked(r, reason);
3970                }
3971            }
3972            mStackSupervisor.resumeFocusedStackTopActivityLocked();
3973        } finally {
3974            Binder.restoreCallingIdentity(origId);
3975        }
3976    }
3977
3978    void releaseBackgroundResources(ActivityRecord r) {
3979        if (hasVisibleBehindActivity() &&
3980                !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
3981            if (r == topRunningActivityLocked()
3982                    && getStackVisibilityLocked(null) == STACK_VISIBLE) {
3983                // Don't release the top activity if it has requested to run behind the next
3984                // activity and the stack is currently visible.
3985                return;
3986            }
3987            if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
3988                    mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
3989                    " thread=" + r.app.thread);
3990            if (r != null && r.app != null && r.app.thread != null) {
3991                try {
3992                    r.app.thread.scheduleCancelVisibleBehind(r.appToken);
3993                } catch (RemoteException e) {
3994                }
3995                mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
3996            } else {
3997                Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
3998                backgroundResourcesReleased();
3999            }
4000        }
4001    }
4002
4003    final void backgroundResourcesReleased() {
4004        mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
4005        final ActivityRecord r = getVisibleBehindActivity();
4006        if (r != null) {
4007            mStackSupervisor.mStoppingActivities.add(r);
4008            setVisibleBehindActivity(null);
4009            mStackSupervisor.scheduleIdleTimeoutLocked(null);
4010        }
4011        mStackSupervisor.resumeFocusedStackTopActivityLocked();
4012    }
4013
4014    boolean hasVisibleBehindActivity() {
4015        return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
4016    }
4017
4018    void setVisibleBehindActivity(ActivityRecord r) {
4019        if (isAttached()) {
4020            mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
4021        }
4022    }
4023
4024    ActivityRecord getVisibleBehindActivity() {
4025        return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
4026    }
4027
4028    private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
4029            ProcessRecord app, String listName) {
4030        int i = list.size();
4031        if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4032            "Removing app " + app + " from list " + listName + " with " + i + " entries");
4033        while (i > 0) {
4034            i--;
4035            ActivityRecord r = list.get(i);
4036            if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
4037            if (r.app == app) {
4038                if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
4039                list.remove(i);
4040                removeTimeoutsForActivityLocked(r);
4041            }
4042        }
4043    }
4044
4045    boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
4046        removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
4047        removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
4048                "mStoppingActivities");
4049        removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
4050                "mGoingToSleepActivities");
4051        removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
4052                "mWaitingVisibleActivities");
4053        removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
4054                "mFinishingActivities");
4055
4056        boolean hasVisibleActivities = false;
4057
4058        // Clean out the history list.
4059        int i = numActivities();
4060        if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4061                "Removing app " + app + " from history with " + i + " entries");
4062        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4063            final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4064            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4065                final ActivityRecord r = activities.get(activityNdx);
4066                --i;
4067                if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4068                        "Record #" + i + " " + r + ": app=" + r.app);
4069                if (r.app == app) {
4070                    if (r.visible) {
4071                        hasVisibleActivities = true;
4072                    }
4073                    final boolean remove;
4074                    if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
4075                        // Don't currently have state for the activity, or
4076                        // it is finishing -- always remove it.
4077                        remove = true;
4078                    } else if (!r.visible && r.launchCount > 2 &&
4079                            r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
4080                        // We have launched this activity too many times since it was
4081                        // able to run, so give up and remove it.
4082                        // (Note if the activity is visible, we don't remove the record.
4083                        // We leave the dead window on the screen but the process will
4084                        // not be restarted unless user explicitly tap on it.)
4085                        remove = true;
4086                    } else {
4087                        // The process may be gone, but the activity lives on!
4088                        remove = false;
4089                    }
4090                    if (remove) {
4091                        if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
4092                                "Removing activity " + r + " from stack at " + i
4093                                + ": haveState=" + r.haveState
4094                                + " stateNotNeeded=" + r.stateNotNeeded
4095                                + " finishing=" + r.finishing
4096                                + " state=" + r.state + " callers=" + Debug.getCallers(5));
4097                        if (!r.finishing) {
4098                            Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
4099                            EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
4100                                    r.userId, System.identityHashCode(r),
4101                                    r.task.taskId, r.shortComponentName,
4102                                    "proc died without state saved");
4103                            if (r.state == ActivityState.RESUMED) {
4104                                mService.updateUsageStats(r, false);
4105                            }
4106                        }
4107                    } else {
4108                        // We have the current state for this activity, so
4109                        // it can be restarted later when needed.
4110                        if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
4111                        if (DEBUG_APP) Slog.v(TAG_APP,
4112                                "Clearing app during removeHistory for activity " + r);
4113                        r.app = null;
4114                        // Set nowVisible to previous visible state. If the app was visible while
4115                        // it died, we leave the dead window on screen so it's basically visible.
4116                        // This is needed when user later tap on the dead window, we need to stop
4117                        // other apps when user transfers focus to the restarted activity.
4118                        r.nowVisible = r.visible;
4119                        if (!r.haveState) {
4120                            if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
4121                                    "App died, clearing saved state of " + r);
4122                            r.icicle = null;
4123                        }
4124                    }
4125                    cleanUpActivityLocked(r, true, true);
4126                    if (remove) {
4127                        removeActivityFromHistoryLocked(r, "appDied");
4128                    }
4129                }
4130            }
4131        }
4132
4133        return hasVisibleActivities;
4134    }
4135
4136    final void updateTransitLocked(int transit, ActivityOptions options) {
4137        if (options != null) {
4138            ActivityRecord r = topRunningActivityLocked();
4139            if (r != null && r.state != ActivityState.RESUMED) {
4140                r.updateOptionsLocked(options);
4141            } else {
4142                ActivityOptions.abort(options);
4143            }
4144        }
4145        mWindowManager.prepareAppTransition(transit, false);
4146    }
4147
4148    void updateTaskMovement(TaskRecord task, boolean toFront) {
4149        if (task.isPersistable) {
4150            task.mLastTimeMoved = System.currentTimeMillis();
4151            // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
4152            // recently will be most negative, tasks sent to the bottom before that will be less
4153            // negative. Similarly for recent tasks moved to the top which will be most positive.
4154            if (!toFront) {
4155                task.mLastTimeMoved *= -1;
4156            }
4157        }
4158        mStackSupervisor.invalidateTaskLayers();
4159    }
4160
4161    void moveHomeStackTaskToTop(int homeStackTaskType) {
4162        final int top = mTaskHistory.size() - 1;
4163        for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
4164            final TaskRecord task = mTaskHistory.get(taskNdx);
4165            if (task.taskType == homeStackTaskType) {
4166                if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
4167                        "moveHomeStackTaskToTop: moving " + task);
4168                mTaskHistory.remove(taskNdx);
4169                mTaskHistory.add(top, task);
4170                updateTaskMovement(task, true);
4171                return;
4172            }
4173        }
4174    }
4175
4176    final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
4177            AppTimeTracker timeTracker, String reason) {
4178        if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
4179
4180        final int numTasks = mTaskHistory.size();
4181        final int index = mTaskHistory.indexOf(tr);
4182        if (numTasks == 0 || index < 0)  {
4183            // nothing to do!
4184            if (noAnimation) {
4185                ActivityOptions.abort(options);
4186            } else {
4187                updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
4188            }
4189            return;
4190        }
4191
4192        if (timeTracker != null) {
4193            // The caller wants a time tracker associated with this task.
4194            for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4195                tr.mActivities.get(i).appTimeTracker = timeTracker;
4196            }
4197        }
4198
4199        // Shift all activities with this task up to the top
4200        // of the stack, keeping them in the same internal order.
4201        insertTaskAtTop(tr, null);
4202
4203        // Don't refocus if invisible to current user
4204        ActivityRecord top = tr.getTopActivity();
4205        if (!okToShowLocked(top)) {
4206            addRecentActivityLocked(top);
4207            ActivityOptions.abort(options);
4208            return;
4209        }
4210
4211        // Set focus to the top running activity of this stack.
4212        ActivityRecord r = topRunningActivityLocked();
4213        mService.setFocusedActivityLocked(r, reason);
4214
4215        if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
4216        if (noAnimation) {
4217            mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
4218            if (r != null) {
4219                mNoAnimActivities.add(r);
4220            }
4221            ActivityOptions.abort(options);
4222        } else {
4223            updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
4224        }
4225
4226        mStackSupervisor.resumeFocusedStackTopActivityLocked();
4227        EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
4228
4229        if (VALIDATE_TOKENS) {
4230            validateAppTokensLocked();
4231        }
4232    }
4233
4234    /**
4235     * Worker method for rearranging history stack. Implements the function of moving all
4236     * activities for a specific task (gathering them if disjoint) into a single group at the
4237     * bottom of the stack.
4238     *
4239     * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4240     * to premeptively cancel the move.
4241     *
4242     * @param taskId The taskId to collect and move to the bottom.
4243     * @return Returns true if the move completed, false if not.
4244     */
4245    final boolean moveTaskToBackLocked(int taskId) {
4246        final TaskRecord tr = taskForIdLocked(taskId);
4247        if (tr == null) {
4248            Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4249            return false;
4250        }
4251
4252        Slog.i(TAG, "moveTaskToBack: " + tr);
4253        mStackSupervisor.removeLockedTaskLocked(tr);
4254
4255        // If we have a watcher, preflight the move before committing to it.  First check
4256        // for *other* available tasks, but if none are available, then try again allowing the
4257        // current task to be selected.
4258        if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
4259            ActivityRecord next = topRunningActivityLocked(null, taskId);
4260            if (next == null) {
4261                next = topRunningActivityLocked(null, 0);
4262            }
4263            if (next != null) {
4264                // ask watcher if this is allowed
4265                boolean moveOK = true;
4266                try {
4267                    moveOK = mService.mController.activityResuming(next.packageName);
4268                } catch (RemoteException e) {
4269                    mService.mController = null;
4270                    Watchdog.getInstance().setActivityController(null);
4271                }
4272                if (!moveOK) {
4273                    return false;
4274                }
4275            }
4276        }
4277
4278        if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
4279
4280        if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
4281            // For the case where we are moving the home task back and there is an activity visible
4282            // behind it on the fullscreen stack, we want to move the focus to the visible behind
4283            // activity to maintain order with what the user is seeing.
4284            final ActivityStack fullscreenStack =
4285                    mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4286            if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4287                final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
4288                mService.setFocusedActivityLocked(visibleBehind, "moveTaskToBack");
4289                mStackSupervisor.resumeFocusedStackTopActivityLocked();
4290                return true;
4291            }
4292        }
4293
4294        boolean prevIsHome = false;
4295
4296        // If true, we should resume the home activity next if the task we are moving to the
4297        // back is over the home stack. We force to false if the task we are moving to back
4298        // is the home task and we don't want it resumed after moving to the back.
4299        final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4300        if (canGoHome) {
4301            final TaskRecord nextTask = getNextTask(tr);
4302            if (nextTask != null) {
4303                nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4304            } else {
4305                prevIsHome = true;
4306            }
4307        }
4308        mTaskHistory.remove(tr);
4309        mTaskHistory.add(0, tr);
4310        updateTaskMovement(tr, false);
4311
4312        // There is an assumption that moving a task to the back moves it behind the home activity.
4313        // We make sure here that some activity in the stack will launch home.
4314        int numTasks = mTaskHistory.size();
4315        for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4316            final TaskRecord task = mTaskHistory.get(taskNdx);
4317            if (task.isOverHomeStack()) {
4318                break;
4319            }
4320            if (taskNdx == 1) {
4321                // Set the last task before tr to go to home.
4322                task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4323            }
4324        }
4325
4326        mWindowManager.prepareAppTransition(TRANSIT_TASK_TO_BACK, false);
4327        mWindowManager.moveTaskToBottom(taskId);
4328
4329        if (VALIDATE_TOKENS) {
4330            validateAppTokensLocked();
4331        }
4332
4333        final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
4334        if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
4335            if (!mService.mBooting && !mService.mBooted) {
4336                // Not ready yet!
4337                return false;
4338            }
4339            final int taskToReturnTo = tr.getTaskToReturnTo();
4340            tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
4341            return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
4342        }
4343
4344        mStackSupervisor.resumeFocusedStackTopActivityLocked();
4345        return true;
4346    }
4347
4348    static final void logStartActivity(int tag, ActivityRecord r,
4349            TaskRecord task) {
4350        final Uri data = r.intent.getData();
4351        final String strData = data != null ? data.toSafeString() : null;
4352
4353        EventLog.writeEvent(tag,
4354                r.userId, System.identityHashCode(r), task.taskId,
4355                r.shortComponentName, r.intent.getAction(),
4356                r.intent.getType(), strData, r.intent.getFlags());
4357    }
4358
4359    /**
4360     * Make sure the given activity matches the current configuration.  Returns
4361     * false if the activity had to be destroyed.  Returns true if the
4362     * configuration is the same, or the activity will remain running as-is
4363     * for whatever reason.  Ensures the HistoryRecord is updated with the
4364     * correct configuration and all other bookkeeping is handled.
4365     */
4366    final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4367            boolean preserveWindow) {
4368        if (mConfigWillChange) {
4369            if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4370                    "Skipping config check (will change): " + r);
4371            return true;
4372        }
4373
4374        if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4375                "Ensuring correct configuration: " + r);
4376
4377        // Short circuit: if the two configurations are equal (the common case), then there is
4378        // nothing to do.
4379        final Configuration newConfig = mService.mConfiguration;
4380        final Configuration taskConfig = r.task.mOverrideConfig;
4381        if (r.configuration.equals(newConfig)
4382                && r.taskConfigOverride.equals(taskConfig)
4383                && !r.forceNewConfig) {
4384            if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4385                    "Configuration unchanged in " + r);
4386            return true;
4387        }
4388
4389        // We don't worry about activities that are finishing.
4390        if (r.finishing) {
4391            if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4392                    "Configuration doesn't matter in finishing " + r);
4393            r.stopFreezingScreenLocked(false);
4394            return true;
4395        }
4396
4397        // Okay we now are going to make this activity have the new config.
4398        // But then we need to figure out how it needs to deal with that.
4399        final Configuration oldConfig = r.configuration;
4400        final Configuration oldTaskOverride = r.taskConfigOverride;
4401        r.configuration = newConfig;
4402        r.taskConfigOverride = taskConfig;
4403
4404        int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
4405        final int changes = oldConfig.diff(newConfig) | taskChanges;
4406        if (changes == 0 && !r.forceNewConfig) {
4407            if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4408                    "Configuration no differences in " + r);
4409            // There are no significant differences, so we won't relaunch but should still deliver
4410            // the new configuration to the client process.
4411            r.scheduleConfigurationChanged(taskConfig, true);
4412            return true;
4413        }
4414
4415        if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4416                "Configuration changes for " + r + " ; taskChanges="
4417                        + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4418                        + Configuration.configurationDiffToString(changes));
4419
4420        // If the activity isn't currently running, just leave the new
4421        // configuration and it will pick that up next time it starts.
4422        if (r.app == null || r.app.thread == null) {
4423            if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4424                    "Configuration doesn't matter not running " + r);
4425            r.stopFreezingScreenLocked(false);
4426            r.forceNewConfig = false;
4427            return true;
4428        }
4429
4430        // Figure out how to handle the changes between the configurations.
4431        if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4432                "Checking to restart " + r.info.name + ": changed=0x"
4433                + Integer.toHexString(changes) + ", handles=0x"
4434                + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4435                + ", taskConfig=" + taskConfig);
4436
4437        if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
4438            // Aha, the activity isn't handling the change, so DIE DIE DIE.
4439            r.configChangeFlags |= changes;
4440            r.startFreezingScreenLocked(r.app, globalChanges);
4441            r.forceNewConfig = false;
4442            preserveWindow &= isResizeOnlyChange(changes);
4443            if (r.app == null || r.app.thread == null) {
4444                if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4445                        "Config is destroying non-running " + r);
4446                destroyActivityLocked(r, true, "config");
4447            } else if (r.state == ActivityState.PAUSING) {
4448                // A little annoying: we are waiting for this activity to finish pausing. Let's not
4449                // do anything now, but just flag that it needs to be restarted when done pausing.
4450                if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4451                        "Config is skipping already pausing " + r);
4452                r.deferRelaunchUntilPaused = true;
4453                r.preserveWindowOnDeferredRelaunch = preserveWindow;
4454                return true;
4455            } else if (r.state == ActivityState.RESUMED) {
4456                // Try to optimize this case: the configuration is changing and we need to restart
4457                // the top, resumed activity. Instead of doing the normal handshaking, just say
4458                // "restart!".
4459                if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4460                        "Config is relaunching resumed " + r);
4461
4462                if (DEBUG_STATES && !r.visible) {
4463                    Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + r
4464                            + " called by " + Debug.getCallers(4));
4465                }
4466
4467                relaunchActivityLocked(r, r.configChangeFlags, true, preserveWindow);
4468            } else {
4469                if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4470                        "Config is relaunching non-resumed " + r);
4471                relaunchActivityLocked(r, r.configChangeFlags, false, preserveWindow);
4472            }
4473
4474            // All done...  tell the caller we weren't able to keep this activity around.
4475            return false;
4476        }
4477
4478        // Default case: the activity can handle this new configuration, so hand it over.
4479        // NOTE: We only forward the task override configuration as the system level configuration
4480        // changes is always sent to all processes when they happen so it can just use whatever
4481        // system level configuration it last got.
4482        r.scheduleConfigurationChanged(taskConfig, true);
4483        r.stopFreezingScreenLocked(false);
4484
4485        return true;
4486    }
4487
4488    private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4489            Configuration oldTaskOverride) {
4490
4491        // If we went from full-screen to non-full-screen, make sure to use the correct
4492        // configuration task diff, so the diff stays as small as possible.
4493        if (Configuration.EMPTY.equals(oldTaskOverride)
4494                && !Configuration.EMPTY.equals(taskConfig)) {
4495            oldTaskOverride = record.task.extractOverrideConfig(record.configuration);
4496        }
4497
4498        // Conversely, do the same when going the other direction.
4499        if (Configuration.EMPTY.equals(taskConfig)
4500                && !Configuration.EMPTY.equals(oldTaskOverride)) {
4501            taskConfig = record.task.extractOverrideConfig(record.configuration);
4502        }
4503
4504        // Determine what has changed.  May be nothing, if this is a config
4505        // that has come back from the app after going idle.  In that case
4506        // we just want to leave the official config object now in the
4507        // activity and do nothing else.
4508        int taskChanges = oldTaskOverride.diff(taskConfig);
4509        // We don't want to use size changes if they don't cross boundaries that are important to
4510        // the app.
4511        if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
4512            final boolean crosses = record.crossesHorizontalSizeThreshold(
4513                    oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4514                    || record.crossesVerticalSizeThreshold(
4515                    oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4516            if (!crosses) {
4517                taskChanges &= ~CONFIG_SCREEN_SIZE;
4518            }
4519        }
4520        if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
4521            final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4522            final int newSmallest = taskConfig.smallestScreenWidthDp;
4523            if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
4524                taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
4525            }
4526        }
4527        return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4528    }
4529
4530    private static int catchConfigChangesFromUnset(Configuration taskConfig,
4531            Configuration oldTaskOverride, int taskChanges) {
4532        if (taskChanges == 0) {
4533            // {@link Configuration#diff} doesn't catch changes from unset values.
4534            // Check for changes we care about.
4535            if (oldTaskOverride.orientation != taskConfig.orientation) {
4536                taskChanges |= CONFIG_ORIENTATION;
4537            }
4538            // We want to explicitly track situations where the size configuration goes from
4539            // undefined to defined. We don't care about crossing the threshold in that case,
4540            // because there is no threshold.
4541            final int oldHeight = oldTaskOverride.screenHeightDp;
4542            final int newHeight = taskConfig.screenHeightDp;
4543            final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4544            if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4545                    || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
4546                taskChanges |= CONFIG_SCREEN_SIZE;
4547            }
4548            final int oldWidth = oldTaskOverride.screenWidthDp;
4549            final int newWidth = taskConfig.screenWidthDp;
4550            final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4551            if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4552                    || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
4553                taskChanges |= CONFIG_SCREEN_SIZE;
4554            }
4555            final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4556            final int newSmallest = taskConfig.smallestScreenWidthDp;
4557            final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4558            if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4559                    || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
4560                taskChanges |= CONFIG_SMALLEST_SCREEN_SIZE;
4561            }
4562            final int oldLayout = oldTaskOverride.screenLayout;
4563            final int newLayout = taskConfig.screenLayout;
4564            if ((oldLayout == SCREENLAYOUT_UNDEFINED && newLayout != SCREENLAYOUT_UNDEFINED)
4565                || (oldLayout != SCREENLAYOUT_UNDEFINED && newLayout == SCREENLAYOUT_UNDEFINED)) {
4566                taskChanges |= CONFIG_SCREEN_LAYOUT;
4567            }
4568        }
4569        return taskChanges;
4570    }
4571
4572    private static boolean isResizeOnlyChange(int change) {
4573        return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
4574                | CONFIG_SCREEN_LAYOUT)) == 0;
4575    }
4576
4577    private void relaunchActivityLocked(
4578            ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4579        if (mService.mSuppressResizeConfigChanges && preserveWindow) {
4580            r.configChangeFlags = 0;
4581            return;
4582        }
4583
4584        List<ResultInfo> results = null;
4585        List<ReferrerIntent> newIntents = null;
4586        if (andResume) {
4587            results = r.results;
4588            newIntents = r.newIntents;
4589        }
4590        if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4591                "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
4592                + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
4593        EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
4594                : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
4595                r.task.taskId, r.shortComponentName);
4596
4597        r.startFreezingScreenLocked(r.app, 0);
4598
4599        mStackSupervisor.removeChildActivityContainers(r);
4600
4601        try {
4602            if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4603                    "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r
4604                    + " callers=" + Debug.getCallers(6));
4605            r.forceNewConfig = false;
4606            mStackSupervisor.activityRelaunchingLocked(r);
4607            r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4608                    !andResume, new Configuration(mService.mConfiguration),
4609                    new Configuration(r.task.mOverrideConfig), preserveWindow);
4610            // Note: don't need to call pauseIfSleepingLocked() here, because
4611            // the caller will only pass in 'andResume' if this activity is
4612            // currently resumed, which implies we aren't sleeping.
4613        } catch (RemoteException e) {
4614            if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
4615        }
4616
4617        if (andResume) {
4618            if (DEBUG_STATES) {
4619                Slog.d(TAG_STATES, "Resumed after relaunch " + r);
4620            }
4621            r.state = ActivityState.RESUMED;
4622            // Relaunch-resume could happen either when the app is already in the front,
4623            // or while it's being brought to front. In the latter case, it's marked RESUMED
4624            // but not yet visible (or stopped). We need to complete the resume here as the
4625            // code in resumeTopActivityInnerLocked to complete the resume might be skipped.
4626            if (!r.visible || r.stopped) {
4627                mWindowManager.setAppVisibility(r.appToken, true);
4628                completeResumeLocked(r);
4629            } else {
4630                r.results = null;
4631                r.newIntents = null;
4632            }
4633        } else {
4634            mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4635            r.state = ActivityState.PAUSED;
4636        }
4637
4638        r.configChangeFlags = 0;
4639        r.deferRelaunchUntilPaused = false;
4640        r.preserveWindowOnDeferredRelaunch = false;
4641    }
4642
4643    boolean willActivityBeVisibleLocked(IBinder token) {
4644        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4645            final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4646            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4647                final ActivityRecord r = activities.get(activityNdx);
4648                if (r.appToken == token) {
4649                    return true;
4650                }
4651                if (r.fullscreen && !r.finishing) {
4652                    return false;
4653                }
4654            }
4655        }
4656        final ActivityRecord r = ActivityRecord.forTokenLocked(token);
4657        if (r == null) {
4658            return false;
4659        }
4660        if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4661                + " would have returned true for r=" + r);
4662        return !r.finishing;
4663    }
4664
4665    void closeSystemDialogsLocked() {
4666        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4667            final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4668            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4669                final ActivityRecord r = activities.get(activityNdx);
4670                if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
4671                    finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
4672                }
4673            }
4674        }
4675    }
4676
4677    boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4678            boolean doit, boolean evenPersistent, int userId) {
4679        boolean didSomething = false;
4680        TaskRecord lastTask = null;
4681        ComponentName homeActivity = null;
4682        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4683            final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4684            int numActivities = activities.size();
4685            for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4686                ActivityRecord r = activities.get(activityNdx);
4687                final boolean sameComponent =
4688                        (r.packageName.equals(packageName) && (filterByClasses == null
4689                                || filterByClasses.contains(r.realActivity.getClassName())))
4690                        || (packageName == null && r.userId == userId);
4691                if ((userId == UserHandle.USER_ALL || r.userId == userId)
4692                        && (sameComponent || r.task == lastTask)
4693                        && (r.app == null || evenPersistent || !r.app.persistent)) {
4694                    if (!doit) {
4695                        if (r.finishing) {
4696                            // If this activity is just finishing, then it is not
4697                            // interesting as far as something to stop.
4698                            continue;
4699                        }
4700                        return true;
4701                    }
4702                    if (r.isHomeActivity()) {
4703                        if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4704                            Slog.i(TAG, "Skip force-stop again " + r);
4705                            continue;
4706                        } else {
4707                            homeActivity = r.realActivity;
4708                        }
4709                    }
4710                    didSomething = true;
4711                    Slog.i(TAG, "  Force finishing activity " + r);
4712                    if (sameComponent) {
4713                        if (r.app != null) {
4714                            r.app.removed = true;
4715                        }
4716                        r.app = null;
4717                    }
4718                    lastTask = r.task;
4719                    if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4720                            true)) {
4721                        // r has been deleted from mActivities, accommodate.
4722                        --numActivities;
4723                        --activityNdx;
4724                    }
4725                }
4726            }
4727        }
4728        return didSomething;
4729    }
4730
4731    void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
4732        boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4733        boolean topTask = true;
4734        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4735            final TaskRecord task = mTaskHistory.get(taskNdx);
4736            if (task.getTopActivity() == null) {
4737                continue;
4738            }
4739            ActivityRecord r = null;
4740            ActivityRecord top = null;
4741            ActivityRecord tmp;
4742            int numActivities = 0;
4743            int numRunning = 0;
4744            final ArrayList<ActivityRecord> activities = task.mActivities;
4745            if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
4746                continue;
4747            }
4748            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4749                tmp = activities.get(activityNdx);
4750                if (tmp.finishing) {
4751                    continue;
4752                }
4753                r = tmp;
4754
4755                // Initialize state for next task if needed.
4756                if (top == null || (top.state == ActivityState.INITIALIZING)) {
4757                    top = r;
4758                    numActivities = numRunning = 0;
4759                }
4760
4761                // Add 'r' into the current task.
4762                numActivities++;
4763                if (r.app != null && r.app.thread != null) {
4764                    numRunning++;
4765                }
4766
4767                if (DEBUG_ALL) Slog.v(
4768                    TAG, r.intent.getComponent().flattenToShortString()
4769                    + ": task=" + r.task);
4770            }
4771
4772            RunningTaskInfo ci = new RunningTaskInfo();
4773            ci.id = task.taskId;
4774            ci.stackId = mStackId;
4775            ci.baseActivity = r.intent.getComponent();
4776            ci.topActivity = top.intent.getComponent();
4777            ci.lastActiveTime = task.lastActiveTime;
4778            if (focusedStack && topTask) {
4779                // Give the latest time to ensure foreground task can be sorted
4780                // at the first, because lastActiveTime of creating task is 0.
4781                ci.lastActiveTime = System.currentTimeMillis();
4782                topTask = false;
4783            }
4784
4785            if (top.task != null) {
4786                ci.description = top.task.lastDescription;
4787            }
4788            ci.numActivities = numActivities;
4789            ci.numRunning = numRunning;
4790            ci.isDockable = task.canGoInDockedStack();
4791            ci.resizeMode = task.mResizeMode;
4792            list.add(ci);
4793        }
4794    }
4795
4796    public void unhandledBackLocked() {
4797        final int top = mTaskHistory.size() - 1;
4798        if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
4799        if (top >= 0) {
4800            final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4801            int activityTop = activities.size() - 1;
4802            if (activityTop > 0) {
4803                finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4804                        "unhandled-back", true);
4805            }
4806        }
4807    }
4808
4809    /**
4810     * Reset local parameters because an app's activity died.
4811     * @param app The app of the activity that died.
4812     * @return result from removeHistoryRecordsForAppLocked.
4813     */
4814    boolean handleAppDiedLocked(ProcessRecord app) {
4815        if (mPausingActivity != null && mPausingActivity.app == app) {
4816            if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
4817                    "App died while pausing: " + mPausingActivity);
4818            mPausingActivity = null;
4819        }
4820        if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4821            mLastPausedActivity = null;
4822            mLastNoHistoryActivity = null;
4823        }
4824
4825        return removeHistoryRecordsForAppLocked(app);
4826    }
4827
4828    void handleAppCrashLocked(ProcessRecord app) {
4829        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4830            final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4831            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4832                final ActivityRecord r = activities.get(activityNdx);
4833                if (r.app == app) {
4834                    Slog.w(TAG, "  Force finishing activity "
4835                            + r.intent.getComponent().flattenToShortString());
4836                    // Force the destroy to skip right to removal.
4837                    r.app = null;
4838                    finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
4839                }
4840            }
4841        }
4842    }
4843
4844    boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
4845            boolean dumpClient, String dumpPackage, boolean needSep, String header) {
4846        boolean printed = false;
4847        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4848            final TaskRecord task = mTaskHistory.get(taskNdx);
4849            printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4850                    mTaskHistory.get(taskNdx).mActivities, "    ", "Hist", true, !dumpAll,
4851                    dumpClient, dumpPackage, needSep, header,
4852                    "    Task id #" + task.taskId + "\n" +
4853                    "    mFullscreen=" + task.mFullscreen + "\n" +
4854                    "    mBounds=" + task.mBounds + "\n" +
4855                    "    mMinimalWidth=" + task.mMinimalWidth + "\n" +
4856                    "    mMinimalHeight=" + task.mMinimalHeight + "\n" +
4857                    "    mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
4858            if (printed) {
4859                header = null;
4860            }
4861        }
4862        return printed;
4863    }
4864
4865    ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4866        ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4867
4868        if ("all".equals(name)) {
4869            for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4870                activities.addAll(mTaskHistory.get(taskNdx).mActivities);
4871            }
4872        } else if ("top".equals(name)) {
4873            final int top = mTaskHistory.size() - 1;
4874            if (top >= 0) {
4875                final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4876                int listTop = list.size() - 1;
4877                if (listTop >= 0) {
4878                    activities.add(list.get(listTop));
4879                }
4880            }
4881        } else {
4882            ItemMatcher matcher = new ItemMatcher();
4883            matcher.build(name);
4884
4885            for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4886                for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4887                    if (matcher.match(r1, r1.intent.getComponent())) {
4888                        activities.add(r1);
4889                    }
4890                }
4891            }
4892        }
4893
4894        return activities;
4895    }
4896
4897    ActivityRecord restartPackage(String packageName) {
4898        ActivityRecord starting = topRunningActivityLocked();
4899
4900        // All activities that came from the package must be
4901        // restarted as if there was a config change.
4902        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4903            final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4904            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4905                final ActivityRecord a = activities.get(activityNdx);
4906                if (a.info.packageName.equals(packageName)) {
4907                    a.forceNewConfig = true;
4908                    if (starting != null && a == starting && a.visible) {
4909                        a.startFreezingScreenLocked(starting.app,
4910                                CONFIG_SCREEN_LAYOUT);
4911                    }
4912                }
4913            }
4914        }
4915
4916        return starting;
4917    }
4918
4919    void removeTask(TaskRecord task, String reason) {
4920        removeTask(task, reason, !MOVING);
4921    }
4922
4923    /**
4924     * Removes the input task from this stack.
4925     * @param task to remove.
4926     * @param reason for removal.
4927     * @param moving task to another stack. In the case we are moving we don't want to perform
4928     *               some operations on the task like removing it from window manager or recents.
4929     */
4930    void removeTask(TaskRecord task, String reason, boolean moving) {
4931        if (!moving) {
4932            mStackSupervisor.removeLockedTaskLocked(task);
4933            mWindowManager.removeTask(task.taskId);
4934            if (!StackId.persistTaskBounds(mStackId)) {
4935                // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4936                // default configuration the next time it launches.
4937                task.updateOverrideConfiguration(null);
4938            }
4939        }
4940
4941        final ActivityRecord r = mResumedActivity;
4942        if (r != null && r.task == task) {
4943            mResumedActivity = null;
4944        }
4945
4946        final int taskNdx = mTaskHistory.indexOf(task);
4947        final int topTaskNdx = mTaskHistory.size() - 1;
4948        if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4949            final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4950            if (!nextTask.isOverHomeStack()) {
4951                nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4952            }
4953        }
4954        mTaskHistory.remove(task);
4955        updateTaskMovement(task, true);
4956
4957        if (!moving && task.mActivities.isEmpty()) {
4958            // TODO: VI what about activity?
4959            final boolean isVoiceSession = task.voiceSession != null;
4960            if (isVoiceSession) {
4961                try {
4962                    task.voiceSession.taskFinished(task.intent, task.taskId);
4963                } catch (RemoteException e) {
4964                }
4965            }
4966            if (task.autoRemoveFromRecents() || isVoiceSession) {
4967                // Task creator asked to remove this when done, or this task was a voice
4968                // interaction, so it should not remain on the recent tasks list.
4969                mRecentTasks.remove(task);
4970                task.removedFromRecents();
4971            }
4972        }
4973
4974        if (mTaskHistory.isEmpty()) {
4975            if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
4976            // We only need to adjust focused stack if this stack is in focus.
4977            if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
4978                String myReason = reason + " leftTaskHistoryEmpty";
4979                if (mFullscreen
4980                        || !adjustFocusToNextFocusableStackLocked(
4981                        task.getTaskToReturnTo(), myReason)) {
4982                    mStackSupervisor.moveHomeStackToFront(myReason);
4983                }
4984            }
4985            if (mStacks != null) {
4986                mStacks.remove(this);
4987                mStacks.add(0, this);
4988            }
4989            if (!isHomeStack()) {
4990                mActivityContainer.onTaskListEmptyLocked();
4991            }
4992        }
4993
4994        task.stack = null;
4995    }
4996
4997    TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4998            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4999            boolean toTop) {
5000        TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
5001                voiceInteractor);
5002        // add the task to stack first, mTaskPositioner might need the stack association
5003        addTask(task, toTop, "createTaskRecord");
5004        final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
5005        if (!layoutTaskInStack(task, info.windowLayout) && mBounds != null && task.isResizeable()
5006                && !isLockscreenShown) {
5007            task.updateOverrideConfiguration(mBounds);
5008        }
5009        return task;
5010    }
5011
5012    boolean layoutTaskInStack(TaskRecord task, ActivityInfo.WindowLayout windowLayout) {
5013        if (mTaskPositioner == null) {
5014            return false;
5015        }
5016        mTaskPositioner.updateDefaultBounds(task, mTaskHistory, windowLayout);
5017        return true;
5018    }
5019
5020    ArrayList<TaskRecord> getAllTasks() {
5021        return new ArrayList<>(mTaskHistory);
5022    }
5023
5024    void addTask(final TaskRecord task, final boolean toTop, String reason) {
5025        final ActivityStack prevStack = preAddTask(task, reason);
5026
5027        task.stack = this;
5028        if (toTop) {
5029            insertTaskAtTop(task, null);
5030        } else {
5031            mTaskHistory.add(0, task);
5032            updateTaskMovement(task, false);
5033        }
5034        postAddTask(task, prevStack);
5035    }
5036
5037    void positionTask(final TaskRecord task, int position) {
5038        final ActivityRecord topRunningActivity = task.topRunningActivityLocked();
5039        final boolean wasResumed = topRunningActivity == task.stack.mResumedActivity;
5040        final ActivityStack prevStack = preAddTask(task, "positionTask");
5041        task.stack = this;
5042        insertTaskAtPosition(task, position);
5043        postAddTask(task, prevStack);
5044        if (wasResumed) {
5045            if (mResumedActivity != null) {
5046                Log.wtf(TAG, "mResumedActivity was already set when moving mResumedActivity from"
5047                        + " other stack to this stack mResumedActivity=" + mResumedActivity
5048                        + " other mResumedActivity=" + topRunningActivity);
5049            }
5050            mResumedActivity = topRunningActivity;
5051        }
5052    }
5053
5054    private ActivityStack preAddTask(TaskRecord task, String reason) {
5055        final ActivityStack prevStack = task.stack;
5056        if (prevStack != null && prevStack != this) {
5057            prevStack.removeTask(task, reason, MOVING);
5058        }
5059        return prevStack;
5060    }
5061
5062    private void postAddTask(TaskRecord task, ActivityStack prevStack) {
5063        if (prevStack != null) {
5064            mStackSupervisor.scheduleReportPictureInPictureModeChangedIfNeeded(task, prevStack);
5065        } else if (task.voiceSession != null) {
5066            try {
5067                task.voiceSession.taskStarted(task.intent, task.taskId);
5068            } catch (RemoteException e) {
5069            }
5070        }
5071    }
5072
5073    void addConfigOverride(ActivityRecord r, TaskRecord task) {
5074        final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
5075        // TODO: VI deal with activity
5076        mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
5077                r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
5078                (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
5079                task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
5080                task.mResizeMode, r.isAlwaysFocusable(), task.isHomeTask());
5081        r.taskConfigOverride = task.mOverrideConfig;
5082    }
5083
5084    void moveToFrontAndResumeStateIfNeeded(
5085            ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
5086        if (!moveToFront) {
5087            return;
5088        }
5089
5090        // If the activity owns the last resumed activity, transfer that together,
5091        // so that we don't resume the same activity again in the new stack.
5092        // Apps may depend on onResume()/onPause() being called in pairs.
5093        if (setResume) {
5094            mResumedActivity = r;
5095        }
5096        // Move the stack in which we are placing the activity to the front. The call will also
5097        // make sure the activity focus is set.
5098        moveToFront(reason);
5099    }
5100
5101    /**
5102     * Moves the input activity from its current stack to this one.
5103     * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
5104     * created on this stack which the activity is added to.
5105     * */
5106    void moveActivityToStack(ActivityRecord r) {
5107        final ActivityStack prevStack = r.task.stack;
5108        if (prevStack.mStackId == mStackId) {
5109            // You are already in the right stack silly...
5110            return;
5111        }
5112
5113        final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
5114                && (mStackSupervisor.topRunningActivityLocked() == r);
5115        final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
5116
5117        final TaskRecord task = createTaskRecord(
5118                mStackSupervisor.getNextTaskIdForUserLocked(r.userId),
5119                r.info, r.intent, null, null, true);
5120        r.setTask(task, null);
5121        task.addActivityToTop(r);
5122        setAppTask(r, task);
5123        mStackSupervisor.scheduleReportPictureInPictureModeChangedIfNeeded(task, prevStack);
5124        moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
5125        if (wasResumed) {
5126            prevStack.mResumedActivity = null;
5127        }
5128    }
5129
5130    private void setAppTask(ActivityRecord r, TaskRecord task) {
5131        final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
5132        mWindowManager.setAppTask(r.appToken, task.taskId, mStackId, bounds, task.mOverrideConfig,
5133                task.mResizeMode, task.isHomeTask());
5134        r.taskConfigOverride = task.mOverrideConfig;
5135    }
5136
5137    public int getStackId() {
5138        return mStackId;
5139    }
5140
5141    @Override
5142    public String toString() {
5143        return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
5144                + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
5145    }
5146
5147    void onLockTaskPackagesUpdatedLocked() {
5148        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5149            mTaskHistory.get(taskNdx).setLockTaskAuth();
5150        }
5151    }
5152}
5153