blob: ca4fa1173ce7b2262bd8a0d3b8b7c7fe5d98ca62 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* //device/libs/android_runtime/android_os_SystemProperties.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include "cutils/properties.h"
19#include "jni.h"
20#include "android_runtime/AndroidRuntime.h"
21#include <nativehelper/JNIHelp.h>
22
23namespace android
24{
25
26static jstring SystemProperties_getSS(JNIEnv *env, jobject clazz,
27 jstring keyJ, jstring defJ)
28{
29 int len;
30 const char* key;
31 char buf[PROPERTY_VALUE_MAX];
32 jstring rvJ = NULL;
33
34 if (keyJ == NULL) {
35 jniThrowException(env, "java/lang/NullPointerException",
36 "key must not be null.");
37 goto error;
38 }
39
40 key = env->GetStringUTFChars(keyJ, NULL);
41
42 len = property_get(key, buf, "");
43 if ((len <= 0) && (defJ != NULL)) {
44 rvJ = defJ;
45 } else if (len >= 0) {
46 rvJ = env->NewStringUTF(buf);
47 } else {
48 rvJ = env->NewStringUTF("");
49 }
50
51 env->ReleaseStringUTFChars(keyJ, key);
52
53error:
54 return rvJ;
55}
56
57static jstring SystemProperties_getS(JNIEnv *env, jobject clazz,
58 jstring keyJ)
59{
60 return SystemProperties_getSS(env, clazz, keyJ, NULL);
61}
62
63static void SystemProperties_set(JNIEnv *env, jobject clazz,
64 jstring keyJ, jstring valJ)
65{
66 int err;
67 const char* key;
68 const char* val;
69
70 if (keyJ == NULL) {
71 jniThrowException(env, "java/lang/NullPointerException",
72 "key must not be null.");
73 return ;
74 }
75 key = env->GetStringUTFChars(keyJ, NULL);
76
77 if (valJ == NULL) {
78 val = ""; /* NULL pointer not allowed here */
79 } else {
80 val = env->GetStringUTFChars(valJ, NULL);
81 }
82
83 err = property_set(key, val);
84
85 env->ReleaseStringUTFChars(keyJ, key);
86
87 if (valJ != NULL) {
88 env->ReleaseStringUTFChars(valJ, val);
89 }
90}
91
92static JNINativeMethod method_table[] = {
93 { "native_get", "(Ljava/lang/String;)Ljava/lang/String;",
94 (void*) SystemProperties_getS },
95 { "native_get", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
96 (void*) SystemProperties_getSS },
97 { "native_set", "(Ljava/lang/String;Ljava/lang/String;)V",
98 (void*) SystemProperties_set },
99};
100
101int register_android_os_SystemProperties(JNIEnv *env)
102{
103 return AndroidRuntime::registerNativeMethods(
104 env, "android/os/SystemProperties",
105 method_table, NELEM(method_table));
106}
107
108};