Prevent potential MusicRepository deadlock
This commit is contained in:
parent
9eecd3513f
commit
4e7275c4aa
@ -17,10 +17,7 @@ import androidx.media2.session.MediaController
|
|||||||
import androidx.media2.session.SessionCommandGroup
|
import androidx.media2.session.SessionCommandGroup
|
||||||
import de.mm20.launcher2.preferences.LauncherDataStore
|
import de.mm20.launcher2.preferences.LauncherDataStore
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.*
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
|
||||||
import kotlinx.coroutines.flow.first
|
|
||||||
import kotlinx.coroutines.flow.map
|
|
||||||
import kotlinx.coroutines.sync.Semaphore
|
import kotlinx.coroutines.sync.Semaphore
|
||||||
import org.koin.core.component.KoinComponent
|
import org.koin.core.component.KoinComponent
|
||||||
import org.koin.core.component.inject
|
import org.koin.core.component.inject
|
||||||
@ -70,16 +67,11 @@ class MusicRepositoryImpl(val context: Context) : MusicRepository, KoinComponent
|
|||||||
|
|
||||||
scope.launch {
|
scope.launch {
|
||||||
val filterMusicApps = dataStore.data.map { it.musicWidget.filterSources }.first()
|
val filterMusicApps = dataStore.data.map { it.musicWidget.filterSources }.first()
|
||||||
if (filterMusicApps) {
|
if (filterMusicApps && !isMusicApp(packageName)) {
|
||||||
val intent =
|
|
||||||
Intent(Intent.ACTION_MAIN).apply { addCategory(Intent.CATEGORY_APP_MUSIC) }
|
|
||||||
if (context.packageManager.queryIntentActivities(intent, 0)
|
|
||||||
.none { it.activityInfo.packageName == packageName }
|
|
||||||
) {
|
|
||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
try {
|
||||||
semaphore.acquire()
|
semaphore.acquire()
|
||||||
mediaController?.close()
|
mediaController?.close()
|
||||||
val appName = context.packageManager.getPackageInfo(
|
val appName = context.packageManager.getPackageInfo(
|
||||||
@ -102,8 +94,9 @@ class MusicRepositoryImpl(val context: Context) : MusicRepository, KoinComponent
|
|||||||
packageName
|
packageName
|
||||||
)
|
)
|
||||||
lastToken = token.toString()
|
lastToken = token.toString()
|
||||||
|
} finally {
|
||||||
semaphore.release()
|
semaphore.release()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,6 +269,14 @@ class MusicRepositoryImpl(val context: Context) : MusicRepository, KoinComponent
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private suspend fun isMusicApp(packageName: String): Boolean {
|
||||||
|
val intent = Intent(Intent.ACTION_MAIN).apply { addCategory(Intent.CATEGORY_APP_MUSIC) }
|
||||||
|
return withContext(Dispatchers.IO) {
|
||||||
|
context.packageManager.queryIntentActivities(intent, 0)
|
||||||
|
.none { it.activityInfo.packageName == packageName }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun setMetadata(
|
private suspend fun setMetadata(
|
||||||
title: String?,
|
title: String?,
|
||||||
artist: String?,
|
artist: String?,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user