OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser; | 5 package org.chromium.chrome.browser; |
6 | 6 |
7 import android.app.ActivityManager; | 7 import android.app.ActivityManager; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.content.Intent; | 9 import android.content.Intent; |
10 import android.graphics.Bitmap; | 10 import android.graphics.Bitmap; |
11 import android.graphics.Canvas; | 11 import android.graphics.Canvas; |
12 import android.graphics.Color; | 12 import android.graphics.Color; |
13 import android.graphics.LinearGradient; | 13 import android.graphics.LinearGradient; |
14 import android.graphics.Paint; | 14 import android.graphics.Paint; |
15 import android.graphics.Path; | 15 import android.graphics.Path; |
16 import android.graphics.PorterDuff; | |
17 import android.graphics.PorterDuffXfermode; | |
16 import android.graphics.Rect; | 18 import android.graphics.Rect; |
17 import android.graphics.RectF; | 19 import android.graphics.RectF; |
18 import android.graphics.drawable.BitmapDrawable; | 20 import android.graphics.drawable.BitmapDrawable; |
19 import android.graphics.drawable.Drawable; | 21 import android.graphics.drawable.Drawable; |
20 import android.net.Uri; | 22 import android.net.Uri; |
21 import android.os.Build; | 23 import android.os.Build; |
22 import android.util.Log; | 24 import android.util.Log; |
23 | 25 |
24 import org.chromium.chrome.R; | 26 import org.chromium.chrome.R; |
25 | 27 |
26 /** | 28 /** |
27 * Util class for bookmarks. | 29 * Util class for bookmarks. |
28 */ | 30 */ |
29 public class BookmarkUtils { | 31 public class BookmarkUtils { |
30 | 32 |
31 // There is no public string defining this intent so if Home changes the val ue, we | 33 // There is no public string defining this intent so if Home changes the val ue, we |
32 // have to update this string. | 34 // have to update this string. |
33 private static final String INSTALL_SHORTCUT = "com.android.launcher.action. INSTALL_SHORTCUT"; | 35 private static final String INSTALL_SHORTCUT = "com.android.launcher.action. INSTALL_SHORTCUT"; |
34 private static final int DEFAULT_RGB_VALUE = 145; | 36 private static final int DEFAULT_RGB_VALUE = 145; |
35 private static final String TAG = "BookmarkUtils"; | 37 private static final String TAG = "BookmarkUtils"; |
36 public static final String REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB = | 38 public static final String REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB = |
37 "REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB"; | 39 "REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB"; |
40 private static final int INSET_DIMENSION_FOR_TOUCHICON = 1; | |
38 private static final int SDK_VERSION_FOR_ACCESS_TO_METHODS = 15; | 41 private static final int SDK_VERSION_FOR_ACCESS_TO_METHODS = 15; |
39 | 42 |
40 /** | 43 /** |
41 * Creates an intent that will add a shortcut to the home screen. | 44 * Creates an intent that will add a shortcut to the home screen. |
42 * @param context Context used to create the intent. | 45 * @param context Context used to create the intent. |
43 * @param url Url of the bookmark. | 46 * @param url Url of the bookmark. |
44 * @param title Title of the bookmark. | 47 * @param title Title of the bookmark. |
45 * @param favicon Bookmark favicon. | 48 * @param favicon Bookmark favicon. |
46 * @param rValue Red component of the dominant favicon color. | 49 * @param rValue Red component of the dominant favicon color. |
47 * @param gValue Green component of the dominant favicon color. | 50 * @param gValue Green component of the dominant favicon color. |
(...skipping 30 matching lines...) Expand all Loading... | |
78 * @param favicon Bookmark favicon bitmap. | 81 * @param favicon Bookmark favicon bitmap. |
79 * @param rValue Red component of the dominant favicon color. | 82 * @param rValue Red component of the dominant favicon color. |
80 * @param gValue Green component of the dominant favicon color. | 83 * @param gValue Green component of the dominant favicon color. |
81 * @param bValue Blue component of the dominant favicon color. | 84 * @param bValue Blue component of the dominant favicon color. |
82 * @return Bitmap Either the touch-icon or the newly created favicon. | 85 * @return Bitmap Either the touch-icon or the newly created favicon. |
83 */ | 86 */ |
84 private static Bitmap createIcon(Context context, Bitmap favicon, int rValue , | 87 private static Bitmap createIcon(Context context, Bitmap favicon, int rValue , |
85 int gValue, int bValue) { | 88 int gValue, int bValue) { |
86 Bitmap bitmap = null; | 89 Bitmap bitmap = null; |
87 ActivityManager am = (ActivityManager) context.getSystemService(Context. ACTIVITY_SERVICE); | 90 ActivityManager am = (ActivityManager) context.getSystemService(Context. ACTIVITY_SERVICE); |
88 int iconDimension = am.getLauncherLargeIconSize(); | 91 final int iconDimension = am.getLauncherLargeIconSize(); |
89 int iconDensity = am.getLauncherLargeIconDensity(); | 92 final int iconDensity = am.getLauncherLargeIconDensity(); |
90 try { | 93 try { |
91 bitmap = Bitmap.createBitmap(iconDimension, iconDimension, Bitmap.Co nfig.ARGB_8888); | 94 bitmap = Bitmap.createBitmap(iconDimension, iconDimension, Bitmap.Co nfig.ARGB_8888); |
92 Canvas canvas = new Canvas(bitmap); | 95 Canvas canvas = new Canvas(bitmap); |
93 Rect iconBounds = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight ()); | 96 Rect iconBounds = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight ()); |
94 if (favicon == null) { | 97 if (favicon == null) { |
95 favicon = getBitmapFromResourceId(context, R.drawable.globe_favi con, iconDensity); | 98 favicon = getBitmapFromResourceId(context, R.drawable.globe_favi con, iconDensity); |
96 rValue = gValue = bValue = DEFAULT_RGB_VALUE; | 99 rValue = gValue = bValue = DEFAULT_RGB_VALUE; |
97 } | 100 } |
98 Bitmap icon = getIconBackground(context, iconDensity); | 101 final int smallestSide = iconDimension; |
99 if (icon != null) { | 102 if (favicon.getWidth() >= smallestSide / 2 && favicon.getHeight() >= smallestSide / 2) { |
100 canvas.drawBitmap(icon, null, iconBounds, new Paint(Paint.ANTI_A LIAS_FLAG)); | 103 drawTouchIconToCanvas(context, favicon, canvas, iconBounds); |
104 } else { | |
105 Bitmap icon = getIconBackground(context, iconDensity); | |
106 if (icon != null) { | |
107 canvas.drawBitmap(icon, null, iconBounds, new Paint(Paint.AN TI_ALIAS_FLAG)); | |
108 } | |
109 drawFaviconToCanvas(context, favicon, canvas, iconBounds, rValue , gValue, bValue); | |
101 } | 110 } |
102 drawFaviconToCanvas(context, favicon, canvas, iconBounds, rValue, gV alue, bValue); | |
103 canvas.setBitmap(null); | 111 canvas.setBitmap(null); |
104 } catch (OutOfMemoryError e) { | 112 } catch (OutOfMemoryError e) { |
105 Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas." ); | 113 Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas." ); |
106 } | 114 } |
107 return bitmap; | 115 return bitmap; |
108 } | 116 } |
109 | 117 |
110 /** | 118 /** |
111 * Get the icon background asset. | 119 * Get the icon background asset. |
112 * @param context Context used to create the intent. | 120 * @param context Context used to create the intent. |
(...skipping 12 matching lines...) Expand all Loading... | |
125 } | 133 } |
126 if (drawable instanceof BitmapDrawable) { | 134 if (drawable instanceof BitmapDrawable) { |
127 BitmapDrawable bd = (BitmapDrawable) drawable; | 135 BitmapDrawable bd = (BitmapDrawable) drawable; |
128 return bd.getBitmap(); | 136 return bd.getBitmap(); |
129 } | 137 } |
130 assert false : "The drawable was not a bitmap drawable as expected"; | 138 assert false : "The drawable was not a bitmap drawable as expected"; |
131 return null; | 139 return null; |
132 } | 140 } |
133 | 141 |
134 /** | 142 /** |
143 * Use touch-icon or higher-resolution favicon and round the corners. | |
144 * @param context Context used to get resources. | |
145 * @param touchIcon Touch icon bitmap. | |
146 * @param canvas Canvas that holds the touch icon. | |
147 * @param iconBounds Rectangle bounds needed for adding rounded corners of t he touch icon. | |
148 */ | |
149 private static void drawTouchIconToCanvas( | |
150 Context context, Bitmap touchIcon, Canvas canvas, Rect iconBounds) { | |
151 Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight()); | |
152 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
153 paint.setFilterBitmap(true); | |
154 canvas.drawBitmap(touchIcon, src, iconBounds, paint); | |
Ted C
2013/02/26 22:44:42
purely for my understanding, what is the benefit o
aruslan
2013/02/27 00:18:09
It doesn't look like there is a way to anti-alias
| |
155 int borderRadii = | |
156 context.getResources().getDimensionPixelSize(R.dimen.touchicon_b order_radii); | |
157 Path path = new Path(); | |
158 path.setFillType(Path.FillType.INVERSE_WINDING); | |
159 RectF rect = new RectF(iconBounds); | |
160 rect.inset(INSET_DIMENSION_FOR_TOUCHICON, INSET_DIMENSION_FOR_TOUCHICON) ; | |
161 path.addRoundRect(rect, borderRadii, borderRadii, Path.Direction.CW); | |
162 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); | |
163 canvas.drawPath(path, paint); | |
164 } | |
165 | |
166 /** | |
135 * Draw the favicon with dominant color. | 167 * Draw the favicon with dominant color. |
136 * @param context Context used to create the intent. | 168 * @param context Context used to create the intent. |
137 * @param favicon favicon bitmap. | 169 * @param favicon favicon bitmap. |
138 * @param canvas Canvas that holds the favicon. | 170 * @param canvas Canvas that holds the favicon. |
139 * @param iconBounds Rectangle bounds needed to create the homescreen favico n. | 171 * @param iconBounds Rectangle bounds needed to create the homescreen favico n. |
140 * @param rValue Red component of the dominant favicon color. | 172 * @param rValue Red component of the dominant favicon color. |
141 * @param gValue Green component of the dominant favicon color. | 173 * @param gValue Green component of the dominant favicon color. |
142 * @param bValue Blue component of the dominant favicon color. | 174 * @param bValue Blue component of the dominant favicon color. |
143 */ | 175 */ |
144 private static void drawFaviconToCanvas(Context context, Bitmap favicon, | 176 private static void drawFaviconToCanvas(Context context, Bitmap favicon, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 Bitmap scaledFavicon = Bitmap.createScaledBitmap(favicon, faviconSiz e, faviconSize, | 244 Bitmap scaledFavicon = Bitmap.createScaledBitmap(favicon, faviconSiz e, faviconSize, |
213 true); | 245 true); |
214 canvas.drawBitmap(scaledFavicon, | 246 canvas.drawBitmap(scaledFavicon, |
215 iconBounds.exactCenterX() - scaledFavicon.getWidth() / 2.0f, | 247 iconBounds.exactCenterX() - scaledFavicon.getWidth() / 2.0f, |
216 iconBounds.exactCenterY() - scaledFavicon.getHeight() / 2.0f , null); | 248 iconBounds.exactCenterY() - scaledFavicon.getHeight() / 2.0f , null); |
217 } catch (OutOfMemoryError e) { | 249 } catch (OutOfMemoryError e) { |
218 Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas." ); | 250 Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas." ); |
219 } | 251 } |
220 } | 252 } |
221 } | 253 } |
OLD | NEW |