Merge commit '7bbc797715f02e5b080c47d9af8de51cfd41c864' into bt.lnx.5.0.r5

Change-Id: I74dab2f2f6c3fed7ef94059ad2112875e6534595
diff --git a/packages_apps_bluetooth_ext/jni/Android.bp b/packages_apps_bluetooth_ext/jni/Android.bp
index 9fa6731..a041248 100644
--- a/packages_apps_bluetooth_ext/jni/Android.bp
+++ b/packages_apps_bluetooth_ext/jni/Android.bp
@@ -1,6 +1,7 @@
 cc_defaults {
     name: "libbluetoothqti_jni_default",
-    header_libs: ["libbluetooth_headers"],
+    // KEYSTONE(I999cc7985b32bae2db80bcfcede22d37b8e62ff5,b/141267953)
+    header_libs: ["libbluetooth_headers", "libnativehelper_header_only"],
     include_dirs: [
         "system/bt/types",
         "packages/apps/Bluetooth/jni",
diff --git a/packages_apps_bluetooth_ext/src/avrcp/Avrcp_ext.java b/packages_apps_bluetooth_ext/src/avrcp/Avrcp_ext.java
index f4412cc..e515824 100644
--- a/packages_apps_bluetooth_ext/src/avrcp/Avrcp_ext.java
+++ b/packages_apps_bluetooth_ext/src/avrcp/Avrcp_ext.java
@@ -269,6 +269,9 @@
     private SortedMap<Integer, MediaPlayerInfo_ext> mMediaPlayerInfoList;
     private boolean mAvailablePlayerViewChanged;
 
+    private boolean mPlayerSwitching;
+    private List<String> mPkgRequestedMBSConnect;
+
     /* List of media players which supports browse */
     private List<BrowsePlayerInfo_ext> mBrowsePlayerInfoList;
 
@@ -553,6 +556,8 @@
         mAvrcpPlayerAppSettingsRsp = new AvrcpPlayerAppSettingsRsp();
         mMediaPlayerInfoList = new TreeMap<Integer, MediaPlayerInfo_ext>();
         mAvailablePlayerViewChanged = false;
+        mPlayerSwitching = false;
+        mPkgRequestedMBSConnect = new ArrayList<String>();
         mBrowsePlayerInfoList = Collections.synchronizedList(new ArrayList<BrowsePlayerInfo_ext>());
         mPassthroughDispatched = 0;
         mPassthroughLogs = new EvictingQueue_ext<MediaKeyLog>(PASSTHROUGH_LOG_MAX_SIZE);
@@ -675,6 +680,7 @@
         mContext.unregisterReceiver(mAvrcpReceiver);
         mContext.unregisterReceiver(mBootReceiver);
 
+        mPkgRequestedMBSConnect.clear();
         mAddressedMediaPlayer.cleanup();
         mAvrcpBrowseManager.cleanup();
         mCurrentBrowsingDevice = null;
@@ -2340,6 +2346,7 @@
                     deviceFeatures[index].mReportedPlayerID != mCurrAddrPlayerID) {
                 Log.v(TAG, "Update player id: " + deviceFeatures[index].mReportedPlayerID +
                         "-> " + mCurrAddrPlayerID);
+                mPlayerSwitching = true;
                 if (deviceFeatures[index].mAvailablePlayersChangedNT ==
                         AvrcpConstants_ext.NOTIFICATION_TYPE_INTERIM) {
                     registerNotificationRspAvalPlayerChangedNative(
@@ -2666,10 +2673,9 @@
         if (registering)
             deviceFeatures[deviceIndex].mTrackChangedNT = AvrcpConstants_ext.NOTIFICATION_TYPE_INTERIM;
 
-        MediaPlayerInfo_ext info = getAddressedPlayerInfo();
         byte[] byteAddr = getByteAddress(deviceFeatures[deviceIndex].mCurrentDevice);
         // for non-browsable players or no player
-        if ((info != null && !info.isBrowseSupported()) ||
+        if (!isPlayerInBrowseList() ||
                 (deviceFeatures[deviceIndex].mFeatures & BTRC_FEAT_BROWSE) == 0) {
             byte[] track = AvrcpConstants_ext.TRACK_IS_SELECTED;
             if (!mMediaAttributes.exists) track = AvrcpConstants_ext.NO_TRACK_SELECTED;
@@ -2792,6 +2798,23 @@
         return playStatus;
     }
 
+    private boolean isPlayerInBrowseList() {
+        MediaPlayerInfo_ext info = getAddressedPlayerInfo();
+        String pkgName = (info != null) ? info.getPackageName():"";
+        if (pkgName == null || pkgName.isEmpty())
+            return false;
+
+        BrowsedMediaPlayer_ext player =
+                mAvrcpBrowseManager.getBrowsedMediaPlayer(dummyaddr);
+        String browseService = (pkgName != null)?getBrowseServiceName(pkgName):null;
+        if (player == null || browseService == null || browseService.isEmpty())
+            return false;
+
+        boolean isBrowseSupported = player.isPackageInMBSList(pkgName);
+        Log.d(TAG, "Browse supported for pkg " + pkgName + " is " + isBrowseSupported);
+        return isBrowseSupported;
+    }
+
     private boolean isPlayingState(@Nullable PlaybackState state) {
         if (state == null) return false;
         return (state != null) && (state.getState() == PlaybackState.STATE_PLAYING);
@@ -3284,12 +3307,14 @@
                 byte[] addr = getByteAddress(active_device);
                 if (mAvrcpBrowseManager.getBrowsedMediaPlayer(addr) != null) {
                     mCurrentBrowsingDevice = active_device;
-                    Log.w(TAG, "Addr Player update to Browse " + PackageName);
+                    Log.w(TAG, "Addr Player update to Browse " + PackageName +
+                            " already req MBS list " + mPkgRequestedMBSConnect);
                     mAvrcpBrowseManager.getBrowsedMediaPlayer(addr).
                             setCurrentPackage(PackageName, browseService);
-                    if (player != null) {
+                    if (player != null && !mPkgRequestedMBSConnect.contains(PackageName)) {
                         Log.w(TAG,"checkMBSConnection try connect");
                         player.CheckMBSConnection(PackageName, browseService);
+                        mPkgRequestedMBSConnect.add(PackageName);
                     }
                 }
             } else {
@@ -5135,9 +5160,15 @@
                 return;
             }
 
+            if (!isPlayerInBrowseList() && !mPlayerSwitching) {
+                Log.e(TAG,"Disallow sending changed response to non Browsable Players");
+                return;
+            }
+
             if (!registerNotificationRspNowPlayingChangedNative(type, addr)) {
                 Log.e(TAG, "registerNotificationRspNowPlayingChangedNative failed!");
             }
+            mPlayerSwitching = false;
             mNowPlayingListChangedNT = AvrcpConstants_ext.NOTIFICATION_TYPE_CHANGED;
         }