TaskView.java revision 11ca76a53c60a1898956614315ae929668c523d6
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.content.Context;
20import android.graphics.Rect;
21import android.util.AttributeSet;
22import android.view.View;
23import android.widget.FrameLayout;
24import com.android.systemui.R;
25import com.android.systemui.recents.BakedBezierInterpolator;
26import com.android.systemui.recents.RecentsConfiguration;
27import com.android.systemui.recents.Utilities;
28import com.android.systemui.recents.model.Task;
29
30
31/* A task view */
32public class TaskView extends FrameLayout implements View.OnClickListener, Task.TaskCallbacks {
33    /** The TaskView callbacks */
34    interface TaskViewCallbacks {
35        public void onTaskIconClicked(TaskView tv);
36        // public void onTaskViewReboundToTask(TaskView tv, Task t);
37    }
38
39    Task mTask;
40    boolean mTaskDataLoaded;
41
42    TaskThumbnailView mThumbnailView;
43    TaskBarView mBarView;
44    TaskViewCallbacks mCb;
45
46
47    public TaskView(Context context) {
48        this(context, null);
49    }
50
51    public TaskView(Context context, AttributeSet attrs) {
52        this(context, attrs, 0);
53    }
54
55    public TaskView(Context context, AttributeSet attrs, int defStyleAttr) {
56        this(context, attrs, defStyleAttr, 0);
57    }
58
59    public TaskView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
60        super(context, attrs, defStyleAttr, defStyleRes);
61    }
62
63    @Override
64    protected void onFinishInflate() {
65        // Bind the views
66        mThumbnailView = (TaskThumbnailView) findViewById(R.id.task_view_thumbnail);
67        mBarView = (TaskBarView) findViewById(R.id.task_view_bar);
68        mBarView.mApplicationIcon.setOnClickListener(this);
69        if (mTaskDataLoaded) {
70            onTaskDataLoaded(false);
71        }
72    }
73
74    /** Set callback */
75    void setCallbacks(TaskViewCallbacks cb) {
76        mCb = cb;
77    }
78
79    /** Gets the task */
80    Task getTask() {
81        return mTask;
82    }
83
84    /** Synchronizes this view's properties with the task's transform */
85    void updateViewPropertiesToTaskTransform(TaskViewTransform animateFromTransform,
86                                             TaskViewTransform toTransform, int duration) {
87        if (duration > 0) {
88            if (animateFromTransform != null) {
89                setTranslationY(animateFromTransform.translationY);
90                setScaleX(animateFromTransform.scale);
91                setScaleY(animateFromTransform.scale);
92                setAlpha(animateFromTransform.alpha);
93            }
94            animate().translationY(toTransform.translationY)
95                    .scaleX(toTransform.scale)
96                    .scaleY(toTransform.scale)
97                    .alpha(toTransform.alpha)
98                    .setDuration(duration)
99                    .setInterpolator(BakedBezierInterpolator.INSTANCE)
100                    .withLayer()
101                    .start();
102        } else {
103            setTranslationY(toTransform.translationY);
104            setScaleX(toTransform.scale);
105            setScaleY(toTransform.scale);
106            setAlpha(toTransform.alpha);
107        }
108    }
109
110    /** Resets this view's properties */
111    void resetViewProperties() {
112        setTranslationX(0f);
113        setTranslationY(0f);
114        setScaleX(1f);
115        setScaleY(1f);
116        setAlpha(1f);
117    }
118
119    /**
120     * When we are un/filtering, this method will set up the transform that we are animating to,
121     * in order to hide the task.
122     */
123    void prepareTaskTransformForFilterTaskHidden(TaskViewTransform toTransform) {
124        // Fade the view out and slide it away
125        toTransform.alpha = 0f;
126        toTransform.translationY += 200;
127    }
128
129    /**
130     * When we are un/filtering, this method will setup the transform that we are animating from,
131     * in order to show the task.
132     */
133    void prepareTaskTransformForFilterTaskVisible(TaskViewTransform fromTransform) {
134        // Fade the view in
135        fromTransform.alpha = 0f;
136    }
137
138    /** Animates this task view as it enters recents */
139    public void animateOnEnterRecents() {
140        RecentsConfiguration config = RecentsConfiguration.getInstance();
141        int translate = config.pxFromDp(10);
142        mBarView.setScaleX(1.25f);
143        mBarView.setScaleY(1.25f);
144        mBarView.setAlpha(0f);
145        mBarView.setTranslationX(translate / 2);
146        mBarView.setTranslationY(-translate);
147        mBarView.animate()
148                .alpha(1f)
149                .scaleX(1f)
150                .scaleY(1f)
151                .translationX(0)
152                .translationY(0)
153                .setStartDelay(235)
154                .setInterpolator(BakedBezierInterpolator.INSTANCE)
155                .setDuration(config.taskBarEnterAnimDuration)
156                .withLayer()
157                .start();
158    }
159
160    /** Animates this task view as it exits recents */
161    public void animateOnLeavingRecents(final Runnable r) {
162        RecentsConfiguration config = RecentsConfiguration.getInstance();
163        int translate = config.pxFromDp(10);
164        mBarView.animate()
165            .alpha(0f)
166            .scaleX(1.1f)
167            .scaleY(1.1f)
168            .translationX(translate / 2)
169            .translationY(-translate)
170            .setStartDelay(0)
171            .setInterpolator(BakedBezierInterpolator.INSTANCE)
172            .setDuration(Utilities.calculateTranslationAnimationDuration(translate))
173            .withLayer()
174            .withEndAction(new Runnable() {
175                @Override
176                public void run() {
177                    post(r);
178                }
179            })
180            .start();
181    }
182
183    /** Returns the rect we want to clip (it may not be the full rect) */
184    Rect getClippingRect(Rect outRect) {
185        getHitRect(outRect);
186        // XXX: We should get the hit rect of the thumbnail view and intersect, but this is faster
187        outRect.right = outRect.left + mThumbnailView.getRight();
188        outRect.bottom = outRect.top + mThumbnailView.getBottom();
189        return outRect;
190    }
191
192    /** Enable the hw layers on this task view */
193    void enableHwLayers() {
194        mThumbnailView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
195    }
196
197    /** Disable the hw layers on this task view */
198    void disableHwLayers() {
199        mThumbnailView.setLayerType(View.LAYER_TYPE_NONE, null);
200    }
201
202    /**** TaskCallbacks Implementation ****/
203
204    /** Binds this task view to the task */
205    public void onTaskBound(Task t) {
206        mTask = t;
207        mTask.setCallbacks(this);
208    }
209
210    @Override
211    public void onTaskDataLoaded(boolean reloadingTaskData) {
212        if (mThumbnailView != null && mBarView != null) {
213            // Bind each of the views to the new task data
214            mThumbnailView.rebindToTask(mTask, reloadingTaskData);
215            mBarView.rebindToTask(mTask, reloadingTaskData);
216        }
217        mTaskDataLoaded = true;
218    }
219
220    @Override
221    public void onTaskDataUnloaded() {
222        if (mThumbnailView != null && mBarView != null) {
223            // Unbind each of the views from the task data and remove the task callback
224            mTask.setCallbacks(null);
225            mThumbnailView.unbindFromTask();
226            mBarView.unbindFromTask();
227        }
228        mTaskDataLoaded = false;
229    }
230
231    @Override
232    public void onClick(View v) {
233        mCb.onTaskIconClicked(this);
234    }
235}