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 1edeb1f29..869fd1ef6 100644 --- a/app/src/main/java/fr/neamar/kiss/utils/DrawableUtils.java +++ b/app/src/main/java/fr/neamar/kiss/utils/DrawableUtils.java @@ -94,7 +94,6 @@ private static float getScaleToFit(int shape) { /** * Handle adaptive icons for compatible devices - * Synchronized because class fields like {@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 ctx {@link Context} * @param icon the {@link Drawable} to shape @@ -104,8 +103,7 @@ private static float getScaleToFit(int shape) { * @return shaped icon */ @NonNull - @SuppressLint("NewApi") - public synchronized static Drawable applyIconMaskShape(@NonNull Context ctx, @NonNull Drawable icon, int shape, boolean fitInside, @ColorInt int backgroundColor) { + public static Drawable applyIconMaskShape(@NonNull Context ctx, @NonNull Drawable icon, int shape, boolean fitInside, @ColorInt int backgroundColor) { if (shape == SHAPE_SYSTEM && !hasDeviceConfiguredMask()) // if no icon mask can be configured for device, then use icon as is return icon; @@ -114,10 +112,7 @@ public synchronized static Drawable applyIconMaskShape(@NonNull Context ctx, @No Bitmap outputBitmap; Canvas outputCanvas; - final Paint outputPaint = PAINT; - outputPaint.reset(); - outputPaint.setFlags(Paint.ANTI_ALIAS_FLAG); - if (isAdaptiveIconDrawable(icon)) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && isAdaptiveIconDrawable(icon)) { AdaptiveIconDrawable adaptiveIcon = (AdaptiveIconDrawable) icon; Drawable bgDrawable = adaptiveIcon.getBackground(); Drawable fgDrawable = adaptiveIcon.getForeground(); @@ -131,9 +126,8 @@ public synchronized static Drawable applyIconMaskShape(@NonNull Context ctx, @No outputBitmap = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888); outputCanvas = new Canvas(outputBitmap); - outputPaint.setColor(backgroundColor); - setIconShape(outputCanvas, outputPaint, shape); + setIconShape(outputCanvas, backgroundColor, shape); // Stretch adaptive layers because they are 108dp and the icon size is 48dp if (bgDrawable != null) { @@ -165,9 +159,8 @@ else if (icon != null) { outputBitmap = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888); outputCanvas = new Canvas(outputBitmap); - outputPaint.setColor(backgroundColor); - setIconShape(outputCanvas, outputPaint, shape); + setIconShape(outputCanvas, backgroundColor, shape); // Shrink icon so that it fits the shape int bottomRightCorner = iconSize - iconOffset; @@ -178,24 +171,28 @@ else if (icon != null) { outputBitmap = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888); outputCanvas = new Canvas(outputBitmap); - outputPaint.setColor(Color.BLACK); - setIconShape(outputCanvas, outputPaint, shape); + setIconShape(outputCanvas, Color.BLACK, shape); } return new BitmapDrawable(ctx.getResources(), outputBitmap); } /** * Set the shape of adaptive icons - * Synchronized because class fields like {@link DrawableUtils#SHAPE_PATH} and {@link DrawableUtils#RECT_F} are reused for every call, which may result in unexpected behaviour if method is called from different threads running in parallel. + * 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_* */ - private synchronized static void setIconShape(Canvas canvas, Paint paint, int shape) { + private synchronized static void setIconShape(Canvas canvas, @ColorInt int backgroundColor, int shape) { int iconSize = canvas.getHeight(); final Path path = SHAPE_PATH; path.rewind(); + final Paint paint = PAINT; + paint.reset(); + paint.setFlags(Paint.ANTI_ALIAS_FLAG); + paint.setColor(backgroundColor); + switch (shape) { case SHAPE_SYSTEM: { if (hasDeviceConfiguredMask()) {