From 87dffd45e1963475ea01fb85abdb9e3d03c6b068 Mon Sep 17 00:00:00 2001 From: Tom Hennen Date: Sat, 7 Nov 2015 14:49:07 -0500 Subject: [PATCH 1/6] put ff and rewind buttons on the large notification. refs AntennaPod/AntennaPod#1268 --- .../service/playback/PlaybackService.java | 88 +++++++++++-------- 1 file changed, 49 insertions(+), 39 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 2ce91645b..0e306b452 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -836,42 +836,24 @@ public class PlaybackService extends Service { String contentTitle = info.playable.getFeedTitle(); Notification notification = null; - Intent pauseButtonIntent = new Intent( // pause button intent - PlaybackService.this, PlaybackService.class); - pauseButtonIntent.putExtra( - MediaButtonReceiver.EXTRA_KEYCODE, - KeyEvent.KEYCODE_MEDIA_PAUSE); - PendingIntent pauseButtonPendingIntent = PendingIntent - .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); - Intent skipButtonIntent = new Intent( - PlaybackService.this, PlaybackService.class); - skipButtonIntent.putExtra( - MediaButtonReceiver.EXTRA_KEYCODE, - KeyEvent.KEYCODE_MEDIA_NEXT); - PendingIntent skipButtonPendingIntent = PendingIntent - .getService(PlaybackService.this, 3, - skipButtonIntent, - PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent pauseButtonPendingIntent = getPendingIntentForMediaAction( + KeyEvent.KEYCODE_MEDIA_PAUSE, 0); + + PendingIntent playButtonPendingIntent = getPendingIntentForMediaAction( + KeyEvent.KEYCODE_MEDIA_PLAY, 1); + + PendingIntent stopButtonPendingIntent = getPendingIntentForMediaAction( + KeyEvent.KEYCODE_MEDIA_STOP, 2); + + PendingIntent skipButtonPendingIntent = getPendingIntentForMediaAction( + KeyEvent.KEYCODE_MEDIA_NEXT, 3); + + PendingIntent ffButtonPendingIntent = getPendingIntentForMediaAction( + KeyEvent.KEYCODE_MEDIA_FAST_FORWARD, 4); + + PendingIntent rewindButtonPendingIntent = getPendingIntentForMediaAction( + KeyEvent.KEYCODE_MEDIA_REWIND, 5); + NotificationCompat.Builder notificationBuilder = new android.support.v7.app.NotificationCompat.Builder( PlaybackService.this) .setContentTitle(contentTitle) @@ -883,22 +865,38 @@ public class PlaybackService extends Service { .setWhen(0) // we don't need the time .setPriority(UserPreferences.getNotifyPriority()); // set notification priority IntList actionList = new IntList(); + + + // always let them rewind + notificationBuilder.addAction(android.R.drawable.ic_media_rew, + getString(R.string.rewind_label), + rewindButtonPendingIntent); + + int numActions = 0; // we start and 0 and then increment by 1 for each call to addAction + if (playerStatus == PlayerStatus.PLAYING) { notificationBuilder.addAction(android.R.drawable.ic_media_pause, //pause action getString(R.string.pause_label), pauseButtonPendingIntent); - actionList.add(actionList.size()); + actionList.add(++numActions); } else { notificationBuilder.addAction(android.R.drawable.ic_media_play, //play action getString(R.string.play_label), playButtonPendingIntent); - actionList.add(actionList.size()); + actionList.add(++numActions); } + + // ff follows play, then we have skip (if it's present) + notificationBuilder.addAction(android.R.drawable.ic_media_ff, + getString(R.string.fast_forward_label), + ffButtonPendingIntent); + ++numActions; + if (UserPreferences.isFollowQueue()) { notificationBuilder.addAction(android.R.drawable.ic_media_next, getString(R.string.skip_episode_label), skipButtonPendingIntent); - actionList.add(actionList.size()); + actionList.add(++numActions); } notificationBuilder.setStyle(new android.support.v7.app.NotificationCompat.MediaStyle() @@ -928,6 +926,18 @@ public class PlaybackService extends Service { notificationSetupThread.start(); } + private PendingIntent getPendingIntentForMediaAction(int keycodeValue, int requestCode) { + Intent intent = new Intent( + PlaybackService.this, PlaybackService.class); + intent.putExtra( + MediaButtonReceiver.EXTRA_KEYCODE, + keycodeValue); + return PendingIntent + .getService(PlaybackService.this, requestCode, + intent, + PendingIntent.FLAG_UPDATE_CURRENT); + } + /** * Persists the current position and last played time of the media file. * From d25899d858f3d217e5ecf21146d13dfac59b4329 Mon Sep 17 00:00:00 2001 From: Tom Hennen Date: Sat, 7 Nov 2015 14:55:38 -0500 Subject: [PATCH 2/6] make action handling make somewhat more sense --- .../core/service/playback/PlaybackService.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 0e306b452..115e7730a 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -864,44 +864,45 @@ public class PlaybackService extends Service { .setSmallIcon(smallIcon) .setWhen(0) // we don't need the time .setPriority(UserPreferences.getNotifyPriority()); // set notification priority - IntList actionList = new IntList(); + IntList compactActionList = new IntList(); + int numActions = 0; // we start and 0 and then increment by 1 for each call to addAction + // always let them rewind notificationBuilder.addAction(android.R.drawable.ic_media_rew, getString(R.string.rewind_label), rewindButtonPendingIntent); - - int numActions = 0; // we start and 0 and then increment by 1 for each call to addAction + numActions++; if (playerStatus == PlayerStatus.PLAYING) { notificationBuilder.addAction(android.R.drawable.ic_media_pause, //pause action getString(R.string.pause_label), pauseButtonPendingIntent); - actionList.add(++numActions); + compactActionList.add(numActions++); } else { notificationBuilder.addAction(android.R.drawable.ic_media_play, //play action getString(R.string.play_label), playButtonPendingIntent); - actionList.add(++numActions); + compactActionList.add(numActions++); } // ff follows play, then we have skip (if it's present) notificationBuilder.addAction(android.R.drawable.ic_media_ff, getString(R.string.fast_forward_label), ffButtonPendingIntent); - ++numActions; + numActions++; if (UserPreferences.isFollowQueue()) { notificationBuilder.addAction(android.R.drawable.ic_media_next, getString(R.string.skip_episode_label), skipButtonPendingIntent); - actionList.add(++numActions); + compactActionList.add(numActions++); } notificationBuilder.setStyle(new android.support.v7.app.NotificationCompat.MediaStyle() .setMediaSession(mediaPlayer.getSessionToken()) - .setShowActionsInCompactView(actionList.toArray()) + .setShowActionsInCompactView(compactActionList.toArray()) .setShowCancelButton(true) .setCancelButtonIntent(stopButtonPendingIntent)) .setVisibility(Notification.VISIBILITY_PUBLIC) From 72c2a46359b5b762ed5fb8cfc2e5c0b56f24b370 Mon Sep 17 00:00:00 2001 From: Tom Hennen Date: Sat, 7 Nov 2015 15:14:49 -0500 Subject: [PATCH 3/6] slight code refactoring, also BluetoothA2dp.EXTRA_STATE isn't available until API 11 --- .../service/playback/PlaybackService.java | 42 +++++++++++-------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 115e7730a..a019f4a98 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -836,23 +836,17 @@ public class PlaybackService extends Service { String contentTitle = info.playable.getFeedTitle(); Notification notification = null; - PendingIntent pauseButtonPendingIntent = getPendingIntentForMediaAction( - KeyEvent.KEYCODE_MEDIA_PAUSE, 0); - PendingIntent playButtonPendingIntent = getPendingIntentForMediaAction( - KeyEvent.KEYCODE_MEDIA_PLAY, 1); - PendingIntent stopButtonPendingIntent = getPendingIntentForMediaAction( - KeyEvent.KEYCODE_MEDIA_STOP, 2); - PendingIntent skipButtonPendingIntent = getPendingIntentForMediaAction( - KeyEvent.KEYCODE_MEDIA_NEXT, 3); - PendingIntent ffButtonPendingIntent = getPendingIntentForMediaAction( - KeyEvent.KEYCODE_MEDIA_FAST_FORWARD, 4); - PendingIntent rewindButtonPendingIntent = getPendingIntentForMediaAction( - KeyEvent.KEYCODE_MEDIA_REWIND, 5); + + + + + + NotificationCompat.Builder notificationBuilder = new android.support.v7.app.NotificationCompat.Builder( PlaybackService.this) @@ -870,17 +864,23 @@ public class PlaybackService extends Service { int numActions = 0; // we start and 0 and then increment by 1 for each call to addAction // always let them rewind + PendingIntent rewindButtonPendingIntent = getPendingIntentForMediaAction( + KeyEvent.KEYCODE_MEDIA_REWIND, numActions); notificationBuilder.addAction(android.R.drawable.ic_media_rew, getString(R.string.rewind_label), rewindButtonPendingIntent); numActions++; if (playerStatus == PlayerStatus.PLAYING) { + PendingIntent pauseButtonPendingIntent = getPendingIntentForMediaAction( + KeyEvent.KEYCODE_MEDIA_PAUSE, numActions); notificationBuilder.addAction(android.R.drawable.ic_media_pause, //pause action getString(R.string.pause_label), pauseButtonPendingIntent); compactActionList.add(numActions++); } else { + PendingIntent playButtonPendingIntent = getPendingIntentForMediaAction( + KeyEvent.KEYCODE_MEDIA_PLAY, numActions); notificationBuilder.addAction(android.R.drawable.ic_media_play, //play action getString(R.string.play_label), playButtonPendingIntent); @@ -888,18 +888,24 @@ public class PlaybackService extends Service { } // ff follows play, then we have skip (if it's present) + PendingIntent ffButtonPendingIntent = getPendingIntentForMediaAction( + KeyEvent.KEYCODE_MEDIA_FAST_FORWARD, numActions); notificationBuilder.addAction(android.R.drawable.ic_media_ff, getString(R.string.fast_forward_label), ffButtonPendingIntent); numActions++; if (UserPreferences.isFollowQueue()) { + PendingIntent skipButtonPendingIntent = getPendingIntentForMediaAction( + KeyEvent.KEYCODE_MEDIA_NEXT, numActions); notificationBuilder.addAction(android.R.drawable.ic_media_next, getString(R.string.skip_episode_label), skipButtonPendingIntent); compactActionList.add(numActions++); } + PendingIntent stopButtonPendingIntent = getPendingIntentForMediaAction( + KeyEvent.KEYCODE_MEDIA_STOP, numActions); notificationBuilder.setStyle(new android.support.v7.app.NotificationCompat.MediaStyle() .setMediaSession(mediaPlayer.getSessionToken()) .setShowActionsInCompactView(compactActionList.toArray()) @@ -1046,11 +1052,13 @@ public class PlaybackService extends Service { private BroadcastReceiver bluetoothStateUpdated = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - if (StringUtils.equals(intent.getAction(), BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)) { - int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, -1); - if (state == BluetoothA2dp.STATE_CONNECTED) { - Log.d(TAG, "Received bluetooth connection intent"); - unpauseIfPauseOnDisconnect(true); + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + if (StringUtils.equals(intent.getAction(), BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)) { + int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, -1); + if (state == BluetoothA2dp.STATE_CONNECTED) { + Log.d(TAG, "Received bluetooth connection intent"); + unpauseIfPauseOnDisconnect(true); + } } } } From 32e870aeb506cbac28e19973f2e1a93c4b3c8e4c Mon Sep 17 00:00:00 2001 From: Tom Hennen Date: Sat, 7 Nov 2015 15:16:37 -0500 Subject: [PATCH 4/6] get rid of whitespace. :( --- .../core/service/playback/PlaybackService.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index a019f4a98..c4e3b6f51 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -836,18 +836,6 @@ public class PlaybackService extends Service { String contentTitle = info.playable.getFeedTitle(); Notification notification = null; - - - - - - - - - - - - NotificationCompat.Builder notificationBuilder = new android.support.v7.app.NotificationCompat.Builder( PlaybackService.this) .setContentTitle(contentTitle) From 42e6dffc82fc6a51a24a6f971bd3f52476b5d764 Mon Sep 17 00:00:00 2001 From: Tom Hennen Date: Sat, 7 Nov 2015 15:28:24 -0500 Subject: [PATCH 5/6] also check in register and unregister. make receivers final --- .../service/playback/PlaybackService.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index c4e3b6f51..ecb307196 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -1,6 +1,5 @@ package de.danoeh.antennapod.core.service.playback; -import android.annotation.SuppressLint; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; @@ -212,7 +211,6 @@ public class PlaybackService extends Service { return ClientConfig.playbackServiceCallbacks.getPlayerActivityIntent(context, mt); } - @SuppressLint("NewApi") @Override public void onCreate() { super.onCreate(); @@ -223,8 +221,10 @@ public class PlaybackService extends Service { Intent.ACTION_HEADSET_PLUG)); registerReceiver(shutdownReceiver, new IntentFilter( ACTION_SHUTDOWN_PLAYBACK_SERVICE)); - registerReceiver(bluetoothStateUpdated, new IntentFilter( - BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)); + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + registerReceiver(bluetoothStateUpdated, new IntentFilter( + BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)); + } registerReceiver(audioBecomingNoisy, new IntentFilter( AudioManager.ACTION_AUDIO_BECOMING_NOISY)); registerReceiver(skipCurrentEpisodeReceiver, new IntentFilter( @@ -238,7 +238,6 @@ public class PlaybackService extends Service { } - @SuppressLint("NewApi") @Override public void onDestroy() { super.onDestroy(); @@ -249,7 +248,9 @@ public class PlaybackService extends Service { unregisterReceiver(headsetDisconnected); unregisterReceiver(shutdownReceiver); - unregisterReceiver(bluetoothStateUpdated); + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + unregisterReceiver(bluetoothStateUpdated); + } unregisterReceiver(audioBecomingNoisy); unregisterReceiver(skipCurrentEpisodeReceiver); unregisterReceiver(pausePlayCurrentEpisodeReceiver); @@ -1012,7 +1013,7 @@ public class PlaybackService extends Service { * Pauses playback when the headset is disconnected and the preference is * set */ - private BroadcastReceiver headsetDisconnected = new BroadcastReceiver() { + private final BroadcastReceiver headsetDisconnected = new BroadcastReceiver() { private static final String TAG = "headsetDisconnected"; private static final int UNPLUGGED = 0; private static final int PLUGGED = 1; @@ -1037,7 +1038,7 @@ public class PlaybackService extends Service { } }; - private BroadcastReceiver bluetoothStateUpdated = new BroadcastReceiver() { + private final BroadcastReceiver bluetoothStateUpdated = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { @@ -1052,7 +1053,7 @@ public class PlaybackService extends Service { } }; - private BroadcastReceiver audioBecomingNoisy = new BroadcastReceiver() { + private final BroadcastReceiver audioBecomingNoisy = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -1098,7 +1099,7 @@ public class PlaybackService extends Service { } } - private BroadcastReceiver shutdownReceiver = new BroadcastReceiver() { + private final BroadcastReceiver shutdownReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -1109,7 +1110,7 @@ public class PlaybackService extends Service { }; - private BroadcastReceiver skipCurrentEpisodeReceiver = new BroadcastReceiver() { + private final BroadcastReceiver skipCurrentEpisodeReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (StringUtils.equals(intent.getAction(), ACTION_SKIP_CURRENT_EPISODE)) { @@ -1119,7 +1120,7 @@ public class PlaybackService extends Service { } }; - private BroadcastReceiver pauseResumeCurrentEpisodeReceiver = new BroadcastReceiver() { + private final BroadcastReceiver pauseResumeCurrentEpisodeReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (StringUtils.equals(intent.getAction(), ACTION_RESUME_PLAY_CURRENT_EPISODE)) { @@ -1129,7 +1130,7 @@ public class PlaybackService extends Service { } }; - private BroadcastReceiver pausePlayCurrentEpisodeReceiver = new BroadcastReceiver() { + private final BroadcastReceiver pausePlayCurrentEpisodeReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (StringUtils.equals(intent.getAction(), ACTION_PAUSE_PLAY_CURRENT_EPISODE)) { From 3b13ff1faab6de7a97ee08b64142cde5b29d89b3 Mon Sep 17 00:00:00 2001 From: Tom Hennen Date: Sat, 7 Nov 2015 15:32:21 -0500 Subject: [PATCH 6/6] missed a SuppressLint --- .../danoeh/antennapod/core/service/playback/PlaybackService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index ecb307196..5e0738cb8 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -786,7 +786,6 @@ public class PlaybackService extends Service { /** * Prepares notification and starts the service in the foreground. */ - @SuppressLint("NewApi") private void setupNotification(final PlaybackServiceMediaPlayer.PSMPInfo info) { final PendingIntent pIntent = PendingIntent.getActivity(this, 0, PlaybackService.getPlayerActivityIntent(this),