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 cce204e727
commit fc56df74a4
2 changed files with 25 additions and 3 deletions

View File

@ -100,6 +100,20 @@ public class Song implements Parcelable {
*/
public int flags;
/**
* @return true if it's possible to retrieve any songs, otherwise false. For example, false
* could be returned if there are no songs in the library.
*/
public static boolean isSongAvailable()
{
ContentResolver resolver = ContextApplication.getContext().getContentResolver();
Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
Cursor cursor = resolver.query(media, FILLED_PROJECTION, selection, null, null);
return cursor != null && cursor.getCount() > 0;
}
/**
* Returns a song randomly selected from all the songs in the Android
* MediaStore.
@ -149,7 +163,7 @@ public class Song implements Parcelable {
{
this.id = id;
}
/**
* Initialize the song with the specified id and flags. Call populate to
* fill fields in the song.

View File

@ -273,7 +273,8 @@ public final class SongTimeline {
/**
* Returns the song <code>delta</code> places away from the current
* position. If there is no song at the given position, a random
* song will be placed in that position.
* song will be placed in that position. Returns null if no songs are
* available.
*
* Note: This returns songs based on their position in the playback
* sequence, not necessarily the stored timeline. When repeat is enabled,
@ -284,6 +285,9 @@ public final class SongTimeline {
*/
public Song getSong(int delta)
{
if (!Song.isSongAvailable())
return null;
ArrayList<Song> timeline = mSongs;
Song song;
@ -332,8 +336,12 @@ public final class SongTimeline {
}
}
if (song == null || !song.query(false)) {
if (song == null)
return null;
if (!song.query(false)) {
song.copy(Song.randomSong());
if (song == null || !song.query(false))
return null;
}