Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 1 | /* |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 3 | * |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 13 | */ |
| 14 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 15 | package com.android.systemui.qs.tileimpl; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 16 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 17 | import static com.android.systemui.qs.tileimpl.QSTileImpl.getColorForState; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 18 | |
| 19 | import android.animation.ValueAnimator; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 20 | import android.content.Context; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 21 | import android.content.res.ColorStateList; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 22 | import android.content.res.Resources; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 23 | import android.graphics.Color; |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 24 | import android.graphics.drawable.Animatable2; |
| 25 | import android.graphics.drawable.Animatable2.AnimationCallback; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 26 | import android.graphics.drawable.Drawable; |
| 27 | import android.view.View; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 28 | import android.widget.ImageView; |
Jason Monk | eae7c31 | 2016-02-04 13:00:48 -0500 | [diff] [blame] | 29 | import android.widget.ImageView.ScaleType; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 30 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 31 | import com.android.systemui.R; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 32 | import com.android.systemui.plugins.qs.QSIconView; |
| 33 | import com.android.systemui.plugins.qs.QSTile; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 34 | import com.android.systemui.plugins.qs.QSTile.State; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 35 | |
| 36 | import java.util.Objects; |
| 37 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 38 | public class QSIconViewImpl extends QSIconView { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 39 | |
Evan Laird | 8d2e540 | 2017-06-08 10:36:57 -0400 | [diff] [blame] | 40 | public static final long QS_ANIM_LENGTH = 350; |
| 41 | |
Xiaohui Chen | 2f3551b | 2016-04-07 10:37:25 -0700 | [diff] [blame] | 42 | protected final View mIcon; |
| 43 | protected final int mIconSizePx; |
| 44 | protected final int mTilePaddingBelowIconPx; |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 45 | private boolean mAnimationEnabled = true; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 46 | private int mState = -1; |
| 47 | private int mTint; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 48 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 49 | public QSIconViewImpl(Context context) { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 50 | super(context); |
| 51 | |
| 52 | final Resources res = context.getResources(); |
| 53 | mIconSizePx = res.getDimensionPixelSize(R.dimen.qs_tile_icon_size); |
| 54 | mTilePaddingBelowIconPx = res.getDimensionPixelSize(R.dimen.qs_tile_padding_below_icon); |
| 55 | |
| 56 | mIcon = createIcon(); |
| 57 | addView(mIcon); |
| 58 | } |
| 59 | |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 60 | public void disableAnimation() { |
| 61 | mAnimationEnabled = false; |
| 62 | } |
| 63 | |
Xiaohui Chen | 2f3551b | 2016-04-07 10:37:25 -0700 | [diff] [blame] | 64 | public View getIconView() { |
| 65 | return mIcon; |
| 66 | } |
| 67 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 68 | @Override |
| 69 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 70 | final int w = MeasureSpec.getSize(widthMeasureSpec); |
| 71 | final int iconSpec = exactly(mIconSizePx); |
| 72 | mIcon.measure(MeasureSpec.makeMeasureSpec(w, getIconMeasureMode()), iconSpec); |
| 73 | setMeasuredDimension(w, mIcon.getMeasuredHeight() + mTilePaddingBelowIconPx); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 78 | final int w = getMeasuredWidth(); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 79 | int top = 0; |
| 80 | final int iconLeft = (w - mIcon.getMeasuredWidth()) / 2; |
| 81 | layout(mIcon, iconLeft, top); |
| 82 | } |
| 83 | |
| 84 | public void setIcon(QSTile.State state) { |
| 85 | setIcon((ImageView) mIcon, state); |
| 86 | } |
| 87 | |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 88 | protected void updateIcon(ImageView iv, State state) { |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 89 | if (!Objects.equals(state.icon, iv.getTag(R.id.qs_icon_tag)) |
| 90 | || !Objects.equals(state.slash, iv.getTag(R.id.qs_slash_tag))) { |
Jason Monk | da9395e | 2017-03-27 15:18:23 -0400 | [diff] [blame] | 91 | boolean shouldAnimate = iv.isShown() && mAnimationEnabled |
| 92 | && iv.getDrawable() != null; |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 93 | Drawable d = state.icon != null |
Jason Monk | da9395e | 2017-03-27 15:18:23 -0400 | [diff] [blame] | 94 | ? shouldAnimate ? state.icon.getDrawable(mContext) |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 95 | : state.icon.getInvisibleDrawable(mContext) : null; |
Jason Monk | e19cc14 | 2016-06-01 10:25:01 -0400 | [diff] [blame] | 96 | int padding = state.icon != null ? state.icon.getPadding() : 0; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 97 | if (d != null) { |
Jason Monk | d686e9c | 2017-03-27 15:45:32 -0400 | [diff] [blame] | 98 | d.setAutoMirrored(false); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 99 | } |
| 100 | iv.setImageDrawable(d); |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 101 | if (state.slash != null && iv instanceof SlashImageView) { |
| 102 | ((SlashImageView) iv).setState(state.slash); |
| 103 | } |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 104 | iv.setTag(R.id.qs_icon_tag, state.icon); |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 105 | iv.setTag(R.id.qs_slash_tag, state.slash); |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 106 | iv.setPadding(0, padding, 0, padding); |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 107 | if (d instanceof Animatable2) { |
| 108 | Animatable2 a = (Animatable2) d; |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 109 | a.start(); |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 110 | if (state.isTransient) { |
| 111 | a.registerAnimationCallback(new AnimationCallback() { |
| 112 | @Override |
| 113 | public void onAnimationEnd(Drawable drawable) { |
| 114 | a.start(); |
| 115 | } |
| 116 | }); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | } |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | protected void setIcon(ImageView iv, QSTile.State state) { |
| 123 | updateIcon(iv, state); |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 124 | if (state.disabledByPolicy) { |
| 125 | iv.setColorFilter(getContext().getColor(R.color.qs_tile_disabled_color)); |
| 126 | } else { |
| 127 | iv.clearColorFilter(); |
| 128 | } |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 129 | if (state.state != mState) { |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 130 | int color = getColor(state.state); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 131 | mState = state.state; |
Jason Monk | 9a82d12 | 2017-03-27 16:41:24 -0400 | [diff] [blame] | 132 | if (iv.isShown() && mTint != 0) { |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 133 | animateGrayScale(mTint, color, iv); |
| 134 | mTint = color; |
| 135 | } else { |
| 136 | setTint(iv, color); |
| 137 | mTint = color; |
| 138 | } |
| 139 | } |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 140 | } |
| 141 | |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 142 | protected int getColor(int state) { |
| 143 | return getColorForState(getContext(), state); |
| 144 | } |
| 145 | |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 146 | public static void animateGrayScale(int fromColor, int toColor, ImageView iv) { |
| 147 | final float fromAlpha = Color.alpha(fromColor); |
| 148 | final float toAlpha = Color.alpha(toColor); |
| 149 | final float fromChannel = Color.red(fromColor); |
| 150 | final float toChannel = Color.red(toColor); |
| 151 | |
| 152 | ValueAnimator anim = ValueAnimator.ofFloat(0, 1); |
Evan Laird | 8d2e540 | 2017-06-08 10:36:57 -0400 | [diff] [blame] | 153 | anim.setDuration(QS_ANIM_LENGTH); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 154 | |
| 155 | anim.addUpdateListener(animation -> { |
| 156 | float fraction = animation.getAnimatedFraction(); |
| 157 | int alpha = (int) (fromAlpha + (toAlpha - fromAlpha) * fraction); |
| 158 | int channel = (int) (fromChannel + (toChannel - fromChannel) * fraction); |
| 159 | |
| 160 | setTint(iv, Color.argb(alpha, channel, channel, channel)); |
| 161 | }); |
| 162 | anim.start(); |
| 163 | } |
| 164 | |
| 165 | public static void setTint(ImageView iv, int color) { |
| 166 | iv.setImageTintList(ColorStateList.valueOf(color)); |
| 167 | } |
| 168 | |
| 169 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 170 | protected int getIconMeasureMode() { |
| 171 | return MeasureSpec.EXACTLY; |
| 172 | } |
| 173 | |
| 174 | protected View createIcon() { |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 175 | final ImageView icon = new SlashImageView(mContext); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 176 | icon.setId(android.R.id.icon); |
Jason Monk | eae7c31 | 2016-02-04 13:00:48 -0500 | [diff] [blame] | 177 | icon.setScaleType(ScaleType.FIT_CENTER); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 178 | return icon; |
| 179 | } |
| 180 | |
Xiaohui Chen | 2f3551b | 2016-04-07 10:37:25 -0700 | [diff] [blame] | 181 | protected final int exactly(int size) { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 182 | return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); |
| 183 | } |
| 184 | |
Xiaohui Chen | 2f3551b | 2016-04-07 10:37:25 -0700 | [diff] [blame] | 185 | protected final void layout(View child, int left, int top) { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 186 | child.layout(left, top, left + child.getMeasuredWidth(), top + child.getMeasuredHeight()); |
| 187 | } |
| 188 | } |