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