Fix music widget not launching player app

This commit is contained in:
MM20
2024-07-24 19:39:30 +02:00
parent de3b7baa07
commit fd50aa11c3
3 changed files with 23 additions and 10 deletions
@@ -22,9 +22,12 @@ import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import de.mm20.launcher2.crashreporter.CrashReporter
import de.mm20.launcher2.ktx.sendWithBackgroundPermission
import de.mm20.launcher2.music.MusicService import de.mm20.launcher2.music.MusicService
import de.mm20.launcher2.music.PlaybackState import de.mm20.launcher2.music.PlaybackState
import de.mm20.launcher2.music.SupportedActions import de.mm20.launcher2.music.SupportedActions
@@ -68,8 +71,11 @@ class MusicPartProvider : PartProvider, KoinComponent {
.combinedClickable( .combinedClickable(
onClick = { onClick = {
try { try {
musicService.openPlayer()?.send() musicService
.openPlayer()
?.sendWithBackgroundPermission(context)
} catch (e: PendingIntent.CanceledException) { } catch (e: PendingIntent.CanceledException) {
CrashReporter.logException(e)
} }
}, },
onLongClick = { onLongClick = {
@@ -99,14 +105,17 @@ class MusicPartProvider : PartProvider, KoinComponent {
painter = rememberAnimatedVectorPainter( painter = rememberAnimatedVectorPainter(
animatedImageVector = playIcon, animatedImageVector = playIcon,
atEnd = state == PlaybackState.Playing atEnd = state == PlaybackState.Playing
), contentDescription = null ), contentDescription = stringResource(
if (state == PlaybackState.Playing) R.string.music_widget_pause
else R.string.music_widget_play
)
) )
} }
if (supportedActions.skipToNext) { if (supportedActions.skipToNext) {
IconButton(onClick = { musicService.next() }) { IconButton(onClick = { musicService.next() }) {
Icon( Icon(
imageVector = Icons.Rounded.SkipNext, imageVector = Icons.Rounded.SkipNext,
contentDescription = null contentDescription = stringResource(R.string.music_widget_next_track)
) )
} }
} }
@@ -122,7 +131,7 @@ class MusicPartProvider : PartProvider, KoinComponent {
.combinedClickable( .combinedClickable(
onClick = { onClick = {
try { try {
musicService.openPlayer()?.send() musicService.openPlayer()?.sendWithBackgroundPermission(context)
} catch (e: PendingIntent.CanceledException) { } catch (e: PendingIntent.CanceledException) {
} }
}, },
@@ -156,7 +165,7 @@ class MusicPartProvider : PartProvider, KoinComponent {
IconButton(onClick = { musicService.previous() }) { IconButton(onClick = { musicService.previous() }) {
Icon( Icon(
imageVector = Icons.Rounded.SkipPrevious, imageVector = Icons.Rounded.SkipPrevious,
contentDescription = null contentDescription = stringResource(R.string.music_widget_previous_track)
) )
} }
} }
@@ -165,14 +174,17 @@ class MusicPartProvider : PartProvider, KoinComponent {
painter = rememberAnimatedVectorPainter( painter = rememberAnimatedVectorPainter(
animatedImageVector = playIcon, animatedImageVector = playIcon,
atEnd = state == PlaybackState.Playing atEnd = state == PlaybackState.Playing
), contentDescription = null ), contentDescription = stringResource(
if (state == PlaybackState.Playing) R.string.music_widget_pause
else R.string.music_widget_play
)
) )
} }
if (supportedActions.skipToPrevious) { if (supportedActions.skipToPrevious) {
IconButton(onClick = { musicService.next() }) { IconButton(onClick = { musicService.next() }) {
Icon( Icon(
imageVector = Icons.Rounded.SkipNext, imageVector = Icons.Rounded.SkipNext,
contentDescription = null contentDescription = stringResource(R.string.music_widget_next_track)
) )
} }
} }
@@ -258,7 +258,7 @@ fun MusicWidget(widget: MusicWidget) {
.clip(MaterialTheme.shapes.small) .clip(MaterialTheme.shapes.small)
.combinedClickable( .combinedClickable(
onClick = { onClick = {
viewModel.openPlayer() viewModel.openPlayer(context)
}, },
onLongClick = { onLongClick = {
viewModel.openPlayerSelector(context) viewModel.openPlayerSelector(context)
@@ -7,6 +7,7 @@ import android.media.session.PlaybackState.CustomAction
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import de.mm20.launcher2.crashreporter.CrashReporter import de.mm20.launcher2.crashreporter.CrashReporter
import de.mm20.launcher2.ktx.sendWithBackgroundPermission
import de.mm20.launcher2.music.MusicService import de.mm20.launcher2.music.MusicService
import de.mm20.launcher2.music.PlaybackState import de.mm20.launcher2.music.PlaybackState
import de.mm20.launcher2.music.SupportedActions import de.mm20.launcher2.music.SupportedActions
@@ -50,9 +51,9 @@ class MusicWidgetVM: ViewModel(), KoinComponent {
musicService.togglePause() musicService.togglePause()
} }
fun openPlayer() { fun openPlayer(context: Context) {
try { try {
musicService.openPlayer()?.send() musicService.openPlayer()?.sendWithBackgroundPermission(context)
} catch (e: PendingIntent.CanceledException) { } catch (e: PendingIntent.CanceledException) {
CrashReporter.logException(e) CrashReporter.logException(e)
} }