#include "system_utils.h"
#include <android/api-level.h>
#include <stdlib.h>
#include <sys/system_properties.h>
namespace gamesdk {
#if __ANDROID_API__ >= 26
std::string getSystemPropViaCallback(const char* key, const char* default_value = "") {
const prop_info* prop = __system_property_find(key);
if (prop == nullptr) {
return default_value;
}
std::string return_value;
auto thunk = [](void* cookie, const char* , const char* value, uint32_t ) {
if (value != nullptr) {
std::string* r = static_cast<std::string*>(cookie);
*r = value;
}
};
__system_property_read_callback(prop, thunk, &return_value);
return return_value;
}
#else
std::string getSystemPropViaGet(const char* key, const char* default_value = "") {
char buffer[PROP_VALUE_MAX + 1] = ""; int bufferLen = __system_property_get(key, buffer);
if (bufferLen > 0)
return buffer;
else
return "";
}
#endif
std::string GetSystemProp(const char* key, const char* default_value) {
#if __ANDROID_API__ >= 26
return getSystemPropViaCallback(key, default_value);
#else
return getSystemPropViaGet(key, default_value);
#endif
}
int GetSystemPropAsInt(const char* key, int default_value) {
std::string prop = GetSystemProp(key);
return prop == "" ? default_value : strtoll(prop.c_str(), nullptr, 10);
}
bool GetSystemPropAsBool(const char* key, bool default_value) {
return GetSystemPropAsInt(key, default_value) != 0;
}
}