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