persistant expanded notification with play/pause/stop buttons

This commit is contained in:
Mike Chelen 2014-07-30 03:59:34 -04:00
parent 460e061d35
commit fb4ccb381b
2 changed files with 35 additions and 4 deletions

View File

@ -90,6 +90,7 @@
<string name="download_label">Download</string>
<string name="play_label">Play</string>
<string name="pause_label">Pause</string>
<string name="stop_label">Stop</string>
<string name="stream_label">Stream</string>
<string name="remove_label">Remove</string>
<string name="remove_episode_lable">Remove episode</string>

View File

@ -323,6 +323,12 @@ public class PlaybackService extends Service {
case KeyEvent.KEYCODE_MEDIA_REWIND:
mediaPlayer.seekDelta(-UserPreferences.getSeekDeltaMs());
break;
case KeyEvent.KEYCODE_MEDIA_STOP:
if (status == PlayerStatus.PLAYING) {
mediaPlayer.pause(true, true);
}
stopForeground(true); // gets rid of persistent notification
break;
default:
if (info.playable != null && info.playerStatus == PlayerStatus.PLAYING) { // only notify the user about an unknown key event if it is actually doing something
String message = String.format(getResources().getString(R.string.unknown_media_key), keycode);
@ -396,7 +402,7 @@ public class PlaybackService extends Service {
taskManager.cancelPositionSaver();
saveCurrentPosition(false, 0);
taskManager.cancelWidgetUpdater();
stopForeground(true);
// stopForeground(true); // do not remove notification on pause
break;
case STOPPED:
@ -703,7 +709,7 @@ public class PlaybackService extends Service {
String contentTitle = info.playable.getEpisodeTitle();
Notification notification = null;
if (android.os.Build.VERSION.SDK_INT >= 16) {
Intent pauseButtonIntent = new Intent(
Intent pauseButtonIntent = new Intent( // pause button intent
PlaybackService.this, PlaybackService.class);
pauseButtonIntent.putExtra(
MediaButtonReceiver.EXTRA_KEYCODE,
@ -712,6 +718,24 @@ public class PlaybackService extends Service {
.getService(PlaybackService.this, 0,
pauseButtonIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Intent playButtonIntent = new Intent( // play button intent
PlaybackService.this, PlaybackService.class);
playButtonIntent.putExtra(
MediaButtonReceiver.EXTRA_KEYCODE,
KeyEvent.KEYCODE_MEDIA_PLAY);
PendingIntent playButtonPendingIntent = PendingIntent
.getService(PlaybackService.this, 1,
playButtonIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Intent stopButtonIntent = new Intent( // stop button intent
PlaybackService.this, PlaybackService.class);
stopButtonIntent.putExtra(
MediaButtonReceiver.EXTRA_KEYCODE,
KeyEvent.KEYCODE_MEDIA_STOP);
PendingIntent stopButtonPendingIntent = PendingIntent
.getService(PlaybackService.this, 2,
stopButtonIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder notificationBuilder = new Notification.Builder(
PlaybackService.this)
.setContentTitle(contentTitle)
@ -720,9 +744,15 @@ public class PlaybackService extends Service {
.setContentIntent(pIntent)
.setLargeIcon(icon)
.setSmallIcon(R.drawable.ic_stat_antenna)
.addAction(android.R.drawable.ic_media_pause,
.addAction(android.R.drawable.ic_media_pause, //pause action
getString(R.string.pause_label),
pauseButtonPendingIntent);
pauseButtonPendingIntent)
.addAction(android.R.drawable.ic_media_play, //play action
getString(R.string.play_label),
playButtonPendingIntent)
.addAction(android.R.drawable.ic_media_stop, // stop action
getString(R.string.stop_label),
stopButtonPendingIntent);
notification = notificationBuilder.build();
} else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(