Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
- try to calculate real shape in single place
- introduce TEARDROP_SHAPES
  • Loading branch information
marunjar committed Jan 30, 2024
1 parent aeadcea commit c3aabcf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
10 changes: 5 additions & 5 deletions app/src/main/java/fr/neamar/kiss/IconsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}


Expand Down
38 changes: 26 additions & 12 deletions app/src/main/java/fr/neamar/kiss/utils/DrawableUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -329,15 +332,15 @@ 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);
}

@NonNull
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);

Expand Down Expand Up @@ -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;
}
}


}
14 changes: 0 additions & 14 deletions app/src/main/java/fr/neamar/kiss/utils/IconShape.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit c3aabcf

Please sign in to comment.