blob: be3ba90145d96cecf80806303fee266835b33be1 [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 Goyal508da152014-08-14 10:53:27 -070019import android.animation.ObjectAnimator;
20import android.animation.TimeInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.graphics.Bitmap;
22import android.graphics.Canvas;
Sunny Goyal508da152014-08-14 10:53:27 -070023import android.graphics.Color;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.ColorFilter;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070025import android.graphics.ColorMatrix;
26import android.graphics.ColorMatrixColorFilter;
Winson Chung45e1d6e2010-11-09 17:19:49 -080027import android.graphics.Paint;
28import android.graphics.PixelFormat;
Sunny Goyal508da152014-08-14 10:53:27 -070029import android.graphics.PorterDuff;
30import android.graphics.PorterDuffColorFilter;
Winson Chung45e1d6e2010-11-09 17:19:49 -080031import android.graphics.drawable.Drawable;
Tony Wickham1e618492017-02-02 12:57:18 -080032import android.util.Property;
Sunny Goyal508da152014-08-14 10:53:27 -070033import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034
Tony Wickham9a8d11f2017-01-11 09:53:12 -080035import com.android.launcher3.badge.BadgeInfo;
Tony Wickham010d2552017-01-20 08:15:28 -080036import com.android.launcher3.badge.BadgeRenderer;
37import com.android.launcher3.graphics.IconPalette;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080038
Hyunyoung Song3f471442015-04-08 19:01:34 -070039public class FastBitmapDrawable extends Drawable {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080040
41 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
42
43 private static final float PRESSED_BRIGHTNESS = 100f / 255f;
Tony Wickham6b910a22016-11-08 10:40:34 -080044 private static final float DISABLED_DESATURATION = 1f;
45 private static final float DISABLED_BRIGHTNESS = 0.5f;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070046
Winsonc0880492015-08-21 11:16:27 -070047 public static final TimeInterpolator CLICK_FEEDBACK_INTERPOLATOR = new TimeInterpolator() {
Sunny Goyal508da152014-08-14 10:53:27 -070048
49 @Override
50 public float getInterpolation(float input) {
51 if (input < 0.05f) {
52 return input / 0.05f;
53 } else if (input < 0.3f){
54 return 1;
55 } else {
56 return (1 - input) / 0.7f;
57 }
58 }
59 };
Winsonc0880492015-08-21 11:16:27 -070060 public static final int CLICK_FEEDBACK_DURATION = 2000;
Sunny Goyal508da152014-08-14 10:53:27 -070061
Winsonc0880492015-08-21 11:16:27 -070062 // Since we don't need 256^2 values for combinations of both the brightness and saturation, we
63 // reduce the value space to a smaller value V, which reduces the number of cached
64 // ColorMatrixColorFilters that we need to keep to V^2
65 private static final int REDUCED_FILTER_VALUE_SPACE = 48;
Sunny Goyal95abbb32014-08-04 10:53:22 -070066
Winsonc0880492015-08-21 11:16:27 -070067 // A cache of ColorFilters for optimizing brightness and saturation animations
68 private static final SparseArray<ColorFilter> sCachedFilter = new SparseArray<>();
Sunny Goyal508da152014-08-14 10:53:27 -070069
Winsonc0880492015-08-21 11:16:27 -070070 // Temporary matrices used for calculation
71 private static final ColorMatrix sTempBrightnessMatrix = new ColorMatrix();
72 private static final ColorMatrix sTempFilterMatrix = new ColorMatrix();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070073
Sunny Goyal55cb70b2016-11-12 09:58:29 -080074 protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Sunny Goyal508da152014-08-14 10:53:27 -070075 private final Bitmap mBitmap;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080076
77 private boolean mIsPressed;
Tony Wickham6b910a22016-11-08 10:40:34 -080078 private boolean mIsDisabled;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070079
Tony Wickham9a8d11f2017-01-11 09:53:12 -080080 private BadgeInfo mBadgeInfo;
81 private BadgeRenderer mBadgeRenderer;
82 private IconPalette mIconPalette;
Tony Wickham1e618492017-02-02 12:57:18 -080083 private float mBadgeScale;
84
85 private static final Property<FastBitmapDrawable, Float> BADGE_SCALE_PROPERTY
86 = new Property<FastBitmapDrawable, Float>(Float.TYPE, "badgeScale") {
87 @Override
88 public Float get(FastBitmapDrawable fastBitmapDrawable) {
89 return fastBitmapDrawable.mBadgeScale;
90 }
91
92 @Override
93 public void set(FastBitmapDrawable fastBitmapDrawable, Float value) {
94 fastBitmapDrawable.mBadgeScale = value;
95 fastBitmapDrawable.invalidateSelf();
96 }
97 };
Tony Wickham9a8d11f2017-01-11 09:53:12 -080098
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080099 private static final Property<FastBitmapDrawable, Float> BRIGHTNESS
100 = new Property<FastBitmapDrawable, Float>(Float.TYPE, "brightness") {
101 @Override
102 public Float get(FastBitmapDrawable fastBitmapDrawable) {
103 return fastBitmapDrawable.getBrightness();
104 }
105
106 @Override
107 public void set(FastBitmapDrawable fastBitmapDrawable, Float value) {
108 fastBitmapDrawable.setBrightness(value);
109 }
110 };
111
Winsonc0880492015-08-21 11:16:27 -0700112 // The saturation and brightness are values that are mapped to REDUCED_FILTER_VALUE_SPACE and
113 // as a result, can be used to compose the key for the cached ColorMatrixColorFilters
114 private int mDesaturation = 0;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700115 private int mBrightness = 0;
Winsonc0880492015-08-21 11:16:27 -0700116 private int mAlpha = 255;
117 private int mPrevUpdateKey = Integer.MAX_VALUE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800119 // Animators for the fast bitmap drawable's brightness
120 private ObjectAnimator mBrightnessAnimator;
Sunny Goyal508da152014-08-14 10:53:27 -0700121
Hyunyoung Song3f471442015-04-08 19:01:34 -0700122 public FastBitmapDrawable(Bitmap b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 mBitmap = b;
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800124 setFilterBitmap(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 }
126
Tony Wickham1e618492017-02-02 12:57:18 -0800127 public void applyIconBadge(final BadgeInfo badgeInfo, BadgeRenderer badgeRenderer,
128 boolean animate) {
Tony Wickham010d2552017-01-20 08:15:28 -0800129 boolean wasBadged = mBadgeInfo != null;
130 boolean isBadged = badgeInfo != null;
Tony Wickham1e618492017-02-02 12:57:18 -0800131 float newBadgeScale = isBadged ? 1f : 0;
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800132 mBadgeInfo = badgeInfo;
133 mBadgeRenderer = badgeRenderer;
Tony Wickham010d2552017-01-20 08:15:28 -0800134 if (wasBadged || isBadged) {
Tony Wickham9438ed42017-01-20 09:38:25 -0800135 mIconPalette = getIconPalette();
Tony Wickham1e618492017-02-02 12:57:18 -0800136 // Animate when a badge is first added or when it is removed.
137 if (animate && (wasBadged ^ isBadged) && isVisible()) {
138 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
139 } else {
140 mBadgeScale = newBadgeScale;
141 invalidateSelf();
142 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800143 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800144 }
145
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 @Override
147 public void draw(Canvas canvas) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800148 drawInternal(canvas);
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800149 // Draw the icon badge in the top right corner.
150 drawBadgeIfNecessary(canvas);
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800151 }
152
153 public void drawWithBrightness(Canvas canvas, float brightness) {
154 float oldBrightness = getBrightness();
155 setBrightness(brightness);
156 drawInternal(canvas);
157 setBrightness(oldBrightness);
158 }
159
160 protected void drawInternal(Canvas canvas) {
Winsonc0880492015-08-21 11:16:27 -0700161 canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 }
163
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800164 protected void drawBadgeIfNecessary(Canvas canvas) {
165 if (hasBadge()) {
Tony Wickham1e618492017-02-02 12:57:18 -0800166 mBadgeRenderer.draw(canvas, mIconPalette, mBadgeInfo, getBounds(), mBadgeScale);
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800167 }
168 }
169
Tony Wickhamce445162017-04-10 14:05:34 -0700170 public IconPalette getIconPalette() {
Tony Wickham9438ed42017-01-20 09:38:25 -0800171 if (mIconPalette == null) {
172 mIconPalette = IconPalette.fromDominantColor(Utilities
173 .findDominantColorByHue(mBitmap, 20));
174 }
175 return mIconPalette;
176 }
177
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800178 private boolean hasBadge() {
Tony Wickham1e618492017-02-02 12:57:18 -0800179 return (mBadgeInfo != null && mBadgeInfo.getNotificationCount() > 0) || mBadgeScale > 0;
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800180 }
181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700183 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700184 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -0700185 }
186
187 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 public int getOpacity() {
189 return PixelFormat.TRANSLUCENT;
190 }
191
192 @Override
193 public void setAlpha(int alpha) {
Winson Chung29d6fea2010-12-01 15:47:31 -0800194 mAlpha = alpha;
Winson Chungb3347bb2010-08-19 14:51:28 -0700195 mPaint.setAlpha(alpha);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 }
197
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700198 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700199 public void setFilterBitmap(boolean filterBitmap) {
200 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700201 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700202 }
203
Winson Chung29d6fea2010-12-01 15:47:31 -0800204 public int getAlpha() {
205 return mAlpha;
206 }
207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700210 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 }
212
213 @Override
214 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700215 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 }
217
218 @Override
219 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800220 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 }
222
223 @Override
224 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800225 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800226 }
227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 public Bitmap getBitmap() {
229 return mBitmap;
230 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700231
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800232 @Override
233 public boolean isStateful() {
234 return true;
Winsonc0880492015-08-21 11:16:27 -0700235 }
236
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800237 @Override
238 protected boolean onStateChange(int[] state) {
239 boolean isPressed = false;
240 for (int s : state) {
241 if (s == android.R.attr.state_pressed) {
242 isPressed = true;
243 break;
244 }
245 }
246 if (mIsPressed != isPressed) {
247 mIsPressed = isPressed;
Winsonc0880492015-08-21 11:16:27 -0700248
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800249 if (mBrightnessAnimator != null) {
250 mBrightnessAnimator.cancel();
251 }
Winsonc0880492015-08-21 11:16:27 -0700252
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800253 if (mIsPressed) {
254 // Animate when going to pressed state
255 mBrightnessAnimator = ObjectAnimator.ofFloat(
256 this, BRIGHTNESS, getExpectedBrightness());
257 mBrightnessAnimator.setDuration(CLICK_FEEDBACK_DURATION);
258 mBrightnessAnimator.setInterpolator(CLICK_FEEDBACK_INTERPOLATOR);
259 mBrightnessAnimator.start();
260 } else {
261 setBrightness(getExpectedBrightness());
262 }
Winsonc0880492015-08-21 11:16:27 -0700263 return true;
264 }
265 return false;
266 }
267
Tony Wickham6b910a22016-11-08 10:40:34 -0800268 private void invalidateDesaturationAndBrightness() {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800269 setDesaturation(mIsDisabled ? DISABLED_DESATURATION : 0);
270 setBrightness(getExpectedBrightness());
Tony Wickham6b910a22016-11-08 10:40:34 -0800271 }
272
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800273 private float getExpectedBrightness() {
274 return mIsDisabled ? DISABLED_BRIGHTNESS :
275 (mIsPressed ? PRESSED_BRIGHTNESS : 0);
Winsonc0880492015-08-21 11:16:27 -0700276 }
277
Tony Wickham6b910a22016-11-08 10:40:34 -0800278 public void setIsDisabled(boolean isDisabled) {
279 if (mIsDisabled != isDisabled) {
280 mIsDisabled = isDisabled;
281 invalidateDesaturationAndBrightness();
282 }
283 }
284
Winsonc0880492015-08-21 11:16:27 -0700285 /**
Winsonc0880492015-08-21 11:16:27 -0700286 * Sets the saturation of this icon, 0 [full color] -> 1 [desaturated]
287 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800288 private void setDesaturation(float desaturation) {
Winsonc0880492015-08-21 11:16:27 -0700289 int newDesaturation = (int) Math.floor(desaturation * REDUCED_FILTER_VALUE_SPACE);
290 if (mDesaturation != newDesaturation) {
291 mDesaturation = newDesaturation;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700292 updateFilter();
293 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700294 }
295
Winsonc0880492015-08-21 11:16:27 -0700296 public float getDesaturation() {
297 return (float) mDesaturation / REDUCED_FILTER_VALUE_SPACE;
Sunny Goyal508da152014-08-14 10:53:27 -0700298 }
299
Winsonc0880492015-08-21 11:16:27 -0700300 /**
301 * Sets the brightness of this icon, 0 [no add. brightness] -> 1 [2bright2furious]
302 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800303 private void setBrightness(float brightness) {
Winsonc0880492015-08-21 11:16:27 -0700304 int newBrightness = (int) Math.floor(brightness * REDUCED_FILTER_VALUE_SPACE);
305 if (mBrightness != newBrightness) {
306 mBrightness = newBrightness;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700307 updateFilter();
308 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700309 }
310
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800311 private float getBrightness() {
Winsonc0880492015-08-21 11:16:27 -0700312 return (float) mBrightness / REDUCED_FILTER_VALUE_SPACE;
313 }
314
315 /**
316 * Updates the paint to reflect the current brightness and saturation.
317 */
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700318 private void updateFilter() {
Winsonc0880492015-08-21 11:16:27 -0700319 boolean usePorterDuffFilter = false;
320 int key = -1;
321 if (mDesaturation > 0) {
322 key = (mDesaturation << 16) | mBrightness;
323 } else if (mBrightness > 0) {
324 // Compose a key with a fully saturated icon if we are just animating brightness
325 key = (1 << 16) | mBrightness;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700326
Winsonc0880492015-08-21 11:16:27 -0700327 // We found that in L, ColorFilters cause drawing artifacts with shadows baked into
328 // icons, so just use a PorterDuff filter when we aren't animating saturation
329 usePorterDuffFilter = true;
330 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700331
Winsonc0880492015-08-21 11:16:27 -0700332 // Debounce multiple updates on the same frame
333 if (key == mPrevUpdateKey) {
334 return;
335 }
336 mPrevUpdateKey = key;
337
338 if (key != -1) {
339 ColorFilter filter = sCachedFilter.get(key);
Sunny Goyal508da152014-08-14 10:53:27 -0700340 if (filter == null) {
Winsonc0880492015-08-21 11:16:27 -0700341 float brightnessF = getBrightness();
342 int brightnessI = (int) (255 * brightnessF);
343 if (usePorterDuffFilter) {
344 filter = new PorterDuffColorFilter(Color.argb(brightnessI, 255, 255, 255),
345 PorterDuff.Mode.SRC_ATOP);
346 } else {
347 float saturationF = 1f - getDesaturation();
348 sTempFilterMatrix.setSaturation(saturationF);
349 if (mBrightness > 0) {
350 // Brightness: C-new = C-old*(1-amount) + amount
351 float scale = 1f - brightnessF;
352 float[] mat = sTempBrightnessMatrix.getArray();
353 mat[0] = scale;
354 mat[6] = scale;
355 mat[12] = scale;
356 mat[4] = brightnessI;
357 mat[9] = brightnessI;
358 mat[14] = brightnessI;
359 sTempFilterMatrix.preConcat(sTempBrightnessMatrix);
360 }
361 filter = new ColorMatrixColorFilter(sTempFilterMatrix);
362 }
363 sCachedFilter.append(key, filter);
Sunny Goyal508da152014-08-14 10:53:27 -0700364 }
365 mPaint.setColorFilter(filter);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700366 } else {
367 mPaint.setColorFilter(null);
368 }
Winsonc0880492015-08-21 11:16:27 -0700369 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700370 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371}