From bcd64a46d43b4e6cf59ce1e6224d1c80a4dff7f4 Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Sun, 30 Oct 2016 15:51:03 +0100 Subject: [PATCH] automatically remove last appended random song from queue if we are going out of RANDOM mode --- .../android/vanilla/SongTimeline.java | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/ch/blinkenlights/android/vanilla/SongTimeline.java b/src/ch/blinkenlights/android/vanilla/SongTimeline.java index f8f2e401..1649abbb 100644 --- a/src/ch/blinkenlights/android/vanilla/SongTimeline.java +++ b/src/ch/blinkenlights/android/vanilla/SongTimeline.java @@ -238,6 +238,10 @@ public final class SongTimeline { * Hash code of mSongs while mShuffleCache was generated */ private int mShuffleTicket; + /** + * The last song we added randomly by calling MediaUtils.getRandomSong() + */ + private Song mLastRandomSong; // for saveActiveSongs() private Song mSavedPrevious; @@ -485,10 +489,28 @@ public final class SongTimeline { */ public void setFinishAction(int action) { - saveActiveSongs(); - mFinishAction = action; - broadcastChangedSongs(); - changed(); + synchronized (this) { + saveActiveSongs(); + + if (mFinishAction == FINISH_RANDOM) { + // Remove the last song if we are going out of RANDOM mode and we + // are currently playing the 2nd last one. + int lastSongPos = getLength() - 1; + if (getPosition()+1 == lastSongPos) { + Song lastSong = mSongs.get(lastSongPos); + if (lastSong.isRandom() && lastSong.equals(mLastRandomSong)) { + mSongs.remove(lastSongPos); + } + } + // forget about the last random song, even if it survived (eg: was switching modes while not playing + // the 2nd last song -> the last song is now considered to be part of the queue) + mLastRandomSong = null; + } + + mFinishAction = action; + broadcastChangedSongs(); + changed(); + } } /** @@ -558,6 +580,7 @@ public final class SongTimeline { if (song == null) return null; timeline.add(song); + mLastRandomSong = song; // Keep the queue at 20 items to avoid growing forever // Note that we do not broadcast the addition of this song, as it // was virtually 'always there'