blob: 26e2072297923a1bb8449916ae0e7a6d0a968d43 [file] [log] [blame]
Ben Murdoch90b40262010-06-30 13:33:28 +01001/*
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
Kenny Rootc65991b2011-01-26 19:45:07 -080019import android.app.ActivityManager;
Leon Scroggins75630672011-01-13 17:56:15 -050020import android.app.AlertDialog;
21import android.content.ContentUris;
Ben Murdoch90b40262010-06-30 13:33:28 +010022import android.content.Context;
Leon Scroggins75630672011-01-13 17:56:15 -050023import android.content.DialogInterface;
Ben Murdoch90b40262010-06-30 13:33:28 +010024import android.content.Intent;
John Reck0ce8a942011-01-14 19:57:09 -080025import android.content.res.Resources;
kaiyizbf7043d2013-07-29 15:22:13 +080026import android.database.Cursor;
Ben Murdoch90b40262010-06-30 13:33:28 +010027import android.graphics.Bitmap;
Ben Murdoch90b40262010-06-30 13:33:28 +010028import android.graphics.Canvas;
29import android.graphics.Color;
30import android.graphics.Paint;
31import android.graphics.Path;
32import android.graphics.PorterDuff;
33import android.graphics.PorterDuffXfermode;
34import android.graphics.Rect;
35import android.graphics.RectF;
Kenny Rootc65991b2011-01-26 19:45:07 -080036import android.graphics.drawable.BitmapDrawable;
John Reck0ce8a942011-01-14 19:57:09 -080037import android.graphics.drawable.Drawable;
38import android.graphics.drawable.PaintDrawable;
Ben Murdoch90b40262010-06-30 13:33:28 +010039import android.net.Uri;
Leon Scroggins75630672011-01-13 17:56:15 -050040import android.os.Message;
Ben Murdoch90b40262010-06-30 13:33:28 +010041import android.provider.Browser;
John Reckc8490812010-11-22 14:15:36 -080042import android.provider.BrowserContract;
kaiyizbf7043d2013-07-29 15:22:13 +080043import android.provider.BrowserContract.Bookmarks;
Ben Murdoch90b40262010-06-30 13:33:28 +010044
John Reckdd03faf2010-12-13 11:03:16 -080045public class BookmarkUtils {
Ben Murdoch90b40262010-06-30 13:33:28 +010046 private final static String LOGTAG = "BookmarkUtils";
47
48 // XXX: There is no public string defining this intent so if Home changes the value, we
49 // have to update this string.
50 private static final String INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
51
52 enum BookmarkIconType {
53 ICON_INSTALLABLE_WEB_APP, // Icon for an installable web app (launches WebAppRuntime).
John Reck8e8e71c2010-12-16 11:17:54 -080054 ICON_HOME_SHORTCUT, // Icon for a shortcut on the home screen (launches Browser).
55 ICON_WIDGET,
56 }
Ben Murdoch90b40262010-06-30 13:33:28 +010057
58 /**
59 * Creates an icon to be associated with this bookmark. If available, the apple touch icon
60 * will be used, else we draw our own depending on the type of "bookmark" being created.
61 */
62 static Bitmap createIcon(Context context, Bitmap touchIcon, Bitmap favicon,
63 BookmarkIconType type) {
Kenny Rootc65991b2011-01-26 19:45:07 -080064 final ActivityManager am = (ActivityManager) context
65 .getSystemService(Context.ACTIVITY_SERVICE);
66 final int iconDimension = am.getLauncherLargeIconSize();
67 final int iconDensity = am.getLauncherLargeIconDensity();
68 return createIcon(context, touchIcon, favicon, type, iconDimension, iconDensity);
John Reck8e8e71c2010-12-16 11:17:54 -080069 }
70
John Reck0ce8a942011-01-14 19:57:09 -080071 static Drawable createListFaviconBackground(Context context) {
72 PaintDrawable faviconBackground = new PaintDrawable();
73 Resources res = context.getResources();
74 int padding = res.getDimensionPixelSize(R.dimen.list_favicon_padding);
75 faviconBackground.setPadding(padding, padding, padding, padding);
76 faviconBackground.getPaint().setColor(context.getResources()
77 .getColor(R.color.bookmarkListFaviconBackground));
78 faviconBackground.setCornerRadius(
79 res.getDimension(R.dimen.list_favicon_corner_radius));
80 return faviconBackground;
John Reck8e8e71c2010-12-16 11:17:54 -080081 }
82
83 private static Bitmap createIcon(Context context, Bitmap touchIcon,
Kenny Rootc65991b2011-01-26 19:45:07 -080084 Bitmap favicon, BookmarkIconType type, int iconDimension, int iconDensity) {
Ben Murdoch90b40262010-06-30 13:33:28 +010085 Bitmap bm = Bitmap.createBitmap(iconDimension, iconDimension, Bitmap.Config.ARGB_8888);
86 Canvas canvas = new Canvas(bm);
87 Rect iconBounds = new Rect(0, 0, bm.getWidth(), bm.getHeight());
88
89 // Use the apple-touch-icon if available
90 if (touchIcon != null) {
91 drawTouchIconToCanvas(touchIcon, canvas, iconBounds);
92 } else {
93 // No touch icon so create our own.
94 // Set the background based on the type of shortcut (either webapp or home shortcut).
Kenny Rootc65991b2011-01-26 19:45:07 -080095 Bitmap icon = getIconBackground(context, type, iconDensity);
Ben Murdoch90b40262010-06-30 13:33:28 +010096
97 if (icon != null) {
98 // Now draw the correct icon background into our new bitmap.
Ben Murdoch77835fd2010-11-29 15:41:15 +000099 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
100 canvas.drawBitmap(icon, null, iconBounds, p);
Ben Murdoch90b40262010-06-30 13:33:28 +0100101 }
102
103 // If we have a favicon, overlay it in a nice rounded white box on top of the
104 // background.
105 if (favicon != null) {
John Reck8e8e71c2010-12-16 11:17:54 -0800106 drawFaviconToCanvas(context, favicon, canvas, iconBounds, type);
Ben Murdoch90b40262010-06-30 13:33:28 +0100107 }
108 }
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -0700109 canvas.setBitmap(null);
Ben Murdoch90b40262010-06-30 13:33:28 +0100110 return bm;
111 }
112
113 /**
114 * Convenience method for creating an intent that will add a shortcut to the home screen.
115 */
116 static Intent createAddToHomeIntent(Context context, String url, String title,
117 Bitmap touchIcon, Bitmap favicon) {
118 Intent i = new Intent(INSTALL_SHORTCUT);
John Reckdb22ec42011-06-29 11:31:24 -0700119 Intent shortcutIntent = createShortcutIntent(url);
Ben Murdoch90b40262010-06-30 13:33:28 +0100120 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
121 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
122 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(context, touchIcon, favicon,
123 BookmarkIconType.ICON_HOME_SHORTCUT));
124
125 // Do not allow duplicate items
126 i.putExtra("duplicate", false);
127 return i;
128 }
129
John Reckdb22ec42011-06-29 11:31:24 -0700130 static Intent createShortcutIntent(String url) {
131 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
132 long urlHash = url.hashCode();
133 long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
134 shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID, Long.toString(uniqueId));
135 return shortcutIntent;
136 }
137
Kenny Rootc65991b2011-01-26 19:45:07 -0800138 private static Bitmap getIconBackground(Context context, BookmarkIconType type, int density) {
Ben Murdoch90b40262010-06-30 13:33:28 +0100139 if (type == BookmarkIconType.ICON_HOME_SHORTCUT) {
140 // Want to create a shortcut icon on the homescreen, so the icon
141 // background is the red bookmark.
Kenny Rootc65991b2011-01-26 19:45:07 -0800142 Drawable drawable = context.getResources().getDrawableForDensity(
143 R.mipmap.ic_launcher_shortcut_browser_bookmark, density);
144 if (drawable instanceof BitmapDrawable) {
145 BitmapDrawable bd = (BitmapDrawable) drawable;
146 return bd.getBitmap();
147 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100148 } else if (type == BookmarkIconType.ICON_INSTALLABLE_WEB_APP) {
149 // Use the web browser icon as the background for the icon for an installable
150 // web app.
Kenny Rootc65991b2011-01-26 19:45:07 -0800151 Drawable drawable = context.getResources().getDrawableForDensity(
152 R.mipmap.ic_launcher_browser, density);
153 if (drawable instanceof BitmapDrawable) {
154 BitmapDrawable bd = (BitmapDrawable) drawable;
155 return bd.getBitmap();
156 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100157 }
158 return null;
159 }
160
161 private static void drawTouchIconToCanvas(Bitmap touchIcon, Canvas canvas, Rect iconBounds) {
162 Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());
163
164 // Paint used for scaling the bitmap and drawing the rounded rect.
165 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
166 paint.setFilterBitmap(true);
167 canvas.drawBitmap(touchIcon, src, iconBounds, paint);
168
169 // Construct a path from a round rect. This will allow drawing with
170 // an inverse fill so we can punch a hole using the round rect.
171 Path path = new Path();
172 path.setFillType(Path.FillType.INVERSE_WINDING);
173 RectF rect = new RectF(iconBounds);
174 rect.inset(1, 1);
175 path.addRoundRect(rect, 8f, 8f, Path.Direction.CW);
176
177 // Reuse the paint and clear the outside of the rectangle.
178 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
179 canvas.drawPath(path, paint);
180 }
181
John Reck8e8e71c2010-12-16 11:17:54 -0800182 private static void drawFaviconToCanvas(Context context, Bitmap favicon,
183 Canvas canvas, Rect iconBounds, BookmarkIconType type) {
Ben Murdoch90b40262010-06-30 13:33:28 +0100184 // Make a Paint for the white background rectangle and for
185 // filtering the favicon.
186 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
187 p.setStyle(Paint.Style.FILL_AND_STROKE);
John Reck8e8e71c2010-12-16 11:17:54 -0800188 if (type == BookmarkIconType.ICON_WIDGET) {
189 p.setColor(context.getResources()
190 .getColor(R.color.bookmarkWidgetFaviconBackground));
191 } else {
192 p.setColor(Color.WHITE);
193 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100194
195 // Create a rectangle that is slightly wider than the favicon
Ben Murdoch77835fd2010-11-29 15:41:15 +0000196 int faviconDimension = context.getResources().getDimensionPixelSize(R.dimen.favicon_size);
John Reck8e8e71c2010-12-16 11:17:54 -0800197 int faviconPaddedRectDimension;
198 if (type == BookmarkIconType.ICON_WIDGET) {
199 faviconPaddedRectDimension = canvas.getWidth();
200 } else {
201 faviconPaddedRectDimension = context.getResources().getDimensionPixelSize(
202 R.dimen.favicon_padded_size);
203 }
Ben Murdoch77835fd2010-11-29 15:41:15 +0000204 float padding = (faviconPaddedRectDimension - faviconDimension) / 2;
205 final float x = iconBounds.exactCenterX() - (faviconPaddedRectDimension / 2);
John Reck8e8e71c2010-12-16 11:17:54 -0800206 float y = iconBounds.exactCenterY() - (faviconPaddedRectDimension / 2);
207 if (type != BookmarkIconType.ICON_WIDGET) {
208 // Note: Subtract from the y position since the box is
209 // slightly higher than center. Use padding since it is already
210 // device independent.
211 y -= padding;
212 }
Ben Murdoch77835fd2010-11-29 15:41:15 +0000213 RectF r = new RectF(x, y, x + faviconPaddedRectDimension, y + faviconPaddedRectDimension);
Ben Murdoch90b40262010-06-30 13:33:28 +0100214 // Draw a white rounded rectangle behind the favicon
John Reck8e8e71c2010-12-16 11:17:54 -0800215 canvas.drawRoundRect(r, 3, 3, p);
Ben Murdoch90b40262010-06-30 13:33:28 +0100216
217 // Draw the favicon in the same rectangle as the rounded
218 // rectangle but inset by the padding
219 // (results in a 16x16 favicon).
220 r.inset(padding, padding);
Ben Murdoch77835fd2010-11-29 15:41:15 +0000221 canvas.drawBitmap(favicon, null, r, null);
Ben Murdoch90b40262010-06-30 13:33:28 +0100222 }
223
John Reckc8490812010-11-22 14:15:36 -0800224 /* package */ static Uri getBookmarksUri(Context context) {
John Reck006ae182011-05-13 11:16:07 -0700225 return BrowserContract.Bookmarks.CONTENT_URI;
John Reck3dff1ce2010-12-10 11:13:57 -0800226 }
227
Leon Scroggins75630672011-01-13 17:56:15 -0500228 /**
229 * Show a confirmation dialog to remove a bookmark.
230 * @param id Id of the bookmark to remove
231 * @param title Title of the bookmark, to be displayed in the confirmation method.
232 * @param context Package Context for strings, dialog, ContentResolver
233 * @param msg Message to send if the bookmark is deleted.
234 */
235 static void displayRemoveBookmarkDialog( final long id, final String title,
236 final Context context, final Message msg) {
237
238 new AlertDialog.Builder(context)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200239 .setIconAttribute(android.R.attr.alertDialogIcon)
Leon Scroggins75630672011-01-13 17:56:15 -0500240 .setMessage(context.getString(R.string.delete_bookmark_warning,
241 title))
242 .setPositiveButton(R.string.ok,
243 new DialogInterface.OnClickListener() {
244 @Override
245 public void onClick(DialogInterface dialog, int whichButton) {
246 if (msg != null) {
247 msg.sendToTarget();
248 }
249 Runnable runnable = new Runnable(){
250 @Override
251 public void run() {
kaiyizbf7043d2013-07-29 15:22:13 +0800252 removeBookmarkOrFolder(context, id);
Leon Scroggins75630672011-01-13 17:56:15 -0500253 }
254 };
255 new Thread(runnable).start();
256 }
257 })
258 .setNegativeButton(R.string.cancel, null)
259 .show();
260 }
kaiyizbf7043d2013-07-29 15:22:13 +0800261
262 /**
263 * Remove the bookmark or folder.Remove all sub folders and bookmarks under current folder.
264 * @param context Package Context for strings, dialog, ContentResolver.
265 * @param id Id of the bookmark to remove.
266 */
267 private static void removeBookmarkOrFolder(Context context, long id) {
268 Cursor cursor = context.getContentResolver().query(Bookmarks.CONTENT_URI,
269 new String[] {Bookmarks._ID},
270 Bookmarks.PARENT + "=?",
271 new String[] {Long.toString(id)},
272 null);
273
274 if (cursor != null) {
275 try {
276 if (cursor.moveToFirst()) {
277 do {
278 removeBookmarkOrFolder(context,
279 cursor.getLong(cursor.getColumnIndex(Bookmarks._ID)));
280 } while (cursor.moveToNext());
281 }
282 } catch (Exception e) {
283 e.printStackTrace();
284 } finally {
285 cursor.close();
286 }
287 }
288
289 context.getContentResolver().delete(
290 ContentUris.withAppendedId(Bookmarks.CONTENT_URI, id), null, null);
291 }
John Reck3dff1ce2010-12-10 11:13:57 -0800292}