Merge cherrypicks of [5317808, 5317809, 5318498, 5317873, 5318338, 5318195, 5318499, 5317874, 5317875, 5317876, 5318243, 5318244, 5318537, 5318538, 5318539, 5318540, 5318541, 5318542, 5318543, 5318544, 5318545, 5318546, 5315210, 5317756, 5318557, 5318558, 5318559, 5318560, 5318561, 5318339, 5318547, 5318548, 5318549, 5318562, 5318563, 5318564, 5318565, 5318566, 5318172, 5318173, 5318174, 5318550, 5318401, 5318196, 5317889, 5318175, 5318176, 5318577, 5318578, 5318579, 5318580, 5318581, 5318503, 5318390, 5318505, 5318341, 5318551] into pi-qpr1-release

Change-Id: I000919e1a27af9db6da52cac19da8b0445e03df1
diff --git a/jni/com_android_bluetooth_avrcp_target.cpp b/jni/com_android_bluetooth_avrcp_target.cpp
index 2532be2..8ac04e4 100644
--- a/jni/com_android_bluetooth_avrcp_target.cpp
+++ b/jni/com_android_bluetooth_avrcp_target.cpp
@@ -62,6 +62,15 @@
 static void volumeDeviceDisconnected(const RawAddress& address);
 static void setVolume(int8_t volume);
 
+// Local Variables
+// TODO (apanicke): Use a map here to store the callback in order to
+// support multi-browsing
+SetBrowsedPlayerCb set_browsed_player_cb;
+using map_entry = std::pair<std::string, GetFolderItemsCb>;
+std::map<std::string, GetFolderItemsCb> get_folder_items_cb_map;
+std::map<RawAddress, ::bluetooth::avrcp::VolumeInterface::VolumeChangedCb>
+    volumeCallbackMap;
+
 // TODO (apanicke): In the future, this interface should guarantee that
 // all calls happen on the JNI Thread. Right now this is very difficult
 // as it is hard to get a handle on the JNI thread from here.
@@ -249,6 +258,9 @@
   std::unique_lock<std::shared_timed_mutex> interface_lock(interface_mutex);
   std::unique_lock<std::shared_timed_mutex> callbacks_lock(callbacks_mutex);
 
+  get_folder_items_cb_map.clear();
+  volumeCallbackMap.clear();
+
   sServiceInterface->Cleanup();
   env->DeleteGlobalRef(mJavaInterface);
   mJavaInterface = nullptr;
@@ -593,10 +605,6 @@
   return ret_list;
 }
 
-// TODO (apanicke): Use a map here to store the callback in order to
-// support multi-browsing
-SetBrowsedPlayerCb set_browsed_player_cb;
-
 static void setBrowsedPlayer(uint16_t player_id, SetBrowsedPlayerCb cb) {
   ALOGD("%s", __func__);
   std::shared_lock<std::shared_timed_mutex> lock(callbacks_mutex);
@@ -623,9 +631,6 @@
   set_browsed_player_cb.Run(success == JNI_TRUE, root, num_items);
 }
 
-using map_entry = std::pair<std::string, GetFolderItemsCb>;
-std::map<std::string, GetFolderItemsCb> get_folder_items_cb_map;
-
 static void getFolderItemsResponseNative(JNIEnv* env, jobject object,
                                          jstring parent_id, jobject list) {
   ALOGD("%s", __func__);
@@ -751,9 +756,6 @@
                                j_bdaddr, JNI_FALSE);
 }
 
-std::map<RawAddress, ::bluetooth::avrcp::VolumeInterface::VolumeChangedCb>
-    volumeCallbackMap;
-
 static void volumeDeviceConnected(
     const RawAddress& address,
     ::bluetooth::avrcp::VolumeInterface::VolumeChangedCb cb) {