1 /*
2  * Copyright (C) 2023 The Android Open Source Project
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  *      http://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 
17 package com.android.server.companion.datatransfer.contextsync;
18 
19 import android.graphics.Bitmap;
20 import android.graphics.Canvas;
21 import android.graphics.drawable.BitmapDrawable;
22 import android.graphics.drawable.Drawable;
23 
24 import java.io.ByteArrayOutputStream;
25 
26 /** Provides bitmap utility operations for rendering drawables to byte arrays. */
27 public class BitmapUtils {
28 
29     private static final int APP_ICON_BITMAP_DIMENSION = 256;
30 
31     /** Render a drawable to a bitmap, which is then reformatted as a byte array. */
renderDrawableToByteArray(Drawable drawable)32     public static byte[] renderDrawableToByteArray(Drawable drawable) {
33         if (drawable instanceof BitmapDrawable) {
34             // Can't recycle the drawable's bitmap, so handle separately
35             final Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
36             if (bitmap.getWidth() > APP_ICON_BITMAP_DIMENSION
37                     || bitmap.getHeight() > APP_ICON_BITMAP_DIMENSION) {
38                 // Downscale, as the original drawable bitmap is too large.
39                 final Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap,
40                         APP_ICON_BITMAP_DIMENSION, APP_ICON_BITMAP_DIMENSION, /* filter= */ true);
41                 final byte[] renderedBitmap = renderBitmapToByteArray(scaledBitmap);
42                 scaledBitmap.recycle();
43                 return renderedBitmap;
44             }
45             return renderBitmapToByteArray(bitmap);
46         }
47         final Bitmap bitmap = Bitmap.createBitmap(APP_ICON_BITMAP_DIMENSION,
48                 APP_ICON_BITMAP_DIMENSION,
49                 Bitmap.Config.ARGB_8888);
50         try {
51             final Canvas canvas = new Canvas(bitmap);
52             drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
53             drawable.draw(canvas);
54             return renderBitmapToByteArray(bitmap);
55         } finally {
56             bitmap.recycle();
57         }
58     }
59 
renderBitmapToByteArray(Bitmap bitmap)60     private static byte[] renderBitmapToByteArray(Bitmap bitmap) {
61         final ByteArrayOutputStream baos = new ByteArrayOutputStream(bitmap.getByteCount());
62         bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
63         return baos.toByteArray();
64     }
65 }
66