Cleanup updateState()
Changes are mostly aesthetic. Was annoyed by the long conditionals.
This commit is contained in:
parent
83cdef201f
commit
7bbe6184e9
@ -176,7 +176,6 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
Object mStateLock = new Object();
|
Object mStateLock = new Object();
|
||||||
boolean mPlayingBeforeCall;
|
boolean mPlayingBeforeCall;
|
||||||
private int mPendingSeek;
|
private int mPendingSeek;
|
||||||
private Song mLastSongBroadcast;
|
|
||||||
public Receiver mReceiver;
|
public Receiver mReceiver;
|
||||||
public ComponentName mButtonReceiver;
|
public ComponentName mButtonReceiver;
|
||||||
public InCallListener mCallListener;
|
public InCallListener mCallListener;
|
||||||
@ -475,18 +474,25 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
private void setFlag(int flag)
|
private void setFlag(int flag)
|
||||||
{
|
{
|
||||||
synchronized (mStateLock) {
|
synchronized (mStateLock) {
|
||||||
updateState(mState | flag);
|
updateState(mState | flag, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void unsetFlag(int flag)
|
private void unsetFlag(int flag)
|
||||||
{
|
{
|
||||||
synchronized (mStateLock) {
|
synchronized (mStateLock) {
|
||||||
updateState(mState & ~flag);
|
updateState(mState & ~flag, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateState(int state)
|
/**
|
||||||
|
* Modify the service state.
|
||||||
|
*
|
||||||
|
* @param state Union of PlaybackService.STATE_* flags
|
||||||
|
* @param forceBroadcast Broadcast state/song update even if the state has
|
||||||
|
* not changed.
|
||||||
|
*/
|
||||||
|
private void updateState(int state, boolean forceBroadcast)
|
||||||
{
|
{
|
||||||
state &= ALL_FLAGS;
|
state &= ALL_FLAGS;
|
||||||
|
|
||||||
@ -509,7 +515,7 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
int oldState = mState;
|
int oldState = mState;
|
||||||
mState = state;
|
mState = state;
|
||||||
|
|
||||||
if (state != oldState || song != mLastSongBroadcast) {
|
if (state != oldState || forceBroadcast) {
|
||||||
Intent intent = new Intent(EVENT_CHANGED);
|
Intent intent = new Intent(EVENT_CHANGED);
|
||||||
intent.putExtra("state", state);
|
intent.putExtra("state", state);
|
||||||
intent.putExtra("song", song);
|
intent.putExtra("song", song);
|
||||||
@ -525,39 +531,45 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateNotification(song);
|
updateNotification(song);
|
||||||
|
|
||||||
mLastSongBroadcast = song;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((oldState & PlaybackService.FLAG_SHUFFLE) == 0 && (state & PlaybackService.FLAG_SHUFFLE) != 0) {
|
int toggled = oldState ^ state;
|
||||||
mTimeline.setShuffle(true);
|
|
||||||
Toast.makeText(this, R.string.shuffle_enabling, Toast.LENGTH_LONG).show();
|
|
||||||
} else if ((oldState & PlaybackService.FLAG_SHUFFLE) != 0 && (state & PlaybackService.FLAG_SHUFFLE) == 0) {
|
|
||||||
mTimeline.setShuffle(false);
|
|
||||||
Toast.makeText(this, R.string.shuffle_disabling, Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((oldState & PlaybackService.FLAG_REPEAT) == 0 && (state & PlaybackService.FLAG_REPEAT) != 0) {
|
if ((toggled & FLAG_SHUFFLE) != 0) {
|
||||||
mTimeline.setRepeat(true);
|
if ((state & FLAG_SHUFFLE) != 0) {
|
||||||
Toast.makeText(this, R.string.repeat_enabling, Toast.LENGTH_LONG).show();
|
mTimeline.setShuffle(true);
|
||||||
} else if ((oldState & PlaybackService.FLAG_REPEAT) != 0 && (state & PlaybackService.FLAG_REPEAT) == 0) {
|
Toast.makeText(this, R.string.shuffle_enabling, Toast.LENGTH_LONG).show();
|
||||||
mTimeline.setRepeat(false);
|
} else {
|
||||||
Toast.makeText(this, R.string.repeat_disabling, Toast.LENGTH_SHORT).show();
|
mTimeline.setShuffle(false);
|
||||||
}
|
Toast.makeText(this, R.string.shuffle_disabling, Toast.LENGTH_SHORT).show();
|
||||||
|
|
||||||
if ((state & FLAG_PLAYING) != 0 && (oldState & FLAG_PLAYING) == 0) {
|
|
||||||
if (mNotificationMode != NEVER)
|
|
||||||
startForegroundCompat(NOTIFICATION_ID, mNotification);
|
|
||||||
if (mMediaPlayerInitialized) {
|
|
||||||
synchronized (mMediaPlayer) {
|
|
||||||
mMediaPlayer.start();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if ((state & FLAG_PLAYING) == 0 && (oldState & FLAG_PLAYING) != 0) {
|
}
|
||||||
stopForegroundCompat(false);
|
|
||||||
if (mMediaPlayerInitialized) {
|
if ((toggled & FLAG_REPEAT) != 0) {
|
||||||
synchronized (mMediaPlayer) {
|
if ((state & FLAG_REPEAT) != 0) {
|
||||||
mMediaPlayer.pause();
|
mTimeline.setRepeat(true);
|
||||||
|
Toast.makeText(this, R.string.repeat_enabling, Toast.LENGTH_LONG).show();
|
||||||
|
} else {
|
||||||
|
mTimeline.setRepeat(false);
|
||||||
|
Toast.makeText(this, R.string.repeat_disabling, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((toggled & FLAG_PLAYING) != 0) {
|
||||||
|
if ((state & FLAG_PLAYING) != 0) {
|
||||||
|
if (mNotificationMode != NEVER)
|
||||||
|
startForegroundCompat(NOTIFICATION_ID, mNotification);
|
||||||
|
if (mMediaPlayerInitialized) {
|
||||||
|
synchronized (mMediaPlayer) {
|
||||||
|
mMediaPlayer.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
stopForegroundCompat(false);
|
||||||
|
if (mMediaPlayerInitialized) {
|
||||||
|
synchronized (mMediaPlayer) {
|
||||||
|
mMediaPlayer.pause();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -582,7 +594,7 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
public void toggleFlag(int flag)
|
public void toggleFlag(int flag)
|
||||||
{
|
{
|
||||||
synchronized (mStateLock) {
|
synchronized (mStateLock) {
|
||||||
updateState(mState ^ flag);
|
updateState(mState ^ flag, false);
|
||||||
}
|
}
|
||||||
userActionTriggered();
|
userActionTriggered();
|
||||||
}
|
}
|
||||||
@ -610,10 +622,6 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
unsetFlag(FLAG_NO_MEDIA);
|
unsetFlag(FLAG_NO_MEDIA);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure that we broadcast a change event even if we play the same
|
|
||||||
// song again.
|
|
||||||
mLastSongBroadcast = null;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
synchronized (mMediaPlayer) {
|
synchronized (mMediaPlayer) {
|
||||||
mMediaPlayer.reset();
|
mMediaPlayer.reset();
|
||||||
@ -624,11 +632,11 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
}
|
}
|
||||||
if ((mState & FLAG_PLAYING) != 0)
|
if ((mState & FLAG_PLAYING) != 0)
|
||||||
mMediaPlayer.start();
|
mMediaPlayer.start();
|
||||||
updateState(mState & ~FLAG_ERROR);
|
updateState(mState & ~FLAG_ERROR, true);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// FLAG_PLAYING is set to force showing the Toast. updateState()
|
// FLAG_PLAYING is set to force showing the Toast. updateState()
|
||||||
// will unset it.
|
// will unset it.
|
||||||
updateState(mState | FLAG_PLAYING | FLAG_ERROR);
|
updateState(mState | FLAG_PLAYING | FLAG_ERROR, true);
|
||||||
Log.e("VanillaMusic", "IOException", e);
|
Log.e("VanillaMusic", "IOException", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user