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