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

Commit b735fd90 authored by Ajay Panicker's avatar Ajay Panicker Committed by Myles Watson
Browse files

Set active volume device to null if there is no new device

Change broadcastActiveDevice to also set the global active device
variable and move the AVRCP volume memory code to that function in order
to simplify the code.

Fixes: 110162585
Test: Forget device while device is active
Change-Id: If8a66b51a762c5edc5dd5133636f91454c9cab65
parent 68f6714e
Loading
Loading
Loading
Loading
+21 −15
Original line number Diff line number Diff line
@@ -439,11 +439,9 @@ public class A2dpService extends ProfileService {
    private void removeActiveDevice(boolean forceStopPlayingAudio) {
        BluetoothDevice previousActiveDevice = mActiveDevice;
        synchronized (mStateMachines) {
            // Clear the active device
            mActiveDevice = null;
            // This needs to happen before we inform the audio manager that the device
            // disconnected. Please see comment in broadcastActiveDevice() for why.
            broadcastActiveDevice(null);
            // disconnected. Please see comment in updateAndBroadcastActiveDevice() for why.
            updateAndBroadcastActiveDevice(null);

            if (previousActiveDevice == null) {
                return;
@@ -483,10 +481,6 @@ public class A2dpService extends ProfileService {
                Log.d(TAG, "setActiveDevice(" + device + "): previous is " + previousActiveDevice);
            }

            if (previousActiveDevice != null && AvrcpTargetService.get() != null) {
                AvrcpTargetService.get().storeVolumeForDevice(previousActiveDevice);
            }

            if (device == null) {
                // Remove active device and continue playing audio only if necessary.
                removeActiveDevice(false);
@@ -512,10 +506,9 @@ public class A2dpService extends ProfileService {
            codecStatus = sm.getCodecStatus();

            boolean deviceChanged = !Objects.equals(device, mActiveDevice);
            mActiveDevice = device;
            // This needs to happen before we inform the audio manager that the device
            // disconnected. Please see comment in broadcastActiveDevice() for why.
            broadcastActiveDevice(mActiveDevice);
            // disconnected. Please see comment in updateAndBroadcastActiveDevice() for why.
            updateAndBroadcastActiveDevice(device);
            if (deviceChanged) {
                // Send an intent with the active device codec config
                if (codecStatus != null) {
@@ -538,8 +531,6 @@ public class A2dpService extends ProfileService {

                int rememberedVolume = -1;
                if (AvrcpTargetService.get() != null) {
                    AvrcpTargetService.get().volumeDeviceSwitched(mActiveDevice);

                    rememberedVolume = AvrcpTargetService.get()
                            .getRememberedVolumeForDevice(mActiveDevice);
                }
@@ -839,9 +830,24 @@ public class A2dpService extends ProfileService {
        }
    }

    private void broadcastActiveDevice(BluetoothDevice device) {
    // This needs to run before any of the Audio Manager connection functions since
    // AVRCP needs to be aware that the audio device is changed before the Audio Manager
    // changes the volume of the output devices.
    private void updateAndBroadcastActiveDevice(BluetoothDevice device) {
        if (DBG) {
            Log.d(TAG, "broadcastActiveDevice(" + device + ")");
            Log.d(TAG, "updateAndBroadcastActiveDevice(" + device + ")");
        }

        synchronized (mStateMachines) {
            if (AvrcpTargetService.get() != null) {
                if (mActiveDevice != null) {
                    AvrcpTargetService.get().storeVolumeForDevice(mActiveDevice);
                }

                AvrcpTargetService.get().volumeDeviceSwitched(device);
            }

            mActiveDevice = device;
        }

        Intent intent = new Intent(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED);
+2 −2
Original line number Diff line number Diff line
@@ -115,7 +115,7 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
        volumeMapEditor.apply();
    }

    void storeVolumeForDevice(BluetoothDevice device) {
    synchronized void storeVolumeForDevice(BluetoothDevice device) {
        SharedPreferences.Editor pref = getVolumeMap().edit();
        int storeVolume =  mAudioManager.getStreamVolume(STREAM_MUSIC);
        Log.i(TAG, "storeVolume: Storing stream volume level for device " + device
@@ -128,7 +128,7 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
        pref.apply();
    }

    int getVolume(@NonNull BluetoothDevice device, int defaultValue) {
    synchronized int getVolume(@NonNull BluetoothDevice device, int defaultValue) {
        if (!mVolumeMap.containsKey(device)) {
            Log.w(TAG, "getVolume: Couldn't find volume preference for device: " + device);
            return defaultValue;