blob: 156600e378ed186414ea545e49259f4bde01e21d [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
21import android.util.Log;
22
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
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
Dianne Hackborna53de062012-05-08 18:53:51 -070035 private static final ArrayList<Runnable> sChangeCallbacks = new ArrayList<Runnable>();
36
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 private static native String native_get(String key);
38 private static native String native_get(String key, String def);
Mike Lockwoodd1945952009-08-12 17:15:51 -040039 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 private static native void native_set(String key, String def);
Dianne Hackborna53de062012-05-08 18:53:51 -070043 private static native void native_add_change_callback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
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) {
Mike Lockwoodd1945952009-08-12 17:15:51 -040078 if (key.length() > PROP_NAME_MAX) {
79 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 }
Mike Lockwoodd1945952009-08-12 17:15:51 -040081 return native_get_int(key, def);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 }
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) {
Mike Lockwoodd1945952009-08-12 17:15:51 -040093 if (key.length() > PROP_NAME_MAX) {
94 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 }
Mike Lockwoodd1945952009-08-12 17:15:51 -040096 return native_get_long(key, def);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 }
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.
Brad Fitzpatrickc1a968a2010-11-24 08:56:40 -0800103 * (case sensitive).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 * 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) {
Mike Lockwoodd1945952009-08-12 17:15:51 -0400113 if (key.length() > PROP_NAME_MAX) {
114 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 }
Mike Lockwoodd1945952009-08-12 17:15:51 -0400116 return native_get_boolean(key, def);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 }
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 }
Dianne Hackborna53de062012-05-08 18:53:51 -0700134
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 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156}