1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 package android.os;
18 
19 import java.util.ArrayList;
20 
21 
22 /**
23  * Gives access to the system properties store.  The system properties
24  * store contains a list of string key-value pairs.
25  *
26  * {@hide}
27  */
28 public class SystemProperties
29 {
30     public static final int PROP_NAME_MAX = 31;
31     public static final int PROP_VALUE_MAX = 91;
32 
33     private static final ArrayList<Runnable> sChangeCallbacks = new ArrayList<Runnable>();
34 
native_get(String key)35     private static native String native_get(String key);
native_get(String key, String def)36     private static native String native_get(String key, String def);
native_get_int(String key, int def)37     private static native int native_get_int(String key, int def);
native_get_long(String key, long def)38     private static native long native_get_long(String key, long def);
native_get_boolean(String key, boolean def)39     private static native boolean native_get_boolean(String key, boolean def);
native_set(String key, String def)40     private static native void native_set(String key, String def);
native_add_change_callback()41     private static native void native_add_change_callback();
42 
43     /**
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
47      */
get(String key)48     public static String get(String key) {
49         if (key.length() > PROP_NAME_MAX) {
50             throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
51         }
52         return native_get(key);
53     }
54 
55     /**
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
59      */
get(String key, String def)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);
63         }
64         return native_get(key, def);
65     }
66 
67     /**
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
72      *         cannot be parsed
73      * @throws IllegalArgumentException if the key exceeds 32 characters
74      */
getInt(String key, int def)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);
78         }
79         return native_get_int(key, def);
80     }
81 
82     /**
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
87      *         cannot be parsed
88      * @throws IllegalArgumentException if the key exceeds 32 characters
89      */
getLong(String key, long def)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);
93         }
94         return native_get_long(key, def);
95     }
96 
97     /**
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.
101      * (case sensitive).
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
109      */
getBoolean(String key, boolean def)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);
113         }
114         return native_get_boolean(key, def);
115     }
116 
117     /**
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
121      */
set(String key, String val)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);
125         }
126         if (val != null && val.length() > PROP_VALUE_MAX) {
127             throw new IllegalArgumentException("val.length > " +
128                 PROP_VALUE_MAX);
129         }
130         native_set(key, val);
131     }
132 
addChangeCallback(Runnable callback)133     public static void addChangeCallback(Runnable callback) {
134         synchronized (sChangeCallbacks) {
135             if (sChangeCallbacks.size() == 0) {
136                 native_add_change_callback();
137             }
138             sChangeCallbacks.add(callback);
139         }
140     }
141 
callChangeCallbacks()142     static void callChangeCallbacks() {
143         synchronized (sChangeCallbacks) {
144             //Log.i("foo", "Calling " + sChangeCallbacks.size() + " change callbacks!");
145             if (sChangeCallbacks.size() == 0) {
146                 return;
147             }
148             ArrayList<Runnable> callbacks = new ArrayList<Runnable>(sChangeCallbacks);
149             for (int i=0; i<callbacks.size(); i++) {
150                 callbacks.get(i).run();
151             }
152         }
153     }
154 }
155