TaskView.java revision 2ed7c47e2788ff0f008b106c67672c271a6cf958
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 static android.app.ActivityManager.StackId.INVALID_STACK_ID;
20
21import android.animation.Animator;
22import android.animation.AnimatorSet;
23import android.animation.ObjectAnimator;
24import android.animation.ValueAnimator;
25import android.app.ActivityManager;
26import android.content.Context;
27import android.content.res.Resources;
28import android.graphics.Outline;
29import android.graphics.Point;
30import android.graphics.Rect;
31import android.util.AttributeSet;
32import android.util.FloatProperty;
33import android.util.Property;
34import android.view.MotionEvent;
35import android.view.View;
36import android.view.ViewDebug;
37import android.view.ViewOutlineProvider;
38import android.widget.TextView;
39import android.widget.Toast;
40
41import com.android.internal.logging.MetricsLogger;
42import com.android.internal.logging.MetricsProto.MetricsEvent;
43import com.android.systemui.Interpolators;
44import com.android.systemui.R;
45import com.android.systemui.recents.Recents;
46import com.android.systemui.recents.RecentsActivity;
47import com.android.systemui.recents.RecentsConfiguration;
48import com.android.systemui.recents.events.EventBus;
49import com.android.systemui.recents.events.activity.LaunchTaskEvent;
50import com.android.systemui.recents.events.ui.DismissTaskViewEvent;
51import com.android.systemui.recents.events.ui.TaskViewDismissedEvent;
52import com.android.systemui.recents.events.ui.dragndrop.DragEndCancelledEvent;
53import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
54import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
55import com.android.systemui.recents.misc.ReferenceCountedTrigger;
56import com.android.systemui.recents.misc.SystemServicesProxy;
57import com.android.systemui.recents.misc.Utilities;
58import com.android.systemui.recents.model.Task;
59import com.android.systemui.recents.model.TaskStack;
60
61import java.util.ArrayList;
62
63/**
64 * A {@link TaskView} represents a fixed view of a task. Because the TaskView's layout is directed
65 * solely by the {@link TaskStackView}, we make it a fixed size layout which allows relayouts down
66 * the view hierarchy, but not upwards from any of its children (the TaskView will relayout itself
67 * with the previous bounds if any child requests layout).
68 */
69public class TaskView extends FixedSizeFrameLayout implements Task.TaskCallbacks,
70        TaskStackAnimationHelper.Callbacks, View.OnClickListener, View.OnLongClickListener {
71
72    /** The TaskView callbacks */
73    interface TaskViewCallbacks {
74        void onTaskViewClipStateChanged(TaskView tv);
75    }
76
77    /**
78     * The dim overlay is generally calculated from the task progress, but occasionally (like when
79     * launching) needs to be animated independently of the task progress.  This call is only used
80     * when animating the task into Recents, when the header dim is already applied
81     */
82    public static final Property<TaskView, Float> DIM_ALPHA_WITHOUT_HEADER =
83            new FloatProperty<TaskView>("dimAlphaWithoutHeader") {
84                @Override
85                public void setValue(TaskView tv, float dimAlpha) {
86                    tv.setDimAlphaWithoutHeader(dimAlpha);
87                }
88
89                @Override
90                public Float get(TaskView tv) {
91                    return tv.getDimAlpha();
92                }
93            };
94
95    /**
96     * The dim overlay is generally calculated from the task progress, but occasionally (like when
97     * launching) needs to be animated independently of the task progress.
98     */
99    public static final Property<TaskView, Float> DIM_ALPHA =
100            new FloatProperty<TaskView>("dimAlpha") {
101                @Override
102                public void setValue(TaskView tv, float dimAlpha) {
103                    tv.setDimAlpha(dimAlpha);
104                }
105
106                @Override
107                public Float get(TaskView tv) {
108                    return tv.getDimAlpha();
109                }
110            };
111
112    /**
113     * The dim overlay is generally calculated from the task progress, but occasionally (like when
114     * launching) needs to be animated independently of the task progress.
115     */
116    public static final Property<TaskView, Float> VIEW_OUTLINE_ALPHA =
117            new FloatProperty<TaskView>("viewOutlineAlpha") {
118                @Override
119                public void setValue(TaskView tv, float alpha) {
120                    tv.getViewBounds().setAlpha(alpha);
121                }
122
123                @Override
124                public Float get(TaskView tv) {
125                    return tv.getViewBounds().getAlpha();
126                }
127            };
128
129    @ViewDebug.ExportedProperty(category="recents")
130    private float mDimAlpha;
131    private float mActionButtonTranslationZ;
132
133    @ViewDebug.ExportedProperty(deepExport=true, prefix="task_")
134    private Task mTask;
135    @ViewDebug.ExportedProperty(category="recents")
136    private boolean mClipViewInStack = true;
137    @ViewDebug.ExportedProperty(category="recents")
138    private boolean mTouchExplorationEnabled;
139    @ViewDebug.ExportedProperty(category="recents")
140    private boolean mIsDisabledInSafeMode;
141    @ViewDebug.ExportedProperty(deepExport=true, prefix="view_bounds_")
142    private AnimateableViewBounds mViewBounds;
143
144    private AnimatorSet mTransformAnimation;
145    private ObjectAnimator mDimAnimator;
146    private ObjectAnimator mOutlineAnimator;
147    private final TaskViewTransform mTargetAnimationTransform = new TaskViewTransform();
148    private ArrayList<Animator> mTmpAnimators = new ArrayList<>();
149
150    @ViewDebug.ExportedProperty(deepExport=true, prefix="thumbnail_")
151    TaskViewThumbnail mThumbnailView;
152    @ViewDebug.ExportedProperty(deepExport=true, prefix="header_")
153    TaskViewHeader mHeaderView;
154    private View mActionButtonView;
155    private View mIncompatibleAppToastView;
156    private TaskViewCallbacks mCb;
157
158    @ViewDebug.ExportedProperty(category="recents")
159    private Point mDownTouchPos = new Point();
160
161    private Toast mDisabledAppToast;
162
163    public TaskView(Context context) {
164        this(context, null);
165    }
166
167    public TaskView(Context context, AttributeSet attrs) {
168        this(context, attrs, 0);
169    }
170
171    public TaskView(Context context, AttributeSet attrs, int defStyleAttr) {
172        this(context, attrs, defStyleAttr, 0);
173    }
174
175    public TaskView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
176        super(context, attrs, defStyleAttr, defStyleRes);
177        RecentsConfiguration config = Recents.getConfiguration();
178        Resources res = context.getResources();
179        mViewBounds = new AnimateableViewBounds(this, res.getDimensionPixelSize(
180                R.dimen.recents_task_view_shadow_rounded_corners_radius));
181        if (config.fakeShadows) {
182            setBackground(new FakeShadowDrawable(res, config));
183        }
184        setOutlineProvider(mViewBounds);
185        setOnLongClickListener(this);
186    }
187
188    /** Set callback */
189    void setCallbacks(TaskViewCallbacks cb) {
190        mCb = cb;
191    }
192
193    /**
194     * Called from RecentsActivity when it is relaunched.
195     */
196    void onReload(boolean isResumingFromVisible) {
197        if (!Recents.getSystemServices().hasFreeformWorkspaceSupport()) {
198            resetNoUserInteractionState();
199        }
200        if (!isResumingFromVisible) {
201            resetViewProperties();
202        }
203    }
204
205    /** Gets the task */
206    public Task getTask() {
207        return mTask;
208    }
209
210    /** Returns the view bounds. */
211    AnimateableViewBounds getViewBounds() {
212        return mViewBounds;
213    }
214
215    @Override
216    protected void onFinishInflate() {
217        // Bind the views
218        mHeaderView = (TaskViewHeader) findViewById(R.id.task_view_bar);
219        if (Recents.getSystemServices().hasFreeformWorkspaceSupport()) {
220            mHeaderView.setNoUserInteractionState();
221        }
222        mThumbnailView = (TaskViewThumbnail) findViewById(R.id.task_view_thumbnail);
223        mThumbnailView.updateClipToTaskBar(mHeaderView);
224        mActionButtonView = findViewById(R.id.lock_to_app_fab);
225        mActionButtonView.setOutlineProvider(new ViewOutlineProvider() {
226            @Override
227            public void getOutline(View view, Outline outline) {
228                // Set the outline to match the FAB background
229                outline.setOval(0, 0, mActionButtonView.getWidth(), mActionButtonView.getHeight());
230                outline.setAlpha(0.35f);
231            }
232        });
233        mActionButtonView.setOnClickListener(this);
234        mActionButtonTranslationZ = mActionButtonView.getTranslationZ();
235    }
236
237    /**
238     * Update the task view when the configuration changes.
239     */
240    void onConfigurationChanged() {
241        mHeaderView.onConfigurationChanged();
242    }
243
244    @Override
245    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
246        super.onSizeChanged(w, h, oldw, oldh);
247        if (w > 0 && h > 0) {
248            mHeaderView.onTaskViewSizeChanged(w, h);
249            mThumbnailView.onTaskViewSizeChanged(w, h);
250
251            mActionButtonView.setTranslationX(w - getMeasuredWidth());
252            mActionButtonView.setTranslationY(h - getMeasuredHeight());
253        }
254    }
255
256    @Override
257    public boolean hasOverlappingRendering() {
258        return false;
259    }
260
261    @Override
262    public boolean onInterceptTouchEvent(MotionEvent ev) {
263        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
264            mDownTouchPos.set((int) (ev.getX() * getScaleX()), (int) (ev.getY() * getScaleY()));
265        }
266        return super.onInterceptTouchEvent(ev);
267    }
268
269
270    @Override
271    protected void measureContents(int width, int height) {
272        int widthWithoutPadding = width - mPaddingLeft - mPaddingRight;
273        int heightWithoutPadding = height - mPaddingTop - mPaddingBottom;
274        int widthSpec = MeasureSpec.makeMeasureSpec(widthWithoutPadding, MeasureSpec.EXACTLY);
275        int heightSpec = MeasureSpec.makeMeasureSpec(heightWithoutPadding, MeasureSpec.EXACTLY);
276
277        // Measure the content
278        measureChildren(widthSpec, heightSpec);
279
280        setMeasuredDimension(width, height);
281    }
282
283    void updateViewPropertiesToTaskTransform(TaskViewTransform toTransform,
284            AnimationProps toAnimation, ValueAnimator.AnimatorUpdateListener updateCallback) {
285        RecentsConfiguration config = Recents.getConfiguration();
286        cancelTransformAnimation();
287
288        // Compose the animations for the transform
289        mTmpAnimators.clear();
290        toTransform.applyToTaskView(this, mTmpAnimators, toAnimation, !config.fakeShadows);
291        if (toAnimation.isImmediate()) {
292            if (Float.compare(getDimAlpha(), toTransform.dimAlpha) != 0) {
293                setDimAlpha(toTransform.dimAlpha);
294            }
295            if (Float.compare(mViewBounds.getAlpha(), toTransform.viewOutlineAlpha) != 0) {
296                mViewBounds.setAlpha(toTransform.viewOutlineAlpha);
297            }
298            // Manually call back to the animator listener and update callback
299            if (toAnimation.getListener() != null) {
300                toAnimation.getListener().onAnimationEnd(null);
301            }
302            if (updateCallback != null) {
303                updateCallback.onAnimationUpdate(null);
304            }
305        } else {
306            // Both the progress and the update are a function of the bounds movement of the task
307            if (Float.compare(getDimAlpha(), toTransform.dimAlpha) != 0) {
308                mDimAnimator = ObjectAnimator.ofFloat(this, DIM_ALPHA, getDimAlpha(),
309                        toTransform.dimAlpha);
310                mTmpAnimators.add(toAnimation.apply(AnimationProps.BOUNDS, mDimAnimator));
311            }
312            if (Float.compare(mViewBounds.getAlpha(), toTransform.viewOutlineAlpha) != 0) {
313                mOutlineAnimator = ObjectAnimator.ofFloat(this, VIEW_OUTLINE_ALPHA,
314                        mViewBounds.getAlpha(), toTransform.viewOutlineAlpha);
315                mTmpAnimators.add(toAnimation.apply(AnimationProps.BOUNDS, mOutlineAnimator));
316            }
317            if (updateCallback != null) {
318                ValueAnimator updateCallbackAnim = ValueAnimator.ofInt(0, 1);
319                updateCallbackAnim.addUpdateListener(updateCallback);
320                mTmpAnimators.add(toAnimation.apply(AnimationProps.BOUNDS, updateCallbackAnim));
321            }
322
323            // Create the animator
324            mTransformAnimation = toAnimation.createAnimator(mTmpAnimators);
325            mTransformAnimation.start();
326            mTargetAnimationTransform.copyFrom(toTransform);
327        }
328    }
329
330    /** Resets this view's properties */
331    void resetViewProperties() {
332        cancelTransformAnimation();
333        setDimAlpha(0);
334        setVisibility(View.VISIBLE);
335        getViewBounds().reset();
336        getHeaderView().reset();
337        TaskViewTransform.reset(this);
338
339        mActionButtonView.setScaleX(1f);
340        mActionButtonView.setScaleY(1f);
341        mActionButtonView.setAlpha(0f);
342        mActionButtonView.setTranslationX(0f);
343        mActionButtonView.setTranslationY(0f);
344        mActionButtonView.setTranslationZ(mActionButtonTranslationZ);
345        if (mIncompatibleAppToastView != null) {
346            mIncompatibleAppToastView.setVisibility(View.INVISIBLE);
347        }
348    }
349
350    /**
351     * @return whether we are animating towards {@param transform}
352     */
353    boolean isAnimatingTo(TaskViewTransform transform) {
354        return mTransformAnimation != null && mTransformAnimation.isStarted()
355                && mTargetAnimationTransform.isSame(transform);
356    }
357
358    /**
359     * Cancels any current transform animations.
360     */
361    public void cancelTransformAnimation() {
362        Utilities.cancelAnimationWithoutCallbacks(mTransformAnimation);
363        Utilities.cancelAnimationWithoutCallbacks(mDimAnimator);
364        Utilities.cancelAnimationWithoutCallbacks(mOutlineAnimator);
365    }
366
367    /** Enables/disables handling touch on this task view. */
368    void setTouchEnabled(boolean enabled) {
369        setOnClickListener(enabled ? this : null);
370    }
371
372    /** Animates this task view if the user does not interact with the stack after a certain time. */
373    void startNoUserInteractionAnimation() {
374        mHeaderView.startNoUserInteractionAnimation();
375    }
376
377    /** Mark this task view that the user does has not interacted with the stack after a certain time. */
378    void setNoUserInteractionState() {
379        mHeaderView.setNoUserInteractionState();
380    }
381
382    /** Resets the state tracking that the user has not interacted with the stack after a certain time. */
383    void resetNoUserInteractionState() {
384        mHeaderView.resetNoUserInteractionState();
385    }
386
387    /** Dismisses this task. */
388    void dismissTask() {
389        // Animate out the view and call the callback
390        final TaskView tv = this;
391        DismissTaskViewEvent dismissEvent = new DismissTaskViewEvent(tv);
392        dismissEvent.addPostAnimationCallback(new Runnable() {
393            @Override
394            public void run() {
395                EventBus.getDefault().send(new TaskViewDismissedEvent(mTask, tv,
396                        new AnimationProps(TaskStackView.DEFAULT_SYNC_STACK_DURATION,
397                                Interpolators.FAST_OUT_SLOW_IN)));
398            }
399        });
400        EventBus.getDefault().send(dismissEvent);
401    }
402
403    /**
404     * Returns whether this view should be clipped, or any views below should clip against this
405     * view.
406     */
407    boolean shouldClipViewInStack() {
408        // Never clip for freeform tasks or if invisible
409        if (mTask.isFreeformTask() || getVisibility() != View.VISIBLE) {
410            return false;
411        }
412        return mClipViewInStack;
413    }
414
415    /** Sets whether this view should be clipped, or clipped against. */
416    void setClipViewInStack(boolean clip) {
417        if (clip != mClipViewInStack) {
418            mClipViewInStack = clip;
419            if (mCb != null) {
420                mCb.onTaskViewClipStateChanged(this);
421            }
422        }
423    }
424
425    public TaskViewHeader getHeaderView() {
426        return mHeaderView;
427    }
428
429    /**
430     * Sets the current dim.
431     */
432    public void setDimAlpha(float dimAlpha) {
433        mDimAlpha = dimAlpha;
434        mThumbnailView.setDimAlpha(dimAlpha);
435        mHeaderView.setDimAlpha(dimAlpha);
436    }
437
438    /**
439     * Sets the current dim without updating the header's dim.
440     */
441    public void setDimAlphaWithoutHeader(float dimAlpha) {
442        mDimAlpha = dimAlpha;
443        mThumbnailView.setDimAlpha(dimAlpha);
444    }
445
446    /**
447     * Returns the current dim.
448     */
449    public float getDimAlpha() {
450        return mDimAlpha;
451    }
452
453    /**
454     * Explicitly sets the focused state of this task.
455     */
456    public void setFocusedState(boolean isFocused, boolean requestViewFocus) {
457        if (isFocused) {
458            if (requestViewFocus && !isFocused()) {
459                requestFocus();
460            }
461        } else {
462            if (isAccessibilityFocused() && mTouchExplorationEnabled) {
463                clearAccessibilityFocus();
464            }
465        }
466    }
467
468    /**
469     * Shows the action button.
470     * @param fadeIn whether or not to animate the action button in.
471     * @param fadeInDuration the duration of the action button animation, only used if
472     *                       {@param fadeIn} is true.
473     */
474    public void showActionButton(boolean fadeIn, int fadeInDuration) {
475        mActionButtonView.setVisibility(View.VISIBLE);
476
477        if (fadeIn && mActionButtonView.getAlpha() < 1f) {
478            mActionButtonView.animate()
479                    .alpha(1f)
480                    .scaleX(1f)
481                    .scaleY(1f)
482                    .setDuration(fadeInDuration)
483                    .setInterpolator(Interpolators.ALPHA_IN)
484                    .start();
485        } else {
486            mActionButtonView.setScaleX(1f);
487            mActionButtonView.setScaleY(1f);
488            mActionButtonView.setAlpha(1f);
489            mActionButtonView.setTranslationZ(mActionButtonTranslationZ);
490        }
491    }
492
493    /**
494     * Immediately hides the action button.
495     *
496     * @param fadeOut whether or not to animate the action button out.
497     */
498    public void hideActionButton(boolean fadeOut, int fadeOutDuration, boolean scaleDown,
499            final Animator.AnimatorListener animListener) {
500        if (fadeOut && mActionButtonView.getAlpha() > 0f) {
501            if (scaleDown) {
502                float toScale = 0.9f;
503                mActionButtonView.animate()
504                        .scaleX(toScale)
505                        .scaleY(toScale);
506            }
507            mActionButtonView.animate()
508                    .alpha(0f)
509                    .setDuration(fadeOutDuration)
510                    .setInterpolator(Interpolators.ALPHA_OUT)
511                    .withEndAction(new Runnable() {
512                        @Override
513                        public void run() {
514                            if (animListener != null) {
515                                animListener.onAnimationEnd(null);
516                            }
517                            mActionButtonView.setVisibility(View.INVISIBLE);
518                        }
519                    })
520                    .start();
521        } else {
522            mActionButtonView.setAlpha(0f);
523            mActionButtonView.setVisibility(View.INVISIBLE);
524            if (animListener != null) {
525                animListener.onAnimationEnd(null);
526            }
527        }
528    }
529
530    /**** TaskStackAnimationHelper.Callbacks Implementation ****/
531
532    @Override
533    public void onPrepareLaunchTargetForEnterAnimation() {
534        // These values will be animated in when onStartLaunchTargetEnterAnimation() is called
535        setDimAlphaWithoutHeader(0);
536        mActionButtonView.setAlpha(0f);
537        if (mIncompatibleAppToastView != null &&
538                mIncompatibleAppToastView.getVisibility() == View.VISIBLE) {
539            mIncompatibleAppToastView.setAlpha(0f);
540        }
541    }
542
543    @Override
544    public void onStartLaunchTargetEnterAnimation(TaskViewTransform transform, int duration,
545            boolean screenPinningEnabled, ReferenceCountedTrigger postAnimationTrigger) {
546        Utilities.cancelAnimationWithoutCallbacks(mDimAnimator);
547
548        // Dim the view after the app window transitions down into recents
549        postAnimationTrigger.increment();
550        AnimationProps animation = new AnimationProps(duration, Interpolators.ALPHA_OUT);
551        mDimAnimator = animation.apply(AnimationProps.DIM_ALPHA, ObjectAnimator.ofFloat(this,
552                DIM_ALPHA_WITHOUT_HEADER, getDimAlpha(), transform.dimAlpha));
553        mDimAnimator.addListener(postAnimationTrigger.decrementOnAnimationEnd());
554        mDimAnimator.start();
555
556        if (screenPinningEnabled) {
557            showActionButton(true /* fadeIn */, duration /* fadeInDuration */);
558        }
559
560        if (mIncompatibleAppToastView != null &&
561                mIncompatibleAppToastView.getVisibility() == View.VISIBLE) {
562            mIncompatibleAppToastView.animate()
563                    .alpha(1f)
564                    .setDuration(duration)
565                    .setInterpolator(Interpolators.ALPHA_IN)
566                    .start();
567        }
568    }
569
570    @Override
571    public void onStartLaunchTargetLaunchAnimation(int duration, boolean screenPinningRequested,
572            ReferenceCountedTrigger postAnimationTrigger) {
573        Utilities.cancelAnimationWithoutCallbacks(mDimAnimator);
574
575        // Un-dim the view before/while launching the target
576        AnimationProps animation = new AnimationProps(duration, Interpolators.ALPHA_OUT);
577        mDimAnimator = animation.apply(AnimationProps.DIM_ALPHA, ObjectAnimator.ofFloat(this,
578                DIM_ALPHA, getDimAlpha(), 0));
579        mDimAnimator.start();
580
581        postAnimationTrigger.increment();
582        hideActionButton(true /* fadeOut */, duration,
583                !screenPinningRequested /* scaleDown */,
584                postAnimationTrigger.decrementOnAnimationEnd());
585    }
586
587    @Override
588    public void onStartFrontTaskEnterAnimation(boolean screenPinningEnabled) {
589        if (screenPinningEnabled) {
590            showActionButton(false /* fadeIn */, 0 /* fadeInDuration */);
591        }
592    }
593
594    /**** TaskCallbacks Implementation ****/
595
596    public void onTaskBound(Task t, boolean touchExplorationEnabled, int displayOrientation,
597            Rect displayRect) {
598        SystemServicesProxy ssp = Recents.getSystemServices();
599        mTouchExplorationEnabled = touchExplorationEnabled;
600        mTask = t;
601        mTask.addCallback(this);
602        mIsDisabledInSafeMode = !mTask.isSystemApp && ssp.isInSafeMode();
603        mThumbnailView.bindToTask(mTask, mIsDisabledInSafeMode, displayOrientation, displayRect);
604        mHeaderView.bindToTask(mTask, mTouchExplorationEnabled, mIsDisabledInSafeMode);
605
606        if (!t.isDockable && ssp.hasDockedTask()) {
607            if (mIncompatibleAppToastView == null) {
608                mIncompatibleAppToastView = Utilities.findViewStubById(this,
609                        R.id.incompatible_app_toast_stub).inflate();
610                TextView msg = (TextView) findViewById(com.android.internal.R.id.message);
611                msg.setText(R.string.recents_incompatible_app_message);
612            }
613            mIncompatibleAppToastView.setVisibility(View.VISIBLE);
614        } else if (mIncompatibleAppToastView != null) {
615            mIncompatibleAppToastView.setVisibility(View.INVISIBLE);
616        }
617    }
618
619    @Override
620    public void onTaskDataLoaded(Task task, ActivityManager.TaskThumbnailInfo thumbnailInfo) {
621        // Update each of the views to the new task data
622        mThumbnailView.onTaskDataLoaded(thumbnailInfo);
623        mHeaderView.onTaskDataLoaded();
624    }
625
626    @Override
627    public void onTaskDataUnloaded() {
628        // Unbind each of the views from the task and remove the task callback
629        mTask.removeCallback(this);
630        mThumbnailView.unbindFromTask();
631        mHeaderView.unbindFromTask(mTouchExplorationEnabled);
632    }
633
634    @Override
635    public void onTaskStackIdChanged() {
636        // Force rebind the header, the thumbnail does not change due to stack changes
637        mHeaderView.bindToTask(mTask, mTouchExplorationEnabled, mIsDisabledInSafeMode);
638        mHeaderView.onTaskDataLoaded();
639    }
640
641    /**** View.OnClickListener Implementation ****/
642
643    @Override
644     public void onClick(final View v) {
645        if (mIsDisabledInSafeMode) {
646            Context context = getContext();
647            String msg = context.getString(R.string.recents_launch_disabled_message, mTask.title);
648            if (mDisabledAppToast != null) {
649                mDisabledAppToast.cancel();
650            }
651            mDisabledAppToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
652            mDisabledAppToast.show();
653            return;
654        }
655
656        boolean screenPinningRequested = false;
657        if (v == mActionButtonView) {
658            // Reset the translation of the action button before we animate it out
659            mActionButtonView.setTranslationZ(0f);
660            screenPinningRequested = true;
661        }
662        EventBus.getDefault().send(new LaunchTaskEvent(this, mTask, null, INVALID_STACK_ID,
663                screenPinningRequested));
664
665        MetricsLogger.action(v.getContext(), MetricsEvent.ACTION_OVERVIEW_SELECT,
666                mTask.key.getComponent().toString());
667    }
668
669    /**** View.OnLongClickListener Implementation ****/
670
671    @Override
672    public boolean onLongClick(View v) {
673        SystemServicesProxy ssp = Recents.getSystemServices();
674        // Since we are clipping the view to the bounds, manually do the hit test
675        Rect clipBounds = new Rect(mViewBounds.mClipBounds);
676        clipBounds.scale(getScaleX());
677        boolean inBounds = clipBounds.contains(mDownTouchPos.x, mDownTouchPos.y);
678        if (v == this && inBounds && !ssp.hasDockedTask()) {
679            // Start listening for drag events
680            setClipViewInStack(false);
681
682            mDownTouchPos.x += ((1f - getScaleX()) * getWidth()) / 2;
683            mDownTouchPos.y += ((1f - getScaleY()) * getHeight()) / 2;
684
685            EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
686            EventBus.getDefault().send(new DragStartEvent(mTask, this, mDownTouchPos));
687            return true;
688        }
689        return false;
690    }
691
692    /**** Events ****/
693
694    public final void onBusEvent(DragEndEvent event) {
695        if (!(event.dropTarget instanceof TaskStack.DockState)) {
696            event.addPostAnimationCallback(() -> {
697                // Reset the clip state for the drag view after the end animation completes
698                setClipViewInStack(true);
699            });
700        }
701        EventBus.getDefault().unregister(this);
702    }
703
704    public final void onBusEvent(DragEndCancelledEvent event) {
705        // Reset the clip state for the drag view after the cancel animation completes
706        event.addPostAnimationCallback(() -> {
707            setClipViewInStack(true);
708        });
709    }
710}
711