• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* //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 
23 namespace android
24 {
25 
SystemProperties_getSS(JNIEnv * env,jobject clazz,jstring keyJ,jstring defJ)26 static 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 
53 error:
54     return rvJ;
55 }
56 
SystemProperties_getS(JNIEnv * env,jobject clazz,jstring keyJ)57 static jstring SystemProperties_getS(JNIEnv *env, jobject clazz,
58                                       jstring keyJ)
59 {
60     return SystemProperties_getSS(env, clazz, keyJ, NULL);
61 }
62 
SystemProperties_get_int(JNIEnv * env,jobject clazz,jstring keyJ,jint defJ)63 static jint SystemProperties_get_int(JNIEnv *env, jobject clazz,
64                                       jstring keyJ, jint defJ)
65 {
66     int len;
67     const char* key;
68     char buf[PROPERTY_VALUE_MAX];
69     jint result = defJ;
70 
71     if (keyJ == NULL) {
72         jniThrowException(env, "java/lang/NullPointerException",
73                                 "key must not be null.");
74         goto error;
75     }
76 
77     key = env->GetStringUTFChars(keyJ, NULL);
78 
79     len = property_get(key, buf, "");
80     if (len > 0) {
81         jint temp;
82         if (sscanf(buf, "%d", &temp) == 1)
83             result = temp;
84     }
85 
86     env->ReleaseStringUTFChars(keyJ, key);
87 
88 error:
89     return result;
90 }
91 
SystemProperties_get_long(JNIEnv * env,jobject clazz,jstring keyJ,jlong defJ)92 static jlong SystemProperties_get_long(JNIEnv *env, jobject clazz,
93                                       jstring keyJ, jlong defJ)
94 {
95     int len;
96     const char* key;
97     char buf[PROPERTY_VALUE_MAX];
98     jlong result = defJ;
99 
100     if (keyJ == NULL) {
101         jniThrowException(env, "java/lang/NullPointerException",
102                                 "key must not be null.");
103         goto error;
104     }
105 
106     key = env->GetStringUTFChars(keyJ, NULL);
107 
108     len = property_get(key, buf, "");
109     if (len > 0) {
110         jlong temp;
111         if (sscanf(buf, "%lld", &temp) == 1)
112             result = temp;
113     }
114 
115     env->ReleaseStringUTFChars(keyJ, key);
116 
117 error:
118     return result;
119 }
120 
SystemProperties_get_boolean(JNIEnv * env,jobject clazz,jstring keyJ,jboolean defJ)121 static jboolean SystemProperties_get_boolean(JNIEnv *env, jobject clazz,
122                                       jstring keyJ, jboolean defJ)
123 {
124     int len;
125     const char* key;
126     char buf[PROPERTY_VALUE_MAX];
127     jboolean result = defJ;
128 
129     if (keyJ == NULL) {
130         jniThrowException(env, "java/lang/NullPointerException",
131                                 "key must not be null.");
132         goto error;
133     }
134 
135     key = env->GetStringUTFChars(keyJ, NULL);
136 
137     len = property_get(key, buf, "");
138     if (len == 1) {
139         char ch = buf[0];
140         if (ch == '0' || ch == 'n')
141             result = false;
142         else if (ch == '1' || ch == 'y')
143             result = true;
144     } else if (len > 1) {
145          if (!strcmp(buf, "no") || !strcmp(buf, "false") || !strcmp(buf, "off")) {
146             result = false;
147         } else if (!strcmp(buf, "yes") || !strcmp(buf, "true") || !strcmp(buf, "on")) {
148             result = true;
149         }
150     }
151 
152     env->ReleaseStringUTFChars(keyJ, key);
153 
154 error:
155     return result;
156 }
157 
SystemProperties_set(JNIEnv * env,jobject clazz,jstring keyJ,jstring valJ)158 static void SystemProperties_set(JNIEnv *env, jobject clazz,
159                                       jstring keyJ, jstring valJ)
160 {
161     int err;
162     const char* key;
163     const char* val;
164 
165     if (keyJ == NULL) {
166         jniThrowException(env, "java/lang/NullPointerException",
167                                 "key must not be null.");
168         return ;
169     }
170     key = env->GetStringUTFChars(keyJ, NULL);
171 
172     if (valJ == NULL) {
173         val = "";       /* NULL pointer not allowed here */
174     } else {
175         val = env->GetStringUTFChars(valJ, NULL);
176     }
177 
178     err = property_set(key, val);
179 
180     env->ReleaseStringUTFChars(keyJ, key);
181 
182     if (valJ != NULL) {
183     	env->ReleaseStringUTFChars(valJ, val);
184     }
185 }
186 
187 static 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 },
192     { "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 },
198     { "native_set", "(Ljava/lang/String;Ljava/lang/String;)V",
199       (void*) SystemProperties_set },
200 };
201 
register_android_os_SystemProperties(JNIEnv * env)202 int 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 };
210