1 /*
2  * Copyright (C) 2018 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 package com.android.launcher3.icons;
17 
18 import android.graphics.Bitmap;
19 import android.graphics.Rect;
20 import android.graphics.Region;
21 import android.graphics.RegionIterator;
22 import android.util.Log;
23 
24 import androidx.annotation.ColorInt;
25 
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 
29 public class GraphicsUtils {
30 
31     private static final String TAG = "GraphicsUtils";
32 
33     public static Runnable sOnNewBitmapRunnable = () -> { };
34 
35     /**
36      * Set the alpha component of {@code color} to be {@code alpha}. Unlike the support lib version,
37      * it bounds the alpha in valid range instead of throwing an exception to allow for safer
38      * interpolation of color animations
39      */
40     @ColorInt
setColorAlphaBound(int color, int alpha)41     public static int setColorAlphaBound(int color, int alpha) {
42         if (alpha < 0) {
43             alpha = 0;
44         } else if (alpha > 255) {
45             alpha = 255;
46         }
47         return (color & 0x00ffffff) | (alpha << 24);
48     }
49 
50     /**
51      * Compresses the bitmap to a byte array for serialization.
52      */
flattenBitmap(Bitmap bitmap)53     public static byte[] flattenBitmap(Bitmap bitmap) {
54         // Try go guesstimate how much space the icon will take when serialized
55         // to avoid unnecessary allocations/copies during the write (4 bytes per pixel).
56         int size = bitmap.getWidth() * bitmap.getHeight() * 4;
57         ByteArrayOutputStream out = new ByteArrayOutputStream(size);
58         try {
59             bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
60             out.flush();
61             out.close();
62             return out.toByteArray();
63         } catch (IOException e) {
64             Log.w(TAG, "Could not write bitmap");
65             return null;
66         }
67     }
68 
getArea(Region r)69     public static int getArea(Region r) {
70         RegionIterator itr = new RegionIterator(r);
71         int area = 0;
72         Rect tempRect = new Rect();
73         while (itr.next(tempRect)) {
74             area += tempRect.width() * tempRect.height();
75         }
76         return area;
77     }
78 
79     /**
80      * Utility method to track new bitmap creation
81      */
noteNewBitmapCreated()82     public static void noteNewBitmapCreated() {
83         sOnNewBitmapRunnable.run();
84     }
85 }
86