Merge 15d1143cd2311b469a25a96318485a23056ebde5 on remote branch

Change-Id: I1128627a56fbdc0f5675a61948e3b8553ce5bfa5
diff --git a/BATestApp/Android.mk b/BATestApp/Android.mk
index df6c8b2..e7ba6d1 100644
--- a/BATestApp/Android.mk
+++ b/BATestApp/Android.mk
@@ -11,7 +11,6 @@
 LOCAL_AIDL_INCLUDES += system/bt/binder
 
 LOCAL_PACKAGE_NAME := BATestApp
-LOCAL_PRIVILEGED_MODULE := true
 LOCAL_CERTIFICATE := platform
 
 LOCAL_MODULE_OWNER := qti
diff --git a/BATestApp/AndroidManifest.xml b/BATestApp/AndroidManifest.xml
index 3ca4c63..736d2ae 100644
--- a/BATestApp/AndroidManifest.xml
+++ b/BATestApp/AndroidManifest.xml
@@ -37,8 +37,6 @@
     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
-    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
-    <uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS" />
     <uses-permission android:name="android.permission.RECORD_AUDIO" />
     <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
 
diff --git a/certification_tools/blegatt_test/gatt_test.cpp b/certification_tools/blegatt_test/gatt_test.cpp
index 3453369..eb06c6d 100644
--- a/certification_tools/blegatt_test/gatt_test.cpp
+++ b/certification_tools/blegatt_test/gatt_test.cpp
@@ -122,6 +122,7 @@
 #define L2CAP_LE_DEFAULT_MPS 230
 #define L2CAP_LE_MAX_CREDIT 65535
 #define L2CAP_LE_DEFAULT_CREDIT 1
+#define LE_CHAR_MAX_LEN_VAL 512
 
 /************************************************************************************
 **  Local type definitions
@@ -278,7 +279,7 @@
 uint8_t long_byte_value[] = {31, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
 uint8_t short_byte_value[] = {31, 2};
 
-int len_long_char = 512;
+int len_long_char = LE_CHAR_MAX_LEN_VAL;
 int len_short_char = 2;
 
 int curr_char_val_len =0;
@@ -1603,6 +1604,7 @@
 void do_smp_encrypt(char *p);
 void do_le_gap_conn_param_update(char *p);
 void do_le_gap_attr_init(char *p);
+void do_le_set_char_len(char *p);
 void do_pairing(char *p);
 void do_l2cap_send_data_cid(char *p);
 static void do_set_localname(char *p);
@@ -1664,6 +1666,7 @@
     { "c_set_idle_timeout", do_le_set_idle_timeout, "bd_addr, time_out(int)", 0 },
     { "c_gap_attr_init", do_le_gap_attr_init, "::", 0 },
     { "c_gap_conn_param_update", do_le_gap_conn_param_update, "::", 0 },
+    { "c_set_char_len", do_le_set_char_len, ":: <Default value: 512>", 0},
 
     { "s_register", do_le_server_register, "::UUID: 1<1111..> 2<12323..> 3<321111..>", 0 },
     { "s_connect", do_le_server_connect, ":: transport, BdAddr<00112233445566>", 0 },
@@ -3586,6 +3589,19 @@
     printf("%s:: GAP connection parameter Update\n", __FUNCTION__);
 
 }
+
+void do_le_set_char_len(char *p)
+{
+    int len;
+    len = get_int(&p, -1); //arg1
+    if (len > LE_CHAR_MAX_LEN_VAL || len < 1) {
+      printf("Invalid length. Enter 1 to 512\n");
+    } else {
+      len_long_char = len;
+      printf("long characteristic length is %d\n", len_long_char);
+    }
+}
+
 void do_le_gap_attr_init(char *p)
 {
     sGapInterface->Gap_AttrInit();
diff --git a/packages_apps_bluetooth_ext/jni/Android.bp b/packages_apps_bluetooth_ext/jni/Android.bp
index 439f7a2..9fa6731 100644
--- a/packages_apps_bluetooth_ext/jni/Android.bp
+++ b/packages_apps_bluetooth_ext/jni/Android.bp
@@ -2,7 +2,6 @@
     name: "libbluetoothqti_jni_default",
     header_libs: ["libbluetooth_headers"],
     include_dirs: [
-        "libnativehelper/include/nativehelper",
         "system/bt/types",
         "packages/apps/Bluetooth/jni",
         "vendor/qcom/opensource/commonsys/bluetooth_ext/vhal/include",
diff --git a/packages_apps_bluetooth_ext/src/avrcp/Avrcp_ext.java b/packages_apps_bluetooth_ext/src/avrcp/Avrcp_ext.java
index 55a989a..923ad45 100644
--- a/packages_apps_bluetooth_ext/src/avrcp/Avrcp_ext.java
+++ b/packages_apps_bluetooth_ext/src/avrcp/Avrcp_ext.java
@@ -1130,12 +1130,13 @@
                     Log.e(TAG,"invalid index for device");
                     break;
                 }
+                device = deviceFeatures[deviceIndex].mCurrentDevice;
                 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 (((!(activeDevice != null
-                    && Objects.equals(deviceFeatures[deviceIndex].mCurrentDevice, activeDevice))) &&
+                if (((!(activeDevice != null && (isTwsPlusPair(activeDevice, device)
+                    || Objects.equals(device, activeDevice)))) &&
                     (deviceFeatures[deviceIndex].mInitialRemoteVolume != -1)) ||
                     (!deviceFeatures[deviceIndex].isAbsoluteVolumeSupportingDevice)) {
                         if (deviceFeatures[deviceIndex].isAbsoluteVolumeSupportingDevice) {
@@ -1246,7 +1247,7 @@
                         deviceFeatures[deviceIndex].mBlackListVolume = -1;
                         break;
                     } else if (activeDevice != null &&
-                          Objects.equals(activeDevice, deviceFeatures[deviceIndex].mCurrentDevice)) {
+                          (Objects.equals(activeDevice, device) || isTwsPlusPair(activeDevice, device))) {
                         /*Avoid send set absolute volume for store volume untill volume registration
                         complete and making synchronization to send only one setAbsolute volume
                         during connection*/
@@ -1280,16 +1281,16 @@
                         Log.d(TAG, "Don't show media UI when slide volume bar");
                         isShowUI = false;
                     }
-                    device = mA2dpService.getActiveDevice();
-                    if (deviceFeatures[deviceIndex].mCurrentDevice.isTwsPlusDevice() &&
-                        device != null && !device.isTwsPlusDevice()) {
+                    if (device.isTwsPlusDevice() &&
+                        activeDevice != null && !activeDevice.isTwsPlusDevice()) {
                         Log.d(TAG,"TWS+ device is not active, ignore volume change type: " + msg.arg2);
                         break;
                     }
                     /* If the volume has successfully changed */
-                    if (device != null && !(activeDevice != null &&
-                           Objects.equals(activeDevice, deviceFeatures[deviceIndex].mCurrentDevice)) &&
-                           (msg.arg2 == AVRC_RSP_CHANGED || msg.arg2 == AVRC_RSP_INTERIM)) {
+                    if (!(activeDevice != null &&
+                        (isTwsPlusPair(activeDevice, device) ||
+                        Objects.equals(activeDevice, device))) &&
+                        (msg.arg2 == AVRC_RSP_CHANGED || msg.arg2 == AVRC_RSP_INTERIM)) {
                         Log.d(TAG, "Do not change volume from an inactive device");
                         break;
                     }
@@ -1348,12 +1349,13 @@
                 if (DEBUG) Log.v(TAG, "MSG_SET_ABSOLUTE_VOLUME");
 
                 int avrcpVolume = convertToAvrcpVolume(msg.arg1);
-                BluetoothDevice activeDevice = null;
+                BluetoothDevice activeDevice = mA2dpService.getActiveDevice();
                 avrcpVolume = Math.min(AVRCP_MAX_VOL, Math.max(0, avrcpVolume));
                 for (int i = 0; i < maxAvrcpConnections; i++) {
                     if (deviceFeatures[i].mCurrentDevice != null && activeDevice != null &&
-                            Objects.equals(activeDevice, deviceFeatures[deviceIndex].mCurrentDevice) &&
-                            deviceFeatures[i].isAbsoluteVolumeSupportingDevice) {
+                        (isTwsPlusPair(activeDevice, deviceFeatures[i].mCurrentDevice) ||
+                         Objects.equals(activeDevice, deviceFeatures[i].mCurrentDevice)) &&
+                         deviceFeatures[i].isAbsoluteVolumeSupportingDevice) {
 
                           deviceIndex = i;
 
@@ -2560,31 +2562,7 @@
                    param = 1;
 
                 long update_interval = 0L;
-                // Split A2dp will be enabled by default
-                boolean isSplitA2dpEnabled = true;
-                AdapterService adapterService = AdapterService.getAdapterService();
-                if (adapterService != null) {
-                    //Todo, Once KS TAG is available, need to remove ReflectionUtils
-                    //isSplitA2dpEnabled= adapterService.isSplitA2dpEnabled();
-                    ReflectionUtils rUtils = new ReflectionUtils();
-                    if (rUtils.isMethodAvailable(adapterService,"isSplitA2dpEnabled", null)) {
-                      Object obj = rUtils.invokeMethod(adapterService,"isSplitA2dpEnabled", null);
-                      if (obj != null) {
-                          isSplitA2dpEnabled = (boolean)obj;
-                      } else {
-                          Log.v(TAG,"Obj is null");
-                      }
-                    } else {
-                      Log.v(TAG,"isSplitA2dpEnabled method is not available");
-                    }
-                    Log.v(TAG,"split enabled: " + isSplitA2dpEnabled);
-                }
-
-                if (isSplitA2dpEnabled) {
-                    update_interval = SystemProperties.getLong("persist.vendor.btstack.avrcp.pos_time", 3000L);
-                } else {
-                    update_interval = SystemProperties.getLong("persist.vendor.btstack.avrcp.pos_time", 1000L);
-                }
+                update_interval = SystemProperties.getLong("persist.vendor.btstack.avrcp.pos_time", 3000L);
                 deviceFeatures[deviceIndex].mPlayPosChangedNT =
                                              AvrcpConstants_ext.NOTIFICATION_TYPE_INTERIM;
                 update_interval = Math.max((long)param * 1000L, update_interval);
@@ -2919,7 +2897,8 @@
         BluetoothDevice activeDevice = mA2dpService.getActiveDevice();
         for (int i = 0; i < maxAvrcpConnections; i++) {
             if (deviceFeatures[i].mCurrentDevice != null && activeDevice != null &&
-                    Objects.equals(deviceFeatures[i].mCurrentDevice, activeDevice)) {
+                (isTwsPlusPair(activeDevice, deviceFeatures[i].mCurrentDevice) ||
+                Objects.equals(deviceFeatures[i].mCurrentDevice, activeDevice))) {
                 if ((deviceFeatures[i].mFeatures &
                         BTRC_FEAT_ABSOLUTE_VOLUME) != 0) {
                     status = true;
@@ -3288,6 +3267,7 @@
 
     private void SetBrowsePackage(String PackageName) {
         String browseService = (PackageName != null)?getBrowseServiceName(PackageName):null;
+        BrowsedMediaPlayer_ext player = mAvrcpBrowseManager.getBrowsedMediaPlayer(dummyaddr);
         Log.w(TAG, "SetBrowsePackage for pkg " + PackageName + "svc" + browseService);
         if (browseService != null && !browseService.isEmpty()) {
             BluetoothDevice active_device = null;
@@ -3306,6 +3286,10 @@
                     Log.w(TAG, "Addr Player update to Browse " + PackageName);
                     mAvrcpBrowseManager.getBrowsedMediaPlayer(addr).
                             setCurrentPackage(PackageName, browseService);
+                    if (player != null) {
+                        Log.w(TAG,"checkMBSConnection try connect");
+                        player.CheckMBSConnection(PackageName, browseService);
+                    }
                 }
             } else {
                 Log.w(TAG, "SetBrowsePackage Active device not set yet cache " + PackageName +
diff --git a/packages_apps_bluetooth_ext/src/ba/BATService.java b/packages_apps_bluetooth_ext/src/ba/BATService.java
index 5f6a01b..d65eda5 100644
--- a/packages_apps_bluetooth_ext/src/ba/BATService.java
+++ b/packages_apps_bluetooth_ext/src/ba/BATService.java
@@ -301,8 +301,10 @@
                         BluetoothAdapter.ERROR);
                 if (state == BluetoothAdapter.STATE_BLE_ON || state == BluetoothAdapter.STATE_ON) {
                     Log.d(TAG,"ACTION_BLE_STATE_CHANGED state: " + state);
-                    mGattBroadcastService = new GattBroadcastService();
-                    mGattBroadcastService.start(getApplicationContext());
+                    if (mGattBroadcastService == null) {
+                        mGattBroadcastService = new GattBroadcastService();
+                        mGattBroadcastService.start(getApplicationContext());
+                    }
                 }
             }
         }
@@ -420,6 +422,7 @@
 
         if(mGattBroadcastService != null) {
             mGattBroadcastService.stop();
+            mGattBroadcastService = null;
         }
         Log.d(TAG, "BATService :: stop - ");
         return true;
diff --git a/packages_apps_bluetooth_ext/src/ba/GattBroadcastService.java b/packages_apps_bluetooth_ext/src/ba/GattBroadcastService.java
index cef1810..b77e501 100644
--- a/packages_apps_bluetooth_ext/src/ba/GattBroadcastService.java
+++ b/packages_apps_bluetooth_ext/src/ba/GattBroadcastService.java
@@ -1290,25 +1290,29 @@
             addState(mBRADisabledStreamingActive);
             addState(mBRAEnabledStreamingPaused);
             addState(mBRAEnabledStreamingActive);
-
-            if (mBATService.getBATState() == BATService.STATE_PLAYING) {
-                setInitialState(mBRADisabledStreamingActive);
-                int div = mBATService.getDIV();
-                Log.d("GattBroadcastServiceStateMachine:", "div = " + div);
-                if (div != -1) {
-                    mBleAdvertiser.setDiv(div);
+            if (mBATService != null) {
+                if (mBATService.getBATState() == BATService.STATE_PLAYING) {
+                    setInitialState(mBRADisabledStreamingActive);
+                    int div = mBATService.getDIV();
+                    Log.d("GattBroadcastServiceStateMachine:", "div = " + div);
+                    if (div != -1 && mBleAdvertiser != null) {
+                        mBleAdvertiser.setDiv(div);
+                    }
+                    if (mBleAdvertiser != null) {
+                        mBleAdvertiser.stopAdvertising();
+                        Log.d("GattBroadcastServiceStateMachine:",
+                            " starting advertising");
+                        mBleAdvertiser.startAdvertising(false,
+                            BleAdvertiser.BRA_DISABLED_STREAMING_ACTIVE_ADV_INTERVAL);
+                    }
                 }
-                if (mBleAdvertiser != null) {
-                    mBleAdvertiser.stopAdvertising();
-                    Log.d("GattBroadcastServiceStateMachine:"," starting advertising");
-                    mBleAdvertiser.startAdvertising(false,
-                        BleAdvertiser.BRA_DISABLED_STREAMING_ACTIVE_ADV_INTERVAL);
-                }
+                else if (mBATService.getBATState() == BATService.STATE_PAUSED)
+                    setInitialState(mBRADisabledStreamingPaused);
+                else
+                    setInitialState(mBRADisabledStreamingDisabled);
             }
-            else if (mBATService.getBATState() == BATService.STATE_PAUSED)
-                setInitialState(mBRADisabledStreamingPaused);
             else
-                setInitialState(mBRADisabledStreamingDisabled);
+                Log.e(TAG, "BA Service is null");
 
         }
 
@@ -2075,4 +2079,4 @@
             }
         }
     }
-}
\ No newline at end of file
+}
diff --git a/system_bt_ext/btif/src/btif_l2cap.cc b/system_bt_ext/btif/src/btif_l2cap.cc
index 4107d86..3b799eb 100644
--- a/system_bt_ext/btif/src/btif_l2cap.cc
+++ b/system_bt_ext/btif/src/btif_l2cap.cc
@@ -35,6 +35,7 @@
 #define LOG_TAG "bluedroid"
 
 #include "btif_api.h"
+#include "btif_config.h"
 #include "bt_utils.h"
 #include "l2cdefs.h"
 #include "l2c_api.h"
@@ -238,6 +239,14 @@
 {
     BTIF_TRACE_DEBUG("LE-L2CAP: %s:: %s", __FUNCTION__, address.ToString().c_str());
 
+    int addr_type = 0;
+    int device_type = 0;
+    if (btif_get_address_type(address, &addr_type) &&
+          btif_get_device_type(address, &device_type) &&
+          device_type != BT_DEVICE_TYPE_BREDR) {
+        BTA_DmAddBleDevice(address, addr_type, device_type);
+    }
+
     if (0 == (g_lcid = L2CA_CONNECT_COC_REQ (le_psm, address, p_cfg))) {
         BTIF_TRACE_ERROR("LE-L2CAP: L2CA_LE_CreditBasedConn_Req failed for le_psm ");
     }
diff --git a/system_bt_ext/btif/src/btif_vendor.cc b/system_bt_ext/btif/src/btif_vendor.cc
index b8dbcf8..208444f 100644
--- a/system_bt_ext/btif/src/btif_vendor.cc
+++ b/system_bt_ext/btif/src/btif_vendor.cc
@@ -91,6 +91,7 @@
 #define BTA_SERVICE_ID_TO_SERVICE_MASK(id)  ((tBTA_SERVICE_MASK)1 << (id))
 #define CALLBACK_TIMER_PERIOD_MS      (60000)
 #define BTIF_VENDOR_BREDR_CLEANUP 1
+#define BTIF_VENDOR_HCI_CLOSE 2
 
 typedef struct {
   RawAddress bd_addr; /* BD address peer device. */
@@ -173,6 +174,10 @@
     HAL_CBACK(bt_vendor_callbacks, bredr_cleanup_cb, true);
 }
 
+static void btif_vendor_hci_close_event(uint16_t event, char *p_param)
+{
+    btif_hci_close();
+}
 static void btif_vendor_send_iot_info_cb(uint16_t event, char *p_param)
 {
     broadcast_cb_data.is_valid = false;
@@ -354,7 +359,8 @@
 static void hciclose(void)
 {
     LOG_INFO(LOG_TAG,"hciclose");
-    btif_hci_close();
+    btif_transfer_context(btif_vendor_hci_close_event,BTIF_VENDOR_HCI_CLOSE,
+                          NULL, 0, NULL);
 }
 
 
diff --git a/system_bt_ext/conf/interop_database.conf b/system_bt_ext/conf/interop_database.conf
index 1a04db3..cc3f770 100644
--- a/system_bt_ext/conf/interop_database.conf
+++ b/system_bt_ext/conf/interop_database.conf
@@ -208,6 +208,7 @@
 EB:06:EF = Address_Based
 74:95:EC = Address_Based
 AC:FD:93 = Address_Based
+AA:A8:AC = Address_Based
 
 # Disable role switch policy for headsets/car-kits
 # Some car kits initiate a role switch but won't initiate encryption
diff --git a/system_bt_ext/osi/src/vnd_log.cc b/system_bt_ext/osi/src/vnd_log.cc
index 4fb9a64..67219a7 100644
--- a/system_bt_ext/osi/src/vnd_log.cc
+++ b/system_bt_ext/osi/src/vnd_log.cc
@@ -127,6 +127,7 @@
   if(socket_local_server_bind(listen_socket_local, LOCAL_SOCKET_NAME,
       ANDROID_SOCKET_NAMESPACE_ABSTRACT) < 0) {
     LOG_ERROR(LOG_TAG, "Failed to create Local Socket (%s)", strerror(errno));
+    close(listen_socket_local);
     return -1;
   }