Index: chrome/android/java/src/org/chromium/chrome/browser/BookmarkUtils.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/BookmarkUtils.java b/chrome/android/java/src/org/chromium/chrome/browser/BookmarkUtils.java |
index fe43e3be164b80a0b7cc877c0bb0a1e781c4376d..5ad155caa836e9a15a87eef9912c9847c48b04f2 100644 |
--- a/chrome/android/java/src/org/chromium/chrome/browser/BookmarkUtils.java |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/BookmarkUtils.java |
@@ -10,10 +10,10 @@ import android.content.Intent; |
import android.graphics.Bitmap; |
import android.graphics.Canvas; |
import android.graphics.Color; |
-import android.graphics.LinearGradient; |
import android.graphics.Paint; |
import android.graphics.Path; |
import android.graphics.PorterDuff; |
+import android.graphics.PorterDuffColorFilter; |
import android.graphics.PorterDuffXfermode; |
import android.graphics.Rect; |
import android.graphics.RectF; |
@@ -22,6 +22,7 @@ import android.graphics.drawable.Drawable; |
import android.net.Uri; |
import android.os.Build; |
import android.util.Log; |
+import android.util.TypedValue; |
import org.chromium.chrome.R; |
@@ -38,7 +39,7 @@ public class BookmarkUtils { |
public static final String REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB = |
"REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB"; |
private static final int INSET_DIMENSION_FOR_TOUCHICON = 1; |
- private static final int SDK_VERSION_FOR_ACCESS_TO_METHODS = 15; |
+ private static final int TOUCHICON_BORDER_RADII = 10; |
/** |
* Creates an intent that will add a shortcut to the home screen. |
@@ -88,25 +89,22 @@ public class BookmarkUtils { |
int gValue, int bValue) { |
Bitmap bitmap = null; |
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); |
- final int iconDimension = am.getLauncherLargeIconSize(); |
+ final int iconSize = am.getLauncherLargeIconSize(); |
final int iconDensity = am.getLauncherLargeIconDensity(); |
try { |
- bitmap = Bitmap.createBitmap(iconDimension, iconDimension, Bitmap.Config.ARGB_8888); |
+ bitmap = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888); |
Canvas canvas = new Canvas(bitmap); |
- Rect iconBounds = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); |
if (favicon == null) { |
favicon = getBitmapFromResourceId(context, R.drawable.globe_favicon, iconDensity); |
rValue = gValue = bValue = DEFAULT_RGB_VALUE; |
} |
- final int smallestSide = iconDimension; |
+ final int smallestSide = iconSize; |
if (favicon.getWidth() >= smallestSide / 2 && favicon.getHeight() >= smallestSide / 2) { |
- drawTouchIconToCanvas(context, favicon, canvas, iconBounds); |
+ drawTouchIconToCanvas(context, favicon, canvas); |
} else { |
- Bitmap icon = getIconBackground(context, iconDensity); |
- if (icon != null) { |
- canvas.drawBitmap(icon, null, iconBounds, new Paint(Paint.ANTI_ALIAS_FLAG)); |
- } |
- drawFaviconToCanvas(context, favicon, canvas, iconBounds, rValue, gValue, bValue); |
+ drawWidgetBackgroundToCanvas(context, canvas, iconDensity, |
+ Color.rgb(rValue, gValue, bValue)); |
+ drawFaviconToCanvas(context, favicon, canvas); |
} |
canvas.setBitmap(null); |
} catch (OutOfMemoryError e) { |
@@ -115,22 +113,14 @@ public class BookmarkUtils { |
return bitmap; |
} |
- /** |
- * Get the icon background asset. |
- * @param context Context used to create the intent. |
- * @return Bitmap favicon background asset. |
- */ |
- private static Bitmap getIconBackground(Context context, int density) { |
- return getBitmapFromResourceId(context, R.mipmap.homescreen_bg, density); |
- } |
- |
private static Bitmap getBitmapFromResourceId(Context context, int id, int density) { |
Drawable drawable = null; |
- if (Build.VERSION.SDK_INT >= SDK_VERSION_FOR_ACCESS_TO_METHODS) { |
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { |
drawable = context.getResources().getDrawableForDensity(id, density); |
} else { |
drawable = context.getResources().getDrawable(id); |
} |
+ |
if (drawable instanceof BitmapDrawable) { |
BitmapDrawable bd = (BitmapDrawable) drawable; |
return bd.getBitmap(); |
@@ -144,16 +134,17 @@ public class BookmarkUtils { |
* @param context Context used to get resources. |
* @param touchIcon Touch icon bitmap. |
* @param canvas Canvas that holds the touch icon. |
- * @param iconBounds Rectangle bounds needed for adding rounded corners of the touch icon. |
*/ |
private static void drawTouchIconToCanvas( |
- Context context, Bitmap touchIcon, Canvas canvas, Rect iconBounds) { |
+ Context context, Bitmap touchIcon, Canvas canvas) { |
+ Rect iconBounds = new Rect(0, 0, canvas.getWidth(), canvas.getHeight()); |
Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight()); |
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); |
paint.setFilterBitmap(true); |
canvas.drawBitmap(touchIcon, src, iconBounds, paint); |
- int borderRadii = |
- context.getResources().getDimensionPixelSize(R.dimen.touchicon_border_radii); |
+ // Convert dp to px. |
+ int borderRadii = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, |
+ TOUCHICON_BORDER_RADII, context.getResources().getDisplayMetrics()); |
Path path = new Path(); |
path.setFillType(Path.FillType.INVERSE_WINDING); |
RectF rect = new RectF(iconBounds); |
@@ -168,86 +159,41 @@ public class BookmarkUtils { |
* @param context Context used to create the intent. |
* @param favicon favicon bitmap. |
* @param canvas Canvas that holds the favicon. |
- * @param iconBounds Rectangle bounds needed to create the homescreen favicon. |
- * @param rValue Red component of the dominant favicon color. |
- * @param gValue Green component of the dominant favicon color. |
- * @param bValue Blue component of the dominant favicon color. |
*/ |
- private static void drawFaviconToCanvas(Context context, Bitmap favicon, |
- Canvas canvas, Rect iconBounds, int rValue, int gValue, int bValue) { |
- int colorStripWidth = context.getResources().getDimensionPixelSize( |
- R.dimen.favicon_colorstrip_width); |
- int colorStripHeight = context.getResources().getDimensionPixelSize( |
- R.dimen.favicon_colorstrip_height); |
- int colorStripPadding = context.getResources().getDimensionPixelSize( |
- R.dimen.favicon_colorstrip_padding); |
- int colorStripCornerRadii = context.getResources().getDimensionPixelSize( |
- R.dimen.favicon_colorstrip_corner_radii); |
- int foldSize = context.getResources().getDimensionPixelSize(R.dimen.favicon_fold_size); |
- int foldCornerRadii = context.getResources().getDimensionPixelSize( |
- R.dimen.favicon_fold_corner_radii); |
- int foldBorder = context.getResources().getDimensionPixelSize(R.dimen.favicon_fold_border); |
- int foldShadow = context.getResources().getDimensionPixelSize(R.dimen.favicon_fold_shadow); |
- int faviconSize = context.getResources().getDimensionPixelSize(R.dimen.favicon_size); |
- |
- float x1 = iconBounds.exactCenterX() - (colorStripWidth / 2.0f); |
- float x2 = x1 + colorStripWidth; |
- float y1 = iconBounds.height() - colorStripPadding; |
- float y2 = y1 - colorStripHeight; |
- int faviconColorAlpha100 = Color.argb(255, rValue, gValue, bValue); |
- Paint stripPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
- stripPaint.setColor(faviconColorAlpha100); |
- stripPaint.setStyle(Paint.Style.FILL); |
- Path stripPath = new Path(); |
- stripPath.moveTo(x1, y2); |
- stripPath.lineTo(x2, y2); |
- stripPath.lineTo(x2, y1 - colorStripCornerRadii); |
- stripPath.arcTo(new RectF(x2 - colorStripCornerRadii, y1 - colorStripCornerRadii, x2, y1), |
- 0, 90); |
- stripPath.lineTo(x1 + colorStripCornerRadii, y1); |
- stripPath.arcTo(new RectF(x1, y1 - colorStripCornerRadii, x1 + colorStripCornerRadii, y1), |
- 90, 90); |
- stripPath.lineTo(x1, y2); |
- canvas.drawPath(stripPath, stripPaint); |
- |
- float ax, ay, bx, by, cx, cy; |
- bx = ax = x2 - foldSize; |
- cx = x2; |
- ay = colorStripPadding; |
- by = cy = colorStripPadding + foldSize; |
- int blackAlpha10 = Color.argb((int) (255 * 0.10f), 0, 0, 0); |
+ private static void drawFaviconToCanvas(Context context, Bitmap favicon, Canvas canvas) { |
+ Rect iconBounds = new Rect(0, 0, canvas.getWidth(), canvas.getHeight()); |
+ int faviconSize = iconBounds.width() / 3; |
+ Bitmap scaledFavicon = Bitmap.createScaledBitmap(favicon, faviconSize, faviconSize, true); |
+ canvas.drawBitmap(scaledFavicon, |
+ iconBounds.exactCenterX() - scaledFavicon.getWidth() / 2.0f, |
+ iconBounds.exactCenterY() - scaledFavicon.getHeight() / 2.0f, null); |
+ } |
- Paint foldShadowPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
- foldShadowPaint.setColor(Color.WHITE); |
- foldShadowPaint.setShadowLayer(1, 0, foldShadow, blackAlpha10); |
- int faviconColorAlpha60 = Color.argb((int) (255 * 0.60f), rValue, gValue, bValue); |
- Paint foldGradientPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
- foldGradientPaint.setShader(new LinearGradient(x2 - foldSize / 2.0f, |
- colorStripPadding + foldSize / 2.0f, x2 - foldSize, colorStripPadding + foldSize, |
- faviconColorAlpha60, faviconColorAlpha100, |
- android.graphics.Shader.TileMode.CLAMP)); |
- Path foldPath = new Path(); |
- foldPath.moveTo(ax, ay); |
- foldPath.lineTo(cx, cy); |
- foldPath.lineTo(bx + foldCornerRadii, by); |
- foldPath.arcTo(new RectF(bx, by - foldCornerRadii, bx + foldCornerRadii, by), 90, 90); |
- foldPath.lineTo(ax, ay); |
- canvas.drawPath(foldPath, foldShadowPaint); |
- canvas.drawPath(foldPath, foldGradientPaint); |
- int faviconColorAlpha80 = Color.argb((int) (255 * 0.80f), rValue, gValue, bValue); |
- Paint foldBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
- foldBorderPaint.setColor(faviconColorAlpha80); |
- foldBorderPaint.setStyle(Paint.Style.STROKE); |
- foldBorderPaint.setStrokeWidth(foldBorder); |
- canvas.drawPath(foldPath, foldBorderPaint); |
- try { |
- Bitmap scaledFavicon = Bitmap.createScaledBitmap(favicon, faviconSize, faviconSize, |
- true); |
- canvas.drawBitmap(scaledFavicon, |
- iconBounds.exactCenterX() - scaledFavicon.getWidth() / 2.0f, |
- iconBounds.exactCenterY() - scaledFavicon.getHeight() / 2.0f, null); |
- } catch (OutOfMemoryError e) { |
- Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas."); |
+ /** |
+ * Draw document icon to canvas. |
+ * @param context Context used to get bitmap resources. |
+ * @param canvas Canvas that holds the document icon. |
+ * @param iconDensity Density information to get bitmap resources. |
+ * @param color Color for the document icon's folding and the bottom strip. |
+ */ |
+ private static void drawWidgetBackgroundToCanvas( |
+ Context context, Canvas canvas, int iconDensity, int color) { |
+ Rect iconBounds = new Rect(0, 0, canvas.getWidth(), canvas.getHeight()); |
+ Bitmap bookmark_widget_bg = |
+ getBitmapFromResourceId(context, R.mipmap.bookmark_widget_bg, iconDensity); |
+ Bitmap bookmark_widget_bg_highlights = getBitmapFromResourceId(context, |
+ R.mipmap.bookmark_widget_bg_highlights, iconDensity); |
+ if (bookmark_widget_bg == null || bookmark_widget_bg_highlights == null) { |
+ Log.w(TAG, "Can't load R.mipmap.bookmark_widget_bg or " + |
+ "R.mipmap.bookmark_widget_bg_highlights."); |
+ return; |
} |
+ Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG); |
+ canvas.drawBitmap(bookmark_widget_bg, null, iconBounds, paint); |
+ |
+ // The following color filter will convert bookmark_widget_bg_highlights' white color to |
+ // the 'color' variable when it is painted to 'canvas'. |
+ paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN)); |
+ canvas.drawBitmap(bookmark_widget_bg_highlights, null, iconBounds, paint); |
} |
} |