From 1a35a3e51f1a47b86e0c56824880ca4bc189e519 Mon Sep 17 00:00:00 2001 From: vrepetsky <123964412+vrepetsky@users.noreply.github.com> Date: Fri, 27 Sep 2024 12:22:00 +0100 Subject: [PATCH] Fix usage of __system_property_read_callback for Android<26 Modify the function for getting system properties on Android: *Use the new __system_property_read_callback API that appeared in Android O / API level 26 when available. It is the preferred way because it allows handling long property names and better property caching. *When not available use the deprecated __system_property_get function. --- src/layer/layer_settings_manager.cpp | 40 +++++++++++++++++++++------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/layer/layer_settings_manager.cpp b/src/layer/layer_settings_manager.cpp index 1678e25..1722cff 100644 --- a/src/layer/layer_settings_manager.cpp +++ b/src/layer/layer_settings_manager.cpp @@ -33,18 +33,38 @@ #include #if defined(__ANDROID__) +/* + * Use the __system_property_read_callback API that appeared in + * Android API level 26. If not avaible use the old __system_property_get function. + */ + +// Weak function declaration, used only when not decalered in the Android headers +void __system_property_read_callback(const prop_info* info, + void (*callback)(void* cookie, + const char* name, + const char* value, + uint32_t serial), + void* cookie) __attribute__((weak)); static std::string GetAndroidProperty(const char *name) { std::string output; - const prop_info *pi = __system_property_find(name); - if (pi) { - __system_property_read_callback( - pi, - [](void *cookie, const char *name, const char *value, uint32_t serial) { - (void)name; - (void)serial; - reinterpret_cast(cookie)->assign(value); - }, - reinterpret_cast(&output)); + if(__system_property_read_callback != nullptr) { + const prop_info *pi = __system_property_find(name); + if (pi) { + __system_property_read_callback( + pi, + [](void *cookie, const char *name, const char *value, uint32_t serial) { + (void)name; + (void)serial; + reinterpret_cast(cookie)->assign(value); + }, + reinterpret_cast(&output)); + } + } else { + char value_buf[PROP_VALUE_MAX] = ""; + size_t len = static_cast(__system_property_get(name, value_buf)); + if (len > 0 && len < sizeof(value_buf)) { + output.assign(value_buf, len); + } } return output; }