1 /* 2 * Copyright 2021 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.google.android.enterprise.connectedapps.parcelablewrappers; 17 18 import android.graphics.Bitmap; 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 import com.google.android.enterprise.connectedapps.internal.Bundler; 22 import com.google.android.enterprise.connectedapps.internal.BundlerType; 23 24 /** Wrapper for reading & writing {@link Bitmap} instances from and to {@link Parcel} instances. */ 25 // Though Bitmap is itself Parcelable, in some circumstances the Parcelling process can fail (see 26 // b/159895007). 27 public class ParcelableBitmap implements Parcelable { 28 private final Bitmap bitmap; 29 30 /** Create a wrapper for a given bitmap. */ of(Bundler bundler, BundlerType type, Bitmap bitmap)31 public static ParcelableBitmap of(Bundler bundler, BundlerType type, Bitmap bitmap) { 32 return new ParcelableBitmap(bitmap); 33 } 34 ParcelableBitmap(Bitmap bitmap)35 private ParcelableBitmap(Bitmap bitmap) { 36 this.bitmap = bitmap; 37 } 38 ParcelableBitmap(Parcel in)39 private ParcelableBitmap(Parcel in) { 40 String configKey = in.readString(); 41 42 if (configKey == null) { 43 bitmap = null; 44 return; 45 } 46 47 Bitmap.Config config = Bitmap.Config.valueOf(configKey); 48 int width = in.readInt(); 49 int height = in.readInt(); 50 int[] colors = new int[width * height]; 51 in.readIntArray(colors); 52 53 bitmap = Bitmap.createBitmap(colors, width, height, config); 54 } 55 56 @Override writeToParcel(Parcel out, int flags)57 public void writeToParcel(Parcel out, int flags) { 58 if (bitmap == null) { 59 out.writeString(null); 60 return; 61 } 62 63 Bitmap.Config config = bitmap.getConfig(); 64 int width = bitmap.getWidth(); 65 int height = bitmap.getHeight(); 66 int[] colors = bitmapToPixelArray(bitmap); 67 68 out.writeString(config.toString()); 69 out.writeInt(width); 70 out.writeInt(height); 71 out.writeIntArray(colors); 72 } 73 74 @Override describeContents()75 public int describeContents() { 76 return 0; 77 } 78 get()79 public Bitmap get() { 80 return bitmap; 81 } 82 83 public static final Creator<ParcelableBitmap> CREATOR = 84 new Creator<ParcelableBitmap>() { 85 @Override 86 public ParcelableBitmap createFromParcel(Parcel in) { 87 return new ParcelableBitmap(in); 88 } 89 90 @Override 91 public ParcelableBitmap[] newArray(int size) { 92 return new ParcelableBitmap[size]; 93 } 94 }; 95 bitmapToPixelArray(Bitmap bitmap)96 private static int[] bitmapToPixelArray(Bitmap bitmap) { 97 int[] pixels = new int[bitmap.getHeight() * bitmap.getWidth()]; 98 bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight()); 99 return pixels; 100 } 101 } 102