Simplify song change handling
This fixes some issues with thread safety
This commit is contained in:
parent
ee5f907d3f
commit
52013ce482
@ -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()
|
||||
|
@ -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);
|
||||
}
|
@ -7,6 +7,7 @@ interface IPlaybackService {
|
||||
void registerWatcher(IMusicPlayerWatcher watcher);
|
||||
|
||||
Song[] getCurrentSongs();
|
||||
Song getSong(int delta);
|
||||
int getState();
|
||||
int getPosition();
|
||||
int getDuration();
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user