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="download_label">Download</string>
<string name="play_label">Play</string> <string name="play_label">Play</string>
<string name="pause_label">Pause</string> <string name="pause_label">Pause</string>
<string name="stop_label">Stop</string>
<string name="stream_label">Stream</string> <string name="stream_label">Stream</string>
<string name="remove_label">Remove</string> <string name="remove_label">Remove</string>
<string name="remove_episode_lable">Remove episode</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: case KeyEvent.KEYCODE_MEDIA_REWIND:
mediaPlayer.seekDelta(-UserPreferences.getSeekDeltaMs()); mediaPlayer.seekDelta(-UserPreferences.getSeekDeltaMs());
break; break;
case KeyEvent.KEYCODE_MEDIA_STOP:
if (status == PlayerStatus.PLAYING) {
mediaPlayer.pause(true, true);
}
stopForeground(true); // gets rid of persistent notification
break;
default: default:
if (info.playable != null && info.playerStatus == PlayerStatus.PLAYING) { // only notify the user about an unknown key event if it is actually doing something 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); String message = String.format(getResources().getString(R.string.unknown_media_key), keycode);
@ -396,7 +402,7 @@ public class PlaybackService extends Service {
taskManager.cancelPositionSaver(); taskManager.cancelPositionSaver();
saveCurrentPosition(false, 0); saveCurrentPosition(false, 0);
taskManager.cancelWidgetUpdater(); taskManager.cancelWidgetUpdater();
stopForeground(true); // stopForeground(true); // do not remove notification on pause
break; break;
case STOPPED: case STOPPED:
@ -703,7 +709,7 @@ public class PlaybackService extends Service {
String contentTitle = info.playable.getEpisodeTitle(); String contentTitle = info.playable.getEpisodeTitle();
Notification notification = null; Notification notification = null;
if (android.os.Build.VERSION.SDK_INT >= 16) { if (android.os.Build.VERSION.SDK_INT >= 16) {
Intent pauseButtonIntent = new Intent( Intent pauseButtonIntent = new Intent( // pause button intent
PlaybackService.this, PlaybackService.class); PlaybackService.this, PlaybackService.class);
pauseButtonIntent.putExtra( pauseButtonIntent.putExtra(
MediaButtonReceiver.EXTRA_KEYCODE, MediaButtonReceiver.EXTRA_KEYCODE,
@ -712,6 +718,24 @@ public class PlaybackService extends Service {
.getService(PlaybackService.this, 0, .getService(PlaybackService.this, 0,
pauseButtonIntent, pauseButtonIntent,
PendingIntent.FLAG_UPDATE_CURRENT); 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( Notification.Builder notificationBuilder = new Notification.Builder(
PlaybackService.this) PlaybackService.this)
.setContentTitle(contentTitle) .setContentTitle(contentTitle)
@ -720,9 +744,15 @@ public class PlaybackService extends Service {
.setContentIntent(pIntent) .setContentIntent(pIntent)
.setLargeIcon(icon) .setLargeIcon(icon)
.setSmallIcon(R.drawable.ic_stat_antenna) .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), 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(); notification = notificationBuilder.build();
} else { } else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(