blob: 27d3310dcd58b32d9cdf2ffcaece1306e7893927 [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;
Ben Murdoch90b40262010-06-30 13:33:28 +010025import android.graphics.Bitmap;
26import android.graphics.BitmapFactory;
27import 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;
35import android.net.Uri;
Leon Scroggins75630672011-01-13 17:56:15 -050036import android.os.Message;
John Reckc8490812010-11-22 14:15:36 -080037import android.preference.PreferenceManager;
Ben Murdoch90b40262010-06-30 13:33:28 +010038import android.provider.Browser;
John Reckc8490812010-11-22 14:15:36 -080039import android.provider.BrowserContract;
40import android.text.TextUtils;
Ben Murdoch90b40262010-06-30 13:33:28 +010041
John Reckdd03faf2010-12-13 11:03:16 -080042public class BookmarkUtils {
Ben Murdoch90b40262010-06-30 13:33:28 +010043 private final static String LOGTAG = "BookmarkUtils";
44
45 // XXX: There is no public string defining this intent so if Home changes the value, we
46 // have to update this string.
47 private static final String INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
48
49 enum BookmarkIconType {
50 ICON_INSTALLABLE_WEB_APP, // Icon for an installable web app (launches WebAppRuntime).
John Reck8e8e71c2010-12-16 11:17:54 -080051 ICON_HOME_SHORTCUT, // Icon for a shortcut on the home screen (launches Browser).
52 ICON_WIDGET,
53 }
Ben Murdoch90b40262010-06-30 13:33:28 +010054
55 /**
56 * Creates an icon to be associated with this bookmark. If available, the apple touch icon
57 * will be used, else we draw our own depending on the type of "bookmark" being created.
58 */
59 static Bitmap createIcon(Context context, Bitmap touchIcon, Bitmap favicon,
60 BookmarkIconType type) {
61 int iconDimension = context.getResources().getDimensionPixelSize(
62 android.R.dimen.app_icon_size);
63
John Reck8e8e71c2010-12-16 11:17:54 -080064 return createIcon(context, touchIcon, favicon, type, iconDimension);
65 }
66
67 public static Bitmap createListWidgetIcon(Context context, Bitmap touchIcon,
68 Bitmap favicon) {
69 int iconDimension = context.getResources().getDimensionPixelSize(
70 R.dimen.bookmark_widget_favicon_size);
71
72 return createIcon(context, touchIcon, favicon,
73 BookmarkIconType.ICON_WIDGET, iconDimension);
74 }
75
76 private static Bitmap createIcon(Context context, Bitmap touchIcon,
77 Bitmap favicon, BookmarkIconType type, int iconDimension) {
Ben Murdoch90b40262010-06-30 13:33:28 +010078 Bitmap bm = Bitmap.createBitmap(iconDimension, iconDimension, Bitmap.Config.ARGB_8888);
79 Canvas canvas = new Canvas(bm);
80 Rect iconBounds = new Rect(0, 0, bm.getWidth(), bm.getHeight());
81
82 // Use the apple-touch-icon if available
83 if (touchIcon != null) {
84 drawTouchIconToCanvas(touchIcon, canvas, iconBounds);
85 } else {
86 // No touch icon so create our own.
87 // Set the background based on the type of shortcut (either webapp or home shortcut).
88 Bitmap icon = getIconBackground(context, type);
89
90 if (icon != null) {
91 // Now draw the correct icon background into our new bitmap.
Ben Murdoch77835fd2010-11-29 15:41:15 +000092 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
93 canvas.drawBitmap(icon, null, iconBounds, p);
Ben Murdoch90b40262010-06-30 13:33:28 +010094 }
95
96 // If we have a favicon, overlay it in a nice rounded white box on top of the
97 // background.
98 if (favicon != null) {
John Reck8e8e71c2010-12-16 11:17:54 -080099 drawFaviconToCanvas(context, favicon, canvas, iconBounds, type);
Ben Murdoch90b40262010-06-30 13:33:28 +0100100 }
101 }
102 return bm;
103 }
104
105 /**
106 * Convenience method for creating an intent that will add a shortcut to the home screen.
107 */
108 static Intent createAddToHomeIntent(Context context, String url, String title,
109 Bitmap touchIcon, Bitmap favicon) {
110 Intent i = new Intent(INSTALL_SHORTCUT);
111 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
112 long urlHash = url.hashCode();
113 long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
114 shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID, Long.toString(uniqueId));
115 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
116 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
117 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(context, touchIcon, favicon,
118 BookmarkIconType.ICON_HOME_SHORTCUT));
119
120 // Do not allow duplicate items
121 i.putExtra("duplicate", false);
122 return i;
123 }
124
125 private static Bitmap getIconBackground(Context context, BookmarkIconType type) {
126 if (type == BookmarkIconType.ICON_HOME_SHORTCUT) {
127 // Want to create a shortcut icon on the homescreen, so the icon
128 // background is the red bookmark.
129 return BitmapFactory.decodeResource(context.getResources(),
Ying Wang79697cb2010-11-23 11:17:46 -0800130 R.mipmap.ic_launcher_shortcut_browser_bookmark);
Ben Murdoch90b40262010-06-30 13:33:28 +0100131 } else if (type == BookmarkIconType.ICON_INSTALLABLE_WEB_APP) {
132 // Use the web browser icon as the background for the icon for an installable
133 // web app.
134 return BitmapFactory.decodeResource(context.getResources(),
Ying Wang79697cb2010-11-23 11:17:46 -0800135 R.mipmap.ic_launcher_browser);
Ben Murdoch90b40262010-06-30 13:33:28 +0100136 }
137 return null;
138 }
139
140 private static void drawTouchIconToCanvas(Bitmap touchIcon, Canvas canvas, Rect iconBounds) {
141 Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());
142
143 // Paint used for scaling the bitmap and drawing the rounded rect.
144 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
145 paint.setFilterBitmap(true);
146 canvas.drawBitmap(touchIcon, src, iconBounds, paint);
147
148 // Construct a path from a round rect. This will allow drawing with
149 // an inverse fill so we can punch a hole using the round rect.
150 Path path = new Path();
151 path.setFillType(Path.FillType.INVERSE_WINDING);
152 RectF rect = new RectF(iconBounds);
153 rect.inset(1, 1);
154 path.addRoundRect(rect, 8f, 8f, Path.Direction.CW);
155
156 // Reuse the paint and clear the outside of the rectangle.
157 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
158 canvas.drawPath(path, paint);
159 }
160
John Reck8e8e71c2010-12-16 11:17:54 -0800161 private static void drawFaviconToCanvas(Context context, Bitmap favicon,
162 Canvas canvas, Rect iconBounds, BookmarkIconType type) {
Ben Murdoch90b40262010-06-30 13:33:28 +0100163 // Make a Paint for the white background rectangle and for
164 // filtering the favicon.
165 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
166 p.setStyle(Paint.Style.FILL_AND_STROKE);
John Reck8e8e71c2010-12-16 11:17:54 -0800167 if (type == BookmarkIconType.ICON_WIDGET) {
168 p.setColor(context.getResources()
169 .getColor(R.color.bookmarkWidgetFaviconBackground));
170 } else {
171 p.setColor(Color.WHITE);
172 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100173
174 // Create a rectangle that is slightly wider than the favicon
Ben Murdoch77835fd2010-11-29 15:41:15 +0000175 int faviconDimension = context.getResources().getDimensionPixelSize(R.dimen.favicon_size);
John Reck8e8e71c2010-12-16 11:17:54 -0800176 int faviconPaddedRectDimension;
177 if (type == BookmarkIconType.ICON_WIDGET) {
178 faviconPaddedRectDimension = canvas.getWidth();
179 } else {
180 faviconPaddedRectDimension = context.getResources().getDimensionPixelSize(
181 R.dimen.favicon_padded_size);
182 }
Ben Murdoch77835fd2010-11-29 15:41:15 +0000183 float padding = (faviconPaddedRectDimension - faviconDimension) / 2;
184 final float x = iconBounds.exactCenterX() - (faviconPaddedRectDimension / 2);
John Reck8e8e71c2010-12-16 11:17:54 -0800185 float y = iconBounds.exactCenterY() - (faviconPaddedRectDimension / 2);
186 if (type != BookmarkIconType.ICON_WIDGET) {
187 // Note: Subtract from the y position since the box is
188 // slightly higher than center. Use padding since it is already
189 // device independent.
190 y -= padding;
191 }
Ben Murdoch77835fd2010-11-29 15:41:15 +0000192 RectF r = new RectF(x, y, x + faviconPaddedRectDimension, y + faviconPaddedRectDimension);
Ben Murdoch90b40262010-06-30 13:33:28 +0100193 // Draw a white rounded rectangle behind the favicon
John Reck8e8e71c2010-12-16 11:17:54 -0800194 canvas.drawRoundRect(r, 3, 3, p);
Ben Murdoch90b40262010-06-30 13:33:28 +0100195
196 // Draw the favicon in the same rectangle as the rounded
197 // rectangle but inset by the padding
198 // (results in a 16x16 favicon).
199 r.inset(padding, padding);
Ben Murdoch77835fd2010-11-29 15:41:15 +0000200 canvas.drawBitmap(favicon, null, r, null);
Ben Murdoch90b40262010-06-30 13:33:28 +0100201 }
202
John Reckc8490812010-11-22 14:15:36 -0800203 /* package */ static Uri getBookmarksUri(Context context) {
John Reck3dff1ce2010-12-10 11:13:57 -0800204 return addAccountInfo(context,
205 BrowserContract.Bookmarks.CONTENT_URI.buildUpon()).build();
206 }
207
John Reckdd03faf2010-12-13 11:03:16 -0800208 public static Uri.Builder addAccountInfo(Context context, Uri.Builder ub) {
John Reckc8490812010-11-22 14:15:36 -0800209 Uri uri = BrowserContract.Bookmarks.CONTENT_URI;
210 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
211 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
212 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
John Reck6c22ce62011-01-06 16:22:48 -0800213 ub.appendQueryParameter(
214 BrowserContract.Bookmarks.PARAM_ACCOUNT_NAME,accountName);
215 ub.appendQueryParameter(
216 BrowserContract.Bookmarks.PARAM_ACCOUNT_TYPE, accountType);
John Reck3dff1ce2010-12-10 11:13:57 -0800217 return ub;
John Reckc8490812010-11-22 14:15:36 -0800218 }
Leon Scroggins75630672011-01-13 17:56:15 -0500219
220 /**
221 * Show a confirmation dialog to remove a bookmark.
222 * @param id Id of the bookmark to remove
223 * @param title Title of the bookmark, to be displayed in the confirmation method.
224 * @param context Package Context for strings, dialog, ContentResolver
225 * @param msg Message to send if the bookmark is deleted.
226 */
227 static void displayRemoveBookmarkDialog( final long id, final String title,
228 final Context context, final Message msg) {
229
230 new AlertDialog.Builder(context)
231 .setTitle(R.string.delete_bookmark)
232 .setIcon(android.R.drawable.ic_dialog_alert)
233 .setMessage(context.getString(R.string.delete_bookmark_warning,
234 title))
235 .setPositiveButton(R.string.ok,
236 new DialogInterface.OnClickListener() {
237 @Override
238 public void onClick(DialogInterface dialog, int whichButton) {
239 if (msg != null) {
240 msg.sendToTarget();
241 }
242 Runnable runnable = new Runnable(){
243 @Override
244 public void run() {
245 Uri uri = ContentUris.withAppendedId(
246 BrowserContract.Bookmarks.CONTENT_URI,
247 id);
248 context.getContentResolver().delete(uri, null, null);
249 }
250 };
251 new Thread(runnable).start();
252 }
253 })
254 .setNegativeButton(R.string.cancel, null)
255 .show();
256 }
John Reck3dff1ce2010-12-10 11:13:57 -0800257}