1 /* 2 * Copyright (C) 2017 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.Bitmap.Config; 20 21 import androidx.annotation.NonNull; 22 23 public class BitmapInfo { 24 25 public static final Bitmap LOW_RES_ICON = Bitmap.createBitmap(1, 1, Config.ALPHA_8); 26 public static final BitmapInfo LOW_RES_INFO = fromBitmap(LOW_RES_ICON); 27 28 public final Bitmap icon; 29 public final int color; 30 BitmapInfo(Bitmap icon, int color)31 public BitmapInfo(Bitmap icon, int color) { 32 this.icon = icon; 33 this.color = color; 34 } 35 36 /** 37 * Ideally icon should not be null, except in cases when generating hardware bitmap failed 38 */ isNullOrLowRes()39 public final boolean isNullOrLowRes() { 40 return icon == null || icon == LOW_RES_ICON; 41 } 42 isLowRes()43 public final boolean isLowRes() { 44 return LOW_RES_ICON == icon; 45 } 46 fromBitmap(@onNull Bitmap bitmap)47 public static BitmapInfo fromBitmap(@NonNull Bitmap bitmap) { 48 return of(bitmap, 0); 49 } 50 of(@onNull Bitmap bitmap, int color)51 public static BitmapInfo of(@NonNull Bitmap bitmap, int color) { 52 return new BitmapInfo(bitmap, color); 53 } 54 55 /** 56 * Interface to be implemented by drawables to provide a custom BitmapInfo 57 */ 58 public interface Extender { 59 60 /** 61 * Called for creating a custom BitmapInfo 62 */ getExtendedInfo(Bitmap bitmap, int color, BaseIconFactory iconFactory)63 default BitmapInfo getExtendedInfo(Bitmap bitmap, int color, BaseIconFactory iconFactory) { 64 return BitmapInfo.of(bitmap, color); 65 } 66 67 /** 68 * Notifies the drawable that it will be drawn directly in the UI, without any preprocessing 69 */ prepareToDrawOnUi()70 default void prepareToDrawOnUi() { } 71 } 72 } 73