Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9a8d11f9 authored by Tony Wickham's avatar Tony Wickham
Browse files

FastBitmapDrawable can draw an icon badge (notification count)

- Added BadgeInfo to contain data to be shown in a badge
  (currently just notification count).
- Added BadgeRenderer in DeviceProfile to contain things
  relevant to drawing the badge, such as size and Paint's.
- Added IconPalette to compute colors for the badge based
  on a dominant color (will also be used for notifications)
- FastBitmapDrawable uses these classes to draw the badge.

Bug: 32410600
Change-Id: I6595a4879943357590f7d20c22594691a573ecaf
parent 19ea5cc0
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -169,6 +169,10 @@
         also happens to equal 19dp-->
    <dimen name="deep_shortcuts_arrow_horizontal_offset">19dp</dimen>

<!-- Icon badges (with notification counts) -->
    <dimen name="badge_size">24dp</dimen>
    <dimen name="badge_text_size">12dp</dimen>

<!-- Other -->
    <!-- Approximates the system status bar height. Not guaranteed to be always be correct. -->
    <dimen name="status_bar_height">24dp</dimen>
+13 −4
Original line number Diff line number Diff line
@@ -39,6 +39,8 @@ import android.widget.TextView;

import com.android.launcher3.IconCache.IconLoadRequest;
import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
import com.android.launcher3.badge.BadgeRenderer;
import com.android.launcher3.badge.BadgeInfo;
import com.android.launcher3.folder.FolderIcon;
import com.android.launcher3.graphics.DrawableFactory;
import com.android.launcher3.graphics.HolographicOutlineHelper;
@@ -164,7 +166,7 @@ public class BubbleTextView extends TextView
        applyIconAndLabel(info.iconBitmap, info);
        setTag(info);
        if (promiseStateChanged || info.isPromise()) {
            applyState(promiseStateChanged);
            applyPromiseState(promiseStateChanged);
        }
    }

@@ -470,7 +472,7 @@ public class BubbleTextView extends TextView
        mLongPressHelper.cancelLongPress();
    }

    public void applyState(boolean promiseStateChanged) {
    public void applyPromiseState(boolean promiseStateChanged) {
        if (getTag() instanceof ShortcutInfo) {
            ShortcutInfo info = (ShortcutInfo) getTag();
            final boolean isPromise = info.isPromise();
@@ -500,6 +502,13 @@ public class BubbleTextView extends TextView
        }
    }

    public void applyBadgeState(BadgeInfo badgeInfo) {
        if (mIcon instanceof FastBitmapDrawable) {
            BadgeRenderer badgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
            ((FastBitmapDrawable) mIcon).applyIconBadge(badgeInfo, badgeRenderer);
        }
    }

    private Theme getPreloaderTheme() {
        Object tag = getTag();
        int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
+8 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;

import com.android.launcher3.CellLayout.ContainerType;
import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.badge.BadgeRenderer;

import java.util.ArrayList;

@@ -136,6 +136,9 @@ public class DeviceProfile {
    // Listeners
    private ArrayList<LauncherLayoutChangeListener> mListeners = new ArrayList<>();

    // Icon badges
    public BadgeRenderer mBadgeRenderer;

    public DeviceProfile(Context context, InvariantDeviceProfile inv,
            Point minSize, Point maxSize,
            int width, int height, boolean isLandscape) {
@@ -193,6 +196,10 @@ public class DeviceProfile {
        hotseatBarBottomPaddingPx = 0;
        hotseatLandGutterPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_gutter_width);

        int badgeSize = res.getDimensionPixelSize(R.dimen.badge_size);
        int badgeTextSize = res.getDimensionPixelSize(R.dimen.badge_text_size);
        mBadgeRenderer = new BadgeRenderer(badgeSize, badgeTextSize);

        // Determine sizes.
        widthPx = width;
        heightPx = height;
+30 −0
Original line number Diff line number Diff line
@@ -33,6 +33,10 @@ import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.animation.DecelerateInterpolator;

import com.android.launcher3.graphics.IconPalette;
import com.android.launcher3.badge.BadgeRenderer;
import com.android.launcher3.badge.BadgeInfo;

public class FastBitmapDrawable extends Drawable {
    private static final float DISABLED_DESATURATION = 1f;
    private static final float DISABLED_BRIGHTNESS = 0.5f;
@@ -99,6 +103,10 @@ public class FastBitmapDrawable extends Drawable {
    private State mState = State.NORMAL;
    private boolean mIsDisabled;

    private BadgeInfo mBadgeInfo;
    private BadgeRenderer mBadgeRenderer;
    private IconPalette mIconPalette;

    // The saturation and brightness are values that are mapped to REDUCED_FILTER_VALUE_SPACE and
    // as a result, can be used to compose the key for the cached ColorMatrixColorFilters
    private int mDesaturation = 0;
@@ -114,9 +122,21 @@ public class FastBitmapDrawable extends Drawable {
        setBounds(0, 0, b.getWidth(), b.getHeight());
    }

    public void applyIconBadge(BadgeInfo badgeInfo, BadgeRenderer badgeRenderer) {
        mBadgeInfo = badgeInfo;
        mBadgeRenderer = badgeRenderer;
        if (mIconPalette == null) {
            mIconPalette = IconPalette.fromDominantColor(Utilities
                    .findDominantColorByHue(mBitmap, 20));
        }
        invalidateSelf();
    }

    @Override
    public void draw(Canvas canvas) {
        drawInternal(canvas);
        // Draw the icon badge in the top right corner.
        drawBadgeIfNecessary(canvas);
    }

    public void drawWithBrightness(Canvas canvas, float brightness) {
@@ -130,6 +150,16 @@ public class FastBitmapDrawable extends Drawable {
        canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
    }

    protected void drawBadgeIfNecessary(Canvas canvas) {
        if (hasBadge()) {
            mBadgeRenderer.draw(canvas, mIconPalette, mBadgeInfo, getBounds());
        }
    }

    private boolean hasBadge() {
        return mBadgeInfo != null && mBadgeInfo.getNotificationCount() != null;
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        // No op
+1 −1
Original line number Diff line number Diff line
@@ -306,7 +306,7 @@ public final class Utilities {
     * @param bitmap The bitmap to scan
     * @param samples The approximate max number of samples to use.
     */
    static int findDominantColorByHue(Bitmap bitmap, int samples) {
    public static int findDominantColorByHue(Bitmap bitmap, int samples) {
        final int height = bitmap.getHeight();
        final int width = bitmap.getWidth();
        int sampleStride = (int) Math.sqrt((height * width) / samples);
Loading