Move song randomization into Song

This commit is contained in:
Christopher Eby 2010-03-28 18:37:20 -05:00
parent dc2d1ddbcc
commit e11c0fec25
2 changed files with 34 additions and 27 deletions

View File

@ -284,7 +284,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
private AudioManager mAudioManager; private AudioManager mAudioManager;
private NotificationManager mNotificationManager; private NotificationManager mNotificationManager;
private int[] mSongs;
private ArrayList<Song> mSongTimeline; private ArrayList<Song> mSongTimeline;
private int mCurrentSong; private int mCurrentSong;
private int mQueuePos; private int mQueuePos;
@ -329,8 +328,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
} }
} }
retrieveSongs();
if (mSongTimeline == null) { if (mSongTimeline == null) {
mSongTimeline = new ArrayList<Song>(); mSongTimeline = new ArrayList<Song>();
broadcastChange(mState, mState, getSong(0)); broadcastChange(mState, mState, getSong(0));
@ -485,15 +482,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
return true; return true;
} }
private void retrieveSongs()
{
mSongs = Song.getAllSongIds(null);
if (mSongs == null)
setFlag(FLAG_NO_MEDIA);
else if ((mState & FLAG_NO_MEDIA) != 0)
unsetFlag(FLAG_NO_MEDIA);
}
private boolean updateNotification(Song song) private boolean updateNotification(Song song)
{ {
if (song == null || !mNotifyWhilePaused && (mState & FLAG_PLAYING) == 0) { if (song == null || !mNotifyWhilePaused && (mState & FLAG_PLAYING) == 0) {
@ -587,11 +575,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
return true; return true;
} }
private int randomSong()
{
return mSongs[ContextApplication.getRandom().nextInt(mSongs.length)];
}
private Song getSong(int delta) private Song getSong(int delta)
{ {
if (mSongTimeline == null) if (mSongTimeline == null)
@ -609,20 +592,19 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
return null; return null;
if (pos == size) { if (pos == size) {
if (mSongs == null) song = new Song();
return null; mSongTimeline.add(song);
mSongTimeline.add(new Song(randomSong())); } else {
song = mSongTimeline.get(pos);
} }
song = mSongTimeline.get(pos);
} }
if (!song.populate()) { if (!song.populate()) {
if (mSongs == null) song.randomize();
return null; if (!song.populate()) {
song.id = randomSong(); setFlag(FLAG_NO_MEDIA);
if (!song.populate())
return null; return null;
}
} }
return song; return song;
@ -705,7 +687,10 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
mWakeLock.release(); mWakeLock.release();
break; break;
case RETRIEVE_SONGS: case RETRIEVE_SONGS:
retrieveSongs(); if (!Song.retrieveSongs())
setFlag(FLAG_NO_MEDIA);
else if ((mState & FLAG_NO_MEDIA) != 0)
unsetFlag(FLAG_NO_MEDIA);
break; break;
case CALL: case CALL:
boolean inCall = message.arg1 == 1; boolean inCall = message.arg1 == 1;

View File

@ -26,6 +26,8 @@ import android.os.Parcelable;
import android.provider.MediaStore; import android.provider.MediaStore;
public class Song implements Parcelable { public class Song implements Parcelable {
private static int[] mIds;
public int id; public int id;
public String path; public String path;
@ -35,6 +37,11 @@ public class Song implements Parcelable {
public String album; public String album;
public String artist; public String artist;
public Song()
{
randomize();
}
public Song(int id) public Song(int id)
{ {
this.id = id; this.id = id;
@ -127,6 +134,21 @@ public class Song implements Parcelable {
return null; return null;
} }
public static boolean retrieveSongs()
{
mIds = getAllSongIds(null);
return mIds != null;
}
public void randomize()
{
if (mIds == null) {
if (!retrieveSongs())
id = -1;
}
id = mIds[ContextApplication.getRandom().nextInt(mIds.length)];
}
public boolean equals(Song other) public boolean equals(Song other)
{ {
if (other == null) if (other == null)