Fixed a bug in the playing queue where it would get messed up

This commit is contained in:
Karim Abou Zeid 2015-12-24 20:24:29 +01:00
commit 8ca5522971

View file

@ -615,19 +615,40 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
} else { } else {
originalPlayingQueue.remove(playingQueue.remove(position)); originalPlayingQueue.remove(playingQueue.remove(position));
} }
rePosition(position);
notifyChange(QUEUE_CHANGED); notifyChange(QUEUE_CHANGED);
} }
public void removeSong(@NonNull Song song) { public void removeSong(@NonNull Song song) {
for (int i = 0; i < playingQueue.size(); i++) { for (int i = 0; i < playingQueue.size(); i++) {
if (playingQueue.get(i).id == song.id) playingQueue.remove(i); if (playingQueue.get(i).id == song.id) {
playingQueue.remove(i);
rePosition(i);
}
} }
for (int i = 0; i < originalPlayingQueue.size(); i++) { for (int i = 0; i < originalPlayingQueue.size(); i++) {
if (originalPlayingQueue.get(i).id == song.id) originalPlayingQueue.remove(i); if (originalPlayingQueue.get(i).id == song.id) {
originalPlayingQueue.remove(i);
}
} }
notifyChange(QUEUE_CHANGED); notifyChange(QUEUE_CHANGED);
} }
private void rePosition(int deletedPosition) {
int currentPosition = getPosition();
if (deletedPosition < currentPosition) {
setPositionInternal(currentPosition - 1);
} else if (deletedPosition == currentPosition) {
if (playingQueue.size() > deletedPosition) {
setPosition(position);
} else {
setPosition(position - 1);
}
}
}
public void moveSong(int from, int to) { public void moveSong(int from, int to) {
if (from == to) return; if (from == to) return;
final int currentPosition = getPosition(); final int currentPosition = getPosition();
@ -638,9 +659,9 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
originalPlayingQueue.add(to, tmpSong); originalPlayingQueue.add(to, tmpSong);
} }
if (from > currentPosition && to <= currentPosition) { if (from > currentPosition && to <= currentPosition) {
setPositionInternal(getPosition() + 1); setPositionInternal(currentPosition + 1);
} else if (from < currentPosition && to >= currentPosition) { } else if (from < currentPosition && to >= currentPosition) {
setPositionInternal(getPosition() - 1); setPositionInternal(currentPosition - 1);
} else if (from == currentPosition) { } else if (from == currentPosition) {
setPositionInternal(to); setPositionInternal(to);
} }
@ -982,6 +1003,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
case SET_POSITION: case SET_POSITION:
service.openTrackAndPrepareNextAt(msg.arg1); service.openTrackAndPrepareNextAt(msg.arg1);
service.notifyChange(PLAY_STATE_CHANGED);
break; break;
case PREPARE_NEXT: case PREPARE_NEXT: