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