Selim Cinek | 7d5f374 | 2014-11-07 18:07:49 +0100 | [diff] [blame] | 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 | |
| 17 | package com.android.systemui.statusbar; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.graphics.Canvas; |
| 21 | import android.graphics.Rect; |
| 22 | import android.graphics.drawable.AnimatedVectorDrawable; |
| 23 | import android.graphics.drawable.Drawable; |
Selim Cinek | 7d5f374 | 2014-11-07 18:07:49 +0100 | [diff] [blame] | 24 | import android.util.AttributeSet; |
Selim Cinek | 7d5f374 | 2014-11-07 18:07:49 +0100 | [diff] [blame] | 25 | import android.view.View; |
| 26 | import android.view.ViewGroup; |
Selim Cinek | 7d5f374 | 2014-11-07 18:07:49 +0100 | [diff] [blame] | 27 | import android.widget.Button; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame^] | 28 | |
Selim Cinek | 7d5f374 | 2014-11-07 18:07:49 +0100 | [diff] [blame] | 29 | import com.android.systemui.R; |
| 30 | import com.android.systemui.statusbar.stack.NotificationStackScrollLayout; |
| 31 | |
| 32 | public class DismissViewButton extends Button { |
| 33 | private AnimatedVectorDrawable mAnimatedDismissDrawable; |
| 34 | private final Drawable mStaticDismissDrawable; |
| 35 | private Drawable mActiveDrawable; |
| 36 | |
| 37 | public DismissViewButton(Context context) { |
| 38 | this(context, null); |
| 39 | } |
| 40 | |
| 41 | public DismissViewButton(Context context, AttributeSet attrs) { |
| 42 | this(context, attrs, 0); |
| 43 | } |
| 44 | |
| 45 | public DismissViewButton(Context context, AttributeSet attrs, int defStyleAttr) { |
| 46 | this(context, attrs, defStyleAttr, 0); |
| 47 | } |
| 48 | |
| 49 | public DismissViewButton(Context context, AttributeSet attrs, int defStyleAttr, |
| 50 | int defStyleRes) { |
| 51 | super(context, attrs, defStyleAttr, defStyleRes); |
John Spurlock | f5b1f04 | 2015-06-09 14:46:01 -0400 | [diff] [blame] | 52 | mAnimatedDismissDrawable = (AnimatedVectorDrawable) getContext().getDrawable( |
Selim Cinek | 7d5f374 | 2014-11-07 18:07:49 +0100 | [diff] [blame] | 53 | R.drawable.dismiss_all_shape_animation).mutate(); |
| 54 | mAnimatedDismissDrawable.setCallback(this); |
| 55 | mAnimatedDismissDrawable.setBounds(0, |
| 56 | 0, |
| 57 | mAnimatedDismissDrawable.getIntrinsicWidth(), |
| 58 | mAnimatedDismissDrawable.getIntrinsicHeight()); |
John Spurlock | f5b1f04 | 2015-06-09 14:46:01 -0400 | [diff] [blame] | 59 | mStaticDismissDrawable = getContext().getDrawable(R.drawable.dismiss_all_shape); |
Selim Cinek | 7d5f374 | 2014-11-07 18:07:49 +0100 | [diff] [blame] | 60 | mStaticDismissDrawable.setBounds(0, |
| 61 | 0, |
| 62 | mStaticDismissDrawable.getIntrinsicWidth(), |
| 63 | mStaticDismissDrawable.getIntrinsicHeight()); |
| 64 | mStaticDismissDrawable.setCallback(this); |
| 65 | mActiveDrawable = mStaticDismissDrawable; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected void onDraw(Canvas canvas) { |
| 70 | super.onDraw(canvas); |
| 71 | canvas.save(); |
| 72 | int drawableHeight = mActiveDrawable.getBounds().height(); |
| 73 | boolean isRtl = (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL); |
| 74 | int dx = isRtl ? getWidth() / 2 + drawableHeight / 2 : getWidth() / 2 - drawableHeight / 2; |
| 75 | canvas.translate(dx, getHeight() / 2.0f + drawableHeight / |
| 76 | 2.0f); |
| 77 | canvas.scale(isRtl ? -1.0f : 1.0f, -1.0f); |
| 78 | mActiveDrawable.draw(canvas); |
| 79 | canvas.restore(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public boolean performClick() { |
| 84 | if (!mAnimatedDismissDrawable.isRunning()) { |
| 85 | mActiveDrawable = mAnimatedDismissDrawable; |
| 86 | mAnimatedDismissDrawable.start(); |
| 87 | } |
| 88 | return super.performClick(); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | protected boolean verifyDrawable(Drawable who) { |
| 93 | return super.verifyDrawable(who) |
| 94 | || who == mAnimatedDismissDrawable |
| 95 | || who == mStaticDismissDrawable; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public boolean hasOverlappingRendering() { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * This method returns the drawing rect for the view which is different from the regular |
| 105 | * drawing rect, since we layout all children in the {@link NotificationStackScrollLayout} at |
| 106 | * position 0 and usually the translation is neglected. The standard implementation doesn't |
| 107 | * account for translation. |
| 108 | * |
| 109 | * @param outRect The (scrolled) drawing bounds of the view. |
| 110 | */ |
| 111 | @Override |
| 112 | public void getDrawingRect(Rect outRect) { |
| 113 | super.getDrawingRect(outRect); |
| 114 | float translationX = ((ViewGroup) mParent).getTranslationX(); |
| 115 | float translationY = ((ViewGroup) mParent).getTranslationY(); |
| 116 | outRect.left += translationX; |
| 117 | outRect.right += translationX; |
| 118 | outRect.top += translationY; |
| 119 | outRect.bottom += translationY; |
| 120 | } |
| 121 | |
| 122 | public void showButton() { |
| 123 | mActiveDrawable = mStaticDismissDrawable; |
| 124 | invalidate(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @return Whether the button is currently static and not being animated. |
| 129 | */ |
| 130 | public boolean isButtonStatic() { |
| 131 | return mActiveDrawable == mStaticDismissDrawable; |
| 132 | } |
| 133 | } |