1 /* 2 * Copyright (C) 2021 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.mono 17 18 import android.content.Context 19 import android.graphics.Bitmap 20 import android.graphics.BlendMode.SRC_IN 21 import android.graphics.BlendModeColorFilter 22 import android.graphics.Canvas 23 import android.graphics.Paint 24 import android.graphics.Rect 25 import com.android.launcher3.icons.BitmapInfo 26 import com.android.launcher3.icons.FastBitmapDrawable 27 import com.android.launcher3.icons.R 28 29 /** Class to handle monochrome themed app icons */ 30 class ThemedIconDrawable(constantState: ThemedConstantState) : 31 FastBitmapDrawable(constantState.getBitmapInfo()) { 32 private val colorFg = constantState.colorFg 33 private val colorBg = constantState.colorBg 34 35 // The foreground/monochrome icon for the app 36 private val monoIcon = constantState.mono 37 private val monoFilter = BlendModeColorFilter(colorFg, SRC_IN) 38 private val monoPaint = <lambda>null39 Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG).apply { colorFilter = monoFilter } 40 41 private val bgBitmap = constantState.whiteShadowLayer 42 private val bgFilter = BlendModeColorFilter(colorBg, SRC_IN) 43 private val mBgPaint = <lambda>null44 Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG).apply { colorFilter = bgFilter } 45 drawInternalnull46 override fun drawInternal(canvas: Canvas, bounds: Rect) { 47 canvas.drawBitmap(bgBitmap, null, bounds, mBgPaint) 48 canvas.drawBitmap(monoIcon, null, bounds, monoPaint) 49 } 50 updateFilternull51 override fun updateFilter() { 52 super.updateFilter() 53 val alpha = if (mIsDisabled) (mDisabledAlpha * FULLY_OPAQUE).toInt() else FULLY_OPAQUE 54 mBgPaint.alpha = alpha 55 mBgPaint.setColorFilter( 56 if (mIsDisabled) BlendModeColorFilter(getDisabledColor(colorBg), SRC_IN) else bgFilter 57 ) 58 59 monoPaint.alpha = alpha 60 monoPaint.setColorFilter( 61 if (mIsDisabled) BlendModeColorFilter(getDisabledColor(colorFg), SRC_IN) else monoFilter 62 ) 63 } 64 isThemednull65 override fun isThemed() = true 66 67 override fun newConstantState() = 68 ThemedConstantState(mBitmapInfo, monoIcon, bgBitmap, colorBg, colorFg) 69 70 override fun getIconColor() = colorFg 71 72 class ThemedConstantState( 73 bitmapInfo: BitmapInfo, 74 val mono: Bitmap, 75 val whiteShadowLayer: Bitmap, 76 val colorBg: Int, 77 val colorFg: Int, 78 ) : FastBitmapConstantState(bitmapInfo) { 79 80 public override fun createDrawable() = ThemedIconDrawable(this) 81 82 fun getBitmapInfo(): BitmapInfo = mBitmapInfo 83 } 84 85 companion object { 86 const val TAG: String = "ThemedIconDrawable" 87 88 /** Get an int array representing background and foreground colors for themed icons */ 89 @JvmStatic getColorsnull90 fun getColors(context: Context): IntArray { 91 val res = context.resources 92 return intArrayOf( 93 res.getColor(R.color.themed_icon_background_color), 94 res.getColor(R.color.themed_icon_color), 95 ) 96 } 97 } 98 } 99