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