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

Unified 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 minor issue Created 5 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDataStorage.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java b/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java
index ecb2acdea553ba595f9bc8bddd9d754555f935c8..e274e12fdfd6a1d426085a661718498a8d8be92e 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java
@@ -10,6 +10,7 @@ import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
@@ -23,6 +24,7 @@ import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
+import android.text.TextUtils;
import android.util.Base64;
import android.util.DisplayMetrics;
import android.util.TypedValue;
@@ -119,13 +121,7 @@ public class ShortcutHelper {
Intent shortcutIntent;
if (isWebappCapable) {
// Encode the icon as a base64 string (Launcher drops Bitmaps in the Intent).
- String encodedIcon = "";
- if (icon != null) {
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- icon.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
- byte[] byteArray = byteArrayOutputStream.toByteArray();
- encodedIcon = Base64.encodeToString(byteArray, Base64.DEFAULT);
- }
+ String encodedIcon = encodeBitmapAsString(icon);
// Add the shortcut as a launcher icon for a full-screen Activity.
shortcutIntent = new Intent();
@@ -257,6 +253,31 @@ public class ShortcutHelper {
return bitmap;
}
+ /**
+ * Compresses a bitmap into a PNG and converts into a Base64 encoded string.
+ * The encoded string can be decoded using {@link decodeBitmapFromString(String)}.
+ * @param bitmap The Bitmap to compress and encode.
+ * @return the String encoding the Bitmap.
+ */
+ public static String encodeBitmapAsString(Bitmap bitmap) {
+ if (bitmap == null) return "";
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
+ return Base64.encodeToString(output.toByteArray(), Base64.DEFAULT);
+ }
+
+ /**
+ * Decodes a Base64 string into a Bitmap. Used to decode Bitmaps encoded by
+ * {@link encodeBitmapAsString(Bitmap)}.
+ * @param encodedString the Base64 String to decode.
+ * @return the Bitmap which was encoded by the String.
+ */
+ public static Bitmap decodeBitmapFromString(String encodedString) {
+ if (TextUtils.isEmpty(encodedString)) return null;
+ byte[] decoded = Base64.decode(encodedString, Base64.DEFAULT);
+ return BitmapFactory.decodeByteArray(decoded, 0, decoded.length);
+ }
+
private static Bitmap getBitmapFromResourceId(Context context, int id, int density) {
Drawable drawable = ApiCompatibilityUtils.getDrawableForDensity(
context.getResources(), id, density);
« no previous file with comments | « no previous file | chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDataStorage.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698