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

Commit 096f459d authored by Ajay Panicker's avatar Ajay Panicker Committed by android-build-merger
Browse files

Merge "Inform AudioManager if absolute volume is supported after connection" into pi-dev

am: f640c98d

Change-Id: Ie24339420b31a2e8ed8acbbb32e0d76f3c2f29bb
parents 82ccdc93 f640c98d
Loading
Loading
Loading
Loading
+39 −17
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioDeviceCallback;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;
import android.util.Log;

@@ -29,7 +31,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

class AvrcpVolumeManager {
class AvrcpVolumeManager extends AudioDeviceCallback {
    public static final String TAG = "NewAvrcpVolumeManager";
    public static final boolean DEBUG = true;

@@ -90,6 +92,8 @@ class AvrcpVolumeManager {
        mNativeInterface = nativeInterface;
        sDeviceMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

        mAudioManager.registerAudioDeviceCallback(this, null);

        // Load the stored volume preferences into a hash map since shared preferences are slow
        // to poll and update. If the device has been unbonded since last start remove it from
        // the map.
@@ -133,6 +137,40 @@ class AvrcpVolumeManager {
        return mVolumeMap.get(device);
    }

    @Override
    public synchronized void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
        if (mCurrentDevice == null) {
            d("onAudioDevicesAdded: Not expecting device changed");
            return;
        }

        boolean foundDevice = false;
        d("onAudioDevicesAdded: size: " + addedDevices.length);
        for (int i = 0; i < addedDevices.length; i++) {
            d("onAudioDevicesAdded: address=" + addedDevices[i].getAddress());
            if (addedDevices[i].getType() == AudioDeviceInfo.TYPE_BLUETOOTH_A2DP
                    && Objects.equals(addedDevices[i].getAddress(), mCurrentDevice.getAddress())) {
                foundDevice = true;
                break;
            }
        }

        if (!foundDevice) {
            d("Didn't find deferred device in list: device=" + mCurrentDevice);
            return;
        }

        // A2DP can sometimes connect and set a device to active before AVRCP has determined if the
        // device supports absolute volume. Defer switching the device until AVRCP returns the
        // info.
        if (!mDeviceMap.containsKey(mCurrentDevice)) {
            Log.w(TAG, "volumeDeviceSwitched: Device isn't connected: " + mCurrentDevice);
            return;
        }

        switchVolumeDevice(mCurrentDevice);
    }

    synchronized void deviceConnected(@NonNull BluetoothDevice device, boolean absoluteVolume) {
        d("deviceConnected: device=" + device + " absoluteVolume=" + absoluteVolume);

@@ -154,22 +192,6 @@ class AvrcpVolumeManager {

        // Wait until AudioManager informs us that the new device is connected
        mCurrentDevice = device;

        // If device is null, that means that there is no active Bluetooth device
        if (device == null) {
            mCurrentDevice = null;
            return;
        }

        // If the device is connected then switch the device which informs audio manager that
        // absolute volume is supported and set the volume for the device. Otherwise disable
        // absolute volume until the new device connects to prevent sending unattenuated audio.
        if (mDeviceMap.containsKey(device)) {
            switchVolumeDevice(device);
        } else {
            d("volumeDeviceSwitched: Set Absolute Volume support to false until device connects.");
            mAudioManager.avrcpSupportsAbsoluteVolume(device.getAddress(), false);
        }
    }

    void deviceDisconnected(@NonNull BluetoothDevice device) {