blob: b3e59f99bfa11020321c450b801a341d93e23b1b [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;
Sunny Goyal508da152014-08-14 10:53:27 -070033import android.util.SparseArray;
Winsonc0880492015-08-21 11:16:27 -070034import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035
Tony Wickham9a8d11f2017-01-11 09:53:12 -080036import com.android.launcher3.graphics.IconPalette;
37import com.android.launcher3.badge.BadgeRenderer;
38import com.android.launcher3.badge.BadgeInfo;
39
Hyunyoung Song3f471442015-04-08 19:01:34 -070040public class FastBitmapDrawable extends Drawable {
Tony Wickham6b910a22016-11-08 10:40:34 -080041 private static final float DISABLED_DESATURATION = 1f;
42 private static final float DISABLED_BRIGHTNESS = 0.5f;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070043
Winsonc0880492015-08-21 11:16:27 -070044 /**
45 * The possible states that a FastBitmapDrawable can be in.
46 */
47 public enum State {
48
49 NORMAL (0f, 0f, 1f, new DecelerateInterpolator()),
50 PRESSED (0f, 100f / 255f, 1f, CLICK_FEEDBACK_INTERPOLATOR),
Winsonc08c59d2015-10-28 15:30:38 -070051 FAST_SCROLL_HIGHLIGHTED (0f, 0f, 1.15f, new DecelerateInterpolator()),
Tony Wickham6b910a22016-11-08 10:40:34 -080052 FAST_SCROLL_UNHIGHLIGHTED (0f, 0f, 1f, new DecelerateInterpolator());
Winsonc0880492015-08-21 11:16:27 -070053
54 public final float desaturation;
55 public final float brightness;
56 /**
57 * Used specifically by the view drawing this FastBitmapDrawable.
58 */
59 public final float viewScale;
60 public final TimeInterpolator interpolator;
61
62 State(float desaturation, float brightness, float viewScale, TimeInterpolator interpolator) {
63 this.desaturation = desaturation;
64 this.brightness = brightness;
65 this.viewScale = viewScale;
66 this.interpolator = interpolator;
67 }
68 }
69
70 public static final TimeInterpolator CLICK_FEEDBACK_INTERPOLATOR = new TimeInterpolator() {
Sunny Goyal508da152014-08-14 10:53:27 -070071
72 @Override
73 public float getInterpolation(float input) {
74 if (input < 0.05f) {
75 return input / 0.05f;
76 } else if (input < 0.3f){
77 return 1;
78 } else {
79 return (1 - input) / 0.7f;
80 }
81 }
82 };
Winsonc0880492015-08-21 11:16:27 -070083 public static final int CLICK_FEEDBACK_DURATION = 2000;
84 public static final int FAST_SCROLL_HIGHLIGHT_DURATION = 225;
85 public static final int FAST_SCROLL_UNHIGHLIGHT_DURATION = 150;
86 public static final int FAST_SCROLL_UNHIGHLIGHT_FROM_NORMAL_DURATION = 225;
87 public static final int FAST_SCROLL_INACTIVE_DURATION = 275;
Sunny Goyal508da152014-08-14 10:53:27 -070088
Winsonc0880492015-08-21 11:16:27 -070089 // Since we don't need 256^2 values for combinations of both the brightness and saturation, we
90 // reduce the value space to a smaller value V, which reduces the number of cached
91 // ColorMatrixColorFilters that we need to keep to V^2
92 private static final int REDUCED_FILTER_VALUE_SPACE = 48;
Sunny Goyal95abbb32014-08-04 10:53:22 -070093
Winsonc0880492015-08-21 11:16:27 -070094 // A cache of ColorFilters for optimizing brightness and saturation animations
95 private static final SparseArray<ColorFilter> sCachedFilter = new SparseArray<>();
Sunny Goyal508da152014-08-14 10:53:27 -070096
Winsonc0880492015-08-21 11:16:27 -070097 // Temporary matrices used for calculation
98 private static final ColorMatrix sTempBrightnessMatrix = new ColorMatrix();
99 private static final ColorMatrix sTempFilterMatrix = new ColorMatrix();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700100
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800101 protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Sunny Goyal508da152014-08-14 10:53:27 -0700102 private final Bitmap mBitmap;
Winsonc0880492015-08-21 11:16:27 -0700103 private State mState = State.NORMAL;
Tony Wickham6b910a22016-11-08 10:40:34 -0800104 private boolean mIsDisabled;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700105
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800106 private BadgeInfo mBadgeInfo;
107 private BadgeRenderer mBadgeRenderer;
108 private IconPalette mIconPalette;
109
Winsonc0880492015-08-21 11:16:27 -0700110 // The saturation and brightness are values that are mapped to REDUCED_FILTER_VALUE_SPACE and
111 // as a result, can be used to compose the key for the cached ColorMatrixColorFilters
112 private int mDesaturation = 0;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700113 private int mBrightness = 0;
Winsonc0880492015-08-21 11:16:27 -0700114 private int mAlpha = 255;
115 private int mPrevUpdateKey = Integer.MAX_VALUE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116
Winsonc0880492015-08-21 11:16:27 -0700117 // Animators for the fast bitmap drawable's properties
118 private AnimatorSet mPropertyAnimator;
Sunny Goyal508da152014-08-14 10:53:27 -0700119
Hyunyoung Song3f471442015-04-08 19:01:34 -0700120 public FastBitmapDrawable(Bitmap b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 mBitmap = b;
Winson Chung268f1c52013-11-18 14:04:41 -0800122 setBounds(0, 0, b.getWidth(), b.getHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 }
124
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800125 public void applyIconBadge(BadgeInfo badgeInfo, BadgeRenderer badgeRenderer) {
126 mBadgeInfo = badgeInfo;
127 mBadgeRenderer = badgeRenderer;
128 if (mIconPalette == null) {
129 mIconPalette = IconPalette.fromDominantColor(Utilities
130 .findDominantColorByHue(mBitmap, 20));
131 }
132 invalidateSelf();
133 }
134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 @Override
136 public void draw(Canvas canvas) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800137 drawInternal(canvas);
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800138 // Draw the icon badge in the top right corner.
139 drawBadgeIfNecessary(canvas);
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800140 }
141
142 public void drawWithBrightness(Canvas canvas, float brightness) {
143 float oldBrightness = getBrightness();
144 setBrightness(brightness);
145 drawInternal(canvas);
146 setBrightness(oldBrightness);
147 }
148
149 protected void drawInternal(Canvas canvas) {
Winsonc0880492015-08-21 11:16:27 -0700150 canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 }
152
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800153 protected void drawBadgeIfNecessary(Canvas canvas) {
154 if (hasBadge()) {
155 mBadgeRenderer.draw(canvas, mIconPalette, mBadgeInfo, getBounds());
156 }
157 }
158
159 private boolean hasBadge() {
160 return mBadgeInfo != null && mBadgeInfo.getNotificationCount() != null;
161 }
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700164 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700165 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -0700166 }
167
168 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 public int getOpacity() {
170 return PixelFormat.TRANSLUCENT;
171 }
172
173 @Override
174 public void setAlpha(int alpha) {
Winson Chung29d6fea2010-12-01 15:47:31 -0800175 mAlpha = alpha;
Winson Chungb3347bb2010-08-19 14:51:28 -0700176 mPaint.setAlpha(alpha);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 }
178
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700179 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700180 public void setFilterBitmap(boolean filterBitmap) {
181 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700182 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700183 }
184
Winson Chung29d6fea2010-12-01 15:47:31 -0800185 public int getAlpha() {
186 return mAlpha;
187 }
188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700191 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 }
193
194 @Override
195 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700196 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 }
198
199 @Override
200 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800201 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 }
203
204 @Override
205 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800206 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800207 }
208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 public Bitmap getBitmap() {
210 return mBitmap;
211 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700212
Sunny Goyal95abbb32014-08-04 10:53:22 -0700213 /**
Winsonc0880492015-08-21 11:16:27 -0700214 * Animates this drawable to a new state.
215 *
216 * @return whether the state has changed.
Sunny Goyal95abbb32014-08-04 10:53:22 -0700217 */
Winsonc0880492015-08-21 11:16:27 -0700218 public boolean animateState(State newState) {
219 State prevState = mState;
220 if (mState != newState) {
221 mState = newState;
222
Tony Wickham6b910a22016-11-08 10:40:34 -0800223 float desaturation = mIsDisabled ? DISABLED_DESATURATION : newState.desaturation;
224 float brightness = mIsDisabled ? DISABLED_BRIGHTNESS: newState.brightness;
225
Winsonc0880492015-08-21 11:16:27 -0700226 mPropertyAnimator = cancelAnimator(mPropertyAnimator);
227 mPropertyAnimator = new AnimatorSet();
228 mPropertyAnimator.playTogether(
Tony Wickham6b910a22016-11-08 10:40:34 -0800229 ObjectAnimator.ofFloat(this, "desaturation", desaturation),
230 ObjectAnimator.ofFloat(this, "brightness", brightness));
Winsonc0880492015-08-21 11:16:27 -0700231 mPropertyAnimator.setInterpolator(newState.interpolator);
232 mPropertyAnimator.setDuration(getDurationForStateChange(prevState, newState));
233 mPropertyAnimator.setStartDelay(getStartDelayForStateChange(prevState, newState));
234 mPropertyAnimator.start();
235 return true;
236 }
237 return false;
238 }
239
240 /**
241 * Immediately sets this drawable to a new state.
242 *
243 * @return whether the state has changed.
244 */
245 public boolean setState(State newState) {
246 if (mState != newState) {
247 mState = newState;
248
249 mPropertyAnimator = cancelAnimator(mPropertyAnimator);
250
Tony Wickham6b910a22016-11-08 10:40:34 -0800251 invalidateDesaturationAndBrightness();
Winsonc0880492015-08-21 11:16:27 -0700252 return true;
253 }
254 return false;
255 }
256
Tony Wickham6b910a22016-11-08 10:40:34 -0800257 private void invalidateDesaturationAndBrightness() {
258 setDesaturation(mIsDisabled ? DISABLED_DESATURATION : mState.desaturation);
259 setBrightness(mIsDisabled ? DISABLED_BRIGHTNESS: mState.brightness);
260 }
261
Winsonc0880492015-08-21 11:16:27 -0700262 /**
263 * Returns the current state.
264 */
265 public State getCurrentState() {
266 return mState;
267 }
268
Tony Wickham6b910a22016-11-08 10:40:34 -0800269 public void setIsDisabled(boolean isDisabled) {
270 if (mIsDisabled != isDisabled) {
271 mIsDisabled = isDisabled;
272 invalidateDesaturationAndBrightness();
273 }
274 }
275
Winsonc0880492015-08-21 11:16:27 -0700276 /**
277 * Returns the duration for the state change animation.
278 */
279 public static int getDurationForStateChange(State fromState, State toState) {
280 switch (toState) {
281 case NORMAL:
282 switch (fromState) {
283 case PRESSED:
284 return 0;
285 case FAST_SCROLL_HIGHLIGHTED:
286 case FAST_SCROLL_UNHIGHLIGHTED:
287 return FAST_SCROLL_INACTIVE_DURATION;
288 }
289 case PRESSED:
290 return CLICK_FEEDBACK_DURATION;
291 case FAST_SCROLL_HIGHLIGHTED:
292 return FAST_SCROLL_HIGHLIGHT_DURATION;
293 case FAST_SCROLL_UNHIGHLIGHTED:
294 switch (fromState) {
295 case NORMAL:
296 // When animating from normal state, take a little longer
297 return FAST_SCROLL_UNHIGHLIGHT_FROM_NORMAL_DURATION;
298 default:
299 return FAST_SCROLL_UNHIGHLIGHT_DURATION;
300 }
301 }
302 return 0;
303 }
304
305 /**
306 * Returns the start delay when animating between certain fast scroll states.
307 */
308 public static int getStartDelayForStateChange(State fromState, State toState) {
309 switch (toState) {
310 case FAST_SCROLL_UNHIGHLIGHTED:
311 switch (fromState) {
312 case NORMAL:
313 return FAST_SCROLL_UNHIGHLIGHT_DURATION / 4;
314 }
315 }
316 return 0;
317 }
318
319 /**
320 * Sets the saturation of this icon, 0 [full color] -> 1 [desaturated]
321 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800322 private void setDesaturation(float desaturation) {
Winsonc0880492015-08-21 11:16:27 -0700323 int newDesaturation = (int) Math.floor(desaturation * REDUCED_FILTER_VALUE_SPACE);
324 if (mDesaturation != newDesaturation) {
325 mDesaturation = newDesaturation;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700326 updateFilter();
327 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700328 }
329
Winsonc0880492015-08-21 11:16:27 -0700330 public float getDesaturation() {
331 return (float) mDesaturation / REDUCED_FILTER_VALUE_SPACE;
Sunny Goyal508da152014-08-14 10:53:27 -0700332 }
333
Winsonc0880492015-08-21 11:16:27 -0700334 /**
335 * Sets the brightness of this icon, 0 [no add. brightness] -> 1 [2bright2furious]
336 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800337 private void setBrightness(float brightness) {
Winsonc0880492015-08-21 11:16:27 -0700338 int newBrightness = (int) Math.floor(brightness * REDUCED_FILTER_VALUE_SPACE);
339 if (mBrightness != newBrightness) {
340 mBrightness = newBrightness;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700341 updateFilter();
342 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700343 }
344
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800345 private float getBrightness() {
Winsonc0880492015-08-21 11:16:27 -0700346 return (float) mBrightness / REDUCED_FILTER_VALUE_SPACE;
347 }
348
349 /**
350 * Updates the paint to reflect the current brightness and saturation.
351 */
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700352 private void updateFilter() {
Winsonc0880492015-08-21 11:16:27 -0700353 boolean usePorterDuffFilter = false;
354 int key = -1;
355 if (mDesaturation > 0) {
356 key = (mDesaturation << 16) | mBrightness;
357 } else if (mBrightness > 0) {
358 // Compose a key with a fully saturated icon if we are just animating brightness
359 key = (1 << 16) | mBrightness;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700360
Winsonc0880492015-08-21 11:16:27 -0700361 // We found that in L, ColorFilters cause drawing artifacts with shadows baked into
362 // icons, so just use a PorterDuff filter when we aren't animating saturation
363 usePorterDuffFilter = true;
364 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700365
Winsonc0880492015-08-21 11:16:27 -0700366 // Debounce multiple updates on the same frame
367 if (key == mPrevUpdateKey) {
368 return;
369 }
370 mPrevUpdateKey = key;
371
372 if (key != -1) {
373 ColorFilter filter = sCachedFilter.get(key);
Sunny Goyal508da152014-08-14 10:53:27 -0700374 if (filter == null) {
Winsonc0880492015-08-21 11:16:27 -0700375 float brightnessF = getBrightness();
376 int brightnessI = (int) (255 * brightnessF);
377 if (usePorterDuffFilter) {
378 filter = new PorterDuffColorFilter(Color.argb(brightnessI, 255, 255, 255),
379 PorterDuff.Mode.SRC_ATOP);
380 } else {
381 float saturationF = 1f - getDesaturation();
382 sTempFilterMatrix.setSaturation(saturationF);
383 if (mBrightness > 0) {
384 // Brightness: C-new = C-old*(1-amount) + amount
385 float scale = 1f - brightnessF;
386 float[] mat = sTempBrightnessMatrix.getArray();
387 mat[0] = scale;
388 mat[6] = scale;
389 mat[12] = scale;
390 mat[4] = brightnessI;
391 mat[9] = brightnessI;
392 mat[14] = brightnessI;
393 sTempFilterMatrix.preConcat(sTempBrightnessMatrix);
394 }
395 filter = new ColorMatrixColorFilter(sTempFilterMatrix);
396 }
397 sCachedFilter.append(key, filter);
Sunny Goyal508da152014-08-14 10:53:27 -0700398 }
399 mPaint.setColorFilter(filter);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700400 } else {
401 mPaint.setColorFilter(null);
402 }
Winsonc0880492015-08-21 11:16:27 -0700403 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700404 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700405
Winsonc0880492015-08-21 11:16:27 -0700406 private AnimatorSet cancelAnimator(AnimatorSet animator) {
407 if (animator != null) {
Winsonc0880492015-08-21 11:16:27 -0700408 animator.cancel();
409 }
410 return null;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700411 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412}