Add plugin badges

This commit is contained in:
MM20 2023-12-07 20:56:57 +01:00
parent a94cc2ae01
commit 0f6d636ca0
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
4 changed files with 32 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package de.mm20.launcher2.search
import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Bundle
import de.mm20.launcher2.icons.LauncherIcon
import de.mm20.launcher2.icons.StaticLauncherIcon
@ -31,6 +32,7 @@ interface SavableSearchable : Searchable, Comparable<SavableSearchable> {
themed: Boolean
): LauncherIcon? = null
suspend fun getProviderIcon(context: Context): Drawable? = null
override fun compareTo(other: SavableSearchable): Int {
val label1 = labelOverride ?: label

View File

@ -19,6 +19,8 @@ import de.mm20.launcher2.search.FileMetaType
import de.mm20.launcher2.search.SavableSearchable
import de.mm20.launcher2.search.SearchableSerializer
import kotlinx.collections.immutable.ImmutableMap
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
data class PluginFile(
val id: String,
@ -54,6 +56,12 @@ data class PluginFile(
return PluginFileSerializer()
}
override suspend fun getProviderIcon(context: Context): Drawable? {
return withContext(Dispatchers.IO) {
context.packageManager.resolveContentProvider(authority, 0)?.loadIcon(context.packageManager)
}
}
override suspend fun loadIcon(context: Context, size: Int, themed: Boolean): LauncherIcon? {
if (thumbnailUri != null) {
val request = ImageRequest.Builder(context)

View File

@ -37,6 +37,7 @@ internal class BadgeServiceImpl(private val context: Context) : BadgeService, Ko
if (it.suspendedApps) {
providers += SuspendedAppsBadgeProvider()
}
providers += PluginBadgeProvider(context)
badgeProviders.value = providers
}
}

View File

@ -0,0 +1,21 @@
package de.mm20.launcher2.badges.providers
import android.content.Context
import de.mm20.launcher2.badges.Badge
import de.mm20.launcher2.search.SavableSearchable
import de.mm20.launcher2.search.Searchable
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
class PluginBadgeProvider(private val context: Context): BadgeProvider {
override fun getBadge(searchable: Searchable): Flow<Badge?> {
if (searchable !is SavableSearchable) return flowOf(null)
return flow {
val icon = searchable.getProviderIcon(context)
if (icon != null) {
emit(Badge(icon = icon))
}
}
}
}