AVRCP_Browse: NPE Fixes & improvements in BrowseMediaPlayer

- Fix NPE & other probable crash issues in BrowseMediaPlayer

- Don't try re-connection with MBS upon package updates for
players which are already in Browsable Player list.

- Upon removal/uninstalling a package remove it from the list
of Browsable Player list if exists.

- Don't entertain MBS connection updates after 3 sec timeout.

Change-Id: I8e76d72e3eeda25b8505c9e0a69c3471c72e9712
diff --git a/packages_apps_bluetooth_ext/src/avrcp/Avrcp_ext.java b/packages_apps_bluetooth_ext/src/avrcp/Avrcp_ext.java
index bef8fa9..9ce4fd0 100644
--- a/packages_apps_bluetooth_ext/src/avrcp/Avrcp_ext.java
+++ b/packages_apps_bluetooth_ext/src/avrcp/Avrcp_ext.java
@@ -3602,6 +3602,10 @@
                 int browseInfoID = getBrowseId(packageName);
                 if (browseInfoID != -1) {
                     mBrowsePlayerInfoList.remove(browseInfoID);
+                    BrowsedMediaPlayer_ext player =
+                            mAvrcpBrowseManager.getBrowsedMediaPlayer(dummyaddr);
+                    if (player != null)
+                        player.updateBrowsablePlayerList(packageName);
                 }
             }
         }
diff --git a/packages_apps_bluetooth_ext/src/avrcp/BrowsedMediaPlayer_ext.java b/packages_apps_bluetooth_ext/src/avrcp/BrowsedMediaPlayer_ext.java
index 1327a4a..5fe0028 100644
--- a/packages_apps_bluetooth_ext/src/avrcp/BrowsedMediaPlayer_ext.java
+++ b/packages_apps_bluetooth_ext/src/avrcp/BrowsedMediaPlayer_ext.java
@@ -129,8 +129,9 @@
                     break;
                 case MSG_DISCONNECT_PLAYER:
                     MediaBrowser mb = (MediaBrowser)msg.obj;
-                    mb.disconnect();
                     Log.w(TAG, "Trigger disconnect for MediaBrowser " + mb);
+                    if (mb != null)
+                        mb.disconnect();
                     break;
                 case MSG_TIMEOUT:
                     Log.w(TAG, "MSG_TIMEOUT");
@@ -157,8 +158,10 @@
 
         @Override
         public void onConnected() {
-            if ((mHandler != null) && !mBrowsablePlayerList.contains(mCallbackPackageName)) {
-                Log.d(TAG, "Add " + mCallbackPackageName + " to MBS List " + mBrowser);
+            Log.d(TAG, "Onconnected pkg " + mCallbackPackageName + " mb " + mBrowser);
+            if ((mHandler != null) && !mBrowsablePlayerList.contains(mCallbackPackageName)
+                    && mHandler.hasMessages(MSG_TIMEOUT, mCallbackPackageName)) {
+                Log.d(TAG, "Add " + mCallbackPackageName + " to MBS List " + mBrowsablePlayerList);
                 mBrowsablePlayerList.add(mCallbackPackageName);
                 mHandler.removeMessages(MSG_TIMEOUT, mCallbackPackageName);
                 Message msg = mHandler.obtainMessage(MSG_DISCONNECT_PLAYER, 0, 0, mBrowser);
@@ -175,7 +178,9 @@
 
         @Override
         public void onConnectionFailed() {
-            if ((mHandler != null) && !mBrowsablePlayerList.contains(mCallbackPackageName)) {
+            Log.d(TAG, "Onconnectedfail pkg " + mCallbackPackageName + " mb " + mBrowser);
+            if ((mHandler != null) && !mBrowsablePlayerList.contains(mCallbackPackageName)
+                    && mHandler.hasMessages(MSG_TIMEOUT, mCallbackPackageName)) {
                 mHandler.removeMessages(MSG_TIMEOUT, mCallbackPackageName);
             }
             mConnState = DISCONNECTED;
@@ -189,7 +194,9 @@
 
         @Override
         public void onConnectionSuspended() {
-            if ((mHandler != null) && !mBrowsablePlayerList.contains(mCallbackPackageName)) {
+            Log.d(TAG, "Onconnectedsuspend pkg " + mCallbackPackageName + " mb " + mBrowser);
+            if ((mHandler != null) && !mBrowsablePlayerList.contains(mCallbackPackageName)
+                    && mHandler.hasMessages(MSG_TIMEOUT, mCallbackPackageName)) {
                 mHandler.removeMessages(MSG_TIMEOUT, mCallbackPackageName);
             }
             mBrowser = null;
@@ -542,8 +549,21 @@
         Log.w(TAG, "Reconnected with Browser service");
     }
 
+    public void updateBrowsablePlayerList(String packageName) {
+        if (packageName == null || packageName.isEmpty())
+            return;
+        if (mBrowsablePlayerList.contains(packageName)) {
+            Log.w(TAG, "Remove pkg" + packageName + "from list" + mBrowsablePlayerList);
+            mBrowsablePlayerList.remove(packageName);
+        }
+    }
+
     public void CheckMBSConnection(String packageName, String cls) {
         Log.w(TAG, "TryconnectMBS with Browser service for package = " + packageName);
+        if (mBrowsablePlayerList.contains(packageName)) {
+            Log.w(TAG, "Already in MBS List don't reconnect" + mBrowsablePlayerList);
+            return;
+        }
         Message msg = mHandler.obtainMessage(MSG_CONNECT_PLAYER);
         Bundle data = new Bundle();
         data.putCharSequence("package", packageName);
@@ -698,7 +718,10 @@
                 mPathStack.push(newPath);
             }
         } else if (direction == AvrcpConstants_ext.DIR_UP) { /* move up */
-            if (!isBrowsableFolderUp()) {
+            if (mPathStack == null || mPathStack.isEmpty()) {
+                Log.w(TAG, "Path Stack not initialized send internal error!");
+                mMediaInterface.changePathRsp(mBDAddr, AvrcpConstants_ext.RSP_INTERNAL_ERR, 0);
+            } else if (!isBrowsableFolderUp()) {
                 /* Already on the root, cannot allow up: PTS: test case TC_TG_MCN_CB_BI_02_C
                  * This is required, otherwise some CT will keep on sending change path up
                  * until they receive error */