From fee45590d1f0b7cf3c7bfcbc014f835f24dcffd0 Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Sat, 15 Jul 2023 18:29:52 +0200 Subject: [PATCH] Use compat adaptive icons for themed icon packs --- .../integrations/icon-packs.md | 5 ++--- .../mm20/launcher2/icons/IconPackManager.kt | 20 +++++++++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/docs/docs/developer-guide/integrations/icon-packs.md b/docs/docs/developer-guide/integrations/icon-packs.md index 884ef2b1..c14cbb05 100644 --- a/docs/docs/developer-guide/integrations/icon-packs.md +++ b/docs/docs/developer-guide/integrations/icon-packs.md @@ -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 `` layer, this icon will be used as the themed icon. -- If the icon is an adaptive icon, and it does not have a `` 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 `` 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 diff --git a/services/icons/src/main/java/de/mm20/launcher2/icons/IconPackManager.kt b/services/icons/src/main/java/de/mm20/launcher2/icons/IconPackManager.kt index 4bd88b5a..b60ba164 100644 --- a/services/icons/src/main/java/de/mm20/launcher2/icons/IconPackManager.kt +++ b/services/icons/src/main/java/de/mm20/launcher2/icons/IconPackManager.kt @@ -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