PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/appshortcuts/AppShortcutIconGenerator.kt

91 lines
3.3 KiB
Kotlin
Raw Normal View History

2019-03-03 09:20:15 +00:00
/*
2020-10-06 08:46:04 +00:00
* Copyright (c) 2020 Hemanth Savarla.
2019-03-03 09:20:15 +00:00
*
* Licensed under the GNU General Public License v3
*
2020-10-06 08:46:04 +00:00
* This is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
2019-03-03 09:20:15 +00:00
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
2020-10-06 08:46:04 +00:00
*
2019-03-03 09:20:15 +00:00
*/
2018-12-25 14:58:47 +00:00
package code.name.monkey.retromusic.appshortcuts
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import android.graphics.drawable.Icon
import android.graphics.drawable.LayerDrawable
2018-12-25 14:58:47 +00:00
import android.os.Build
import android.util.TypedValue
import androidx.annotation.RequiresApi
import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.retromusic.R
2020-06-06 18:57:28 +00:00
import code.name.monkey.retromusic.util.PreferenceUtil
import code.name.monkey.retromusic.util.RetroUtil
2018-12-25 14:58:47 +00:00
@RequiresApi(Build.VERSION_CODES.N_MR1)
object AppShortcutIconGenerator {
fun generateThemedIcon(context: Context, iconId: Int): Icon {
2020-06-06 18:57:28 +00:00
return if (PreferenceUtil.isColoredAppShortcuts) {
generateUserThemedIcon(context, iconId)
} else {
generateDefaultThemedIcon(context, iconId)
}
}
2018-12-25 14:58:47 +00:00
private fun generateDefaultThemedIcon(context: Context, iconId: Int): Icon {
// Return an Icon of iconId with default colors
return generateThemedIcon(
context,
iconId,
context.getColor(R.color.app_shortcut_default_foreground),
context.getColor(R.color.app_shortcut_default_background)
)
}
2018-12-25 14:58:47 +00:00
private fun generateUserThemedIcon(context: Context, iconId: Int): Icon {
// Get background color from context's theme
val typedColorBackground = TypedValue()
context.theme.resolveAttribute(android.R.attr.colorBackground, typedColorBackground, true)
2018-12-25 14:58:47 +00:00
// Return an Icon of iconId with those colors
return generateThemedIcon(
context, iconId, ThemeStore.accentColor(context), typedColorBackground.data
)
}
2018-12-25 14:58:47 +00:00
private fun generateThemedIcon(
2020-10-06 08:46:04 +00:00
context: Context,
iconId: Int,
foregroundColor: Int,
backgroundColor: Int
): Icon {
// Get and tint foreground and background drawables
val vectorDrawable = RetroUtil.getTintedVectorDrawable(context, iconId, foregroundColor)
val backgroundDrawable = RetroUtil.getTintedVectorDrawable(
context, R.drawable.ic_app_shortcut_background, backgroundColor
)
2018-12-25 14:58:47 +00:00
// Squash the two drawables together
val layerDrawable = LayerDrawable(arrayOf(backgroundDrawable, vectorDrawable))
2018-12-25 14:58:47 +00:00
// Return as an Icon
return Icon.createWithBitmap(drawableToBitmap(layerDrawable))
}
2018-12-25 14:58:47 +00:00
private fun drawableToBitmap(drawable: Drawable): Bitmap {
val bitmap = Bitmap.createBitmap(
drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
return bitmap
}
2018-12-25 14:58:47 +00:00
}