Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 21 | import android.content.SharedPreferences; |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 22 | import android.graphics.Bitmap; |
| 23 | import android.graphics.BitmapFactory; |
| 24 | import android.graphics.Canvas; |
| 25 | import android.graphics.Color; |
| 26 | import android.graphics.Paint; |
| 27 | import android.graphics.Path; |
| 28 | import android.graphics.PorterDuff; |
| 29 | import android.graphics.PorterDuffXfermode; |
| 30 | import android.graphics.Rect; |
| 31 | import android.graphics.RectF; |
| 32 | import android.net.Uri; |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 33 | import android.preference.PreferenceManager; |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 34 | import android.provider.Browser; |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 35 | import android.provider.BrowserContract; |
| 36 | import android.text.TextUtils; |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 37 | |
| 38 | class 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 Murdoch | 77835fd | 2010-11-29 15:41:15 +0000 | [diff] [blame^] | 73 | Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); |
| 74 | canvas.drawBitmap(icon, null, iconBounds, p); |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 75 | } |
| 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 Murdoch | 77835fd | 2010-11-29 15:41:15 +0000 | [diff] [blame^] | 80 | drawFaviconToCanvas(context, favicon, canvas, iconBounds); |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 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 Wang | 79697cb | 2010-11-23 11:17:46 -0800 | [diff] [blame] | 111 | R.mipmap.ic_launcher_shortcut_browser_bookmark); |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 112 | } 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 Wang | 79697cb | 2010-11-23 11:17:46 -0800 | [diff] [blame] | 116 | R.mipmap.ic_launcher_browser); |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 117 | } |
| 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 Murdoch | 77835fd | 2010-11-29 15:41:15 +0000 | [diff] [blame^] | 142 | private static void drawFaviconToCanvas(Context context, Bitmap favicon, Canvas canvas, |
| 143 | Rect iconBounds) { |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 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 |
Ben Murdoch | 77835fd | 2010-11-29 15:41:15 +0000 | [diff] [blame^] | 151 | 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 Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 157 | // slightly higher than center. Use padding since it is already |
Ben Murdoch | 77835fd | 2010-11-29 15:41:15 +0000 | [diff] [blame^] | 158 | // device independent. |
| 159 | final float y = iconBounds.exactCenterY() - (faviconPaddedRectDimension / 2) - padding; |
| 160 | RectF r = new RectF(x, y, x + faviconPaddedRectDimension, y + faviconPaddedRectDimension); |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 161 | |
| 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 Murdoch | 77835fd | 2010-11-29 15:41:15 +0000 | [diff] [blame^] | 169 | canvas.drawBitmap(favicon, null, r, null); |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 170 | } |
| 171 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 172 | /* 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 Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 185 | }; |