FR55512:Support for multiple A2DP Streams
    Change the active device management policy when multi-A2DP streams enabled;
    Disable the Absolute Volume feature dynamically when multi-A2DP streams ongoing.

CRs-Fixed: 2541910

Change-Id: I2392d232aba24fa75aa7e90692224ec07de26456
Signed-off-by: Cheng Jiang <chejiang@codeaurora.org>
diff --git a/src/com/android/bluetooth/a2dp/A2dpService.java b/src/com/android/bluetooth/a2dp/A2dpService.java
old mode 100644
new mode 100755
index f0ede44..a2998fe
--- a/src/com/android/bluetooth/a2dp/A2dpService.java
+++ b/src/com/android/bluetooth/a2dp/A2dpService.java
@@ -117,6 +117,7 @@
     private static final long Aptx_BLEScanDisable = 0x2000;
     private static final int SET_EBMONO_CFG = 1;
     private static final int MonoCfg_Timeout = 5000;
+    private static boolean a2dpMulticast = false;
 
     private Handler mHandler = new Handler() {
         @Override
@@ -236,6 +237,8 @@
         // Step 10: Clear active device
         setActiveDevice(null);
 
+        // Step 11: get the a2dp multicast flag
+        a2dpMulticast = SystemProperties.getBoolean("persist.vendor.service.bt.a2dp_multicast_enable", false);
         return true;
     }
 
@@ -771,16 +774,18 @@
                                                  mAudioManager.FLAG_BLUETOOTH_ABS_VOLUME);
                    wasMuted = true;
                 }
-                if (mDummyDevice != null &&
-                    mAdapterService.isTwsPlusDevice(previousActiveDevice)) {
-                    mAudioManager.setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent(
-                            mDummyDevice, BluetoothProfile.STATE_DISCONNECTED,
-                            BluetoothProfile.A2DP, true, -1);
-                    mDummyDevice = null;
-                } else  {
-                    mAudioManager.setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent(
-                            previousActiveDevice, BluetoothProfile.STATE_DISCONNECTED,
-                            BluetoothProfile.A2DP, true, -1);
+                if (!a2dpMulticast) {
+                    if (mDummyDevice != null &&
+                        mAdapterService.isTwsPlusDevice(previousActiveDevice)) {
+                        mAudioManager.setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent(
+                                mDummyDevice, BluetoothProfile.STATE_DISCONNECTED,
+                                BluetoothProfile.A2DP, true, -1);
+                        mDummyDevice = null;
+                    } else  {
+                        mAudioManager.setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent(
+                                previousActiveDevice, BluetoothProfile.STATE_DISCONNECTED,
+                                BluetoothProfile.A2DP, true, -1);
+                    }
                 }
             }
             // Check if ther is any delay set on audioservice for previous
@@ -826,8 +831,11 @@
                                           AudioManager.ADJUST_UNMUTE,
                                           mAudioManager.FLAG_BLUETOOTH_ABS_VOLUME);
             }
-            if(mAvrcp_ext != null)
-                mAvrcp_ext.setAbsVolumeFlag(device);
+            // Don't update the absVolume flags when disconnect one device in multicast mode
+            if (!a2dpMulticast || previousActiveDevice == null) {
+                if(mAvrcp_ext != null)
+                    mAvrcp_ext.setAbsVolumeFlag(device);
+            }
         }
         return true;
     }
@@ -913,6 +921,18 @@
         }
     }
 
+    public void storeDeviceAudioVolume(BluetoothDevice device) {
+        if (device != null)
+        {
+            if (AvrcpTargetService.get() != null) {
+                AvrcpTargetService.get().storeVolumeForDevice(device);
+            } else if (mAvrcp_ext != null) {
+                //store volume in multi-a2dp for the device doesn't set as active
+                mAvrcp_ext.storeVolumeForDevice(device);
+            }
+        }
+    }
+
     public void resetAvrcpBlacklist(BluetoothDevice device) {
         synchronized(mBtAvrcpLock) {
             if (mAvrcp_ext != null) {
diff --git a/src/com/android/bluetooth/btservice/ActiveDeviceManager.java b/src/com/android/bluetooth/btservice/ActiveDeviceManager.java
old mode 100644
new mode 100755
index 9e3e62b..735dac4
--- a/src/com/android/bluetooth/btservice/ActiveDeviceManager.java
+++ b/src/com/android/bluetooth/btservice/ActiveDeviceManager.java
@@ -33,6 +33,7 @@
 import android.os.HandlerThread;
 import android.os.Looper;
 import android.os.Message;
+import android.os.SystemProperties;
 import android.support.annotation.VisibleForTesting;
 import android.util.Log;
 
@@ -124,6 +125,7 @@
     private BluetoothDevice mA2dpActiveDevice = null;
     private BluetoothDevice mHfpActiveDevice = null;
     private BluetoothDevice mHearingAidActiveDevice = null;
+    private static boolean a2dpMulticast = false;
 
     // Broadcast receiver for all changes
     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@@ -214,8 +216,19 @@
                         mA2dpConnectedDevices.add(device);
                         if (mHearingAidActiveDevice == null) {
                             // New connected device: select it as active
-                            setA2dpActiveDevice(device);
-                            break;
+                            if (!a2dpMulticast) {
+                                setA2dpActiveDevice(device);
+                            }
+                            else {
+                                if (mA2dpActiveDevice == null) {
+                                    setA2dpActiveDevice(device);
+                                }
+                                else {
+                                    // store the volume for the new added device
+                                    final A2dpService a2dpService = mFactory.getA2dpService();
+                                    a2dpService.storeDeviceAudioVolume(device);
+                                }
+                            }
                         }
                         break;
                     }
@@ -240,6 +253,16 @@
                                     Log.w(TAG, "Set a2dp active device failed");
                                     setA2dpActiveDevice(null);
                                 }
+                            }
+                            else if (a2dpMulticast && !mA2dpConnectedDevices.isEmpty() &&
+                                (a2dpService != null) &&
+                                (a2dpService.getConnectionState(mA2dpConnectedDevices.get(0)) ==
+                                BluetoothProfile.STATE_CONNECTED)) {
+                                Log.d(TAG, "a2dp Multicast calling set a2dp Active dev: " + mA2dpConnectedDevices.get(0));
+                                if (!setA2dpActiveDevice(mA2dpConnectedDevices.get(0))) {
+                                    Log.w(TAG, "Set a2dp active device failed");
+                                    setA2dpActiveDevice(null);
+                                }
                             } else
                                 setA2dpActiveDevice(null);
                         }
@@ -284,7 +307,7 @@
                             break;      // The device is already connected
                         }
                         mHfpConnectedDevices.add(device);
-                        if (mHearingAidActiveDevice == null) {
+                        if ((!a2dpMulticast || mHfpActiveDevice == null) && mHearingAidActiveDevice == null) {
                             // New connected device: select it as active
                             setHfpActiveDevice(device);
                             break;
@@ -312,6 +335,17 @@
                                    Log.w(TAG, "Set hfp active device failed");
                                    setHfpActiveDevice(null);
                                }
+                            }
+                            else if (a2dpMulticast && !mHfpConnectedDevices.isEmpty() &&
+                                (hfpService != null) &&
+                                (hfpService.getConnectionState(mHfpConnectedDevices.get(0)) ==
+                                 BluetoothProfile.STATE_CONNECTED)) {
+
+                                Log.d(TAG, "calling set HFP Active dev: " + mHfpConnectedDevices.get(0));
+                                if (!setHfpActiveDevice(mHfpConnectedDevices.get(0))) {
+                                    Log.w(TAG, "Set hfp active device failed");
+                                    setHfpActiveDevice(null);
+                                }
                             } else {
                                setHfpActiveDevice(null);
                             }
@@ -422,6 +456,7 @@
         mAdapterService.registerReceiver(mReceiver, filter);
 
         mAudioManager.registerAudioDeviceCallback(mAudioManagerAudioDeviceCallback, mHandler);
+        a2dpMulticast = SystemProperties.getBoolean("persist.vendor.service.bt.a2dp_multicast_enable", false);
     }
 
     void cleanup() {