diff --git a/library/src/main/kotlin/dev/jahir/blueprint/data/models/RequestApp.kt b/library/src/main/kotlin/dev/jahir/blueprint/data/models/RequestApp.kt index 348c7e5ea..cd9af38aa 100644 --- a/library/src/main/kotlin/dev/jahir/blueprint/data/models/RequestApp.kt +++ b/library/src/main/kotlin/dev/jahir/blueprint/data/models/RequestApp.kt @@ -14,7 +14,7 @@ data class RequestApp(val name: String, val packageName: String, val component: var icon: Drawable? = null private set - suspend fun loadIcon(context: Context?) { + fun loadIcon(context: Context?) { context ?: return if (icon != null) return icon = context.getAppIcon(packageName) diff --git a/library/src/main/kotlin/dev/jahir/blueprint/data/viewmodels/RequestsViewModel.kt b/library/src/main/kotlin/dev/jahir/blueprint/data/viewmodels/RequestsViewModel.kt index 526fb95e0..7a41ea714 100644 --- a/library/src/main/kotlin/dev/jahir/blueprint/data/viewmodels/RequestsViewModel.kt +++ b/library/src/main/kotlin/dev/jahir/blueprint/data/viewmodels/RequestsViewModel.kt @@ -178,7 +178,6 @@ class RequestsViewModel(application: Application) : AndroidViewModel(application riPkg, component ) - app.loadIcon(context) installedApps.add(app) loaded += 1 } @@ -201,6 +200,7 @@ class RequestsViewModel(application: Application) : AndroidViewModel(application delay(10) val appsToRequest = loadAppsToRequest(debug) appsToRequestData.postValue(appsToRequest) + appsToRequest.forEach { it.loadIcon(context) } } } @@ -295,4 +295,4 @@ class RequestsViewModel(application: Application) : AndroidViewModel(application appsToRequestData.removeObservers(owner) selectedAppsData.removeObservers(owner) } -} \ No newline at end of file +} diff --git a/library/src/main/kotlin/dev/jahir/blueprint/extensions/AppIcon.kt b/library/src/main/kotlin/dev/jahir/blueprint/extensions/AppIcon.kt index d50dd15f5..cac756fe2 100644 --- a/library/src/main/kotlin/dev/jahir/blueprint/extensions/AppIcon.kt +++ b/library/src/main/kotlin/dev/jahir/blueprint/extensions/AppIcon.kt @@ -4,48 +4,55 @@ import android.content.Context import android.content.pm.ApplicationInfo import android.content.res.Resources import android.graphics.drawable.Drawable +import android.os.Build import android.util.DisplayMetrics -import androidx.annotation.DrawableRes import androidx.core.content.res.ResourcesCompat import dev.jahir.blueprint.R import dev.jahir.frames.extensions.context.drawable import dev.jahir.frames.extensions.context.string import dev.jahir.frames.extensions.resources.hasContent - -@DrawableRes -fun Context.getAppIconResId(pkg: String): Int = getAppInfo(pkg)?.icon ?: 0 +fun getDefaultAppIcon(): Drawable? { + return Resources.getSystem().getAppIcon(android.R.mipmap.sym_def_app_icon) +} @Suppress("USELESS_ELVIS") fun Context.getAppIcon(pkg: String): Drawable? { if (!pkg.hasContent()) return null - return try { - loadIcon(pkg) - ?: drawable(getAppIconResId(pkg)) - ?: packageManager.getApplicationIcon(pkg) - ?: drawable(string(R.string.icons_placeholder)) - ?: drawable(R.drawable.ic_na_launcher) + var icon: Drawable? = null + try { + icon = loadIcon(pkg) + if (icon == null) icon = packageManager.getApplicationIcon(pkg) } catch (e: Exception) { - null + if (icon == null) icon = drawable(string(R.string.icons_placeholder)) + if (icon == null) icon = drawable(R.drawable.ic_na_launcher) + if (icon == null) icon = getDefaultAppIcon() } + return icon } private fun Context.loadIcon(pkg: String): Drawable? { - try { + return try { val ai = getAppInfo(pkg) if (ai != null) { var icon = ai.loadIcon(packageManager) if (icon == null) icon = getResources(ai)?.getAppIcon(ai.icon) return icon - } + } else null } catch (e: Exception) { + null } - return null } private fun Resources.getAppIcon(iconId: Int): Drawable? { return try { - ResourcesCompat.getDrawableForDensity(this, iconId, DisplayMetrics.DENSITY_XXXHIGH, null) + ResourcesCompat.getDrawableForDensity( + this, + iconId, + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) DisplayMetrics.DENSITY_DEVICE_STABLE + else DisplayMetrics.DENSITY_DEFAULT, + null + ) } catch (e: Exception) { null } diff --git a/library/src/main/kotlin/dev/jahir/blueprint/ui/viewholders/IconViewHolder.kt b/library/src/main/kotlin/dev/jahir/blueprint/ui/viewholders/IconViewHolder.kt index c4e773628..25241c43e 100644 --- a/library/src/main/kotlin/dev/jahir/blueprint/ui/viewholders/IconViewHolder.kt +++ b/library/src/main/kotlin/dev/jahir/blueprint/ui/viewholders/IconViewHolder.kt @@ -70,4 +70,4 @@ class IconViewHolder(itemView: View) : SectionedViewHolder(itemView) { internal const val ICON_ANIMATION_DELAY: Long = 50L internal const val ICON_ANIMATION_DURATION: Long = 250L } -} \ No newline at end of file +} diff --git a/library/src/main/kotlin/dev/jahir/blueprint/ui/viewholders/RequestViewHolder.kt b/library/src/main/kotlin/dev/jahir/blueprint/ui/viewholders/RequestViewHolder.kt index 0b5ad69ec..4ae410746 100644 --- a/library/src/main/kotlin/dev/jahir/blueprint/ui/viewholders/RequestViewHolder.kt +++ b/library/src/main/kotlin/dev/jahir/blueprint/ui/viewholders/RequestViewHolder.kt @@ -44,4 +44,4 @@ class RequestViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { } } } -} \ No newline at end of file +}