Merge 372d9e58ab97e29bead2c3796b6389edf8486057 on remote branch

Change-Id: If2f06866c8bdeaf106784f7bd2febcd5f48b1ab3
diff --git a/packages_apps_bluetooth_ext/src/avrcp/Avrcp_ext.java b/packages_apps_bluetooth_ext/src/avrcp/Avrcp_ext.java
index 8308db7..55a989a 100644
--- a/packages_apps_bluetooth_ext/src/avrcp/Avrcp_ext.java
+++ b/packages_apps_bluetooth_ext/src/avrcp/Avrcp_ext.java
@@ -926,7 +926,7 @@
                         }
                     } else
                         mAudioManager.avrcpSupportsAbsoluteVolume(device.getAddress(), true);
-                } else if (mDevice != null && mDevice.equals(device)) {
+                } else if (mDevice != null && Objects.equals(mDevice, device)) {
                     mAudioManager.avrcpSupportsAbsoluteVolume(device.getAddress(),
                         isAbsoluteVolumeSupported(deviceIndex));
                     Log.v(TAG,"update audio manager for abs vol state = "
@@ -1123,17 +1123,19 @@
                 Bundle data = msg.getData();
                 byte[] bdaddr = data.getByteArray("BdAddress");
                 String address = Utils.getAddressStringFromByte(bdaddr);
-                BluetoothDevice device;
+                BluetoothDevice device, activeDevice = null;
                 Log.v(TAG, "event for device address " + address);
                 deviceIndex = getIndexForDevice(mAdapter.getRemoteDevice(address));
                 if (deviceIndex == INVALID_DEVICE_INDEX) {
                     Log.e(TAG,"invalid index for device");
                     break;
                 }
+                activeDevice = mA2dpService.getActiveDevice();
                 byte absVol = (byte) ((byte) msg.arg1 & 0x7f); // discard MSB as it is RFD
                 if (DEBUG) Log.v(TAG, "MSG_NATIVE_REQ_VOLUME_CHANGE addr: " + address);
 
-                if (((!(deviceFeatures[deviceIndex].isActiveDevice)) &&
+                if (((!(activeDevice != null
+                    && Objects.equals(deviceFeatures[deviceIndex].mCurrentDevice, activeDevice))) &&
                     (deviceFeatures[deviceIndex].mInitialRemoteVolume != -1)) ||
                     (!deviceFeatures[deviceIndex].isAbsoluteVolumeSupportingDevice)) {
                         if (deviceFeatures[deviceIndex].isAbsoluteVolumeSupportingDevice) {
@@ -1229,8 +1231,9 @@
                     deviceFeatures[deviceIndex].mInitialRemoteVolume = absVol;
                     //Avoid fluctuation of volume during device added in blacklist
                     // use send setAbsolute volume for blacklisted volume
-                    if(deviceFeatures[deviceIndex].mBlackListVolume != -1 &&
-                       deviceFeatures[deviceIndex].isActiveDevice) {
+                    if (deviceFeatures[deviceIndex].mBlackListVolume != -1 &&
+                       activeDevice != null &&
+                       Objects.equals(activeDevice, deviceFeatures[deviceIndex].mCurrentDevice)) {
                         resetBlackList(address);
                         if (DEBUG) Log.v(TAG, "remote initial volume as audio stream volume : " +
                             deviceFeatures[deviceIndex].mBlackListVolume);
@@ -1242,7 +1245,8 @@
                         deviceFeatures[deviceIndex].mLocalVolume = deviceFeatures[deviceIndex].mBlackListVolume;
                         deviceFeatures[deviceIndex].mBlackListVolume = -1;
                         break;
-                    } else if (deviceFeatures[deviceIndex].isActiveDevice) {
+                    } else if (activeDevice != null &&
+                          Objects.equals(activeDevice, deviceFeatures[deviceIndex].mCurrentDevice)) {
                         /*Avoid send set absolute volume for store volume untill volume registration
                         complete and making synchronization to send only one setAbsolute volume
                         during connection*/
@@ -1283,7 +1287,8 @@
                         break;
                     }
                     /* If the volume has successfully changed */
-                    if (device != null && !deviceFeatures[deviceIndex].isActiveDevice &&
+                    if (device != null && !(activeDevice != null &&
+                           Objects.equals(activeDevice, deviceFeatures[deviceIndex].mCurrentDevice)) &&
                            (msg.arg2 == AVRC_RSP_CHANGED || msg.arg2 == AVRC_RSP_INTERIM)) {
                         Log.d(TAG, "Do not change volume from an inactive device");
                         break;
@@ -1343,10 +1348,12 @@
                 if (DEBUG) Log.v(TAG, "MSG_SET_ABSOLUTE_VOLUME");
 
                 int avrcpVolume = convertToAvrcpVolume(msg.arg1);
+                BluetoothDevice activeDevice = null;
                 avrcpVolume = Math.min(AVRCP_MAX_VOL, Math.max(0, avrcpVolume));
                 for (int i = 0; i < maxAvrcpConnections; i++) {
-                    if (deviceFeatures[i].mCurrentDevice != null &&
-                            deviceFeatures[i].isActiveDevice && deviceFeatures[i].isAbsoluteVolumeSupportingDevice) {
+                    if (deviceFeatures[i].mCurrentDevice != null && activeDevice != null &&
+                            Objects.equals(activeDevice, deviceFeatures[deviceIndex].mCurrentDevice) &&
+                            deviceFeatures[i].isAbsoluteVolumeSupportingDevice) {
 
                           deviceIndex = i;
 
@@ -2909,9 +2916,10 @@
 
  public boolean isAbsoluteVolumeSupported() {
         boolean status = false;
+        BluetoothDevice activeDevice = mA2dpService.getActiveDevice();
         for (int i = 0; i < maxAvrcpConnections; i++) {
-            if (deviceFeatures[i].mCurrentDevice != null &&
-                    deviceFeatures[i].isActiveDevice) {
+            if (deviceFeatures[i].mCurrentDevice != null && activeDevice != null &&
+                    Objects.equals(deviceFeatures[i].mCurrentDevice, activeDevice)) {
                 if ((deviceFeatures[i].mFeatures &
                         BTRC_FEAT_ABSOLUTE_VOLUME) != 0) {
                     status = true;
diff --git a/system_bt_ext/bta/tws_plus/ag/bta_ag_twsp_dev.cc b/system_bt_ext/bta/tws_plus/ag/bta_ag_twsp_dev.cc
index 7b3e8e2..8000abb 100644
--- a/system_bt_ext/bta/tws_plus/ag/bta_ag_twsp_dev.cc
+++ b/system_bt_ext/bta/tws_plus/ag/bta_ag_twsp_dev.cc
@@ -673,7 +673,10 @@
            set_twsp_mic_quality(idx, int_arg);
         } break;
         case BTA_AG_TWSP_AT_QES_EVT: {
-           set_twsp_state(idx, int_arg);
+           if (set_twsp_state(idx, int_arg)) {
+                APPL_TRACE_DEBUG("%s: Update twsp state to BTIF", __func__);
+               (*bta_ag_cb.p_cback)((tBTA_AG_EVT)BTA_AG_TWSP_STATE_UPDATE, (tBTA_AG *)val);
+           }
         } break;
         case BTA_AG_TWSP_AT_QER_EVT: {
             set_twsp_role(idx, int_arg);
diff --git a/system_bt_ext/bta/tws_plus/ag/bta_ag_twsp_sco.cc b/system_bt_ext/bta/tws_plus/ag/bta_ag_twsp_sco.cc
index 2cfcdc2..5d579ed 100644
--- a/system_bt_ext/bta/tws_plus/ag/bta_ag_twsp_sco.cc
+++ b/system_bt_ext/bta/tws_plus/ag/bta_ag_twsp_sco.cc
@@ -307,7 +307,8 @@
                 /*SCO is up!*/
                 p_sco->state = BTA_AG_SCO_OPEN_ST;
                 other_scb = get_other_twsp_scb((p_scb->peer_addr));
-                if (other_scb && twsp_sco_active(other_scb) == false) {
+                if (other_scb && twsp_sco_active(other_scb) == false &&
+                        get_twsp_state(other_scb) == TWSPLUS_EB_STATE_INEAR) {
                     APPL_TRACE_WARNING("Calling SCO open");
                     dispatch_event_primary_peer_device(p_scb, BTA_AG_SCO_OPEN_E);
                 }
diff --git a/system_bt_ext/btif/include/btif_twsp_hf.h b/system_bt_ext/btif/include/btif_twsp_hf.h
index 2c9c7c4..cd7da30 100644
--- a/system_bt_ext/btif/include/btif_twsp_hf.h
+++ b/system_bt_ext/btif/include/btif_twsp_hf.h
@@ -32,9 +32,11 @@
  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#define BTA_AG_TWSP_STATE_UPDATE 252
 #define BTA_AG_TWSP_BATTERY_UPDATE 255
 
 #include "bta_ag_api.h"
+#include "bta_ag_twsp_dev.h"
 
 void btif_handle_vendor_hf_events(uint16_t event, tBTA_AG* data,
                                   RawAddress *bd_addr);