Store volume for devices not support absolute volume

Reigister broadcast for AudioManager.VOLUME_CHANGED_ACTION to monitor
STREAM_MUSIC volume value change.
Store volume value for non absolute volume device whenever
STREAM_MUSIC volume value changed.

Bug: 146561367
Bug: 141746655
Test: manual
Change-Id: I317f5fa31b243eb5f8203d00ec4585c931a066fc
diff --git a/android/app/src/com/android/bluetooth/avrcp/AvrcpTargetService.java b/android/app/src/com/android/bluetooth/avrcp/AvrcpTargetService.java
index 5bcdf65..0edae76 100644
--- a/android/app/src/com/android/bluetooth/avrcp/AvrcpTargetService.java
+++ b/android/app/src/com/android/bluetooth/avrcp/AvrcpTargetService.java
@@ -114,6 +114,17 @@
                         Log.d(TAG, "request to disconnect device " + device);
                     }
                 }
+            } else if (action.equals(AudioManager.VOLUME_CHANGED_ACTION)) {
+                int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1);
+                if (streamType == AudioManager.STREAM_MUSIC) {
+                    int volume = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, 0);
+                    BluetoothDevice activeDevice = mFactory.getA2dpService().getActiveDevice();
+                    if (activeDevice != null
+                            && !mVolumeManager.getAbsoluteVolumeSupported(activeDevice)) {
+                        Log.d(TAG, "stream volume change to " + volume + " " + activeDevice);
+                        mVolumeManager.storeVolumeForDevice(activeDevice, volume);
+                    }
+                }
             }
         }
     }
@@ -185,6 +196,7 @@
         IntentFilter filter = new IntentFilter();
         filter.addAction(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED);
         filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
+        filter.addAction(AudioManager.VOLUME_CHANGED_ACTION);
         registerReceiver(mReceiver, filter);
 
         // Only allow the service to be used once it is initialized
diff --git a/android/app/src/com/android/bluetooth/avrcp/AvrcpVolumeManager.java b/android/app/src/com/android/bluetooth/avrcp/AvrcpVolumeManager.java
index c1369f8..e48d428 100644
--- a/android/app/src/com/android/bluetooth/avrcp/AvrcpVolumeManager.java
+++ b/android/app/src/com/android/bluetooth/avrcp/AvrcpVolumeManager.java
@@ -115,12 +115,11 @@
         volumeMapEditor.apply();
     }
 
-    synchronized void storeVolumeForDevice(@NonNull BluetoothDevice device) {
+    synchronized void storeVolumeForDevice(@NonNull BluetoothDevice device, int storeVolume) {
         if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
             return;
         }
         SharedPreferences.Editor pref = getVolumeMap().edit();
-        int storeVolume =  mAudioManager.getStreamVolume(STREAM_MUSIC);
         Log.i(TAG, "storeVolume: Storing stream volume level for device " + device
                 + " : " + storeVolume);
         mVolumeMap.put(device, storeVolume);
@@ -130,6 +129,11 @@
         pref.apply();
     }
 
+    synchronized void storeVolumeForDevice(@NonNull BluetoothDevice device) {
+        int storeVolume =  mAudioManager.getStreamVolume(STREAM_MUSIC);
+        storeVolumeForDevice(device, storeVolume);
+    }
+
     synchronized void removeStoredVolumeForDevice(@NonNull BluetoothDevice device) {
         if (device.getBondState() != BluetoothDevice.BOND_NONE) {
             return;
@@ -183,6 +187,17 @@
         storeVolumeForDevice(device);
     }
 
+    /**
+     * True if remote device supported Absolute volume, false if remote device is not supported or
+     * not connected.
+     */
+    boolean getAbsoluteVolumeSupported(BluetoothDevice device) {
+        if (mDeviceMap.containsKey(device)) {
+            return mDeviceMap.get(device);
+        }
+        return false;
+    }
+
     @Override
     public synchronized void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
         if (mCurrentDevice == null) {