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 | |
Amin Shaikh | a5902ea | 2018-03-29 14:23:29 -0400 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 21 | import android.animation.ValueAnimator; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 22 | import android.content.Context; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 23 | import android.content.res.ColorStateList; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 24 | import android.content.res.Resources; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 25 | import android.graphics.Color; |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 26 | import android.graphics.drawable.Animatable2; |
| 27 | import android.graphics.drawable.Animatable2.AnimationCallback; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
| 29 | import android.view.View; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 30 | import android.widget.ImageView; |
Jason Monk | eae7c31 | 2016-02-04 13:00:48 -0500 | [diff] [blame] | 31 | import android.widget.ImageView.ScaleType; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 32 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 33 | import com.android.systemui.R; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 34 | import com.android.systemui.plugins.qs.QSIconView; |
| 35 | import com.android.systemui.plugins.qs.QSTile; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 36 | import com.android.systemui.plugins.qs.QSTile.State; |
Evan Laird | 6026b05 | 2017-09-21 14:11:00 -0400 | [diff] [blame] | 37 | import com.android.systemui.qs.AlphaControlledSignalTileView.AlphaControlledSlashImageView; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 38 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 39 | import java.util.Objects; |
| 40 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 41 | public class QSIconViewImpl extends QSIconView { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 42 | |
Evan Laird | 8d2e540 | 2017-06-08 10:36:57 -0400 | [diff] [blame] | 43 | public static final long QS_ANIM_LENGTH = 350; |
| 44 | |
Xiaohui Chen | 2f3551b | 2016-04-07 10:37:25 -0700 | [diff] [blame] | 45 | protected final View mIcon; |
| 46 | protected final int mIconSizePx; |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 47 | private boolean mAnimationEnabled = true; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 48 | private int mState = -1; |
| 49 | private int mTint; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 50 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 51 | public QSIconViewImpl(Context context) { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 52 | super(context); |
| 53 | |
| 54 | final Resources res = context.getResources(); |
| 55 | mIconSizePx = res.getDimensionPixelSize(R.dimen.qs_tile_icon_size); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 56 | |
| 57 | mIcon = createIcon(); |
| 58 | addView(mIcon); |
| 59 | } |
| 60 | |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 61 | public void disableAnimation() { |
| 62 | mAnimationEnabled = false; |
| 63 | } |
| 64 | |
Xiaohui Chen | 2f3551b | 2016-04-07 10:37:25 -0700 | [diff] [blame] | 65 | public View getIconView() { |
| 66 | return mIcon; |
| 67 | } |
| 68 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 69 | @Override |
| 70 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 71 | final int w = MeasureSpec.getSize(widthMeasureSpec); |
| 72 | final int iconSpec = exactly(mIconSizePx); |
| 73 | mIcon.measure(MeasureSpec.makeMeasureSpec(w, getIconMeasureMode()), iconSpec); |
Fabian Kozynski | bca09b7 | 2019-04-04 15:57:42 -0400 | [diff] [blame] | 74 | setMeasuredDimension(w, mIcon.getMeasuredHeight()); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 79 | final int w = getMeasuredWidth(); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 80 | int top = 0; |
| 81 | final int iconLeft = (w - mIcon.getMeasuredWidth()) / 2; |
| 82 | layout(mIcon, iconLeft, top); |
| 83 | } |
| 84 | |
Amin Shaikh | 32d786f | 2018-08-01 17:08:26 -0400 | [diff] [blame] | 85 | public void setIcon(State state, boolean allowAnimations) { |
| 86 | setIcon((ImageView) mIcon, state, allowAnimations); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 87 | } |
| 88 | |
Amin Shaikh | 32d786f | 2018-08-01 17:08:26 -0400 | [diff] [blame] | 89 | protected void updateIcon(ImageView iv, State state, boolean allowAnimations) { |
Evan Laird | b3daf2b | 2017-10-17 16:00:29 -0400 | [diff] [blame] | 90 | final QSTile.Icon icon = state.iconSupplier != null ? state.iconSupplier.get() : state.icon; |
| 91 | if (!Objects.equals(icon, iv.getTag(R.id.qs_icon_tag)) |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 92 | || !Objects.equals(state.slash, iv.getTag(R.id.qs_slash_tag))) { |
Amin Shaikh | 32d786f | 2018-08-01 17:08:26 -0400 | [diff] [blame] | 93 | boolean shouldAnimate = allowAnimations && shouldAnimate(iv); |
Evan Laird | b3daf2b | 2017-10-17 16:00:29 -0400 | [diff] [blame] | 94 | Drawable d = icon != null |
| 95 | ? shouldAnimate ? icon.getDrawable(mContext) |
| 96 | : icon.getInvisibleDrawable(mContext) : null; |
| 97 | int padding = icon != null ? icon.getPadding() : 0; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 98 | if (d != null) { |
Jason Monk | d686e9c | 2017-03-27 15:45:32 -0400 | [diff] [blame] | 99 | d.setAutoMirrored(false); |
Evan Laird | 3367c33 | 2017-07-07 13:24:56 -0400 | [diff] [blame] | 100 | d.setLayoutDirection(getLayoutDirection()); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 101 | } |
Evan Laird | 929bd54 | 2017-07-12 16:36:06 -0400 | [diff] [blame] | 102 | |
| 103 | if (iv instanceof SlashImageView) { |
| 104 | ((SlashImageView) iv).setAnimationEnabled(shouldAnimate); |
Jason Monk | cb2307c | 2018-01-15 14:19:45 -0500 | [diff] [blame] | 105 | ((SlashImageView) iv).setState(null, d); |
Evan Laird | 929bd54 | 2017-07-12 16:36:06 -0400 | [diff] [blame] | 106 | } else { |
| 107 | iv.setImageDrawable(d); |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 108 | } |
Evan Laird | 929bd54 | 2017-07-12 16:36:06 -0400 | [diff] [blame] | 109 | |
Evan Laird | b3daf2b | 2017-10-17 16:00:29 -0400 | [diff] [blame] | 110 | iv.setTag(R.id.qs_icon_tag, icon); |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 111 | iv.setTag(R.id.qs_slash_tag, state.slash); |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 112 | iv.setPadding(0, padding, 0, padding); |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 113 | if (d instanceof Animatable2) { |
| 114 | Animatable2 a = (Animatable2) d; |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 115 | a.start(); |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 116 | if (state.isTransient) { |
| 117 | a.registerAnimationCallback(new AnimationCallback() { |
| 118 | @Override |
| 119 | public void onAnimationEnd(Drawable drawable) { |
| 120 | a.start(); |
| 121 | } |
| 122 | }); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 126 | } |
| 127 | |
Amin Shaikh | 32d786f | 2018-08-01 17:08:26 -0400 | [diff] [blame] | 128 | private boolean shouldAnimate(ImageView iv) { |
| 129 | return mAnimationEnabled && iv.isShown() && iv.getDrawable() != null; |
| 130 | } |
| 131 | |
| 132 | protected void setIcon(ImageView iv, QSTile.State state, boolean allowAnimations) { |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 133 | if (state.disabledByPolicy) { |
| 134 | iv.setColorFilter(getContext().getColor(R.color.qs_tile_disabled_color)); |
| 135 | } else { |
| 136 | iv.clearColorFilter(); |
| 137 | } |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 138 | if (state.state != mState) { |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 139 | int color = getColor(state.state); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 140 | mState = state.state; |
Amin Shaikh | 32d786f | 2018-08-01 17:08:26 -0400 | [diff] [blame] | 141 | if (mTint != 0 && allowAnimations && shouldAnimate(iv)) { |
| 142 | animateGrayScale(mTint, color, iv, () -> updateIcon(iv, state, allowAnimations)); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 143 | mTint = color; |
| 144 | } else { |
Evan Laird | 6026b05 | 2017-09-21 14:11:00 -0400 | [diff] [blame] | 145 | if (iv instanceof AlphaControlledSlashImageView) { |
| 146 | ((AlphaControlledSlashImageView)iv) |
| 147 | .setFinalImageTintList(ColorStateList.valueOf(color)); |
| 148 | } else { |
| 149 | setTint(iv, color); |
| 150 | } |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 151 | mTint = color; |
Amin Shaikh | 32d786f | 2018-08-01 17:08:26 -0400 | [diff] [blame] | 152 | updateIcon(iv, state, allowAnimations); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 153 | } |
Amin Shaikh | a5902ea | 2018-03-29 14:23:29 -0400 | [diff] [blame] | 154 | } else { |
Amin Shaikh | 32d786f | 2018-08-01 17:08:26 -0400 | [diff] [blame] | 155 | updateIcon(iv, state, allowAnimations); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 156 | } |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 157 | } |
| 158 | |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 159 | protected int getColor(int state) { |
| 160 | return getColorForState(getContext(), state); |
| 161 | } |
| 162 | |
Amin Shaikh | 5df1a75 | 2018-04-03 13:39:53 -0400 | [diff] [blame] | 163 | private void animateGrayScale(int fromColor, int toColor, ImageView iv, |
| 164 | final Runnable endRunnable) { |
Evan Laird | 6026b05 | 2017-09-21 14:11:00 -0400 | [diff] [blame] | 165 | if (iv instanceof AlphaControlledSlashImageView) { |
| 166 | ((AlphaControlledSlashImageView)iv) |
| 167 | .setFinalImageTintList(ColorStateList.valueOf(toColor)); |
| 168 | } |
Amin Shaikh | 5df1a75 | 2018-04-03 13:39:53 -0400 | [diff] [blame] | 169 | if (mAnimationEnabled && ValueAnimator.areAnimatorsEnabled()) { |
Evan Laird | bdcd1cc | 2017-07-05 17:10:04 -0400 | [diff] [blame] | 170 | final float fromAlpha = Color.alpha(fromColor); |
| 171 | final float toAlpha = Color.alpha(toColor); |
| 172 | final float fromChannel = Color.red(fromColor); |
| 173 | final float toChannel = Color.red(toColor); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 174 | |
Evan Laird | bdcd1cc | 2017-07-05 17:10:04 -0400 | [diff] [blame] | 175 | ValueAnimator anim = ValueAnimator.ofFloat(0, 1); |
| 176 | anim.setDuration(QS_ANIM_LENGTH); |
| 177 | anim.addUpdateListener(animation -> { |
| 178 | float fraction = animation.getAnimatedFraction(); |
| 179 | int alpha = (int) (fromAlpha + (toAlpha - fromAlpha) * fraction); |
| 180 | int channel = (int) (fromChannel + (toChannel - fromChannel) * fraction); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 181 | |
Evan Laird | bdcd1cc | 2017-07-05 17:10:04 -0400 | [diff] [blame] | 182 | setTint(iv, Color.argb(alpha, channel, channel, channel)); |
| 183 | }); |
Amin Shaikh | a5902ea | 2018-03-29 14:23:29 -0400 | [diff] [blame] | 184 | anim.addListener(new AnimatorListenerAdapter() { |
| 185 | @Override |
| 186 | public void onAnimationEnd(Animator animation) { |
| 187 | endRunnable.run(); |
| 188 | } |
| 189 | }); |
Evan Laird | bdcd1cc | 2017-07-05 17:10:04 -0400 | [diff] [blame] | 190 | anim.start(); |
| 191 | } else { |
| 192 | setTint(iv, toColor); |
Amin Shaikh | a5902ea | 2018-03-29 14:23:29 -0400 | [diff] [blame] | 193 | endRunnable.run(); |
Evan Laird | bdcd1cc | 2017-07-05 17:10:04 -0400 | [diff] [blame] | 194 | } |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | public static void setTint(ImageView iv, int color) { |
| 198 | iv.setImageTintList(ColorStateList.valueOf(color)); |
| 199 | } |
| 200 | |
| 201 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 202 | protected int getIconMeasureMode() { |
| 203 | return MeasureSpec.EXACTLY; |
| 204 | } |
| 205 | |
| 206 | protected View createIcon() { |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 207 | final ImageView icon = new SlashImageView(mContext); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 208 | icon.setId(android.R.id.icon); |
Jason Monk | eae7c31 | 2016-02-04 13:00:48 -0500 | [diff] [blame] | 209 | icon.setScaleType(ScaleType.FIT_CENTER); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 210 | return icon; |
| 211 | } |
| 212 | |
Xiaohui Chen | 2f3551b | 2016-04-07 10:37:25 -0700 | [diff] [blame] | 213 | protected final int exactly(int size) { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 214 | return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); |
| 215 | } |
| 216 | |
Xiaohui Chen | 2f3551b | 2016-04-07 10:37:25 -0700 | [diff] [blame] | 217 | protected final void layout(View child, int left, int top) { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 218 | child.layout(left, top, left + child.getMeasuredWidth(), top + child.getMeasuredHeight()); |
| 219 | } |
| 220 | } |