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" /> <action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter> </intent-filter>
</receiver> </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" /> <activity android:name="PreferencesActivity" />
</application> </application>
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="10" /> <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. * Action for startService: toggle playback on/off.
*/ */
public static final String ACTION_TOGGLE_PLAYBACK = "org.kreed.vanilla.action.TOGGLE_PLAYBACK"; 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. * Action for startService: toggle playback on/off.
* *
@ -299,9 +307,12 @@ public final class PlaybackService extends Service implements Handler.Callback,
go(-1, false); go(-1, false);
} else if (ACTION_PREVIOUS_SONG_AUTOPLAY.equals(action)) { } else if (ACTION_PREVIOUS_SONG_AUTOPLAY.equals(action)) {
go(-1, true); go(-1, true);
} else if (ACTION_PLAY.equals(action)) {
play();
} else if (ACTION_PAUSE.equals(action)) {
pause();
} }
userActionTriggered();
MediaButtonHandler buttons = MediaButtonHandler.getInstance(this); MediaButtonHandler buttons = MediaButtonHandler.getInstance(this);
if (buttons != null) if (buttons != null)
buttons.registerMediaButton(); buttons.registerMediaButton();
@ -583,6 +594,39 @@ public final class PlaybackService extends Service implements Handler.Callback,
mNotificationManager.cancel(NOTIFICATION_ID); 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. * If playing, pause. If paused, play.
* *
@ -591,13 +635,10 @@ public final class PlaybackService extends Service implements Handler.Callback,
public int playPause() public int playPause()
{ {
synchronized (mStateLock) { synchronized (mStateLock) {
userActionTriggered(); if ((mState & FLAG_PLAYING) != 0)
// If trying to play with empty queue, enter random mode. return pause();
if ((mState & FLAG_PLAYING) == 0 && (mState & FLAG_EMPTY_QUEUE) != 0) { else
updateState((mState | FLAG_RANDOM) & ~FLAG_REPEAT); return play();
setCurrentSong(0);
}
return updateState(mState ^ FLAG_PLAYING);
} }
} }
@ -767,6 +808,8 @@ public final class PlaybackService extends Service implements Handler.Callback,
playPause(); playPause();
else else
setCurrentSong(delta); setCurrentSong(delta);
userActionTriggered();
} }
private class Receiver extends BroadcastReceiver { private class Receiver extends BroadcastReceiver {