Skip to content

Commit

Permalink
Merge pull request #2118 from marunjar/cleanup_synchronized
Browse files Browse the repository at this point in the history
remove unnecessary `synchronized`
  • Loading branch information
marunjar authored Jul 2, 2023
2 parents 940b241 + c848288 commit 26955b0
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions app/src/main/java/fr/neamar/kiss/utils/DrawableUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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()) {
Expand Down

0 comments on commit 26955b0

Please sign in to comment.