| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser; | |
| 6 | |
| 7 import android.app.ActivityManager; | |
| 8 import android.content.Context; | |
| 9 import android.content.Intent; | |
| 10 import android.content.pm.PackageManager; | |
| 11 import android.content.pm.ResolveInfo; | |
| 12 import android.graphics.Bitmap; | |
| 13 import android.graphics.Canvas; | |
| 14 import android.graphics.Color; | |
| 15 import android.graphics.Paint; | |
| 16 import android.graphics.Path; | |
| 17 import android.graphics.PorterDuff; | |
| 18 import android.graphics.PorterDuffXfermode; | |
| 19 import android.graphics.Rect; | |
| 20 import android.graphics.RectF; | |
| 21 import android.graphics.drawable.BitmapDrawable; | |
| 22 import android.graphics.drawable.Drawable; | |
| 23 import android.net.Uri; | |
| 24 import android.util.DisplayMetrics; | |
| 25 import android.util.Log; | |
| 26 import android.util.TypedValue; | |
| 27 | |
| 28 import org.chromium.base.ApiCompatibilityUtils; | |
| 29 import org.chromium.chrome.R; | |
| 30 import org.chromium.chrome.browser.widget.RoundedIconGenerator; | |
| 31 | |
| 32 import java.util.List; | |
| 33 | |
| 34 /** | |
| 35 * Util class for bookmarks. | |
| 36 */ | |
| 37 public class BookmarkUtils { | |
| 38 // There is no public string defining this intent so if Home changes the val
ue, we | |
| 39 // have to update this string. | |
| 40 private static final String INSTALL_SHORTCUT = "com.android.launcher.action.
INSTALL_SHORTCUT"; | |
| 41 private static final int DEFAULT_RGB_VALUE = 145; | |
| 42 private static final String TAG = "BookmarkUtils"; | |
| 43 public static final String REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB = | |
| 44 "REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB"; | |
| 45 private static final int INSET_DIMENSION_FOR_TOUCHICON = 1; | |
| 46 private static final int TOUCHICON_BORDER_RADII_DP = 4; | |
| 47 private static final int GENERATED_ICON_SIZE_DP = 40; | |
| 48 private static final int GENERATED_ICON_ROUNDED_CORNERS_DP = 2; | |
| 49 private static final int GENERATED_ICON_FONT_SIZE_DP = 16; | |
| 50 | |
| 51 /** | |
| 52 * Creates an intent that will add a shortcut to the home screen. | |
| 53 * @param shortcutIntent Intent to fire when the shortcut is activated. | |
| 54 * @param title Title of the bookmark. | |
| 55 * @param icon Image that represents the bookmark. | |
| 56 * @param url URL of the bookmark. | |
| 57 * @return Intent for the shortcut. | |
| 58 */ | |
| 59 public static Intent createAddToHomeIntent( | |
| 60 Intent shortcutIntent, String title, Bitmap icon, String url) { | |
| 61 Intent i = new Intent(INSTALL_SHORTCUT); | |
| 62 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); | |
| 63 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); | |
| 64 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon); | |
| 65 return i; | |
| 66 } | |
| 67 | |
| 68 /** | |
| 69 * Creates an intent that will add a shortcut to the home screen. | |
| 70 * @param url Url of the bookmark. | |
| 71 * @param title Title of the bookmark. | |
| 72 * @param icon Image that represents the bookmark. | |
| 73 * @return Intent for the shortcut. | |
| 74 */ | |
| 75 public static Intent createAddToHomeIntent(String url, String title, Bitmap
icon) { | |
| 76 Intent shortcutIntent = createShortcutIntent(url); | |
| 77 return createAddToHomeIntent(shortcutIntent, title, icon, url); | |
| 78 } | |
| 79 | |
| 80 /** | |
| 81 * Shortcut intent for icon on homescreen. | |
| 82 * @param url Url of the bookmark. | |
| 83 * @return Intent for onclick action of the shortcut. | |
| 84 */ | |
| 85 public static Intent createShortcutIntent(String url) { | |
| 86 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); | |
| 87 shortcutIntent.putExtra(REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB, true); | |
| 88 return shortcutIntent; | |
| 89 } | |
| 90 | |
| 91 /** | |
| 92 * Utility method to check if a shortcut can be added to the homescreen. | |
| 93 * @param context Context used to get the package manager. | |
| 94 * @return if a shortcut can be added to the homescreen under the current pr
ofile. | |
| 95 */ | |
| 96 public static boolean isAddToHomeIntentSupported(Context context) { | |
| 97 PackageManager pm = context.getPackageManager(); | |
| 98 Intent i = new Intent(INSTALL_SHORTCUT); | |
| 99 List<ResolveInfo> receivers = pm.queryBroadcastReceivers( | |
| 100 i, PackageManager.GET_INTENT_FILTERS); | |
| 101 return !receivers.isEmpty(); | |
| 102 } | |
| 103 | |
| 104 /** | |
| 105 * Creates an icon to be associated with this bookmark. If available, the to
uch icon | |
| 106 * will be used, else we draw our own. | |
| 107 * @param context Context used to create the intent. | |
| 108 * @param icon Image representing the bookmark. | |
| 109 * @param url URL of the bookmark. | |
| 110 * @param rValue Red component of the dominant icon color. | |
| 111 * @param gValue Green component of the dominant icon color. | |
| 112 * @param bValue Blue component of the dominant icon color. | |
| 113 * @return Bitmap Either the touch-icon or the newly created favicon. | |
| 114 */ | |
| 115 public static Bitmap createLauncherIcon(Context context, Bitmap icon, String
url, int rValue, | |
| 116 int gValue, int bValue) { | |
| 117 Bitmap bitmap = null; | |
| 118 ActivityManager am = (ActivityManager) context.getSystemService(Context.
ACTIVITY_SERVICE); | |
| 119 final int iconSize = am.getLauncherLargeIconSize(); | |
| 120 final int iconDensity = am.getLauncherLargeIconDensity(); | |
| 121 try { | |
| 122 bitmap = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_
8888); | |
| 123 Canvas canvas = new Canvas(bitmap); | |
| 124 if (icon == null) { | |
| 125 icon = getBitmapFromResourceId(context, R.drawable.globe_favicon
, iconDensity); | |
| 126 rValue = gValue = bValue = DEFAULT_RGB_VALUE; | |
| 127 } | |
| 128 final int smallestSide = iconSize; | |
| 129 if (icon.getWidth() >= smallestSide / 2 && icon.getHeight() >= small
estSide / 2) { | |
| 130 drawTouchIconToCanvas(context, icon, canvas); | |
| 131 } else { | |
| 132 drawWidgetBackgroundToCanvas(context, canvas, iconDensity, url, | |
| 133 Color.rgb(rValue, gValue, bValue)); | |
| 134 } | |
| 135 canvas.setBitmap(null); | |
| 136 } catch (OutOfMemoryError e) { | |
| 137 Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas."
); | |
| 138 } | |
| 139 return bitmap; | |
| 140 } | |
| 141 | |
| 142 private static Bitmap getBitmapFromResourceId(Context context, int id, int d
ensity) { | |
| 143 Drawable drawable = ApiCompatibilityUtils.getDrawableForDensity( | |
| 144 context.getResources(), id, density); | |
| 145 | |
| 146 if (drawable instanceof BitmapDrawable) { | |
| 147 BitmapDrawable bd = (BitmapDrawable) drawable; | |
| 148 return bd.getBitmap(); | |
| 149 } | |
| 150 assert false : "The drawable was not a bitmap drawable as expected"; | |
| 151 return null; | |
| 152 } | |
| 153 | |
| 154 /** | |
| 155 * Use touch-icon or higher-resolution favicon and round the corners. | |
| 156 * @param context Context used to get resources. | |
| 157 * @param touchIcon Touch icon bitmap. | |
| 158 * @param canvas Canvas that holds the touch icon. | |
| 159 */ | |
| 160 private static void drawTouchIconToCanvas(Context context, Bitmap touchIcon,
Canvas canvas) { | |
| 161 Rect iconBounds = new Rect(0, 0, canvas.getWidth(), canvas.getHeight()); | |
| 162 Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight()); | |
| 163 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
| 164 paint.setFilterBitmap(true); | |
| 165 canvas.drawBitmap(touchIcon, src, iconBounds, paint); | |
| 166 // Convert dp to px. | |
| 167 int borderRadii = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNI
T_DIP, | |
| 168 TOUCHICON_BORDER_RADII_DP, context.getResources().getDisplayMetr
ics()); | |
| 169 Path path = new Path(); | |
| 170 path.setFillType(Path.FillType.INVERSE_WINDING); | |
| 171 RectF rect = new RectF(iconBounds); | |
| 172 rect.inset(INSET_DIMENSION_FOR_TOUCHICON, INSET_DIMENSION_FOR_TOUCHICON)
; | |
| 173 path.addRoundRect(rect, borderRadii, borderRadii, Path.Direction.CW); | |
| 174 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); | |
| 175 canvas.drawPath(path, paint); | |
| 176 } | |
| 177 | |
| 178 /** | |
| 179 * Draw document icon to canvas. | |
| 180 * @param context Context used to get bitmap resources. | |
| 181 * @param canvas Canvas that holds the document icon. | |
| 182 * @param iconDensity Density information to get bitmap resources. | |
| 183 * @param url URL of the bookmark. | |
| 184 * @param color Color for the document icon's folding and the bottom s
trip. | |
| 185 */ | |
| 186 private static void drawWidgetBackgroundToCanvas( | |
| 187 Context context, Canvas canvas, int iconDensity, String url, int col
or) { | |
| 188 Rect iconBounds = new Rect(0, 0, canvas.getWidth(), canvas.getHeight()); | |
| 189 Bitmap bookmarkWidgetBg = | |
| 190 getBitmapFromResourceId(context, R.mipmap.bookmark_widget_bg, ic
onDensity); | |
| 191 | |
| 192 Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG); | |
| 193 canvas.drawBitmap(bookmarkWidgetBg, null, iconBounds, paint); | |
| 194 | |
| 195 float density = (float) iconDensity / DisplayMetrics.DENSITY_MEDIUM; | |
| 196 int iconSize = (int) (GENERATED_ICON_SIZE_DP * density); | |
| 197 int iconRoundedEdge = (int) (GENERATED_ICON_ROUNDED_CORNERS_DP * density
); | |
| 198 int iconFontSize = (int) (GENERATED_ICON_FONT_SIZE_DP * density); | |
| 199 | |
| 200 RoundedIconGenerator generator = new RoundedIconGenerator( | |
| 201 iconSize, iconSize, iconRoundedEdge, color, iconFontSize); | |
| 202 Bitmap icon = generator.generateIconForUrl(url); | |
| 203 if (icon == null) return; // Bookmark URL does not have a domain. | |
| 204 canvas.drawBitmap(icon, iconBounds.exactCenterX() - icon.getWidth() / 2.
0f, | |
| 205 iconBounds.exactCenterY() - icon.getHeight() / 2.0f, null); | |
| 206 } | |
| 207 } | |
| OLD | NEW |