blob: 1479035df978bd695fd0494511f51773fcad4d44 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Dianne Hackborna53de062012-05-08 18:53:51 -070019import java.util.ArrayList;
20
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
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 */
28public class SystemProperties
29{
30 public static final int PROP_NAME_MAX = 31;
31 public static final int PROP_VALUE_MAX = 91;
32
Dianne Hackborna53de062012-05-08 18:53:51 -070033 private static final ArrayList<Runnable> sChangeCallbacks = new ArrayList<Runnable>();
34
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035 private static native String native_get(String key);
36 private static native String native_get(String key, String def);
Mike Lockwoodd1945952009-08-12 17:15:51 -040037 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 private static native void native_set(String key, String def);
Dianne Hackborna53de062012-05-08 18:53:51 -070041 private static native void native_add_change_callback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
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 */
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 */
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 */
75 public static int getInt(String key, int def) {
Mike Lockwoodd1945952009-08-12 17:15:51 -040076 if (key.length() > PROP_NAME_MAX) {
77 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 }
Mike Lockwoodd1945952009-08-12 17:15:51 -040079 return native_get_int(key, def);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 }
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 */
90 public static long getLong(String key, long def) {
Mike Lockwoodd1945952009-08-12 17:15:51 -040091 if (key.length() > PROP_NAME_MAX) {
92 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 }
Mike Lockwoodd1945952009-08-12 17:15:51 -040094 return native_get_long(key, def);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 }
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.
Brad Fitzpatrickc1a968a2010-11-24 08:56:40 -0800101 * (case sensitive).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * 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 */
110 public static boolean getBoolean(String key, boolean def) {
Mike Lockwoodd1945952009-08-12 17:15:51 -0400111 if (key.length() > PROP_NAME_MAX) {
112 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 }
Mike Lockwoodd1945952009-08-12 17:15:51 -0400114 return native_get_boolean(key, def);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 }
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 */
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 }
Dianne Hackborna53de062012-05-08 18:53:51 -0700132
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
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 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154}