Show the song cover as lockscreen wallpaper (#170)

This commit is contained in:
vfsfitvnm 2022-08-05 08:56:23 +02:00
parent 20da9e5df3
commit a246f1f336
3 changed files with 30 additions and 0 deletions

View file

@ -76,6 +76,7 @@ import it.vfsfitvnm.vimusic.utils.forcePlayFromBeginning
import it.vfsfitvnm.vimusic.utils.getEnum
import it.vfsfitvnm.vimusic.utils.intent
import it.vfsfitvnm.vimusic.utils.isInvincibilityEnabledKey
import it.vfsfitvnm.vimusic.utils.isShowingThumbnailInLockscreenKey
import it.vfsfitvnm.vimusic.utils.mediaItems
import it.vfsfitvnm.vimusic.utils.persistentQueueKey
import it.vfsfitvnm.vimusic.utils.preferences
@ -132,6 +133,7 @@ class PlayerService : InvincibleService(), Player.Listener, PlaybackStatsListene
private var isVolumeNormalizationEnabled = false
private var isPersistentQueueEnabled = false
private var isShowingThumbnailInLockscreen = true
override var isInvincibilityEnabled = false
private val binder = Binder()
@ -166,6 +168,7 @@ class PlayerService : InvincibleService(), Player.Listener, PlaybackStatsListene
isPersistentQueueEnabled = preferences.getBoolean(persistentQueueKey, false)
isVolumeNormalizationEnabled = preferences.getBoolean(volumeNormalizationKey, false)
isInvincibilityEnabled = preferences.getBoolean(isInvincibilityEnabledKey, false)
isShowingThumbnailInLockscreen = preferences.getBoolean(isShowingThumbnailInLockscreenKey, true)
val cacheEvictor = when (val size =
preferences.getEnum(exoPlayerDiskCacheMaxSizeKey, ExoPlayerDiskCacheMaxSize.`2GB`)) {
@ -354,6 +357,12 @@ class PlayerService : InvincibleService(), Player.Listener, PlaybackStatsListene
}
}
private fun maybeShowSongCoverInLockScreen() {
val bitmap = if (isShowingThumbnailInLockscreen) bitmapProvider.bitmap else null
metadataBuilder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, bitmap)
mediaSession.setMetadata(metadataBuilder.build())
}
private val Player.androidPlaybackState: Int
get() = when (playbackState) {
Player.STATE_BUFFERING -> if (playWhenReady) PlaybackState.STATE_BUFFERING else PlaybackState.STATE_PAUSED
@ -436,6 +445,10 @@ class PlayerService : InvincibleService(), Player.Listener, PlaybackStatsListene
isInvincibilityEnabledKey -> isInvincibilityEnabled =
sharedPreferences.getBoolean(key, isInvincibilityEnabled)
skipSilenceKey -> player.skipSilenceEnabled = sharedPreferences.getBoolean(key, false)
isShowingThumbnailInLockscreenKey -> {
isShowingThumbnailInLockscreen = sharedPreferences.getBoolean(key, true)
maybeShowSongCoverInLockScreen()
}
}
}
@ -486,6 +499,7 @@ class PlayerService : InvincibleService(), Player.Listener, PlaybackStatsListene
.addAction(R.drawable.play_skip_forward, "Skip forward", nextIntent)
bitmapProvider.load(mediaMetadata.artworkUri) { bitmap ->
maybeShowSongCoverInLockScreen()
notificationManager?.notify(NotificationId, builder.setLargeIcon(bitmap).build())
}

View file

@ -26,9 +26,11 @@ import it.vfsfitvnm.vimusic.ui.components.TopAppBar
import it.vfsfitvnm.vimusic.ui.screens.EnumValueSelectorSettingsEntry
import it.vfsfitvnm.vimusic.ui.screens.SettingsEntryGroupText
import it.vfsfitvnm.vimusic.ui.screens.SettingsTitle
import it.vfsfitvnm.vimusic.ui.screens.SwitchSettingEntry
import it.vfsfitvnm.vimusic.ui.screens.globalRoutes
import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance
import it.vfsfitvnm.vimusic.utils.colorPaletteModeKey
import it.vfsfitvnm.vimusic.utils.isShowingThumbnailInLockscreenKey
import it.vfsfitvnm.vimusic.utils.rememberPreference
import it.vfsfitvnm.vimusic.utils.thumbnailRoundnessKey
@ -48,6 +50,10 @@ fun AppearanceSettingsScreen() {
thumbnailRoundnessKey,
ThumbnailRoundness.Light
)
var isShowingThumbnailInLockscreen by rememberPreference(
isShowingThumbnailInLockscreenKey,
true
)
Column(
modifier = Modifier
@ -92,6 +98,15 @@ fun AppearanceSettingsScreen() {
thumbnailRoundness = it
}
)
SettingsEntryGroupText(title = "LOCKSCREEN")
SwitchSettingEntry(
title = "Show song cover",
text = "Use the playing song cover as the lockscreen wallpaper",
isChecked = isShowingThumbnailInLockscreen,
onCheckedChange = { isShowingThumbnailInLockscreen = it }
)
}
}
}

View file

@ -27,6 +27,7 @@ const val skipSilenceKey = "skipSilence"
const val volumeNormalizationKey = "volumeNormalization"
const val persistentQueueKey = "persistentQueue"
const val isShowingSynchronizedLyricsKey = "isShowingSynchronizedLyrics"
const val isShowingThumbnailInLockscreenKey = "isShowingThumbnailInLockscreen"
inline fun <reified T : Enum<T>> SharedPreferences.getEnum(
key: String,