The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 19 | import android.animation.AnimatorSet; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 20 | import android.animation.ObjectAnimator; |
| 21 | import android.animation.TimeInterpolator; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 22 | import android.graphics.Bitmap; |
| 23 | import android.graphics.Canvas; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 24 | import android.graphics.Color; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 25 | import android.graphics.ColorFilter; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 26 | import android.graphics.ColorMatrix; |
| 27 | import android.graphics.ColorMatrixColorFilter; |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 28 | import android.graphics.Paint; |
| 29 | import android.graphics.PixelFormat; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 30 | import android.graphics.PorterDuff; |
| 31 | import android.graphics.PorterDuffColorFilter; |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 32 | import android.graphics.drawable.Drawable; |
Tony Wickham | 1e61849 | 2017-02-02 12:57:18 -0800 | [diff] [blame^] | 33 | import android.util.Property; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 34 | import android.util.SparseArray; |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 35 | import android.view.animation.DecelerateInterpolator; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 37 | import com.android.launcher3.badge.BadgeInfo; |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 38 | import com.android.launcher3.badge.BadgeRenderer; |
| 39 | import com.android.launcher3.graphics.IconPalette; |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 40 | |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 41 | public class FastBitmapDrawable extends Drawable { |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 42 | private static final float DISABLED_DESATURATION = 1f; |
| 43 | private static final float DISABLED_BRIGHTNESS = 0.5f; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 44 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 45 | /** |
| 46 | * The possible states that a FastBitmapDrawable can be in. |
| 47 | */ |
| 48 | public enum State { |
| 49 | |
| 50 | NORMAL (0f, 0f, 1f, new DecelerateInterpolator()), |
| 51 | PRESSED (0f, 100f / 255f, 1f, CLICK_FEEDBACK_INTERPOLATOR), |
Winson | c08c59d | 2015-10-28 15:30:38 -0700 | [diff] [blame] | 52 | FAST_SCROLL_HIGHLIGHTED (0f, 0f, 1.15f, new DecelerateInterpolator()), |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 53 | FAST_SCROLL_UNHIGHLIGHTED (0f, 0f, 1f, new DecelerateInterpolator()); |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 54 | |
| 55 | public final float desaturation; |
| 56 | public final float brightness; |
| 57 | /** |
| 58 | * Used specifically by the view drawing this FastBitmapDrawable. |
| 59 | */ |
| 60 | public final float viewScale; |
| 61 | public final TimeInterpolator interpolator; |
| 62 | |
| 63 | State(float desaturation, float brightness, float viewScale, TimeInterpolator interpolator) { |
| 64 | this.desaturation = desaturation; |
| 65 | this.brightness = brightness; |
| 66 | this.viewScale = viewScale; |
| 67 | this.interpolator = interpolator; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public static final TimeInterpolator CLICK_FEEDBACK_INTERPOLATOR = new TimeInterpolator() { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 72 | |
| 73 | @Override |
| 74 | public float getInterpolation(float input) { |
| 75 | if (input < 0.05f) { |
| 76 | return input / 0.05f; |
| 77 | } else if (input < 0.3f){ |
| 78 | return 1; |
| 79 | } else { |
| 80 | return (1 - input) / 0.7f; |
| 81 | } |
| 82 | } |
| 83 | }; |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 84 | public static final int CLICK_FEEDBACK_DURATION = 2000; |
| 85 | public static final int FAST_SCROLL_HIGHLIGHT_DURATION = 225; |
| 86 | public static final int FAST_SCROLL_UNHIGHLIGHT_DURATION = 150; |
| 87 | public static final int FAST_SCROLL_UNHIGHLIGHT_FROM_NORMAL_DURATION = 225; |
| 88 | public static final int FAST_SCROLL_INACTIVE_DURATION = 275; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 89 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 90 | // Since we don't need 256^2 values for combinations of both the brightness and saturation, we |
| 91 | // reduce the value space to a smaller value V, which reduces the number of cached |
| 92 | // ColorMatrixColorFilters that we need to keep to V^2 |
| 93 | private static final int REDUCED_FILTER_VALUE_SPACE = 48; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 94 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 95 | // A cache of ColorFilters for optimizing brightness and saturation animations |
| 96 | private static final SparseArray<ColorFilter> sCachedFilter = new SparseArray<>(); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 97 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 98 | // Temporary matrices used for calculation |
| 99 | private static final ColorMatrix sTempBrightnessMatrix = new ColorMatrix(); |
| 100 | private static final ColorMatrix sTempFilterMatrix = new ColorMatrix(); |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 101 | |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 102 | protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 103 | private final Bitmap mBitmap; |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 104 | private State mState = State.NORMAL; |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 105 | private boolean mIsDisabled; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 106 | |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 107 | private BadgeInfo mBadgeInfo; |
| 108 | private BadgeRenderer mBadgeRenderer; |
| 109 | private IconPalette mIconPalette; |
Tony Wickham | 1e61849 | 2017-02-02 12:57:18 -0800 | [diff] [blame^] | 110 | private float mBadgeScale; |
| 111 | |
| 112 | private static final Property<FastBitmapDrawable, Float> BADGE_SCALE_PROPERTY |
| 113 | = new Property<FastBitmapDrawable, Float>(Float.TYPE, "badgeScale") { |
| 114 | @Override |
| 115 | public Float get(FastBitmapDrawable fastBitmapDrawable) { |
| 116 | return fastBitmapDrawable.mBadgeScale; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void set(FastBitmapDrawable fastBitmapDrawable, Float value) { |
| 121 | fastBitmapDrawable.mBadgeScale = value; |
| 122 | fastBitmapDrawable.invalidateSelf(); |
| 123 | } |
| 124 | }; |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 125 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 126 | // The saturation and brightness are values that are mapped to REDUCED_FILTER_VALUE_SPACE and |
| 127 | // as a result, can be used to compose the key for the cached ColorMatrixColorFilters |
| 128 | private int mDesaturation = 0; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 129 | private int mBrightness = 0; |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 130 | private int mAlpha = 255; |
| 131 | private int mPrevUpdateKey = Integer.MAX_VALUE; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 132 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 133 | // Animators for the fast bitmap drawable's properties |
| 134 | private AnimatorSet mPropertyAnimator; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 135 | |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 136 | public FastBitmapDrawable(Bitmap b) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 137 | mBitmap = b; |
Sunny Goyal | 96ac68a | 2017-02-02 16:37:21 -0800 | [diff] [blame] | 138 | setFilterBitmap(true); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Tony Wickham | 1e61849 | 2017-02-02 12:57:18 -0800 | [diff] [blame^] | 141 | public void applyIconBadge(final BadgeInfo badgeInfo, BadgeRenderer badgeRenderer, |
| 142 | boolean animate) { |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 143 | boolean wasBadged = mBadgeInfo != null; |
| 144 | boolean isBadged = badgeInfo != null; |
Tony Wickham | 1e61849 | 2017-02-02 12:57:18 -0800 | [diff] [blame^] | 145 | float newBadgeScale = isBadged ? 1f : 0; |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 146 | mBadgeInfo = badgeInfo; |
| 147 | mBadgeRenderer = badgeRenderer; |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 148 | if (wasBadged || isBadged) { |
Tony Wickham | 9438ed4 | 2017-01-20 09:38:25 -0800 | [diff] [blame] | 149 | mIconPalette = getIconPalette(); |
Tony Wickham | 1e61849 | 2017-02-02 12:57:18 -0800 | [diff] [blame^] | 150 | // Animate when a badge is first added or when it is removed. |
| 151 | if (animate && (wasBadged ^ isBadged) && isVisible()) { |
| 152 | ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start(); |
| 153 | } else { |
| 154 | mBadgeScale = newBadgeScale; |
| 155 | invalidateSelf(); |
| 156 | } |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 157 | } |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 158 | } |
| 159 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 160 | @Override |
| 161 | public void draw(Canvas canvas) { |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 162 | drawInternal(canvas); |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 163 | // Draw the icon badge in the top right corner. |
| 164 | drawBadgeIfNecessary(canvas); |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | public void drawWithBrightness(Canvas canvas, float brightness) { |
| 168 | float oldBrightness = getBrightness(); |
| 169 | setBrightness(brightness); |
| 170 | drawInternal(canvas); |
| 171 | setBrightness(oldBrightness); |
| 172 | } |
| 173 | |
| 174 | protected void drawInternal(Canvas canvas) { |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 175 | canvas.drawBitmap(mBitmap, null, getBounds(), mPaint); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 178 | protected void drawBadgeIfNecessary(Canvas canvas) { |
| 179 | if (hasBadge()) { |
Tony Wickham | 1e61849 | 2017-02-02 12:57:18 -0800 | [diff] [blame^] | 180 | mBadgeRenderer.draw(canvas, mIconPalette, mBadgeInfo, getBounds(), mBadgeScale); |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Tony Wickham | 9438ed4 | 2017-01-20 09:38:25 -0800 | [diff] [blame] | 184 | public IconPalette getIconPalette() { |
| 185 | if (mIconPalette == null) { |
| 186 | mIconPalette = IconPalette.fromDominantColor(Utilities |
| 187 | .findDominantColorByHue(mBitmap, 20)); |
| 188 | } |
| 189 | return mIconPalette; |
| 190 | } |
| 191 | |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 192 | private boolean hasBadge() { |
Tony Wickham | 1e61849 | 2017-02-02 12:57:18 -0800 | [diff] [blame^] | 193 | return (mBadgeInfo != null && mBadgeInfo.getNotificationCount() > 0) || mBadgeScale > 0; |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 194 | } |
| 195 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 196 | @Override |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 197 | public void setColorFilter(ColorFilter cf) { |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 198 | // No op |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 202 | public int getOpacity() { |
| 203 | return PixelFormat.TRANSLUCENT; |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void setAlpha(int alpha) { |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 208 | mAlpha = alpha; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 209 | mPaint.setAlpha(alpha); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 212 | @Override |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 213 | public void setFilterBitmap(boolean filterBitmap) { |
| 214 | mPaint.setFilterBitmap(filterBitmap); |
Winson Chung | 6e1c0d3 | 2013-10-25 15:24:24 -0700 | [diff] [blame] | 215 | mPaint.setAntiAlias(filterBitmap); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 218 | public int getAlpha() { |
| 219 | return mAlpha; |
| 220 | } |
| 221 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 222 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 223 | public int getIntrinsicWidth() { |
Sunny Goyal | c424f22 | 2014-09-05 07:04:59 -0700 | [diff] [blame] | 224 | return mBitmap.getWidth(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public int getIntrinsicHeight() { |
Sunny Goyal | c424f22 | 2014-09-05 07:04:59 -0700 | [diff] [blame] | 229 | return mBitmap.getHeight(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public int getMinimumWidth() { |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 234 | return getBounds().width(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public int getMinimumHeight() { |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 239 | return getBounds().height(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 240 | } |
| 241 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 242 | public Bitmap getBitmap() { |
| 243 | return mBitmap; |
| 244 | } |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 245 | |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 246 | /** |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 247 | * Animates this drawable to a new state. |
| 248 | * |
| 249 | * @return whether the state has changed. |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 250 | */ |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 251 | public boolean animateState(State newState) { |
| 252 | State prevState = mState; |
| 253 | if (mState != newState) { |
| 254 | mState = newState; |
| 255 | |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 256 | float desaturation = mIsDisabled ? DISABLED_DESATURATION : newState.desaturation; |
| 257 | float brightness = mIsDisabled ? DISABLED_BRIGHTNESS: newState.brightness; |
| 258 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 259 | mPropertyAnimator = cancelAnimator(mPropertyAnimator); |
| 260 | mPropertyAnimator = new AnimatorSet(); |
| 261 | mPropertyAnimator.playTogether( |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 262 | ObjectAnimator.ofFloat(this, "desaturation", desaturation), |
| 263 | ObjectAnimator.ofFloat(this, "brightness", brightness)); |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 264 | mPropertyAnimator.setInterpolator(newState.interpolator); |
| 265 | mPropertyAnimator.setDuration(getDurationForStateChange(prevState, newState)); |
| 266 | mPropertyAnimator.setStartDelay(getStartDelayForStateChange(prevState, newState)); |
| 267 | mPropertyAnimator.start(); |
| 268 | return true; |
| 269 | } |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Immediately sets this drawable to a new state. |
| 275 | * |
| 276 | * @return whether the state has changed. |
| 277 | */ |
| 278 | public boolean setState(State newState) { |
| 279 | if (mState != newState) { |
| 280 | mState = newState; |
| 281 | |
| 282 | mPropertyAnimator = cancelAnimator(mPropertyAnimator); |
| 283 | |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 284 | invalidateDesaturationAndBrightness(); |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 285 | return true; |
| 286 | } |
| 287 | return false; |
| 288 | } |
| 289 | |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 290 | private void invalidateDesaturationAndBrightness() { |
| 291 | setDesaturation(mIsDisabled ? DISABLED_DESATURATION : mState.desaturation); |
| 292 | setBrightness(mIsDisabled ? DISABLED_BRIGHTNESS: mState.brightness); |
| 293 | } |
| 294 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 295 | /** |
| 296 | * Returns the current state. |
| 297 | */ |
| 298 | public State getCurrentState() { |
| 299 | return mState; |
| 300 | } |
| 301 | |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 302 | public void setIsDisabled(boolean isDisabled) { |
| 303 | if (mIsDisabled != isDisabled) { |
| 304 | mIsDisabled = isDisabled; |
| 305 | invalidateDesaturationAndBrightness(); |
| 306 | } |
| 307 | } |
| 308 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 309 | /** |
| 310 | * Returns the duration for the state change animation. |
| 311 | */ |
| 312 | public static int getDurationForStateChange(State fromState, State toState) { |
| 313 | switch (toState) { |
| 314 | case NORMAL: |
| 315 | switch (fromState) { |
| 316 | case PRESSED: |
| 317 | return 0; |
| 318 | case FAST_SCROLL_HIGHLIGHTED: |
| 319 | case FAST_SCROLL_UNHIGHLIGHTED: |
| 320 | return FAST_SCROLL_INACTIVE_DURATION; |
| 321 | } |
| 322 | case PRESSED: |
| 323 | return CLICK_FEEDBACK_DURATION; |
| 324 | case FAST_SCROLL_HIGHLIGHTED: |
| 325 | return FAST_SCROLL_HIGHLIGHT_DURATION; |
| 326 | case FAST_SCROLL_UNHIGHLIGHTED: |
| 327 | switch (fromState) { |
| 328 | case NORMAL: |
| 329 | // When animating from normal state, take a little longer |
| 330 | return FAST_SCROLL_UNHIGHLIGHT_FROM_NORMAL_DURATION; |
| 331 | default: |
| 332 | return FAST_SCROLL_UNHIGHLIGHT_DURATION; |
| 333 | } |
| 334 | } |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Returns the start delay when animating between certain fast scroll states. |
| 340 | */ |
| 341 | public static int getStartDelayForStateChange(State fromState, State toState) { |
| 342 | switch (toState) { |
| 343 | case FAST_SCROLL_UNHIGHLIGHTED: |
| 344 | switch (fromState) { |
| 345 | case NORMAL: |
| 346 | return FAST_SCROLL_UNHIGHLIGHT_DURATION / 4; |
| 347 | } |
| 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Sets the saturation of this icon, 0 [full color] -> 1 [desaturated] |
| 354 | */ |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 355 | private void setDesaturation(float desaturation) { |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 356 | int newDesaturation = (int) Math.floor(desaturation * REDUCED_FILTER_VALUE_SPACE); |
| 357 | if (mDesaturation != newDesaturation) { |
| 358 | mDesaturation = newDesaturation; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 359 | updateFilter(); |
| 360 | } |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 363 | public float getDesaturation() { |
| 364 | return (float) mDesaturation / REDUCED_FILTER_VALUE_SPACE; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 367 | /** |
| 368 | * Sets the brightness of this icon, 0 [no add. brightness] -> 1 [2bright2furious] |
| 369 | */ |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 370 | private void setBrightness(float brightness) { |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 371 | int newBrightness = (int) Math.floor(brightness * REDUCED_FILTER_VALUE_SPACE); |
| 372 | if (mBrightness != newBrightness) { |
| 373 | mBrightness = newBrightness; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 374 | updateFilter(); |
| 375 | } |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 378 | private float getBrightness() { |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 379 | return (float) mBrightness / REDUCED_FILTER_VALUE_SPACE; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Updates the paint to reflect the current brightness and saturation. |
| 384 | */ |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 385 | private void updateFilter() { |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 386 | boolean usePorterDuffFilter = false; |
| 387 | int key = -1; |
| 388 | if (mDesaturation > 0) { |
| 389 | key = (mDesaturation << 16) | mBrightness; |
| 390 | } else if (mBrightness > 0) { |
| 391 | // Compose a key with a fully saturated icon if we are just animating brightness |
| 392 | key = (1 << 16) | mBrightness; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 393 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 394 | // We found that in L, ColorFilters cause drawing artifacts with shadows baked into |
| 395 | // icons, so just use a PorterDuff filter when we aren't animating saturation |
| 396 | usePorterDuffFilter = true; |
| 397 | } |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 398 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 399 | // Debounce multiple updates on the same frame |
| 400 | if (key == mPrevUpdateKey) { |
| 401 | return; |
| 402 | } |
| 403 | mPrevUpdateKey = key; |
| 404 | |
| 405 | if (key != -1) { |
| 406 | ColorFilter filter = sCachedFilter.get(key); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 407 | if (filter == null) { |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 408 | float brightnessF = getBrightness(); |
| 409 | int brightnessI = (int) (255 * brightnessF); |
| 410 | if (usePorterDuffFilter) { |
| 411 | filter = new PorterDuffColorFilter(Color.argb(brightnessI, 255, 255, 255), |
| 412 | PorterDuff.Mode.SRC_ATOP); |
| 413 | } else { |
| 414 | float saturationF = 1f - getDesaturation(); |
| 415 | sTempFilterMatrix.setSaturation(saturationF); |
| 416 | if (mBrightness > 0) { |
| 417 | // Brightness: C-new = C-old*(1-amount) + amount |
| 418 | float scale = 1f - brightnessF; |
| 419 | float[] mat = sTempBrightnessMatrix.getArray(); |
| 420 | mat[0] = scale; |
| 421 | mat[6] = scale; |
| 422 | mat[12] = scale; |
| 423 | mat[4] = brightnessI; |
| 424 | mat[9] = brightnessI; |
| 425 | mat[14] = brightnessI; |
| 426 | sTempFilterMatrix.preConcat(sTempBrightnessMatrix); |
| 427 | } |
| 428 | filter = new ColorMatrixColorFilter(sTempFilterMatrix); |
| 429 | } |
| 430 | sCachedFilter.append(key, filter); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 431 | } |
| 432 | mPaint.setColorFilter(filter); |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 433 | } else { |
| 434 | mPaint.setColorFilter(null); |
| 435 | } |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 436 | invalidateSelf(); |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 437 | } |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 438 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 439 | private AnimatorSet cancelAnimator(AnimatorSet animator) { |
| 440 | if (animator != null) { |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 441 | animator.cancel(); |
| 442 | } |
| 443 | return null; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 444 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 445 | } |