BookmarkUtils.java revision 3dff1cedc22620ab56fbd39f3703f19bb552ca34
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 17package com.android.browser; 18 19import android.content.Context; 20import android.content.Intent; 21import android.content.SharedPreferences; 22import 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; 33import android.preference.PreferenceManager; 34import android.provider.Browser; 35import android.provider.BrowserContract; 36import android.text.TextUtils; 37 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 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); 74 canvas.drawBitmap(icon, null, iconBounds, p); 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) { 80 drawFaviconToCanvas(context, favicon, canvas, iconBounds); 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(), 111 R.mipmap.ic_launcher_shortcut_browser_bookmark); 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(), 116 R.mipmap.ic_launcher_browser); 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 142 private static void drawFaviconToCanvas(Context context, Bitmap favicon, Canvas canvas, 143 Rect iconBounds) { 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 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 157 // slightly higher than center. Use padding since it is already 158 // device independent. 159 final float y = iconBounds.exactCenterY() - (faviconPaddedRectDimension / 2) - padding; 160 RectF r = new RectF(x, y, x + faviconPaddedRectDimension, y + faviconPaddedRectDimension); 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); 169 canvas.drawBitmap(favicon, null, r, null); 170 } 171 172 /* package */ static Uri getBookmarksUri(Context context) { 173 return addAccountInfo(context, 174 BrowserContract.Bookmarks.CONTENT_URI.buildUpon()).build(); 175 } 176 177 /* package */ static Uri.Builder addAccountInfo(Context context, Uri.Builder ub) { 178 Uri uri = BrowserContract.Bookmarks.CONTENT_URI; 179 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 180 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null); 181 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null); 182 if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { 183 ub.appendQueryParameter( 184 BrowserContract.Bookmarks.PARAM_ACCOUNT_NAME,accountName); 185 ub.appendQueryParameter( 186 BrowserContract.Bookmarks.PARAM_ACCOUNT_TYPE, accountType); 187 } 188 return ub; 189 } 190} 191