blob: 379b22d63044276f2d2ff341327664968bf21991 [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
Leon Scroggins75630672011-01-13 17:56:15 -050019import android.app.AlertDialog;
20import android.content.ContentUris;
Ben Murdoch90b40262010-06-30 13:33:28 +010021import android.content.Context;
Leon Scroggins75630672011-01-13 17:56:15 -050022import android.content.DialogInterface;
Ben Murdoch90b40262010-06-30 13:33:28 +010023import android.content.Intent;
John Reckc8490812010-11-22 14:15:36 -080024import android.content.SharedPreferences;
John Reck0ce8a942011-01-14 19:57:09 -080025import android.content.res.Resources;
Ben Murdoch90b40262010-06-30 13:33:28 +010026import android.graphics.Bitmap;
27import android.graphics.BitmapFactory;
28import 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;
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;
John Reckc8490812010-11-22 14:15:36 -080040import android.preference.PreferenceManager;
Ben Murdoch90b40262010-06-30 13:33:28 +010041import android.provider.Browser;
John Reckc8490812010-11-22 14:15:36 -080042import android.provider.BrowserContract;
Ben Murdoch90b40262010-06-30 13:33:28 +010043
John Reckdd03faf2010-12-13 11:03:16 -080044public class BookmarkUtils {
Ben Murdoch90b40262010-06-30 13:33:28 +010045 private final static String LOGTAG = "BookmarkUtils";
46
47 // XXX: There is no public string defining this intent so if Home changes the value, we
48 // have to update this string.
49 private static final String INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
50
51 enum BookmarkIconType {
52 ICON_INSTALLABLE_WEB_APP, // Icon for an installable web app (launches WebAppRuntime).
John Reck8e8e71c2010-12-16 11:17:54 -080053 ICON_HOME_SHORTCUT, // Icon for a shortcut on the home screen (launches Browser).
54 ICON_WIDGET,
55 }
Ben Murdoch90b40262010-06-30 13:33:28 +010056
57 /**
58 * Creates an icon to be associated with this bookmark. If available, the apple touch icon
59 * will be used, else we draw our own depending on the type of "bookmark" being created.
60 */
61 static Bitmap createIcon(Context context, Bitmap touchIcon, Bitmap favicon,
62 BookmarkIconType type) {
63 int iconDimension = context.getResources().getDimensionPixelSize(
64 android.R.dimen.app_icon_size);
65
John Reck8e8e71c2010-12-16 11:17:54 -080066 return createIcon(context, touchIcon, favicon, type, iconDimension);
67 }
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,
82 Bitmap favicon, BookmarkIconType type, int iconDimension) {
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).
93 Bitmap icon = getIconBackground(context, type);
94
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);
116 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
117 long urlHash = url.hashCode();
118 long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
119 shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID, Long.toString(uniqueId));
120 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
130 private static Bitmap getIconBackground(Context context, BookmarkIconType type) {
131 if (type == BookmarkIconType.ICON_HOME_SHORTCUT) {
132 // Want to create a shortcut icon on the homescreen, so the icon
133 // background is the red bookmark.
134 return BitmapFactory.decodeResource(context.getResources(),
Ying Wang79697cb2010-11-23 11:17:46 -0800135 R.mipmap.ic_launcher_shortcut_browser_bookmark);
Ben Murdoch90b40262010-06-30 13:33:28 +0100136 } else if (type == BookmarkIconType.ICON_INSTALLABLE_WEB_APP) {
137 // Use the web browser icon as the background for the icon for an installable
138 // web app.
139 return BitmapFactory.decodeResource(context.getResources(),
Ying Wang79697cb2010-11-23 11:17:46 -0800140 R.mipmap.ic_launcher_browser);
Ben Murdoch90b40262010-06-30 13:33:28 +0100141 }
142 return null;
143 }
144
145 private static void drawTouchIconToCanvas(Bitmap touchIcon, Canvas canvas, Rect iconBounds) {
146 Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());
147
148 // Paint used for scaling the bitmap and drawing the rounded rect.
149 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
150 paint.setFilterBitmap(true);
151 canvas.drawBitmap(touchIcon, src, iconBounds, paint);
152
153 // Construct a path from a round rect. This will allow drawing with
154 // an inverse fill so we can punch a hole using the round rect.
155 Path path = new Path();
156 path.setFillType(Path.FillType.INVERSE_WINDING);
157 RectF rect = new RectF(iconBounds);
158 rect.inset(1, 1);
159 path.addRoundRect(rect, 8f, 8f, Path.Direction.CW);
160
161 // Reuse the paint and clear the outside of the rectangle.
162 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
163 canvas.drawPath(path, paint);
164 }
165
John Reck8e8e71c2010-12-16 11:17:54 -0800166 private static void drawFaviconToCanvas(Context context, Bitmap favicon,
167 Canvas canvas, Rect iconBounds, BookmarkIconType type) {
Ben Murdoch90b40262010-06-30 13:33:28 +0100168 // Make a Paint for the white background rectangle and for
169 // filtering the favicon.
170 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
171 p.setStyle(Paint.Style.FILL_AND_STROKE);
John Reck8e8e71c2010-12-16 11:17:54 -0800172 if (type == BookmarkIconType.ICON_WIDGET) {
173 p.setColor(context.getResources()
174 .getColor(R.color.bookmarkWidgetFaviconBackground));
175 } else {
176 p.setColor(Color.WHITE);
177 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100178
179 // Create a rectangle that is slightly wider than the favicon
Ben Murdoch77835fd2010-11-29 15:41:15 +0000180 int faviconDimension = context.getResources().getDimensionPixelSize(R.dimen.favicon_size);
John Reck8e8e71c2010-12-16 11:17:54 -0800181 int faviconPaddedRectDimension;
182 if (type == BookmarkIconType.ICON_WIDGET) {
183 faviconPaddedRectDimension = canvas.getWidth();
184 } else {
185 faviconPaddedRectDimension = context.getResources().getDimensionPixelSize(
186 R.dimen.favicon_padded_size);
187 }
Ben Murdoch77835fd2010-11-29 15:41:15 +0000188 float padding = (faviconPaddedRectDimension - faviconDimension) / 2;
189 final float x = iconBounds.exactCenterX() - (faviconPaddedRectDimension / 2);
John Reck8e8e71c2010-12-16 11:17:54 -0800190 float y = iconBounds.exactCenterY() - (faviconPaddedRectDimension / 2);
191 if (type != BookmarkIconType.ICON_WIDGET) {
192 // Note: Subtract from the y position since the box is
193 // slightly higher than center. Use padding since it is already
194 // device independent.
195 y -= padding;
196 }
Ben Murdoch77835fd2010-11-29 15:41:15 +0000197 RectF r = new RectF(x, y, x + faviconPaddedRectDimension, y + faviconPaddedRectDimension);
Ben Murdoch90b40262010-06-30 13:33:28 +0100198 // Draw a white rounded rectangle behind the favicon
John Reck8e8e71c2010-12-16 11:17:54 -0800199 canvas.drawRoundRect(r, 3, 3, p);
Ben Murdoch90b40262010-06-30 13:33:28 +0100200
201 // Draw the favicon in the same rectangle as the rounded
202 // rectangle but inset by the padding
203 // (results in a 16x16 favicon).
204 r.inset(padding, padding);
Ben Murdoch77835fd2010-11-29 15:41:15 +0000205 canvas.drawBitmap(favicon, null, r, null);
Ben Murdoch90b40262010-06-30 13:33:28 +0100206 }
207
John Reckc8490812010-11-22 14:15:36 -0800208 /* package */ static Uri getBookmarksUri(Context context) {
John Reck3dff1ce2010-12-10 11:13:57 -0800209 return addAccountInfo(context,
210 BrowserContract.Bookmarks.CONTENT_URI.buildUpon()).build();
211 }
212
John Reckdd03faf2010-12-13 11:03:16 -0800213 public static Uri.Builder addAccountInfo(Context context, Uri.Builder ub) {
John Reckc8490812010-11-22 14:15:36 -0800214 Uri uri = BrowserContract.Bookmarks.CONTENT_URI;
215 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
216 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
217 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
John Reck6c22ce62011-01-06 16:22:48 -0800218 ub.appendQueryParameter(
219 BrowserContract.Bookmarks.PARAM_ACCOUNT_NAME,accountName);
220 ub.appendQueryParameter(
221 BrowserContract.Bookmarks.PARAM_ACCOUNT_TYPE, accountType);
John Reck3dff1ce2010-12-10 11:13:57 -0800222 return ub;
John Reckc8490812010-11-22 14:15:36 -0800223 }
Leon Scroggins75630672011-01-13 17:56:15 -0500224
225 /**
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}