blob: 587d44552ece1102c6a0f2aa19ab17d7f5a1a54d [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
Winsonc0880492015-08-21 11:16:27 -070019import android.animation.AnimatorSet;
Sunny Goyal508da152014-08-14 10:53:27 -070020import android.animation.ObjectAnimator;
21import android.animation.TimeInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.graphics.Bitmap;
23import android.graphics.Canvas;
Sunny Goyal508da152014-08-14 10:53:27 -070024import android.graphics.Color;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.graphics.ColorFilter;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070026import android.graphics.ColorMatrix;
27import android.graphics.ColorMatrixColorFilter;
Winson Chung45e1d6e2010-11-09 17:19:49 -080028import android.graphics.Paint;
29import android.graphics.PixelFormat;
Sunny Goyal508da152014-08-14 10:53:27 -070030import android.graphics.PorterDuff;
31import android.graphics.PorterDuffColorFilter;
Winson Chung45e1d6e2010-11-09 17:19:49 -080032import android.graphics.drawable.Drawable;
Tony Wickham010d2552017-01-20 08:15:28 -080033import android.util.Log;
Sunny Goyal508da152014-08-14 10:53:27 -070034import android.util.SparseArray;
Winsonc0880492015-08-21 11:16:27 -070035import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036
Tony Wickham9a8d11f2017-01-11 09:53:12 -080037import com.android.launcher3.badge.BadgeInfo;
Tony Wickham010d2552017-01-20 08:15:28 -080038import com.android.launcher3.badge.BadgeRenderer;
39import com.android.launcher3.graphics.IconPalette;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080040
Hyunyoung Song3f471442015-04-08 19:01:34 -070041public class FastBitmapDrawable extends Drawable {
Tony Wickham6b910a22016-11-08 10:40:34 -080042 private static final float DISABLED_DESATURATION = 1f;
43 private static final float DISABLED_BRIGHTNESS = 0.5f;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070044
Winsonc0880492015-08-21 11:16:27 -070045 /**
46 * The possible states that a FastBitmapDrawable can be in.
47 */
48 public enum State {
49
50 NORMAL (0f, 0f, 1f, new DecelerateInterpolator()),
51 PRESSED (0f, 100f / 255f, 1f, CLICK_FEEDBACK_INTERPOLATOR),
Winsonc08c59d2015-10-28 15:30:38 -070052 FAST_SCROLL_HIGHLIGHTED (0f, 0f, 1.15f, new DecelerateInterpolator()),
Tony Wickham6b910a22016-11-08 10:40:34 -080053 FAST_SCROLL_UNHIGHLIGHTED (0f, 0f, 1f, new DecelerateInterpolator());
Winsonc0880492015-08-21 11:16:27 -070054
55 public final float desaturation;
56 public final float brightness;
57 /**
58 * Used specifically by the view drawing this FastBitmapDrawable.
59 */
60 public final float viewScale;
61 public final TimeInterpolator interpolator;
62
63 State(float desaturation, float brightness, float viewScale, TimeInterpolator interpolator) {
64 this.desaturation = desaturation;
65 this.brightness = brightness;
66 this.viewScale = viewScale;
67 this.interpolator = interpolator;
68 }
69 }
70
71 public static final TimeInterpolator CLICK_FEEDBACK_INTERPOLATOR = new TimeInterpolator() {
Sunny Goyal508da152014-08-14 10:53:27 -070072
73 @Override
74 public float getInterpolation(float input) {
75 if (input < 0.05f) {
76 return input / 0.05f;
77 } else if (input < 0.3f){
78 return 1;
79 } else {
80 return (1 - input) / 0.7f;
81 }
82 }
83 };
Winsonc0880492015-08-21 11:16:27 -070084 public static final int CLICK_FEEDBACK_DURATION = 2000;
85 public static final int FAST_SCROLL_HIGHLIGHT_DURATION = 225;
86 public static final int FAST_SCROLL_UNHIGHLIGHT_DURATION = 150;
87 public static final int FAST_SCROLL_UNHIGHLIGHT_FROM_NORMAL_DURATION = 225;
88 public static final int FAST_SCROLL_INACTIVE_DURATION = 275;
Sunny Goyal508da152014-08-14 10:53:27 -070089
Winsonc0880492015-08-21 11:16:27 -070090 // Since we don't need 256^2 values for combinations of both the brightness and saturation, we
91 // reduce the value space to a smaller value V, which reduces the number of cached
92 // ColorMatrixColorFilters that we need to keep to V^2
93 private static final int REDUCED_FILTER_VALUE_SPACE = 48;
Sunny Goyal95abbb32014-08-04 10:53:22 -070094
Winsonc0880492015-08-21 11:16:27 -070095 // A cache of ColorFilters for optimizing brightness and saturation animations
96 private static final SparseArray<ColorFilter> sCachedFilter = new SparseArray<>();
Sunny Goyal508da152014-08-14 10:53:27 -070097
Winsonc0880492015-08-21 11:16:27 -070098 // Temporary matrices used for calculation
99 private static final ColorMatrix sTempBrightnessMatrix = new ColorMatrix();
100 private static final ColorMatrix sTempFilterMatrix = new ColorMatrix();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700101
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800102 protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Sunny Goyal508da152014-08-14 10:53:27 -0700103 private final Bitmap mBitmap;
Winsonc0880492015-08-21 11:16:27 -0700104 private State mState = State.NORMAL;
Tony Wickham6b910a22016-11-08 10:40:34 -0800105 private boolean mIsDisabled;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700106
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800107 private BadgeInfo mBadgeInfo;
108 private BadgeRenderer mBadgeRenderer;
109 private IconPalette mIconPalette;
110
Winsonc0880492015-08-21 11:16:27 -0700111 // The saturation and brightness are values that are mapped to REDUCED_FILTER_VALUE_SPACE and
112 // as a result, can be used to compose the key for the cached ColorMatrixColorFilters
113 private int mDesaturation = 0;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700114 private int mBrightness = 0;
Winsonc0880492015-08-21 11:16:27 -0700115 private int mAlpha = 255;
116 private int mPrevUpdateKey = Integer.MAX_VALUE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
Winsonc0880492015-08-21 11:16:27 -0700118 // Animators for the fast bitmap drawable's properties
119 private AnimatorSet mPropertyAnimator;
Sunny Goyal508da152014-08-14 10:53:27 -0700120
Hyunyoung Song3f471442015-04-08 19:01:34 -0700121 public FastBitmapDrawable(Bitmap b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 mBitmap = b;
Winson Chung268f1c52013-11-18 14:04:41 -0800123 setBounds(0, 0, b.getWidth(), b.getHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 }
125
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800126 public void applyIconBadge(BadgeInfo badgeInfo, BadgeRenderer badgeRenderer) {
Tony Wickham010d2552017-01-20 08:15:28 -0800127 boolean wasBadged = mBadgeInfo != null;
128 boolean isBadged = badgeInfo != null;
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800129 mBadgeInfo = badgeInfo;
130 mBadgeRenderer = badgeRenderer;
Tony Wickham010d2552017-01-20 08:15:28 -0800131 if (wasBadged || isBadged) {
132 if (mBadgeInfo != null && mIconPalette == null) {
133 mIconPalette = IconPalette.fromDominantColor(Utilities
134 .findDominantColorByHue(mBitmap, 20));
135 }
136 invalidateSelf();
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800137 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800138 }
139
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 @Override
141 public void draw(Canvas canvas) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800142 drawInternal(canvas);
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800143 // Draw the icon badge in the top right corner.
144 drawBadgeIfNecessary(canvas);
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800145 }
146
147 public void drawWithBrightness(Canvas canvas, float brightness) {
148 float oldBrightness = getBrightness();
149 setBrightness(brightness);
150 drawInternal(canvas);
151 setBrightness(oldBrightness);
152 }
153
154 protected void drawInternal(Canvas canvas) {
Winsonc0880492015-08-21 11:16:27 -0700155 canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 }
157
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800158 protected void drawBadgeIfNecessary(Canvas canvas) {
159 if (hasBadge()) {
160 mBadgeRenderer.draw(canvas, mIconPalette, mBadgeInfo, getBounds());
161 }
162 }
163
164 private boolean hasBadge() {
Tony Wickham010d2552017-01-20 08:15:28 -0800165 return mBadgeInfo != null && mBadgeInfo.getNotificationCount() != 0;
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800166 }
167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700169 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700170 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -0700171 }
172
173 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 public int getOpacity() {
175 return PixelFormat.TRANSLUCENT;
176 }
177
178 @Override
179 public void setAlpha(int alpha) {
Winson Chung29d6fea2010-12-01 15:47:31 -0800180 mAlpha = alpha;
Winson Chungb3347bb2010-08-19 14:51:28 -0700181 mPaint.setAlpha(alpha);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 }
183
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700184 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700185 public void setFilterBitmap(boolean filterBitmap) {
186 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700187 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700188 }
189
Winson Chung29d6fea2010-12-01 15:47:31 -0800190 public int getAlpha() {
191 return mAlpha;
192 }
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700196 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 }
198
199 @Override
200 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700201 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 }
203
204 @Override
205 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800206 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 }
208
209 @Override
210 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800211 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800212 }
213
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 public Bitmap getBitmap() {
215 return mBitmap;
216 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700217
Sunny Goyal95abbb32014-08-04 10:53:22 -0700218 /**
Winsonc0880492015-08-21 11:16:27 -0700219 * Animates this drawable to a new state.
220 *
221 * @return whether the state has changed.
Sunny Goyal95abbb32014-08-04 10:53:22 -0700222 */
Winsonc0880492015-08-21 11:16:27 -0700223 public boolean animateState(State newState) {
224 State prevState = mState;
225 if (mState != newState) {
226 mState = newState;
227
Tony Wickham6b910a22016-11-08 10:40:34 -0800228 float desaturation = mIsDisabled ? DISABLED_DESATURATION : newState.desaturation;
229 float brightness = mIsDisabled ? DISABLED_BRIGHTNESS: newState.brightness;
230
Winsonc0880492015-08-21 11:16:27 -0700231 mPropertyAnimator = cancelAnimator(mPropertyAnimator);
232 mPropertyAnimator = new AnimatorSet();
233 mPropertyAnimator.playTogether(
Tony Wickham6b910a22016-11-08 10:40:34 -0800234 ObjectAnimator.ofFloat(this, "desaturation", desaturation),
235 ObjectAnimator.ofFloat(this, "brightness", brightness));
Winsonc0880492015-08-21 11:16:27 -0700236 mPropertyAnimator.setInterpolator(newState.interpolator);
237 mPropertyAnimator.setDuration(getDurationForStateChange(prevState, newState));
238 mPropertyAnimator.setStartDelay(getStartDelayForStateChange(prevState, newState));
239 mPropertyAnimator.start();
240 return true;
241 }
242 return false;
243 }
244
245 /**
246 * Immediately sets this drawable to a new state.
247 *
248 * @return whether the state has changed.
249 */
250 public boolean setState(State newState) {
251 if (mState != newState) {
252 mState = newState;
253
254 mPropertyAnimator = cancelAnimator(mPropertyAnimator);
255
Tony Wickham6b910a22016-11-08 10:40:34 -0800256 invalidateDesaturationAndBrightness();
Winsonc0880492015-08-21 11:16:27 -0700257 return true;
258 }
259 return false;
260 }
261
Tony Wickham6b910a22016-11-08 10:40:34 -0800262 private void invalidateDesaturationAndBrightness() {
263 setDesaturation(mIsDisabled ? DISABLED_DESATURATION : mState.desaturation);
264 setBrightness(mIsDisabled ? DISABLED_BRIGHTNESS: mState.brightness);
265 }
266
Winsonc0880492015-08-21 11:16:27 -0700267 /**
268 * Returns the current state.
269 */
270 public State getCurrentState() {
271 return mState;
272 }
273
Tony Wickham6b910a22016-11-08 10:40:34 -0800274 public void setIsDisabled(boolean isDisabled) {
275 if (mIsDisabled != isDisabled) {
276 mIsDisabled = isDisabled;
277 invalidateDesaturationAndBrightness();
278 }
279 }
280
Winsonc0880492015-08-21 11:16:27 -0700281 /**
282 * Returns the duration for the state change animation.
283 */
284 public static int getDurationForStateChange(State fromState, State toState) {
285 switch (toState) {
286 case NORMAL:
287 switch (fromState) {
288 case PRESSED:
289 return 0;
290 case FAST_SCROLL_HIGHLIGHTED:
291 case FAST_SCROLL_UNHIGHLIGHTED:
292 return FAST_SCROLL_INACTIVE_DURATION;
293 }
294 case PRESSED:
295 return CLICK_FEEDBACK_DURATION;
296 case FAST_SCROLL_HIGHLIGHTED:
297 return FAST_SCROLL_HIGHLIGHT_DURATION;
298 case FAST_SCROLL_UNHIGHLIGHTED:
299 switch (fromState) {
300 case NORMAL:
301 // When animating from normal state, take a little longer
302 return FAST_SCROLL_UNHIGHLIGHT_FROM_NORMAL_DURATION;
303 default:
304 return FAST_SCROLL_UNHIGHLIGHT_DURATION;
305 }
306 }
307 return 0;
308 }
309
310 /**
311 * Returns the start delay when animating between certain fast scroll states.
312 */
313 public static int getStartDelayForStateChange(State fromState, State toState) {
314 switch (toState) {
315 case FAST_SCROLL_UNHIGHLIGHTED:
316 switch (fromState) {
317 case NORMAL:
318 return FAST_SCROLL_UNHIGHLIGHT_DURATION / 4;
319 }
320 }
321 return 0;
322 }
323
324 /**
325 * Sets the saturation of this icon, 0 [full color] -> 1 [desaturated]
326 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800327 private void setDesaturation(float desaturation) {
Winsonc0880492015-08-21 11:16:27 -0700328 int newDesaturation = (int) Math.floor(desaturation * REDUCED_FILTER_VALUE_SPACE);
329 if (mDesaturation != newDesaturation) {
330 mDesaturation = newDesaturation;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700331 updateFilter();
332 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700333 }
334
Winsonc0880492015-08-21 11:16:27 -0700335 public float getDesaturation() {
336 return (float) mDesaturation / REDUCED_FILTER_VALUE_SPACE;
Sunny Goyal508da152014-08-14 10:53:27 -0700337 }
338
Winsonc0880492015-08-21 11:16:27 -0700339 /**
340 * Sets the brightness of this icon, 0 [no add. brightness] -> 1 [2bright2furious]
341 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800342 private void setBrightness(float brightness) {
Winsonc0880492015-08-21 11:16:27 -0700343 int newBrightness = (int) Math.floor(brightness * REDUCED_FILTER_VALUE_SPACE);
344 if (mBrightness != newBrightness) {
345 mBrightness = newBrightness;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700346 updateFilter();
347 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700348 }
349
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800350 private float getBrightness() {
Winsonc0880492015-08-21 11:16:27 -0700351 return (float) mBrightness / REDUCED_FILTER_VALUE_SPACE;
352 }
353
354 /**
355 * Updates the paint to reflect the current brightness and saturation.
356 */
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700357 private void updateFilter() {
Winsonc0880492015-08-21 11:16:27 -0700358 boolean usePorterDuffFilter = false;
359 int key = -1;
360 if (mDesaturation > 0) {
361 key = (mDesaturation << 16) | mBrightness;
362 } else if (mBrightness > 0) {
363 // Compose a key with a fully saturated icon if we are just animating brightness
364 key = (1 << 16) | mBrightness;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700365
Winsonc0880492015-08-21 11:16:27 -0700366 // We found that in L, ColorFilters cause drawing artifacts with shadows baked into
367 // icons, so just use a PorterDuff filter when we aren't animating saturation
368 usePorterDuffFilter = true;
369 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700370
Winsonc0880492015-08-21 11:16:27 -0700371 // Debounce multiple updates on the same frame
372 if (key == mPrevUpdateKey) {
373 return;
374 }
375 mPrevUpdateKey = key;
376
377 if (key != -1) {
378 ColorFilter filter = sCachedFilter.get(key);
Sunny Goyal508da152014-08-14 10:53:27 -0700379 if (filter == null) {
Winsonc0880492015-08-21 11:16:27 -0700380 float brightnessF = getBrightness();
381 int brightnessI = (int) (255 * brightnessF);
382 if (usePorterDuffFilter) {
383 filter = new PorterDuffColorFilter(Color.argb(brightnessI, 255, 255, 255),
384 PorterDuff.Mode.SRC_ATOP);
385 } else {
386 float saturationF = 1f - getDesaturation();
387 sTempFilterMatrix.setSaturation(saturationF);
388 if (mBrightness > 0) {
389 // Brightness: C-new = C-old*(1-amount) + amount
390 float scale = 1f - brightnessF;
391 float[] mat = sTempBrightnessMatrix.getArray();
392 mat[0] = scale;
393 mat[6] = scale;
394 mat[12] = scale;
395 mat[4] = brightnessI;
396 mat[9] = brightnessI;
397 mat[14] = brightnessI;
398 sTempFilterMatrix.preConcat(sTempBrightnessMatrix);
399 }
400 filter = new ColorMatrixColorFilter(sTempFilterMatrix);
401 }
402 sCachedFilter.append(key, filter);
Sunny Goyal508da152014-08-14 10:53:27 -0700403 }
404 mPaint.setColorFilter(filter);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700405 } else {
406 mPaint.setColorFilter(null);
407 }
Winsonc0880492015-08-21 11:16:27 -0700408 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700409 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700410
Winsonc0880492015-08-21 11:16:27 -0700411 private AnimatorSet cancelAnimator(AnimatorSet animator) {
412 if (animator != null) {
Winsonc0880492015-08-21 11:16:27 -0700413 animator.cancel();
414 }
415 return null;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700416 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417}