summaryrefslogtreecommitdiff
path: root/iconloaderlib/src/com/android/launcher3/icons/FastBitmapDrawable.java
blob: 0ee9db971e6b0b228900ef420a9238b4c211ee32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.launcher3.icons;

import static com.android.launcher3.icons.BaseIconFactory.getBadgeSizeForIconSize;
import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;

import android.animation.ObjectAnimator;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.FloatProperty;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.PathInterpolator;

import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.core.graphics.ColorUtils;

public class FastBitmapDrawable extends Drawable implements Drawable.Callback {

    private static final Interpolator ACCEL = new AccelerateInterpolator();
    private static final Interpolator DEACCEL = new DecelerateInterpolator();
    private static final Interpolator HOVER_EMPHASIZED_DECELERATE_INTERPOLATOR =
            new PathInterpolator(0.05f, 0.7f, 0.1f, 1.0f);

    @VisibleForTesting protected static final float PRESSED_SCALE = 1.1f;
    @VisibleForTesting protected static final float HOVERED_SCALE = 1.1f;
    public static final int WHITE_SCRIM_ALPHA = 138;

    private static final float DISABLED_DESATURATION = 1f;
    private static final float DISABLED_BRIGHTNESS = 0.5f;
    protected static final int FULLY_OPAQUE = 255;

    public static final int CLICK_FEEDBACK_DURATION = 200;
    public static final int HOVER_FEEDBACK_DURATION = 300;

    private static boolean sFlagHoverEnabled = false;

    protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
    protected final Bitmap mBitmap;
    protected final int mIconColor;

    @Nullable private ColorFilter mColorFilter;

    @VisibleForTesting protected boolean mIsPressed;
    @VisibleForTesting protected boolean mIsHovered;
    protected boolean mIsDisabled;
    float mDisabledAlpha = 1f;

    // Animator and properties for the fast bitmap drawable's scale
    @VisibleForTesting protected static final FloatProperty<FastBitmapDrawable> SCALE
            = new FloatProperty<FastBitmapDrawable>("scale") {
        @Override
        public Float get(FastBitmapDrawable fastBitmapDrawable) {
            return fastBitmapDrawable.mScale;
        }

        @Override
        public void setValue(FastBitmapDrawable fastBitmapDrawable, float value) {
            fastBitmapDrawable.mScale = value;
            fastBitmapDrawable.invalidateSelf();
        }
    };
    @VisibleForTesting protected ObjectAnimator mScaleAnimation;
    private float mScale = 1;
    private int mAlpha = 255;

    private Drawable mBadge;

    public FastBitmapDrawable(Bitmap b) {
        this(b, Color.TRANSPARENT);
    }

    public FastBitmapDrawable(BitmapInfo info) {
        this(info.icon, info.color);
    }

    protected FastBitmapDrawable(Bitmap b, int iconColor) {
        mBitmap = b;
        mIconColor = iconColor;
        setFilterBitmap(true);
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        updateBadgeBounds(bounds);
    }

    private void updateBadgeBounds(Rect bounds) {
        if (mBadge != null) {
            setBadgeBounds(mBadge, bounds);
        }
    }

    @Override
    public final void draw(Canvas canvas) {
        if (mScale != 1f) {
            int count = canvas.save();
            Rect bounds = getBounds();
            canvas.scale(mScale, mScale, bounds.exactCenterX(), bounds.exactCenterY());
            drawInternal(canvas, bounds);
            if (mBadge != null) {
                mBadge.draw(canvas);
            }
            canvas.restoreToCount(count);
        } else {
            drawInternal(canvas, getBounds());
            if (mBadge != null) {
                mBadge.draw(canvas);
            }
        }
    }

    protected void drawInternal(Canvas canvas, Rect bounds) {
        canvas.drawBitmap(mBitmap, null, bounds, mPaint);
    }

    /**
     * Returns the primary icon color, slightly tinted white
     */
    public int getIconColor() {
        int whiteScrim = setColorAlphaBound(Color.WHITE, WHITE_SCRIM_ALPHA);
        return ColorUtils.compositeColors(whiteScrim, mIconColor);
    }

    /**
     * Returns if this represents a themed icon
     */
    public boolean isThemed() {
        return false;
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        mColorFilter = cf;
        updateFilter();
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public void setAlpha(int alpha) {
        if (mAlpha != alpha) {
            mAlpha = alpha;
            mPaint.setAlpha(alpha);
            invalidateSelf();
            if (mBadge != null) {
                mBadge.setAlpha(alpha);
            }
        }
    }

    @Override
    public void setFilterBitmap(boolean filterBitmap) {
        mPaint.setFilterBitmap(filterBitmap);
        mPaint.setAntiAlias(filterBitmap);
    }

    @Override
    public int getAlpha() {
        return mAlpha;
    }

    public void resetScale() {
        if (mScaleAnimation != null) {
            mScaleAnimation.cancel();
            mScaleAnimation = null;
        }
        mScale = 1;
        invalidateSelf();
    }

    public float getAnimatedScale() {
        return mScaleAnimation == null ? 1 : mScale;
    }

    @Override
    public int getIntrinsicWidth() {
        return mBitmap.getWidth();
    }

    @Override
    public int getIntrinsicHeight() {
        return mBitmap.getHeight();
    }

    @Override
    public int getMinimumWidth() {
        return getBounds().width();
    }

    @Override
    public int getMinimumHeight() {
        return getBounds().height();
    }

    @Override
    public boolean isStateful() {
        return true;
    }

    @Override
    public ColorFilter getColorFilter() {
        return mPaint.getColorFilter();
    }

    @Override
    protected boolean onStateChange(int[] state) {
        boolean isPressed = false;
        boolean isHovered = false;
        for (int s : state) {
            if (s == android.R.attr.state_pressed) {
                isPressed = true;
                break;
            } else if (sFlagHoverEnabled && s == android.R.attr.state_hovered) {
                isHovered = true;
                // Do not break on hovered state, as pressed state should take precedence.
            }
        }
        if (mIsPressed != isPressed || mIsHovered != isHovered) {
            if (mScaleAnimation != null) {
                mScaleAnimation.cancel();
            }

            float endScale = isPressed ? PRESSED_SCALE : (isHovered ? HOVERED_SCALE : 1f);
            if (mScale != endScale) {
                if (isVisible()) {
                    Interpolator interpolator =
                            isPressed != mIsPressed ? (isPressed ? ACCEL : DEACCEL)
                                    : HOVER_EMPHASIZED_DECELERATE_INTERPOLATOR;
                    int duration =
                            isPressed != mIsPressed ? CLICK_FEEDBACK_DURATION
                                    : HOVER_FEEDBACK_DURATION;
                    mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, endScale);
                    mScaleAnimation.setDuration(duration);
                    mScaleAnimation.setInterpolator(interpolator);
                    mScaleAnimation.start();
                } else {
                    mScale = endScale;
                    invalidateSelf();
                }
            }
            mIsPressed = isPressed;
            mIsHovered = isHovered;
            return true;
        }
        return false;
    }

    public void setIsDisabled(boolean isDisabled) {
        if (mIsDisabled != isDisabled) {
            mIsDisabled = isDisabled;
            updateFilter();
        }
    }

    protected boolean isDisabled() {
        return mIsDisabled;
    }

    public void setBadge(Drawable badge) {
        if (mBadge != null) {
            mBadge.setCallback(null);
        }
        mBadge = badge;
        if (mBadge != null) {
            mBadge.setCallback(this);
        }
        updateBadgeBounds(getBounds());
        updateFilter();
    }

    /**
     * Updates the paint to reflect the current brightness and saturation.
     */
    protected void updateFilter() {
        mPaint.setColorFilter(mIsDisabled ? getDisabledColorFilter(mDisabledAlpha) : mColorFilter);
        if (mBadge != null) {
            mBadge.setColorFilter(getColorFilter());
        }
        invalidateSelf();
    }

    protected FastBitmapConstantState newConstantState() {
        return new FastBitmapConstantState(mBitmap, mIconColor);
    }

    @Override
    public final ConstantState getConstantState() {
        FastBitmapConstantState cs = newConstantState();
        cs.mIsDisabled = mIsDisabled;
        if (mBadge != null) {
            cs.mBadgeConstantState = mBadge.getConstantState();
        }
        return cs;
    }

    public static ColorFilter getDisabledColorFilter() {
        return getDisabledColorFilter(1);
    }

    private static ColorFilter getDisabledColorFilter(float disabledAlpha) {
        ColorMatrix tempBrightnessMatrix = new ColorMatrix();
        ColorMatrix tempFilterMatrix = new ColorMatrix();

        tempFilterMatrix.setSaturation(1f - DISABLED_DESATURATION);
        float scale = 1 - DISABLED_BRIGHTNESS;
        int brightnessI =   (int) (255 * DISABLED_BRIGHTNESS);
        float[] mat = tempBrightnessMatrix.getArray();
        mat[0] = scale;
        mat[6] = scale;
        mat[12] = scale;
        mat[4] = brightnessI;
        mat[9] = brightnessI;
        mat[14] = brightnessI;
        mat[18] = disabledAlpha;
        tempFilterMatrix.preConcat(tempBrightnessMatrix);
        return new ColorMatrixColorFilter(tempFilterMatrix);
    }

    protected static final int getDisabledColor(int color) {
        int component = (Color.red(color) + Color.green(color) + Color.blue(color)) / 3;
        float scale = 1 - DISABLED_BRIGHTNESS;
        int brightnessI = (int) (255 * DISABLED_BRIGHTNESS);
        component = Math.min(Math.round(scale * component + brightnessI), FULLY_OPAQUE);
        return Color.rgb(component, component, component);
    }

    /**
     * Sets the bounds for the badge drawable based on the main icon bounds
     */
    public static void setBadgeBounds(Drawable badge, Rect iconBounds) {
        int size = getBadgeSizeForIconSize(iconBounds.width());
        badge.setBounds(iconBounds.right - size, iconBounds.bottom - size,
                iconBounds.right, iconBounds.bottom);
    }

    @Override
    public void invalidateDrawable(Drawable who) {
        if (who == mBadge) {
            invalidateSelf();
        }
    }

    @Override
    public void scheduleDrawable(Drawable who, Runnable what, long when) {
        if (who == mBadge) {
            scheduleSelf(what, when);
        }
    }

    @Override
    public void unscheduleDrawable(Drawable who, Runnable what) {
        unscheduleSelf(what);
    }

    /**
     * Sets whether hover state functionality is enabled.
     */
    public static void setFlagHoverEnabled(boolean isFlagHoverEnabled) {
        sFlagHoverEnabled = isFlagHoverEnabled;
    }

    protected static class FastBitmapConstantState extends ConstantState {
        protected final Bitmap mBitmap;
        protected final int mIconColor;

        // These are initialized later so that subclasses don't need to
        // pass everything in constructor
        protected boolean mIsDisabled;
        private ConstantState mBadgeConstantState;

        public FastBitmapConstantState(Bitmap bitmap, int color) {
            mBitmap = bitmap;
            mIconColor = color;
        }

        protected FastBitmapDrawable createDrawable() {
            return new FastBitmapDrawable(mBitmap, mIconColor);
        }

        @Override
        public final FastBitmapDrawable newDrawable() {
            FastBitmapDrawable drawable = createDrawable();
            drawable.setIsDisabled(mIsDisabled);
            if (mBadgeConstantState != null) {
                drawable.setBadge(mBadgeConstantState.newDrawable());
            }
            return drawable;
        }

        @Override
        public int getChangingConfigurations() {
            return 0;
        }
    }
}