Fix #1560 for some remaining platforms still affected

Added extra check in onStartCommand() for PlaybackService to check if
eventTime or downTime are positive (which seems to indicate the intent
was generated by a hardware button press instead of a touchscreen tap,
like the lockscreen playback controls).
This commit is contained in:
Mats Wahlberg 2020-06-20 15:25:21 +02:00
parent ea28400fc7
commit c6edf77fc2
2 changed files with 17 additions and 2 deletions

View File

@ -15,6 +15,7 @@ public class MediaButtonReceiver extends BroadcastReceiver {
private static final String TAG = "MediaButtonReceiver";
public static final String EXTRA_KEYCODE = "de.danoeh.antennapod.core.service.extra.MediaButtonReceiver.KEYCODE";
public static final String EXTRA_SOURCE = "de.danoeh.antennapod.core.service.extra.MediaButtonReceiver.SOURCE";
public static final String EXTRA_HARDWAREBUTTON = "de.danoeh.antennapod.core.service.extra.MediaButtonReceiver.HARDWAREBUTTON";
public static final String NOTIFY_BUTTON_RECEIVER = "de.danoeh.antennapod.NOTIFY_BUTTON_RECEIVER";
@ -30,6 +31,12 @@ public class MediaButtonReceiver extends BroadcastReceiver {
Intent serviceIntent = new Intent(context, PlaybackService.class);
serviceIntent.putExtra(EXTRA_KEYCODE, event.getKeyCode());
serviceIntent.putExtra(EXTRA_SOURCE, event.getSource());
//detect if this is a hardware button press
if (event.getEventTime() > 0 || event.getDownTime() > 0) {
serviceIntent.putExtra(EXTRA_HARDWAREBUTTON, true);
} else {
serviceIntent.putExtra(EXTRA_HARDWAREBUTTON, false);
}
ContextCompat.startForegroundService(context, serviceIntent);
}

View File

@ -450,6 +450,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
notificationManager.cancel(R.id.notification_streaming_confirmation);
final int keycode = intent.getIntExtra(MediaButtonReceiver.EXTRA_KEYCODE, -1);
final boolean hardwareButton = intent.getBooleanExtra(MediaButtonReceiver.EXTRA_HARDWAREBUTTON, false);
final boolean castDisconnect = intent.getBooleanExtra(EXTRA_CAST_DISCONNECT, false);
Playable playable = intent.getParcelableExtra(EXTRA_PLAYABLE);
if (keycode == -1 && playable == null && !castDisconnect) {
@ -463,8 +464,15 @@ public class PlaybackService extends MediaBrowserServiceCompat {
stateManager.stopForeground(true);
} else {
if (keycode != -1) {
Log.d(TAG, "Received media button event");
boolean handled = handleKeycode(keycode, true);
boolean notificationButton;
if (hardwareButton) {
Log.d(TAG, "Received hardware button event");
notificationButton = false;
} else {
Log.d(TAG, "Received media button event");
notificationButton = true;
}
boolean handled = handleKeycode(keycode, notificationButton);
if (!handled && !stateManager.hasReceivedValidStartCommand()) {
stateManager.stopService();
return Service.START_NOT_STICKY;