Skip to content

Commit

Permalink
Merge pull request #16 from oxters168/CustomIconAlphaIssue
Browse files Browse the repository at this point in the history
Custom icon alpha issue
  • Loading branch information
oxters168 authored Aug 27, 2023
2 parents d28e169 + 093339a commit 4c19b01
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/java/com/OxGames/OxShell/Data/DataRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,33 @@ else if (loc instanceof Bitmap)
onIconLoaded.accept(null);
}

public void getImageAsBitmap(Consumer<Bitmap> onIconLoaded) {
if (locType == DataLocation.resource) {
try {
int resId = OxShellApp.getCurrentActivity().getResources().getIdentifier((String)loc, "drawable", BuildConfig.APPLICATION_ID);
onIconLoaded.accept(AndroidHelpers.bitmapFromResource(OxShellApp.getContext(), resId));
} catch (Exception e) {
Log.e("ImageRef", "Failed to find resource " + loc + ": " + e);
onIconLoaded.accept(AndroidHelpers.bitmapFromResource(OxShellApp.getContext(), R.drawable.ic_baseline_question_mark_24));
}
}
else if (locType == DataLocation.asset)
onIconLoaded.accept(AndroidHelpers.readAssetAsBitmap(OxShellApp.getContext(), (String)loc));
else if (locType == DataLocation.pkg)
PackagesCache.requestPackageIcon((String)loc, (icon) -> { onIconLoaded.accept(AndroidHelpers.drawableToBitmap(icon));});
else if (locType == DataLocation.file)
onIconLoaded.accept(AndroidHelpers.bitmapFromFile((String)loc));
else if (locType == DataLocation.resolverUri)
onIconLoaded.accept(AndroidHelpers.readResolverUriAsBitmap(OxShellApp.getContext(), (Uri)loc));
else if (locType == DataLocation.self) {
if (loc instanceof Drawable)
onIconLoaded.accept(AndroidHelpers.drawableToBitmap((Drawable)loc));
else if (loc instanceof Bitmap)
onIconLoaded.accept((Bitmap)loc);
} else
onIconLoaded.accept(null);
}

public Typeface getFont() {
if (locType == DataLocation.resource || locType == DataLocation.resolverUri)
throw new UnsupportedOperationException("Failed to reference font, resources and resolver uris are not supported at the moment");
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/OxGames/OxShell/Data/XMBItem.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.OxGames.OxShell.Data;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;

Expand Down
28 changes: 25 additions & 3 deletions app/src/main/java/com/OxGames/OxShell/Helpers/AndroidHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
Expand Down Expand Up @@ -158,8 +159,29 @@ public static void setWallpaper(Context context, Bitmap bitmap, Rect visibleCrop
wallpaperManager = WallpaperManager.getInstance(context);
try { wallpaperManager.setBitmap(bitmap, visibleCropHint, allowBackup, which); } catch (IOException e) { e.printStackTrace(); }
}
public static Bitmap bitmapFromResource(Resources res, int resId) {
return BitmapFactory.decodeResource(res, resId);
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = null;

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

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);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
public static Bitmap bitmapFromResource(Context context, int resId) {
return BitmapFactory.decodeResource(context.getResources(), resId);
}
public static Bitmap bitmapFromFile(String path) {
return BitmapFactory.decodeFile(path);
Expand Down Expand Up @@ -305,7 +327,7 @@ public static void saveBitmapToFile(Bitmap bm, String path) {
createPathIfNotExist(path);
try {
FileOutputStream out = new FileOutputStream(path);
bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch(Exception e) { Log.e("AndroidHelpers", e.toString()); }
Expand Down

0 comments on commit 4c19b01

Please sign in to comment.