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 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 jniThrowNullPointerException(env, "key must not be null."); 36 goto error; 37 } 38 39 key = env->GetStringUTFChars(keyJ, NULL); 40 41 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 } 49 50 env->ReleaseStringUTFChars(keyJ, key); 51 52 error: 53 return rvJ; 54 } 55 56 static jstring SystemProperties_getS(JNIEnv *env, jobject clazz, 57 jstring keyJ) 58 { 59 return SystemProperties_getSS(env, clazz, keyJ, NULL); 60 } 61 62 static 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) { 71 jniThrowNullPointerException(env, "key must not be null."); 72 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 86 error: 87 return result; 88 } 89 90 static 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) { 99 jniThrowNullPointerException(env, "key must not be null."); 100 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 114 error: 115 return result; 116 } 117 118 static 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) { 127 jniThrowNullPointerException(env, "key must not be null."); 128 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 150 error: 151 return result; 152 } 153 154 static 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) { 162 jniThrowNullPointerException(env, "key must not be null."); 163 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 } 172 173 err = property_set(key, val); 174 175 env->ReleaseStringUTFChars(keyJ, key); 176 177 if (valJ != NULL) { 178 env->ReleaseStringUTFChars(valJ, val); 179 } 180 181 if (err < 0) { 182 jniThrowException(env, "java/lang/RuntimeException", 183 "failed to set system property"); 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 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