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 androidx.annotation.RequiresApi
|
||||
import androidx.core.content.getSystemService
|
||||
import de.mm20.launcher2.badges.Badge
|
||||
import de.mm20.launcher2.badges.BadgeProvider
|
||||
import de.mm20.launcher2.graphics.BadgeDrawable
|
||||
import de.mm20.launcher2.ktx.jsonObjectOf
|
||||
import de.mm20.launcher2.search.SearchableDeserializer
|
||||
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.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.inject
|
||||
@ -72,13 +66,12 @@ class AppShortcutSerializer : SearchableSerializer {
|
||||
}
|
||||
|
||||
class AppShortcutDeserializer(
|
||||
val context: Context,
|
||||
val context: Context
|
||||
) : SearchableDeserializer, KoinComponent {
|
||||
|
||||
private val badgeProvider: BadgeProvider by inject()
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.N_MR1)
|
||||
override fun deserialize(serialized: String): Searchable? {
|
||||
val badgeProvider: BadgeProvider by inject()
|
||||
val launcherApps = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
|
||||
if (!launcherApps.hasShortcutHostPermission()) return null
|
||||
else {
|
||||
@ -110,23 +103,11 @@ class AppShortcutDeserializer(
|
||||
if (shortcuts == null || shortcuts.isEmpty()) {
|
||||
return null
|
||||
} else {
|
||||
GlobalScope.launch {
|
||||
val activity = shortcuts[0].activity
|
||||
withContext(Dispatchers.IO) {
|
||||
val icon = try {
|
||||
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
|
||||
)
|
||||
}
|
||||
val activity = shortcuts[0].activity
|
||||
if (activity != null) {
|
||||
badgeProvider.addAppShortcutBadge(
|
||||
activity
|
||||
)
|
||||
}
|
||||
return AppShortcut(
|
||||
context = context,
|
||||
|
||||
@ -46,5 +46,6 @@ dependencies {
|
||||
|
||||
implementation(project(":ktx"))
|
||||
implementation(project(":preferences"))
|
||||
implementation(project(":base"))
|
||||
|
||||
}
|
||||
@ -1,13 +1,16 @@
|
||||
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
|
||||
@ -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() {
|
||||
setBadge("gdrive://", Badge(iconRes = R.drawable.ic_badge_gdrive))
|
||||
setBadge("onedrive://", Badge(iconRes = R.drawable.ic_badge_onedrive))
|
||||
|
||||
@ -52,5 +52,6 @@ dependencies {
|
||||
implementation(project(":files"))
|
||||
implementation(project(":websites"))
|
||||
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.Searchable
|
||||
import kotlinx.coroutines.*
|
||||
import org.koin.core.component.get
|
||||
import org.koin.core.component.inject
|
||||
import org.koin.core.parameter.parametersOf
|
||||
import kotlin.math.max
|
||||
@ -34,7 +35,7 @@ class FavoritesRepository(private val context: Context) : BaseSearchableReposito
|
||||
val pinnedCalendarEvents = MediatorLiveData<List<CalendarEvent>>()
|
||||
|
||||
private fun fromDatabaseEntity(entity: FavoritesItemEntity): FavoritesItem {
|
||||
val deserializer: SearchableDeserializer by inject { parametersOf(entity.serializedSearchable) }
|
||||
val deserializer: SearchableDeserializer = get { parametersOf(entity.serializedSearchable) }
|
||||
return FavoritesItem(
|
||||
key = entity.key,
|
||||
searchable = deserializer.deserialize(entity.serializedSearchable.substringAfter("#")),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user