Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2a8bb725 authored by Joseph Pirozzo's avatar Joseph Pirozzo
Browse files

A2DP sink audio focus

Update AudioFocusRequest to receive notification on
AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK.

Bug: 38389934
Test: stream music over Bluetooth and play Nav guidance, observe music
is ducked.

Change-Id: I1b4d33fd63cc6033c06bf24ac0f9a41aeeaf292d
Merged-In: Id0403116019e46191508ed9a4f6f627467db1726
(cherry picked from commit 7d06215f)
parent 0cff9baf
Loading
Loading
Loading
Loading
+17 −2
Original line number Original line Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.bluetooth.a2dpsink;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioAttributes;
import android.media.AudioFocusRequest;
import android.media.AudioManager;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
import android.media.AudioManager.OnAudioFocusChangeListener;
import android.os.Handler;
import android.os.Handler;
@@ -212,8 +213,22 @@ public class A2dpSinkStreamHandler extends Handler {
     * Utility functions.
     * Utility functions.
     */
     */
    private int requestAudioFocus() {
    private int requestAudioFocus() {
        int focusRequestStatus = mAudioManager.requestAudioFocus(
        // Bluetooth A2DP may carry Music, Audio Books, Navigation, or other sounds so mark content
                mAudioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        // type unknown.
        AudioAttributes streamAttributes =
                new AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_MEDIA)
                        .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
                        .build();
        // Bluetooth ducking is handled at the native layer so tell the Audio Manger to notify the
        // focus change listener via .setWillPauseWhenDucked().
        AudioFocusRequest focusRequest =
                new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
                        .setAudioAttributes(streamAttributes)
                        .setWillPauseWhenDucked(true)
                        .setOnAudioFocusChangeListener(mAudioFocusListener, this)
                        .build();
        int focusRequestStatus = mAudioManager.requestAudioFocus(focusRequest);
        // If the request is granted begin streaming immediately and schedule an upgrade.
        // If the request is granted begin streaming immediately and schedule an upgrade.
        if (focusRequestStatus == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        if (focusRequestStatus == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            startAvrcpUpdates();
            startAvrcpUpdates();