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

Commit 9d9a63aa authored by Michal Belusiak (xWF)'s avatar Michal Belusiak (xWF) Committed by Gerrit Code Review
Browse files

Merge "VCS: Minor fix for initial volume set and refactor" into main

parents 57f1201c 6b667006
Loading
Loading
Loading
Loading
+49 −35
Original line number Diff line number Diff line
@@ -269,15 +269,33 @@ public class VolumeControlService extends ProfileService {
    }

    public List<BluetoothDevice> getConnectedDevices() {
        synchronized (mStateMachines) {
        List<BluetoothDevice> devices = new ArrayList<>();
        synchronized (mStateMachines) {
            for (VolumeControlStateMachine sm : mStateMachines.values()) {
                if (sm.isConnected()) {
                    devices.add(sm.getDevice());
                }
            }
        }
        return devices;
    }

    private List<BluetoothDevice> getConnectedDevices(int groupId) {
        List<BluetoothDevice> devices = new ArrayList<>();
        LeAudioService leAudioService = mFactory.getLeAudioService();
        if (leAudioService == null) {
            Log.e(TAG, "leAudioService not available");
            return devices;
        }
        synchronized (mStateMachines) {
            for (BluetoothDevice dev : leAudioService.getGroupDevices(groupId)) {
                VolumeControlStateMachine sm = mStateMachines.get(dev);
                if (sm != null && sm.isConnected()) {
                    devices.add(sm.getDevice());
                }
            }
        }
        return devices;
    }

    /**
@@ -460,13 +478,13 @@ public class VolumeControlService extends ProfileService {
                TAG,
                "setDeviceVolume: " + device + ", volume: " + volume + ", isGroupOp: " + isGroupOp);

        if (isGroupOp) {
            LeAudioService leAudioService = mFactory.getLeAudioService();
            if (leAudioService == null) {
                Log.e(TAG, "leAudioService not available");
                return;
            }

        if (isGroupOp) {
            int groupId = leAudioService.getGroupId(device);
            if (groupId == IBluetoothLeAudio.LE_AUDIO_GROUP_ID_INVALID) {
                Log.e(TAG, "Device not a part of a group");
@@ -482,6 +500,8 @@ public class VolumeControlService extends ProfileService {
    }

    public void setGroupVolume(int groupId, int volume) {
        Log.d(TAG, "setGroupVolume: " + groupId + ", volume: " + volume);

        if (volume < 0) {
            Log.w(TAG, "Tried to set invalid volume " + volume + ". Ignored.");
            return;
@@ -658,22 +678,12 @@ public class VolumeControlService extends ProfileService {
            if (sm == null) {
                return;
            }
            if (sm.getConnectionState() != STATE_CONNECTED) {
            if (!sm.isConnected()) {
                return;
            }
        }

        // 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() == STATE_CONNECTED);
            }
        }

        // If group volume has already changed, the new group member should set it
        if (can_change_volume) {
        Integer groupVolume =
                mGroupVolumeCache.getOrDefault(
                        groupId, IBluetoothVolumeControl.VOLUME_CONTROL_UNKNOWN_VOLUME);
@@ -690,7 +700,6 @@ public class VolumeControlService extends ProfileService {
            mNativeInterface.unmute(device);
        }
    }
    }

    void updateGroupCacheAndAudioSystem(int groupId, int volume, boolean mute, boolean showInUI) {
        Log.d(
@@ -797,7 +806,7 @@ public class VolumeControlService extends ProfileService {
             * Note, to match BR/EDR behavior, don't show volume change in UI here
             */
            if (((flags & VOLUME_FLAGS_PERSISTED_USER_SET_VOLUME_MASK) == 0x01)
                    && (getConnectedDevices().size() == 1)) {
                    && (getConnectedDevices(groupId).size() == 1)) {
                updateGroupCacheAndAudioSystem(groupId, volume, mute, false);
                return;
            }
@@ -815,7 +824,12 @@ public class VolumeControlService extends ProfileService {
            return;
        }

        Log.i(TAG, "handleVolumeControlChanged: " + device + "; volume: " + volume);
        Log.i(
                TAG,
                "handleVolumeControlChanged: "
                        + ("device: " + device)
                        + (", groupId: " + groupId)
                        + (", volume: " + volume));
        if (device == null) {
            // notify group devices volume changed
            LeAudioService leAudioService = mFactory.getLeAudioService();
@@ -854,7 +868,7 @@ public class VolumeControlService extends ProfileService {
                synchronized (mStateMachines) {
                    VolumeControlStateMachine sm = mStateMachines.get(device);
                    if (sm != null) {
                        can_change_volume = (sm.getConnectionState() == STATE_CONNECTED);
                        can_change_volume = sm.isConnected();
                    }
                }

+4 −4
Original line number Diff line number Diff line
@@ -158,7 +158,7 @@ class VolumeControlStateMachine extends StateMachine {
                            processConnectionEvent(event.valueInt1);
                        }
                        default -> {
                            Log.e(TAG, "Disconnected: forwarding stack event: " + event);
                            Log.d(TAG, "Disconnected: forwarding stack event: " + event);
                            mService.handleStackEvent(event);
                        }
                    }
@@ -270,7 +270,7 @@ class VolumeControlStateMachine extends StateMachine {
                            deferMessage(message);
                        }
                        default -> {
                            Log.e(TAG, "Connecting: forwarding stack event: " + event);
                            Log.d(TAG, "Connecting: forwarding stack event: " + event);
                            mService.handleStackEvent(event);
                        }
                    }
@@ -366,7 +366,7 @@ class VolumeControlStateMachine extends StateMachine {
                            processConnectionEvent(event.valueInt1);
                        }
                        default -> {
                            Log.e(TAG, "Disconnecting: forwarding stack event: " + event);
                            Log.d(TAG, "Disconnecting: forwarding stack event: " + event);
                            mService.handleStackEvent(event);
                        }
                    }
@@ -465,7 +465,7 @@ class VolumeControlStateMachine extends StateMachine {
                            processConnectionEvent(event.valueInt1);
                        }
                        default -> {
                            Log.e(TAG, "Connected: forwarding stack event: " + event);
                            Log.d(TAG, "Connected: forwarding stack event: " + event);
                            mService.handleStackEvent(event);
                        }
                    }
+2 −2
Original line number Diff line number Diff line
@@ -589,8 +589,8 @@ public class VolumeControlServiceTest {
        boolean initialAutonomousFlag = true;

        // Both devices are in the same group
        when(mCsipService.getGroupId(mDevice, BluetoothUuid.CAP)).thenReturn(groupId);
        when(mCsipService.getGroupId(mDeviceTwo, BluetoothUuid.CAP)).thenReturn(groupId);
        when(mLeAudioService.getGroupDevices(groupId))
                .thenReturn(Arrays.asList(mDevice, mDeviceTwo));

        generateDeviceAvailableMessageFromNative(mDevice, 1);
        generateConnectionMessageFromNative(mDevice, STATE_CONNECTED, STATE_DISCONNECTED);