From 84f0e02882c7e4c627dba23cfca7f48dfca4a713 Mon Sep 17 00:00:00 2001 From: Christopher Eby Date: Wed, 21 Sep 2011 22:20:39 -0500 Subject: [PATCH] Add repeat current song mode --- res/values/strings.xml | 4 ++- src/org/kreed/vanilla/PlaybackActivity.java | 28 +++++++++++++++------ src/org/kreed/vanilla/PlaybackService.java | 25 +++++++++++++++--- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 0fbd30ec..49b897e1 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -34,8 +34,10 @@ THE SOFTWARE. Shuffle enabled Shuffle disabled Enable Repeat + Repeat Song Disable Repeat - Repeat enabled + Repeating all songs + Repeating current song Repeat disabled Enable Random Disable Random diff --git a/src/org/kreed/vanilla/PlaybackActivity.java b/src/org/kreed/vanilla/PlaybackActivity.java index 06c2eaee..425ae2e1 100644 --- a/src/org/kreed/vanilla/PlaybackActivity.java +++ b/src/org/kreed/vanilla/PlaybackActivity.java @@ -302,8 +302,14 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View int state = mState; boolean isShuffling = (state & PlaybackService.FLAG_SHUFFLE) != 0; menu.findItem(MENU_SHUFFLE).setTitle(isShuffling ? R.string.shuffle_disable : R.string.shuffle_enable); - boolean isRepeating = (state & PlaybackService.FLAG_REPEAT) != 0; - menu.findItem(MENU_REPEAT).setTitle(isRepeating ? R.string.repeat_disable : R.string.repeat_enable); + int repeatRes; + if ((state & PlaybackService.FLAG_REPEAT) != 0) + repeatRes = R.string.repeat_current; + else if ((state & PlaybackService.FLAG_REPEAT_CURRENT) != 0) + repeatRes = R.string.repeat_disable; + else + repeatRes = R.string.repeat_enable; + menu.findItem(MENU_REPEAT).setTitle(repeatRes); boolean isRandom = (state & PlaybackService.FLAG_RANDOM) != 0; // TODO: find icon (dice? arrow pointing in many directions?) menu.findItem(MENU_RANDOM).setTitle(isRandom ? R.string.random_disable : R.string.random_enable); @@ -318,7 +324,7 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View toggleShuffle(); return true; case MENU_REPEAT: - toggleRepeat(); + cycleRepeat(); return true; case MENU_RANDOM: toggleRandom(); @@ -349,12 +355,18 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View } /** - * Toggle repeat mode on/off + * Cycle repeat mode. */ - public void toggleRepeat() + public void cycleRepeat() { - int state = PlaybackService.get(this).toggleRepeat(); - int res = (state & PlaybackService.FLAG_REPEAT) == 0 ? R.string.repeat_disabling : R.string.repeat_enabling; + int state = PlaybackService.get(this).cycleRepeat(); + int res; + if ((state & PlaybackService.FLAG_REPEAT) != 0) + res = R.string.repeat_enabling; + else if ((state & PlaybackService.FLAG_REPEAT_CURRENT) != 0) + res = R.string.repeat_current_enabling; + else + res = R.string.repeat_disabling; Toast.makeText(this, res, Toast.LENGTH_SHORT).show(); setState(state); } @@ -401,7 +413,7 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View previousSong(); break; case ACTION_REPEAT: - toggleRepeat(); + cycleRepeat(); break; case ACTION_SHUFFLE: toggleShuffle(); diff --git a/src/org/kreed/vanilla/PlaybackService.java b/src/org/kreed/vanilla/PlaybackService.java index d7a2db22..82d88001 100644 --- a/src/org/kreed/vanilla/PlaybackService.java +++ b/src/org/kreed/vanilla/PlaybackService.java @@ -65,7 +65,7 @@ public final class PlaybackService extends Service implements Handler.Callback, /** * State file version that indicates data order. */ - private static final int STATE_VERSION = 1; + private static final int STATE_VERSION = 2; private static final int NOTIFICATION_ID = 2; @@ -145,6 +145,11 @@ public final class PlaybackService extends Service implements Handler.Callback, * Set when the user needs to select songs to play. */ public static final int FLAG_EMPTY_QUEUE = 0x40; + /** + * If set, replay the current song when the end of the song is reached + * instead of advancing to the next song. + */ + public static final int FLAG_REPEAT_CURRENT = 0x80; public static final int NEVER = 0; public static final int WHEN_PLAYING = 1; @@ -573,14 +578,21 @@ public final class PlaybackService extends Service implements Handler.Callback, } /** - * Toggle repeat mode. Disables random mode. + * Cycle repeat mode. Disables random mode. * * @return The new state after this is called. */ - public int toggleRepeat() + public int cycleRepeat() { synchronized (mStateLock) { - return updateState((mState ^ FLAG_REPEAT) & ~FLAG_RANDOM); + int state = mState & ~FLAG_RANDOM; + if ((state & FLAG_REPEAT_CURRENT) != 0) + state &= ~(FLAG_REPEAT|FLAG_REPEAT_CURRENT); + else if ((state & FLAG_REPEAT) == 0) + state |= FLAG_REPEAT; + else if ((state & FLAG_REPEAT) != 0) + state = (state | FLAG_REPEAT_CURRENT) & ~FLAG_REPEAT;; + return updateState(state); } } @@ -678,6 +690,8 @@ public final class PlaybackService extends Service implements Handler.Callback, { if (mTimeline.isEndOfQueue()) unsetFlag(FLAG_PLAYING); + else if ((mState & FLAG_REPEAT_CURRENT) != 0) + setCurrentSong(0); else setCurrentSong(+1); } @@ -1160,6 +1174,7 @@ public final class PlaybackService extends Service implements Handler.Callback, if (in.readLong() == STATE_FILE_MAGIC && in.readInt() == STATE_VERSION) { mPendingSeek = in.readInt(); + int savedState = in.readInt(); mTimeline.readState(in); int finishAction = mTimeline.getFinishAction(); @@ -1169,6 +1184,7 @@ public final class PlaybackService extends Service implements Handler.Callback, state |= FLAG_REPEAT; if (mTimeline.isShuffling()) state |= FLAG_SHUFFLE; + state |= savedState & FLAG_REPEAT_CURRENT; } in.close(); @@ -1194,6 +1210,7 @@ public final class PlaybackService extends Service implements Handler.Callback, out.writeLong(STATE_FILE_MAGIC); out.writeInt(STATE_VERSION); out.writeInt(pendingSeek); + out.writeInt(mState); mTimeline.writeState(out); out.close(); } catch (IOException e) {