Skip to content

Commit

Permalink
Re introduce restore fallback if queue are corrupted
Browse files Browse the repository at this point in the history
  • Loading branch information
Octoton committed May 3, 2022
1 parent a82d100 commit 19fbb2e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -15,6 +16,8 @@
/** Provide playing queue management (save list of songs, add/move/remove, know currently played position, restore, ...) */
public class StaticPlayingQueue {

public static final String TAG = StaticPlayingQueue.class.getSimpleName();

public static final int REPEAT_MODE_NONE = 0;
public static final int REPEAT_MODE_ALL = 1;
public static final int REPEAT_MODE_THIS = 2;
Expand Down Expand Up @@ -104,11 +107,19 @@ public boolean restoreQueue(Context context, int restoredPosition) {
}
}

try {
restoreUniqueId();
} catch (ArrayIndexOutOfBoundsException queueCopiesOutOfSync) {
// fallback, when the copies of the restored queues are out of sync or the queues are corrupted
Log.e(TAG, "Restored queues are corrupted", queueCopiesOutOfSync);
this.queue = new ArrayList<>();
this.originalQueue = new ArrayList<>();
this.currentPosition = INVALID_POSITION;
}

songsIsStale = true;
resetSongs();

restoreUniqueId();

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@
*/
public class MusicService extends MediaBrowserServiceCompat implements SharedPreferences.OnSharedPreferenceChangeListener, Playback.PlaybackCallbacks {

public static final String TAG = MusicService.class.getSimpleName();

public static final String VINYL_MUSIC_PLAYER_PACKAGE_NAME = "com.poupa.vinylmusicplayer";
public static final String MUSIC_PACKAGE_NAME = "com.android.music";

Expand Down Expand Up @@ -153,7 +151,7 @@ public class MusicService extends MediaBrowserServiceCompat implements SharedPre
private Playback playback;

private StaticPlayingQueue playingQueue = new StaticPlayingQueue();
/** is {@link MusicService#playingQueue} instance of {@link com.poupa.vinylmusicplayer.model.Song.DynamicPlayingQueue} or not */
/** is {@link MusicService#playingQueue} instance of {@link com.poupa.vinylmusicplayer.misc.queue.DynamicPlayingQueue} or not */
private boolean queueIsDynamic = false;

private boolean queuesRestored;
Expand Down Expand Up @@ -405,7 +403,7 @@ public synchronized void restoreQueuesAndPositionIfNecessary() {

queueIsDynamic = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(SAVED_QUEUE_TYPE, false);
if (queueIsDynamic) {
playingQueue = new DynamicPlayingQueue(new AlbumShufflingQueueLoader()); // For album shuffling V2: Will depend on a saved preference to have the same than before
playingQueue = new DynamicPlayingQueue(playingQueue, new AlbumShufflingQueueLoader()); // For album shuffling V2: Will depend on a saved preference to have the same than before
}

if (playingQueue.restoreQueue(this, restoredPosition)) {
Expand Down

0 comments on commit 19fbb2e

Please sign in to comment.