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,40 +67,36 @@ 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 =
|
return@launch
|
||||||
Intent(Intent.ACTION_MAIN).apply { addCategory(Intent.CATEGORY_APP_MUSIC) }
|
|
||||||
if (context.packageManager.queryIntentActivities(intent, 0)
|
|
||||||
.none { it.activityInfo.packageName == packageName }
|
|
||||||
) {
|
|
||||||
return@launch
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
semaphore.acquire()
|
try {
|
||||||
mediaController?.close()
|
semaphore.acquire()
|
||||||
val appName = context.packageManager.getPackageInfo(
|
mediaController?.close()
|
||||||
packageName,
|
val appName = context.packageManager.getPackageInfo(
|
||||||
0
|
packageName,
|
||||||
).applicationInfo.loadLabel(context.packageManager)
|
0
|
||||||
mediaController = MediaController.Builder(context)
|
).applicationInfo.loadLabel(context.packageManager)
|
||||||
.setSessionCompatToken(token)
|
mediaController = MediaController.Builder(context)
|
||||||
.setControllerCallback(
|
.setSessionCompatToken(token)
|
||||||
Executors.newSingleThreadExecutor(),
|
.setControllerCallback(
|
||||||
mediaSessionCallback
|
Executors.newSingleThreadExecutor(),
|
||||||
|
mediaSessionCallback
|
||||||
|
)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
setMetadata(
|
||||||
|
title = context.getString(R.string.music_widget_default_title, appName),
|
||||||
|
artist = null,
|
||||||
|
album = null,
|
||||||
|
albumArt = null,
|
||||||
|
packageName
|
||||||
)
|
)
|
||||||
.build()
|
lastToken = token.toString()
|
||||||
|
} finally {
|
||||||
setMetadata(
|
semaphore.release()
|
||||||
title = context.getString(R.string.music_widget_default_title, appName),
|
}
|
||||||
artist = null,
|
|
||||||
album = null,
|
|
||||||
albumArt = null,
|
|
||||||
packageName
|
|
||||||
)
|
|
||||||
lastToken = token.toString()
|
|
||||||
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