RecentsView.java revision 3e8747414520ee348cf4b9c4a6afd9ff80b5a8f8
1/*
2 * Copyright (C) 2014 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.systemui.recents.views;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.content.Context;
22import android.content.res.Resources;
23import android.graphics.Canvas;
24import android.graphics.Rect;
25import android.graphics.drawable.Drawable;
26import android.os.Handler;
27import android.util.ArraySet;
28import android.util.AttributeSet;
29import android.view.LayoutInflater;
30import android.view.MotionEvent;
31import android.view.View;
32import android.view.ViewPropertyAnimator;
33import android.view.WindowInsets;
34import android.view.animation.AnimationUtils;
35import android.view.animation.Interpolator;
36import android.widget.FrameLayout;
37import android.widget.TextView;
38import com.android.internal.logging.MetricsLogger;
39import com.android.systemui.R;
40import com.android.systemui.recents.Recents;
41import com.android.systemui.recents.RecentsActivity;
42import com.android.systemui.recents.RecentsActivityLaunchState;
43import com.android.systemui.recents.RecentsAppWidgetHostView;
44import com.android.systemui.recents.RecentsConfiguration;
45import com.android.systemui.recents.RecentsDebugFlags;
46import com.android.systemui.recents.events.EventBus;
47import com.android.systemui.recents.events.activity.CancelEnterRecentsWindowAnimationEvent;
48import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
49import com.android.systemui.recents.events.activity.HideHistoryButtonEvent;
50import com.android.systemui.recents.events.activity.HideHistoryEvent;
51import com.android.systemui.recents.events.activity.LaunchTaskEvent;
52import com.android.systemui.recents.events.activity.ShowHistoryButtonEvent;
53import com.android.systemui.recents.events.activity.ShowHistoryEvent;
54import com.android.systemui.recents.events.activity.TaskStackUpdatedEvent;
55import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
56import com.android.systemui.recents.events.ui.DraggingInRecentsEndedEvent;
57import com.android.systemui.recents.events.ui.DraggingInRecentsEvent;
58import com.android.systemui.recents.events.ui.dragndrop.DragDropTargetChangedEvent;
59import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
60import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
61import com.android.systemui.recents.misc.ReferenceCountedTrigger;
62import com.android.systemui.recents.misc.SystemServicesProxy;
63import com.android.systemui.recents.model.Task;
64import com.android.systemui.recents.model.TaskStack;
65import com.android.systemui.stackdivider.WindowManagerProxy;
66import com.android.systemui.statusbar.FlingAnimationUtils;
67import com.android.systemui.statusbar.phone.PhoneStatusBar;
68
69import java.util.ArrayList;
70import java.util.Collections;
71import java.util.List;
72
73import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
74
75/**
76 * This view is the the top level layout that contains TaskStacks (which are laid out according
77 * to their SpaceNode bounds.
78 */
79public class RecentsView extends FrameLayout {
80
81    private static final int DOCK_AREA_OVERLAY_TRANSITION_DURATION = 135;
82
83    private final Handler mHandler;
84
85    private TaskStack mStack;
86    private TaskStackView mTaskStackView;
87    private RecentsAppWidgetHostView mSearchBar;
88    private TextView mHistoryButton;
89    private View mEmptyView;
90    private boolean mAwaitingFirstLayout = true;
91    private boolean mLastTaskLaunchedWasFreeform;
92    private Rect mSystemInsets = new Rect();
93    private int mDividerSize;
94
95    private RecentsTransitionHelper mTransitionHelper;
96    private RecentsViewTouchHandler mTouchHandler;
97    private TaskStack.DockState[] mVisibleDockStates = {
98            TaskStack.DockState.LEFT,
99            TaskStack.DockState.TOP,
100            TaskStack.DockState.RIGHT,
101            TaskStack.DockState.BOTTOM,
102    };
103
104    private final Interpolator mFastOutSlowInInterpolator;
105    private final Interpolator mFastOutLinearInInterpolator;
106    private final FlingAnimationUtils mFlingAnimationUtils;
107
108    public RecentsView(Context context) {
109        this(context, null);
110    }
111
112    public RecentsView(Context context, AttributeSet attrs) {
113        this(context, attrs, 0);
114    }
115
116    public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
117        this(context, attrs, defStyleAttr, 0);
118    }
119
120    public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
121        super(context, attrs, defStyleAttr, defStyleRes);
122        setWillNotDraw(false);
123
124        SystemServicesProxy ssp = Recents.getSystemServices();
125        mHandler = new Handler();
126        mTransitionHelper = new RecentsTransitionHelper(getContext(), mHandler);
127        mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
128                com.android.internal.R.interpolator.fast_out_slow_in);
129        mFastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
130                com.android.internal.R.interpolator.fast_out_linear_in);
131        mDividerSize = ssp.getDockedDividerSize(context);
132        mTouchHandler = new RecentsViewTouchHandler(this);
133        mFlingAnimationUtils = new FlingAnimationUtils(context, 0.3f);
134
135        LayoutInflater inflater = LayoutInflater.from(context);
136        mHistoryButton = (TextView) inflater.inflate(R.layout.recents_history_button, this, false);
137        mHistoryButton.setOnClickListener(new View.OnClickListener() {
138            @Override
139            public void onClick(View v) {
140                EventBus.getDefault().send(new ShowHistoryEvent());
141            }
142        });
143        addView(mHistoryButton);
144        mEmptyView = inflater.inflate(R.layout.recents_empty, this, false);
145        addView(mEmptyView);
146    }
147
148    /** Set/get the bsp root node */
149    public void setTaskStack(TaskStack stack) {
150        RecentsConfiguration config = Recents.getConfiguration();
151        RecentsActivityLaunchState launchState = config.getLaunchState();
152        mStack = stack;
153        if (launchState.launchedReuseTaskStackViews) {
154            if (mTaskStackView != null) {
155                // If onRecentsHidden is not triggered, we need to the stack view again here
156                mTaskStackView.reset();
157                mTaskStackView.setStack(stack);
158            } else {
159                mTaskStackView = new TaskStackView(getContext(), stack);
160                addView(mTaskStackView);
161            }
162        } else {
163            if (mTaskStackView != null) {
164                removeView(mTaskStackView);
165            }
166            mTaskStackView = new TaskStackView(getContext(), stack);
167            addView(mTaskStackView);
168        }
169
170        // Update the top level view's visibilities
171        if (stack.getStackTaskCount() > 0) {
172            hideEmptyView();
173        } else {
174            showEmptyView();
175        }
176
177        // Trigger a new layout
178        requestLayout();
179    }
180
181    /**
182     * Returns whether the last task launched was in the freeform stack or not.
183     */
184    public boolean isLastTaskLaunchedFreeform() {
185        return mLastTaskLaunchedWasFreeform;
186    }
187
188    /**
189     * Returns the currently set task stack.
190     */
191    public TaskStack getTaskStack() {
192        return mStack;
193    }
194
195    /** Gets the next task in the stack - or if the last - the top task */
196    public Task getNextTaskOrTopTask(Task taskToSearch) {
197        Task returnTask = null;
198        boolean found = false;
199        if (mTaskStackView != null) {
200            TaskStack stack = mTaskStackView.getStack();
201            ArrayList<Task> taskList = stack.getStackTasks();
202            // Iterate the stack views and try and find the focused task
203            for (int j = taskList.size() - 1; j >= 0; --j) {
204                Task task = taskList.get(j);
205                // Return the next task in the line.
206                if (found)
207                    return task;
208                // Remember the first possible task as the top task.
209                if (returnTask == null)
210                    returnTask = task;
211                if (task == taskToSearch)
212                    found = true;
213            }
214        }
215        return returnTask;
216    }
217
218    /** Launches the focused task from the first stack if possible */
219    public boolean launchFocusedTask() {
220        if (mTaskStackView != null) {
221            Task task = mTaskStackView.getFocusedTask();
222            if (task != null) {
223                TaskView taskView = mTaskStackView.getChildViewForTask(task);
224                EventBus.getDefault().send(new LaunchTaskEvent(taskView, task, null,
225                        INVALID_STACK_ID, false));
226                return true;
227            }
228        }
229        return false;
230    }
231
232    /** Launches the task that recents was launched from if possible */
233    public boolean launchPreviousTask() {
234        if (mTaskStackView != null) {
235            TaskStack stack = mTaskStackView.getStack();
236            Task task = stack.getLaunchTarget();
237            if (task != null) {
238                TaskView taskView = mTaskStackView.getChildViewForTask(task);
239                EventBus.getDefault().send(new LaunchTaskEvent(taskView, task, null,
240                        INVALID_STACK_ID, false));
241                return true;
242            }
243        }
244        return false;
245    }
246
247    /** Launches a given task. */
248    public boolean launchTask(Task task, Rect taskBounds, int destinationStack) {
249        if (mTaskStackView != null) {
250            // Iterate the stack views and try and find the given task.
251            List<TaskView> taskViews = mTaskStackView.getTaskViews();
252            int taskViewCount = taskViews.size();
253            for (int j = 0; j < taskViewCount; j++) {
254                TaskView tv = taskViews.get(j);
255                if (tv.getTask() == task) {
256                    EventBus.getDefault().send(new LaunchTaskEvent(tv, task, taskBounds,
257                            destinationStack, false));
258                    return true;
259                }
260            }
261        }
262        return false;
263    }
264
265    /** Adds the search bar */
266    public void setSearchBar(RecentsAppWidgetHostView searchBar) {
267        // Remove the previous search bar if one exists
268        if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
269            removeView(mSearchBar);
270        }
271        // Add the new search bar
272        if (searchBar != null) {
273            mSearchBar = searchBar;
274            addView(mSearchBar);
275        }
276    }
277
278    /** Returns whether there is currently a search bar */
279    public boolean hasValidSearchBar() {
280        return mSearchBar != null && !mSearchBar.isReinflateRequired();
281    }
282
283    /**
284     * Hides the task stack and shows the empty view.
285     */
286    public void showEmptyView() {
287        if (RecentsDebugFlags.Static.EnableSearchBar && (mSearchBar != null)) {
288            mSearchBar.setVisibility(View.INVISIBLE);
289        }
290        mTaskStackView.setVisibility(View.INVISIBLE);
291        mEmptyView.setVisibility(View.VISIBLE);
292        mEmptyView.bringToFront();
293        mHistoryButton.bringToFront();
294    }
295
296    /**
297     * Shows the task stack and hides the empty view.
298     */
299    public void hideEmptyView() {
300        mEmptyView.setVisibility(View.INVISIBLE);
301        mTaskStackView.setVisibility(View.VISIBLE);
302        if (RecentsDebugFlags.Static.EnableSearchBar && (mSearchBar != null)) {
303            mSearchBar.setVisibility(View.VISIBLE);
304        }
305        mTaskStackView.bringToFront();
306        if (mSearchBar != null) {
307            mSearchBar.bringToFront();
308        }
309        mHistoryButton.bringToFront();
310    }
311
312    /**
313     * Returns the last known system insets.
314     */
315    public Rect getSystemInsets() {
316        return mSystemInsets;
317    }
318
319    @Override
320    protected void onAttachedToWindow() {
321        EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
322        EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
323        super.onAttachedToWindow();
324    }
325
326    @Override
327    protected void onDetachedFromWindow() {
328        super.onDetachedFromWindow();
329        EventBus.getDefault().unregister(this);
330        EventBus.getDefault().unregister(mTouchHandler);
331    }
332
333    /**
334     * This is called with the full size of the window since we are handling our own insets.
335     */
336    @Override
337    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
338        RecentsConfiguration config = Recents.getConfiguration();
339        int width = MeasureSpec.getSize(widthMeasureSpec);
340        int height = MeasureSpec.getSize(heightMeasureSpec);
341
342        // Get the search bar bounds and measure the search bar layout
343        Rect searchBarSpaceBounds = new Rect();
344        if (mSearchBar != null) {
345            config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
346                    searchBarSpaceBounds);
347            mSearchBar.measure(
348                    MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
349                    MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
350        }
351
352        Rect taskStackBounds = new Rect();
353        config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
354                mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
355        if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
356            mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
357            mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
358        }
359
360        // Measure the empty view
361        measureChild(mEmptyView, MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
362                MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
363
364        // Measure the history button with the full space above the stack, but width-constrained
365        // to the stack
366        Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
367        measureChild(mHistoryButton,
368                MeasureSpec.makeMeasureSpec(historyButtonRect.width(), MeasureSpec.EXACTLY),
369                MeasureSpec.makeMeasureSpec(historyButtonRect.height(),
370                        MeasureSpec.EXACTLY));
371
372        setMeasuredDimension(width, height);
373    }
374
375    /**
376     * This is called with the full size of the window since we are handling our own insets.
377     */
378    @Override
379    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
380        RecentsConfiguration config = Recents.getConfiguration();
381
382        // Get the search bar bounds so that we lay it out
383        Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
384        Rect searchBarSpaceBounds = new Rect();
385        if (mSearchBar != null) {
386            config.getSearchBarBounds(measuredRect,
387                    mSystemInsets.top, searchBarSpaceBounds);
388            mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
389                    searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
390        }
391
392        if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
393            mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
394        }
395
396        // Layout the empty view
397        mEmptyView.layout(left, top, right, bottom);
398
399        // Layout the history button left-aligned with the stack, but offset from the top of the
400        // view
401        Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
402        mHistoryButton.layout(historyButtonRect.left, historyButtonRect.top,
403                historyButtonRect.right, historyButtonRect.bottom);
404
405        if (mAwaitingFirstLayout) {
406            mAwaitingFirstLayout = false;
407
408            // If launched via dragging from the nav bar, then we should translate the whole view
409            // down offscreen
410            RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
411            if (launchState.launchedViaDragGesture) {
412                setTranslationY(getMeasuredHeight());
413            } else {
414                setTranslationY(0f);
415            }
416        }
417    }
418
419    @Override
420    public WindowInsets onApplyWindowInsets(WindowInsets insets) {
421        mSystemInsets.set(insets.getSystemWindowInsets());
422        requestLayout();
423        return insets;
424    }
425
426    @Override
427    public boolean onInterceptTouchEvent(MotionEvent ev) {
428        return mTouchHandler.onInterceptTouchEvent(ev);
429    }
430
431    @Override
432    public boolean onTouchEvent(MotionEvent ev) {
433        return mTouchHandler.onTouchEvent(ev);
434    }
435
436    @Override
437    protected void dispatchDraw(Canvas canvas) {
438        super.dispatchDraw(canvas);
439        for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
440            Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
441            if (d.getAlpha() > 0) {
442                d.draw(canvas);
443            }
444        }
445    }
446
447    @Override
448    protected boolean verifyDrawable(Drawable who) {
449        for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
450            Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
451            if (d == who) {
452                return true;
453            }
454        }
455        return super.verifyDrawable(who);
456    }
457
458    /**** EventBus Events ****/
459
460    public final void onBusEvent(LaunchTaskEvent event) {
461        mLastTaskLaunchedWasFreeform = event.task.isFreeformTask();
462        mTransitionHelper.launchTaskFromRecents(mStack, event.task, mTaskStackView, event.taskView,
463                event.screenPinningRequested, event.targetTaskBounds, event.targetTaskStack);
464    }
465
466    public final void onBusEvent(DismissRecentsToHomeAnimationStarted event) {
467        // Hide the history button
468        int taskViewExitToHomeDuration = getResources().getInteger(
469                R.integer.recents_task_exit_to_home_duration);
470        hideHistoryButton(taskViewExitToHomeDuration);
471
472        // If we are going home, cancel the previous task's window transition
473        EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
474    }
475
476    public final void onBusEvent(DragStartEvent event) {
477        updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
478                true /* isDefaultDockState */, TaskStack.DockState.NONE.viewState.dockAreaAlpha,
479                true /* animateAlpha */, false /* animateBounds */);
480    }
481
482    public final void onBusEvent(DragDropTargetChangedEvent event) {
483        if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
484            updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
485                    true /* isDefaultDockState */, TaskStack.DockState.NONE.viewState.dockAreaAlpha,
486                    true /* animateAlpha */, true /* animateBounds */);
487        } else {
488            final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
489            updateVisibleDockRegions(new TaskStack.DockState[] {dockState},
490                    false /* isDefaultDockState */, -1, true /* animateAlpha */,
491                    true /* animateBounds */);
492        }
493    }
494
495    public final void onBusEvent(final DragEndEvent event) {
496        // Handle the case where we drop onto a dock region
497        if (event.dropTarget instanceof TaskStack.DockState) {
498            final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
499
500            // Hide the dock region
501            updateVisibleDockRegions(null, false /* isDefaultDockState */, -1,
502                    false /* animateAlpha */, false /* animateBounds */);
503
504            TaskStackLayoutAlgorithm stackLayout = mTaskStackView.getStackAlgorithm();
505            TaskStackViewScroller stackScroller = mTaskStackView.getScroller();
506            TaskViewTransform tmpTransform = new TaskViewTransform();
507
508            // We translated the view but we need to animate it back from the current layout-space
509            // rect to its final layout-space rect
510            int x = (int) event.taskView.getTranslationX();
511            int y = (int) event.taskView.getTranslationY();
512            Rect taskViewRect = new Rect(event.taskView.getLeft(), event.taskView.getTop(),
513                    event.taskView.getRight(), event.taskView.getBottom());
514            taskViewRect.offset(x, y);
515            event.taskView.setTranslationX(0);
516            event.taskView.setTranslationY(0);
517            event.taskView.setLeftTopRightBottom(taskViewRect.left, taskViewRect.top,
518                    taskViewRect.right, taskViewRect.bottom);
519
520            // Remove the task view after it is docked
521            mTaskStackView.updateLayout(false /* boundScroll */);
522            stackLayout.getStackTransform(event.task, stackScroller.getStackScroll(), tmpTransform,
523                    null);
524            tmpTransform.alpha = 0;
525            tmpTransform.scale = 1f;
526            tmpTransform.rect.set(taskViewRect);
527            mTaskStackView.updateTaskViewToTransform(event.taskView, tmpTransform,
528                    new TaskViewAnimation(125, PhoneStatusBar.ALPHA_OUT,
529                            new AnimatorListenerAdapter() {
530                                @Override
531                                public void onAnimationEnd(Animator animation) {
532                                    // Dock the task and launch it
533                                    SystemServicesProxy ssp = Recents.getSystemServices();
534                                    ssp.startTaskInDockedMode(getContext(), event.task.key.id,
535                                            dockState.createMode);
536                                    launchTask(event.task, null, INVALID_STACK_ID);
537
538                                    mTaskStackView.getStack().removeTask(event.task);
539                                }
540                            }));
541
542
543            MetricsLogger.action(mContext,
544                    MetricsLogger.ACTION_WINDOW_DOCK_DRAG_DROP);
545        } else {
546            // Animate the overlay alpha back to 0
547            updateVisibleDockRegions(null, true /* isDefaultDockState */, -1,
548                    true /* animateAlpha */, false /* animateBounds */);
549        }
550    }
551
552    public final void onBusEvent(DraggingInRecentsEvent event) {
553        if (mTaskStackView.getTaskViews().size() > 0) {
554            setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
555        }
556    }
557
558    public final void onBusEvent(DraggingInRecentsEndedEvent event) {
559        ViewPropertyAnimator animator = animate();
560        if (event.velocity > mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
561            animator.translationY(getHeight());
562            animator.withEndAction(new Runnable() {
563                @Override
564                public void run() {
565                    WindowManagerProxy.getInstance().maximizeDockedStack();
566                }
567            });
568            mFlingAnimationUtils.apply(animator, getTranslationY(), getHeight(), event.velocity);
569        } else {
570            animator.translationY(0f);
571            animator.setListener(null);
572            mFlingAnimationUtils.apply(animator, getTranslationY(), 0, event.velocity);
573        }
574        animator.start();
575    }
576
577    public final void onBusEvent(ShowHistoryEvent event) {
578        // Hide the history button when the history view is shown
579        hideHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
580                event.getAnimationTrigger());
581        event.addPostAnimationCallback(new Runnable() {
582            @Override
583            public void run() {
584                setAlpha(0f);
585            }
586        });
587    }
588
589    public final void onBusEvent(HideHistoryEvent event) {
590        // Show the history button when the history view is hidden
591        setAlpha(1f);
592        showHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
593                event.getAnimationTrigger());
594    }
595
596    public final void onBusEvent(ShowHistoryButtonEvent event) {
597        showHistoryButton(150);
598    }
599
600    public final void onBusEvent(HideHistoryButtonEvent event) {
601        hideHistoryButton(100);
602    }
603
604    public final void onBusEvent(TaskStackUpdatedEvent event) {
605        mStack.setTasks(event.stack.computeAllTasksList(), true /* notifyStackChanges */);
606        mStack.createAffiliatedGroupings(getContext());
607    }
608
609    /**
610     * Shows the history button.
611     */
612    private void showHistoryButton(final int duration) {
613        ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
614        showHistoryButton(duration, postAnimationTrigger);
615        postAnimationTrigger.flushLastDecrementRunnables();
616    }
617
618    private void showHistoryButton(final int duration,
619            final ReferenceCountedTrigger postHideHistoryAnimationTrigger) {
620        mHistoryButton.setText(getContext().getString(R.string.recents_history_label_format,
621                mStack.getHistoricalTasks().size()));
622        if (mHistoryButton.getVisibility() == View.INVISIBLE) {
623            mHistoryButton.setVisibility(View.VISIBLE);
624            mHistoryButton.setAlpha(0f);
625            postHideHistoryAnimationTrigger.addLastDecrementRunnable(new Runnable() {
626                @Override
627                public void run() {
628                    mHistoryButton.animate()
629                            .alpha(1f)
630                            .setDuration(duration)
631                            .setInterpolator(mFastOutSlowInInterpolator)
632                            .withLayer()
633                            .start();
634                }
635            });
636        }
637    }
638
639    /**
640     * Hides the history button.
641     */
642    private void hideHistoryButton(int duration) {
643        ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
644        hideHistoryButton(duration, postAnimationTrigger);
645        postAnimationTrigger.flushLastDecrementRunnables();
646    }
647
648    private void hideHistoryButton(int duration,
649            final ReferenceCountedTrigger postHideStackAnimationTrigger) {
650        if (mHistoryButton.getVisibility() == View.VISIBLE) {
651            mHistoryButton.animate()
652                    .alpha(0f)
653                    .setDuration(duration)
654                    .setInterpolator(mFastOutLinearInInterpolator)
655                    .withEndAction(new Runnable() {
656                        @Override
657                        public void run() {
658                            mHistoryButton.setVisibility(View.INVISIBLE);
659                            postHideStackAnimationTrigger.decrement();
660                        }
661                    })
662                    .withLayer()
663                    .start();
664            postHideStackAnimationTrigger.increment();
665        }
666    }
667
668    /**
669     * Updates the dock region to match the specified dock state.
670     */
671    private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates,
672            boolean isDefaultDockState, int overrideAlpha, boolean animateAlpha,
673            boolean animateBounds) {
674        ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
675        if (newDockStates != null) {
676            Collections.addAll(newDockStatesSet, newDockStates);
677        }
678        for (TaskStack.DockState dockState : mVisibleDockStates) {
679            TaskStack.DockState.ViewState viewState = dockState.viewState;
680            if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
681                // This is no longer visible, so hide it
682                viewState.startAnimation(null, 0, DOCK_AREA_OVERLAY_TRANSITION_DURATION,
683                        PhoneStatusBar.ALPHA_OUT, animateAlpha, animateBounds);
684            } else {
685                // This state is now visible, update the bounds and show it
686                int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
687                Rect bounds = isDefaultDockState
688                        ? dockState.getPreDockedBounds(getMeasuredWidth(), getMeasuredHeight())
689                        : dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight(),
690                        mDividerSize, mSystemInsets, getResources());
691                if (viewState.dockAreaOverlay.getCallback() != this) {
692                    viewState.dockAreaOverlay.setCallback(this);
693                    viewState.dockAreaOverlay.setBounds(bounds);
694                }
695                viewState.startAnimation(bounds, alpha, DOCK_AREA_OVERLAY_TRANSITION_DURATION,
696                        PhoneStatusBar.ALPHA_IN, animateAlpha, animateBounds);
697            }
698        }
699    }
700
701    public final void onBusEvent(RecentsVisibilityChangedEvent event) {
702        if (!event.visible) {
703            // Reset the view state
704            mAwaitingFirstLayout = true;
705            mLastTaskLaunchedWasFreeform = false;
706        }
707    }
708}
709