template<typename Functor>
void ReadProperty(const prop_info
*
prop, Functor&& functor)
{
auto thunk
=
[](void
*
cookie,
const char
*
/
*
name
*
/
,
const char
*
value,
uint32_t
/
*
serial
*
/
) {
std::forward<Functor>(
*
static_cast<Functor
*
>(cookie))(value);
};
__system_property_read_callback(prop, thunk, &functor);
LOG(FATAL) <<
"fast property access supported only on device"
;
}
template<typename Functor>
void ReadProperty(JNIEnv
*
env, jstring keyJ, Functor&& functor)
{
ScopedUtfChars key(env, keyJ);
if
(!key.c_str()) {
return
;
}
const prop_info
*
prop
=
__system_property_find(key.c_str());
if
(!prop) {
return
;
}
ReadProperty(prop, std::forward<Functor>(functor));
std::forward<Functor>(functor)(
android::base::GetProperty(key.c_str(), "").c_str());
}
jstring SystemProperties_getSS(JNIEnv
*
env, jclass clazz, jstring keyJ,
jstring defJ)
{
jstring ret
=
defJ;
ReadProperty(env, keyJ, [&](const char
*
value) {
if
(value[
0
]) {
ret
=
env
-
>NewStringUTF(value);
}
});
if
(ret
=
=
nullptr && !env
-
>ExceptionCheck()) {
ret
=
env
-
>NewStringUTF("");
/
/
Legacy behavior
}
return
ret;
}
int
register_android_os_SystemProperties(JNIEnv
*
env)
{
const JNINativeMethod method_table[]
=
{
{
"native_get"
,
"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"
,
(void
*
) SystemProperties_getSS },
{
"native_get_int"
,
"(Ljava/lang/String;I)I"
,
(void
*
) SystemProperties_get_integral<jint> },
{
"native_get_long"
,
"(Ljava/lang/String;J)J"
,
(void
*
) SystemProperties_get_integral<jlong> },
{
"native_get_boolean"
,
"(Ljava/lang/String;Z)Z"
,
(void
*
) SystemProperties_get_boolean },
{
"native_find"
,
"(Ljava/lang/String;)J"
,
(void
*
) SystemProperties_find },
{
"native_get"
,
"(J)Ljava/lang/String;"
,
(void
*
) SystemProperties_getH },
{
"native_get_int"
,
"(JI)I"
,
(void
*
) SystemProperties_get_integralH<jint> },
{
"native_get_long"
,
"(JJ)J"
,
(void
*
) SystemProperties_get_integralH<jlong> },
{
"native_get_boolean"
,
"(JZ)Z"
,
(void
*
) SystemProperties_get_booleanH },
{
"native_set"
,
"(Ljava/lang/String;Ljava/lang/String;)V"
,
(void
*
) SystemProperties_set },
{
"native_add_change_callback"
,
"()V"
,
(void
*
) SystemProperties_add_change_callback },
{
"native_report_sysprop_change"
,
"()V"
,
(void
*
) SystemProperties_report_sysprop_change },
};
return
RegisterMethodsOrDie(env,
"android/os/SystemProperties"
,
method_table, NELEM(method_table));
}