Double tap detection for Bluetooth media button events only

Issue: androidx/media#233
#minor-release
PiperOrigin-RevId: 505078751
(cherry picked from commit 5c82d6bc18)
This commit is contained in:
bachinger 2023-01-27 11:55:14 +00:00 committed by christosts
parent c37442b24d
commit bcdedb719d
3 changed files with 9 additions and 10 deletions

View file

@ -49,6 +49,8 @@
* Add `onSetMediaItems` callback listener to provide means to modify/set
`MediaItem` list, starting index and position by session before setting
onto Player ([#156](https://github.com/androidx/media/issues/156)).
* Avoid double tap detection for non-Bluetooth media button events
([#233](https://github.com/androidx/media/issues/233)).
* Metadata:
* Parse multiple null-separated values from ID3 frames, as permitted by
ID3 v2.4.

View file

@ -121,6 +121,7 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
private final ConnectionTimeoutHandler connectionTimeoutHandler;
private final MediaPlayPauseKeyHandler mediaPlayPauseKeyHandler;
private final MediaSessionCompat sessionCompat;
private final String appPackageName;
@Nullable private VolumeProviderCompat volumeProviderCompat;
private volatile long connectionTimeoutMs;
@ -133,6 +134,7 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
Handler handler) {
sessionImpl = session;
Context context = sessionImpl.getContext();
appPackageName = context.getPackageName();
sessionManager = MediaSessionManager.getSessionManager(context);
controllerLegacyCbForBroadcast = new ControllerLegacyCbForBroadcast();
connectionTimeoutHandler =
@ -225,7 +227,11 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
switch (keyCode) {
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
case KeyEvent.KEYCODE_HEADSETHOOK:
if (keyEvent.getRepeatCount() == 0) {
// Double tap detection only for media button events from external sources (for instance
// Bluetooth). Media button events from the app package are coming from the notification
// below targetApiLevel 33.
if (!appPackageName.equals(remoteUserInfo.getPackageName())
&& keyEvent.getRepeatCount() == 0) {
if (mediaPlayPauseKeyHandler.hasPendingMediaPlayPauseKey()) {
mediaPlayPauseKeyHandler.clearPendingMediaPlayPauseKey();
onSkipToNext();

View file

@ -237,15 +237,6 @@ public class MediaSessionKeyEventTest {
player.awaitMethodCalled(MockPlayer.METHOD_PAUSE, TIMEOUT_MS);
}
@Test
public void playPauseKeyEvent_doubleTapIsTranslatedToSkipToNext() throws Exception {
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, true);
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO_NEXT, TIMEOUT_MS);
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PLAY)).isFalse();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PAUSE)).isFalse();
}
private void dispatchMediaKeyEvent(int keyCode, boolean doubleTap) {
audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keyCode));