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

Commit e051ae93 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
Merged-In: Icf835d9112b71737dfdccd20ee97a50305fd4ba8
(cherry picked from commit 1e1e2f42)
parent b9969310
Loading
Loading
Loading
Loading
+26 −3
Original line number Diff line number Diff line
@@ -654,9 +654,21 @@ public class VolumeControlService extends ProfileService {
        Integer groupVolume = mGroupVolumeCache.getOrDefault(groupId,
                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);
            }
        }
    }

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

            if (device != null && 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);
                }
            } else {
                Log.e(TAG, "Volume changed did not succeed. Volume: " + volume
                                + " expected volume: " + groupVolume);