From cd3ee6f3cb394e8cb3437dd2d580c3ccf03b27ba Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Sat, 15 Jan 2022 15:41:29 +0100 Subject: [PATCH] Clean up unused badge code --- .../mm20/launcher2/search/data/AppShortcut.kt | 45 +++-- .../de/mm20/launcher2/badges/BadgeProvider.kt | 163 ------------------ .../java/de/mm20/launcher2/badges/Module.kt | 1 - 3 files changed, 22 insertions(+), 187 deletions(-) delete mode 100644 badges/src/main/java/de/mm20/launcher2/badges/BadgeProvider.kt diff --git a/applications/src/main/java/de/mm20/launcher2/search/data/AppShortcut.kt b/applications/src/main/java/de/mm20/launcher2/search/data/AppShortcut.kt index 4a7f6334..bfcf5fc5 100644 --- a/applications/src/main/java/de/mm20/launcher2/search/data/AppShortcut.kt +++ b/applications/src/main/java/de/mm20/launcher2/search/data/AppShortcut.kt @@ -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() ) } } \ No newline at end of file diff --git a/badges/src/main/java/de/mm20/launcher2/badges/BadgeProvider.kt b/badges/src/main/java/de/mm20/launcher2/badges/BadgeProvider.kt deleted file mode 100644 index 1d5ad426..00000000 --- a/badges/src/main/java/de/mm20/launcher2/badges/BadgeProvider.kt +++ /dev/null @@ -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>() - - 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 { - return badges[key] ?: MutableLiveData(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) - } - } - } -} \ No newline at end of file diff --git a/badges/src/main/java/de/mm20/launcher2/badges/Module.kt b/badges/src/main/java/de/mm20/launcher2/badges/Module.kt index e47c0e84..a73cb800 100644 --- a/badges/src/main/java/de/mm20/launcher2/badges/Module.kt +++ b/badges/src/main/java/de/mm20/launcher2/badges/Module.kt @@ -4,6 +4,5 @@ import org.koin.android.ext.koin.androidContext import org.koin.dsl.module val badgesModule = module { - single { BadgeProvider(androidContext()) } single { BadgeRepositoryImpl(androidContext()) } } \ No newline at end of file