Bug 867627 - Speed up dominant color favicon backgrounds. r=bnicholson

This commit is contained in:
Margaret Leibovic 2013-05-06 16:13:59 -04:00
parent 56d8eda5e3
commit 54bcba9c99
7 changed files with 47 additions and 37 deletions

View File

@ -6,6 +6,7 @@
package org.mozilla.gecko;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.gfx.BitmapUtils;
import org.mozilla.gecko.util.GeckoJarReader;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.util.UiAsyncTask;
@ -50,6 +51,7 @@ public class Favicons {
private long mNextFaviconLoadId;
private LruCache<String, Bitmap> mFaviconsCache;
private LruCache<String, Long> mFailedCache;
private LruCache<String, Integer> mColorCache;
private static final String USER_AGENT = GeckoApp.mAppContext.getDefaultUAString();
private AndroidHttpClient mHttpClient;
@ -73,6 +75,9 @@ public class Favicons {
// Create a failed favicon memory cache that has up to 64 entries
mFailedCache = new LruCache<String, Long>(64);
// Create a cache to store favicon dominant colors
mColorCache = new LruCache<String, Integer>(256);
}
private synchronized AndroidHttpClient getHttpClient() {
@ -217,6 +222,17 @@ public class Favicons {
return image;
}
public int getFaviconColor(Bitmap image, String key) {
Integer color = mColorCache.get(key);
if (color != null) {
return color;
}
color = BitmapUtils.getDominantColor(image);
mColorCache.put(key, color);
return color;
}
public void attachToContext(Context context) {
mContext = context;
if (sFaviconSmallSize < 0) {

View File

@ -571,7 +571,7 @@ public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener {
// set the search engine icon (e.g., Google) for the row
FlowLayout suggestionView = viewHolder.suggestionView;
updateFavicon(viewHolder.iconView, engine.icon);
updateFavicon(viewHolder.iconView, engine.icon, engine.name);
// user-entered search term is first suggestion
viewHolder.userEnteredTextView.setText(mSearchTerm);
@ -934,7 +934,7 @@ public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener {
private void displayFavicon(AwesomeEntryViewHolder viewHolder) {
final String url = viewHolder.urlView.getText().toString();
Bitmap bitmap = Favicons.getInstance().getFaviconFromMemCache(url);
updateFavicon(viewHolder.faviconView, bitmap);
updateFavicon(viewHolder.faviconView, bitmap, url);
}
private void updateFavicons() {

View File

@ -8,6 +8,7 @@ package org.mozilla.gecko;
import org.mozilla.gecko.AwesomeBar.ContextMenuSubject;
import org.mozilla.gecko.db.BrowserDB.URLColumns;
import org.mozilla.gecko.gfx.BitmapUtils;
import org.mozilla.gecko.widget.FaviconView;
import android.content.ContentResolver;
import android.content.Context;
@ -54,7 +55,7 @@ abstract public class AwesomeBarTab {
protected class AwesomeEntryViewHolder {
public TextView titleView;
public TextView urlView;
public ImageView faviconView;
public FaviconView faviconView;
public ImageView bookmarkIconView;
}
@ -87,20 +88,8 @@ abstract public class AwesomeBarTab {
return mResources;
}
protected void updateFavicon(ImageView faviconView, Cursor cursor) {
byte[] b = cursor.getBlob(cursor.getColumnIndexOrThrow(URLColumns.FAVICON));
Bitmap favicon = null;
if (b != null) {
Bitmap bitmap = BitmapUtils.decodeByteArray(b);
if (bitmap != null) {
favicon = Favicons.getInstance().scaleImage(bitmap);
}
}
updateFavicon(faviconView, favicon);
}
protected void updateFavicon(ImageView faviconView, Bitmap bitmap) {
faviconView.setImageBitmap(bitmap);
protected void updateFavicon(FaviconView faviconView, Bitmap bitmap, String key) {
faviconView.updateImage(bitmap, key);
}
protected void updateTitle(TextView titleView, Cursor cursor) {

View File

@ -10,6 +10,7 @@ import org.mozilla.gecko.db.BrowserContract.Bookmarks;
import org.mozilla.gecko.db.BrowserContract.Combined;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.BrowserDB.URLColumns;
import org.mozilla.gecko.gfx.BitmapUtils;
import org.mozilla.gecko.util.GamepadUtils;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.widget.FaviconView;
@ -17,6 +18,7 @@ import org.mozilla.gecko.widget.FaviconView;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import android.util.Pair;
@ -342,7 +344,17 @@ public class BookmarksTab extends AwesomeBarTab {
if (viewType == VIEW_TYPE_ITEM) {
updateTitle(viewHolder.titleView, cursor);
updateUrl(viewHolder.urlView, cursor);
updateFavicon(viewHolder.faviconView, cursor);
byte[] b = cursor.getBlob(cursor.getColumnIndexOrThrow(URLColumns.FAVICON));
Bitmap favicon = null;
if (b != null) {
Bitmap bitmap = BitmapUtils.decodeByteArray(b);
if (bitmap != null) {
favicon = Favicons.getInstance().scaleImage(bitmap);
}
}
String url = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL));
updateFavicon(viewHolder.faviconView, favicon, url);
} else {
viewHolder.titleView.setText(getFolderTitle(position));
}

View File

@ -198,7 +198,7 @@ public class HistoryTab extends AwesomeBarTab {
favicon = Favicons.getInstance().scaleImage(bitmap);
}
}
updateFavicon(viewHolder.faviconView, favicon);
updateFavicon(viewHolder.faviconView, favicon, url);
Integer bookmarkId = (Integer) historyItem.get(Combined.BOOKMARK_ID);
Integer display = (Integer) historyItem.get(Combined.DISPLAY);

View File

@ -5,7 +5,6 @@
package org.mozilla.gecko.widget;
import org.mozilla.gecko.gfx.BitmapUtils;
import org.mozilla.gecko.Favicons;
import org.mozilla.gecko.R;
@ -29,8 +28,11 @@ public class FaviconView extends ImageView {
setScaleType(ImageView.ScaleType.CENTER);
}
@Override
public void setImageBitmap(final Bitmap bitmap) {
/*
* @param bitmap favicon image
* @param key string used as a key to cache the dominant color of this image
*/
public void updateImage(Bitmap bitmap, String key) {
if (bitmap == null) {
// Call setImageDrawable directly to avoid creating a useless BitmapDrawable.
setImageDrawable(null);
@ -43,20 +45,11 @@ public class FaviconView extends ImageView {
} else {
super.setImageBitmap(bitmap);
// Otherwise show a dominant color background.
new AsyncTask<Void, Void, Integer>(){
@Override
public Integer doInBackground(Void... params) {
return BitmapUtils.getDominantColor(bitmap);
}
@Override
public void onPostExecute(Integer color) {
// Set an alpha value on the dominant color.
color = Color.argb(70, Color.red(color), Color.green(color), Color.blue(color));
Drawable drawable = getResources().getDrawable(R.drawable.favicon_bg);
drawable.setColorFilter(color, Mode.SRC_ATOP);
setBackgroundDrawable(drawable);
}
}.execute();
int color = Favicons.getInstance().getFaviconColor(bitmap, key);
color = Color.argb(70, Color.red(color), Color.green(color), Color.blue(color));
Drawable drawable = getResources().getDrawable(R.drawable.favicon_bg);
drawable.setColorFilter(color, Mode.SRC_ATOP);
setBackgroundDrawable(drawable);
}
}
}

View File

@ -125,7 +125,7 @@ public class LastTabsSection extends AboutHomeSection {
if (favicon != null) {
FaviconView faviconView = (FaviconView) tabView.findViewById(R.id.last_tab_favicon);
Bitmap bitmap = Favicons.getInstance().scaleImage(favicon);
faviconView.setImageBitmap(favicon);
faviconView.updateImage(favicon, url);
}
tabView.setOnClickListener(new View.OnClickListener() {