2 * Copyright (C) 2006 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import java.util.ArrayList;
23 * Gives access to the system properties store. The system properties
24 * store contains a list of string key-value pairs.
28 public class SystemProperties
30 public static final int PROP_NAME_MAX = 31;
31 public static final int PROP_VALUE_MAX = 91;
33 private static final ArrayList<Runnable> sChangeCallbacks = new ArrayList<Runnable>();
35 private static native String native_get(String key);
36 private static native String native_get(String key, String def);
37 private static native int native_get_int(String key, int def);
38 private static native long native_get_long(String key, long def);
39 private static native boolean native_get_boolean(String key, boolean def);
40 private static native void native_set(String key, String def);
41 private static native void native_add_change_callback();
44 * Get the value for the given key.
45 * @return an empty string if the key isn't found
46 * @throws IllegalArgumentException if the key exceeds 32 characters
48 public static String get(String key) {
49 if (key.length() > PROP_NAME_MAX) {
50 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
52 return native_get(key);
56 * Get the value for the given key.
57 * @return if the key isn't found, return def if it isn't null, or an empty string otherwise
58 * @throws IllegalArgumentException if the key exceeds 32 characters
60 public static String get(String key, String def) {
61 if (key.length() > PROP_NAME_MAX) {
62 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
64 return native_get(key, def);
68 * Get the value for the given key, and return as an integer.
69 * @param key the key to lookup
70 * @param def a default value to return
71 * @return the key parsed as an integer, or def if the key isn't found or
73 * @throws IllegalArgumentException if the key exceeds 32 characters
75 public static int getInt(String key, int def) {
76 if (key.length() > PROP_NAME_MAX) {
77 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
79 return native_get_int(key, def);
83 * Get the value for the given key, and return as a long.
84 * @param key the key to lookup
85 * @param def a default value to return
86 * @return the key parsed as a long, or def if the key isn't found or
88 * @throws IllegalArgumentException if the key exceeds 32 characters
90 public static long getLong(String key, long def) {
91 if (key.length() > PROP_NAME_MAX) {
92 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
94 return native_get_long(key, def);
98 * Get the value for the given key, returned as a boolean.
99 * Values 'n', 'no', '0', 'false' or 'off' are considered false.
100 * Values 'y', 'yes', '1', 'true' or 'on' are considered true.
102 * If the key does not exist, or has any other value, then the default
103 * result is returned.
104 * @param key the key to lookup
105 * @param def a default value to return
106 * @return the key parsed as a boolean, or def if the key isn't found or is
107 * not able to be parsed as a boolean.
108 * @throws IllegalArgumentException if the key exceeds 32 characters
110 public static boolean getBoolean(String key, boolean def) {
111 if (key.length() > PROP_NAME_MAX) {
112 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
114 return native_get_boolean(key, def);
118 * Set the value for the given key.
119 * @throws IllegalArgumentException if the key exceeds 32 characters
120 * @throws IllegalArgumentException if the value exceeds 92 characters
122 public static void set(String key, String val) {
123 if (key.length() > PROP_NAME_MAX) {
124 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
126 if (val != null && val.length() > PROP_VALUE_MAX) {
127 throw new IllegalArgumentException("val.length > " +
130 native_set(key, val);
133 public static void addChangeCallback(Runnable callback) {
134 synchronized (sChangeCallbacks) {
135 if (sChangeCallbacks.size() == 0) {
136 native_add_change_callback();
138 sChangeCallbacks.add(callback);
142 static void callChangeCallbacks() {
143 synchronized (sChangeCallbacks) {
144 //Log.i("foo", "Calling " + sChangeCallbacks.size() + " change callbacks!");
145 if (sChangeCallbacks.size() == 0) {
148 ArrayList<Runnable> callbacks = new ArrayList<Runnable>(sChangeCallbacks);
149 for (int i=0; i<callbacks.size(); i++) {
150 callbacks.get(i).run();