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

View File

@ -26,6 +26,8 @@ import android.os.Parcelable;
import android.provider.MediaStore;
public class Song implements Parcelable {
private static int[] mIds;
public int id;
public String path;
@ -35,6 +37,11 @@ public class Song implements Parcelable {
public String album;
public String artist;
public Song()
{
randomize();
}
public Song(int id)
{
this.id = id;
@ -127,6 +134,21 @@ public class Song implements Parcelable {
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)
{
if (other == null)