OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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.content.pm.PackageManager; | 10 import android.content.pm.PackageManager; |
11 import android.content.pm.ResolveInfo; | 11 import android.content.pm.ResolveInfo; |
12 import android.graphics.Bitmap; | 12 import android.graphics.Bitmap; |
| 13 import android.graphics.BitmapFactory; |
13 import android.graphics.Canvas; | 14 import android.graphics.Canvas; |
14 import android.graphics.Color; | 15 import android.graphics.Color; |
15 import android.graphics.Paint; | 16 import android.graphics.Paint; |
16 import android.graphics.Path; | 17 import android.graphics.Path; |
17 import android.graphics.PorterDuff; | 18 import android.graphics.PorterDuff; |
18 import android.graphics.PorterDuffXfermode; | 19 import android.graphics.PorterDuffXfermode; |
19 import android.graphics.Rect; | 20 import android.graphics.Rect; |
20 import android.graphics.RectF; | 21 import android.graphics.RectF; |
21 import android.graphics.drawable.BitmapDrawable; | 22 import android.graphics.drawable.BitmapDrawable; |
22 import android.graphics.drawable.Drawable; | 23 import android.graphics.drawable.Drawable; |
23 import android.net.Uri; | 24 import android.net.Uri; |
24 import android.os.Handler; | 25 import android.os.Handler; |
25 import android.os.Looper; | 26 import android.os.Looper; |
| 27 import android.text.TextUtils; |
26 import android.util.Base64; | 28 import android.util.Base64; |
27 import android.util.DisplayMetrics; | 29 import android.util.DisplayMetrics; |
28 import android.util.TypedValue; | 30 import android.util.TypedValue; |
29 | 31 |
30 import org.chromium.base.ApiCompatibilityUtils; | 32 import org.chromium.base.ApiCompatibilityUtils; |
31 import org.chromium.base.ApplicationStatus; | 33 import org.chromium.base.ApplicationStatus; |
32 import org.chromium.base.Log; | 34 import org.chromium.base.Log; |
33 import org.chromium.base.VisibleForTesting; | 35 import org.chromium.base.VisibleForTesting; |
34 import org.chromium.base.annotations.CalledByNative; | 36 import org.chromium.base.annotations.CalledByNative; |
35 import org.chromium.chrome.R; | 37 import org.chromium.chrome.R; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 * homescreen as soon as the shortcut is created. | 114 * homescreen as soon as the shortcut is created. |
113 */ | 115 */ |
114 @SuppressWarnings("unused") | 116 @SuppressWarnings("unused") |
115 @CalledByNative | 117 @CalledByNative |
116 private static void addShortcut(Context context, String url, String userTitl
e, String name, | 118 private static void addShortcut(Context context, String url, String userTitl
e, String name, |
117 String shortName, Bitmap icon, boolean isWebappCapable, int orientat
ion, int source, | 119 String shortName, Bitmap icon, boolean isWebappCapable, int orientat
ion, int source, |
118 long themeColor, long backgroundColor) { | 120 long themeColor, long backgroundColor) { |
119 Intent shortcutIntent; | 121 Intent shortcutIntent; |
120 if (isWebappCapable) { | 122 if (isWebappCapable) { |
121 // Encode the icon as a base64 string (Launcher drops Bitmaps in the
Intent). | 123 // Encode the icon as a base64 string (Launcher drops Bitmaps in the
Intent). |
122 String encodedIcon = ""; | 124 String encodedIcon = encodeBitmapAsString(icon); |
123 if (icon != null) { | |
124 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutpu
tStream(); | |
125 icon.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStr
eam); | |
126 byte[] byteArray = byteArrayOutputStream.toByteArray(); | |
127 encodedIcon = Base64.encodeToString(byteArray, Base64.DEFAULT); | |
128 } | |
129 | 125 |
130 // Add the shortcut as a launcher icon for a full-screen Activity. | 126 // Add the shortcut as a launcher icon for a full-screen Activity. |
131 shortcutIntent = new Intent(); | 127 shortcutIntent = new Intent(); |
132 shortcutIntent.setAction(sDelegate.getFullscreenAction()); | 128 shortcutIntent.setAction(sDelegate.getFullscreenAction()); |
133 shortcutIntent.putExtra(EXTRA_ICON, encodedIcon); | 129 shortcutIntent.putExtra(EXTRA_ICON, encodedIcon); |
134 shortcutIntent.putExtra(EXTRA_ID, UUID.randomUUID().toString()); | 130 shortcutIntent.putExtra(EXTRA_ID, UUID.randomUUID().toString()); |
135 shortcutIntent.putExtra(EXTRA_NAME, name); | 131 shortcutIntent.putExtra(EXTRA_NAME, name); |
136 shortcutIntent.putExtra(EXTRA_SHORT_NAME, shortName); | 132 shortcutIntent.putExtra(EXTRA_SHORT_NAME, shortName); |
137 shortcutIntent.putExtra(EXTRA_URL, url); | 133 shortcutIntent.putExtra(EXTRA_URL, url); |
138 shortcutIntent.putExtra(EXTRA_ORIENTATION, orientation); | 134 shortcutIntent.putExtra(EXTRA_ORIENTATION, orientation); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 drawWidgetBackgroundToCanvas(context, canvas, iconDensity, url, | 246 drawWidgetBackgroundToCanvas(context, canvas, iconDensity, url, |
251 Color.rgb(rValue, gValue, bValue)); | 247 Color.rgb(rValue, gValue, bValue)); |
252 } | 248 } |
253 canvas.setBitmap(null); | 249 canvas.setBitmap(null); |
254 } catch (OutOfMemoryError e) { | 250 } catch (OutOfMemoryError e) { |
255 Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas."
); | 251 Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas."
); |
256 } | 252 } |
257 return bitmap; | 253 return bitmap; |
258 } | 254 } |
259 | 255 |
| 256 /** |
| 257 * Compresses a bitmap into a PNG and converts into a Base64 encoded string. |
| 258 * The encoded string can be decoded using {@link decodeBitmapFromString(Str
ing)}. |
| 259 * @param bitmap The Bitmap to compress and encode. |
| 260 * @return the String encoding the Bitmap. |
| 261 */ |
| 262 public static String encodeBitmapAsString(Bitmap bitmap) { |
| 263 if (bitmap == null) return ""; |
| 264 ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 265 bitmap.compress(Bitmap.CompressFormat.PNG, 100, output); |
| 266 return Base64.encodeToString(output.toByteArray(), Base64.DEFAULT); |
| 267 } |
| 268 |
| 269 /** |
| 270 * Decodes a Base64 string into a Bitmap. Used to decode Bitmaps encoded by |
| 271 * {@link encodeBitmapAsString(Bitmap)}. |
| 272 * @param encodedString the Base64 String to decode. |
| 273 * @return the Bitmap which was encoded by the String. |
| 274 */ |
| 275 public static Bitmap decodeBitmapFromString(String encodedString) { |
| 276 if (TextUtils.isEmpty(encodedString)) return null; |
| 277 byte[] decoded = Base64.decode(encodedString, Base64.DEFAULT); |
| 278 return BitmapFactory.decodeByteArray(decoded, 0, decoded.length); |
| 279 } |
| 280 |
260 private static Bitmap getBitmapFromResourceId(Context context, int id, int d
ensity) { | 281 private static Bitmap getBitmapFromResourceId(Context context, int id, int d
ensity) { |
261 Drawable drawable = ApiCompatibilityUtils.getDrawableForDensity( | 282 Drawable drawable = ApiCompatibilityUtils.getDrawableForDensity( |
262 context.getResources(), id, density); | 283 context.getResources(), id, density); |
263 | 284 |
264 if (drawable instanceof BitmapDrawable) { | 285 if (drawable instanceof BitmapDrawable) { |
265 BitmapDrawable bd = (BitmapDrawable) drawable; | 286 BitmapDrawable bd = (BitmapDrawable) drawable; |
266 return bd.getBitmap(); | 287 return bd.getBitmap(); |
267 } | 288 } |
268 assert false : "The drawable was not a bitmap drawable as expected"; | 289 assert false : "The drawable was not a bitmap drawable as expected"; |
269 return null; | 290 return null; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 * @return String that can be used to verify that a WebappActivity is being
started by Chrome. | 348 * @return String that can be used to verify that a WebappActivity is being
started by Chrome. |
328 */ | 349 */ |
329 public static String getEncodedMac(Context context, String url) { | 350 public static String getEncodedMac(Context context, String url) { |
330 // The only reason we convert to a String here is because Android inexpl
icably eats a | 351 // The only reason we convert to a String here is because Android inexpl
icably eats a |
331 // byte[] when adding the shortcut -- the Bundle received by the launche
d Activity even | 352 // byte[] when adding the shortcut -- the Bundle received by the launche
d Activity even |
332 // lacks the key for the extra. | 353 // lacks the key for the extra. |
333 byte[] mac = WebappAuthenticator.getMacForUrl(context, url); | 354 byte[] mac = WebappAuthenticator.getMacForUrl(context, url); |
334 return Base64.encodeToString(mac, Base64.DEFAULT); | 355 return Base64.encodeToString(mac, Base64.DEFAULT); |
335 } | 356 } |
336 } | 357 } |
OLD | NEW |