Make service actions public and add play/pause actions

This commit is contained in:
Christopher Eby 2011-09-27 18:23:05 -05:00
parent 0938428514
commit a62659a917
2 changed files with 60 additions and 9 deletions

View File

@ -84,7 +84,15 @@ THE SOFTWARE.
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
<service android:name="PlaybackService" />
<service android:name="PlaybackService">
<intent-filter>
<action android:name="org.kreed.vanilla.action.PLAY" />
<action android:name="org.kreed.vanilla.action.PAUSE" />
<action android:name="org.kreed.vanilla.action.TOGGLE_PLAYBACK" />
<action android:name="org.kreed.vanilla.action.NEXT_SONG" />
<action android:name="org.kreed.vanilla.action.PREVIOUS_SONG" />
</intent-filter>
</service>
<activity android:name="PreferencesActivity" />
</application>
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="10" />

View File

@ -75,6 +75,14 @@ public final class PlaybackService extends Service implements Handler.Callback,
* Action for startService: toggle playback on/off.
*/
public static final String ACTION_TOGGLE_PLAYBACK = "org.kreed.vanilla.action.TOGGLE_PLAYBACK";
/**
* Action for startService: start playback if paused.
*/
public static final String ACTION_PLAY = "org.kreed.vanilla.action.PLAY";
/**
* Action for startService: pause playback if playing.
*/
public static final String ACTION_PAUSE = "org.kreed.vanilla.action.PAUSE";
/**
* Action for startService: toggle playback on/off.
*
@ -299,9 +307,12 @@ public final class PlaybackService extends Service implements Handler.Callback,
go(-1, false);
} else if (ACTION_PREVIOUS_SONG_AUTOPLAY.equals(action)) {
go(-1, true);
} else if (ACTION_PLAY.equals(action)) {
play();
} else if (ACTION_PAUSE.equals(action)) {
pause();
}
userActionTriggered();
MediaButtonHandler buttons = MediaButtonHandler.getInstance(this);
if (buttons != null)
buttons.registerMediaButton();
@ -583,6 +594,39 @@ public final class PlaybackService extends Service implements Handler.Callback,
mNotificationManager.cancel(NOTIFICATION_ID);
}
/**
* Start playing if currently paused.
*
* @return The new state after this is called.
*/
public int play()
{
synchronized (mStateLock) {
if ((mState & FLAG_EMPTY_QUEUE) != 0) {
updateState((mState | FLAG_RANDOM) & ~FLAG_REPEAT);
setCurrentSong(0);
}
int state = updateState(mState | FLAG_PLAYING);
userActionTriggered();
return state;
}
}
/**
* Pause if currently playing.
*
* @return The new state after this is called.
*/
public int pause()
{
synchronized (mStateLock) {
int state = updateState(mState & ~FLAG_PLAYING);
userActionTriggered();
return state;
}
}
/**
* If playing, pause. If paused, play.
*
@ -591,13 +635,10 @@ public final class PlaybackService extends Service implements Handler.Callback,
public int playPause()
{
synchronized (mStateLock) {
userActionTriggered();
// If trying to play with empty queue, enter random mode.
if ((mState & FLAG_PLAYING) == 0 && (mState & FLAG_EMPTY_QUEUE) != 0) {
updateState((mState | FLAG_RANDOM) & ~FLAG_REPEAT);
setCurrentSong(0);
}
return updateState(mState ^ FLAG_PLAYING);
if ((mState & FLAG_PLAYING) != 0)
return pause();
else
return play();
}
}
@ -767,6 +808,8 @@ public final class PlaybackService extends Service implements Handler.Callback,
playPause();
else
setCurrentSong(delta);
userActionTriggered();
}
private class Receiver extends BroadcastReceiver {