blob: 537530e410c31f5a0d9c27fb79b9dffe99a3ed50 [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Ben Murdoch90b40262010-06-30 13:33:28 +010018
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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080041
Bijan Amirzada41242f22014-03-21 12:12:18 -070042import com.android.browser.R;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070043import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070044import com.android.browser.platformsupport.BrowserContract;
45import com.android.browser.platformsupport.BrowserContract.Bookmarks;
Ben Murdoch90b40262010-06-30 13:33:28 +010046
John Reckdd03faf2010-12-13 11:03:16 -080047public class BookmarkUtils {
Ben Murdoch90b40262010-06-30 13:33:28 +010048 private final static String LOGTAG = "BookmarkUtils";
49
50 // XXX: There is no public string defining this intent so if Home changes the value, we
51 // have to update this string.
52 private static final String INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
53
54 enum BookmarkIconType {
55 ICON_INSTALLABLE_WEB_APP, // Icon for an installable web app (launches WebAppRuntime).
John Reck8e8e71c2010-12-16 11:17:54 -080056 ICON_HOME_SHORTCUT, // Icon for a shortcut on the home screen (launches Browser).
57 ICON_WIDGET,
58 }
Ben Murdoch90b40262010-06-30 13:33:28 +010059
60 /**
61 * Creates an icon to be associated with this bookmark. If available, the apple touch icon
62 * will be used, else we draw our own depending on the type of "bookmark" being created.
63 */
64 static Bitmap createIcon(Context context, Bitmap touchIcon, Bitmap favicon,
65 BookmarkIconType type) {
Kenny Rootc65991b2011-01-26 19:45:07 -080066 final ActivityManager am = (ActivityManager) context
67 .getSystemService(Context.ACTIVITY_SERVICE);
68 final int iconDimension = am.getLauncherLargeIconSize();
69 final int iconDensity = am.getLauncherLargeIconDensity();
70 return createIcon(context, touchIcon, favicon, type, iconDimension, iconDensity);
John Reck8e8e71c2010-12-16 11:17:54 -080071 }
72
John Reck0ce8a942011-01-14 19:57:09 -080073 static Drawable createListFaviconBackground(Context context) {
74 PaintDrawable faviconBackground = new PaintDrawable();
75 Resources res = context.getResources();
76 int padding = res.getDimensionPixelSize(R.dimen.list_favicon_padding);
77 faviconBackground.setPadding(padding, padding, padding, padding);
78 faviconBackground.getPaint().setColor(context.getResources()
79 .getColor(R.color.bookmarkListFaviconBackground));
80 faviconBackground.setCornerRadius(
81 res.getDimension(R.dimen.list_favicon_corner_radius));
82 return faviconBackground;
John Reck8e8e71c2010-12-16 11:17:54 -080083 }
84
85 private static Bitmap createIcon(Context context, Bitmap touchIcon,
Kenny Rootc65991b2011-01-26 19:45:07 -080086 Bitmap favicon, BookmarkIconType type, int iconDimension, int iconDensity) {
Ben Murdoch90b40262010-06-30 13:33:28 +010087 Bitmap bm = Bitmap.createBitmap(iconDimension, iconDimension, Bitmap.Config.ARGB_8888);
88 Canvas canvas = new Canvas(bm);
89 Rect iconBounds = new Rect(0, 0, bm.getWidth(), bm.getHeight());
90
91 // Use the apple-touch-icon if available
92 if (touchIcon != null) {
93 drawTouchIconToCanvas(touchIcon, canvas, iconBounds);
94 } else {
95 // No touch icon so create our own.
96 // Set the background based on the type of shortcut (either webapp or home shortcut).
Kenny Rootc65991b2011-01-26 19:45:07 -080097 Bitmap icon = getIconBackground(context, type, iconDensity);
Ben Murdoch90b40262010-06-30 13:33:28 +010098
99 if (icon != null) {
100 // Now draw the correct icon background into our new bitmap.
Ben Murdoch77835fd2010-11-29 15:41:15 +0000101 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
102 canvas.drawBitmap(icon, null, iconBounds, p);
Ben Murdoch90b40262010-06-30 13:33:28 +0100103 }
104
105 // If we have a favicon, overlay it in a nice rounded white box on top of the
106 // background.
107 if (favicon != null) {
John Reck8e8e71c2010-12-16 11:17:54 -0800108 drawFaviconToCanvas(context, favicon, canvas, iconBounds, type);
Ben Murdoch90b40262010-06-30 13:33:28 +0100109 }
110 }
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -0700111 canvas.setBitmap(null);
Ben Murdoch90b40262010-06-30 13:33:28 +0100112 return bm;
113 }
114
115 /**
116 * Convenience method for creating an intent that will add a shortcut to the home screen.
117 */
118 static Intent createAddToHomeIntent(Context context, String url, String title,
119 Bitmap touchIcon, Bitmap favicon) {
120 Intent i = new Intent(INSTALL_SHORTCUT);
John Reckdb22ec42011-06-29 11:31:24 -0700121 Intent shortcutIntent = createShortcutIntent(url);
Sagar Dhawan78d08592015-07-22 16:08:07 -0700122 shortcutIntent.setPackage(context.getPackageName());
Ben Murdoch90b40262010-06-30 13:33:28 +0100123 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
124 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
125 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(context, touchIcon, favicon,
126 BookmarkIconType.ICON_HOME_SHORTCUT));
127
128 // Do not allow duplicate items
129 i.putExtra("duplicate", false);
130 return i;
131 }
132
John Reckdb22ec42011-06-29 11:31:24 -0700133 static Intent createShortcutIntent(String url) {
134 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
135 long urlHash = url.hashCode();
136 long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
137 shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID, Long.toString(uniqueId));
138 return shortcutIntent;
139 }
140
Kenny Rootc65991b2011-01-26 19:45:07 -0800141 private static Bitmap getIconBackground(Context context, BookmarkIconType type, int density) {
Ben Murdoch90b40262010-06-30 13:33:28 +0100142 if (type == BookmarkIconType.ICON_HOME_SHORTCUT) {
143 // Want to create a shortcut icon on the homescreen, so the icon
144 // background is the red bookmark.
Kenny Rootc65991b2011-01-26 19:45:07 -0800145 Drawable drawable = context.getResources().getDrawableForDensity(
146 R.mipmap.ic_launcher_shortcut_browser_bookmark, density);
147 if (drawable instanceof BitmapDrawable) {
148 BitmapDrawable bd = (BitmapDrawable) drawable;
149 return bd.getBitmap();
150 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100151 } else if (type == BookmarkIconType.ICON_INSTALLABLE_WEB_APP) {
152 // Use the web browser icon as the background for the icon for an installable
153 // web app.
Kenny Rootc65991b2011-01-26 19:45:07 -0800154 Drawable drawable = context.getResources().getDrawableForDensity(
155 R.mipmap.ic_launcher_browser, density);
156 if (drawable instanceof BitmapDrawable) {
157 BitmapDrawable bd = (BitmapDrawable) drawable;
158 return bd.getBitmap();
159 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100160 }
161 return null;
162 }
163
164 private static void drawTouchIconToCanvas(Bitmap touchIcon, Canvas canvas, Rect iconBounds) {
165 Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());
166
167 // Paint used for scaling the bitmap and drawing the rounded rect.
168 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
169 paint.setFilterBitmap(true);
170 canvas.drawBitmap(touchIcon, src, iconBounds, paint);
171
172 // Construct a path from a round rect. This will allow drawing with
173 // an inverse fill so we can punch a hole using the round rect.
174 Path path = new Path();
175 path.setFillType(Path.FillType.INVERSE_WINDING);
176 RectF rect = new RectF(iconBounds);
177 rect.inset(1, 1);
178 path.addRoundRect(rect, 8f, 8f, Path.Direction.CW);
179
180 // Reuse the paint and clear the outside of the rectangle.
181 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
182 canvas.drawPath(path, paint);
183 }
184
John Reck8e8e71c2010-12-16 11:17:54 -0800185 private static void drawFaviconToCanvas(Context context, Bitmap favicon,
186 Canvas canvas, Rect iconBounds, BookmarkIconType type) {
Ben Murdoch90b40262010-06-30 13:33:28 +0100187 // Make a Paint for the white background rectangle and for
188 // filtering the favicon.
189 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
190 p.setStyle(Paint.Style.FILL_AND_STROKE);
John Reck8e8e71c2010-12-16 11:17:54 -0800191 if (type == BookmarkIconType.ICON_WIDGET) {
192 p.setColor(context.getResources()
193 .getColor(R.color.bookmarkWidgetFaviconBackground));
194 } else {
195 p.setColor(Color.WHITE);
196 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100197
198 // Create a rectangle that is slightly wider than the favicon
Ben Murdoch77835fd2010-11-29 15:41:15 +0000199 int faviconDimension = context.getResources().getDimensionPixelSize(R.dimen.favicon_size);
John Reck8e8e71c2010-12-16 11:17:54 -0800200 int faviconPaddedRectDimension;
201 if (type == BookmarkIconType.ICON_WIDGET) {
202 faviconPaddedRectDimension = canvas.getWidth();
203 } else {
204 faviconPaddedRectDimension = context.getResources().getDimensionPixelSize(
205 R.dimen.favicon_padded_size);
206 }
Ben Murdoch77835fd2010-11-29 15:41:15 +0000207 float padding = (faviconPaddedRectDimension - faviconDimension) / 2;
208 final float x = iconBounds.exactCenterX() - (faviconPaddedRectDimension / 2);
John Reck8e8e71c2010-12-16 11:17:54 -0800209 float y = iconBounds.exactCenterY() - (faviconPaddedRectDimension / 2);
210 if (type != BookmarkIconType.ICON_WIDGET) {
211 // Note: Subtract from the y position since the box is
212 // slightly higher than center. Use padding since it is already
213 // device independent.
214 y -= padding;
215 }
Ben Murdoch77835fd2010-11-29 15:41:15 +0000216 RectF r = new RectF(x, y, x + faviconPaddedRectDimension, y + faviconPaddedRectDimension);
Ben Murdoch90b40262010-06-30 13:33:28 +0100217 // Draw a white rounded rectangle behind the favicon
John Reck8e8e71c2010-12-16 11:17:54 -0800218 canvas.drawRoundRect(r, 3, 3, p);
Ben Murdoch90b40262010-06-30 13:33:28 +0100219
220 // Draw the favicon in the same rectangle as the rounded
221 // rectangle but inset by the padding
222 // (results in a 16x16 favicon).
223 r.inset(padding, padding);
Ben Murdoch77835fd2010-11-29 15:41:15 +0000224 canvas.drawBitmap(favicon, null, r, null);
Ben Murdoch90b40262010-06-30 13:33:28 +0100225 }
226
John Reckc8490812010-11-22 14:15:36 -0800227 /* package */ static Uri getBookmarksUri(Context context) {
John Reck006ae182011-05-13 11:16:07 -0700228 return BrowserContract.Bookmarks.CONTENT_URI;
John Reck3dff1ce2010-12-10 11:13:57 -0800229 }
230
Leon Scroggins75630672011-01-13 17:56:15 -0500231 /**
232 * Show a confirmation dialog to remove a bookmark.
233 * @param id Id of the bookmark to remove
234 * @param title Title of the bookmark, to be displayed in the confirmation method.
235 * @param context Package Context for strings, dialog, ContentResolver
236 * @param msg Message to send if the bookmark is deleted.
237 */
238 static void displayRemoveBookmarkDialog( final long id, final String title,
Pankaj Garg634bf432015-07-13 09:54:21 -0700239 final Context context, final Message msg, boolean is_folder) {
Leon Scroggins75630672011-01-13 17:56:15 -0500240
241 new AlertDialog.Builder(context)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200242 .setIconAttribute(android.R.attr.alertDialogIcon)
Pankaj Garg634bf432015-07-13 09:54:21 -0700243 .setMessage(is_folder ?
244 context.getString(R.string.delete_folder_warning, title) :
245 context.getString(R.string.delete_bookmark_warning, title))
Leon Scroggins75630672011-01-13 17:56:15 -0500246 .setPositiveButton(R.string.ok,
247 new DialogInterface.OnClickListener() {
248 @Override
249 public void onClick(DialogInterface dialog, int whichButton) {
250 if (msg != null) {
251 msg.sendToTarget();
252 }
Pankaj Garg634bf432015-07-13 09:54:21 -0700253 Runnable runnable = new Runnable() {
Leon Scroggins75630672011-01-13 17:56:15 -0500254 @Override
255 public void run() {
kaiyizbf7043d2013-07-29 15:22:13 +0800256 removeBookmarkOrFolder(context, id);
Leon Scroggins75630672011-01-13 17:56:15 -0500257 }
258 };
259 new Thread(runnable).start();
260 }
261 })
262 .setNegativeButton(R.string.cancel, null)
263 .show();
264 }
kaiyizbf7043d2013-07-29 15:22:13 +0800265
266 /**
267 * Remove the bookmark or folder.Remove all sub folders and bookmarks under current folder.
268 * @param context Package Context for strings, dialog, ContentResolver.
269 * @param id Id of the bookmark to remove.
270 */
271 private static void removeBookmarkOrFolder(Context context, long id) {
272 Cursor cursor = context.getContentResolver().query(Bookmarks.CONTENT_URI,
273 new String[] {Bookmarks._ID},
274 Bookmarks.PARENT + "=?",
275 new String[] {Long.toString(id)},
276 null);
277
278 if (cursor != null) {
279 try {
280 if (cursor.moveToFirst()) {
281 do {
282 removeBookmarkOrFolder(context,
283 cursor.getLong(cursor.getColumnIndex(Bookmarks._ID)));
284 } while (cursor.moveToNext());
285 }
286 } catch (Exception e) {
287 e.printStackTrace();
288 } finally {
289 cursor.close();
290 }
291 }
292
293 context.getContentResolver().delete(
294 ContentUris.withAppendedId(Bookmarks.CONTENT_URI, id), null, null);
295 }
John Reck3dff1ce2010-12-10 11:13:57 -0800296}