From c3aabcfd48f28aaf8ff59aa35bfa7f9c7b82e915 Mon Sep 17 00:00:00 2001 From: marunjar Date: Tue, 30 Jan 2024 16:35:24 +0100 Subject: [PATCH] cleanup code - try to calculate real shape in single place - introduce TEARDROP_SHAPES --- .../java/fr/neamar/kiss/IconsHandler.java | 10 ++--- .../fr/neamar/kiss/utils/DrawableUtils.java | 38 +++++++++++++------ .../java/fr/neamar/kiss/utils/IconShape.java | 14 ------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/fr/neamar/kiss/IconsHandler.java b/app/src/main/java/fr/neamar/kiss/IconsHandler.java index 4767a87af..176293fa2 100644 --- a/app/src/main/java/fr/neamar/kiss/IconsHandler.java +++ b/app/src/main/java/fr/neamar/kiss/IconsHandler.java @@ -224,7 +224,7 @@ public Drawable getBackgroundDrawable(@ColorInt int backgroundColor) { return null; } - final IconShape shape = getShapeForGeneratingDrawable(backgroundColor); + final IconShape shape = getShapeForGeneratingDrawable(); Drawable drawable = DrawableUtils.generateBackgroundDrawable(ctx, backgroundColor, shape); return forceIconMask(drawable, shape); } @@ -234,7 +234,7 @@ public Drawable getDrawableIconForCodepoint(int codePoint, @ColorInt int textCol if (mIconPack != null && !mIconPack.isLoaded()) { return null; } - final IconShape shape = getShapeForGeneratingDrawable(codePoint); + final IconShape shape = getShapeForGeneratingDrawable(); Drawable drawable = DrawableUtils.generateCodepointDrawable(ctx, codePoint, textColor, backgroundColor, shape); return forceIconMask(drawable, shape); } @@ -318,6 +318,7 @@ private IconShape getContactsShape() { if (shape == IconShape.SHAPE_SYSTEM) { shape = mSystemPack.getAdaptiveShape(); } + // contacts have square images, so fallback to circle explicitly if (shape == IconShape.SHAPE_SYSTEM && !DrawableUtils.hasDeviceConfiguredMask()) { shape = IconShape.SHAPE_CIRCLE; } @@ -329,16 +330,15 @@ private IconShape getContactsShape() { * If icon pack has mask then {@link IconShape#SHAPE_SYSTEM} is used. * If shape is {@link IconShape#SHAPE_SYSTEM} too and no icon mask can be configured for device, used shape is a circle. * - * @param hash, for pseudo random shape if applicable * @return shape */ @NonNull - private IconShape getShapeForGeneratingDrawable(int hash) { + private IconShape getShapeForGeneratingDrawable() { IconShape shape = mSystemPack.getAdaptiveShape(); if (mIconPack != null && mIconPack.hasMask()) { shape = IconShape.SHAPE_SYSTEM; } - return shape.getFinalShape(hash); + return shape; } diff --git a/app/src/main/java/fr/neamar/kiss/utils/DrawableUtils.java b/app/src/main/java/fr/neamar/kiss/utils/DrawableUtils.java index 62a737d69..488ba883f 100644 --- a/app/src/main/java/fr/neamar/kiss/utils/DrawableUtils.java +++ b/app/src/main/java/fr/neamar/kiss/utils/DrawableUtils.java @@ -34,6 +34,7 @@ public class DrawableUtils { private static final RectF RECT_F = new RectF(); public static final String KEY_THEMED_ICONS = "themed-icons"; private static final String TAG = DrawableUtils.class.getSimpleName(); + private static final IconShape[] TEARDROP_SHAPES = {IconShape.SHAPE_TEARDROP_BR, IconShape.SHAPE_TEARDROP_BL, IconShape.SHAPE_TEARDROP_TL, IconShape.SHAPE_TEARDROP_TR}; // https://stackoverflow.com/questions/3035692/how-to-convert-a-drawable-to-a-bitmap public static Bitmap drawableToBitmap(@NonNull Drawable drawable) { @@ -102,7 +103,6 @@ public static Drawable applyIconMaskShape(@NonNull Context ctx, @NonNull Drawabl // if no icon mask can be configured for device, then use icon as is return icon; } - shape = shape.getFinalShape(icon.hashCode()); Bitmap outputBitmap; Canvas outputCanvas; @@ -121,7 +121,7 @@ public static Drawable applyIconMaskShape(@NonNull Context ctx, @NonNull Drawabl outputBitmap = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888); outputCanvas = new Canvas(outputBitmap); - setIconShapeAndDrawBackground(outputCanvas, backgroundColor, shape, false); + setIconShapeAndDrawBackground(outputCanvas, backgroundColor, shape, false, icon.hashCode()); // Stretch adaptive layers because they are 108dp and the icon size is 48dp if (bgDrawable != null) { @@ -154,7 +154,7 @@ public static Drawable applyIconMaskShape(@NonNull Context ctx, @NonNull Drawabl outputBitmap = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888); outputCanvas = new Canvas(outputBitmap); - setIconShapeAndDrawBackground(outputCanvas, backgroundColor, shape, true); + setIconShapeAndDrawBackground(outputCanvas, backgroundColor, shape, true, icon.hashCode()); // Shrink icon so that it fits the shape int bottomRightCorner = iconSize - iconOffset; @@ -169,8 +169,11 @@ public static Drawable applyIconMaskShape(@NonNull Context ctx, @NonNull Drawabl * Synchronized because class fields like {@link DrawableUtils#SHAPE_PATH}, {@link DrawableUtils#RECT_F} and {@link DrawableUtils#PAINT} are reused for every call, which may result in unexpected behaviour if method is called from different threads running in parallel. * * @param shape type of shape: DrawableUtils.SHAPE_* + * @param hash, for pseudo random shape if applicable */ - private synchronized static void setIconShapeAndDrawBackground(Canvas canvas, @ColorInt int backgroundColor, @NonNull IconShape shape, boolean drawBackground) { + private synchronized static void setIconShapeAndDrawBackground(Canvas canvas, @ColorInt int backgroundColor, @NonNull IconShape shape, boolean drawBackground, int hash) { + shape = getFinalShape(shape, hash); + int iconSize = canvas.getHeight(); final Path path = SHAPE_PATH; path.rewind(); @@ -329,7 +332,7 @@ public static boolean isThemedIconEnabled(Context ctx) { @NonNull public synchronized static Drawable generateBackgroundDrawable(@NonNull Context ctx, @ColorInt int backgroundColor, @NonNull IconShape shape) { - Bitmap bitmap = generateBackgroundBitmap(ctx, backgroundColor, shape); + Bitmap bitmap = generateBackgroundBitmap(ctx, backgroundColor, shape, backgroundColor); return new BitmapDrawable(ctx.getResources(), bitmap); } @@ -337,7 +340,7 @@ public synchronized static Drawable generateBackgroundDrawable(@NonNull Context public static Drawable generateCodepointDrawable(@NonNull Context ctx, int codepoint, @ColorInt int textColor, @ColorInt int backgroundColor, @NonNull IconShape shape) { int iconSize = ctx.getResources().getDimensionPixelSize(R.dimen.result_icon_size); - Bitmap bitmap = generateBackgroundBitmap(ctx, backgroundColor, shape); + Bitmap bitmap = generateBackgroundBitmap(ctx, backgroundColor, shape, codepoint); // create a canvas from a bitmap Canvas canvas = new Canvas(bitmap); @@ -402,19 +405,30 @@ else if (!Character.UnicodeBlock.BASIC_LATIN.toString().equals(blockString)) { } @NonNull - private synchronized static Bitmap generateBackgroundBitmap(@NonNull Context ctx, @ColorInt int backgroundColor, @NonNull IconShape shape) { + private synchronized static Bitmap generateBackgroundBitmap(@NonNull Context ctx, @ColorInt int backgroundColor, @NonNull IconShape shape, int hash) { int iconSize = ctx.getResources().getDimensionPixelSize(R.dimen.result_icon_size); // create a canvas from a bitmap Bitmap bitmap = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); - if (shape == IconShape.SHAPE_SYSTEM && !hasDeviceConfiguredMask()) { - shape = IconShape.SHAPE_CIRCLE; - } - - setIconShapeAndDrawBackground(canvas, backgroundColor, shape, true); + setIconShapeAndDrawBackground(canvas, backgroundColor, shape, true, hash); return bitmap; } + public static IconShape getFinalShape(IconShape shape, int hash) { + switch (shape) { + case SHAPE_SYSTEM: + if (!hasDeviceConfiguredMask()) { + return IconShape.SHAPE_CIRCLE; + } + return shape; + case SHAPE_TEARDROP_RND: + return TEARDROP_SHAPES[Math.abs(hash % 4)]; + default: + return shape; + } + } + + } diff --git a/app/src/main/java/fr/neamar/kiss/utils/IconShape.java b/app/src/main/java/fr/neamar/kiss/utils/IconShape.java index afe8f79f6..309f7d9d5 100644 --- a/app/src/main/java/fr/neamar/kiss/utils/IconShape.java +++ b/app/src/main/java/fr/neamar/kiss/utils/IconShape.java @@ -37,18 +37,4 @@ public int getId() { return id; } - public IconShape getFinalShape(int hash) { - switch (this) { - case SHAPE_SYSTEM: - if (!DrawableUtils.hasDeviceConfiguredMask()) { - return IconShape.SHAPE_CIRCLE; - } - return this; - case SHAPE_TEARDROP_RND: - IconShape[] shapes = {SHAPE_TEARDROP_BR, SHAPE_TEARDROP_BL, SHAPE_TEARDROP_TL, SHAPE_TEARDROP_TR}; - return shapes[Math.abs(hash % 4)]; - default: - return this; - } - } }