blob: 66af96566ad2470c8a21fd52645032569d40c428 [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**
Elliott Hughes69a017b2011-04-08 14:10:28 -07005** 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008**
Elliott Hughes69a017b2011-04-08 14:10:28 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010**
Elliott Hughes69a017b2011-04-08 14:10:28 -070011** 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015** 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) {
Elliott Hughes69a017b2011-04-08 14:10:28 -070035 jniThrowNullPointerException(env, "key must not be null.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 goto error;
37 }
Mike Lockwoodd1945952009-08-12 17:15:51 -040038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 key = env->GetStringUTFChars(keyJ, NULL);
Mike Lockwoodd1945952009-08-12 17:15:51 -040040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 len = property_get(key, buf, "");
42 if ((len <= 0) && (defJ != NULL)) {
43 rvJ = defJ;
44 } else if (len >= 0) {
45 rvJ = env->NewStringUTF(buf);
46 } else {
47 rvJ = env->NewStringUTF("");
48 }
Mike Lockwoodd1945952009-08-12 17:15:51 -040049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 env->ReleaseStringUTFChars(keyJ, key);
Mike Lockwoodd1945952009-08-12 17:15:51 -040051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052error:
53 return rvJ;
54}
55
56static jstring SystemProperties_getS(JNIEnv *env, jobject clazz,
57 jstring keyJ)
58{
59 return SystemProperties_getSS(env, clazz, keyJ, NULL);
60}
61
Mike Lockwoodd1945952009-08-12 17:15:51 -040062static jint SystemProperties_get_int(JNIEnv *env, jobject clazz,
63 jstring keyJ, jint defJ)
64{
65 int len;
66 const char* key;
67 char buf[PROPERTY_VALUE_MAX];
68 jint result = defJ;
69
70 if (keyJ == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -070071 jniThrowNullPointerException(env, "key must not be null.");
Mike Lockwoodd1945952009-08-12 17:15:51 -040072 goto error;
73 }
74
75 key = env->GetStringUTFChars(keyJ, NULL);
76
77 len = property_get(key, buf, "");
78 if (len > 0) {
79 jint temp;
80 if (sscanf(buf, "%d", &temp) == 1)
81 result = temp;
82 }
83
84 env->ReleaseStringUTFChars(keyJ, key);
85
86error:
87 return result;
88}
89
90static jlong SystemProperties_get_long(JNIEnv *env, jobject clazz,
91 jstring keyJ, jlong defJ)
92{
93 int len;
94 const char* key;
95 char buf[PROPERTY_VALUE_MAX];
96 jlong result = defJ;
97
98 if (keyJ == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -070099 jniThrowNullPointerException(env, "key must not be null.");
Mike Lockwoodd1945952009-08-12 17:15:51 -0400100 goto error;
101 }
102
103 key = env->GetStringUTFChars(keyJ, NULL);
104
105 len = property_get(key, buf, "");
106 if (len > 0) {
107 jlong temp;
108 if (sscanf(buf, "%lld", &temp) == 1)
109 result = temp;
110 }
111
112 env->ReleaseStringUTFChars(keyJ, key);
113
114error:
115 return result;
116}
117
118static jboolean SystemProperties_get_boolean(JNIEnv *env, jobject clazz,
119 jstring keyJ, jboolean defJ)
120{
121 int len;
122 const char* key;
123 char buf[PROPERTY_VALUE_MAX];
124 jboolean result = defJ;
125
126 if (keyJ == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700127 jniThrowNullPointerException(env, "key must not be null.");
Mike Lockwoodd1945952009-08-12 17:15:51 -0400128 goto error;
129 }
130
131 key = env->GetStringUTFChars(keyJ, NULL);
132
133 len = property_get(key, buf, "");
134 if (len == 1) {
135 char ch = buf[0];
136 if (ch == '0' || ch == 'n')
137 result = false;
138 else if (ch == '1' || ch == 'y')
139 result = true;
140 } else if (len > 1) {
141 if (!strcmp(buf, "no") || !strcmp(buf, "false") || !strcmp(buf, "off")) {
142 result = false;
143 } else if (!strcmp(buf, "yes") || !strcmp(buf, "true") || !strcmp(buf, "on")) {
144 result = true;
145 }
146 }
147
148 env->ReleaseStringUTFChars(keyJ, key);
149
150error:
151 return result;
152}
153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154static void SystemProperties_set(JNIEnv *env, jobject clazz,
155 jstring keyJ, jstring valJ)
156{
157 int err;
158 const char* key;
159 const char* val;
160
161 if (keyJ == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700162 jniThrowNullPointerException(env, "key must not be null.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 return ;
164 }
165 key = env->GetStringUTFChars(keyJ, NULL);
166
167 if (valJ == NULL) {
168 val = ""; /* NULL pointer not allowed here */
169 } else {
170 val = env->GetStringUTFChars(valJ, NULL);
171 }
Brad Fitzpatrick06f36742011-03-10 16:04:59 -0800172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 err = property_set(key, val);
Brad Fitzpatrick06f36742011-03-10 16:04:59 -0800174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 env->ReleaseStringUTFChars(keyJ, key);
Brad Fitzpatrick06f36742011-03-10 16:04:59 -0800176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 if (valJ != NULL) {
Brad Fitzpatrick06f36742011-03-10 16:04:59 -0800178 env->ReleaseStringUTFChars(valJ, val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 }
Brad Fitzpatrick06f36742011-03-10 16:04:59 -0800180
181 if (err < 0) {
182 jniThrowException(env, "java/lang/RuntimeException",
183 "failed to set system property");
184 }
185}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
187static JNINativeMethod method_table[] = {
188 { "native_get", "(Ljava/lang/String;)Ljava/lang/String;",
189 (void*) SystemProperties_getS },
190 { "native_get", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
191 (void*) SystemProperties_getSS },
Mike Lockwoodd1945952009-08-12 17:15:51 -0400192 { "native_get_int", "(Ljava/lang/String;I)I",
193 (void*) SystemProperties_get_int },
194 { "native_get_long", "(Ljava/lang/String;J)J",
195 (void*) SystemProperties_get_long },
196 { "native_get_boolean", "(Ljava/lang/String;Z)Z",
197 (void*) SystemProperties_get_boolean },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 { "native_set", "(Ljava/lang/String;Ljava/lang/String;)V",
199 (void*) SystemProperties_set },
200};
201
202int register_android_os_SystemProperties(JNIEnv *env)
203{
204 return AndroidRuntime::registerNativeMethods(
205 env, "android/os/SystemProperties",
206 method_table, NELEM(method_table));
207}
208
209};