AVRCP Controller transient loss while idle
While gaining and losing focus due to transient focus requests ensure
Bluetooth restores to the correct playback state. This corrects issues
that could arise during non media sonification durring a transient
focus loss.
Bug: 136092891
Test: atest com.android.bluetooth.a2dpsink.A2dpSinkStreamHandlerTest
Change-Id: I58997b3b8b96da309ff4c31a2b3039dccf9b3578
(cherry picked from commit 32d3d5acac7eabd2bd00be46ea9e399a8b8db10b)
Merged-In: I58997b3b8b96da309ff4c31a2b3039dccf9b3578
Change-Id: Ibefa524f14e2c0792278ab76161f7a8bcbc4fb9b
diff --git a/src/com/android/bluetooth/a2dpsink/A2dpSinkStreamHandler.java b/src/com/android/bluetooth/a2dpsink/A2dpSinkStreamHandler.java
index c24eca9..fa87284 100644
--- a/src/com/android/bluetooth/a2dpsink/A2dpSinkStreamHandler.java
+++ b/src/com/android/bluetooth/a2dpsink/A2dpSinkStreamHandler.java
@@ -26,6 +26,7 @@
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
+import android.support.v4.media.session.PlaybackStateCompat;
import android.util.Log;
import com.android.bluetooth.R;
@@ -225,7 +226,8 @@
break;
case DELAYED_PAUSE:
- if (mStreamAvailable && !inCallFromStreamingDevice()) {
+ if (BluetoothMediaBrowserService.getPlaybackState()
+ == PlaybackStateCompat.STATE_PLAYING && !inCallFromStreamingDevice()) {
sendAvrcpPause();
mSentPause = true;
mStreamAvailable = false;
diff --git a/src/com/android/bluetooth/avrcpcontroller/BluetoothMediaBrowserService.java b/src/com/android/bluetooth/avrcpcontroller/BluetoothMediaBrowserService.java
index 774f953..b8a3337 100644
--- a/src/com/android/bluetooth/avrcpcontroller/BluetoothMediaBrowserService.java
+++ b/src/com/android/bluetooth/avrcpcontroller/BluetoothMediaBrowserService.java
@@ -185,6 +185,20 @@
}
/**
+ * Get playback state
+ */
+ public static synchronized int getPlaybackState() {
+ if (sBluetoothMediaBrowserService != null) {
+ PlaybackStateCompat currentPlaybackState =
+ sBluetoothMediaBrowserService.mSession.getController().getPlaybackState();
+ if (currentPlaybackState != null) {
+ return currentPlaybackState.getState();
+ }
+ }
+ return PlaybackStateCompat.STATE_ERROR;
+ }
+
+ /**
* Get object for controlling playback
*/
public static synchronized MediaControllerCompat.TransportControls getTransportControls() {
diff --git a/tests/unit/src/com/android/bluetooth/a2dpsink/A2dpSinkStreamHandlerTest.java b/tests/unit/src/com/android/bluetooth/a2dpsink/A2dpSinkStreamHandlerTest.java
index 53e8419..c759a8a 100644
--- a/tests/unit/src/com/android/bluetooth/a2dpsink/A2dpSinkStreamHandlerTest.java
+++ b/tests/unit/src/com/android/bluetooth/a2dpsink/A2dpSinkStreamHandlerTest.java
@@ -195,6 +195,24 @@
}
@Test
+ public void testFocusGainTransient() {
+ // Focus was lost then regained.
+ testSnkPlay();
+ mStreamHandler.handleMessage(
+ mStreamHandler.obtainMessage(A2dpSinkStreamHandler.AUDIO_FOCUS_CHANGE,
+ AudioManager.AUDIOFOCUS_LOSS_TRANSIENT));
+ mStreamHandler.handleMessage(
+ mStreamHandler.obtainMessage(A2dpSinkStreamHandler.DELAYED_PAUSE));
+ mStreamHandler.handleMessage(
+ mStreamHandler.obtainMessage(A2dpSinkStreamHandler.AUDIO_FOCUS_CHANGE,
+ AudioManager.AUDIOFOCUS_GAIN));
+ verify(mMockAudioManager, times(0)).abandonAudioFocus(any());
+ verify(mMockA2dpSink, times(0)).informAudioFocusStateNative(0);
+ verify(mMockA2dpSink, times(1)).informAudioTrackGainNative(0);
+ verify(mMockA2dpSink, times(2)).informAudioTrackGainNative(1.0f);
+ }
+
+ @Test
public void testFocusLost() {
// Focus was lost permanently, expect streaming to stop.
testSnkPlay();