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.Rect; | 16 import android.graphics.Rect; |
17 import android.graphics.RectF; | 17 import android.graphics.RectF; |
18 import android.graphics.drawable.BitmapDrawable; | 18 import android.graphics.drawable.BitmapDrawable; |
19 import android.graphics.drawable.Drawable; | 19 import android.graphics.drawable.Drawable; |
20 import android.net.Uri; | 20 import android.net.Uri; |
21 import android.os.Build; | 21 import android.os.Build; |
22 import android.util.Log; | 22 import android.util.Log; |
23 | 23 |
24 import org.chromium.content.app.AppResource; | 24 import org.chromium.chrome.R; |
25 | 25 |
26 /** | 26 /** |
27 * Util class for bookmarks. | 27 * Util class for bookmarks. |
28 */ | 28 */ |
29 public class BookmarkUtils { | 29 public class BookmarkUtils { |
30 | 30 |
31 // There is no public string defining this intent so if Home changes the val
ue, we | 31 // There is no public string defining this intent so if Home changes the val
ue, we |
32 // have to update this string. | 32 // have to update this string. |
33 private static final String INSTALL_SHORTCUT = "com.android.launcher.action.
INSTALL_SHORTCUT"; | 33 private static final String INSTALL_SHORTCUT = "com.android.launcher.action.
INSTALL_SHORTCUT"; |
34 private static final int DEFAULT_RGB_VALUE = 145; | 34 private static final int DEFAULT_RGB_VALUE = 145; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 int gValue, int bValue) { | 85 int gValue, int bValue) { |
86 Bitmap bitmap = null; | 86 Bitmap bitmap = null; |
87 ActivityManager am = (ActivityManager) context.getSystemService(Context.
ACTIVITY_SERVICE); | 87 ActivityManager am = (ActivityManager) context.getSystemService(Context.
ACTIVITY_SERVICE); |
88 int iconDimension = am.getLauncherLargeIconSize(); | 88 int iconDimension = am.getLauncherLargeIconSize(); |
89 int iconDensity = am.getLauncherLargeIconDensity(); | 89 int iconDensity = am.getLauncherLargeIconDensity(); |
90 try { | 90 try { |
91 bitmap = Bitmap.createBitmap(iconDimension, iconDimension, Bitmap.Co
nfig.ARGB_8888); | 91 bitmap = Bitmap.createBitmap(iconDimension, iconDimension, Bitmap.Co
nfig.ARGB_8888); |
92 Canvas canvas = new Canvas(bitmap); | 92 Canvas canvas = new Canvas(bitmap); |
93 Rect iconBounds = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight
()); | 93 Rect iconBounds = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight
()); |
94 if (favicon == null) { | 94 if (favicon == null) { |
95 assert AppResource.DRAWABLE_ICON_DEFAULT_FAVICON != 0; | 95 favicon = getBitmapFromResourceId(context, R.drawable.globe_favi
con, iconDensity); |
96 favicon = getBitmapFromResourceId(context, | |
97 AppResource.DRAWABLE_ICON_DEFAULT_FAVICON, iconDensity); | |
98 rValue = gValue = bValue = DEFAULT_RGB_VALUE; | 96 rValue = gValue = bValue = DEFAULT_RGB_VALUE; |
99 } | 97 } |
100 Bitmap icon = getIconBackground(context, iconDensity); | 98 Bitmap icon = getIconBackground(context, iconDensity); |
101 if (icon != null) { | 99 if (icon != null) { |
102 canvas.drawBitmap(icon, null, iconBounds, new Paint(Paint.ANTI_A
LIAS_FLAG)); | 100 canvas.drawBitmap(icon, null, iconBounds, new Paint(Paint.ANTI_A
LIAS_FLAG)); |
103 } | 101 } |
104 drawFaviconToCanvas(context, favicon, canvas, iconBounds, rValue, gV
alue, bValue); | 102 drawFaviconToCanvas(context, favicon, canvas, iconBounds, rValue, gV
alue, bValue); |
105 canvas.setBitmap(null); | 103 canvas.setBitmap(null); |
106 } catch (OutOfMemoryError e) { | 104 } catch (OutOfMemoryError e) { |
107 Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas."
); | 105 Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas."
); |
108 } | 106 } |
109 return bitmap; | 107 return bitmap; |
110 } | 108 } |
111 | 109 |
112 /** | 110 /** |
113 * Get the icon background asset. | 111 * Get the icon background asset. |
114 * @param context Context used to create the intent. | 112 * @param context Context used to create the intent. |
115 * @return Bitmap favicon background asset. | 113 * @return Bitmap favicon background asset. |
116 */ | 114 */ |
117 private static Bitmap getIconBackground(Context context, int density) { | 115 private static Bitmap getIconBackground(Context context, int density) { |
118 assert AppResource.MIPMAP_BOOKMARK_SHORTCUT_BACKGROUND != 0; | 116 return getBitmapFromResourceId(context, R.mipmap.homescreen_bg, density)
; |
119 return getBitmapFromResourceId(context, AppResource.MIPMAP_BOOKMARK_SHOR
TCUT_BACKGROUND, | |
120 density); | |
121 } | 117 } |
122 | 118 |
123 private static Bitmap getBitmapFromResourceId(Context context, int id, int d
ensity) { | 119 private static Bitmap getBitmapFromResourceId(Context context, int id, int d
ensity) { |
124 Drawable drawable = null; | 120 Drawable drawable = null; |
125 if (Build.VERSION.SDK_INT >= SDK_VERSION_FOR_ACCESS_TO_METHODS) { | 121 if (Build.VERSION.SDK_INT >= SDK_VERSION_FOR_ACCESS_TO_METHODS) { |
126 drawable = context.getResources().getDrawableForDensity(id, density)
; | 122 drawable = context.getResources().getDrawableForDensity(id, density)
; |
127 } else { | 123 } else { |
128 drawable = context.getResources().getDrawable(id); | 124 drawable = context.getResources().getDrawable(id); |
129 } | 125 } |
130 if (drawable instanceof BitmapDrawable) { | 126 if (drawable instanceof BitmapDrawable) { |
131 BitmapDrawable bd = (BitmapDrawable) drawable; | 127 BitmapDrawable bd = (BitmapDrawable) drawable; |
132 return bd.getBitmap(); | 128 return bd.getBitmap(); |
133 } | 129 } |
134 assert false : "The drawable was not a bitmap drawable as expected"; | 130 assert false : "The drawable was not a bitmap drawable as expected"; |
135 return null; | 131 return null; |
136 } | 132 } |
137 | 133 |
138 /** | 134 /** |
139 * Draw the favicon with dominant color. | 135 * Draw the favicon with dominant color. |
140 * @param context Context used to create the intent. | 136 * @param context Context used to create the intent. |
141 * @param favicon favicon bitmap. | 137 * @param favicon favicon bitmap. |
142 * @param canvas Canvas that holds the favicon. | 138 * @param canvas Canvas that holds the favicon. |
143 * @param iconBounds Rectangle bounds needed to create the homescreen favico
n. | 139 * @param iconBounds Rectangle bounds needed to create the homescreen favico
n. |
144 * @param rValue Red component of the dominant favicon color. | 140 * @param rValue Red component of the dominant favicon color. |
145 * @param gValue Green component of the dominant favicon color. | 141 * @param gValue Green component of the dominant favicon color. |
146 * @param bValue Blue component of the dominant favicon color. | 142 * @param bValue Blue component of the dominant favicon color. |
147 */ | 143 */ |
148 private static void drawFaviconToCanvas(Context context, Bitmap favicon, | 144 private static void drawFaviconToCanvas(Context context, Bitmap favicon, |
149 Canvas canvas, Rect iconBounds, int rValue, int gValue, int bValue)
{ | 145 Canvas canvas, Rect iconBounds, int rValue, int gValue, int bValue)
{ |
150 assert AppResource.DIMENSION_FAVICON_SIZE != 0; | |
151 assert AppResource.DIMENSION_FAVICON_COLORSTRIP_CORNER_RADII != 0; | |
152 assert AppResource.DIMENSION_FAVICON_COLORSTRIP_HEIGHT != 0; | |
153 assert AppResource.DIMENSION_FAVICON_COLORSTRIP_PADDING != 0; | |
154 assert AppResource.DIMENSION_FAVICON_COLORSTRIP_WIDTH != 0; | |
155 assert AppResource.DIMENSION_FAVICON_FOLD_BORDER != 0; | |
156 assert AppResource.DIMENSION_FAVICON_FOLD_CORNER_RADII != 0; | |
157 assert AppResource.DIMENSION_FAVICON_FOLD_SHADOW != 0; | |
158 assert AppResource.DIMENSION_FAVICON_FOLD_SIZE != 0; | |
159 | |
160 int colorStripWidth = context.getResources().getDimensionPixelSize( | 146 int colorStripWidth = context.getResources().getDimensionPixelSize( |
161 AppResource.DIMENSION_FAVICON_COLORSTRIP_WIDTH); | 147 R.dimen.favicon_colorstrip_width); |
162 int colorStripHeight = context.getResources().getDimensionPixelSize( | 148 int colorStripHeight = context.getResources().getDimensionPixelSize( |
163 AppResource.DIMENSION_FAVICON_COLORSTRIP_HEIGHT); | 149 R.dimen.favicon_colorstrip_height); |
164 int colorStripPadding = context.getResources().getDimensionPixelSize( | 150 int colorStripPadding = context.getResources().getDimensionPixelSize( |
165 AppResource.DIMENSION_FAVICON_COLORSTRIP_PADDING); | 151 R.dimen.favicon_colorstrip_padding); |
166 int colorStripCornerRadii = context.getResources().getDimensionPixelSize
( | 152 int colorStripCornerRadii = context.getResources().getDimensionPixelSize
( |
167 AppResource.DIMENSION_FAVICON_COLORSTRIP_CORNER_RADII); | 153 R.dimen.favicon_colorstrip_corner_radii); |
168 int foldSize = context.getResources().getDimensionPixelSize( | 154 int foldSize = context.getResources().getDimensionPixelSize(R.dimen.favi
con_fold_size); |
169 AppResource.DIMENSION_FAVICON_FOLD_SIZE); | |
170 int foldCornerRadii = context.getResources().getDimensionPixelSize( | 155 int foldCornerRadii = context.getResources().getDimensionPixelSize( |
171 AppResource.DIMENSION_FAVICON_FOLD_CORNER_RADII); | 156 R.dimen.favicon_fold_corner_radii); |
172 int foldBorder = context.getResources().getDimensionPixelSize( | 157 int foldBorder = context.getResources().getDimensionPixelSize(R.dimen.fa
vicon_fold_border); |
173 AppResource.DIMENSION_FAVICON_FOLD_BORDER); | 158 int foldShadow = context.getResources().getDimensionPixelSize(R.dimen.fa
vicon_fold_shadow); |
174 int foldShadow = context.getResources().getDimensionPixelSize( | 159 int faviconSize = context.getResources().getDimensionPixelSize(R.dimen.f
avicon_size); |
175 AppResource.DIMENSION_FAVICON_FOLD_SHADOW); | |
176 int faviconSize = context.getResources().getDimensionPixelSize( | |
177 AppResource.DIMENSION_FAVICON_SIZE); | |
178 | 160 |
179 float x1 = iconBounds.exactCenterX() - (colorStripWidth / 2.0f); | 161 float x1 = iconBounds.exactCenterX() - (colorStripWidth / 2.0f); |
180 float x2 = x1 + colorStripWidth; | 162 float x2 = x1 + colorStripWidth; |
181 float y1 = iconBounds.height() - colorStripPadding; | 163 float y1 = iconBounds.height() - colorStripPadding; |
182 float y2 = y1 - colorStripHeight; | 164 float y2 = y1 - colorStripHeight; |
183 int faviconColorAlpha100 = Color.argb(255, rValue, gValue, bValue); | 165 int faviconColorAlpha100 = Color.argb(255, rValue, gValue, bValue); |
184 Paint stripPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | 166 Paint stripPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
185 stripPaint.setColor(faviconColorAlpha100); | 167 stripPaint.setColor(faviconColorAlpha100); |
186 stripPaint.setStyle(Paint.Style.FILL); | 168 stripPaint.setStyle(Paint.Style.FILL); |
187 Path stripPath = new Path(); | 169 Path stripPath = new Path(); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 Bitmap scaledFavicon = Bitmap.createScaledBitmap(favicon, faviconSiz
e, faviconSize, | 212 Bitmap scaledFavicon = Bitmap.createScaledBitmap(favicon, faviconSiz
e, faviconSize, |
231 true); | 213 true); |
232 canvas.drawBitmap(scaledFavicon, | 214 canvas.drawBitmap(scaledFavicon, |
233 iconBounds.exactCenterX() - scaledFavicon.getWidth() / 2.0f, | 215 iconBounds.exactCenterX() - scaledFavicon.getWidth() / 2.0f, |
234 iconBounds.exactCenterY() - scaledFavicon.getHeight() / 2.0f
, null); | 216 iconBounds.exactCenterY() - scaledFavicon.getHeight() / 2.0f
, null); |
235 } catch (OutOfMemoryError e) { | 217 } catch (OutOfMemoryError e) { |
236 Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas."
); | 218 Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas."
); |
237 } | 219 } |
238 } | 220 } |
239 } | 221 } |
OLD | NEW |