blob: b548607ff29939c14b958b2df37fd1916430ccb3 [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
19import android.content.Context;
20import android.content.Intent;
John Reckc8490812010-11-22 14:15:36 -080021import android.content.SharedPreferences;
Ben Murdoch90b40262010-06-30 13:33:28 +010022import android.graphics.Bitmap;
23import android.graphics.BitmapFactory;
24import android.graphics.Canvas;
25import android.graphics.Color;
26import android.graphics.Paint;
27import android.graphics.Path;
28import android.graphics.PorterDuff;
29import android.graphics.PorterDuffXfermode;
30import android.graphics.Rect;
31import android.graphics.RectF;
32import android.net.Uri;
John Reckc8490812010-11-22 14:15:36 -080033import android.preference.PreferenceManager;
Ben Murdoch90b40262010-06-30 13:33:28 +010034import android.provider.Browser;
John Reckc8490812010-11-22 14:15:36 -080035import android.provider.BrowserContract;
36import android.text.TextUtils;
Ben Murdoch90b40262010-06-30 13:33:28 +010037
John Reckdd03faf2010-12-13 11:03:16 -080038public class BookmarkUtils {
Ben Murdoch90b40262010-06-30 13:33:28 +010039 private final static String LOGTAG = "BookmarkUtils";
40
41 // XXX: There is no public string defining this intent so if Home changes the value, we
42 // have to update this string.
43 private static final String INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
44
45 enum BookmarkIconType {
46 ICON_INSTALLABLE_WEB_APP, // Icon for an installable web app (launches WebAppRuntime).
John Reck8e8e71c2010-12-16 11:17:54 -080047 ICON_HOME_SHORTCUT, // Icon for a shortcut on the home screen (launches Browser).
48 ICON_WIDGET,
49 }
Ben Murdoch90b40262010-06-30 13:33:28 +010050
51 /**
52 * Creates an icon to be associated with this bookmark. If available, the apple touch icon
53 * will be used, else we draw our own depending on the type of "bookmark" being created.
54 */
55 static Bitmap createIcon(Context context, Bitmap touchIcon, Bitmap favicon,
56 BookmarkIconType type) {
57 int iconDimension = context.getResources().getDimensionPixelSize(
58 android.R.dimen.app_icon_size);
59
John Reck8e8e71c2010-12-16 11:17:54 -080060 return createIcon(context, touchIcon, favicon, type, iconDimension);
61 }
62
63 public static Bitmap createListWidgetIcon(Context context, Bitmap touchIcon,
64 Bitmap favicon) {
65 int iconDimension = context.getResources().getDimensionPixelSize(
66 R.dimen.bookmark_widget_favicon_size);
67
68 return createIcon(context, touchIcon, favicon,
69 BookmarkIconType.ICON_WIDGET, iconDimension);
70 }
71
72 private static Bitmap createIcon(Context context, Bitmap touchIcon,
73 Bitmap favicon, BookmarkIconType type, int iconDimension) {
Ben Murdoch90b40262010-06-30 13:33:28 +010074 Bitmap bm = Bitmap.createBitmap(iconDimension, iconDimension, Bitmap.Config.ARGB_8888);
75 Canvas canvas = new Canvas(bm);
76 Rect iconBounds = new Rect(0, 0, bm.getWidth(), bm.getHeight());
77
78 // Use the apple-touch-icon if available
79 if (touchIcon != null) {
80 drawTouchIconToCanvas(touchIcon, canvas, iconBounds);
81 } else {
82 // No touch icon so create our own.
83 // Set the background based on the type of shortcut (either webapp or home shortcut).
84 Bitmap icon = getIconBackground(context, type);
85
86 if (icon != null) {
87 // Now draw the correct icon background into our new bitmap.
Ben Murdoch77835fd2010-11-29 15:41:15 +000088 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
89 canvas.drawBitmap(icon, null, iconBounds, p);
Ben Murdoch90b40262010-06-30 13:33:28 +010090 }
91
92 // If we have a favicon, overlay it in a nice rounded white box on top of the
93 // background.
94 if (favicon != null) {
John Reck8e8e71c2010-12-16 11:17:54 -080095 drawFaviconToCanvas(context, favicon, canvas, iconBounds, type);
Ben Murdoch90b40262010-06-30 13:33:28 +010096 }
97 }
98 return bm;
99 }
100
101 /**
102 * Convenience method for creating an intent that will add a shortcut to the home screen.
103 */
104 static Intent createAddToHomeIntent(Context context, String url, String title,
105 Bitmap touchIcon, Bitmap favicon) {
106 Intent i = new Intent(INSTALL_SHORTCUT);
107 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
108 long urlHash = url.hashCode();
109 long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
110 shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID, Long.toString(uniqueId));
111 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
112 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
113 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(context, touchIcon, favicon,
114 BookmarkIconType.ICON_HOME_SHORTCUT));
115
116 // Do not allow duplicate items
117 i.putExtra("duplicate", false);
118 return i;
119 }
120
121 private static Bitmap getIconBackground(Context context, BookmarkIconType type) {
122 if (type == BookmarkIconType.ICON_HOME_SHORTCUT) {
123 // Want to create a shortcut icon on the homescreen, so the icon
124 // background is the red bookmark.
125 return BitmapFactory.decodeResource(context.getResources(),
Ying Wang79697cb2010-11-23 11:17:46 -0800126 R.mipmap.ic_launcher_shortcut_browser_bookmark);
Ben Murdoch90b40262010-06-30 13:33:28 +0100127 } else if (type == BookmarkIconType.ICON_INSTALLABLE_WEB_APP) {
128 // Use the web browser icon as the background for the icon for an installable
129 // web app.
130 return BitmapFactory.decodeResource(context.getResources(),
Ying Wang79697cb2010-11-23 11:17:46 -0800131 R.mipmap.ic_launcher_browser);
Ben Murdoch90b40262010-06-30 13:33:28 +0100132 }
133 return null;
134 }
135
136 private static void drawTouchIconToCanvas(Bitmap touchIcon, Canvas canvas, Rect iconBounds) {
137 Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());
138
139 // Paint used for scaling the bitmap and drawing the rounded rect.
140 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
141 paint.setFilterBitmap(true);
142 canvas.drawBitmap(touchIcon, src, iconBounds, paint);
143
144 // Construct a path from a round rect. This will allow drawing with
145 // an inverse fill so we can punch a hole using the round rect.
146 Path path = new Path();
147 path.setFillType(Path.FillType.INVERSE_WINDING);
148 RectF rect = new RectF(iconBounds);
149 rect.inset(1, 1);
150 path.addRoundRect(rect, 8f, 8f, Path.Direction.CW);
151
152 // Reuse the paint and clear the outside of the rectangle.
153 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
154 canvas.drawPath(path, paint);
155 }
156
John Reck8e8e71c2010-12-16 11:17:54 -0800157 private static void drawFaviconToCanvas(Context context, Bitmap favicon,
158 Canvas canvas, Rect iconBounds, BookmarkIconType type) {
Ben Murdoch90b40262010-06-30 13:33:28 +0100159 // Make a Paint for the white background rectangle and for
160 // filtering the favicon.
161 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
162 p.setStyle(Paint.Style.FILL_AND_STROKE);
John Reck8e8e71c2010-12-16 11:17:54 -0800163 if (type == BookmarkIconType.ICON_WIDGET) {
164 p.setColor(context.getResources()
165 .getColor(R.color.bookmarkWidgetFaviconBackground));
166 } else {
167 p.setColor(Color.WHITE);
168 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100169
170 // Create a rectangle that is slightly wider than the favicon
Ben Murdoch77835fd2010-11-29 15:41:15 +0000171 int faviconDimension = context.getResources().getDimensionPixelSize(R.dimen.favicon_size);
John Reck8e8e71c2010-12-16 11:17:54 -0800172 int faviconPaddedRectDimension;
173 if (type == BookmarkIconType.ICON_WIDGET) {
174 faviconPaddedRectDimension = canvas.getWidth();
175 } else {
176 faviconPaddedRectDimension = context.getResources().getDimensionPixelSize(
177 R.dimen.favicon_padded_size);
178 }
Ben Murdoch77835fd2010-11-29 15:41:15 +0000179 float padding = (faviconPaddedRectDimension - faviconDimension) / 2;
180 final float x = iconBounds.exactCenterX() - (faviconPaddedRectDimension / 2);
John Reck8e8e71c2010-12-16 11:17:54 -0800181 float y = iconBounds.exactCenterY() - (faviconPaddedRectDimension / 2);
182 if (type != BookmarkIconType.ICON_WIDGET) {
183 // Note: Subtract from the y position since the box is
184 // slightly higher than center. Use padding since it is already
185 // device independent.
186 y -= padding;
187 }
Ben Murdoch77835fd2010-11-29 15:41:15 +0000188 RectF r = new RectF(x, y, x + faviconPaddedRectDimension, y + faviconPaddedRectDimension);
Ben Murdoch90b40262010-06-30 13:33:28 +0100189 // Draw a white rounded rectangle behind the favicon
John Reck8e8e71c2010-12-16 11:17:54 -0800190 canvas.drawRoundRect(r, 3, 3, p);
Ben Murdoch90b40262010-06-30 13:33:28 +0100191
192 // Draw the favicon in the same rectangle as the rounded
193 // rectangle but inset by the padding
194 // (results in a 16x16 favicon).
195 r.inset(padding, padding);
Ben Murdoch77835fd2010-11-29 15:41:15 +0000196 canvas.drawBitmap(favicon, null, r, null);
Ben Murdoch90b40262010-06-30 13:33:28 +0100197 }
198
John Reckc8490812010-11-22 14:15:36 -0800199 /* package */ static Uri getBookmarksUri(Context context) {
John Reck3dff1ce2010-12-10 11:13:57 -0800200 return addAccountInfo(context,
201 BrowserContract.Bookmarks.CONTENT_URI.buildUpon()).build();
202 }
203
John Reckdd03faf2010-12-13 11:03:16 -0800204 public static Uri.Builder addAccountInfo(Context context, Uri.Builder ub) {
John Reckc8490812010-11-22 14:15:36 -0800205 Uri uri = BrowserContract.Bookmarks.CONTENT_URI;
206 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
207 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
208 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
John Reck6c22ce62011-01-06 16:22:48 -0800209 ub.appendQueryParameter(
210 BrowserContract.Bookmarks.PARAM_ACCOUNT_NAME,accountName);
211 ub.appendQueryParameter(
212 BrowserContract.Bookmarks.PARAM_ACCOUNT_TYPE, accountType);
John Reck3dff1ce2010-12-10 11:13:57 -0800213 return ub;
John Reckc8490812010-11-22 14:15:36 -0800214 }
John Reck3dff1ce2010-12-10 11:13:57 -0800215}