blob: f0c01f7d50250a62fd780cb45cd276603b0eab8d [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 }
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -0700107 canvas.setBitmap(null);
Ben Murdoch90b40262010-06-30 13:33:28 +0100108 return bm;
109 }
110
111 /**
112 * Convenience method for creating an intent that will add a shortcut to the home screen.
113 */
114 static Intent createAddToHomeIntent(Context context, String url, String title,
115 Bitmap touchIcon, Bitmap favicon) {
116 Intent i = new Intent(INSTALL_SHORTCUT);
John Reckdb22ec42011-06-29 11:31:24 -0700117 Intent shortcutIntent = createShortcutIntent(url);
Ben Murdoch90b40262010-06-30 13:33:28 +0100118 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
119 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
120 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(context, touchIcon, favicon,
121 BookmarkIconType.ICON_HOME_SHORTCUT));
122
123 // Do not allow duplicate items
124 i.putExtra("duplicate", false);
125 return i;
126 }
127
John Reckdb22ec42011-06-29 11:31:24 -0700128 static Intent createShortcutIntent(String url) {
129 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
130 long urlHash = url.hashCode();
131 long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
132 shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID, Long.toString(uniqueId));
133 return shortcutIntent;
134 }
135
Kenny Rootc65991b2011-01-26 19:45:07 -0800136 private static Bitmap getIconBackground(Context context, BookmarkIconType type, int density) {
Ben Murdoch90b40262010-06-30 13:33:28 +0100137 if (type == BookmarkIconType.ICON_HOME_SHORTCUT) {
138 // Want to create a shortcut icon on the homescreen, so the icon
139 // background is the red bookmark.
Kenny Rootc65991b2011-01-26 19:45:07 -0800140 Drawable drawable = context.getResources().getDrawableForDensity(
141 R.mipmap.ic_launcher_shortcut_browser_bookmark, density);
142 if (drawable instanceof BitmapDrawable) {
143 BitmapDrawable bd = (BitmapDrawable) drawable;
144 return bd.getBitmap();
145 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100146 } else if (type == BookmarkIconType.ICON_INSTALLABLE_WEB_APP) {
147 // Use the web browser icon as the background for the icon for an installable
148 // web app.
Kenny Rootc65991b2011-01-26 19:45:07 -0800149 Drawable drawable = context.getResources().getDrawableForDensity(
150 R.mipmap.ic_launcher_browser, density);
151 if (drawable instanceof BitmapDrawable) {
152 BitmapDrawable bd = (BitmapDrawable) drawable;
153 return bd.getBitmap();
154 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100155 }
156 return null;
157 }
158
159 private static void drawTouchIconToCanvas(Bitmap touchIcon, Canvas canvas, Rect iconBounds) {
160 Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());
161
162 // Paint used for scaling the bitmap and drawing the rounded rect.
163 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
164 paint.setFilterBitmap(true);
165 canvas.drawBitmap(touchIcon, src, iconBounds, paint);
166
167 // Construct a path from a round rect. This will allow drawing with
168 // an inverse fill so we can punch a hole using the round rect.
169 Path path = new Path();
170 path.setFillType(Path.FillType.INVERSE_WINDING);
171 RectF rect = new RectF(iconBounds);
172 rect.inset(1, 1);
173 path.addRoundRect(rect, 8f, 8f, Path.Direction.CW);
174
175 // Reuse the paint and clear the outside of the rectangle.
176 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
177 canvas.drawPath(path, paint);
178 }
179
John Reck8e8e71c2010-12-16 11:17:54 -0800180 private static void drawFaviconToCanvas(Context context, Bitmap favicon,
181 Canvas canvas, Rect iconBounds, BookmarkIconType type) {
Ben Murdoch90b40262010-06-30 13:33:28 +0100182 // Make a Paint for the white background rectangle and for
183 // filtering the favicon.
184 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
185 p.setStyle(Paint.Style.FILL_AND_STROKE);
John Reck8e8e71c2010-12-16 11:17:54 -0800186 if (type == BookmarkIconType.ICON_WIDGET) {
187 p.setColor(context.getResources()
188 .getColor(R.color.bookmarkWidgetFaviconBackground));
189 } else {
190 p.setColor(Color.WHITE);
191 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100192
193 // Create a rectangle that is slightly wider than the favicon
Ben Murdoch77835fd2010-11-29 15:41:15 +0000194 int faviconDimension = context.getResources().getDimensionPixelSize(R.dimen.favicon_size);
John Reck8e8e71c2010-12-16 11:17:54 -0800195 int faviconPaddedRectDimension;
196 if (type == BookmarkIconType.ICON_WIDGET) {
197 faviconPaddedRectDimension = canvas.getWidth();
198 } else {
199 faviconPaddedRectDimension = context.getResources().getDimensionPixelSize(
200 R.dimen.favicon_padded_size);
201 }
Ben Murdoch77835fd2010-11-29 15:41:15 +0000202 float padding = (faviconPaddedRectDimension - faviconDimension) / 2;
203 final float x = iconBounds.exactCenterX() - (faviconPaddedRectDimension / 2);
John Reck8e8e71c2010-12-16 11:17:54 -0800204 float y = iconBounds.exactCenterY() - (faviconPaddedRectDimension / 2);
205 if (type != BookmarkIconType.ICON_WIDGET) {
206 // Note: Subtract from the y position since the box is
207 // slightly higher than center. Use padding since it is already
208 // device independent.
209 y -= padding;
210 }
Ben Murdoch77835fd2010-11-29 15:41:15 +0000211 RectF r = new RectF(x, y, x + faviconPaddedRectDimension, y + faviconPaddedRectDimension);
Ben Murdoch90b40262010-06-30 13:33:28 +0100212 // Draw a white rounded rectangle behind the favicon
John Reck8e8e71c2010-12-16 11:17:54 -0800213 canvas.drawRoundRect(r, 3, 3, p);
Ben Murdoch90b40262010-06-30 13:33:28 +0100214
215 // Draw the favicon in the same rectangle as the rounded
216 // rectangle but inset by the padding
217 // (results in a 16x16 favicon).
218 r.inset(padding, padding);
Ben Murdoch77835fd2010-11-29 15:41:15 +0000219 canvas.drawBitmap(favicon, null, r, null);
Ben Murdoch90b40262010-06-30 13:33:28 +0100220 }
221
John Reckc8490812010-11-22 14:15:36 -0800222 /* package */ static Uri getBookmarksUri(Context context) {
John Reck006ae182011-05-13 11:16:07 -0700223 return BrowserContract.Bookmarks.CONTENT_URI;
John Reck3dff1ce2010-12-10 11:13:57 -0800224 }
225
Leon Scroggins75630672011-01-13 17:56:15 -0500226 /**
227 * Show a confirmation dialog to remove a bookmark.
228 * @param id Id of the bookmark to remove
229 * @param title Title of the bookmark, to be displayed in the confirmation method.
230 * @param context Package Context for strings, dialog, ContentResolver
231 * @param msg Message to send if the bookmark is deleted.
232 */
233 static void displayRemoveBookmarkDialog( final long id, final String title,
234 final Context context, final Message msg) {
235
236 new AlertDialog.Builder(context)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200237 .setIconAttribute(android.R.attr.alertDialogIcon)
Leon Scroggins75630672011-01-13 17:56:15 -0500238 .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}