TaskView.java revision 9f49df933f01a32d04bdf92d53c943065aa8ddf7
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.TimeInterpolator;
20import android.animation.ValueAnimator;
21import android.content.Context;
22import android.graphics.Canvas;
23import android.graphics.Outline;
24import android.graphics.Path;
25import android.graphics.Point;
26import android.graphics.Rect;
27import android.graphics.RectF;
28import android.util.AttributeSet;
29import android.view.MotionEvent;
30import android.view.View;
31import android.view.animation.AccelerateInterpolator;
32import android.widget.FrameLayout;
33import com.android.systemui.R;
34import com.android.systemui.recents.BakedBezierInterpolator;
35import com.android.systemui.recents.Constants;
36import com.android.systemui.recents.RecentsConfiguration;
37import com.android.systemui.recents.model.Task;
38
39
40/* A task view */
41public class TaskView extends FrameLayout implements View.OnClickListener,
42        Task.TaskCallbacks {
43    /** The TaskView callbacks */
44    interface TaskViewCallbacks {
45        public void onTaskIconClicked(TaskView tv);
46        public void onTaskInfoPanelShown(TaskView tv);
47        public void onTaskInfoPanelHidden(TaskView tv);
48        public void onTaskAppInfoClicked(TaskView tv);
49
50        // public void onTaskViewReboundToTask(TaskView tv, Task t);
51    }
52
53    int mDim;
54    int mMaxDim;
55    TimeInterpolator mDimInterpolator = new AccelerateInterpolator();
56
57    Task mTask;
58    boolean mTaskDataLoaded;
59    boolean mTaskInfoPaneVisible;
60    Point mLastTouchDown = new Point();
61    Path mRoundedRectClipPath = new Path();
62
63    TaskThumbnailView mThumbnailView;
64    TaskBarView mBarView;
65    TaskInfoView mInfoView;
66    TaskViewCallbacks mCb;
67
68
69    public TaskView(Context context) {
70        this(context, null);
71    }
72
73    public TaskView(Context context, AttributeSet attrs) {
74        this(context, attrs, 0);
75    }
76
77    public TaskView(Context context, AttributeSet attrs, int defStyleAttr) {
78        this(context, attrs, defStyleAttr, 0);
79    }
80
81    public TaskView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
82        super(context, attrs, defStyleAttr, defStyleRes);
83        setWillNotDraw(false);
84    }
85
86    @Override
87    protected void onFinishInflate() {
88        RecentsConfiguration config = RecentsConfiguration.getInstance();
89        mMaxDim = config.taskStackMaxDim;
90
91        // Bind the views
92        mThumbnailView = (TaskThumbnailView) findViewById(R.id.task_view_thumbnail);
93        mBarView = (TaskBarView) findViewById(R.id.task_view_bar);
94        mInfoView = (TaskInfoView) findViewById(R.id.task_view_info_pane);
95
96        if (mTaskDataLoaded) {
97            onTaskDataLoaded(false);
98        }
99    }
100
101    @Override
102    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
103        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
104
105        // Update the rounded rect clip path
106        RecentsConfiguration config = RecentsConfiguration.getInstance();
107        float radius = config.taskViewRoundedCornerRadiusPx;
108        mRoundedRectClipPath.reset();
109        mRoundedRectClipPath.addRoundRect(new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight()),
110                radius, radius, Path.Direction.CW);
111
112        // Update the outline
113        Outline o = new Outline();
114        o.setRoundRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), radius);
115        setOutline(o);
116    }
117
118    @Override
119    public boolean onInterceptTouchEvent(MotionEvent ev) {
120        switch (ev.getAction()) {
121            case MotionEvent.ACTION_DOWN:
122            case MotionEvent.ACTION_MOVE:
123                mLastTouchDown.set((int) ev.getX(), (int) ev.getY());
124                break;
125        }
126        return super.onInterceptTouchEvent(ev);
127    }
128
129    /** Set callback */
130    void setCallbacks(TaskViewCallbacks cb) {
131        mCb = cb;
132    }
133
134    /** Gets the task */
135    Task getTask() {
136        return mTask;
137    }
138
139    /** Synchronizes this view's properties with the task's transform */
140    void updateViewPropertiesToTaskTransform(TaskViewTransform animateFromTransform,
141                                             TaskViewTransform toTransform, int duration) {
142        RecentsConfiguration config = RecentsConfiguration.getInstance();
143        int minZ = config.taskViewTranslationZMinPx;
144        int incZ = config.taskViewTranslationZIncrementPx;
145
146        if (duration > 0) {
147            if (animateFromTransform != null) {
148                setTranslationY(animateFromTransform.translationY);
149                if (Constants.DebugFlags.App.EnableShadows) {
150                    setTranslationZ(Math.max(minZ, minZ + (animateFromTransform.t * incZ)));
151                }
152                setScaleX(animateFromTransform.scale);
153                setScaleY(animateFromTransform.scale);
154                setAlpha(animateFromTransform.alpha);
155            }
156            if (Constants.DebugFlags.App.EnableShadows) {
157                animate().translationZ(Math.max(minZ, minZ + (toTransform.t * incZ)));
158            }
159            animate().translationY(toTransform.translationY)
160                    .scaleX(toTransform.scale)
161                    .scaleY(toTransform.scale)
162                    .alpha(toTransform.alpha)
163                    .setDuration(duration)
164                    .setInterpolator(BakedBezierInterpolator.INSTANCE)
165                    .withLayer()
166                    .setUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
167                        @Override
168                        public void onAnimationUpdate(ValueAnimator animation) {
169                            updateDimOverlayFromScale();
170                        }
171                    })
172                    .start();
173        } else {
174            setTranslationY(toTransform.translationY);
175            if (Constants.DebugFlags.App.EnableShadows) {
176                setTranslationZ(Math.max(minZ, minZ + (toTransform.t * incZ)));
177            }
178            setScaleX(toTransform.scale);
179            setScaleY(toTransform.scale);
180            setAlpha(toTransform.alpha);
181        }
182        updateDimOverlayFromScale();
183        invalidate();
184    }
185
186    /** Resets this view's properties */
187    void resetViewProperties() {
188        setTranslationX(0f);
189        setTranslationY(0f);
190        if (Constants.DebugFlags.App.EnableShadows) {
191            setTranslationZ(0f);
192        }
193        setScaleX(1f);
194        setScaleY(1f);
195        setAlpha(1f);
196        invalidate();
197    }
198
199    /**
200     * When we are un/filtering, this method will set up the transform that we are animating to,
201     * in order to hide the task.
202     */
203    void prepareTaskTransformForFilterTaskHidden(TaskViewTransform toTransform) {
204        // Fade the view out and slide it away
205        toTransform.alpha = 0f;
206        toTransform.translationY += 200;
207    }
208
209    /**
210     * When we are un/filtering, this method will setup the transform that we are animating from,
211     * in order to show the task.
212     */
213    void prepareTaskTransformForFilterTaskVisible(TaskViewTransform fromTransform) {
214        // Fade the view in
215        fromTransform.alpha = 0f;
216    }
217
218    /** Animates this task view as it enters recents */
219    public void animateOnEnterRecents() {
220        RecentsConfiguration config = RecentsConfiguration.getInstance();
221        mBarView.setAlpha(0f);
222        mBarView.animate()
223                .alpha(1f)
224                .setStartDelay(235)
225                .setInterpolator(BakedBezierInterpolator.INSTANCE)
226                .setDuration(config.taskBarEnterAnimDuration)
227                .withLayer()
228                .start();
229    }
230
231    /** Animates this task view as it exits recents */
232    public void animateOnLeavingRecents(final Runnable r) {
233        RecentsConfiguration config = RecentsConfiguration.getInstance();
234        mBarView.animate()
235            .alpha(0f)
236            .setStartDelay(0)
237            .setInterpolator(BakedBezierInterpolator.INSTANCE)
238            .setDuration(config.taskBarExitAnimDuration)
239            .withLayer()
240            .withEndAction(new Runnable() {
241                @Override
242                public void run() {
243                    post(r);
244                }
245            })
246            .start();
247    }
248
249    /** Animates the deletion of this task view */
250    public void animateRemoval(final Runnable r) {
251        RecentsConfiguration config = RecentsConfiguration.getInstance();
252        animate().translationX(config.taskViewRemoveAnimTranslationXPx)
253            .alpha(0f)
254            .setStartDelay(0)
255            .setInterpolator(BakedBezierInterpolator.INSTANCE)
256            .setDuration(config.taskViewRemoveAnimDuration)
257            .withLayer()
258            .withEndAction(new Runnable() {
259                @Override
260                public void run() {
261                    post(r);
262                }
263            })
264            .start();
265    }
266
267    /** Returns the rect we want to clip (it may not be the full rect) */
268    Rect getClippingRect(Rect outRect) {
269        getHitRect(outRect);
270        // XXX: We should get the hit rect of the thumbnail view and intersect, but this is faster
271        outRect.right = outRect.left + mThumbnailView.getRight();
272        outRect.bottom = outRect.top + mThumbnailView.getBottom();
273        return outRect;
274    }
275
276    /** Returns whether this task has an info pane visible */
277    boolean isInfoPaneVisible() {
278        return mTaskInfoPaneVisible;
279    }
280
281    /** Shows the info pane if it is not visible. */
282    void showInfoPane(Rect taskVisibleRect) {
283        if (mTaskInfoPaneVisible) return;
284
285        // Remove the bar view from the visible rect and update the info pane contents
286        taskVisibleRect.top += mBarView.getMeasuredHeight();
287        mInfoView.updateContents(taskVisibleRect);
288
289        // Show the info pane and animate it into view
290        mInfoView.setVisibility(View.VISIBLE);
291        mInfoView.animateCircularClip(mLastTouchDown, 0f, 1f, null, true);
292        mInfoView.setOnClickListener(this);
293        mTaskInfoPaneVisible = true;
294
295        // Notify any callbacks
296        if (mCb != null) {
297            mCb.onTaskInfoPanelShown(this);
298        }
299    }
300
301    /** Hides the info pane if it is visible. */
302    void hideInfoPane() {
303        if (!mTaskInfoPaneVisible) return;
304        RecentsConfiguration config = RecentsConfiguration.getInstance();
305
306        // Cancel any circular clip animation
307        mInfoView.cancelCircularClipAnimation();
308
309        // Animate the info pane out
310        mInfoView.animate()
311                .alpha(0f)
312                .setDuration(config.taskViewInfoPaneAnimDuration)
313                .setInterpolator(BakedBezierInterpolator.INSTANCE)
314                .withLayer()
315                .withEndAction(new Runnable() {
316                    @Override
317                    public void run() {
318                        mInfoView.setVisibility(View.INVISIBLE);
319                        mInfoView.setOnClickListener(null);
320
321                        mInfoView.setAlpha(1f);
322                    }
323                })
324                .start();
325        mTaskInfoPaneVisible = false;
326
327        // Notify any callbacks
328        if (mCb != null) {
329            mCb.onTaskInfoPanelHidden(this);
330        }
331    }
332
333    /** Enable the hw layers on this task view */
334    void enableHwLayers() {
335        mThumbnailView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
336    }
337
338    /** Disable the hw layers on this task view */
339    void disableHwLayers() {
340        mThumbnailView.setLayerType(View.LAYER_TYPE_NONE, null);
341    }
342
343    /** Update the dim as a function of the scale of this view. */
344    void updateDimOverlayFromScale() {
345        float minScale = Constants.Values.TaskStackView.StackPeekMinScale;
346        float scaleRange = 1f - minScale;
347        float dim = (1f - getScaleX()) / scaleRange;
348        dim = mDimInterpolator.getInterpolation(Math.min(dim, 1f));
349        mDim = Math.max(0, Math.min(mMaxDim, (int) (dim * 255)));
350        invalidate();
351    }
352
353    @Override
354    public void draw(Canvas canvas) {
355        // Apply the rounded rect clip path on the whole view
356        canvas.clipPath(mRoundedRectClipPath);
357
358        super.draw(canvas);
359
360        // Apply the dim if necessary
361        if (mDim > 0) {
362            canvas.drawColor(mDim << 24);
363        }
364    }
365
366    /**** TaskCallbacks Implementation ****/
367
368    /** Binds this task view to the task */
369    public void onTaskBound(Task t) {
370        mTask = t;
371        mTask.setCallbacks(this);
372    }
373
374    @Override
375    public void onTaskDataLoaded(boolean reloadingTaskData) {
376        if (mThumbnailView != null && mBarView != null && mInfoView != null) {
377            // Bind each of the views to the new task data
378            mThumbnailView.rebindToTask(mTask, reloadingTaskData);
379            mBarView.rebindToTask(mTask, reloadingTaskData);
380            mInfoView.rebindToTask(mTask, reloadingTaskData);
381            // Rebind any listeners
382            mBarView.mApplicationIcon.setOnClickListener(this);
383            mInfoView.mAppInfoButton.setOnClickListener(this);
384        }
385        mTaskDataLoaded = true;
386    }
387
388    @Override
389    public void onTaskDataUnloaded() {
390        if (mThumbnailView != null && mBarView != null && mInfoView != null) {
391            // Unbind each of the views from the task data and remove the task callback
392            mTask.setCallbacks(null);
393            mThumbnailView.unbindFromTask();
394            mBarView.unbindFromTask();
395            // Unbind any listeners
396            mBarView.mApplicationIcon.setOnClickListener(null);
397            mInfoView.mAppInfoButton.setOnClickListener(null);
398        }
399        mTaskDataLoaded = false;
400    }
401
402    @Override
403    public void onClick(View v) {
404        if (v == mInfoView) {
405            hideInfoPane();
406        } else if (v == mBarView.mApplicationIcon) {
407            mCb.onTaskIconClicked(this);
408        } else if (v == mInfoView.mAppInfoButton) {
409            mCb.onTaskAppInfoClicked(this);
410        }
411    }
412}