blob: a63b90fb04a060e8a2d27bb3f6040667e048c069 [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
38class BookmarkUtils {
39 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).
47 ICON_HOME_SHORTCUT // Icon for a shortcut on the home screen (launches Browser).
48 };
49
50 /**
51 * Creates an icon to be associated with this bookmark. If available, the apple touch icon
52 * will be used, else we draw our own depending on the type of "bookmark" being created.
53 */
54 static Bitmap createIcon(Context context, Bitmap touchIcon, Bitmap favicon,
55 BookmarkIconType type) {
56 int iconDimension = context.getResources().getDimensionPixelSize(
57 android.R.dimen.app_icon_size);
58
59 Bitmap bm = Bitmap.createBitmap(iconDimension, iconDimension, Bitmap.Config.ARGB_8888);
60 Canvas canvas = new Canvas(bm);
61 Rect iconBounds = new Rect(0, 0, bm.getWidth(), bm.getHeight());
62
63 // Use the apple-touch-icon if available
64 if (touchIcon != null) {
65 drawTouchIconToCanvas(touchIcon, canvas, iconBounds);
66 } else {
67 // No touch icon so create our own.
68 // Set the background based on the type of shortcut (either webapp or home shortcut).
69 Bitmap icon = getIconBackground(context, type);
70
71 if (icon != null) {
72 // Now draw the correct icon background into our new bitmap.
73 canvas.drawBitmap(icon, null, iconBounds, null);
74 }
75
76 // If we have a favicon, overlay it in a nice rounded white box on top of the
77 // background.
78 if (favicon != null) {
79 drawFaviconToCanvas(favicon, canvas, iconBounds,
80 context.getResources().getDisplayMetrics().density);
81 }
82 }
83 return bm;
84 }
85
86 /**
87 * Convenience method for creating an intent that will add a shortcut to the home screen.
88 */
89 static Intent createAddToHomeIntent(Context context, String url, String title,
90 Bitmap touchIcon, Bitmap favicon) {
91 Intent i = new Intent(INSTALL_SHORTCUT);
92 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
93 long urlHash = url.hashCode();
94 long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
95 shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID, Long.toString(uniqueId));
96 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
97 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
98 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(context, touchIcon, favicon,
99 BookmarkIconType.ICON_HOME_SHORTCUT));
100
101 // Do not allow duplicate items
102 i.putExtra("duplicate", false);
103 return i;
104 }
105
106 private static Bitmap getIconBackground(Context context, BookmarkIconType type) {
107 if (type == BookmarkIconType.ICON_HOME_SHORTCUT) {
108 // Want to create a shortcut icon on the homescreen, so the icon
109 // background is the red bookmark.
110 return BitmapFactory.decodeResource(context.getResources(),
Ying Wang79697cb2010-11-23 11:17:46 -0800111 R.mipmap.ic_launcher_shortcut_browser_bookmark);
Ben Murdoch90b40262010-06-30 13:33:28 +0100112 } else if (type == BookmarkIconType.ICON_INSTALLABLE_WEB_APP) {
113 // Use the web browser icon as the background for the icon for an installable
114 // web app.
115 return BitmapFactory.decodeResource(context.getResources(),
Ying Wang79697cb2010-11-23 11:17:46 -0800116 R.mipmap.ic_launcher_browser);
Ben Murdoch90b40262010-06-30 13:33:28 +0100117 }
118 return null;
119 }
120
121 private static void drawTouchIconToCanvas(Bitmap touchIcon, Canvas canvas, Rect iconBounds) {
122 Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());
123
124 // Paint used for scaling the bitmap and drawing the rounded rect.
125 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
126 paint.setFilterBitmap(true);
127 canvas.drawBitmap(touchIcon, src, iconBounds, paint);
128
129 // Construct a path from a round rect. This will allow drawing with
130 // an inverse fill so we can punch a hole using the round rect.
131 Path path = new Path();
132 path.setFillType(Path.FillType.INVERSE_WINDING);
133 RectF rect = new RectF(iconBounds);
134 rect.inset(1, 1);
135 path.addRoundRect(rect, 8f, 8f, Path.Direction.CW);
136
137 // Reuse the paint and clear the outside of the rectangle.
138 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
139 canvas.drawPath(path, paint);
140 }
141
142 private static void drawFaviconToCanvas(Bitmap favicon, Canvas canvas, Rect iconBounds,
143 float density) {
144 // Make a Paint for the white background rectangle and for
145 // filtering the favicon.
146 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
147 p.setStyle(Paint.Style.FILL_AND_STROKE);
148 p.setColor(Color.WHITE);
149
150 // Create a rectangle that is slightly wider than the favicon
151 final float iconSize = 16 * density; // 16x16 favicon
152 final float padding = 2 * density; // white padding around icon
153 final float rectSize = iconSize + 2 * padding;
154 final float x = iconBounds.exactCenterX() - (rectSize / 2);
155 // Note: Subtract 2 dip from the y position since the box is
156 // slightly higher than center. Use padding since it is already
157 // 2 * density.
158 final float y = iconBounds.exactCenterY() - (rectSize / 2) - padding;
159 RectF r = new RectF(x, y, x + rectSize, y + rectSize);
160
161 // Draw a white rounded rectangle behind the favicon
162 canvas.drawRoundRect(r, 2, 2, p);
163
164 // Draw the favicon in the same rectangle as the rounded
165 // rectangle but inset by the padding
166 // (results in a 16x16 favicon).
167 r.inset(padding, padding);
168 canvas.drawBitmap(favicon, null, r, p);
169 }
170
John Reckc8490812010-11-22 14:15:36 -0800171 /* package */ static Uri getBookmarksUri(Context context) {
172 Uri uri = BrowserContract.Bookmarks.CONTENT_URI;
173 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
174 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
175 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
176 if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) {
177 uri = uri.buildUpon()
178 .appendQueryParameter(BrowserContract.Bookmarks.PARAM_ACCOUNT_NAME, accountName)
179 .appendQueryParameter(BrowserContract.Bookmarks.PARAM_ACCOUNT_TYPE, accountType)
180 .build();
181 }
182 return uri;
183 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100184};