Workaround a bug in Koin
This should hopefully fix java.util.NoSuchElementException as in InsertKoinIO/koin#1149
This commit is contained in:
parent
004212f8c9
commit
051fb1e9c5
@ -10,16 +10,10 @@ import android.os.Process
|
|||||||
import android.os.UserManager
|
import android.os.UserManager
|
||||||
import androidx.annotation.RequiresApi
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.core.content.getSystemService
|
import androidx.core.content.getSystemService
|
||||||
import de.mm20.launcher2.badges.Badge
|
|
||||||
import de.mm20.launcher2.badges.BadgeProvider
|
import de.mm20.launcher2.badges.BadgeProvider
|
||||||
import de.mm20.launcher2.graphics.BadgeDrawable
|
|
||||||
import de.mm20.launcher2.ktx.jsonObjectOf
|
import de.mm20.launcher2.ktx.jsonObjectOf
|
||||||
import de.mm20.launcher2.search.SearchableDeserializer
|
import de.mm20.launcher2.search.SearchableDeserializer
|
||||||
import de.mm20.launcher2.search.SearchableSerializer
|
import de.mm20.launcher2.search.SearchableSerializer
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.GlobalScope
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import org.koin.core.component.KoinComponent
|
import org.koin.core.component.KoinComponent
|
||||||
import org.koin.core.component.inject
|
import org.koin.core.component.inject
|
||||||
@ -72,13 +66,12 @@ class AppShortcutSerializer : SearchableSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class AppShortcutDeserializer(
|
class AppShortcutDeserializer(
|
||||||
val context: Context,
|
val context: Context
|
||||||
) : SearchableDeserializer, KoinComponent {
|
) : SearchableDeserializer, KoinComponent {
|
||||||
|
|
||||||
private val badgeProvider: BadgeProvider by inject()
|
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.N_MR1)
|
@RequiresApi(Build.VERSION_CODES.N_MR1)
|
||||||
override fun deserialize(serialized: String): Searchable? {
|
override fun deserialize(serialized: String): Searchable? {
|
||||||
|
val badgeProvider: BadgeProvider by inject()
|
||||||
val launcherApps = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
|
val launcherApps = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
|
||||||
if (!launcherApps.hasShortcutHostPermission()) return null
|
if (!launcherApps.hasShortcutHostPermission()) return null
|
||||||
else {
|
else {
|
||||||
@ -110,23 +103,11 @@ class AppShortcutDeserializer(
|
|||||||
if (shortcuts == null || shortcuts.isEmpty()) {
|
if (shortcuts == null || shortcuts.isEmpty()) {
|
||||||
return null
|
return null
|
||||||
} else {
|
} else {
|
||||||
GlobalScope.launch {
|
val activity = shortcuts[0].activity
|
||||||
val activity = shortcuts[0].activity
|
if (activity != null) {
|
||||||
withContext(Dispatchers.IO) {
|
badgeProvider.addAppShortcutBadge(
|
||||||
val icon = try {
|
activity
|
||||||
context.packageManager.getActivityIcon(
|
)
|
||||||
activity
|
|
||||||
?: return@withContext
|
|
||||||
)
|
|
||||||
} catch (e: PackageManager.NameNotFoundException) {
|
|
||||||
return@withContext
|
|
||||||
}
|
|
||||||
val badge = Badge(icon = BadgeDrawable(context, icon))
|
|
||||||
badgeProvider.setBadge(
|
|
||||||
"shortcut://${activity.flattenToShortString()}",
|
|
||||||
badge
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return AppShortcut(
|
return AppShortcut(
|
||||||
context = context,
|
context = context,
|
||||||
|
|||||||
@ -46,5 +46,6 @@ dependencies {
|
|||||||
|
|
||||||
implementation(project(":ktx"))
|
implementation(project(":ktx"))
|
||||||
implementation(project(":preferences"))
|
implementation(project(":preferences"))
|
||||||
|
implementation(project(":base"))
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,13 +1,16 @@
|
|||||||
package de.mm20.launcher2.badges
|
package de.mm20.launcher2.badges
|
||||||
|
|
||||||
|
import android.content.ComponentName
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.pm.ApplicationInfo
|
import android.content.pm.ApplicationInfo
|
||||||
import android.content.pm.LauncherApps
|
import android.content.pm.LauncherApps
|
||||||
|
import android.content.pm.PackageManager
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Process
|
import android.os.Process
|
||||||
import androidx.annotation.RequiresApi
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
|
import de.mm20.launcher2.graphics.BadgeDrawable
|
||||||
import de.mm20.launcher2.ktx.getSerialNumber
|
import de.mm20.launcher2.ktx.getSerialNumber
|
||||||
import de.mm20.launcher2.ktx.isAtLeastApiLevel
|
import de.mm20.launcher2.ktx.isAtLeastApiLevel
|
||||||
import de.mm20.launcher2.preferences.LauncherPreferences
|
import de.mm20.launcher2.preferences.LauncherPreferences
|
||||||
@ -106,6 +109,25 @@ class BadgeProvider(val context: Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
fun addCloudBadges() {
|
||||||
setBadge("gdrive://", Badge(iconRes = R.drawable.ic_badge_gdrive))
|
setBadge("gdrive://", Badge(iconRes = R.drawable.ic_badge_gdrive))
|
||||||
setBadge("onedrive://", Badge(iconRes = R.drawable.ic_badge_onedrive))
|
setBadge("onedrive://", Badge(iconRes = R.drawable.ic_badge_onedrive))
|
||||||
|
|||||||
@ -52,5 +52,6 @@ dependencies {
|
|||||||
implementation(project(":files"))
|
implementation(project(":files"))
|
||||||
implementation(project(":websites"))
|
implementation(project(":websites"))
|
||||||
implementation(project(":wikipedia"))
|
implementation(project(":wikipedia"))
|
||||||
|
implementation(project(":badges"))
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -14,6 +14,7 @@ import de.mm20.launcher2.search.SearchableDeserializer
|
|||||||
import de.mm20.launcher2.search.data.CalendarEvent
|
import de.mm20.launcher2.search.data.CalendarEvent
|
||||||
import de.mm20.launcher2.search.data.Searchable
|
import de.mm20.launcher2.search.data.Searchable
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
import org.koin.core.component.get
|
||||||
import org.koin.core.component.inject
|
import org.koin.core.component.inject
|
||||||
import org.koin.core.parameter.parametersOf
|
import org.koin.core.parameter.parametersOf
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
@ -34,7 +35,7 @@ class FavoritesRepository(private val context: Context) : BaseSearchableReposito
|
|||||||
val pinnedCalendarEvents = MediatorLiveData<List<CalendarEvent>>()
|
val pinnedCalendarEvents = MediatorLiveData<List<CalendarEvent>>()
|
||||||
|
|
||||||
private fun fromDatabaseEntity(entity: FavoritesItemEntity): FavoritesItem {
|
private fun fromDatabaseEntity(entity: FavoritesItemEntity): FavoritesItem {
|
||||||
val deserializer: SearchableDeserializer by inject { parametersOf(entity.serializedSearchable) }
|
val deserializer: SearchableDeserializer = get { parametersOf(entity.serializedSearchable) }
|
||||||
return FavoritesItem(
|
return FavoritesItem(
|
||||||
key = entity.key,
|
key = entity.key,
|
||||||
searchable = deserializer.deserialize(entity.serializedSearchable.substringAfter("#")),
|
searchable = deserializer.deserialize(entity.serializedSearchable.substringAfter("#")),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user