TaskViewHeader.java revision b7a42fda313b6e5d5e82591ea9fb5d1b30acfc55
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.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.AnimatorSet;
22import android.animation.ArgbEvaluator;
23import android.animation.ObjectAnimator;
24import android.animation.ValueAnimator;
25import android.content.Context;
26import android.content.res.ColorStateList;
27import android.graphics.Canvas;
28import android.graphics.Color;
29import android.graphics.drawable.Drawable;
30import android.graphics.drawable.GradientDrawable;
31import android.graphics.drawable.RippleDrawable;
32import android.graphics.Outline;
33import android.graphics.Paint;
34import android.graphics.PorterDuff;
35import android.graphics.PorterDuffColorFilter;
36import android.graphics.PorterDuffXfermode;
37import android.graphics.Rect;
38import android.util.AttributeSet;
39import android.view.View;
40import android.view.ViewOutlineProvider;
41import android.widget.FrameLayout;
42import android.widget.ImageView;
43import android.widget.TextView;
44import com.android.systemui.R;
45import com.android.systemui.recents.Constants;
46import com.android.systemui.recents.RecentsConfiguration;
47import com.android.systemui.recents.misc.SystemServicesProxy;
48import com.android.systemui.recents.misc.Utilities;
49import com.android.systemui.recents.model.RecentsTaskLoader;
50import com.android.systemui.recents.model.Task;
51
52
53/* The task bar view */
54public class TaskViewHeader extends FrameLayout {
55
56    RecentsConfiguration mConfig;
57    private SystemServicesProxy mSsp;
58
59    // Header views
60    ImageView mMoveTaskButton;
61    ImageView mDismissButton;
62    ImageView mApplicationIcon;
63    TextView mActivityDescription;
64
65    // Header drawables
66    boolean mCurrentPrimaryColorIsDark;
67    int mCurrentPrimaryColor;
68    int mBackgroundColor;
69    Drawable mLightDismissDrawable;
70    Drawable mDarkDismissDrawable;
71    RippleDrawable mBackground;
72    GradientDrawable mBackgroundColorDrawable;
73    AnimatorSet mFocusAnimator;
74    String mDismissContentDescription;
75
76    // Static highlight that we draw at the top of each view
77    static Paint sHighlightPaint;
78
79    // Header dim, which is only used when task view hardware layers are not used
80    Paint mDimLayerPaint = new Paint();
81    PorterDuffColorFilter mDimColorFilter = new PorterDuffColorFilter(0, PorterDuff.Mode.SRC_ATOP);
82
83    public TaskViewHeader(Context context) {
84        this(context, null);
85    }
86
87    public TaskViewHeader(Context context, AttributeSet attrs) {
88        this(context, attrs, 0);
89    }
90
91    public TaskViewHeader(Context context, AttributeSet attrs, int defStyleAttr) {
92        this(context, attrs, defStyleAttr, 0);
93    }
94
95    public TaskViewHeader(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
96        super(context, attrs, defStyleAttr, defStyleRes);
97        mConfig = RecentsConfiguration.getInstance();
98        mSsp = RecentsTaskLoader.getInstance().getSystemServicesProxy();
99        setWillNotDraw(false);
100        setClipToOutline(true);
101        setOutlineProvider(new ViewOutlineProvider() {
102            @Override
103            public void getOutline(View view, Outline outline) {
104                outline.setRect(0, 0, getMeasuredWidth(), getMeasuredHeight());
105            }
106        });
107
108        // Load the dismiss resources
109        mLightDismissDrawable = context.getDrawable(R.drawable.recents_dismiss_light);
110        mDarkDismissDrawable = context.getDrawable(R.drawable.recents_dismiss_dark);
111        mDismissContentDescription =
112                context.getString(R.string.accessibility_recents_item_will_be_dismissed);
113
114        // Configure the highlight paint
115        if (sHighlightPaint == null) {
116            sHighlightPaint = new Paint();
117            sHighlightPaint.setStyle(Paint.Style.STROKE);
118            sHighlightPaint.setStrokeWidth(mConfig.taskViewHighlightPx);
119            sHighlightPaint.setColor(mConfig.taskBarViewHighlightColor);
120            sHighlightPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
121            sHighlightPaint.setAntiAlias(true);
122        }
123    }
124
125    @Override
126    protected void onFinishInflate() {
127        // Initialize the icon and description views
128        mApplicationIcon = (ImageView) findViewById(R.id.application_icon);
129        mActivityDescription = (TextView) findViewById(R.id.activity_description);
130        mDismissButton = (ImageView) findViewById(R.id.dismiss_task);
131        mMoveTaskButton = (ImageView) findViewById(R.id.move_task);
132
133        // Hide the backgrounds if they are ripple drawables
134        if (!Constants.DebugFlags.App.EnableTaskFiltering) {
135            if (mApplicationIcon.getBackground() instanceof RippleDrawable) {
136                mApplicationIcon.setBackground(null);
137            }
138        }
139
140        mBackgroundColorDrawable = (GradientDrawable) getContext().getDrawable(R.drawable
141                .recents_task_view_header_bg_color);
142        // Copy the ripple drawable since we are going to be manipulating it
143        mBackground = (RippleDrawable)
144                getContext().getDrawable(R.drawable.recents_task_view_header_bg);
145        mBackground = (RippleDrawable) mBackground.mutate().getConstantState().newDrawable();
146        mBackground.setColor(ColorStateList.valueOf(0));
147        mBackground.setDrawableByLayerId(mBackground.getId(0), mBackgroundColorDrawable);
148        setBackground(mBackground);
149    }
150
151    @Override
152    protected void onDraw(Canvas canvas) {
153        // Draw the highlight at the top edge (but put the bottom edge just out of view)
154        float offset = (float) Math.ceil(mConfig.taskViewHighlightPx / 2f);
155        float radius = mConfig.taskViewRoundedCornerRadiusPx;
156        int count = canvas.save(Canvas.CLIP_SAVE_FLAG);
157        canvas.clipRect(0, 0, getMeasuredWidth(), getMeasuredHeight());
158        canvas.drawRoundRect(-offset, 0f, (float) getMeasuredWidth() + offset,
159                getMeasuredHeight() + radius, radius, radius, sHighlightPaint);
160        canvas.restoreToCount(count);
161    }
162
163    @Override
164    public boolean hasOverlappingRendering() {
165        return false;
166    }
167
168    /**
169     * Sets the dim alpha, only used when we are not using hardware layers.
170     * (see RecentsConfiguration.useHardwareLayers)
171     */
172    void setDimAlpha(int alpha) {
173        mDimColorFilter.setColor(Color.argb(alpha, 0, 0, 0));
174        mDimLayerPaint.setColorFilter(mDimColorFilter);
175        setLayerType(LAYER_TYPE_HARDWARE, mDimLayerPaint);
176    }
177
178    /** Returns the secondary color for a primary color. */
179    int getSecondaryColor(int primaryColor, boolean useLightOverlayColor) {
180        int overlayColor = useLightOverlayColor ? Color.WHITE : Color.BLACK;
181        return Utilities.getColorWithOverlay(primaryColor, overlayColor, 0.8f);
182    }
183
184    /** Binds the bar view to the task */
185    public void rebindToTask(Task t) {
186        // If an activity icon is defined, then we use that as the primary icon to show in the bar,
187        // otherwise, we fall back to the application icon
188        if (t.activityIcon != null) {
189            mApplicationIcon.setImageDrawable(t.activityIcon);
190        } else if (t.applicationIcon != null) {
191            mApplicationIcon.setImageDrawable(t.applicationIcon);
192        }
193        mApplicationIcon.setContentDescription(t.contentDescription);
194        if (!mActivityDescription.getText().toString().equals(t.activityLabel)) {
195            mActivityDescription.setText(t.activityLabel);
196        }
197        mActivityDescription.setContentDescription(t.contentDescription);
198
199        // Try and apply the system ui tint
200        int existingBgColor = getBackgroundColor();
201        if (existingBgColor != t.colorPrimary) {
202            mBackgroundColorDrawable.setColor(t.colorPrimary);
203            mBackgroundColor = t.colorPrimary;
204        }
205        mCurrentPrimaryColor = t.colorPrimary;
206        mCurrentPrimaryColorIsDark = t.useLightOnPrimaryColor;
207        mActivityDescription.setTextColor(t.useLightOnPrimaryColor ?
208                mConfig.taskBarViewLightTextColor : mConfig.taskBarViewDarkTextColor);
209        mDismissButton.setImageDrawable(t.useLightOnPrimaryColor ?
210                mLightDismissDrawable : mDarkDismissDrawable);
211        mDismissButton.setContentDescription(String.format(mDismissContentDescription,
212                t.contentDescription));
213        mMoveTaskButton.setVisibility((mConfig.multiStackEnabled) ? View.VISIBLE : View.INVISIBLE);
214        if (mConfig.multiStackEnabled) {
215            updateResizeTaskBarIcon(t);
216        }
217    }
218
219    /** Updates the resize task bar button. */
220    void updateResizeTaskBarIcon(Task t) {
221        Rect display = mSsp.getWindowRect();
222        Rect taskRect = mSsp.getTaskBounds(t.key.stackId);
223        int resId = R.drawable.star;
224        if (display.equals(taskRect) || taskRect.isEmpty()) {
225            resId = R.drawable.vector_drawable_place_fullscreen;
226        } else {
227            boolean top = display.top == taskRect.top;
228            boolean bottom = display.bottom == taskRect.bottom;
229            boolean left = display.left == taskRect.left;
230            boolean right = display.right == taskRect.right;
231            if (top && bottom && left) {
232                resId = R.drawable.vector_drawable_place_left;
233            } else if (top && bottom && right) {
234                resId = R.drawable.vector_drawable_place_right;
235            } else if (top && left && right) {
236                resId = R.drawable.vector_drawable_place_top;
237            } else if (bottom && left && right) {
238                resId = R.drawable.vector_drawable_place_bottom;
239            } else if (top && right) {
240                resId = R.drawable.vector_drawable_place_top_right;
241            } else if (top && left) {
242                resId = R.drawable.vector_drawable_place_top_left;
243            } else if (bottom && right) {
244                resId = R.drawable.vector_drawable_place_bottom_right;
245            } else if (bottom && left) {
246                resId = R.drawable.vector_drawable_place_bottom_left;
247            }
248        }
249        mMoveTaskButton.setImageResource(resId);
250    }
251
252    /** Unbinds the bar view from the task */
253    void unbindFromTask() {
254        mApplicationIcon.setImageDrawable(null);
255    }
256
257    /** Animates this task bar dismiss button when launching a task. */
258    void startLaunchTaskDismissAnimation() {
259        if (mDismissButton.getVisibility() == View.VISIBLE) {
260            mDismissButton.animate().cancel();
261            mDismissButton.animate()
262                    .alpha(0f)
263                    .setStartDelay(0)
264                    .setInterpolator(mConfig.fastOutSlowInInterpolator)
265                    .setDuration(mConfig.taskViewExitToAppDuration)
266                    .withLayer()
267                    .start();
268        }
269    }
270
271    /** Animates this task bar if the user does not interact with the stack after a certain time. */
272    void startNoUserInteractionAnimation() {
273        if (mDismissButton.getVisibility() != View.VISIBLE) {
274            mDismissButton.setVisibility(View.VISIBLE);
275            mDismissButton.setAlpha(0f);
276            mDismissButton.animate()
277                    .alpha(1f)
278                    .setStartDelay(0)
279                    .setInterpolator(mConfig.fastOutLinearInInterpolator)
280                    .setDuration(mConfig.taskViewEnterFromAppDuration)
281                    .withLayer()
282                    .start();
283        }
284    }
285
286    /** Mark this task view that the user does has not interacted with the stack after a certain time. */
287    void setNoUserInteractionState() {
288        if (mDismissButton.getVisibility() != View.VISIBLE) {
289            mDismissButton.animate().cancel();
290            mDismissButton.setVisibility(View.VISIBLE);
291            mDismissButton.setAlpha(1f);
292        }
293    }
294
295    /** Resets the state tracking that the user has not interacted with the stack after a certain time. */
296    void resetNoUserInteractionState() {
297        mDismissButton.setVisibility(View.INVISIBLE);
298    }
299
300    @Override
301    protected int[] onCreateDrawableState(int extraSpace) {
302
303        // Don't forward our state to the drawable - we do it manually in onTaskViewFocusChanged.
304        // This is to prevent layer trashing when the view is pressed.
305        return new int[] {};
306    }
307
308    /** Notifies the associated TaskView has been focused. */
309    void onTaskViewFocusChanged(boolean focused, boolean animateFocusedState) {
310        // If we are not animating the visible state, just return
311        if (!animateFocusedState) return;
312
313        boolean isRunning = false;
314        if (mFocusAnimator != null) {
315            isRunning = mFocusAnimator.isRunning();
316            Utilities.cancelAnimationWithoutCallbacks(mFocusAnimator);
317        }
318
319        if (focused) {
320            int currentColor = mBackgroundColor;
321            int secondaryColor = getSecondaryColor(mCurrentPrimaryColor, mCurrentPrimaryColorIsDark);
322            int[][] states = new int[][] {
323                    new int[] {},
324                    new int[] { android.R.attr.state_enabled },
325                    new int[] { android.R.attr.state_pressed }
326            };
327            int[] newStates = new int[]{
328                    0,
329                    android.R.attr.state_enabled,
330                    android.R.attr.state_pressed
331            };
332            int[] colors = new int[] {
333                    currentColor,
334                    secondaryColor,
335                    secondaryColor
336            };
337            mBackground.setColor(new ColorStateList(states, colors));
338            mBackground.setState(newStates);
339            // Pulse the background color
340            int lightPrimaryColor = getSecondaryColor(mCurrentPrimaryColor, mCurrentPrimaryColorIsDark);
341            ValueAnimator backgroundColor = ValueAnimator.ofObject(new ArgbEvaluator(),
342                    currentColor, lightPrimaryColor);
343            backgroundColor.addListener(new AnimatorListenerAdapter() {
344                @Override
345                public void onAnimationStart(Animator animation) {
346                    mBackground.setState(new int[]{});
347                }
348            });
349            backgroundColor.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
350                @Override
351                public void onAnimationUpdate(ValueAnimator animation) {
352                    int color = (int) animation.getAnimatedValue();
353                    mBackgroundColorDrawable.setColor(color);
354                    mBackgroundColor = color;
355                }
356            });
357            backgroundColor.setRepeatCount(ValueAnimator.INFINITE);
358            backgroundColor.setRepeatMode(ValueAnimator.REVERSE);
359            // Pulse the translation
360            ObjectAnimator translation = ObjectAnimator.ofFloat(this, "translationZ", 15f);
361            translation.setRepeatCount(ValueAnimator.INFINITE);
362            translation.setRepeatMode(ValueAnimator.REVERSE);
363
364            mFocusAnimator = new AnimatorSet();
365            mFocusAnimator.playTogether(backgroundColor, translation);
366            mFocusAnimator.setStartDelay(150);
367            mFocusAnimator.setDuration(750);
368            mFocusAnimator.start();
369        } else {
370            if (isRunning) {
371                // Restore the background color
372                int currentColor = mBackgroundColor;
373                ValueAnimator backgroundColor = ValueAnimator.ofObject(new ArgbEvaluator(),
374                        currentColor, mCurrentPrimaryColor);
375                backgroundColor.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
376                    @Override
377                    public void onAnimationUpdate(ValueAnimator animation) {
378                        int color = (int) animation.getAnimatedValue();
379                        mBackgroundColorDrawable.setColor(color);
380                        mBackgroundColor = color;
381                    }
382                });
383                // Restore the translation
384                ObjectAnimator translation = ObjectAnimator.ofFloat(this, "translationZ", 0f);
385
386                mFocusAnimator = new AnimatorSet();
387                mFocusAnimator.playTogether(backgroundColor, translation);
388                mFocusAnimator.setDuration(150);
389                mFocusAnimator.start();
390            } else {
391                mBackground.setState(new int[] {});
392                setTranslationZ(0f);
393            }
394        }
395    }
396}
397