Commit f7d5af54 authored by Henrique Nakashima's avatar Henrique Nakashima Committed by Commit Bot

More refactoring in favicon generation.

Handle null urls inside FaviconUtils.getIconDrawableWithFilter().
All callers have this behavior already, so centralize it.

Change-Id: Ic3d56468dbfb42ab079903902144eb788142f676
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1850781
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#704787}
parent 75121e9a
......@@ -227,7 +227,8 @@ public class NavigationPopup implements AdapterView.OnItemClickListener {
private void onFaviconAvailable(String pageUrl, Bitmap favicon) {
if (favicon == null) {
if (mDefaultFaviconHelper == null) mDefaultFaviconHelper = new DefaultFaviconHelper();
favicon = mDefaultFaviconHelper.getDefaultFaviconBitmap(mContext, pageUrl, true);
favicon = mDefaultFaviconHelper.getDefaultFaviconBitmap(
mContext.getResources(), pageUrl, true);
}
for (int i = 0; i < mHistory.getEntryCount(); i++) {
NavigationEntry entry = mHistory.getEntryAtIndex(i);
......
......@@ -132,7 +132,7 @@ public class LayerTitleCache implements TitleCache {
Bitmap originalFavicon = TabFavicon.getBitmap(tab);
if (originalFavicon == null) {
originalFavicon = mDefaultFaviconHelper.getDefaultFaviconBitmap(
mContext, tab.getUrl(), !isDarkTheme);
mContext.getResources(), tab.getUrl(), !isDarkTheme);
}
TitleBitmapFactory titleBitmapFactory =
......
......@@ -5,7 +5,6 @@
package org.chromium.chrome.browser.compositor.bottombar.ephemeraltab;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Handler;
......@@ -213,11 +212,9 @@ public class EphemeralTabCoordinator {
*/
public void loadFavicon(final String url, Callback<Drawable> callback) {
FaviconHelper.FaviconImageCallback imageCallback = (bitmap, iconUrl) -> {
Drawable drawable = faviconDrawable(bitmap, url);
if (drawable == null) {
drawable = mDefaultFaviconHelper.getDefaultFaviconDrawable(mContext, url, true);
}
Drawable drawable =
FaviconUtils.getIconDrawableWithFilter(bitmap, url, mIconGenerator,
mDefaultFaviconHelper, mContext.getResources(), mFaviconSize);
callback.onResult(drawable);
};
......@@ -225,14 +222,5 @@ public class EphemeralTabCoordinator {
Profile.getLastUsedProfile(), url, mFaviconSize, imageCallback);
}
/**
* Generates a rounded bitmap for the given favicon. If the given favicon is null, generates
* a favicon from the URL instead.
*/
private Drawable faviconDrawable(Bitmap image, String url) {
if (url == null) return null;
return FaviconUtils.getIconDrawableWithFilter(
image, url, mIconGenerator, mContext.getResources(), mFaviconSize);
}
}
}
......@@ -4,7 +4,7 @@
package org.chromium.chrome.browser.favicon;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
......@@ -75,14 +75,13 @@ public class FaviconHelper {
return NewTabPage.isNTPUrl(url) ? R.drawable.chromelogo16 : R.drawable.default_favicon;
}
private Bitmap createBitmap(Context context, String url, boolean useDarkIcon) {
Bitmap origBitmap =
BitmapFactory.decodeResource(context.getResources(), getResourceId(url));
private Bitmap createBitmap(Resources resources, String url, boolean useDarkIcon) {
Bitmap origBitmap = BitmapFactory.decodeResource(resources, getResourceId(url));
Bitmap tintedBitmap = Bitmap.createBitmap(
origBitmap.getWidth(), origBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(tintedBitmap);
@ColorInt
int tintColor = ApiCompatibilityUtils.getColor(context.getResources(),
int tintColor = ApiCompatibilityUtils.getColor(resources,
useDarkIcon ? R.color.default_icon_color : R.color.default_icon_color_white);
Paint p = new Paint();
p.setColorFilter(new PorterDuffColorFilter(tintColor, PorterDuff.Mode.SRC_IN));
......@@ -92,17 +91,18 @@ public class FaviconHelper {
/**
* Generate a default favicon bitmap for the given URL.
* @param context The context used to fetch the default icons.
* @param resources The {@link Resources} to fetch the icons.
* @param url The URL of the page whose icon is being generated.
* @param useDarkIcon Whether a dark icon should be used.
* @return The favicon.
*/
public Bitmap getDefaultFaviconBitmap(Context context, String url, boolean useDarkIcon) {
public Bitmap getDefaultFaviconBitmap(
Resources resources, String url, boolean useDarkIcon) {
boolean isNtp = NewTabPage.isNTPUrl(url);
Bitmap bitmap = isNtp ? (useDarkIcon ? mChromeDarkBitmap : mChromeLightBitmap)
: (useDarkIcon ? mDefaultDarkBitmap : mDefaultLightBitmap);
if (bitmap != null) return bitmap;
bitmap = createBitmap(context, url, useDarkIcon);
bitmap = createBitmap(resources, url, useDarkIcon);
if (isNtp && useDarkIcon) {
mChromeDarkBitmap = bitmap;
} else if (isNtp) {
......@@ -117,15 +117,15 @@ public class FaviconHelper {
/**
* Generate a default favicon drawable for the given URL.
* @param context The context used to fetch the default icons.
* @param resources The {@link Resources} used to fetch the default icons.
* @param url The URL of the page whose icon is being generated.
* @param useDarkIcon Whether a dark icon should be used.
* @return The favicon.
*/
public Drawable getDefaultFaviconDrawable(
Context context, String url, boolean useDarkIcon) {
Resources resources, String url, boolean useDarkIcon) {
return new BitmapDrawable(
context.getResources(), getDefaultFaviconBitmap(context, url, useDarkIcon));
resources, getDefaultFaviconBitmap(resources, url, useDarkIcon));
}
/** Clears any of the cached default drawables. */
......
......@@ -92,18 +92,25 @@ public class FaviconUtils {
/**
* Creates a {@link Drawable} with the provided icon, or a fallback monogram, with bilinear
* scaling through {@link Bitmap#createScaledBitmap(Bitmap, int, int, boolean)}.
* scaling through {@link Bitmap#createScaledBitmap(Bitmap, int, int, boolean)}, or a fallback
* default favicon.
* @param icon {@link Bitmap} with the icon to display. If null, a fallback monogram will be
* generated.
* @param url Url to generate a monogram. Used only if {@code icon} is null.
* @param iconGenerator RoundedIconGenerator to generate a monogram. Used only if {@code icon}
* is null.
* @param defaultFaviconHelper Helper to generate default favicons.
* @param resources {@link Resources} to create a {@link BitmapDrawable}.
* @param iconSize Width and height of the returned icon.
* @return A {@link Drawable} to be displayed as the favicon.
*/
public static Drawable getIconDrawableWithFilter(@Nullable Bitmap icon, String url,
RoundedIconGenerator iconGenerator, Resources resources, int iconSize) {
public static Drawable getIconDrawableWithFilter(@Nullable Bitmap icon, @Nullable String url,
RoundedIconGenerator iconGenerator,
FaviconHelper.DefaultFaviconHelper defaultFaviconHelper, Resources resources,
int iconSize) {
if (url == null) {
return defaultFaviconHelper.getDefaultFaviconDrawable(resources, url, true);
}
if (icon == null) {
icon = iconGenerator.generateIconForUrl(url);
return new BitmapDrawable(
......
......@@ -95,8 +95,8 @@ public class HistoryItemView extends SelectableItemView<HistoryItem> implements
mTitleView.setTextColor(
ApiCompatibilityUtils.getColor(getResources(), R.color.default_red));
} else {
setIconDrawable(
mFaviconHelper.getDefaultFaviconDrawable(getContext(), item.getUrl(), true));
setIconDrawable(mFaviconHelper.getDefaultFaviconDrawable(
getContext().getResources(), item.getUrl(), true));
if (mHistoryManager != null) requestIcon();
mTitleView.setTextColor(
......
......@@ -667,7 +667,7 @@ public class RecentTabsRowAdapter extends BaseExpandableListAdapter {
}
/**
* Creates an RecentTabsRowAdapter used to populate an ExpandableList with other
* Creates a RecentTabsRowAdapter used to populate an ExpandableList with other
* devices and foreign tab cells.
*
* @param activity The Android activity this adapter will work in.
......@@ -702,17 +702,12 @@ public class RecentTabsRowAdapter extends BaseExpandableListAdapter {
public FaviconImageCallback imageCallback;
}
private Drawable faviconDrawable(Bitmap image, String url) {
if (url == null) return null;
return FaviconUtils.getIconDrawableWithFilter(
image, url, mIconGenerator, mActivity.getResources(), mFaviconSize);
}
private void loadForeignFavicon(final ViewHolder viewHolder, final String url) {
Drawable image;
if (url == null) {
// URL is null for print jobs, for example.
image = mDefaultFaviconHelper.getDefaultFaviconDrawable(mActivity, url, true);
image = mDefaultFaviconHelper.getDefaultFaviconDrawable(
mActivity.getResources(), url, true);
} else {
image = mFaviconCache.getLocalFaviconImage(url);
if (image == null) {
......@@ -720,17 +715,17 @@ public class RecentTabsRowAdapter extends BaseExpandableListAdapter {
@Override
public void onFaviconAvailable(Bitmap bitmap, String iconUrl) {
if (this != viewHolder.imageCallback) return;
Drawable image = faviconDrawable(bitmap, url);
image = image == null ? mDefaultFaviconHelper.getDefaultFaviconDrawable(
mActivity, url, true)
: image;
mFaviconCache.putLocalFaviconImage(url, image);
viewHolder.imageView.setImageDrawable(image);
Drawable faviconDrawable = FaviconUtils.getIconDrawableWithFilter(bitmap,
url, mIconGenerator, mDefaultFaviconHelper,
mActivity.getResources(), mFaviconSize);
mFaviconCache.putLocalFaviconImage(url, faviconDrawable);
viewHolder.imageView.setImageDrawable(faviconDrawable);
}
};
viewHolder.imageCallback = imageCallback;
mRecentTabsManager.getForeignFaviconForUrl(url, mFaviconSize, imageCallback);
image = mDefaultFaviconHelper.getDefaultFaviconDrawable(mActivity, url, true);
image = mDefaultFaviconHelper.getDefaultFaviconDrawable(
mActivity.getResources(), url, true);
}
}
viewHolder.imageView.setImageDrawable(image);
......@@ -740,7 +735,8 @@ public class RecentTabsRowAdapter extends BaseExpandableListAdapter {
Drawable image;
if (url == null) {
// URL is null for print jobs, for example.
image = mDefaultFaviconHelper.getDefaultFaviconDrawable(mActivity, url, true);
image = mDefaultFaviconHelper.getDefaultFaviconDrawable(
mActivity.getResources(), url, true);
} else {
image = mFaviconCache.getLocalFaviconImage(url);
if (image == null) {
......@@ -748,17 +744,17 @@ public class RecentTabsRowAdapter extends BaseExpandableListAdapter {
@Override
public void onFaviconAvailable(Bitmap bitmap, String iconUrl) {
if (this != viewHolder.imageCallback) return;
Drawable image = faviconDrawable(bitmap, url);
image = image == null ? mDefaultFaviconHelper.getDefaultFaviconDrawable(
mActivity, url, true)
: image;
mFaviconCache.putLocalFaviconImage(url, image);
viewHolder.imageView.setImageDrawable(image);
Drawable faviconDrawable = FaviconUtils.getIconDrawableWithFilter(bitmap,
url, mIconGenerator, mDefaultFaviconHelper,
mActivity.getResources(), mFaviconSize);
mFaviconCache.putLocalFaviconImage(url, faviconDrawable);
viewHolder.imageView.setImageDrawable(faviconDrawable);
}
};
viewHolder.imageCallback = imageCallback;
mRecentTabsManager.getLocalFaviconForUrl(url, mFaviconSize, imageCallback);
image = mDefaultFaviconHelper.getDefaultFaviconDrawable(mActivity, url, true);
image = mDefaultFaviconHelper.getDefaultFaviconDrawable(
mActivity.getResources(), url, true);
}
}
viewHolder.imageView.setImageDrawable(image);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment