blob: 2e96613fbc4d269e451b42169f1440ff32ba8edd [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;
Ben Murdoch90b40262010-06-30 13:33:28 +010026import android.graphics.Bitmap;
Ben Murdoch90b40262010-06-30 13:33:28 +010027import android.graphics.Canvas;
28import android.graphics.Color;
29import android.graphics.Paint;
30import android.graphics.Path;
31import android.graphics.PorterDuff;
32import android.graphics.PorterDuffXfermode;
33import android.graphics.Rect;
34import android.graphics.RectF;
Kenny Rootc65991b2011-01-26 19:45:07 -080035import android.graphics.drawable.BitmapDrawable;
John Reck0ce8a942011-01-14 19:57:09 -080036import android.graphics.drawable.Drawable;
37import android.graphics.drawable.PaintDrawable;
Ben Murdoch90b40262010-06-30 13:33:28 +010038import android.net.Uri;
Leon Scroggins75630672011-01-13 17:56:15 -050039import android.os.Message;
Ben Murdoch90b40262010-06-30 13:33:28 +010040import android.provider.Browser;
John Reckc8490812010-11-22 14:15:36 -080041import android.provider.BrowserContract;
Ben Murdoch90b40262010-06-30 13:33:28 +010042
John Reckdd03faf2010-12-13 11:03:16 -080043public class BookmarkUtils {
Ben Murdoch90b40262010-06-30 13:33:28 +010044 private final static String LOGTAG = "BookmarkUtils";
45
46 // XXX: There is no public string defining this intent so if Home changes the value, we
47 // have to update this string.
48 private static final String INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
49
50 enum BookmarkIconType {
51 ICON_INSTALLABLE_WEB_APP, // Icon for an installable web app (launches WebAppRuntime).
John Reck8e8e71c2010-12-16 11:17:54 -080052 ICON_HOME_SHORTCUT, // Icon for a shortcut on the home screen (launches Browser).
53 ICON_WIDGET,
54 }
Ben Murdoch90b40262010-06-30 13:33:28 +010055
56 /**
57 * Creates an icon to be associated with this bookmark. If available, the apple touch icon
58 * will be used, else we draw our own depending on the type of "bookmark" being created.
59 */
60 static Bitmap createIcon(Context context, Bitmap touchIcon, Bitmap favicon,
61 BookmarkIconType type) {
Kenny Rootc65991b2011-01-26 19:45:07 -080062 final ActivityManager am = (ActivityManager) context
63 .getSystemService(Context.ACTIVITY_SERVICE);
64 final int iconDimension = am.getLauncherLargeIconSize();
65 final int iconDensity = am.getLauncherLargeIconDensity();
66 return createIcon(context, touchIcon, favicon, type, iconDimension, iconDensity);
John Reck8e8e71c2010-12-16 11:17:54 -080067 }
68
John Reck0ce8a942011-01-14 19:57:09 -080069 static Drawable createListFaviconBackground(Context context) {
70 PaintDrawable faviconBackground = new PaintDrawable();
71 Resources res = context.getResources();
72 int padding = res.getDimensionPixelSize(R.dimen.list_favicon_padding);
73 faviconBackground.setPadding(padding, padding, padding, padding);
74 faviconBackground.getPaint().setColor(context.getResources()
75 .getColor(R.color.bookmarkListFaviconBackground));
76 faviconBackground.setCornerRadius(
77 res.getDimension(R.dimen.list_favicon_corner_radius));
78 return faviconBackground;
John Reck8e8e71c2010-12-16 11:17:54 -080079 }
80
81 private static Bitmap createIcon(Context context, Bitmap touchIcon,
Kenny Rootc65991b2011-01-26 19:45:07 -080082 Bitmap favicon, BookmarkIconType type, int iconDimension, int iconDensity) {
Ben Murdoch90b40262010-06-30 13:33:28 +010083 Bitmap bm = Bitmap.createBitmap(iconDimension, iconDimension, Bitmap.Config.ARGB_8888);
84 Canvas canvas = new Canvas(bm);
85 Rect iconBounds = new Rect(0, 0, bm.getWidth(), bm.getHeight());
86
87 // Use the apple-touch-icon if available
88 if (touchIcon != null) {
89 drawTouchIconToCanvas(touchIcon, canvas, iconBounds);
90 } else {
91 // No touch icon so create our own.
92 // Set the background based on the type of shortcut (either webapp or home shortcut).
Kenny Rootc65991b2011-01-26 19:45:07 -080093 Bitmap icon = getIconBackground(context, type, iconDensity);
Ben Murdoch90b40262010-06-30 13:33:28 +010094
95 if (icon != null) {
96 // Now draw the correct icon background into our new bitmap.
Ben Murdoch77835fd2010-11-29 15:41:15 +000097 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
98 canvas.drawBitmap(icon, null, iconBounds, p);
Ben Murdoch90b40262010-06-30 13:33:28 +010099 }
100
101 // If we have a favicon, overlay it in a nice rounded white box on top of the
102 // background.
103 if (favicon != null) {
John Reck8e8e71c2010-12-16 11:17:54 -0800104 drawFaviconToCanvas(context, favicon, canvas, iconBounds, type);
Ben Murdoch90b40262010-06-30 13:33:28 +0100105 }
106 }
107 return bm;
108 }
109
110 /**
111 * Convenience method for creating an intent that will add a shortcut to the home screen.
112 */
113 static Intent createAddToHomeIntent(Context context, String url, String title,
114 Bitmap touchIcon, Bitmap favicon) {
115 Intent i = new Intent(INSTALL_SHORTCUT);
John Reckdb22ec42011-06-29 11:31:24 -0700116 Intent shortcutIntent = createShortcutIntent(url);
Ben Murdoch90b40262010-06-30 13:33:28 +0100117 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
118 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
119 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(context, touchIcon, favicon,
120 BookmarkIconType.ICON_HOME_SHORTCUT));
121
122 // Do not allow duplicate items
123 i.putExtra("duplicate", false);
124 return i;
125 }
126
John Reckdb22ec42011-06-29 11:31:24 -0700127 static Intent createShortcutIntent(String url) {
128 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
129 long urlHash = url.hashCode();
130 long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
131 shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID, Long.toString(uniqueId));
132 return shortcutIntent;
133 }
134
Kenny Rootc65991b2011-01-26 19:45:07 -0800135 private static Bitmap getIconBackground(Context context, BookmarkIconType type, int density) {
Ben Murdoch90b40262010-06-30 13:33:28 +0100136 if (type == BookmarkIconType.ICON_HOME_SHORTCUT) {
137 // Want to create a shortcut icon on the homescreen, so the icon
138 // background is the red bookmark.
Kenny Rootc65991b2011-01-26 19:45:07 -0800139 Drawable drawable = context.getResources().getDrawableForDensity(
140 R.mipmap.ic_launcher_shortcut_browser_bookmark, density);
141 if (drawable instanceof BitmapDrawable) {
142 BitmapDrawable bd = (BitmapDrawable) drawable;
143 return bd.getBitmap();
144 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100145 } else if (type == BookmarkIconType.ICON_INSTALLABLE_WEB_APP) {
146 // Use the web browser icon as the background for the icon for an installable
147 // web app.
Kenny Rootc65991b2011-01-26 19:45:07 -0800148 Drawable drawable = context.getResources().getDrawableForDensity(
149 R.mipmap.ic_launcher_browser, density);
150 if (drawable instanceof BitmapDrawable) {
151 BitmapDrawable bd = (BitmapDrawable) drawable;
152 return bd.getBitmap();
153 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100154 }
155 return null;
156 }
157
158 private static void drawTouchIconToCanvas(Bitmap touchIcon, Canvas canvas, Rect iconBounds) {
159 Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());
160
161 // Paint used for scaling the bitmap and drawing the rounded rect.
162 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
163 paint.setFilterBitmap(true);
164 canvas.drawBitmap(touchIcon, src, iconBounds, paint);
165
166 // Construct a path from a round rect. This will allow drawing with
167 // an inverse fill so we can punch a hole using the round rect.
168 Path path = new Path();
169 path.setFillType(Path.FillType.INVERSE_WINDING);
170 RectF rect = new RectF(iconBounds);
171 rect.inset(1, 1);
172 path.addRoundRect(rect, 8f, 8f, Path.Direction.CW);
173
174 // Reuse the paint and clear the outside of the rectangle.
175 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
176 canvas.drawPath(path, paint);
177 }
178
John Reck8e8e71c2010-12-16 11:17:54 -0800179 private static void drawFaviconToCanvas(Context context, Bitmap favicon,
180 Canvas canvas, Rect iconBounds, BookmarkIconType type) {
Ben Murdoch90b40262010-06-30 13:33:28 +0100181 // Make a Paint for the white background rectangle and for
182 // filtering the favicon.
183 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
184 p.setStyle(Paint.Style.FILL_AND_STROKE);
John Reck8e8e71c2010-12-16 11:17:54 -0800185 if (type == BookmarkIconType.ICON_WIDGET) {
186 p.setColor(context.getResources()
187 .getColor(R.color.bookmarkWidgetFaviconBackground));
188 } else {
189 p.setColor(Color.WHITE);
190 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100191
192 // Create a rectangle that is slightly wider than the favicon
Ben Murdoch77835fd2010-11-29 15:41:15 +0000193 int faviconDimension = context.getResources().getDimensionPixelSize(R.dimen.favicon_size);
John Reck8e8e71c2010-12-16 11:17:54 -0800194 int faviconPaddedRectDimension;
195 if (type == BookmarkIconType.ICON_WIDGET) {
196 faviconPaddedRectDimension = canvas.getWidth();
197 } else {
198 faviconPaddedRectDimension = context.getResources().getDimensionPixelSize(
199 R.dimen.favicon_padded_size);
200 }
Ben Murdoch77835fd2010-11-29 15:41:15 +0000201 float padding = (faviconPaddedRectDimension - faviconDimension) / 2;
202 final float x = iconBounds.exactCenterX() - (faviconPaddedRectDimension / 2);
John Reck8e8e71c2010-12-16 11:17:54 -0800203 float y = iconBounds.exactCenterY() - (faviconPaddedRectDimension / 2);
204 if (type != BookmarkIconType.ICON_WIDGET) {
205 // Note: Subtract from the y position since the box is
206 // slightly higher than center. Use padding since it is already
207 // device independent.
208 y -= padding;
209 }
Ben Murdoch77835fd2010-11-29 15:41:15 +0000210 RectF r = new RectF(x, y, x + faviconPaddedRectDimension, y + faviconPaddedRectDimension);
Ben Murdoch90b40262010-06-30 13:33:28 +0100211 // Draw a white rounded rectangle behind the favicon
John Reck8e8e71c2010-12-16 11:17:54 -0800212 canvas.drawRoundRect(r, 3, 3, p);
Ben Murdoch90b40262010-06-30 13:33:28 +0100213
214 // Draw the favicon in the same rectangle as the rounded
215 // rectangle but inset by the padding
216 // (results in a 16x16 favicon).
217 r.inset(padding, padding);
Ben Murdoch77835fd2010-11-29 15:41:15 +0000218 canvas.drawBitmap(favicon, null, r, null);
Ben Murdoch90b40262010-06-30 13:33:28 +0100219 }
220
John Reckc8490812010-11-22 14:15:36 -0800221 /* package */ static Uri getBookmarksUri(Context context) {
John Reck006ae182011-05-13 11:16:07 -0700222 return BrowserContract.Bookmarks.CONTENT_URI;
John Reck3dff1ce2010-12-10 11:13:57 -0800223 }
224
Leon Scroggins75630672011-01-13 17:56:15 -0500225 /**
226 * Show a confirmation dialog to remove a bookmark.
227 * @param id Id of the bookmark to remove
228 * @param title Title of the bookmark, to be displayed in the confirmation method.
229 * @param context Package Context for strings, dialog, ContentResolver
230 * @param msg Message to send if the bookmark is deleted.
231 */
232 static void displayRemoveBookmarkDialog( final long id, final String title,
233 final Context context, final Message msg) {
234
235 new AlertDialog.Builder(context)
236 .setTitle(R.string.delete_bookmark)
237 .setIcon(android.R.drawable.ic_dialog_alert)
238 .setMessage(context.getString(R.string.delete_bookmark_warning,
239 title))
240 .setPositiveButton(R.string.ok,
241 new DialogInterface.OnClickListener() {
242 @Override
243 public void onClick(DialogInterface dialog, int whichButton) {
244 if (msg != null) {
245 msg.sendToTarget();
246 }
247 Runnable runnable = new Runnable(){
248 @Override
249 public void run() {
250 Uri uri = ContentUris.withAppendedId(
251 BrowserContract.Bookmarks.CONTENT_URI,
252 id);
253 context.getContentResolver().delete(uri, null, null);
254 }
255 };
256 new Thread(runnable).start();
257 }
258 })
259 .setNegativeButton(R.string.cancel, null)
260 .show();
261 }
John Reck3dff1ce2010-12-10 11:13:57 -0800262}