From c17311436843e15eeb069b645fcf83ca911b8a61 Mon Sep 17 00:00:00 2001 From: okhowang <3352585+okhowang@users.noreply.github.com> Date: Sun, 21 Nov 2021 17:41:21 +0800 Subject: [PATCH] Fix: crash with app icon (#1260) --- .../java/com/github/kr328/clash/common/compat/App.kt | 11 +++++++++++ .../java/com/github/kr328/clash/design/util/App.kt | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/com/github/kr328/clash/common/compat/App.kt b/common/src/main/java/com/github/kr328/clash/common/compat/App.kt index 87f8a78a..cb1b536c 100644 --- a/common/src/main/java/com/github/kr328/clash/common/compat/App.kt +++ b/common/src/main/java/com/github/kr328/clash/common/compat/App.kt @@ -2,6 +2,8 @@ package com.github.kr328.clash.common.compat import android.app.ActivityThread import android.app.Application +import android.graphics.drawable.AdaptiveIconDrawable +import android.graphics.drawable.Drawable import android.os.Build import com.github.kr328.clash.common.log.Log @@ -18,3 +20,12 @@ val Application.currentProcessName: String packageName } } + +fun Drawable.foreground(): Drawable { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && + this is AdaptiveIconDrawable && this.background == null + ) { + return this.foreground + } + return this +} diff --git a/design/src/main/java/com/github/kr328/clash/design/util/App.kt b/design/src/main/java/com/github/kr328/clash/design/util/App.kt index ecbd93a7..5dfeaee4 100644 --- a/design/src/main/java/com/github/kr328/clash/design/util/App.kt +++ b/design/src/main/java/com/github/kr328/clash/design/util/App.kt @@ -2,12 +2,13 @@ package com.github.kr328.clash.design.util import android.content.pm.PackageInfo import android.content.pm.PackageManager +import com.github.kr328.clash.common.compat.foreground import com.github.kr328.clash.design.model.AppInfo fun PackageInfo.toAppInfo(pm: PackageManager): AppInfo { return AppInfo( packageName = packageName, - icon = applicationInfo.loadIcon(pm), + icon = applicationInfo.loadIcon(pm).foreground(), label = applicationInfo.loadLabel(pm).toString(), installTime = firstInstallTime, updateDate = lastUpdateTime,