TaskViewThumbnail.java revision a4ccb86ddc8f9f486aee25fb836f4aff97bf7679
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.Bitmap;
21import android.graphics.Rect;
22import android.util.AttributeSet;
23import android.view.View;
24import com.android.systemui.recents.RecentsConfiguration;
25import com.android.systemui.recents.model.Task;
26
27
28/** The task thumbnail view */
29public class TaskViewThumbnail extends FixedSizeImageView {
30
31    RecentsConfiguration mConfig;
32
33    // Task bar clipping
34    Rect mClipRect = new Rect();
35
36    public TaskViewThumbnail(Context context) {
37        this(context, null);
38    }
39
40    public TaskViewThumbnail(Context context, AttributeSet attrs) {
41        this(context, attrs, 0);
42    }
43
44    public TaskViewThumbnail(Context context, AttributeSet attrs, int defStyleAttr) {
45        this(context, attrs, defStyleAttr, 0);
46    }
47
48    public TaskViewThumbnail(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
49        super(context, attrs, defStyleAttr, defStyleRes);
50        mConfig = RecentsConfiguration.getInstance();
51        setScaleType(ScaleType.FIT_XY);
52    }
53
54    @Override
55    protected void onFinishInflate() {
56        setAlpha(0.9f);
57    }
58
59    /** Updates the clip rect based on the given task bar. */
60    void enableTaskBarClip(View taskBar) {
61        int top = (int) Math.max(0, taskBar.getTranslationY() +
62                taskBar.getMeasuredHeight() - 1);
63        mClipRect.set(0, top, getMeasuredWidth(), getMeasuredHeight());
64        setClipBounds(mClipRect);
65    }
66
67    /** Disables the task bar clipping. */
68    void disableTaskBarClip() {
69        mClipRect.set(0, 0, getMeasuredWidth(), getMeasuredHeight());
70        setClipBounds(mClipRect);
71    }
72
73    /** Binds the thumbnail view to the screenshot. */
74    boolean bindToScreenshot(Bitmap ss) {
75        if (ss != null) {
76            setImageBitmap(ss);
77            return true;
78        }
79        return false;
80    }
81
82    /** Unbinds the thumbnail view from the screenshot. */
83    void unbindFromScreenshot() {
84        setImageBitmap(null);
85    }
86
87    /** Binds the thumbnail view to the task */
88    void rebindToTask(Task t) {
89        if (t.thumbnail != null) {
90            setImageBitmap(t.thumbnail);
91        }
92    }
93
94    /** Unbinds the thumbnail view from the task */
95    void unbindFromTask() {
96        setImageDrawable(null);
97    }
98
99    /** Handles focus changes. */
100    void onFocusChanged(boolean focused) {
101        if (focused) {
102            if (Float.compare(getAlpha(), 1f) != 0) {
103                startFadeAnimation(1f, 0, 150, null);
104            }
105        } else {
106            if (Float.compare(getAlpha(), mConfig.taskViewThumbnailAlpha) != 0) {
107                startFadeAnimation(mConfig.taskViewThumbnailAlpha, 0, 150, null);
108            }
109        }
110    }
111
112    /** Prepares for the enter recents animation. */
113    void prepareEnterRecentsAnimation(boolean isTaskViewLaunchTargetTask) {
114        if (isTaskViewLaunchTargetTask) {
115            setAlpha(1f);
116        } else {
117            setAlpha(mConfig.taskViewThumbnailAlpha);
118        }
119    }
120
121    /** Animates this task thumbnail as it enters recents */
122    void startEnterRecentsAnimation(int delay, Runnable postAnimRunnable) {
123        startFadeAnimation(mConfig.taskViewThumbnailAlpha, delay,
124                mConfig.taskBarEnterAnimDuration, postAnimRunnable);
125    }
126
127    /** Animates this task thumbnail as it exits recents */
128    void startLaunchTaskAnimation(Runnable postAnimRunnable) {
129        startFadeAnimation(1f, 0, mConfig.taskBarExitAnimDuration, postAnimRunnable);
130    }
131
132    /** Animates the thumbnail alpha. */
133    void startFadeAnimation(float finalAlpha, int delay, int duration, Runnable postAnimRunnable) {
134        if (postAnimRunnable != null) {
135            animate().withEndAction(postAnimRunnable);
136        }
137        animate()
138                .alpha(finalAlpha)
139                .setStartDelay(delay)
140                .setInterpolator(mConfig.fastOutSlowInInterpolator)
141                .setDuration(duration)
142                .withLayer()
143                .start();
144    }
145}
146