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.graphics.Bitmap.Config; 20 import android.graphics.Canvas; 21 import android.graphics.drawable.BitmapDrawable; 22 import android.graphics.drawable.Drawable; 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 import com.google.android.enterprise.connectedapps.internal.Bundler; 26 import com.google.android.enterprise.connectedapps.internal.BundlerType; 27 28 /** 29 * Wrapper for reading & writing {@link Drawable} instances from and to {@link Parcel} instances. 30 * 31 * <p>Note that all {@link Drawable} instances are converted to {@link Bitmap} when parcelling. 32 */ 33 public class ParcelableDrawable implements Parcelable { 34 35 private static final int NULL = -1; 36 private static final int NOT_NULL = 1; 37 38 private final Drawable drawable; 39 40 /** Create a wrapper for a given drawable. */ of(Bundler bundler, BundlerType type, Drawable drawable)41 public static ParcelableDrawable of(Bundler bundler, BundlerType type, Drawable drawable) { 42 return new ParcelableDrawable(drawable); 43 } 44 get()45 public Drawable get() { 46 return drawable; 47 } 48 ParcelableDrawable(Drawable drawable)49 private ParcelableDrawable(Drawable drawable) { 50 this.drawable = drawable; 51 } 52 ParcelableDrawable(Parcel in)53 private ParcelableDrawable(Parcel in) { 54 int present = in.readInt(); 55 56 if (present == NULL) { 57 drawable = null; 58 return; 59 } 60 61 Bitmap bitmap = (Bitmap) in.readParcelable(Bundler.class.getClassLoader()); 62 drawable = new BitmapDrawable(bitmap); 63 } 64 65 @Override writeToParcel(Parcel dest, int flags)66 public void writeToParcel(Parcel dest, int flags) { 67 if (drawable == null) { 68 dest.writeInt(NULL); 69 return; 70 } 71 72 dest.writeInt(NOT_NULL); 73 74 Bitmap bitmap = drawableToBitmap(drawable); 75 dest.writeParcelable(bitmap, /* flags= */ flags); 76 } 77 78 @Override describeContents()79 public int describeContents() { 80 return 0; 81 } 82 83 @SuppressWarnings("rawtypes") 84 public static final Creator<ParcelableDrawable> CREATOR = 85 new Creator<ParcelableDrawable>() { 86 @Override 87 public ParcelableDrawable createFromParcel(Parcel in) { 88 return new ParcelableDrawable(in); 89 } 90 91 @Override 92 public ParcelableDrawable[] newArray(int size) { 93 return new ParcelableDrawable[size]; 94 } 95 }; 96 drawableToBitmap(Drawable drawable)97 private static Bitmap drawableToBitmap(Drawable drawable) { 98 if (drawable instanceof BitmapDrawable) { 99 return ((BitmapDrawable) drawable).getBitmap(); 100 } 101 102 Bitmap bitmap = 103 Bitmap.createBitmap( 104 drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888); 105 drawable.draw(new Canvas(bitmap)); 106 107 return bitmap; 108 } 109 } 110