Insert vndk version to hal library path strings

If ro.vndk.version has <version>, "vndk-sp-<version>" directory must
be used instead of "vndk-sp" directory.

Bug: 69984421
Test: 1. build with all vndk snapshot patches included.
      2. mv /system/lib[64]/vndk and /system/lib[64]/vndk-sp
         directories to /system/lib[64]/vndk-28 and
         /system/lib[64]/vndk-sp-28.
      3. set "ro.vndk.version" to 28 in /vendor/default.prop.
      4. make vbmeta.img skip verity.
      5. "make snod" and "make vnod" to build system/vendor images.
      6. flash the image to device and check if YouTube plays with
         sound.

Change-Id: I02473090c8b9a5281a9290c9c0b9ee64702e4304
diff --git a/base/HidlInternal.cpp b/base/HidlInternal.cpp
index 6cdc24e..10e250b 100644
--- a/base/HidlInternal.cpp
+++ b/base/HidlInternal.cpp
@@ -19,6 +19,8 @@
 #include <hidl/HidlInternal.h>
 
 #include <android-base/logging.h>
+#include <android-base/properties.h>
+#include <android-base/stringprintf.h>
 #include <cutils/properties.h>
 
 #ifdef LIBHIDL_TARGET_DEBUGGABLE
@@ -43,6 +45,21 @@
     LOG(FATAL) << message;
 }
 
+std::string getVndkVersionStr() {
+    static std::string vndkVersion("0");
+    // "0" means the vndkVersion must be initialized with the property value.
+    // Otherwise, return the value.
+    if (vndkVersion == "0") {
+        vndkVersion = android::base::GetProperty("ro.vndk.version", "");
+        if (vndkVersion != "" && vndkVersion != "current") {
+            vndkVersion = "-" + vndkVersion;
+        } else {
+            vndkVersion = "";
+        }
+    }
+    return vndkVersion;
+}
+
 // ----------------------------------------------------------------------
 // HidlInstrumentor implementation.
 HidlInstrumentor::HidlInstrumentor(const std::string& package, const std::string& interface)
@@ -127,8 +144,10 @@
                      "") > 0) {
         instrumentationLibPaths.push_back(instrumentationLibPath);
     } else {
+        static std::string halLibPathVndkSp = android::base::StringPrintf(
+            HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION, getVndkVersionStr().c_str());
         instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM);
-        instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VNDK_SP);
+        instrumentationLibPaths.push_back(halLibPathVndkSp);
         instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR);
         instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM);
     }