blob: e47c2380ca8313705c6a8335e330e8cc7bb5c6d9 [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();
Martijn Coenen0754b272016-11-17 14:06:38 +010042 private static native void native_report_sysprop_change();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44 /**
45 * Get the value for the given key.
46 * @return an empty string if the key isn't found
47 * @throws IllegalArgumentException if the key exceeds 32 characters
48 */
49 public static String get(String key) {
50 if (key.length() > PROP_NAME_MAX) {
51 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
52 }
53 return native_get(key);
54 }
55
56 /**
57 * Get the value for the given key.
58 * @return if the key isn't found, return def if it isn't null, or an empty string otherwise
59 * @throws IllegalArgumentException if the key exceeds 32 characters
60 */
61 public static String get(String key, String def) {
62 if (key.length() > PROP_NAME_MAX) {
63 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
64 }
65 return native_get(key, def);
66 }
67
68 /**
69 * Get the value for the given key, and return as an integer.
70 * @param key the key to lookup
71 * @param def a default value to return
72 * @return the key parsed as an integer, or def if the key isn't found or
73 * cannot be parsed
74 * @throws IllegalArgumentException if the key exceeds 32 characters
75 */
76 public static int getInt(String key, int def) {
Mike Lockwoodd1945952009-08-12 17:15:51 -040077 if (key.length() > PROP_NAME_MAX) {
78 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 }
Mike Lockwoodd1945952009-08-12 17:15:51 -040080 return native_get_int(key, def);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 }
82
83 /**
84 * Get the value for the given key, and return as a long.
85 * @param key the key to lookup
86 * @param def a default value to return
87 * @return the key parsed as a long, or def if the key isn't found or
88 * cannot be parsed
89 * @throws IllegalArgumentException if the key exceeds 32 characters
90 */
91 public static long getLong(String key, long def) {
Mike Lockwoodd1945952009-08-12 17:15:51 -040092 if (key.length() > PROP_NAME_MAX) {
93 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 }
Mike Lockwoodd1945952009-08-12 17:15:51 -040095 return native_get_long(key, def);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 }
97
98 /**
99 * Get the value for the given key, returned as a boolean.
100 * Values 'n', 'no', '0', 'false' or 'off' are considered false.
101 * Values 'y', 'yes', '1', 'true' or 'on' are considered true.
Brad Fitzpatrickc1a968a2010-11-24 08:56:40 -0800102 * (case sensitive).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * If the key does not exist, or has any other value, then the default
104 * result is returned.
105 * @param key the key to lookup
106 * @param def a default value to return
107 * @return the key parsed as a boolean, or def if the key isn't found or is
108 * not able to be parsed as a boolean.
109 * @throws IllegalArgumentException if the key exceeds 32 characters
110 */
111 public static boolean getBoolean(String key, boolean def) {
Mike Lockwoodd1945952009-08-12 17:15:51 -0400112 if (key.length() > PROP_NAME_MAX) {
113 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 }
Mike Lockwoodd1945952009-08-12 17:15:51 -0400115 return native_get_boolean(key, def);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 }
117
118 /**
119 * Set the value for the given key.
120 * @throws IllegalArgumentException if the key exceeds 32 characters
121 * @throws IllegalArgumentException if the value exceeds 92 characters
122 */
123 public static void set(String key, String val) {
124 if (key.length() > PROP_NAME_MAX) {
125 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
126 }
127 if (val != null && val.length() > PROP_VALUE_MAX) {
128 throw new IllegalArgumentException("val.length > " +
129 PROP_VALUE_MAX);
130 }
131 native_set(key, val);
132 }
Dianne Hackborna53de062012-05-08 18:53:51 -0700133
134 public static void addChangeCallback(Runnable callback) {
135 synchronized (sChangeCallbacks) {
136 if (sChangeCallbacks.size() == 0) {
137 native_add_change_callback();
138 }
139 sChangeCallbacks.add(callback);
140 }
141 }
142
143 static void callChangeCallbacks() {
144 synchronized (sChangeCallbacks) {
145 //Log.i("foo", "Calling " + sChangeCallbacks.size() + " change callbacks!");
146 if (sChangeCallbacks.size() == 0) {
147 return;
148 }
149 ArrayList<Runnable> callbacks = new ArrayList<Runnable>(sChangeCallbacks);
150 for (int i=0; i<callbacks.size(); i++) {
151 callbacks.get(i).run();
152 }
153 }
154 }
Martijn Coenen0754b272016-11-17 14:06:38 +0100155
156 /*
157 * Notifies listeners that a system property has changed
158 */
159 public static void reportSyspropChanged() {
160 native_report_sysprop_change();
161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162}