This source file includes following definitions.
- createAddToHomeIntent
- createAddToHomeIntent
- createShortcutIntent
- createIcon
- getBitmapFromResourceId
- drawTouchIconToCanvas
- drawFaviconToCanvas
- drawWidgetBackgroundToCanvas
package org.chromium.chrome.browser;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
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;
import android.graphics.drawable.BitmapDrawable;
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;
public class BookmarkUtils {
private static final String INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
private static final int DEFAULT_RGB_VALUE = 145;
private static final String TAG = "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 TOUCHICON_BORDER_RADII = 10;
public static Intent createAddToHomeIntent(Context context, Intent shortcutIntent, String title,
Bitmap favicon, int rValue, int gValue, int bValue) {
Intent i = new Intent(INSTALL_SHORTCUT);
i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
i.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(context, favicon, rValue,
gValue, bValue));
return i;
}
public static Intent createAddToHomeIntent(Context context, String url, String title,
Bitmap favicon, int rValue, int gValue, int bValue) {
Intent shortcutIntent = createShortcutIntent(context, url);
return createAddToHomeIntent(
context, shortcutIntent, title, favicon, rValue, gValue, bValue);
}
public static Intent createShortcutIntent(Context context, String url) {
Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
shortcutIntent.putExtra(REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB, true);
return shortcutIntent;
}
private static Bitmap createIcon(Context context, Bitmap favicon, int rValue,
int gValue, int bValue) {
Bitmap bitmap = null;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final int iconSize = am.getLauncherLargeIconSize();
final int iconDensity = am.getLauncherLargeIconDensity();
try {
bitmap = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
if (favicon == null) {
favicon = getBitmapFromResourceId(context, R.drawable.globe_favicon, iconDensity);
rValue = gValue = bValue = DEFAULT_RGB_VALUE;
}
final int smallestSide = iconSize;
if (favicon.getWidth() >= smallestSide / 2 && favicon.getHeight() >= smallestSide / 2) {
drawTouchIconToCanvas(context, favicon, canvas);
} else {
drawWidgetBackgroundToCanvas(context, canvas, iconDensity,
Color.rgb(rValue, gValue, bValue));
drawFaviconToCanvas(context, favicon, canvas);
}
canvas.setBitmap(null);
} catch (OutOfMemoryError e) {
Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas.");
}
return bitmap;
}
private static Bitmap getBitmapFromResourceId(Context context, int id, int density) {
Drawable drawable = null;
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();
}
assert false : "The drawable was not a bitmap drawable as expected";
return null;
}
private static void drawTouchIconToCanvas(
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 = (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);
rect.inset(INSET_DIMENSION_FOR_TOUCHICON, INSET_DIMENSION_FOR_TOUCHICON);
path.addRoundRect(rect, borderRadii, borderRadii, Path.Direction.CW);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawPath(path, paint);
}
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);
}
private static void drawWidgetBackgroundToCanvas(
Context context, Canvas canvas, int iconDensity, int color) {
Rect iconBounds = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
Bitmap bookmarkWidgetBg =
getBitmapFromResourceId(context, R.mipmap.bookmark_widget_bg, iconDensity);
Bitmap bookmarkWidgetBgHighlights = getBitmapFromResourceId(context,
R.mipmap.bookmark_widget_bg_highlights, iconDensity);
if (bookmarkWidgetBg == null || bookmarkWidgetBgHighlights == 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(bookmarkWidgetBg, null, iconBounds, paint);
paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bookmarkWidgetBgHighlights, null, iconBounds, paint);
}
}