From 52013ce482464d9d9e2ac7257821c0677e785f0c Mon Sep 17 00:00:00 2001 From: Christopher Eby Date: Fri, 19 Feb 2010 22:14:38 -0600 Subject: [PATCH] Simplify song change handling This fixes some issues with thread safety --- src/org/kreed/tumult/CoverView.java | 39 +++++++------ src/org/kreed/tumult/IMusicPlayerWatcher.aidl | 3 +- src/org/kreed/tumult/IPlaybackService.aidl | 1 + src/org/kreed/tumult/MusicPlayer.java | 13 +++-- src/org/kreed/tumult/NowPlayingActivity.java | 56 +++++++++---------- 5 files changed, 53 insertions(+), 59 deletions(-) diff --git a/src/org/kreed/tumult/CoverView.java b/src/org/kreed/tumult/CoverView.java index 0227b59f..0b4fd802 100755 --- a/src/org/kreed/tumult/CoverView.java +++ b/src/org/kreed/tumult/CoverView.java @@ -157,6 +157,13 @@ public class CoverView extends View { regenerateBitmaps(); } + public void setSong(int delta, Song song) + { + int i = 1 + delta; + mSongs[i] = song; + createBitmap(i); + } + public void regenerateBitmaps() { if (getWidth() == 0 || getHeight() == 0) @@ -167,30 +174,22 @@ public class CoverView extends View { reset(); } - public void setForwardSong(Song song) + public void shiftBackward() { - if (mSongs[mSongs.length - 1] != null) { - System.arraycopy(mSongs, 1, mSongs, 0, mSongs.length - 1); - System.arraycopy(mBitmaps, 1, mBitmaps, 0, mBitmaps.length - 1); - mBitmaps[mBitmaps.length - 1] = null; - reset(); - } - - mSongs[mSongs.length - 1] = song; - createBitmap(mSongs.length - 1); + System.arraycopy(mSongs, 1, mSongs, 0, mSongs.length - 1); + System.arraycopy(mBitmaps, 1, mBitmaps, 0, mBitmaps.length - 1); + mSongs[mSongs.length - 1] = null; + mBitmaps[mBitmaps.length - 1] = null; + reset(); } - public void setBackwardSong(Song song) + public void shiftForward() { - if (mSongs[0] != null) { - System.arraycopy(mSongs, 0, mSongs, 1, mSongs.length - 1); - System.arraycopy(mBitmaps, 0, mBitmaps, 1, mBitmaps.length - 1); - mBitmaps[0] = null; - reset(); - } - - mSongs[0] = song; - createBitmap(0); + System.arraycopy(mSongs, 0, mSongs, 1, mSongs.length - 1); + System.arraycopy(mBitmaps, 0, mBitmaps, 1, mBitmaps.length - 1); + mSongs[0] = null; + mBitmaps[0] = null; + reset(); } public void reset() diff --git a/src/org/kreed/tumult/IMusicPlayerWatcher.aidl b/src/org/kreed/tumult/IMusicPlayerWatcher.aidl index 388a91d8..a4d0924e 100644 --- a/src/org/kreed/tumult/IMusicPlayerWatcher.aidl +++ b/src/org/kreed/tumult/IMusicPlayerWatcher.aidl @@ -3,7 +3,6 @@ package org.kreed.tumult; import org.kreed.tumult.Song; oneway interface IMusicPlayerWatcher { - void previousSong(in Song playingSong, in Song nextForwardSong); - void nextSong(in Song playingSong, in Song nextBackwardSong); + void songChanged(in Song playingSong); void stateChanged(in int oldState, in int newState); } \ No newline at end of file diff --git a/src/org/kreed/tumult/IPlaybackService.aidl b/src/org/kreed/tumult/IPlaybackService.aidl index fdc3365a..b8d33ca7 100644 --- a/src/org/kreed/tumult/IPlaybackService.aidl +++ b/src/org/kreed/tumult/IPlaybackService.aidl @@ -7,6 +7,7 @@ interface IPlaybackService { void registerWatcher(IMusicPlayerWatcher watcher); Song[] getCurrentSongs(); + Song getSong(int delta); int getState(); int getPosition(); int getDuration(); diff --git a/src/org/kreed/tumult/MusicPlayer.java b/src/org/kreed/tumult/MusicPlayer.java index 9a7c0c4a..9d36bb31 100644 --- a/src/org/kreed/tumult/MusicPlayer.java +++ b/src/org/kreed/tumult/MusicPlayer.java @@ -37,7 +37,12 @@ public class MusicPlayer implements Runnable, MediaPlayer.OnCompletionListener, { return new Song[] { getSong(-1), getSong(0), getSong(1) }; } - + + public Song getSong(int delta) + { + return MusicPlayer.this.getSong(delta); + } + public int getState() { return mState; @@ -324,14 +329,10 @@ public class MusicPlayer implements Runnable, MediaPlayer.OnCompletionListener, Log.e("Tumult", "IOException", e); } - Song newSong = getSong(delta); int i = mWatchers.beginBroadcast(); while (--i != -1) { try { - if (delta < 0) - mWatchers.getBroadcastItem(i).previousSong(song, newSong); - else - mWatchers.getBroadcastItem(i).nextSong(song, newSong); + mWatchers.getBroadcastItem(i).songChanged(song); } catch (RemoteException e) { // Null elements will be removed automatically } diff --git a/src/org/kreed/tumult/NowPlayingActivity.java b/src/org/kreed/tumult/NowPlayingActivity.java index edfa2527..dff74b32 100644 --- a/src/org/kreed/tumult/NowPlayingActivity.java +++ b/src/org/kreed/tumult/NowPlayingActivity.java @@ -165,38 +165,23 @@ public class NowPlayingActivity extends Activity implements CoverViewWatcher, Se reconnect(); } - private boolean requestSongInfo(Song playingSong) - { - try { - mDuration = mService.getDuration(); - mHandler.sendEmptyMessage(UPDATE_PROGRESS); - } catch (RemoteException e) { - } - - if (!playingSong.equals(mCoverView.getActiveSong())) { - runOnUiThread(new Runnable() { - public void run() - { - refreshSongs(); - } - }); - return false; - } - - return true; - } - private IMusicPlayerWatcher mWatcher = new IMusicPlayerWatcher.Stub() { - public void nextSong(final Song playingSong, final Song forwardSong) + public void songChanged(Song playingSong) { - if (requestSongInfo(playingSong)) - mCoverView.setForwardSong(forwardSong); - } + try { + mDuration = mService.getDuration(); + mHandler.sendEmptyMessage(UPDATE_PROGRESS); + } catch (RemoteException e) { + } - public void previousSong(final Song playingSong, final Song backwardSong) - { - if (requestSongInfo(playingSong)) - mCoverView.setBackwardSong(backwardSong); + if (!playingSong.equals(mCoverView.getActiveSong())) { + runOnUiThread(new Runnable() { + public void run() + { + refreshSongs(); + } + }); + } } public void stateChanged(final int oldState, final int newState) @@ -212,18 +197,20 @@ public class NowPlayingActivity extends Activity implements CoverViewWatcher, Se public void next() { - mCoverView.setForwardSong(null); try { mService.nextSong(); + mCoverView.shiftBackward(); + mHandler.sendMessage(mHandler.obtainMessage(QUERY_SONG, 1, 0)); } catch (RemoteException e) { } } public void previous() { - mCoverView.setBackwardSong(null); try { mService.previousSong(); + mCoverView.shiftForward(); + mHandler.sendMessage(mHandler.obtainMessage(QUERY_SONG, -1, 0)); } catch (RemoteException e) { } } @@ -352,6 +339,7 @@ public class NowPlayingActivity extends Activity implements CoverViewWatcher, Se private static final int HIDE = 0; private static final int UPDATE_PROGRESS = 1; + private static final int QUERY_SONG = 2; private Handler mHandler = new Handler() { public void handleMessage(Message message) { @@ -364,6 +352,12 @@ public class NowPlayingActivity extends Activity implements CoverViewWatcher, Se case UPDATE_PROGRESS: updateProgress(); break; + case QUERY_SONG: + try { + int delta = message.arg1; + mCoverView.setSong(delta, mService.getSong(delta)); + } catch (RemoteException e) { + } } } };