Cleanup state changing/song changing
Much of the code can be combined
This commit is contained in:
parent
1c7a4aa7c3
commit
ada3acacab
@ -305,6 +305,7 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
|||||||
private int mState = STATE_NORMAL;
|
private int mState = STATE_NORMAL;
|
||||||
private boolean mPlayingBeforeCall;
|
private boolean mPlayingBeforeCall;
|
||||||
private int mPendingSeek;
|
private int mPendingSeek;
|
||||||
|
private Song mLastSongBroadcast;
|
||||||
|
|
||||||
private Method mIsWiredHeadsetOn;
|
private Method mIsWiredHeadsetOn;
|
||||||
private Method mStartForeground;
|
private Method mStartForeground;
|
||||||
@ -316,8 +317,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
|||||||
private static final int QUEUE_ITEM = 4;
|
private static final int QUEUE_ITEM = 4;
|
||||||
private static final int TRACK_CHANGED = 5;
|
private static final int TRACK_CHANGED = 5;
|
||||||
private static final int RELEASE_WAKE_LOCK = 6;
|
private static final int RELEASE_WAKE_LOCK = 6;
|
||||||
private static final int HANDLE_PLAY = 7;
|
|
||||||
private static final int HANDLE_PAUSE = 8;
|
|
||||||
private static final int RETRIEVE_SONGS = 9;
|
private static final int RETRIEVE_SONGS = 9;
|
||||||
private static final int CALL = 10;
|
private static final int CALL = 10;
|
||||||
private static final int SAVE_STATE = 12;
|
private static final int SAVE_STATE = 12;
|
||||||
@ -455,16 +454,12 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setState(int state)
|
public void updateState(int state)
|
||||||
{
|
{
|
||||||
if (mState == state)
|
|
||||||
return;
|
|
||||||
|
|
||||||
int oldState = mState;
|
int oldState = mState;
|
||||||
mState = state;
|
mState = state;
|
||||||
|
|
||||||
updateNotification();
|
if (mState != oldState) {
|
||||||
|
|
||||||
int i = mWatchers.beginBroadcast();
|
int i = mWatchers.beginBroadcast();
|
||||||
while (--i != -1) {
|
while (--i != -1) {
|
||||||
try {
|
try {
|
||||||
@ -474,11 +469,18 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
mWatchers.finishBroadcast();
|
mWatchers.finishBroadcast();
|
||||||
|
}
|
||||||
|
|
||||||
|
Song song = getSong(0);
|
||||||
|
broadcastSongChange(song);
|
||||||
|
|
||||||
|
updateNotification();
|
||||||
|
updateWidgets();
|
||||||
|
|
||||||
if (mScrobble) {
|
if (mScrobble) {
|
||||||
Intent intent = new Intent("net.jjc1138.android.scrobbler.action.MUSIC_STATUS");
|
Intent intent = new Intent("net.jjc1138.android.scrobbler.action.MUSIC_STATUS");
|
||||||
intent.putExtra("playing", mState == STATE_PLAYING);
|
intent.putExtra("playing", mState == STATE_PLAYING);
|
||||||
intent.putExtra("id", getSong(0).id);
|
intent.putExtra("id", song.id);
|
||||||
sendBroadcast(intent);
|
sendBroadcast(intent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -486,12 +488,10 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
|||||||
private void retrieveSongs()
|
private void retrieveSongs()
|
||||||
{
|
{
|
||||||
mSongs = Song.getAllSongs();
|
mSongs = Song.getAllSongs();
|
||||||
if (mSongs == null) {
|
if (mSongs == null)
|
||||||
setState(STATE_NO_MEDIA);
|
updateState(STATE_NO_MEDIA);
|
||||||
} else {
|
else if (mState == STATE_NO_MEDIA)
|
||||||
if (mState == STATE_NO_MEDIA)
|
updateState(STATE_NORMAL);
|
||||||
setState(STATE_NORMAL);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateNotification()
|
private void updateNotification()
|
||||||
@ -548,13 +548,15 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
mMediaPlayer.start();
|
mMediaPlayer.start();
|
||||||
mHandler.sendEmptyMessage(HANDLE_PLAY);
|
updateState(STATE_PLAYING);
|
||||||
|
startForegroundCompat(NOTIFICATION_ID, mNotification);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void pause()
|
private void pause()
|
||||||
{
|
{
|
||||||
mMediaPlayer.pause();
|
mMediaPlayer.pause();
|
||||||
mHandler.sendEmptyMessage(HANDLE_PAUSE);
|
updateState(STATE_NORMAL);
|
||||||
|
stopForegroundCompat(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setPlaying(boolean play)
|
private void setPlaying(boolean play)
|
||||||
@ -567,6 +569,11 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
|||||||
|
|
||||||
private void broadcastSongChange(Song song)
|
private void broadcastSongChange(Song song)
|
||||||
{
|
{
|
||||||
|
if (song == mLastSongBroadcast)
|
||||||
|
return;
|
||||||
|
|
||||||
|
mLastSongBroadcast = song;
|
||||||
|
|
||||||
int i = mWatchers.beginBroadcast();
|
int i = mWatchers.beginBroadcast();
|
||||||
while (--i != -1) {
|
while (--i != -1) {
|
||||||
try {
|
try {
|
||||||
@ -598,7 +605,8 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
|||||||
mMediaPlayer.prepare();
|
mMediaPlayer.prepare();
|
||||||
}
|
}
|
||||||
if (mState == STATE_PLAYING)
|
if (mState == STATE_PLAYING)
|
||||||
play();
|
mMediaPlayer.start();
|
||||||
|
updateState(mState);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e("VanillaMusic", "IOException", e);
|
Log.e("VanillaMusic", "IOException", e);
|
||||||
}
|
}
|
||||||
@ -724,14 +732,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
|||||||
if (mWakeLock != null && mWakeLock.isHeld())
|
if (mWakeLock != null && mWakeLock.isHeld())
|
||||||
mWakeLock.release();
|
mWakeLock.release();
|
||||||
break;
|
break;
|
||||||
case HANDLE_PLAY:
|
|
||||||
setState(STATE_PLAYING);
|
|
||||||
startForegroundCompat(NOTIFICATION_ID, mNotification);
|
|
||||||
break;
|
|
||||||
case HANDLE_PAUSE:
|
|
||||||
setState(STATE_NORMAL);
|
|
||||||
stopForegroundCompat(false);
|
|
||||||
break;
|
|
||||||
case RETRIEVE_SONGS:
|
case RETRIEVE_SONGS:
|
||||||
retrieveSongs();
|
retrieveSongs();
|
||||||
break;
|
break;
|
||||||
@ -761,12 +761,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
|||||||
saveState(false);
|
saveState(false);
|
||||||
break;
|
break;
|
||||||
case PROCESS_SONG:
|
case PROCESS_SONG:
|
||||||
Song song = getSong(0);
|
|
||||||
|
|
||||||
broadcastSongChange(song);
|
|
||||||
updateNotification();
|
|
||||||
updateWidgets();
|
|
||||||
|
|
||||||
getSong(+2);
|
getSong(+2);
|
||||||
|
|
||||||
synchronized (mSongTimeline) {
|
synchronized (mSongTimeline) {
|
||||||
@ -776,6 +770,7 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mHandler.removeMessages(SAVE_STATE);
|
||||||
mHandler.sendEmptyMessageDelayed(SAVE_STATE, 5000);
|
mHandler.sendEmptyMessageDelayed(SAVE_STATE, 5000);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user