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
17package android.os;
18
19import java.util.ArrayList;
20
21import android.util.Log;
22
23
24/**
25 * Gives access to the system properties store.  The system properties
26 * store contains a list of string key-value pairs.
27 *
28 * {@hide}
29 */
30public class SystemProperties
31{
32    public static final int PROP_NAME_MAX = 31;
33    public static final int PROP_VALUE_MAX = 91;
34
35    private static final ArrayList<Runnable> sChangeCallbacks = new ArrayList<Runnable>();
36
37    private static native String native_get(String key);
38    private static native String native_get(String key, String def);
39    private static native int native_get_int(String key, int def);
40    private static native long native_get_long(String key, long def);
41    private static native boolean native_get_boolean(String key, boolean def);
42    private static native void native_set(String key, String def);
43    private static native void native_add_change_callback();
44
45    /**
46     * Get the value for the given key.
47     * @return an empty string if the key isn't found
48     * @throws IllegalArgumentException if the key exceeds 32 characters
49     */
50    public static String get(String key) {
51        if (key.length() > PROP_NAME_MAX) {
52            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
53        }
54        return native_get(key);
55    }
56
57    /**
58     * Get the value for the given key.
59     * @return if the key isn't found, return def if it isn't null, or an empty string otherwise
60     * @throws IllegalArgumentException if the key exceeds 32 characters
61     */
62    public static String get(String key, String def) {
63        if (key.length() > PROP_NAME_MAX) {
64            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
65        }
66        return native_get(key, def);
67    }
68
69    /**
70     * Get the value for the given key, and return as an integer.
71     * @param key the key to lookup
72     * @param def a default value to return
73     * @return the key parsed as an integer, or def if the key isn't found or
74     *         cannot be parsed
75     * @throws IllegalArgumentException if the key exceeds 32 characters
76     */
77    public static int getInt(String key, int def) {
78        if (key.length() > PROP_NAME_MAX) {
79            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
80        }
81        return native_get_int(key, def);
82    }
83
84    /**
85     * Get the value for the given key, and return as a long.
86     * @param key the key to lookup
87     * @param def a default value to return
88     * @return the key parsed as a long, or def if the key isn't found or
89     *         cannot be parsed
90     * @throws IllegalArgumentException if the key exceeds 32 characters
91     */
92    public static long getLong(String key, long def) {
93        if (key.length() > PROP_NAME_MAX) {
94            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
95        }
96        return native_get_long(key, def);
97    }
98
99    /**
100     * Get the value for the given key, returned as a boolean.
101     * Values 'n', 'no', '0', 'false' or 'off' are considered false.
102     * Values 'y', 'yes', '1', 'true' or 'on' are considered true.
103     * (case sensitive).
104     * If the key does not exist, or has any other value, then the default
105     * result is returned.
106     * @param key the key to lookup
107     * @param def a default value to return
108     * @return the key parsed as a boolean, or def if the key isn't found or is
109     *         not able to be parsed as a boolean.
110     * @throws IllegalArgumentException if the key exceeds 32 characters
111     */
112    public static boolean getBoolean(String key, boolean def) {
113        if (key.length() > PROP_NAME_MAX) {
114            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
115        }
116        return native_get_boolean(key, def);
117    }
118
119    /**
120     * Set the value for the given key.
121     * @throws IllegalArgumentException if the key exceeds 32 characters
122     * @throws IllegalArgumentException if the value exceeds 92 characters
123     */
124    public static void set(String key, String val) {
125        if (key.length() > PROP_NAME_MAX) {
126            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
127        }
128        if (val != null && val.length() > PROP_VALUE_MAX) {
129            throw new IllegalArgumentException("val.length > " +
130                PROP_VALUE_MAX);
131        }
132        native_set(key, val);
133    }
134
135    public static void addChangeCallback(Runnable callback) {
136        synchronized (sChangeCallbacks) {
137            if (sChangeCallbacks.size() == 0) {
138                native_add_change_callback();
139            }
140            sChangeCallbacks.add(callback);
141        }
142    }
143
144    static void callChangeCallbacks() {
145        synchronized (sChangeCallbacks) {
146            //Log.i("foo", "Calling " + sChangeCallbacks.size() + " change callbacks!");
147            if (sChangeCallbacks.size() == 0) {
148                return;
149            }
150            ArrayList<Runnable> callbacks = new ArrayList<Runnable>(sChangeCallbacks);
151            for (int i=0; i<callbacks.size(); i++) {
152                callbacks.get(i).run();
153            }
154        }
155    }
156}
157