Use compat adaptive icons for themed icon packs

This commit is contained in:
MM20 2023-07-15 18:29:52 +02:00
parent f9cab47ca2
commit fee45590d1
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
2 changed files with 20 additions and 5 deletions

View File

@ -243,10 +243,9 @@ icons.
Themed icons can be provided in any of the following ways:
- If the icon is an adaptive icon, the device runs at least Android 13 and the drawable has
- If the icon is an adaptive icon, and the drawable has
a `<monochrome>` layer, this icon will be used as the themed icon.
- If the icon is an adaptive icon, and it does not have a `<monochrome>` layer or the device runs on
Android 12 or lower, the foreground layer will be used.
- If the icon is an adaptive icon, and it does not have a `<monochrome>` layer, the foreground layer will be used.
- If the icon is not an adaptive icon, the entire icon will be used.
In any case, the icon will be tinted using the theme color and the background will be set to a solid

View File

@ -19,6 +19,8 @@ import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.room.withTransaction
import de.mm20.launcher2.database.AppDatabase
import de.mm20.launcher2.icons.compat.AdaptiveIconDrawableCompat
import de.mm20.launcher2.icons.compat.toLauncherIcon
import de.mm20.launcher2.icons.loaders.AppFilterIconPackInstaller
import de.mm20.launcher2.icons.loaders.GrayscaleMapIconPackInstaller
import de.mm20.launcher2.ktx.isAtLeastApiLevel
@ -247,6 +249,11 @@ class IconPackManager(
val resId =
resources.getIdentifier(icon.drawable, "drawable", icon.iconPack).takeIf { it != 0 }
?: return null
val adaptiveIconCompat = AdaptiveIconDrawableCompat.from(resources, resId)
if (adaptiveIconCompat != null) {
return adaptiveIconCompat.toLauncherIcon(themed = allowThemed && icon.themed)
}
val drawable = try {
ResourcesCompat.getDrawable(resources, resId, context.theme) ?: return null
} catch (e: Resources.NotFoundException) {
@ -342,9 +349,18 @@ class IconPackManager(
resources: Resources,
allowThemed: Boolean,
): LauncherIcon? {
var drawable = try {
val drawableId = try {
resources.getIdentifier(icon.drawable, "drawable", icon.iconPack).takeIf { it != 0 }
?.let { ResourcesCompat.getDrawable(resources, it, null) }
?: return null
} catch (e: Resources.NotFoundException) {
return null
}
val adaptiveIconCompat = AdaptiveIconDrawableCompat.from(resources, drawableId)
if (adaptiveIconCompat != null) {
return adaptiveIconCompat.toLauncherIcon(icon.themed && allowThemed, icon.config)
}
val drawable = try {
ResourcesCompat.getDrawable(resources, drawableId, null)
} catch (e: Resources.NotFoundException) {
null
} ?: return null