Load external library in the default namespace

Aptx and aptx-Hd are both loaded from system_ext.
In order to load their dependencies from a compatible environement we
need to set the lib as a requirredLibs in the manifest

Fix: 231967310
Test: atest net_test_stack_a2dp_native
Test: Log analyze after bluetooth boot
Tag: #refactor
Ignore-AOSP-First: Apex only on tm and below
Merged-In: I084536b137adde5754eb98d80012bc7fdad1af74
Change-Id: I084536b137adde5754eb98d80012bc7fdad1af74
diff --git a/apex/apex_manifest.json b/apex/apex_manifest.json
index 5ae6795..905a96b 100644
--- a/apex/apex_manifest.json
+++ b/apex/apex_manifest.json
@@ -1,4 +1,8 @@
 {
   "name": "com.android.bluetooth",
-  "version": 330090000
+  "version": 330090000,
+  "requireNativeLibs": [
+    "libaptX_encoder.so",
+    "libaptXHD_encoder.so"
+  ]
 }
diff --git a/apex/linker.config.json b/apex/linker.config.json
index 7593c02..2c63c08 100644
--- a/apex/linker.config.json
+++ b/apex/linker.config.json
@@ -1,5 +1,2 @@
 {
-    "permittedPaths": [
-        "/system_ext/${LIB}"
-    ]
 }
diff --git a/system/stack/a2dp/a2dp_vendor.cc b/system/stack/a2dp/a2dp_vendor.cc
index e8887ec..759fc43 100644
--- a/system/stack/a2dp/a2dp_vendor.cc
+++ b/system/stack/a2dp/a2dp_vendor.cc
@@ -661,22 +661,15 @@
          " codec_id: " + loghex(codec_id);
 }
 
-void* A2DP_VendorCodecLoadExternalLib(const std::vector<std::string>& lib_paths,
+void* A2DP_VendorCodecLoadExternalLib(const std::string& lib_name,
                                       const std::string& friendly_name) {
-  std::string lib_path_error_list = "";
-  for (auto lib_path : lib_paths) {
-    void* lib_handle = dlopen(lib_path.c_str(), RTLD_NOW);
-    if (lib_handle != NULL) {
-      LOG(INFO) << __func__ << "Library found: " << friendly_name << " with ["
-                << lib_path << "]."
-                << " (Tested libs: " << lib_path_error_list << ")";
-      return lib_handle;
-    }
-    lib_path_error_list += "[ Err: ";
-    lib_path_error_list += dlerror();
-    lib_path_error_list += " ], ";
+  void* lib_handle = dlopen(lib_name.c_str(), RTLD_NOW);
+  if (lib_handle == NULL) {
+    LOG(ERROR) << __func__
+               << ": Failed to load codec library: " << friendly_name
+               << ". Err: [" << dlerror() << "]";
+    return nullptr;
   }
-  LOG(ERROR) << __func__ << "Failed to open library: " << friendly_name
-             << ". (Tested libs: " << lib_path_error_list << ")";
-  return nullptr;
+  LOG(INFO) << __func__ << ": Codec library loaded: " << friendly_name;
+  return lib_handle;
 }
diff --git a/system/stack/a2dp/a2dp_vendor_aptx_encoder.cc b/system/stack/a2dp/a2dp_vendor_aptx_encoder.cc
index 1489943..92463f5 100644
--- a/system/stack/a2dp/a2dp_vendor_aptx_encoder.cc
+++ b/system/stack/a2dp/a2dp_vendor_aptx_encoder.cc
@@ -108,15 +108,6 @@
                                 size_t* data_out_index, uint16_t* data16_in,
                                 uint8_t* data_out);
 
-static const std::vector<std::string> APTX_ENCODER_LIB_PATHS = {
-    APTX_ENCODER_LIB_NAME,
-#ifdef __LP64__
-    "/system_ext/lib64/" + APTX_ENCODER_LIB_NAME,
-#else
-    "/system_ext/lib/" + APTX_ENCODER_LIB_NAME,
-#endif
-};
-
 /*******************************************************************************
  *
  * Function         A2DP_VendorLoadEncoderAptx
@@ -133,7 +124,7 @@
 
   // Open the encoder library
   aptx_encoder_lib_handle =
-      A2DP_VendorCodecLoadExternalLib(APTX_ENCODER_LIB_PATHS, "aptX encoder");
+      A2DP_VendorCodecLoadExternalLib(APTX_ENCODER_LIB_NAME, "AptX encoder");
 
   if (!aptx_encoder_lib_handle) return LOAD_ERROR_MISSING_CODEC;
 
diff --git a/system/stack/a2dp/a2dp_vendor_aptx_hd_encoder.cc b/system/stack/a2dp/a2dp_vendor_aptx_hd_encoder.cc
index 06229d8..f3dd9e4 100644
--- a/system/stack/a2dp/a2dp_vendor_aptx_hd_encoder.cc
+++ b/system/stack/a2dp/a2dp_vendor_aptx_hd_encoder.cc
@@ -109,15 +109,6 @@
                                    size_t* data_out_index, uint32_t* data32_in,
                                    uint8_t* data_out);
 
-static const std::vector<std::string> APTX_HD_ENCODER_LIB_PATHS = {
-    APTX_HD_ENCODER_LIB_NAME,
-#ifdef __LP64__
-    "/system_ext/lib64/" + APTX_HD_ENCODER_LIB_NAME,
-#else
-    "/system_ext/lib/" + APTX_HD_ENCODER_LIB_NAME,
-#endif
-};
-
 /*******************************************************************************
  *
  * Function         A2DP_VendorLoadEncoderAptxHd
@@ -135,7 +126,7 @@
 
   // Open the encoder library
   aptx_hd_encoder_lib_handle = A2DP_VendorCodecLoadExternalLib(
-      APTX_HD_ENCODER_LIB_PATHS, "aptX-HD encoder");
+      APTX_HD_ENCODER_LIB_NAME, "AptX-HD encoder");
   if (!aptx_hd_encoder_lib_handle) return LOAD_ERROR_MISSING_CODEC;
 
   aptx_hd_api.init_func =
diff --git a/system/stack/include/a2dp_vendor.h b/system/stack/include/a2dp_vendor.h
index 73db5c4..f2b6feb 100644
--- a/system/stack/include/a2dp_vendor.h
+++ b/system/stack/include/a2dp_vendor.h
@@ -221,11 +221,12 @@
 std::string A2DP_VendorCodecInfoString(const uint8_t* p_codec_info);
 
 // Try to dlopen external codec library
-// Will iterate over |lib_paths| to return the first successfully dlopen library
+//
+// |lib_name| is the name of the library to load
 // |friendly_name| is only use for logging purpose
 // Return pointer to the handle if the library is successfully dlopen,
 // nullptr otherwise
-void* A2DP_VendorCodecLoadExternalLib(const std::vector<std::string>& lib_paths,
+void* A2DP_VendorCodecLoadExternalLib(const std::string& lib_name,
                                       const std::string& friendly_name);
 
 #define LOAD_CODEC_SYMBOL(codec_name, handle, error_fn, symbol_name, api_type) \
diff --git a/system/stack/test/a2dp/misc_fake.cc b/system/stack/test/a2dp/misc_fake.cc
index 13675a4..0c01c44 100644
--- a/system/stack/test/a2dp/misc_fake.cc
+++ b/system/stack/test/a2dp/misc_fake.cc
@@ -42,22 +42,15 @@
 int A2DP_VendorGetTrackSampleRateAptxHd(unsigned char const*) { return 0; }
 int A2DP_VendorGetTrackChannelCountAptxHd(unsigned char const*) { return 0; }
 
-void* A2DP_VendorCodecLoadExternalLib(const std::vector<std::string>& lib_paths,
+void* A2DP_VendorCodecLoadExternalLib(const std::string& lib_name,
                                       const std::string& friendly_name) {
-  std::string lib_path_error_list = "";
-  for (auto lib_path : lib_paths) {
-    void* lib_handle = dlopen(lib_path.c_str(), RTLD_NOW);
-    if (lib_handle != NULL) {
-      LOG(INFO) << __func__ << "Library found: " << friendly_name << " with ["
-                << lib_path << "]."
-                << " (Tested libs: " << lib_path_error_list << ")";
-      return lib_handle;
-    }
-    lib_path_error_list += "[ Err: ";
-    lib_path_error_list += dlerror();
-    lib_path_error_list += " ], ";
+  void* lib_handle = dlopen(lib_name.c_str(), RTLD_NOW);
+  if (lib_handle == NULL) {
+    LOG(ERROR) << __func__
+               << ": Failed to load codec library: " << friendly_name
+               << ". Err: [" << dlerror() << "]";
+    return nullptr;
   }
-  LOG(ERROR) << __func__ << "Failed to open library: " << friendly_name
-             << ". (Tested libs: " << lib_path_error_list << ")";
-  return nullptr;
+  LOG(INFO) << __func__ << ": Codec library loaded: " << friendly_name;
+  return lib_handle;
 }