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 | |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 19 | import static com.android.launcher3.anim.Interpolators.ACCEL; |
Hyunyoung Song | ef468d8 | 2019-01-03 01:02:43 -0800 | [diff] [blame] | 20 | import static com.android.launcher3.anim.Interpolators.DEACCEL; |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 21 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 22 | import android.animation.ObjectAnimator; |
Sunny Goyal | 1416843 | 2019-10-24 15:59:49 -0700 | [diff] [blame] | 23 | import android.content.Context; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.Canvas; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 26 | import android.graphics.Color; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 27 | import android.graphics.ColorFilter; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 28 | import android.graphics.ColorMatrix; |
| 29 | import android.graphics.ColorMatrixColorFilter; |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 30 | import android.graphics.Paint; |
Pierre Barbier de Reuille | adc2326 | 2021-03-25 11:01:34 +0000 | [diff] [blame^] | 31 | import android.graphics.Path; |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 32 | import android.graphics.PixelFormat; |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 33 | import android.graphics.Rect; |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 34 | import android.graphics.drawable.Drawable; |
Tony Wickham | 1e61849 | 2017-02-02 12:57:18 -0800 | [diff] [blame] | 35 | import android.util.Property; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | |
Winson Chung | f993518 | 2020-10-23 09:26:44 -0700 | [diff] [blame] | 37 | import androidx.annotation.Nullable; |
| 38 | |
Sunny Goyal | 1416843 | 2019-10-24 15:59:49 -0700 | [diff] [blame] | 39 | import com.android.launcher3.graphics.PlaceHolderIconDrawable; |
Hyunyoung Song | 48cb7bc | 2018-09-25 17:03:34 -0700 | [diff] [blame] | 40 | import com.android.launcher3.icons.BitmapInfo; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 41 | import com.android.launcher3.model.data.ItemInfoWithIcon; |
Samuel Fufa | 61bc63a | 2020-06-05 12:17:16 -0700 | [diff] [blame] | 42 | import com.android.launcher3.util.Themes; |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 43 | |
Samuel Fufa | 61d3964 | 2020-01-23 12:42:06 -0800 | [diff] [blame] | 44 | |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 45 | public class FastBitmapDrawable extends Drawable { |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 46 | |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 47 | private static final float PRESSED_SCALE = 1.1f; |
| 48 | |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 49 | private static final float DISABLED_DESATURATION = 1f; |
| 50 | private static final float DISABLED_BRIGHTNESS = 0.5f; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 51 | |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 52 | public static final int CLICK_FEEDBACK_DURATION = 200; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 53 | |
Samuel Fufa | 61d3964 | 2020-01-23 12:42:06 -0800 | [diff] [blame] | 54 | private static ColorFilter sDisabledFColorFilter; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 55 | |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 56 | protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG); |
Sunny Goyal | 61e0846 | 2018-03-02 17:25:59 -0800 | [diff] [blame] | 57 | protected Bitmap mBitmap; |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 58 | protected final int mIconColor; |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 59 | |
Winson Chung | f993518 | 2020-10-23 09:26:44 -0700 | [diff] [blame] | 60 | @Nullable private ColorFilter mColorFilter; |
| 61 | |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 62 | private boolean mIsPressed; |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 63 | private boolean mIsDisabled; |
Samuel Fufa | 61bc63a | 2020-06-05 12:17:16 -0700 | [diff] [blame] | 64 | private float mDisabledAlpha = 1f; |
Pierre Barbier de Reuille | adc2326 | 2021-03-25 11:01:34 +0000 | [diff] [blame^] | 65 | private float mRoundedCornersRadius = 0f; |
| 66 | private final Path mClipPath = new Path(); |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 67 | |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 68 | // Animator and properties for the fast bitmap drawable's scale |
| 69 | private static final Property<FastBitmapDrawable, Float> SCALE |
| 70 | = new Property<FastBitmapDrawable, Float>(Float.TYPE, "scale") { |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 71 | @Override |
| 72 | public Float get(FastBitmapDrawable fastBitmapDrawable) { |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 73 | return fastBitmapDrawable.mScale; |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void set(FastBitmapDrawable fastBitmapDrawable, Float value) { |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 78 | fastBitmapDrawable.mScale = value; |
| 79 | fastBitmapDrawable.invalidateSelf(); |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 80 | } |
| 81 | }; |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 82 | private ObjectAnimator mScaleAnimation; |
| 83 | private float mScale = 1; |
| 84 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 85 | private int mAlpha = 255; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 86 | |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 87 | public FastBitmapDrawable(Bitmap b) { |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 88 | this(b, Color.TRANSPARENT); |
| 89 | } |
| 90 | |
| 91 | public FastBitmapDrawable(BitmapInfo info) { |
| 92 | this(info.icon, info.color); |
| 93 | } |
| 94 | |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 95 | protected FastBitmapDrawable(Bitmap b, int iconColor) { |
Jon Miranda | b6d686d | 2019-03-29 10:49:43 -0700 | [diff] [blame] | 96 | this(b, iconColor, false); |
| 97 | } |
| 98 | |
| 99 | protected FastBitmapDrawable(Bitmap b, int iconColor, boolean isDisabled) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 100 | mBitmap = b; |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 101 | mIconColor = iconColor; |
Sunny Goyal | 96ac68a | 2017-02-02 16:37:21 -0800 | [diff] [blame] | 102 | setFilterBitmap(true); |
Jon Miranda | b6d686d | 2019-03-29 10:49:43 -0700 | [diff] [blame] | 103 | setIsDisabled(isDisabled); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 107 | public final void draw(Canvas canvas) { |
Pierre Barbier de Reuille | adc2326 | 2021-03-25 11:01:34 +0000 | [diff] [blame^] | 108 | if (mRoundedCornersRadius > 0) { |
| 109 | float radius = mRoundedCornersRadius * mScale; |
| 110 | mClipPath.reset(); |
| 111 | mClipPath.addRoundRect(0, 0, getIntrinsicWidth(), getIntrinsicHeight(), |
| 112 | radius, radius, Path.Direction.CCW); |
| 113 | canvas.clipPath(mClipPath); |
| 114 | } |
Matthew Ng | eb9cc9d | 2018-06-25 15:32:24 -0700 | [diff] [blame] | 115 | if (mScale != 1f) { |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 116 | int count = canvas.save(); |
| 117 | Rect bounds = getBounds(); |
| 118 | canvas.scale(mScale, mScale, bounds.exactCenterX(), bounds.exactCenterY()); |
| 119 | drawInternal(canvas, bounds); |
| 120 | canvas.restoreToCount(count); |
| 121 | } else { |
| 122 | drawInternal(canvas, getBounds()); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | protected void drawInternal(Canvas canvas, Rect bounds) { |
| 127 | canvas.drawBitmap(mBitmap, null, bounds, mPaint); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 131 | public void setColorFilter(ColorFilter cf) { |
Winson Chung | f993518 | 2020-10-23 09:26:44 -0700 | [diff] [blame] | 132 | mColorFilter = cf; |
| 133 | updateFilter(); |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 137 | public int getOpacity() { |
| 138 | return PixelFormat.TRANSLUCENT; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void setAlpha(int alpha) { |
Jon Miranda | dff0de4 | 2019-08-30 18:42:01 -0700 | [diff] [blame] | 143 | if (mAlpha != alpha) { |
| 144 | mAlpha = alpha; |
| 145 | mPaint.setAlpha(alpha); |
| 146 | invalidateSelf(); |
| 147 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 150 | @Override |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 151 | public void setFilterBitmap(boolean filterBitmap) { |
| 152 | mPaint.setFilterBitmap(filterBitmap); |
Winson Chung | 6e1c0d3 | 2013-10-25 15:24:24 -0700 | [diff] [blame] | 153 | mPaint.setAntiAlias(filterBitmap); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 156 | public int getAlpha() { |
| 157 | return mAlpha; |
| 158 | } |
| 159 | |
Matthew Ng | eb9cc9d | 2018-06-25 15:32:24 -0700 | [diff] [blame] | 160 | public void setScale(float scale) { |
| 161 | if (mScaleAnimation != null) { |
| 162 | mScaleAnimation.cancel(); |
| 163 | mScaleAnimation = null; |
| 164 | } |
| 165 | mScale = scale; |
| 166 | invalidateSelf(); |
| 167 | } |
| 168 | |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 169 | public float getAnimatedScale() { |
| 170 | return mScaleAnimation == null ? 1 : mScale; |
| 171 | } |
| 172 | |
Matthew Ng | eb9cc9d | 2018-06-25 15:32:24 -0700 | [diff] [blame] | 173 | public float getScale() { |
| 174 | return mScale; |
| 175 | } |
| 176 | |
Pierre Barbier de Reuille | adc2326 | 2021-03-25 11:01:34 +0000 | [diff] [blame^] | 177 | public void setRoundedCornersRadius(float radius) { |
| 178 | mRoundedCornersRadius = radius; |
| 179 | } |
| 180 | |
| 181 | public float getRoundedCornersRadius() { |
| 182 | return mRoundedCornersRadius; |
| 183 | } |
| 184 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 185 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 186 | public int getIntrinsicWidth() { |
Sunny Goyal | c424f22 | 2014-09-05 07:04:59 -0700 | [diff] [blame] | 187 | return mBitmap.getWidth(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public int getIntrinsicHeight() { |
Sunny Goyal | c424f22 | 2014-09-05 07:04:59 -0700 | [diff] [blame] | 192 | return mBitmap.getHeight(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public int getMinimumWidth() { |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 197 | return getBounds().width(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public int getMinimumHeight() { |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 202 | return getBounds().height(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 205 | @Override |
| 206 | public boolean isStateful() { |
| 207 | return true; |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 210 | @Override |
Sunny Goyal | 2814112 | 2017-06-21 17:28:23 -0700 | [diff] [blame] | 211 | public ColorFilter getColorFilter() { |
| 212 | return mPaint.getColorFilter(); |
| 213 | } |
| 214 | |
| 215 | @Override |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 216 | protected boolean onStateChange(int[] state) { |
| 217 | boolean isPressed = false; |
| 218 | for (int s : state) { |
| 219 | if (s == android.R.attr.state_pressed) { |
| 220 | isPressed = true; |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | if (mIsPressed != isPressed) { |
| 225 | mIsPressed = isPressed; |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 226 | |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 227 | if (mScaleAnimation != null) { |
| 228 | mScaleAnimation.cancel(); |
| 229 | mScaleAnimation = null; |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 230 | } |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 231 | |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 232 | if (mIsPressed) { |
| 233 | // Animate when going to pressed state |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 234 | mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, PRESSED_SCALE); |
| 235 | mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION); |
| 236 | mScaleAnimation.setInterpolator(ACCEL); |
| 237 | mScaleAnimation.start(); |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 238 | } else { |
Hyunyoung Song | ef468d8 | 2019-01-03 01:02:43 -0800 | [diff] [blame] | 239 | if (isVisible()) { |
| 240 | mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, 1f); |
| 241 | mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION); |
| 242 | mScaleAnimation.setInterpolator(DEACCEL); |
| 243 | mScaleAnimation.start(); |
| 244 | } else { |
| 245 | mScale = 1f; |
| 246 | invalidateSelf(); |
| 247 | } |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 248 | } |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 249 | return true; |
| 250 | } |
| 251 | return false; |
| 252 | } |
| 253 | |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 254 | public void setIsDisabled(boolean isDisabled) { |
| 255 | if (mIsDisabled != isDisabled) { |
| 256 | mIsDisabled = isDisabled; |
Samuel Fufa | 61d3964 | 2020-01-23 12:42:06 -0800 | [diff] [blame] | 257 | updateFilter(); |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Jon Miranda | b6d686d | 2019-03-29 10:49:43 -0700 | [diff] [blame] | 261 | protected boolean isDisabled() { |
| 262 | return mIsDisabled; |
| 263 | } |
| 264 | |
Samuel Fufa | 61d3964 | 2020-01-23 12:42:06 -0800 | [diff] [blame] | 265 | private ColorFilter getDisabledColorFilter() { |
| 266 | if (sDisabledFColorFilter == null) { |
| 267 | ColorMatrix tempBrightnessMatrix = new ColorMatrix(); |
| 268 | ColorMatrix tempFilterMatrix = new ColorMatrix(); |
| 269 | |
| 270 | tempFilterMatrix.setSaturation(1f - DISABLED_DESATURATION); |
| 271 | float scale = 1 - DISABLED_BRIGHTNESS; |
| 272 | int brightnessI = (int) (255 * DISABLED_BRIGHTNESS); |
| 273 | float[] mat = tempBrightnessMatrix.getArray(); |
| 274 | mat[0] = scale; |
| 275 | mat[6] = scale; |
| 276 | mat[12] = scale; |
| 277 | mat[4] = brightnessI; |
| 278 | mat[9] = brightnessI; |
| 279 | mat[14] = brightnessI; |
Samuel Fufa | 61bc63a | 2020-06-05 12:17:16 -0700 | [diff] [blame] | 280 | mat[18] = mDisabledAlpha; |
Samuel Fufa | 61d3964 | 2020-01-23 12:42:06 -0800 | [diff] [blame] | 281 | tempFilterMatrix.preConcat(tempBrightnessMatrix); |
| 282 | sDisabledFColorFilter = new ColorMatrixColorFilter(tempFilterMatrix); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 283 | } |
Samuel Fufa | 61d3964 | 2020-01-23 12:42:06 -0800 | [diff] [blame] | 284 | return sDisabledFColorFilter; |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Updates the paint to reflect the current brightness and saturation. |
| 289 | */ |
Sunny Goyal | 0ffab44 | 2018-06-07 17:31:48 -0700 | [diff] [blame] | 290 | protected void updateFilter() { |
Winson Chung | f993518 | 2020-10-23 09:26:44 -0700 | [diff] [blame] | 291 | mPaint.setColorFilter(mIsDisabled ? getDisabledColorFilter() : mColorFilter); |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 292 | invalidateSelf(); |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 293 | } |
Sunny Goyal | 338d15d | 2018-02-23 12:19:44 -0800 | [diff] [blame] | 294 | |
| 295 | @Override |
| 296 | public ConstantState getConstantState() { |
Schneider Victor-tulias | b40b98d | 2021-01-21 14:41:07 -0800 | [diff] [blame] | 297 | return new FastBitmapConstantState(mBitmap, mIconColor, mIsDisabled); |
Sunny Goyal | 338d15d | 2018-02-23 12:19:44 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Schneider Victor-tulias | b40b98d | 2021-01-21 14:41:07 -0800 | [diff] [blame] | 300 | protected static class FastBitmapConstantState extends ConstantState { |
Sunny Goyal | 61e0846 | 2018-03-02 17:25:59 -0800 | [diff] [blame] | 301 | protected final Bitmap mBitmap; |
| 302 | protected final int mIconColor; |
Jon Miranda | b6d686d | 2019-03-29 10:49:43 -0700 | [diff] [blame] | 303 | protected final boolean mIsDisabled; |
Sunny Goyal | 338d15d | 2018-02-23 12:19:44 -0800 | [diff] [blame] | 304 | |
Schneider Victor-tulias | b40b98d | 2021-01-21 14:41:07 -0800 | [diff] [blame] | 305 | public FastBitmapConstantState(Bitmap bitmap, int color, boolean isDisabled) { |
Sunny Goyal | 338d15d | 2018-02-23 12:19:44 -0800 | [diff] [blame] | 306 | mBitmap = bitmap; |
| 307 | mIconColor = color; |
Jon Miranda | b6d686d | 2019-03-29 10:49:43 -0700 | [diff] [blame] | 308 | mIsDisabled = isDisabled; |
Sunny Goyal | 338d15d | 2018-02-23 12:19:44 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | @Override |
Sunny Goyal | 1416843 | 2019-10-24 15:59:49 -0700 | [diff] [blame] | 312 | public FastBitmapDrawable newDrawable() { |
Jon Miranda | b6d686d | 2019-03-29 10:49:43 -0700 | [diff] [blame] | 313 | return new FastBitmapDrawable(mBitmap, mIconColor, mIsDisabled); |
Sunny Goyal | 338d15d | 2018-02-23 12:19:44 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public int getChangingConfigurations() { |
| 318 | return 0; |
| 319 | } |
| 320 | } |
Sunny Goyal | 1416843 | 2019-10-24 15:59:49 -0700 | [diff] [blame] | 321 | |
| 322 | /** |
| 323 | * Interface to be implemented by custom {@link BitmapInfo} to handle drawable construction |
| 324 | */ |
| 325 | public interface Factory { |
| 326 | |
| 327 | /** |
| 328 | * Called to create a new drawable |
| 329 | */ |
| 330 | FastBitmapDrawable newDrawable(); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Returns a FastBitmapDrawable with the icon. |
| 335 | */ |
| 336 | public static FastBitmapDrawable newIcon(Context context, ItemInfoWithIcon info) { |
| 337 | FastBitmapDrawable drawable = newIcon(context, info.bitmap); |
| 338 | drawable.setIsDisabled(info.isDisabled()); |
| 339 | return drawable; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Creates a drawable for the provided BitmapInfo |
| 344 | */ |
| 345 | public static FastBitmapDrawable newIcon(Context context, BitmapInfo info) { |
Samuel Fufa | 61bc63a | 2020-06-05 12:17:16 -0700 | [diff] [blame] | 346 | final FastBitmapDrawable drawable; |
Sunny Goyal | 1416843 | 2019-10-24 15:59:49 -0700 | [diff] [blame] | 347 | if (info instanceof Factory) { |
Samuel Fufa | 61bc63a | 2020-06-05 12:17:16 -0700 | [diff] [blame] | 348 | drawable = ((Factory) info).newDrawable(); |
Sunny Goyal | 1416843 | 2019-10-24 15:59:49 -0700 | [diff] [blame] | 349 | } else if (info.isLowRes()) { |
Samuel Fufa | 61bc63a | 2020-06-05 12:17:16 -0700 | [diff] [blame] | 350 | drawable = new PlaceHolderIconDrawable(info, context); |
Sunny Goyal | 1416843 | 2019-10-24 15:59:49 -0700 | [diff] [blame] | 351 | } else { |
Samuel Fufa | 61bc63a | 2020-06-05 12:17:16 -0700 | [diff] [blame] | 352 | drawable = new FastBitmapDrawable(info); |
Sunny Goyal | 1416843 | 2019-10-24 15:59:49 -0700 | [diff] [blame] | 353 | } |
Samuel Fufa | 61bc63a | 2020-06-05 12:17:16 -0700 | [diff] [blame] | 354 | drawable.mDisabledAlpha = Themes.getFloat(context, R.attr.disabledIconAlpha, 1f); |
| 355 | return drawable; |
Sunny Goyal | 1416843 | 2019-10-24 15:59:49 -0700 | [diff] [blame] | 356 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 357 | } |