Don't crash with an invalid state

This commit is contained in:
Christopher Eby 2010-03-08 01:45:05 -06:00
parent f108489413
commit ed670a73e3

View File

@ -323,6 +323,8 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
mRandom = new Random(); mRandom = new Random();
boolean stateLoaded = true;
try { try {
DataInputStream in = new DataInputStream(openFileInput(STATE_FILE)); DataInputStream in = new DataInputStream(openFileInput(STATE_FILE));
if (in.readLong() == STATE_FILE_MAGIC) { if (in.readLong() == STATE_FILE_MAGIC) {
@ -337,7 +339,10 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
in.close(); in.close();
Song song = new Song(ids[mCurrentSong]); Song song = new Song(ids[mCurrentSong]);
broadcastSongChange(song); if (song.path == null)
stateLoaded = false;
else
broadcastSongChange(song);
ArrayList<Song> timeline = new ArrayList<Song>(n); ArrayList<Song> timeline = new ArrayList<Song>(n);
for (int i = 0; i != n; ++i) for (int i = 0; i != n; ++i)
@ -351,7 +356,8 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
Log.w("VanillaMusic", e); Log.w("VanillaMusic", e);
} }
boolean stateLoaded = mSongTimeline != null; if (stateLoaded)
stateLoaded = mSongTimeline != null;
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
@ -636,21 +642,28 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
synchronized (mSongTimeline) { synchronized (mSongTimeline) {
int pos = mCurrentSong + delta; int pos = mCurrentSong + delta;
if (pos < 0) if (pos < 0)
return null; return null;
int size = mSongTimeline.size(); int size = mSongTimeline.size();
if (pos > size) if (pos > size)
return null; return null;
if (pos == size) { if (pos == size) {
if (mSongs == null) if (mSongs == null)
return null; return null;
mSongTimeline.add(randomSong()); mSongTimeline.add(randomSong());
} }
return mSongTimeline.get(pos); Song song = mSongTimeline.get(pos);
if (song.path == null) {
song = randomSong();
mSongTimeline.set(pos, song);
}
return song;
} }
} }