TaskView.java revision d916d103ebde6bee3b7335c8f4b0dd5271866e7f
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    @ViewDebug.ExportedProperty(category="recents")
137    private boolean mClipViewInStack = true;
138    @ViewDebug.ExportedProperty(category="recents")
139    private boolean mTouchExplorationEnabled;
140    @ViewDebug.ExportedProperty(category="recents")
141    private boolean mIsDisabledInSafeMode;
142    @ViewDebug.ExportedProperty(deepExport=true, prefix="view_bounds_")
143    private AnimateableViewBounds mViewBounds;
144
145    private AnimatorSet mTransformAnimation;
146    private ObjectAnimator mDimAnimator;
147    private ObjectAnimator mOutlineAnimator;
148    private final TaskViewTransform mTargetAnimationTransform = new TaskViewTransform();
149    private ArrayList<Animator> mTmpAnimators = new ArrayList<>();
150
151    @ViewDebug.ExportedProperty(deepExport=true, prefix="thumbnail_")
152    protected TaskViewThumbnail mThumbnailView;
153    @ViewDebug.ExportedProperty(deepExport=true, prefix="header_")
154    protected TaskViewHeader mHeaderView;
155    private View mActionButtonView;
156    private View mIncompatibleAppToastView;
157    private TaskViewCallbacks mCb;
158
159    @ViewDebug.ExportedProperty(category="recents")
160    private Point mDownTouchPos = new Point();
161
162    private Toast mDisabledAppToast;
163
164    public TaskView(Context context) {
165        this(context, null);
166    }
167
168    public TaskView(Context context, AttributeSet attrs) {
169        this(context, attrs, 0);
170    }
171
172    public TaskView(Context context, AttributeSet attrs, int defStyleAttr) {
173        this(context, attrs, defStyleAttr, 0);
174    }
175
176    public TaskView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
177        super(context, attrs, defStyleAttr, defStyleRes);
178        RecentsConfiguration config = Recents.getConfiguration();
179        Resources res = context.getResources();
180        mViewBounds = createOutlineProvider();
181        if (config.fakeShadows) {
182            setBackground(new FakeShadowDrawable(res, config));
183        }
184        setOutlineProvider(mViewBounds);
185        setOnLongClickListener(this);
186        setAccessibilityDelegate(new TaskViewAccessibilityDelegate(this));
187    }
188
189    /** Set callback */
190    void setCallbacks(TaskViewCallbacks cb) {
191        mCb = cb;
192    }
193
194    /**
195     * Called from RecentsActivity when it is relaunched.
196     */
197    void onReload(boolean isResumingFromVisible) {
198        if (!Recents.getSystemServices().hasFreeformWorkspaceSupport()) {
199            resetNoUserInteractionState();
200        }
201        if (!isResumingFromVisible) {
202            resetViewProperties();
203        }
204    }
205
206    /** Gets the task */
207    public Task getTask() {
208        return mTask;
209    }
210
211    /* Create an outline provider to clip and outline the view */
212    protected AnimateableViewBounds createOutlineProvider() {
213        return new AnimateableViewBounds(this, mContext.getResources().getDimensionPixelSize(
214            R.dimen.recents_task_view_shadow_rounded_corners_radius));
215    }
216
217    /** Returns the view bounds. */
218    AnimateableViewBounds getViewBounds() {
219        return mViewBounds;
220    }
221
222    @Override
223    protected void onFinishInflate() {
224        // Bind the views
225        mHeaderView = (TaskViewHeader) findViewById(R.id.task_view_bar);
226        mThumbnailView = (TaskViewThumbnail) findViewById(R.id.task_view_thumbnail);
227        mThumbnailView.updateClipToTaskBar(mHeaderView);
228        mActionButtonView = findViewById(R.id.lock_to_app_fab);
229        mActionButtonView.setOutlineProvider(new ViewOutlineProvider() {
230            @Override
231            public void getOutline(View view, Outline outline) {
232                // Set the outline to match the FAB background
233                outline.setOval(0, 0, mActionButtonView.getWidth(), mActionButtonView.getHeight());
234                outline.setAlpha(0.35f);
235            }
236        });
237        mActionButtonView.setOnClickListener(this);
238        mActionButtonTranslationZ = mActionButtonView.getTranslationZ();
239    }
240
241    /**
242     * Update the task view when the configuration changes.
243     */
244    protected void onConfigurationChanged() {
245        mHeaderView.onConfigurationChanged();
246    }
247
248    @Override
249    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
250        super.onSizeChanged(w, h, oldw, oldh);
251        if (w > 0 && h > 0) {
252            mHeaderView.onTaskViewSizeChanged(w, h);
253            mThumbnailView.onTaskViewSizeChanged(w, h);
254
255            mActionButtonView.setTranslationX(w - getMeasuredWidth());
256            mActionButtonView.setTranslationY(h - getMeasuredHeight());
257        }
258    }
259
260    @Override
261    public boolean hasOverlappingRendering() {
262        return false;
263    }
264
265    @Override
266    public boolean onInterceptTouchEvent(MotionEvent ev) {
267        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
268            mDownTouchPos.set((int) (ev.getX() * getScaleX()), (int) (ev.getY() * getScaleY()));
269        }
270        return super.onInterceptTouchEvent(ev);
271    }
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        Utilities.cancelAnimationWithoutCallbacks(mTransformAnimation);
367        Utilities.cancelAnimationWithoutCallbacks(mDimAnimator);
368        Utilities.cancelAnimationWithoutCallbacks(mOutlineAnimator);
369    }
370
371    /** Enables/disables handling touch on this task view. */
372    public void setTouchEnabled(boolean enabled) {
373        setOnClickListener(enabled ? this : null);
374    }
375
376    /** Animates this task view if the user does not interact with the stack after a certain time. */
377    public void startNoUserInteractionAnimation() {
378        mHeaderView.startNoUserInteractionAnimation();
379    }
380
381    /** Mark this task view that the user does has not interacted with the stack after a certain time. */
382    void setNoUserInteractionState() {
383        mHeaderView.setNoUserInteractionState();
384    }
385
386    /** Resets the state tracking that the user has not interacted with the stack after a certain time. */
387    void resetNoUserInteractionState() {
388        mHeaderView.resetNoUserInteractionState();
389    }
390
391    /** Dismisses this task. */
392    void dismissTask() {
393        // Animate out the view and call the callback
394        final TaskView tv = this;
395        DismissTaskViewEvent dismissEvent = new DismissTaskViewEvent(tv);
396        dismissEvent.addPostAnimationCallback(new Runnable() {
397            @Override
398            public void run() {
399                EventBus.getDefault().send(new TaskViewDismissedEvent(mTask, tv,
400                        new AnimationProps(TaskStackView.DEFAULT_SYNC_STACK_DURATION,
401                                Interpolators.FAST_OUT_SLOW_IN)));
402            }
403        });
404        EventBus.getDefault().send(dismissEvent);
405    }
406
407    /**
408     * Returns whether this view should be clipped, or any views below should clip against this
409     * view.
410     */
411    boolean shouldClipViewInStack() {
412        // Never clip for freeform tasks or if invisible
413        if (mTask.isFreeformTask() || getVisibility() != View.VISIBLE) {
414            return false;
415        }
416        return mClipViewInStack;
417    }
418
419    /** Sets whether this view should be clipped, or clipped against. */
420    void setClipViewInStack(boolean clip) {
421        if (clip != mClipViewInStack) {
422            mClipViewInStack = clip;
423            if (mCb != null) {
424                mCb.onTaskViewClipStateChanged(this);
425            }
426        }
427    }
428
429    public TaskViewHeader getHeaderView() {
430        return mHeaderView;
431    }
432
433    /**
434     * Sets the current dim.
435     */
436    public void setDimAlpha(float dimAlpha) {
437        mDimAlpha = dimAlpha;
438        mThumbnailView.setDimAlpha(dimAlpha);
439        mHeaderView.setDimAlpha(dimAlpha);
440    }
441
442    /**
443     * Sets the current dim without updating the header's dim.
444     */
445    public void setDimAlphaWithoutHeader(float dimAlpha) {
446        mDimAlpha = dimAlpha;
447        mThumbnailView.setDimAlpha(dimAlpha);
448    }
449
450    /**
451     * Returns the current dim.
452     */
453    public float getDimAlpha() {
454        return mDimAlpha;
455    }
456
457    /**
458     * Explicitly sets the focused state of this task.
459     */
460    public void setFocusedState(boolean isFocused, boolean requestViewFocus) {
461        if (isFocused) {
462            if (requestViewFocus && !isFocused()) {
463                requestFocus();
464            }
465        } else {
466            if (isAccessibilityFocused() && mTouchExplorationEnabled) {
467                clearAccessibilityFocus();
468            }
469        }
470    }
471
472    /**
473     * Shows the action button.
474     * @param fadeIn whether or not to animate the action button in.
475     * @param fadeInDuration the duration of the action button animation, only used if
476     *                       {@param fadeIn} is true.
477     */
478    public void showActionButton(boolean fadeIn, int fadeInDuration) {
479        mActionButtonView.setVisibility(View.VISIBLE);
480
481        if (fadeIn && mActionButtonView.getAlpha() < 1f) {
482            mActionButtonView.animate()
483                    .alpha(1f)
484                    .scaleX(1f)
485                    .scaleY(1f)
486                    .setDuration(fadeInDuration)
487                    .setInterpolator(Interpolators.ALPHA_IN)
488                    .start();
489        } else {
490            mActionButtonView.setScaleX(1f);
491            mActionButtonView.setScaleY(1f);
492            mActionButtonView.setAlpha(1f);
493            mActionButtonView.setTranslationZ(mActionButtonTranslationZ);
494        }
495    }
496
497    /**
498     * Immediately hides the action button.
499     *
500     * @param fadeOut whether or not to animate the action button out.
501     */
502    public void hideActionButton(boolean fadeOut, int fadeOutDuration, boolean scaleDown,
503            final Animator.AnimatorListener animListener) {
504        if (fadeOut && mActionButtonView.getAlpha() > 0f) {
505            if (scaleDown) {
506                float toScale = 0.9f;
507                mActionButtonView.animate()
508                        .scaleX(toScale)
509                        .scaleY(toScale);
510            }
511            mActionButtonView.animate()
512                    .alpha(0f)
513                    .setDuration(fadeOutDuration)
514                    .setInterpolator(Interpolators.ALPHA_OUT)
515                    .withEndAction(new Runnable() {
516                        @Override
517                        public void run() {
518                            if (animListener != null) {
519                                animListener.onAnimationEnd(null);
520                            }
521                            mActionButtonView.setVisibility(View.INVISIBLE);
522                        }
523                    })
524                    .start();
525        } else {
526            mActionButtonView.setAlpha(0f);
527            mActionButtonView.setVisibility(View.INVISIBLE);
528            if (animListener != null) {
529                animListener.onAnimationEnd(null);
530            }
531        }
532    }
533
534    /**** TaskStackAnimationHelper.Callbacks Implementation ****/
535
536    @Override
537    public void onPrepareLaunchTargetForEnterAnimation() {
538        // These values will be animated in when onStartLaunchTargetEnterAnimation() is called
539        setDimAlphaWithoutHeader(0);
540        mActionButtonView.setAlpha(0f);
541        if (mIncompatibleAppToastView != null &&
542                mIncompatibleAppToastView.getVisibility() == View.VISIBLE) {
543            mIncompatibleAppToastView.setAlpha(0f);
544        }
545    }
546
547    @Override
548    public void onStartLaunchTargetEnterAnimation(TaskViewTransform transform, int duration,
549            boolean screenPinningEnabled, ReferenceCountedTrigger postAnimationTrigger) {
550        Utilities.cancelAnimationWithoutCallbacks(mDimAnimator);
551
552        // Dim the view after the app window transitions down into recents
553        postAnimationTrigger.increment();
554        AnimationProps animation = new AnimationProps(duration, Interpolators.ALPHA_OUT);
555        mDimAnimator = animation.apply(AnimationProps.DIM_ALPHA, ObjectAnimator.ofFloat(this,
556                DIM_ALPHA_WITHOUT_HEADER, getDimAlpha(), transform.dimAlpha));
557        mDimAnimator.addListener(postAnimationTrigger.decrementOnAnimationEnd());
558        mDimAnimator.start();
559
560        if (screenPinningEnabled) {
561            showActionButton(true /* fadeIn */, duration /* fadeInDuration */);
562        }
563
564        if (mIncompatibleAppToastView != null &&
565                mIncompatibleAppToastView.getVisibility() == View.VISIBLE) {
566            mIncompatibleAppToastView.animate()
567                    .alpha(1f)
568                    .setDuration(duration)
569                    .setInterpolator(Interpolators.ALPHA_IN)
570                    .start();
571        }
572    }
573
574    @Override
575    public void onStartLaunchTargetLaunchAnimation(int duration, boolean screenPinningRequested,
576            ReferenceCountedTrigger postAnimationTrigger) {
577        Utilities.cancelAnimationWithoutCallbacks(mDimAnimator);
578
579        // Un-dim the view before/while launching the target
580        AnimationProps animation = new AnimationProps(duration, Interpolators.ALPHA_OUT);
581        mDimAnimator = animation.apply(AnimationProps.DIM_ALPHA, ObjectAnimator.ofFloat(this,
582                DIM_ALPHA, getDimAlpha(), 0));
583        mDimAnimator.start();
584
585        postAnimationTrigger.increment();
586        hideActionButton(true /* fadeOut */, duration,
587                !screenPinningRequested /* scaleDown */,
588                postAnimationTrigger.decrementOnAnimationEnd());
589    }
590
591    @Override
592    public void onStartFrontTaskEnterAnimation(boolean screenPinningEnabled) {
593        if (screenPinningEnabled) {
594            showActionButton(false /* fadeIn */, 0 /* fadeInDuration */);
595        }
596    }
597
598    /**** TaskCallbacks Implementation ****/
599
600    public void onTaskBound(Task t, boolean touchExplorationEnabled, int displayOrientation,
601            Rect displayRect) {
602        SystemServicesProxy ssp = Recents.getSystemServices();
603        mTouchExplorationEnabled = touchExplorationEnabled;
604        mTask = t;
605        mTask.addCallback(this);
606        mIsDisabledInSafeMode = !mTask.isSystemApp && ssp.isInSafeMode();
607        mThumbnailView.bindToTask(mTask, mIsDisabledInSafeMode, displayOrientation, displayRect);
608        mHeaderView.bindToTask(mTask, mTouchExplorationEnabled, mIsDisabledInSafeMode);
609
610        if (!t.isDockable && ssp.hasDockedTask()) {
611            if (mIncompatibleAppToastView == null) {
612                mIncompatibleAppToastView = Utilities.findViewStubById(this,
613                        R.id.incompatible_app_toast_stub).inflate();
614                TextView msg = (TextView) findViewById(com.android.internal.R.id.message);
615                msg.setText(R.string.recents_incompatible_app_message);
616            }
617            mIncompatibleAppToastView.setVisibility(View.VISIBLE);
618        } else if (mIncompatibleAppToastView != null) {
619            mIncompatibleAppToastView.setVisibility(View.INVISIBLE);
620        }
621    }
622
623    @Override
624    public void onTaskDataLoaded(Task task, ThumbnailData thumbnailData) {
625        // Update each of the views to the new task data
626        mThumbnailView.onTaskDataLoaded(thumbnailData);
627        mHeaderView.onTaskDataLoaded();
628    }
629
630    @Override
631    public void onTaskDataUnloaded() {
632        // Unbind each of the views from the task and remove the task callback
633        mTask.removeCallback(this);
634        mThumbnailView.unbindFromTask();
635        mHeaderView.unbindFromTask(mTouchExplorationEnabled);
636    }
637
638    @Override
639    public void onTaskStackIdChanged() {
640        // Force rebind the header, the thumbnail does not change due to stack changes
641        mHeaderView.bindToTask(mTask, mTouchExplorationEnabled, mIsDisabledInSafeMode);
642        mHeaderView.onTaskDataLoaded();
643    }
644
645    /**** View.OnClickListener Implementation ****/
646
647    @Override
648     public void onClick(final View v) {
649        if (mIsDisabledInSafeMode) {
650            Context context = getContext();
651            String msg = context.getString(R.string.recents_launch_disabled_message, mTask.title);
652            if (mDisabledAppToast != null) {
653                mDisabledAppToast.cancel();
654            }
655            mDisabledAppToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
656            mDisabledAppToast.show();
657            return;
658        }
659
660        boolean screenPinningRequested = false;
661        if (v == mActionButtonView) {
662            // Reset the translation of the action button before we animate it out
663            mActionButtonView.setTranslationZ(0f);
664            screenPinningRequested = true;
665        }
666        EventBus.getDefault().send(new LaunchTaskEvent(this, mTask, null, INVALID_STACK_ID,
667                screenPinningRequested));
668
669        MetricsLogger.action(v.getContext(), MetricsEvent.ACTION_OVERVIEW_SELECT,
670                mTask.key.getComponent().toString());
671    }
672
673    /**** View.OnLongClickListener Implementation ****/
674
675    @Override
676    public boolean onLongClick(View v) {
677        SystemServicesProxy ssp = Recents.getSystemServices();
678        boolean inBounds = false;
679        Rect clipBounds = new Rect(mViewBounds.mClipBounds);
680        if (!clipBounds.isEmpty()) {
681            // If we are clipping the view to the bounds, manually do the hit test.
682            clipBounds.scale(getScaleX());
683            inBounds = clipBounds.contains(mDownTouchPos.x, mDownTouchPos.y);
684        } else {
685            // Otherwise just make sure we're within the view's bounds.
686            inBounds = mDownTouchPos.x <= getWidth() && mDownTouchPos.y <= getHeight();
687        }
688        if (v == this && inBounds && !ssp.hasDockedTask()) {
689            // Start listening for drag events
690            setClipViewInStack(false);
691
692            mDownTouchPos.x += ((1f - getScaleX()) * getWidth()) / 2;
693            mDownTouchPos.y += ((1f - getScaleY()) * getHeight()) / 2;
694
695            EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
696            EventBus.getDefault().send(new DragStartEvent(mTask, this, mDownTouchPos));
697            return true;
698        }
699        return false;
700    }
701
702    /**** Events ****/
703
704    public final void onBusEvent(DragEndEvent event) {
705        if (!(event.dropTarget instanceof TaskStack.DockState)) {
706            event.addPostAnimationCallback(() -> {
707                // Reset the clip state for the drag view after the end animation completes
708                setClipViewInStack(true);
709            });
710        }
711        EventBus.getDefault().unregister(this);
712    }
713
714    public final void onBusEvent(DragEndCancelledEvent event) {
715        // Reset the clip state for the drag view after the cancel animation completes
716        event.addPostAnimationCallback(() -> {
717            setClipViewInStack(true);
718        });
719    }
720
721    public void dump(String prefix, PrintWriter writer) {
722        String innerPrefix = prefix + "  ";
723
724        writer.print(prefix); writer.print("TaskView");
725        writer.print(" mTask="); writer.print(mTask.key.id);
726        writer.println();
727
728        mThumbnailView.dump(innerPrefix, writer);
729    }
730}
731