From b182527c375ddf913fb8e0dab30ef1ecc3f51b66 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 24 Dec 2014 20:25:43 +0100 Subject: [PATCH] Scale application icons Closes #2095 --- CHANGELOG.md | 3 ++ src/biz/bokhorst/xprivacy/ActivityMain.java | 3 +- .../bokhorst/xprivacy/ApplicationInfoEx.java | 47 +++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4b5c6c1a..7972cd42b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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** diff --git a/src/biz/bokhorst/xprivacy/ActivityMain.java b/src/biz/bokhorst/xprivacy/ActivityMain.java index 48b784407..1fd135754 100644 --- a/src/biz/bokhorst/xprivacy/ActivityMain.java +++ b/src/biz/bokhorst/xprivacy/ActivityMain.java @@ -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 diff --git a/src/biz/bokhorst/xprivacy/ApplicationInfoEx.java b/src/biz/bokhorst/xprivacy/ApplicationInfoEx.java index 0b0137255..52b276ae0 100644 --- a/src/biz/bokhorst/xprivacy/ApplicationInfoEx.java +++ b/src/biz/bokhorst/xprivacy/ApplicationInfoEx.java @@ -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 { @@ -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;