summaryrefslogtreecommitdiffstats
path: root/iconloaderlib
diff options
context:
space:
mode:
authorSunny Goyal <sunnygoyal@google.com>2018-11-06 10:28:37 -0800
committerSunny Goyal <sunnygoyal@google.com>2018-11-06 14:51:55 -0800
commite62d2bb165498521f4033d40d972076ee17f4ff6 (patch)
tree32aad269525675bac376cac305aef3386083c2f8 /iconloaderlib
parent066ace1b8897229445c0fb3515156ef70bdb05e2 (diff)
downloadandroid_packages_apps_Trebuchet-e62d2bb165498521f4033d40d972076ee17f4ff6.tar.gz
android_packages_apps_Trebuchet-e62d2bb165498521f4033d40d972076ee17f4ff6.tar.bz2
android_packages_apps_Trebuchet-e62d2bb165498521f4033d40d972076ee17f4ff6.zip
Removing some Launcher3 dependencies from BaseIconCache
Change-Id: Ic80ed4a5cd2fc414cd6c27096d798e7f0b8efc72
Diffstat (limited to 'iconloaderlib')
-rw-r--r--iconloaderlib/src/com/android/launcher3/icons/GraphicsUtils.java27
1 files changed, 27 insertions, 0 deletions
diff --git a/iconloaderlib/src/com/android/launcher3/icons/GraphicsUtils.java b/iconloaderlib/src/com/android/launcher3/icons/GraphicsUtils.java
index b096cecb5..11d5eef52 100644
--- a/iconloaderlib/src/com/android/launcher3/icons/GraphicsUtils.java
+++ b/iconloaderlib/src/com/android/launcher3/icons/GraphicsUtils.java
@@ -15,10 +15,18 @@
*/
package com.android.launcher3.icons;
+import android.graphics.Bitmap;
+import android.util.Log;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
import androidx.annotation.ColorInt;
public class GraphicsUtils {
+ private static final String TAG = "GraphicsUtils";
+
/**
* Set the alpha component of {@code color} to be {@code alpha}. Unlike the support lib version,
* it bounds the alpha in valid range instead of throwing an exception to allow for safer
@@ -33,4 +41,23 @@ public class GraphicsUtils {
}
return (color & 0x00ffffff) | (alpha << 24);
}
+
+ /**
+ * Compresses the bitmap to a byte array for serialization.
+ */
+ public static byte[] flattenBitmap(Bitmap bitmap) {
+ // Try go guesstimate how much space the icon will take when serialized
+ // to avoid unnecessary allocations/copies during the write (4 bytes per pixel).
+ int size = bitmap.getWidth() * bitmap.getHeight() * 4;
+ ByteArrayOutputStream out = new ByteArrayOutputStream(size);
+ try {
+ bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
+ out.flush();
+ out.close();
+ return out.toByteArray();
+ } catch (IOException e) {
+ Log.w(TAG, "Could not write bitmap");
+ return null;
+ }
+ }
}