VolumeControl: Fix volume for the activated device

For the newly activated device we should be passing volume index
rather than Le Audio volume level.

Bug: 243334827
Bug: 241501978
Tag: #feature
Test: tested manually
Change-Id: I8392120eef653f67b5ca91452847943b4cebb644
Merged-In: I8392120eef653f67b5ca91452847943b4cebb644
(cherry picked from commit f0e34592935ec0b09bc9f4f2127a7c0787524f7a)
diff --git a/android/app/src/com/android/bluetooth/le_audio/LeAudioService.java b/android/app/src/com/android/bluetooth/le_audio/LeAudioService.java
index b9a6fe6..3b4de0d 100644
--- a/android/app/src/com/android/bluetooth/le_audio/LeAudioService.java
+++ b/android/app/src/com/android/bluetooth/le_audio/LeAudioService.java
@@ -415,7 +415,7 @@
         sLeAudioService = instance;
     }
 
-    private int getGroupVolume(int groupId) {
+    private int getAudioDeviceGroupVolume(int groupId) {
         if (mVolumeControlService == null) {
             mVolumeControlService = mServiceFactory.getVolumeControlService();
             if (mVolumeControlService == null) {
@@ -424,7 +424,7 @@
             }
         }
 
-        return mVolumeControlService.getGroupVolume(groupId);
+        return mVolumeControlService.getAudioDeviceGroupVolume(groupId);
     }
 
     public boolean connect(BluetoothDevice device) {
@@ -926,7 +926,7 @@
             }
             int volume = IBluetoothVolumeControl.VOLUME_CONTROL_UNKNOWN_VOLUME;
             if (mActiveAudioOutDevice != null) {
-                volume = getGroupVolume(groupId);
+                volume = getAudioDeviceGroupVolume(groupId);
             }
 
             mAudioManager.handleBluetoothActiveDeviceChanged(mActiveAudioOutDevice,
diff --git a/android/app/src/com/android/bluetooth/vc/VolumeControlService.java b/android/app/src/com/android/bluetooth/vc/VolumeControlService.java
index 0b0fa26..e5cd625 100644
--- a/android/app/src/com/android/bluetooth/vc/VolumeControlService.java
+++ b/android/app/src/com/android/bluetooth/vc/VolumeControlService.java
@@ -586,6 +586,11 @@
      * {@hide}
      */
     public void setGroupVolume(int groupId, int volume) {
+        if (volume < 0) {
+            Log.w(TAG, "Tried to set invalid volume " + volume + ". Ignored.");
+            return;
+        }
+
         mGroupVolumeCache.put(groupId, volume);
         mVolumeControlNativeInterface.setGroupVolume(groupId, volume);
     }
@@ -683,6 +688,15 @@
         }
     }
 
+    /**
+     * {@hide}
+     */
+    public int getAudioDeviceGroupVolume(int groupId) {
+        int volume = getGroupVolume(groupId);
+        if (volume == IBluetoothVolumeControl.VOLUME_CONTROL_UNKNOWN_VOLUME) return -1;
+        return getDeviceVolume(getBluetoothContextualVolumeStream(), volume);
+    }
+
     int getDeviceVolume(int streamType, int bleVolume) {
         int deviceMaxVolume = mAudioManager.getStreamMaxVolume(streamType);
 
diff --git a/android/app/tests/unit/src/com/android/bluetooth/le_audio/LeAudioServiceTest.java b/android/app/tests/unit/src/com/android/bluetooth/le_audio/LeAudioServiceTest.java
index b37ae31..becde7e 100644
--- a/android/app/tests/unit/src/com/android/bluetooth/le_audio/LeAudioServiceTest.java
+++ b/android/app/tests/unit/src/com/android/bluetooth/le_audio/LeAudioServiceTest.java
@@ -1428,7 +1428,7 @@
         //Add location support.
         injectAudioConfChanged(groupId, availableContexts);
 
-        doReturn(-1).when(mVolumeControlService).getGroupVolume(groupId);
+        doReturn(-1).when(mVolumeControlService).getAudioDeviceGroupVolume(groupId);
         //Set group and device as active.
         injectGroupStatusChange(groupId, LeAudioStackEvent.GROUP_STATUS_ACTIVE);
 
@@ -1445,7 +1445,7 @@
         verify(mAudioManager, times(1)).handleBluetoothActiveDeviceChanged(eq(null), any(),
                         any(BluetoothProfileConnectionInfo.class));
 
-        doReturn(100).when(mVolumeControlService).getGroupVolume(groupId);
+        doReturn(100).when(mVolumeControlService).getAudioDeviceGroupVolume(groupId);
 
         //Set back to active and check if last volume is restored.
         injectGroupStatusChange(groupId, LeAudioStackEvent.GROUP_STATUS_ACTIVE);