Fix notification bug

Fix a bug where an incomplete notification will show if one changes the loop mode in PlaybackFragment.
This commit is contained in:
OxygenCobalt 2020-11-23 11:53:21 -07:00
parent 844870f4d4
commit c11882bc7b
2 changed files with 22 additions and 14 deletions

View file

@ -141,6 +141,8 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
// --- PLAYBACKSTATEMANAGER SETUP ---
playbackManager.resetHasPlayedStatus()
playbackManager.addCallback(this)
if (playbackManager.song != null) {
@ -225,12 +227,8 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
uploadMetadataToSession(it)
if (playbackManager.isRestored) {
notification.setMetadata(playbackManager.song!!, this) {
startForegroundOrNotify("Song")
}
} else {
notification.setMetadata(playbackManager.song!!, this) {}
notification.setMetadata(playbackManager.song!!, this) {
startForegroundOrNotify("Song")
}
return
@ -285,6 +283,12 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
player.seekTo(position)
}
override fun onRestoreFinish() {
Log.d(this::class.simpleName, "Restore done")
restorePlayer()
}
// --- OTHER FUNCTIONS ---
private fun restorePlayer() {
@ -321,12 +325,6 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
}
}
override fun onRestoreFinish() {
Log.d(this::class.simpleName, "Restore done")
restorePlayer()
}
private fun uploadMetadataToSession(song: Song) {
val builder = MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.name)
@ -361,7 +359,7 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
private fun startForegroundOrNotify(reason: String) {
// Start the service in the foreground if haven't already.
if (playbackManager.isRestored) {
if (playbackManager.hasPlayed) {
Log.d(this::class.simpleName, "Starting foreground/notifying because of $reason")
if (!isForeground) {

View file

@ -84,6 +84,7 @@ class PlaybackStateManager private constructor() {
}
private var mIsInUserQueue = false
private var mIsRestored = false
private var mHasPlayed = false
private var mShuffleSeed = -1L
val song: Song? get() = mSong
@ -97,6 +98,7 @@ class PlaybackStateManager private constructor() {
val isShuffling: Boolean get() = mIsShuffling
val loopMode: LoopMode get() = mLoopMode
val isRestored: Boolean get() = mIsRestored
val hasPlayed: Boolean get() = mHasPlayed
// --- CALLBACKS ---
@ -210,7 +212,7 @@ class PlaybackStateManager private constructor() {
mPosition = 0
if (!mIsPlaying) {
mIsPlaying = true
setPlayingStatus(true)
}
mIsInUserQueue = false
@ -405,6 +407,10 @@ class PlaybackStateManager private constructor() {
fun setPlayingStatus(value: Boolean) {
if (mIsPlaying != value) {
if (value) {
mHasPlayed = true
}
mIsPlaying = value
}
}
@ -423,6 +429,10 @@ class PlaybackStateManager private constructor() {
mLoopMode = mode
}
fun resetHasPlayedStatus() {
mHasPlayed = false
}
private fun resetLoopMode() {
// Reset the loop mode from ONCE if needed.
if (mLoopMode == LoopMode.ONCE) {