Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Commit

Permalink
Scale application icons
Browse files Browse the repository at this point in the history
Closes #2095
  • Loading branch information
M66B committed Dec 24, 2014
1 parent 52d70b0 commit b182527
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Changelog

**Next release**

* Prevent accidental application icon/name clicks ([issue](/../../issues/2095))
* Scale application icons ([issue](/../../issues/2095))

[Open issues](https://github.com/M66B/XPrivacy/issues?state=open)

**Version 3.5.8 BETA**
Expand Down
3 changes: 2 additions & 1 deletion src/biz/bokhorst/xprivacy/ActivityMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -1988,7 +1988,8 @@ else if (state == ApplicationInfoEx.STATE_SHARED)
getThemed(R.attr.color_state_restricted)));

// Display icon
holder.imgIcon.setImageDrawable(xAppInfo.getIcon(ActivityMain.this));
holder.imgIcon.setImageBitmap(xAppInfo.getIconBitmap(ActivityMain.this));
// holder.imgIcon.setImageDrawable(xAppInfo.getIcon(ActivityMain.this));
holder.imgIcon.setVisibility(View.VISIBLE);

// Display on demand
Expand Down
47 changes: 47 additions & 0 deletions src/biz/bokhorst/xprivacy/ApplicationInfoEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.SparseArray;
import android.util.TypedValue;

@SuppressLint("DefaultLocale")
public class ApplicationInfoEx implements Comparable<ApplicationInfoEx> {
Expand Down Expand Up @@ -155,6 +160,48 @@ public Drawable getIcon(Context context) {
return new ColorDrawable(Color.TRANSPARENT);
}

public Bitmap getIconBitmap(Context context) {
if (mMapAppInfo.size() > 0) {
try {
final ApplicationInfo appInfo = mMapAppInfo.firstEntry().getValue();
final Resources resources = context.getPackageManager().getResourcesForApplication(appInfo);

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(resources, appInfo.icon, options);

final int pixels = Math.round(dipToPixels(context, 48));
options.inSampleSize = calculateInSampleSize(options, pixels, pixels);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(resources, appInfo.icon, options);
} catch (NameNotFoundException ex) {
Util.bug(null, ex);
return null;
}
} else
return null;
}

private static float dipToPixels(Context context, float dipValue) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics);
}

private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth)
inSampleSize *= 2;
}

return inSampleSize;
}

public boolean hasInternet(Context context) {
if (mInternet == null) {
mInternet = false;
Expand Down

0 comments on commit b182527

Please sign in to comment.