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 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 18 | |
Kenny Root | c65991b | 2011-01-26 19:45:07 -0800 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 20 | import android.app.AlertDialog; |
| 21 | import android.content.ContentUris; |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 22 | import android.content.Context; |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 23 | import android.content.DialogInterface; |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 24 | import android.content.Intent; |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame] | 25 | import android.content.res.Resources; |
kaiyiz | bf7043d | 2013-07-29 15:22:13 +0800 | [diff] [blame] | 26 | import android.database.Cursor; |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 27 | import android.graphics.Bitmap; |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 28 | import android.graphics.Canvas; |
| 29 | import android.graphics.Color; |
| 30 | import android.graphics.Paint; |
| 31 | import android.graphics.Path; |
| 32 | import android.graphics.PorterDuff; |
| 33 | import android.graphics.PorterDuffXfermode; |
| 34 | import android.graphics.Rect; |
| 35 | import android.graphics.RectF; |
Kenny Root | c65991b | 2011-01-26 19:45:07 -0800 | [diff] [blame] | 36 | import android.graphics.drawable.BitmapDrawable; |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame] | 37 | import android.graphics.drawable.Drawable; |
| 38 | import android.graphics.drawable.PaintDrawable; |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 39 | import android.net.Uri; |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 40 | import android.os.Message; |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 41 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 42 | import com.android.browser.R; |
Bijan Amirzada | 3f04dc7 | 2014-06-25 11:48:36 -0700 | [diff] [blame] | 43 | import com.android.browser.platformsupport.Browser; |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 44 | import com.android.browser.platformsupport.BrowserContract; |
| 45 | import com.android.browser.platformsupport.BrowserContract.Bookmarks; |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 46 | |
John Reck | dd03faf | 2010-12-13 11:03:16 -0800 | [diff] [blame] | 47 | public class BookmarkUtils { |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 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). |
John Reck | 8e8e71c | 2010-12-16 11:17:54 -0800 | [diff] [blame] | 56 | ICON_HOME_SHORTCUT, // Icon for a shortcut on the home screen (launches Browser). |
| 57 | ICON_WIDGET, |
| 58 | } |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 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) { |
Kenny Root | c65991b | 2011-01-26 19:45:07 -0800 | [diff] [blame] | 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); |
John Reck | 8e8e71c | 2010-12-16 11:17:54 -0800 | [diff] [blame] | 71 | } |
| 72 | |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame] | 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; |
John Reck | 8e8e71c | 2010-12-16 11:17:54 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | private static Bitmap createIcon(Context context, Bitmap touchIcon, |
Kenny Root | c65991b | 2011-01-26 19:45:07 -0800 | [diff] [blame] | 86 | Bitmap favicon, BookmarkIconType type, int iconDimension, int iconDensity) { |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 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). |
Kenny Root | c65991b | 2011-01-26 19:45:07 -0800 | [diff] [blame] | 97 | Bitmap icon = getIconBackground(context, type, iconDensity); |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 98 | |
| 99 | if (icon != null) { |
| 100 | // Now draw the correct icon background into our new bitmap. |
Ben Murdoch | 77835fd | 2010-11-29 15:41:15 +0000 | [diff] [blame] | 101 | Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); |
| 102 | canvas.drawBitmap(icon, null, iconBounds, p); |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 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) { |
John Reck | 8e8e71c | 2010-12-16 11:17:54 -0800 | [diff] [blame] | 108 | drawFaviconToCanvas(context, favicon, canvas, iconBounds, type); |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 109 | } |
| 110 | } |
Dianne Hackborn | 43cfe8a | 2011-08-02 16:59:35 -0700 | [diff] [blame] | 111 | canvas.setBitmap(null); |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 112 | return bm; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Convenience method for creating an intent that will add a shortcut to the home screen. |
| 117 | */ |
| 118 | static Intent createAddToHomeIntent(Context context, String url, String title, |
| 119 | Bitmap touchIcon, Bitmap favicon) { |
| 120 | Intent i = new Intent(INSTALL_SHORTCUT); |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 121 | Intent shortcutIntent = createShortcutIntent(url); |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 122 | i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); |
| 123 | i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); |
| 124 | i.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(context, touchIcon, favicon, |
| 125 | BookmarkIconType.ICON_HOME_SHORTCUT)); |
| 126 | |
| 127 | // Do not allow duplicate items |
| 128 | i.putExtra("duplicate", false); |
| 129 | return i; |
| 130 | } |
| 131 | |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 132 | static Intent createShortcutIntent(String url) { |
| 133 | Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
| 134 | long urlHash = url.hashCode(); |
| 135 | long uniqueId = (urlHash << 32) | shortcutIntent.hashCode(); |
| 136 | shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID, Long.toString(uniqueId)); |
| 137 | return shortcutIntent; |
| 138 | } |
| 139 | |
Kenny Root | c65991b | 2011-01-26 19:45:07 -0800 | [diff] [blame] | 140 | private static Bitmap getIconBackground(Context context, BookmarkIconType type, int density) { |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 141 | if (type == BookmarkIconType.ICON_HOME_SHORTCUT) { |
| 142 | // Want to create a shortcut icon on the homescreen, so the icon |
| 143 | // background is the red bookmark. |
Kenny Root | c65991b | 2011-01-26 19:45:07 -0800 | [diff] [blame] | 144 | Drawable drawable = context.getResources().getDrawableForDensity( |
| 145 | R.mipmap.ic_launcher_shortcut_browser_bookmark, density); |
| 146 | if (drawable instanceof BitmapDrawable) { |
| 147 | BitmapDrawable bd = (BitmapDrawable) drawable; |
| 148 | return bd.getBitmap(); |
| 149 | } |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 150 | } else if (type == BookmarkIconType.ICON_INSTALLABLE_WEB_APP) { |
| 151 | // Use the web browser icon as the background for the icon for an installable |
| 152 | // web app. |
Kenny Root | c65991b | 2011-01-26 19:45:07 -0800 | [diff] [blame] | 153 | Drawable drawable = context.getResources().getDrawableForDensity( |
| 154 | R.mipmap.ic_launcher_browser, density); |
| 155 | if (drawable instanceof BitmapDrawable) { |
| 156 | BitmapDrawable bd = (BitmapDrawable) drawable; |
| 157 | return bd.getBitmap(); |
| 158 | } |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 159 | } |
| 160 | return null; |
| 161 | } |
| 162 | |
| 163 | private static void drawTouchIconToCanvas(Bitmap touchIcon, Canvas canvas, Rect iconBounds) { |
| 164 | Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight()); |
| 165 | |
| 166 | // Paint used for scaling the bitmap and drawing the rounded rect. |
| 167 | Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 168 | paint.setFilterBitmap(true); |
| 169 | canvas.drawBitmap(touchIcon, src, iconBounds, paint); |
| 170 | |
| 171 | // Construct a path from a round rect. This will allow drawing with |
| 172 | // an inverse fill so we can punch a hole using the round rect. |
| 173 | Path path = new Path(); |
| 174 | path.setFillType(Path.FillType.INVERSE_WINDING); |
| 175 | RectF rect = new RectF(iconBounds); |
| 176 | rect.inset(1, 1); |
| 177 | path.addRoundRect(rect, 8f, 8f, Path.Direction.CW); |
| 178 | |
| 179 | // Reuse the paint and clear the outside of the rectangle. |
| 180 | paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); |
| 181 | canvas.drawPath(path, paint); |
| 182 | } |
| 183 | |
John Reck | 8e8e71c | 2010-12-16 11:17:54 -0800 | [diff] [blame] | 184 | private static void drawFaviconToCanvas(Context context, Bitmap favicon, |
| 185 | Canvas canvas, Rect iconBounds, BookmarkIconType type) { |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 186 | // Make a Paint for the white background rectangle and for |
| 187 | // filtering the favicon. |
| 188 | Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); |
| 189 | p.setStyle(Paint.Style.FILL_AND_STROKE); |
John Reck | 8e8e71c | 2010-12-16 11:17:54 -0800 | [diff] [blame] | 190 | if (type == BookmarkIconType.ICON_WIDGET) { |
| 191 | p.setColor(context.getResources() |
| 192 | .getColor(R.color.bookmarkWidgetFaviconBackground)); |
| 193 | } else { |
| 194 | p.setColor(Color.WHITE); |
| 195 | } |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 196 | |
| 197 | // Create a rectangle that is slightly wider than the favicon |
Ben Murdoch | 77835fd | 2010-11-29 15:41:15 +0000 | [diff] [blame] | 198 | int faviconDimension = context.getResources().getDimensionPixelSize(R.dimen.favicon_size); |
John Reck | 8e8e71c | 2010-12-16 11:17:54 -0800 | [diff] [blame] | 199 | int faviconPaddedRectDimension; |
| 200 | if (type == BookmarkIconType.ICON_WIDGET) { |
| 201 | faviconPaddedRectDimension = canvas.getWidth(); |
| 202 | } else { |
| 203 | faviconPaddedRectDimension = context.getResources().getDimensionPixelSize( |
| 204 | R.dimen.favicon_padded_size); |
| 205 | } |
Ben Murdoch | 77835fd | 2010-11-29 15:41:15 +0000 | [diff] [blame] | 206 | float padding = (faviconPaddedRectDimension - faviconDimension) / 2; |
| 207 | final float x = iconBounds.exactCenterX() - (faviconPaddedRectDimension / 2); |
John Reck | 8e8e71c | 2010-12-16 11:17:54 -0800 | [diff] [blame] | 208 | float y = iconBounds.exactCenterY() - (faviconPaddedRectDimension / 2); |
| 209 | if (type != BookmarkIconType.ICON_WIDGET) { |
| 210 | // Note: Subtract from the y position since the box is |
| 211 | // slightly higher than center. Use padding since it is already |
| 212 | // device independent. |
| 213 | y -= padding; |
| 214 | } |
Ben Murdoch | 77835fd | 2010-11-29 15:41:15 +0000 | [diff] [blame] | 215 | RectF r = new RectF(x, y, x + faviconPaddedRectDimension, y + faviconPaddedRectDimension); |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 216 | // Draw a white rounded rectangle behind the favicon |
John Reck | 8e8e71c | 2010-12-16 11:17:54 -0800 | [diff] [blame] | 217 | canvas.drawRoundRect(r, 3, 3, p); |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 218 | |
| 219 | // Draw the favicon in the same rectangle as the rounded |
| 220 | // rectangle but inset by the padding |
| 221 | // (results in a 16x16 favicon). |
| 222 | r.inset(padding, padding); |
Ben Murdoch | 77835fd | 2010-11-29 15:41:15 +0000 | [diff] [blame] | 223 | canvas.drawBitmap(favicon, null, r, null); |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 224 | } |
| 225 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 226 | /* package */ static Uri getBookmarksUri(Context context) { |
John Reck | 006ae18 | 2011-05-13 11:16:07 -0700 | [diff] [blame] | 227 | return BrowserContract.Bookmarks.CONTENT_URI; |
John Reck | 3dff1ce | 2010-12-10 11:13:57 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 230 | /** |
| 231 | * Show a confirmation dialog to remove a bookmark. |
| 232 | * @param id Id of the bookmark to remove |
| 233 | * @param title Title of the bookmark, to be displayed in the confirmation method. |
| 234 | * @param context Package Context for strings, dialog, ContentResolver |
| 235 | * @param msg Message to send if the bookmark is deleted. |
| 236 | */ |
| 237 | static void displayRemoveBookmarkDialog( final long id, final String title, |
Pankaj Garg | 634bf43 | 2015-07-13 09:54:21 -0700 | [diff] [blame^] | 238 | final Context context, final Message msg, boolean is_folder) { |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 239 | |
| 240 | new AlertDialog.Builder(context) |
Björn Lundén | 2aa8ba2 | 2012-05-31 23:05:56 +0200 | [diff] [blame] | 241 | .setIconAttribute(android.R.attr.alertDialogIcon) |
Pankaj Garg | 634bf43 | 2015-07-13 09:54:21 -0700 | [diff] [blame^] | 242 | .setMessage(is_folder ? |
| 243 | context.getString(R.string.delete_folder_warning, title) : |
| 244 | context.getString(R.string.delete_bookmark_warning, title)) |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 245 | .setPositiveButton(R.string.ok, |
| 246 | new DialogInterface.OnClickListener() { |
| 247 | @Override |
| 248 | public void onClick(DialogInterface dialog, int whichButton) { |
| 249 | if (msg != null) { |
| 250 | msg.sendToTarget(); |
| 251 | } |
Pankaj Garg | 634bf43 | 2015-07-13 09:54:21 -0700 | [diff] [blame^] | 252 | Runnable runnable = new Runnable() { |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 253 | @Override |
| 254 | public void run() { |
kaiyiz | bf7043d | 2013-07-29 15:22:13 +0800 | [diff] [blame] | 255 | removeBookmarkOrFolder(context, id); |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 256 | } |
| 257 | }; |
| 258 | new Thread(runnable).start(); |
| 259 | } |
| 260 | }) |
| 261 | .setNegativeButton(R.string.cancel, null) |
| 262 | .show(); |
| 263 | } |
kaiyiz | bf7043d | 2013-07-29 15:22:13 +0800 | [diff] [blame] | 264 | |
| 265 | /** |
| 266 | * Remove the bookmark or folder.Remove all sub folders and bookmarks under current folder. |
| 267 | * @param context Package Context for strings, dialog, ContentResolver. |
| 268 | * @param id Id of the bookmark to remove. |
| 269 | */ |
| 270 | private static void removeBookmarkOrFolder(Context context, long id) { |
| 271 | Cursor cursor = context.getContentResolver().query(Bookmarks.CONTENT_URI, |
| 272 | new String[] {Bookmarks._ID}, |
| 273 | Bookmarks.PARENT + "=?", |
| 274 | new String[] {Long.toString(id)}, |
| 275 | null); |
| 276 | |
| 277 | if (cursor != null) { |
| 278 | try { |
| 279 | if (cursor.moveToFirst()) { |
| 280 | do { |
| 281 | removeBookmarkOrFolder(context, |
| 282 | cursor.getLong(cursor.getColumnIndex(Bookmarks._ID))); |
| 283 | } while (cursor.moveToNext()); |
| 284 | } |
| 285 | } catch (Exception e) { |
| 286 | e.printStackTrace(); |
| 287 | } finally { |
| 288 | cursor.close(); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | context.getContentResolver().delete( |
| 293 | ContentUris.withAppendedId(Bookmarks.CONTENT_URI, id), null, null); |
| 294 | } |
John Reck | 3dff1ce | 2010-12-10 11:13:57 -0800 | [diff] [blame] | 295 | } |