blob: 139d4a85e400c1376c2fd28c6705932ae4108def [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal726bee72018-03-05 12:54:24 -080019import static com.android.launcher3.anim.Interpolators.ACCEL;
Hyunyoung Songef468d82019-01-03 01:02:43 -080020import static com.android.launcher3.anim.Interpolators.DEACCEL;
Sunny Goyal726bee72018-03-05 12:54:24 -080021
Sunny Goyal508da152014-08-14 10:53:27 -070022import android.animation.ObjectAnimator;
Sunny Goyal14168432019-10-24 15:59:49 -070023import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.Bitmap;
25import android.graphics.Canvas;
Sunny Goyal508da152014-08-14 10:53:27 -070026import android.graphics.Color;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.graphics.ColorFilter;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070028import android.graphics.ColorMatrix;
29import android.graphics.ColorMatrixColorFilter;
Winson Chung45e1d6e2010-11-09 17:19:49 -080030import android.graphics.Paint;
31import android.graphics.PixelFormat;
Sunny Goyal726bee72018-03-05 12:54:24 -080032import android.graphics.Rect;
Winson Chung45e1d6e2010-11-09 17:19:49 -080033import android.graphics.drawable.Drawable;
Tony Wickham1e618492017-02-02 12:57:18 -080034import android.util.Property;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035
Winson Chungf9935182020-10-23 09:26:44 -070036import androidx.annotation.Nullable;
37
Sunny Goyal14168432019-10-24 15:59:49 -070038import com.android.launcher3.graphics.PlaceHolderIconDrawable;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070039import com.android.launcher3.icons.BitmapInfo;
Sunny Goyale396abf2020-04-06 15:11:17 -070040import com.android.launcher3.model.data.ItemInfoWithIcon;
Samuel Fufa61bc63a2020-06-05 12:17:16 -070041import com.android.launcher3.util.Themes;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080042
Samuel Fufa61d39642020-01-23 12:42:06 -080043
Hyunyoung Song3f471442015-04-08 19:01:34 -070044public class FastBitmapDrawable extends Drawable {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080045
Sunny Goyal726bee72018-03-05 12:54:24 -080046 private static final float PRESSED_SCALE = 1.1f;
47
Tony Wickham6b910a22016-11-08 10:40:34 -080048 private static final float DISABLED_DESATURATION = 1f;
49 private static final float DISABLED_BRIGHTNESS = 0.5f;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070050
Sunny Goyal726bee72018-03-05 12:54:24 -080051 public static final int CLICK_FEEDBACK_DURATION = 200;
Sunny Goyal508da152014-08-14 10:53:27 -070052
Samuel Fufa61d39642020-01-23 12:42:06 -080053 private static ColorFilter sDisabledFColorFilter;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070054
Sunny Goyal55cb70b2016-11-12 09:58:29 -080055 protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Sunny Goyal61e08462018-03-02 17:25:59 -080056 protected Bitmap mBitmap;
Sunny Goyal179249d2017-12-19 16:49:24 -080057 protected final int mIconColor;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080058
Winson Chungf9935182020-10-23 09:26:44 -070059 @Nullable private ColorFilter mColorFilter;
60
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080061 private boolean mIsPressed;
Tony Wickham6b910a22016-11-08 10:40:34 -080062 private boolean mIsDisabled;
Samuel Fufa61bc63a2020-06-05 12:17:16 -070063 private float mDisabledAlpha = 1f;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070064
Sunny Goyal726bee72018-03-05 12:54:24 -080065 // 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 Goyal2a76e3f2017-02-16 13:33:15 -080068 @Override
69 public Float get(FastBitmapDrawable fastBitmapDrawable) {
Sunny Goyal726bee72018-03-05 12:54:24 -080070 return fastBitmapDrawable.mScale;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080071 }
72
73 @Override
74 public void set(FastBitmapDrawable fastBitmapDrawable, Float value) {
Sunny Goyal726bee72018-03-05 12:54:24 -080075 fastBitmapDrawable.mScale = value;
76 fastBitmapDrawable.invalidateSelf();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080077 }
78 };
Sunny Goyal726bee72018-03-05 12:54:24 -080079 private ObjectAnimator mScaleAnimation;
80 private float mScale = 1;
81
Winsonc0880492015-08-21 11:16:27 -070082 private int mAlpha = 255;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083
Hyunyoung Song3f471442015-04-08 19:01:34 -070084 public FastBitmapDrawable(Bitmap b) {
Sunny Goyal179249d2017-12-19 16:49:24 -080085 this(b, Color.TRANSPARENT);
86 }
87
88 public FastBitmapDrawable(BitmapInfo info) {
89 this(info.icon, info.color);
90 }
91
Sunny Goyal179249d2017-12-19 16:49:24 -080092 protected FastBitmapDrawable(Bitmap b, int iconColor) {
Jon Mirandab6d686d2019-03-29 10:49:43 -070093 this(b, iconColor, false);
94 }
95
96 protected FastBitmapDrawable(Bitmap b, int iconColor, boolean isDisabled) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 mBitmap = b;
Sunny Goyal179249d2017-12-19 16:49:24 -080098 mIconColor = iconColor;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080099 setFilterBitmap(true);
Jon Mirandab6d686d2019-03-29 10:49:43 -0700100 setIsDisabled(isDisabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 }
102
103 @Override
Sunny Goyal726bee72018-03-05 12:54:24 -0800104 public final void draw(Canvas canvas) {
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700105 if (mScale != 1f) {
Sunny Goyal726bee72018-03-05 12:54:24 -0800106 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 Project31dd5032009-03-03 19:32:27 -0800118 }
119
120 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700121 public void setColorFilter(ColorFilter cf) {
Winson Chungf9935182020-10-23 09:26:44 -0700122 mColorFilter = cf;
123 updateFilter();
Adam Cohenbadf71e2011-05-26 19:08:29 -0700124 }
125
126 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 public int getOpacity() {
128 return PixelFormat.TRANSLUCENT;
129 }
130
131 @Override
132 public void setAlpha(int alpha) {
Jon Mirandadff0de42019-08-30 18:42:01 -0700133 if (mAlpha != alpha) {
134 mAlpha = alpha;
135 mPaint.setAlpha(alpha);
136 invalidateSelf();
137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 }
139
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700140 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700141 public void setFilterBitmap(boolean filterBitmap) {
142 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700143 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700144 }
145
Winson Chung29d6fea2010-12-01 15:47:31 -0800146 public int getAlpha() {
147 return mAlpha;
148 }
149
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700150 public void setScale(float scale) {
151 if (mScaleAnimation != null) {
152 mScaleAnimation.cancel();
153 mScaleAnimation = null;
154 }
155 mScale = scale;
156 invalidateSelf();
157 }
158
Sunny Goyal726bee72018-03-05 12:54:24 -0800159 public float getAnimatedScale() {
160 return mScaleAnimation == null ? 1 : mScale;
161 }
162
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700163 public float getScale() {
164 return mScale;
165 }
166
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700169 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 }
171
172 @Override
173 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700174 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 }
176
177 @Override
178 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800179 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 }
181
182 @Override
183 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800184 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800185 }
186
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800187 @Override
188 public boolean isStateful() {
189 return true;
Winsonc0880492015-08-21 11:16:27 -0700190 }
191
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800192 @Override
Sunny Goyal28141122017-06-21 17:28:23 -0700193 public ColorFilter getColorFilter() {
194 return mPaint.getColorFilter();
195 }
196
197 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800198 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;
Winsonc0880492015-08-21 11:16:27 -0700208
Sunny Goyal726bee72018-03-05 12:54:24 -0800209 if (mScaleAnimation != null) {
210 mScaleAnimation.cancel();
211 mScaleAnimation = null;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800212 }
Winsonc0880492015-08-21 11:16:27 -0700213
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800214 if (mIsPressed) {
215 // Animate when going to pressed state
Sunny Goyal726bee72018-03-05 12:54:24 -0800216 mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, PRESSED_SCALE);
217 mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
218 mScaleAnimation.setInterpolator(ACCEL);
219 mScaleAnimation.start();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800220 } else {
Hyunyoung Songef468d82019-01-03 01:02:43 -0800221 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 Goyal2a76e3f2017-02-16 13:33:15 -0800230 }
Winsonc0880492015-08-21 11:16:27 -0700231 return true;
232 }
233 return false;
234 }
235
Tony Wickham6b910a22016-11-08 10:40:34 -0800236 public void setIsDisabled(boolean isDisabled) {
237 if (mIsDisabled != isDisabled) {
238 mIsDisabled = isDisabled;
Samuel Fufa61d39642020-01-23 12:42:06 -0800239 updateFilter();
Tony Wickham6b910a22016-11-08 10:40:34 -0800240 }
241 }
242
Jon Mirandab6d686d2019-03-29 10:49:43 -0700243 protected boolean isDisabled() {
244 return mIsDisabled;
245 }
246
Samuel Fufa61d39642020-01-23 12:42:06 -0800247 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 Fufa61bc63a2020-06-05 12:17:16 -0700262 mat[18] = mDisabledAlpha;
Samuel Fufa61d39642020-01-23 12:42:06 -0800263 tempFilterMatrix.preConcat(tempBrightnessMatrix);
264 sDisabledFColorFilter = new ColorMatrixColorFilter(tempFilterMatrix);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700265 }
Samuel Fufa61d39642020-01-23 12:42:06 -0800266 return sDisabledFColorFilter;
Winsonc0880492015-08-21 11:16:27 -0700267 }
268
269 /**
270 * Updates the paint to reflect the current brightness and saturation.
271 */
Sunny Goyal0ffab442018-06-07 17:31:48 -0700272 protected void updateFilter() {
Winson Chungf9935182020-10-23 09:26:44 -0700273 mPaint.setColorFilter(mIsDisabled ? getDisabledColorFilter() : mColorFilter);
Winsonc0880492015-08-21 11:16:27 -0700274 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700275 }
Sunny Goyal338d15d2018-02-23 12:19:44 -0800276
277 @Override
278 public ConstantState getConstantState() {
Jon Mirandab6d686d2019-03-29 10:49:43 -0700279 return new MyConstantState(mBitmap, mIconColor, mIsDisabled);
Sunny Goyal338d15d2018-02-23 12:19:44 -0800280 }
281
Sunny Goyal61e08462018-03-02 17:25:59 -0800282 protected static class MyConstantState extends ConstantState {
283 protected final Bitmap mBitmap;
284 protected final int mIconColor;
Jon Mirandab6d686d2019-03-29 10:49:43 -0700285 protected final boolean mIsDisabled;
Sunny Goyal338d15d2018-02-23 12:19:44 -0800286
Jon Mirandab6d686d2019-03-29 10:49:43 -0700287 public MyConstantState(Bitmap bitmap, int color, boolean isDisabled) {
Sunny Goyal338d15d2018-02-23 12:19:44 -0800288 mBitmap = bitmap;
289 mIconColor = color;
Jon Mirandab6d686d2019-03-29 10:49:43 -0700290 mIsDisabled = isDisabled;
Sunny Goyal338d15d2018-02-23 12:19:44 -0800291 }
292
293 @Override
Sunny Goyal14168432019-10-24 15:59:49 -0700294 public FastBitmapDrawable newDrawable() {
Jon Mirandab6d686d2019-03-29 10:49:43 -0700295 return new FastBitmapDrawable(mBitmap, mIconColor, mIsDisabled);
Sunny Goyal338d15d2018-02-23 12:19:44 -0800296 }
297
298 @Override
299 public int getChangingConfigurations() {
300 return 0;
301 }
302 }
Sunny Goyal14168432019-10-24 15:59:49 -0700303
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 Fufa61bc63a2020-06-05 12:17:16 -0700328 final FastBitmapDrawable drawable;
Sunny Goyal14168432019-10-24 15:59:49 -0700329 if (info instanceof Factory) {
Samuel Fufa61bc63a2020-06-05 12:17:16 -0700330 drawable = ((Factory) info).newDrawable();
Sunny Goyal14168432019-10-24 15:59:49 -0700331 } else if (info.isLowRes()) {
Samuel Fufa61bc63a2020-06-05 12:17:16 -0700332 drawable = new PlaceHolderIconDrawable(info, context);
Sunny Goyal14168432019-10-24 15:59:49 -0700333 } else {
Samuel Fufa61bc63a2020-06-05 12:17:16 -0700334 drawable = new FastBitmapDrawable(info);
Sunny Goyal14168432019-10-24 15:59:49 -0700335 }
Samuel Fufa61bc63a2020-06-05 12:17:16 -0700336 drawable.mDisabledAlpha = Themes.getFloat(context, R.attr.disabledIconAlpha, 1f);
337 return drawable;
Sunny Goyal14168432019-10-24 15:59:49 -0700338 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339}