Fixed null pointer exception when no songs are present in the

device's library.
This commit is contained in:
David Beswick 2011-07-11 23:03:22 +10:00 committed by Christopher Eby
parent fc56df74a4
commit bd0596cd9e
3 changed files with 23 additions and 26 deletions

View File

@ -565,7 +565,10 @@ public final class PlaybackService extends Service implements Handler.Callback,
Song song = mTimeline.shiftCurrentSong(delta); Song song = mTimeline.shiftCurrentSong(delta);
if (song == null) { if (song == null) {
setFlag(FLAG_NO_MEDIA); if (Song.isSongAvailable())
setCurrentSong(+1); // we only encountered a bad song; skip it
else
setFlag(FLAG_NO_MEDIA); // we don't have any songs : /
return; return;
} else if ((mState & FLAG_NO_MEDIA) != 0) { } else if ((mState & FLAG_NO_MEDIA) != 0) {
unsetFlag(FLAG_NO_MEDIA); unsetFlag(FLAG_NO_MEDIA);

View File

@ -59,6 +59,10 @@ public class Song implements Parcelable {
*/ */
private static final Song[] mRandomSongs = new Song[5]; private static final Song[] mRandomSongs = new Song[5];
private static final String[] EMPTY_PROJECTION = {
MediaStore.Audio.Media._ID,
};
private static final String[] FILLED_PROJECTION = { private static final String[] FILLED_PROJECTION = {
MediaStore.Audio.Media._ID, MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DATA,
@ -109,7 +113,7 @@ public class Song implements Parcelable {
ContentResolver resolver = ContextApplication.getContext().getContentResolver(); ContentResolver resolver = ContextApplication.getContext().getContentResolver();
Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0"; String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
Cursor cursor = resolver.query(media, FILLED_PROJECTION, selection, null, null); Cursor cursor = resolver.query(media, EMPTY_PROJECTION, selection, null, null);
return cursor != null && cursor.getCount() > 0; return cursor != null && cursor.getCount() > 0;
} }

View File

@ -273,11 +273,11 @@ public final class SongTimeline {
/** /**
* Returns the song <code>delta</code> places away from the current * Returns the song <code>delta</code> places away from the current
* position. If there is no song at the given position, a random * position. If there is no song at the given position, a random
* song will be placed in that position. Returns null if no songs are * song will be placed in that position. Returns null if there is a problem
* available. * retrieving the song (caused by either an empty library or stale song id).
* *
* Note: This returns songs based on their position in the playback * Note: This returns songs based on their position in the playback
* sequence, not necessarily the stored timeline. When repeat is enabled, * sequence, not their position in the stored timeline. When repeat is enabled,
* the two will differ. * the two will differ.
* *
* @param delta The offset from the current position. Should be -1, 0, or * @param delta The offset from the current position. Should be -1, 0, or
@ -285,28 +285,24 @@ public final class SongTimeline {
*/ */
public Song getSong(int delta) public Song getSong(int delta)
{ {
if (!Song.isSongAvailable())
return null;
ArrayList<Song> timeline = mSongs; ArrayList<Song> timeline = mSongs;
Song song; Song song = null;
synchronized (this) { synchronized (this) {
int pos = mCurrentPos + delta; int pos = mCurrentPos + delta;
if (pos < 0) if (pos < 0)
return null; return null;
int size = timeline.size(); while (pos >= timeline.size()) {
if (pos > size)
return null;
if (pos == size) {
song = Song.randomSong(); song = Song.randomSong();
if (song == null)
return null;
timeline.add(song); timeline.add(song);
} else {
song = timeline.get(pos);
} }
if (song == null)
song = timeline.get(pos);
if (song != null && mRepeatStart != -1 && (song.flags & Song.FLAG_RANDOM) != 0) { if (song != null && mRepeatStart != -1 && (song.flags & Song.FLAG_RANDOM) != 0) {
if (delta == 1 && mRepeatStart < mCurrentPos + 1) { if (delta == 1 && mRepeatStart < mCurrentPos + 1) {
// We have reached a non-user-selected song; this song will // We have reached a non-user-selected song; this song will
@ -336,16 +332,10 @@ public final class SongTimeline {
} }
} }
if (song == null) if (!song.query(false))
// we have a stale song id
return null; return null;
if (!song.query(false)) {
song.copy(Song.randomSong());
if (song == null || !song.query(false))
return null;
}
return song; return song;
} }