Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(307)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java

Issue 1286973003: webapps: introduce helper class to store extended set of data (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix assert in callback Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 = icon == null ? "" : encodeBitmapAsString(icon);
gone 2015/08/27 18:24:20 nit: This icon == null check should be at the begi
Lalit Maganti 2015/08/27 18:36:56 Done.
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
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 ByteArrayOutputStream output = new ByteArrayOutputStream();
264 bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
265 return Base64.encodeToString(output.toByteArray(), Base64.DEFAULT);
266 }
267
268 /**
269 * Decodes a Base64 string into a Bitmap. Used to decode Bitmaps encoded by
270 * {@link encodeBitmapAsString(Bitmap)}.
271 * @param encodedString the Base64 String to decode.
272 * @return the Bitmap which was encoded by the String.
273 */
274 public static Bitmap decodeBitmapFromString(String encodedString) {
275 if (TextUtils.isEmpty(encodedString)) return null;
276 byte[] decoded = Base64.decode(encodedString, Base64.DEFAULT);
277 return BitmapFactory.decodeByteArray(decoded, 0, decoded.length);
278 }
279
260 private static Bitmap getBitmapFromResourceId(Context context, int id, int d ensity) { 280 private static Bitmap getBitmapFromResourceId(Context context, int id, int d ensity) {
261 Drawable drawable = ApiCompatibilityUtils.getDrawableForDensity( 281 Drawable drawable = ApiCompatibilityUtils.getDrawableForDensity(
262 context.getResources(), id, density); 282 context.getResources(), id, density);
263 283
264 if (drawable instanceof BitmapDrawable) { 284 if (drawable instanceof BitmapDrawable) {
265 BitmapDrawable bd = (BitmapDrawable) drawable; 285 BitmapDrawable bd = (BitmapDrawable) drawable;
266 return bd.getBitmap(); 286 return bd.getBitmap();
267 } 287 }
268 assert false : "The drawable was not a bitmap drawable as expected"; 288 assert false : "The drawable was not a bitmap drawable as expected";
269 return null; 289 return null;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 * @return String that can be used to verify that a WebappActivity is being started by Chrome. 347 * @return String that can be used to verify that a WebappActivity is being started by Chrome.
328 */ 348 */
329 public static String getEncodedMac(Context context, String url) { 349 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 350 // 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 351 // byte[] when adding the shortcut -- the Bundle received by the launche d Activity even
332 // lacks the key for the extra. 352 // lacks the key for the extra.
333 byte[] mac = WebappAuthenticator.getMacForUrl(context, url); 353 byte[] mac = WebappAuthenticator.getMacForUrl(context, url);
334 return Base64.encodeToString(mac, Base64.DEFAULT); 354 return Base64.encodeToString(mac, Base64.DEFAULT);
335 } 355 }
336 } 356 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698