diff --git a/mobile/android/base/Favicons.java b/mobile/android/base/Favicons.java index 6c4cc177ff29..d09b58d0e797 100644 --- a/mobile/android/base/Favicons.java +++ b/mobile/android/base/Favicons.java @@ -39,6 +39,9 @@ public class Favicons { public static final long NOT_LOADING = 0; + private static int sFaviconSmallSize = -1; + private static int sFaviconLargeSize = -1; + private Context mContext; private Map mLoadTasks; @@ -170,8 +173,29 @@ public class Favicons { return Favicons.FaviconsInstanceHolder.INSTANCE; } + public boolean isLargeFavicon(Bitmap image) { + return image.getWidth() > sFaviconSmallSize || image.getHeight() > sFaviconSmallSize; + } + + public Bitmap scaleImage(Bitmap image) { + // If the icon is larger than 16px, scale it to sFaviconLargeSize. + // Otherwise, scale it to sFaviconSmallSize. + if (isLargeFavicon(image)) { + image = Bitmap.createScaledBitmap(image, sFaviconLargeSize, sFaviconLargeSize, false); + } else { + image = Bitmap.createScaledBitmap(image, sFaviconSmallSize, sFaviconSmallSize, false); + } + return image; + } + public void attachToContext(Context context) { mContext = context; + if (sFaviconSmallSize < 0) { + sFaviconSmallSize = Math.round(mContext.getResources().getDimension(R.dimen.awesomebar_row_favicon_size_small)); + } + if (sFaviconLargeSize < 0) { + sFaviconLargeSize = Math.round(mContext.getResources().getDimension(R.dimen.awesomebar_row_favicon_size_large)); + } } private class LoadFaviconTask extends AsyncTask { @@ -286,7 +310,7 @@ public class Favicons { if (storedFaviconUrl != null && storedFaviconUrl.equals(mFaviconUrl)) { image = loadFaviconFromDb(); if (image != null) - return image; + return scaleImage(image); } if (isCancelled()) @@ -296,6 +320,7 @@ public class Favicons { if (image != null && image.getWidth() > 0 && image.getHeight() > 0) { saveFaviconToDb(image); + image = scaleImage(image); } else { image = null; } diff --git a/mobile/android/base/awesomebar/AllPagesTab.java b/mobile/android/base/awesomebar/AllPagesTab.java index 13dfa8e3aca1..0879dbcd57ce 100644 --- a/mobile/android/base/awesomebar/AllPagesTab.java +++ b/mobile/android/base/awesomebar/AllPagesTab.java @@ -807,6 +807,7 @@ public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener { if (favicon == null) continue; + favicon = Favicons.getInstance().scaleImage(favicon); Favicons.getInstance().putFaviconInMemCache(url, favicon); } while (c.moveToNext()); } finally { diff --git a/mobile/android/base/awesomebar/AwesomeBarTab.java b/mobile/android/base/awesomebar/AwesomeBarTab.java index ee369767c016..54a2566741f3 100644 --- a/mobile/android/base/awesomebar/AwesomeBarTab.java +++ b/mobile/android/base/awesomebar/AwesomeBarTab.java @@ -40,17 +40,9 @@ abstract public class AwesomeBarTab { // FIXME: This value should probably come from a prefs key public static final int MAX_RESULTS = 100; protected Context mContext = null; - private static int sFaviconSmallSize = -1; - private static int sFaviconLargeSize = -1; public AwesomeBarTab(Context context) { mContext = context; - if (sFaviconSmallSize < 0) { - sFaviconSmallSize = Math.round(mContext.getResources().getDimension(R.dimen.awesomebar_row_favicon_size_small)); - } - if (sFaviconLargeSize < 0) { - sFaviconLargeSize = Math.round(mContext.getResources().getDimension(R.dimen.awesomebar_row_favicon_size_large)); - } } public void setListTouchListener(View.OnTouchListener listener) { @@ -101,6 +93,7 @@ abstract public class AwesomeBarTab { faviconView.setImageDrawable(null); } else { Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length); + bitmap = Favicons.getInstance().scaleImage(bitmap); updateFavicon(faviconView, bitmap); } } @@ -108,14 +101,11 @@ abstract public class AwesomeBarTab { protected void updateFavicon(ImageView faviconView, Bitmap bitmap) { if (bitmap == null) { faviconView.setImageDrawable(null); - } else if (bitmap.getWidth() > 16 || bitmap.getHeight() > 16) { - // If the icon is larger than 16px, scale it to sFaviconLargeSize and hide the background - bitmap = Bitmap.createScaledBitmap(bitmap, sFaviconLargeSize, sFaviconLargeSize, false); + } else if (Favicons.getInstance().isLargeFavicon(bitmap)) { + // If the icon is large, hide the background faviconView.setImageBitmap(bitmap); faviconView.setBackgroundResource(0); } else { - // If the icon is 16px or smaller, don't scale it up to full size - bitmap = Bitmap.createScaledBitmap(bitmap, sFaviconSmallSize, sFaviconSmallSize, false); faviconView.setImageBitmap(bitmap); faviconView.setBackgroundResource(R.drawable.awesomebar_row_favicon_bg); }