BookmarkUtils.java revision c65991b2d2bf0cfdaa922cb3073aed498b929447
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.browser; 18 19import android.app.ActivityManager; 20import android.app.AlertDialog; 21import android.content.ContentUris; 22import android.content.Context; 23import android.content.DialogInterface; 24import android.content.Intent; 25import android.content.SharedPreferences; 26import android.content.res.Resources; 27import android.graphics.Bitmap; 28import android.graphics.BitmapFactory; 29import android.graphics.Canvas; 30import android.graphics.Color; 31import android.graphics.Paint; 32import android.graphics.Path; 33import android.graphics.PorterDuff; 34import android.graphics.PorterDuffXfermode; 35import android.graphics.Rect; 36import android.graphics.RectF; 37import android.graphics.drawable.BitmapDrawable; 38import android.graphics.drawable.Drawable; 39import android.graphics.drawable.PaintDrawable; 40import android.net.Uri; 41import android.os.Message; 42import android.preference.PreferenceManager; 43import android.provider.Browser; 44import android.provider.BrowserContract; 45import android.util.DisplayMetrics; 46 47public class BookmarkUtils { 48 private final static String LOGTAG = "BookmarkUtils"; 49 50 // XXX: There is no public string defining this intent so if Home changes the value, we 51 // have to update this string. 52 private static final String INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT"; 53 54 enum BookmarkIconType { 55 ICON_INSTALLABLE_WEB_APP, // Icon for an installable web app (launches WebAppRuntime). 56 ICON_HOME_SHORTCUT, // Icon for a shortcut on the home screen (launches Browser). 57 ICON_WIDGET, 58 } 59 60 /** 61 * Creates an icon to be associated with this bookmark. If available, the apple touch icon 62 * will be used, else we draw our own depending on the type of "bookmark" being created. 63 */ 64 static Bitmap createIcon(Context context, Bitmap touchIcon, Bitmap favicon, 65 BookmarkIconType type) { 66 final ActivityManager am = (ActivityManager) context 67 .getSystemService(Context.ACTIVITY_SERVICE); 68 final int iconDimension = am.getLauncherLargeIconSize(); 69 final int iconDensity = am.getLauncherLargeIconDensity(); 70 return createIcon(context, touchIcon, favicon, type, iconDimension, iconDensity); 71 } 72 73 static Drawable createListFaviconBackground(Context context) { 74 PaintDrawable faviconBackground = new PaintDrawable(); 75 Resources res = context.getResources(); 76 int padding = res.getDimensionPixelSize(R.dimen.list_favicon_padding); 77 faviconBackground.setPadding(padding, padding, padding, padding); 78 faviconBackground.getPaint().setColor(context.getResources() 79 .getColor(R.color.bookmarkListFaviconBackground)); 80 faviconBackground.setCornerRadius( 81 res.getDimension(R.dimen.list_favicon_corner_radius)); 82 return faviconBackground; 83 } 84 85 private static Bitmap createIcon(Context context, Bitmap touchIcon, 86 Bitmap favicon, BookmarkIconType type, int iconDimension, int iconDensity) { 87 Bitmap bm = Bitmap.createBitmap(iconDimension, iconDimension, Bitmap.Config.ARGB_8888); 88 Canvas canvas = new Canvas(bm); 89 Rect iconBounds = new Rect(0, 0, bm.getWidth(), bm.getHeight()); 90 91 // Use the apple-touch-icon if available 92 if (touchIcon != null) { 93 drawTouchIconToCanvas(touchIcon, canvas, iconBounds); 94 } else { 95 // No touch icon so create our own. 96 // Set the background based on the type of shortcut (either webapp or home shortcut). 97 Bitmap icon = getIconBackground(context, type, iconDensity); 98 99 if (icon != null) { 100 // Now draw the correct icon background into our new bitmap. 101 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); 102 canvas.drawBitmap(icon, null, iconBounds, p); 103 } 104 105 // If we have a favicon, overlay it in a nice rounded white box on top of the 106 // background. 107 if (favicon != null) { 108 drawFaviconToCanvas(context, favicon, canvas, iconBounds, type); 109 } 110 } 111 return bm; 112 } 113 114 /** 115 * Convenience method for creating an intent that will add a shortcut to the home screen. 116 */ 117 static Intent createAddToHomeIntent(Context context, String url, String title, 118 Bitmap touchIcon, Bitmap favicon) { 119 Intent i = new Intent(INSTALL_SHORTCUT); 120 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 121 long urlHash = url.hashCode(); 122 long uniqueId = (urlHash << 32) | shortcutIntent.hashCode(); 123 shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID, Long.toString(uniqueId)); 124 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 125 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); 126 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(context, touchIcon, favicon, 127 BookmarkIconType.ICON_HOME_SHORTCUT)); 128 129 // Do not allow duplicate items 130 i.putExtra("duplicate", false); 131 return i; 132 } 133 134 private static Bitmap getIconBackground(Context context, BookmarkIconType type, int density) { 135 if (type == BookmarkIconType.ICON_HOME_SHORTCUT) { 136 // Want to create a shortcut icon on the homescreen, so the icon 137 // background is the red bookmark. 138 Drawable drawable = context.getResources().getDrawableForDensity( 139 R.mipmap.ic_launcher_shortcut_browser_bookmark, density); 140 if (drawable instanceof BitmapDrawable) { 141 BitmapDrawable bd = (BitmapDrawable) drawable; 142 return bd.getBitmap(); 143 } 144 } else if (type == BookmarkIconType.ICON_INSTALLABLE_WEB_APP) { 145 // Use the web browser icon as the background for the icon for an installable 146 // web app. 147 Drawable drawable = context.getResources().getDrawableForDensity( 148 R.mipmap.ic_launcher_browser, density); 149 if (drawable instanceof BitmapDrawable) { 150 BitmapDrawable bd = (BitmapDrawable) drawable; 151 return bd.getBitmap(); 152 } 153 } 154 return null; 155 } 156 157 private static void drawTouchIconToCanvas(Bitmap touchIcon, Canvas canvas, Rect iconBounds) { 158 Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight()); 159 160 // Paint used for scaling the bitmap and drawing the rounded rect. 161 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 162 paint.setFilterBitmap(true); 163 canvas.drawBitmap(touchIcon, src, iconBounds, paint); 164 165 // Construct a path from a round rect. This will allow drawing with 166 // an inverse fill so we can punch a hole using the round rect. 167 Path path = new Path(); 168 path.setFillType(Path.FillType.INVERSE_WINDING); 169 RectF rect = new RectF(iconBounds); 170 rect.inset(1, 1); 171 path.addRoundRect(rect, 8f, 8f, Path.Direction.CW); 172 173 // Reuse the paint and clear the outside of the rectangle. 174 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 175 canvas.drawPath(path, paint); 176 } 177 178 private static void drawFaviconToCanvas(Context context, Bitmap favicon, 179 Canvas canvas, Rect iconBounds, BookmarkIconType type) { 180 // Make a Paint for the white background rectangle and for 181 // filtering the favicon. 182 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); 183 p.setStyle(Paint.Style.FILL_AND_STROKE); 184 if (type == BookmarkIconType.ICON_WIDGET) { 185 p.setColor(context.getResources() 186 .getColor(R.color.bookmarkWidgetFaviconBackground)); 187 } else { 188 p.setColor(Color.WHITE); 189 } 190 191 // Create a rectangle that is slightly wider than the favicon 192 int faviconDimension = context.getResources().getDimensionPixelSize(R.dimen.favicon_size); 193 int faviconPaddedRectDimension; 194 if (type == BookmarkIconType.ICON_WIDGET) { 195 faviconPaddedRectDimension = canvas.getWidth(); 196 } else { 197 faviconPaddedRectDimension = context.getResources().getDimensionPixelSize( 198 R.dimen.favicon_padded_size); 199 } 200 float padding = (faviconPaddedRectDimension - faviconDimension) / 2; 201 final float x = iconBounds.exactCenterX() - (faviconPaddedRectDimension / 2); 202 float y = iconBounds.exactCenterY() - (faviconPaddedRectDimension / 2); 203 if (type != BookmarkIconType.ICON_WIDGET) { 204 // Note: Subtract from the y position since the box is 205 // slightly higher than center. Use padding since it is already 206 // device independent. 207 y -= padding; 208 } 209 RectF r = new RectF(x, y, x + faviconPaddedRectDimension, y + faviconPaddedRectDimension); 210 // Draw a white rounded rectangle behind the favicon 211 canvas.drawRoundRect(r, 3, 3, p); 212 213 // Draw the favicon in the same rectangle as the rounded 214 // rectangle but inset by the padding 215 // (results in a 16x16 favicon). 216 r.inset(padding, padding); 217 canvas.drawBitmap(favicon, null, r, null); 218 } 219 220 /* package */ static Uri getBookmarksUri(Context context) { 221 return addAccountInfo(context, 222 BrowserContract.Bookmarks.CONTENT_URI.buildUpon()).build(); 223 } 224 225 public static Uri.Builder addAccountInfo(Context context, Uri.Builder ub) { 226 Uri uri = BrowserContract.Bookmarks.CONTENT_URI; 227 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 228 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null); 229 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null); 230 ub.appendQueryParameter( 231 BrowserContract.Bookmarks.PARAM_ACCOUNT_NAME,accountName); 232 ub.appendQueryParameter( 233 BrowserContract.Bookmarks.PARAM_ACCOUNT_TYPE, accountType); 234 return ub; 235 } 236 237 /** 238 * Show a confirmation dialog to remove a bookmark. 239 * @param id Id of the bookmark to remove 240 * @param title Title of the bookmark, to be displayed in the confirmation method. 241 * @param context Package Context for strings, dialog, ContentResolver 242 * @param msg Message to send if the bookmark is deleted. 243 */ 244 static void displayRemoveBookmarkDialog( final long id, final String title, 245 final Context context, final Message msg) { 246 247 new AlertDialog.Builder(context) 248 .setTitle(R.string.delete_bookmark) 249 .setIcon(android.R.drawable.ic_dialog_alert) 250 .setMessage(context.getString(R.string.delete_bookmark_warning, 251 title)) 252 .setPositiveButton(R.string.ok, 253 new DialogInterface.OnClickListener() { 254 @Override 255 public void onClick(DialogInterface dialog, int whichButton) { 256 if (msg != null) { 257 msg.sendToTarget(); 258 } 259 Runnable runnable = new Runnable(){ 260 @Override 261 public void run() { 262 Uri uri = ContentUris.withAppendedId( 263 BrowserContract.Bookmarks.CONTENT_URI, 264 id); 265 context.getContentResolver().delete(uri, null, null); 266 } 267 }; 268 new Thread(runnable).start(); 269 } 270 }) 271 .setNegativeButton(R.string.cancel, null) 272 .show(); 273 } 274} 275