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

Commit 1e1e2f42 authored by Jakub Tyszkowski's avatar Jakub Tyszkowski
Browse files

VolumeControl: Correct device volume only if connected

Native will ignore requests to devices that were not yet reported as
connected so there is no reason to call to native.

Bug: 248475583
Tag: #feature
Test: atest BluetoothInstrumentationTests
Change-Id: Icf835d9112b71737dfdccd20ee97a50305fd4ba8
parent 90b25176
Loading
Loading
Loading
Loading
+26 −3
Original line number Original line Diff line number Diff line
@@ -654,9 +654,21 @@ public class VolumeControlService extends ProfileService {
        Integer groupVolume = mGroupVolumeCache.getOrDefault(groupId,
        Integer groupVolume = mGroupVolumeCache.getOrDefault(groupId,
                IBluetoothVolumeControl.VOLUME_CONTROL_UNKNOWN_VOLUME);
                IBluetoothVolumeControl.VOLUME_CONTROL_UNKNOWN_VOLUME);
        if (groupVolume != IBluetoothVolumeControl.VOLUME_CONTROL_UNKNOWN_VOLUME) {
        if (groupVolume != IBluetoothVolumeControl.VOLUME_CONTROL_UNKNOWN_VOLUME) {
            // Correct the volume level only if device was already reported as connected.
            boolean can_change_volume = false;
            synchronized (mStateMachines) {
                VolumeControlStateMachine sm = mStateMachines.get(device);
                if (sm != null) {
                    can_change_volume =
                            (sm.getConnectionState() == BluetoothProfile.STATE_CONNECTED);
                }
            }
            if (can_change_volume) {
                Log.i(TAG, "Setting value:" + groupVolume + " to " + device);
                mVolumeControlNativeInterface.setVolume(device, groupVolume);
                mVolumeControlNativeInterface.setVolume(device, groupVolume);
            }
            }
        }
        }
    }


    void handleVolumeControlChanged(BluetoothDevice device, int groupId,
    void handleVolumeControlChanged(BluetoothDevice device, int groupId,
                                    int volume, boolean mute, boolean isAutonomous) {
                                    int volume, boolean mute, boolean isAutonomous) {
@@ -696,8 +708,19 @@ public class VolumeControlService extends ProfileService {


            if (device != null && groupVolume
            if (device != null && groupVolume
                            != IBluetoothVolumeControl.VOLUME_CONTROL_UNKNOWN_VOLUME) {
                            != IBluetoothVolumeControl.VOLUME_CONTROL_UNKNOWN_VOLUME) {
                // Correct the volume level only if device was already reported as connected.
                boolean can_change_volume = false;
                synchronized (mStateMachines) {
                    VolumeControlStateMachine sm = mStateMachines.get(device);
                    if (sm != null) {
                        can_change_volume =
                                (sm.getConnectionState() == BluetoothProfile.STATE_CONNECTED);
                    }
                }
                if (can_change_volume) {
                    Log.i(TAG, "Setting value:" + groupVolume + " to " + device);
                    Log.i(TAG, "Setting value:" + groupVolume + " to " + device);
                    mVolumeControlNativeInterface.setVolume(device, groupVolume);
                    mVolumeControlNativeInterface.setVolume(device, groupVolume);
                }
            } else {
            } else {
                Log.e(TAG, "Volume changed did not succeed. Volume: " + volume
                Log.e(TAG, "Volume changed did not succeed. Volume: " + volume
                                + " expected volume: " + groupVolume);
                                + " expected volume: " + groupVolume);