blob: c72cbd1dac53dd2c2c7c8a510354b0c2a016368f [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.
Ben Murdoch77835fd2010-11-29 15:41:15 +000073 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
74 canvas.drawBitmap(icon, null, iconBounds, p);
Ben Murdoch90b40262010-06-30 13:33:28 +010075 }
76
77 // If we have a favicon, overlay it in a nice rounded white box on top of the
78 // background.
79 if (favicon != null) {
Ben Murdoch77835fd2010-11-29 15:41:15 +000080 drawFaviconToCanvas(context, favicon, canvas, iconBounds);
Ben Murdoch90b40262010-06-30 13:33:28 +010081 }
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
Ben Murdoch77835fd2010-11-29 15:41:15 +0000142 private static void drawFaviconToCanvas(Context context, Bitmap favicon, Canvas canvas,
143 Rect iconBounds) {
Ben Murdoch90b40262010-06-30 13:33:28 +0100144 // 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
Ben Murdoch77835fd2010-11-29 15:41:15 +0000151 int faviconDimension = context.getResources().getDimensionPixelSize(R.dimen.favicon_size);
152 int faviconPaddedRectDimension = context.getResources().getDimensionPixelSize(
153 R.dimen.favicon_padded_size);
154 float padding = (faviconPaddedRectDimension - faviconDimension) / 2;
155 final float x = iconBounds.exactCenterX() - (faviconPaddedRectDimension / 2);
156 // Note: Subtract from the y position since the box is
Ben Murdoch90b40262010-06-30 13:33:28 +0100157 // slightly higher than center. Use padding since it is already
Ben Murdoch77835fd2010-11-29 15:41:15 +0000158 // device independent.
159 final float y = iconBounds.exactCenterY() - (faviconPaddedRectDimension / 2) - padding;
160 RectF r = new RectF(x, y, x + faviconPaddedRectDimension, y + faviconPaddedRectDimension);
Ben Murdoch90b40262010-06-30 13:33:28 +0100161
162 // Draw a white rounded rectangle behind the favicon
163 canvas.drawRoundRect(r, 2, 2, p);
164
165 // Draw the favicon in the same rectangle as the rounded
166 // rectangle but inset by the padding
167 // (results in a 16x16 favicon).
168 r.inset(padding, padding);
Ben Murdoch77835fd2010-11-29 15:41:15 +0000169 canvas.drawBitmap(favicon, null, r, null);
Ben Murdoch90b40262010-06-30 13:33:28 +0100170 }
171
John Reckc8490812010-11-22 14:15:36 -0800172 /* package */ static Uri getBookmarksUri(Context context) {
173 Uri uri = BrowserContract.Bookmarks.CONTENT_URI;
174 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
175 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
176 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
177 if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) {
178 uri = uri.buildUpon()
179 .appendQueryParameter(BrowserContract.Bookmarks.PARAM_ACCOUNT_NAME, accountName)
180 .appendQueryParameter(BrowserContract.Bookmarks.PARAM_ACCOUNT_TYPE, accountType)
181 .build();
182 }
183 return uri;
184 }
Ben Murdoch90b40262010-06-30 13:33:28 +0100185};