Skip to content

Commit

Permalink
Merge pull request #2142 from marunjar/fix_custom_icon_adaptive
Browse files Browse the repository at this point in the history
allow all icons for custom icons
  • Loading branch information
marunjar authored Aug 24, 2023
2 parents d4cf184 + 23c67a8 commit b104b20
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
14 changes: 7 additions & 7 deletions app/src/main/java/fr/neamar/kiss/IconsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Color;
Expand Down Expand Up @@ -327,15 +328,14 @@ private boolean isDrawableInCache(String key) {
}

private void storeDrawable(File drawableFile, Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
FileOutputStream fos;
try {
fos = new FileOutputStream(drawableFile);
((BitmapDrawable) drawable).getBitmap().compress(CompressFormat.PNG, 100, fos);
// convert any drawable to bitmap that can be stored
Bitmap bitmap = DrawableUtils.drawableToBitmap(drawable);
if (bitmap != null) {
try (FileOutputStream fos = new FileOutputStream(drawableFile)) {
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (Exception e) {
Log.e(TAG, "Unable to store drawable in cache " + e);
Log.e(TAG, "Unable to store drawable in cache ", e);
}
}
}
Expand Down
13 changes: 2 additions & 11 deletions app/src/main/java/fr/neamar/kiss/icons/IconPackXML.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,12 @@ private Bitmap getBitmap(@NonNull DrawableInfo drawableInfo) {
* @param icon any {@link Drawable}
* @return a {@link BitmapDrawable}
*/
public BitmapDrawable getBitmapDrawable(Drawable icon) {
private BitmapDrawable getBitmapDrawable(Drawable icon) {
if (icon instanceof BitmapDrawable) {
return (BitmapDrawable) icon;
}

final Canvas canvas = new Canvas();
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG));
Bitmap bitmap;
if (icon.getIntrinsicWidth() <= 0 || icon.getIntrinsicHeight() <= 0)
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
else
bitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
icon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
icon.draw(canvas);
Bitmap bitmap = DrawableUtils.drawableToBitmap(icon);
return new BitmapDrawable(packResources, bitmap);
}

Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/fr/neamar/kiss/utils/DrawableUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.AdaptiveIconDrawable;
Expand Down Expand Up @@ -43,22 +44,23 @@ public class DrawableUtils {

// https://stackoverflow.com/questions/3035692/how-to-convert-a-drawable-to-a-bitmap
public static Bitmap drawableToBitmap(@NonNull Drawable drawable) {
Bitmap bitmap;

if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if (bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}

Bitmap bitmap;
if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}

Canvas canvas = new Canvas(bitmap);
final Canvas canvas = new Canvas();
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG));
canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
Expand Down

0 comments on commit b104b20

Please sign in to comment.