Clean up unused badge code

This commit is contained in:
MM20 2022-01-15 15:41:29 +01:00
parent 526f848f97
commit cd3ee6f3cb
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
3 changed files with 22 additions and 187 deletions

View File

@ -3,11 +3,12 @@ package de.mm20.launcher2.search.data
import android.content.Context
import android.content.Intent
import android.content.pm.LauncherApps
import android.content.pm.PackageManager
import android.content.pm.ShortcutInfo
import android.graphics.drawable.AdaptiveIconDrawable
import android.graphics.drawable.ColorDrawable
import android.os.*
import android.os.Build
import android.os.Bundle
import android.os.Process
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
@ -18,20 +19,18 @@ import de.mm20.launcher2.ktx.isAtLeastApiLevel
import de.mm20.launcher2.preferences.LauncherPreferences
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.lang.IllegalStateException
@RequiresApi(Build.VERSION_CODES.N_MR1)
class AppShortcut(
context: Context,
val launcherShortcut: ShortcutInfo,
val appName: String
context: Context,
val launcherShortcut: ShortcutInfo,
val appName: String
) : Searchable() {
override val label: String
get() = launcherShortcut.shortLabel?.toString() ?: ""
internal val userSerialNumber: Long = launcherShortcut.userHandle.getSerialNumber(context)
private val isMainProfile = launcherShortcut.userHandle == Process.myUserHandle()
@ -44,11 +43,7 @@ class AppShortcut(
override val badgeKey: String
get() {
return if (LauncherPreferences.instance.shortcutBadges) {
if (isMainProfile) "shortcut://${launcherShortcut.activity?.flattenToShortString()}" else "profile://$userSerialNumber"
} else {
"null"
}
return if (isMainProfile) "shortcut://${launcherShortcut.activity?.flattenToShortString()}" else "profile://$userSerialNumber"
}
override fun getLaunchIntent(context: Context): Intent? {
@ -67,28 +62,32 @@ class AppShortcut(
override fun getPlaceholderIcon(context: Context): LauncherIcon {
return LauncherIcon(
foreground = ContextCompat.getDrawable(context, R.drawable.ic_file_android)!!,
background = ColorDrawable(ContextCompat.getColor(context, R.color.green)),
foregroundScale = 0.5f)
foreground = ContextCompat.getDrawable(context, R.drawable.ic_file_android)!!,
background = ColorDrawable(ContextCompat.getColor(context, R.color.green)),
foregroundScale = 0.5f
)
}
override suspend fun loadIcon(context: Context, size: Int): LauncherIcon? {
val launcherApps = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
val icon = withContext(Dispatchers.IO) {
launcherApps.getShortcutIconDrawable(launcherShortcut, context.resources.displayMetrics.densityDpi)
launcherApps.getShortcutIconDrawable(
launcherShortcut,
context.resources.displayMetrics.densityDpi
)
} ?: return null
if (isAtLeastApiLevel(Build.VERSION_CODES.O) && icon is AdaptiveIconDrawable) {
return LauncherIcon(
foreground = icon.foreground,
background = icon.background,
foregroundScale = 1.5f,
backgroundScale = 1.5f
foreground = icon.foreground,
background = icon.background,
foregroundScale = 1.5f,
backgroundScale = 1.5f
)
}
return LauncherIcon(
foreground = icon,
foregroundScale = 1f,
autoGenerateBackgroundMode = LauncherPreferences.instance.legacyIconBg.toInt()
foreground = icon,
foregroundScale = 1f,
autoGenerateBackgroundMode = LauncherPreferences.instance.legacyIconBg.toInt()
)
}
}

View File

@ -1,163 +0,0 @@
package de.mm20.launcher2.badges
import android.content.ComponentName
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.LauncherApps
import android.content.pm.PackageManager
import android.os.Build
import android.os.Process
import androidx.annotation.RequiresApi
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import de.mm20.launcher2.graphics.BadgeDrawable
import de.mm20.launcher2.ktx.getSerialNumber
import de.mm20.launcher2.ktx.isAtLeastApiLevel
import de.mm20.launcher2.preferences.LauncherPreferences
import kotlinx.coroutines.*
class BadgeProvider(val context: Context) {
private val scope = CoroutineScope(Job() + Dispatchers.Main)
private val badges = mutableMapOf<String, MutableLiveData<Badge>>()
init {
if (LauncherPreferences.instance.cloudBadges) {
addCloudBadges()
}
if (LauncherPreferences.instance.suspendBadges) {
addSuspendBadges()
}
if (LauncherPreferences.instance.profileBadges && isAtLeastApiLevel(Build.VERSION_CODES.O)) {
addProfileBadges()
}
}
fun setBadge(key: String, badge: Badge) {
if (badges.containsKey(key)) {
badges[key]?.postValue(badge)
return
}
badges[key] = MutableLiveData(badge)
}
/**
* Updates a badge with all set values of another badge but keeps all other values
*/
fun updateBadge(key: String,
badge: Badge) {
if (badges.containsKey(key)) {
badges[key]?.run {
val updatedBadge = value?.also {
if (badge.number != null) it.number = badge.number
if (badge.progress != null) it.progress = badge.progress
if (badge.iconRes != null) it.iconRes = badge.iconRes
if (badge.icon != null) it.icon = badge.icon
}
postValue(updatedBadge)
}
} else {
badges[key] = MutableLiveData(badge)
}
}
fun removeBadge(key: String) {
badges[key]?.postValue(Badge())
}
fun getBadge(key: String): Badge? {
return badges[key]?.value
}
fun getLiveBadge(key: String): LiveData<Badge> {
return badges[key] ?: MutableLiveData<Badge>(Badge()).also { badges[key] = it }
}
fun removeNotificationBadges() {
for (k in badges.keys) {
if (k.startsWith("app://")) {
val badge = getBadge(k) ?: continue
badge.number = null
badge.progress = null
updateBadge(k, badge)
}
}
}
fun removeSuspendBadges() {
for ((k, v) in badges) {
if (k.startsWith("app://") && v.value?.iconRes == R.drawable.ic_badge_suspended) {
val badge = getBadge(k) ?: continue
badge.iconRes = null
updateBadge(k, badge)
}
}
}
fun addSuspendBadges() {
scope.launch {
withContext(Dispatchers.IO) {
val apps = context.packageManager.getInstalledApplications(0)
for (app in apps) {
if (app.flags and ApplicationInfo.FLAG_SUSPENDED != 0) {
setBadge("app://${app.packageName}", Badge(iconRes = R.drawable.ic_badge_suspended))
}
}
}
}
}
fun addAppShortcutBadge(activity: ComponentName){
scope.launch {
withContext(Dispatchers.IO) {
val icon = try {
context.packageManager.getActivityIcon(
activity
)
} catch (e: PackageManager.NameNotFoundException) {
return@withContext
}
val badge = Badge(icon = BadgeDrawable(context, icon))
setBadge(
"shortcut://${activity.flattenToShortString()}",
badge
)
}
}
}
fun addCloudBadges() {
setBadge("gdrive://", Badge(iconRes = R.drawable.ic_badge_gdrive))
setBadge("onedrive://", Badge(iconRes = R.drawable.ic_badge_onedrive))
setBadge("nextcloud://", Badge(iconRes = R.drawable.ic_badge_nextcloud))
setBadge("owncloud://", Badge(iconRes = R.drawable.ic_badge_owncloud))
}
@RequiresApi(Build.VERSION_CODES.O)
fun addProfileBadges() {
val profiles = (context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps).profiles
for (p in profiles) {
if (p == Process.myUserHandle()) continue
setBadge("profile://${p.getSerialNumber(context)}", Badge(
iconRes = R.drawable.ic_badge_workprofile
))
}
}
fun removeCloudBadges() {
removeBadge("gdrive://")
removeBadge("onedrive://")
removeBadge("nextcloud://")
removeBadge("owncloud://")
}
fun removeShortcutBadges() {
for (k in badges.keys) {
if (k.startsWith("shortcut://")) {
removeBadge(k)
}
}
}
}

View File

@ -4,6 +4,5 @@ import org.koin.android.ext.koin.androidContext
import org.koin.dsl.module
val badgesModule = module {
single { BadgeProvider(androidContext()) }
single<BadgeRepository> { BadgeRepositoryImpl(androidContext()) }
}