Simplify initial cover art load
We now load the state in Service onCreate so that the first song will always be ready when CoverView is initialized. This makes the code somewhat simpler.
This commit is contained in:
parent
a950805ee6
commit
aaf110cb1d
@ -80,12 +80,8 @@ public final class CoverView extends View {
|
||||
|
||||
public void setPlaybackService(IPlaybackService service)
|
||||
{
|
||||
try {
|
||||
mService = service;
|
||||
if (mService.isLoaded())
|
||||
refreshSongs();
|
||||
} catch (RemoteException e) {
|
||||
}
|
||||
mService = service;
|
||||
refreshSongs();
|
||||
}
|
||||
|
||||
private static void drawText(Canvas canvas, String text, float left, float top, float width, float maxWidth, Paint paint)
|
||||
@ -538,7 +534,6 @@ public final class CoverView extends View {
|
||||
|
||||
private static final int GO = 10;
|
||||
private static final int SET_SONG = 11;
|
||||
private static final int SET_COVER = 12;
|
||||
|
||||
private Handler mHandler = new Handler() {
|
||||
public void handleMessage(Message message) {
|
||||
@ -557,17 +552,14 @@ public final class CoverView extends View {
|
||||
mService = null;
|
||||
}
|
||||
break;
|
||||
case SET_COVER:
|
||||
mSongs[STORE_SIZE / 2] = (Song)message.obj;
|
||||
createBitmap(STORE_SIZE / 2);
|
||||
reset();
|
||||
break;
|
||||
default:
|
||||
int i = message.what;
|
||||
int delta = i - STORE_SIZE / 2;
|
||||
mSongs[i] = mService.getSong(delta);
|
||||
if (message.obj == null)
|
||||
mSongs[i] = mService.getSong(i - STORE_SIZE / 2);
|
||||
else
|
||||
mSongs[i] = (Song)message.obj;
|
||||
createBitmap(i);
|
||||
if (delta == 0)
|
||||
if (i == STORE_SIZE / 2)
|
||||
reset();
|
||||
break;
|
||||
}
|
||||
@ -581,16 +573,13 @@ public final class CoverView extends View {
|
||||
{
|
||||
String action = intent.getAction();
|
||||
if (PlaybackService.EVENT_REPLACE_SONG.equals(action)) {
|
||||
if (intent.getBooleanExtra("all", false))
|
||||
refreshSongs();
|
||||
else
|
||||
mHandler.sendEmptyMessage(2);
|
||||
int i = STORE_SIZE / 2 + intent.getIntExtra("pos", 0);
|
||||
Song song = intent.getParcelableExtra("song");
|
||||
mHandler.sendMessage(mHandler.obtainMessage(i, song));
|
||||
} else if (PlaybackService.EVENT_CHANGED.equals(action)) {
|
||||
Song currentSong = mSongs[STORE_SIZE / 2];
|
||||
Song playingSong = intent.getParcelableExtra("song");
|
||||
if (currentSong == null)
|
||||
mHandler.sendMessage(mHandler.obtainMessage(SET_COVER, playingSong));
|
||||
else if (!currentSong.equals(playingSong))
|
||||
if (currentSong == null || !currentSong.equals(playingSong))
|
||||
refreshSongs();
|
||||
}
|
||||
}
|
||||
|
@ -21,8 +21,6 @@ package org.kreed.vanilla;
|
||||
import org.kreed.vanilla.Song;
|
||||
|
||||
interface IPlaybackService {
|
||||
boolean isLoaded();
|
||||
|
||||
Song getSong(int delta);
|
||||
int getState();
|
||||
int getPosition();
|
||||
|
@ -115,6 +115,19 @@ public class PlaybackService extends Service implements Handler.Callback, MediaP
|
||||
{
|
||||
HandlerThread thread = new HandlerThread("PlaybackService");
|
||||
thread.start();
|
||||
|
||||
PlaybackServiceState state = new PlaybackServiceState();
|
||||
if (state.load(this)) {
|
||||
mSongTimeline = new ArrayList<Song>(state.savedIds.length);
|
||||
mCurrentSong = state.savedIndex;
|
||||
mPendingSeek = state.savedSeek;
|
||||
|
||||
for (int i = 0; i != state.savedIds.length; ++i)
|
||||
mSongTimeline.add(new Song(state.savedIds[i]));
|
||||
} else {
|
||||
mSongTimeline = new ArrayList<Song>();
|
||||
}
|
||||
|
||||
mLooper = thread.getLooper();
|
||||
mHandler = new Handler(mLooper, this);
|
||||
mHandler.sendEmptyMessage(CREATE);
|
||||
@ -215,26 +228,6 @@ public class PlaybackService extends Service implements Handler.Callback, MediaP
|
||||
|
||||
private void initialize()
|
||||
{
|
||||
PlaybackServiceState state = new PlaybackServiceState();
|
||||
if (state.load(this)) {
|
||||
Song song = new Song(state.savedIds[state.savedIndex]);
|
||||
if (song.populate(false)) {
|
||||
broadcastChange(mState, mState, song);
|
||||
|
||||
mSongTimeline = new ArrayList<Song>(state.savedIds.length);
|
||||
mCurrentSong = state.savedIndex;
|
||||
mPendingSeek = state.savedSeek;
|
||||
|
||||
for (int i = 0; i != state.savedIds.length; ++i)
|
||||
mSongTimeline.add(i == mCurrentSong ? song : new Song(state.savedIds[i]));
|
||||
}
|
||||
}
|
||||
|
||||
if (mSongTimeline == null) {
|
||||
mSongTimeline = new ArrayList<Song>();
|
||||
broadcastChange(mState, mState, getSong(0));
|
||||
}
|
||||
|
||||
mMediaPlayer = new MediaPlayer();
|
||||
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
||||
mMediaPlayer.setWakeMode(this, PowerManager.PARTIAL_WAKE_LOCK);
|
||||
@ -279,8 +272,6 @@ public class PlaybackService extends Service implements Handler.Callback, MediaP
|
||||
if (mPendingSeek != 0)
|
||||
mMediaPlayer.seekTo(mPendingSeek);
|
||||
|
||||
sendBroadcast(new Intent(EVENT_REPLACE_SONG).putExtra("all", true));
|
||||
|
||||
mHandler.sendEmptyMessage(POST_CREATE);
|
||||
}
|
||||
|
||||
@ -312,6 +303,14 @@ public class PlaybackService extends Service implements Handler.Callback, MediaP
|
||||
}
|
||||
}
|
||||
|
||||
private void broadcastReplaceSong(int delta)
|
||||
{
|
||||
Intent intent = new Intent(EVENT_REPLACE_SONG);
|
||||
intent.putExtra("pos", delta);
|
||||
intent.putExtra("song", getSong(delta));
|
||||
sendBroadcast(intent);
|
||||
}
|
||||
|
||||
public void broadcastChange(int oldState, int newState, Song song)
|
||||
{
|
||||
if (newState != oldState || song != mLastSongBroadcast) {
|
||||
@ -745,7 +744,7 @@ public class PlaybackService extends Service implements Handler.Callback, MediaP
|
||||
mHandler.sendEmptyMessage(TRACK_CHANGED);
|
||||
|
||||
if (changed)
|
||||
sendBroadcast(new Intent(EVENT_REPLACE_SONG));
|
||||
broadcastReplaceSong(+1);
|
||||
|
||||
mHandler.removeMessages(SAVE_STATE);
|
||||
mHandler.sendEmptyMessageDelayed(SAVE_STATE, 5000);
|
||||
@ -804,11 +803,6 @@ public class PlaybackService extends Service implements Handler.Callback, MediaP
|
||||
}
|
||||
|
||||
public IPlaybackService.Stub mBinder = new IPlaybackService.Stub() {
|
||||
public boolean isLoaded()
|
||||
{
|
||||
return mLoaded;
|
||||
}
|
||||
|
||||
public Song getSong(int delta)
|
||||
{
|
||||
return PlaybackService.this.getSong(delta);
|
||||
|
Loading…
x
Reference in New Issue
Block a user