From 5c82d6bc18429842160bb64a851bb1ab5c89ec39 Mon Sep 17 00:00:00 2001 From: bachinger Date: Fri, 27 Jan 2023 11:55:14 +0000 Subject: [PATCH] Double tap detection for Bluetooth media button events only Issue: androidx/media#233 #minor-release PiperOrigin-RevId: 505078751 --- RELEASENOTES.md | 2 ++ .../androidx/media3/session/MediaSessionLegacyStub.java | 8 +++++++- .../media3/session/MediaSessionKeyEventTest.java | 9 --------- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 06c43f3b875..531d9edef57 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -55,6 +55,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. diff --git a/libraries/session/src/main/java/androidx/media3/session/MediaSessionLegacyStub.java b/libraries/session/src/main/java/androidx/media3/session/MediaSessionLegacyStub.java index 1e9caba13bf..b7aa79e8cfe 100644 --- a/libraries/session/src/main/java/androidx/media3/session/MediaSessionLegacyStub.java +++ b/libraries/session/src/main/java/androidx/media3/session/MediaSessionLegacyStub.java @@ -121,6 +121,7 @@ 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 @@ public MediaSessionLegacyStub( Handler handler) { sessionImpl = session; Context context = sessionImpl.getContext(); + appPackageName = context.getPackageName(); sessionManager = MediaSessionManager.getSessionManager(context); controllerLegacyCbForBroadcast = new ControllerLegacyCbForBroadcast(); connectionTimeoutHandler = @@ -225,7 +227,11 @@ public boolean onMediaButtonEvent(Intent mediaButtonEvent) { 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(); diff --git a/libraries/test_session_current/src/androidTest/java/androidx/media3/session/MediaSessionKeyEventTest.java b/libraries/test_session_current/src/androidTest/java/androidx/media3/session/MediaSessionKeyEventTest.java index 245f9449728..a4733f64495 100644 --- a/libraries/test_session_current/src/androidTest/java/androidx/media3/session/MediaSessionKeyEventTest.java +++ b/libraries/test_session_current/src/androidTest/java/androidx/media3/session/MediaSessionKeyEventTest.java @@ -237,15 +237,6 @@ public void playPauseKeyEvent_playing_pause() throws Exception { 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));