blob: d31036c532c7b3d280202a6e175cf0f1f25fd848 [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
John Reckaa67f682016-09-20 14:24:21 -070019import android.util.Log;
20import android.util.MutableInt;
21
22import com.android.internal.annotations.GuardedBy;
23
Dianne Hackborna53de062012-05-08 18:53:51 -070024import java.util.ArrayList;
John Reckaa67f682016-09-20 14:24:21 -070025import java.util.HashMap;
Dianne Hackborna53de062012-05-08 18:53:51 -070026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
28/**
29 * Gives access to the system properties store. The system properties
30 * store contains a list of string key-value pairs.
31 *
32 * {@hide}
33 */
John Reckaa67f682016-09-20 14:24:21 -070034public class SystemProperties {
35 private static final String TAG = "SystemProperties";
36 private static final boolean TRACK_KEY_ACCESS = false;
37
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 public static final int PROP_NAME_MAX = 31;
39 public static final int PROP_VALUE_MAX = 91;
40
Dianne Hackborna53de062012-05-08 18:53:51 -070041 private static final ArrayList<Runnable> sChangeCallbacks = new ArrayList<Runnable>();
42
John Reckaa67f682016-09-20 14:24:21 -070043 @GuardedBy("sRoReads")
44 private static final HashMap<String, MutableInt> sRoReads;
45 static {
46 if (TRACK_KEY_ACCESS) {
47 sRoReads = new HashMap<>();
48 } else {
49 sRoReads = null;
50 }
51 }
52
53 private static void onKeyAccess(String key) {
54 if (!TRACK_KEY_ACCESS) return;
55
56 if (key != null && key.startsWith("ro.")) {
57 synchronized (sRoReads) {
58 MutableInt numReads = sRoReads.getOrDefault(key, null);
59 if (numReads == null) {
60 numReads = new MutableInt(0);
61 sRoReads.put(key, numReads);
62 }
63 numReads.value++;
64 if (numReads.value > 3) {
65 Log.d(TAG, "Repeated read (count=" + numReads.value
66 + ") of a read-only system property '" + key + "'",
67 new Exception());
68 }
69 }
70 }
71 }
72
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 private static native String native_get(String key);
74 private static native String native_get(String key, String def);
Mike Lockwoodd1945952009-08-12 17:15:51 -040075 private static native int native_get_int(String key, int def);
76 private static native long native_get_long(String key, long def);
77 private static native boolean native_get_boolean(String key, boolean def);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 private static native void native_set(String key, String def);
Dianne Hackborna53de062012-05-08 18:53:51 -070079 private static native void native_add_change_callback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
81 /**
82 * Get the value for the given key.
83 * @return an empty string if the key isn't found
84 * @throws IllegalArgumentException if the key exceeds 32 characters
85 */
86 public static String get(String key) {
87 if (key.length() > PROP_NAME_MAX) {
88 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
89 }
John Reckaa67f682016-09-20 14:24:21 -070090 if (TRACK_KEY_ACCESS) onKeyAccess(key);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 return native_get(key);
92 }
93
94 /**
95 * Get the value for the given key.
96 * @return if the key isn't found, return def if it isn't null, or an empty string otherwise
97 * @throws IllegalArgumentException if the key exceeds 32 characters
98 */
99 public static String get(String key, String def) {
100 if (key.length() > PROP_NAME_MAX) {
101 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
102 }
John Reckaa67f682016-09-20 14:24:21 -0700103 if (TRACK_KEY_ACCESS) onKeyAccess(key);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 return native_get(key, def);
105 }
106
107 /**
108 * Get the value for the given key, and return as an integer.
109 * @param key the key to lookup
110 * @param def a default value to return
111 * @return the key parsed as an integer, or def if the key isn't found or
112 * cannot be parsed
113 * @throws IllegalArgumentException if the key exceeds 32 characters
114 */
115 public static int getInt(String key, int def) {
Mike Lockwoodd1945952009-08-12 17:15:51 -0400116 if (key.length() > PROP_NAME_MAX) {
117 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 }
John Reckaa67f682016-09-20 14:24:21 -0700119 if (TRACK_KEY_ACCESS) onKeyAccess(key);
Mike Lockwoodd1945952009-08-12 17:15:51 -0400120 return native_get_int(key, def);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 }
122
123 /**
124 * Get the value for the given key, and return as a long.
125 * @param key the key to lookup
126 * @param def a default value to return
127 * @return the key parsed as a long, or def if the key isn't found or
128 * cannot be parsed
129 * @throws IllegalArgumentException if the key exceeds 32 characters
130 */
131 public static long getLong(String key, long def) {
Mike Lockwoodd1945952009-08-12 17:15:51 -0400132 if (key.length() > PROP_NAME_MAX) {
133 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 }
John Reckaa67f682016-09-20 14:24:21 -0700135 if (TRACK_KEY_ACCESS) onKeyAccess(key);
Mike Lockwoodd1945952009-08-12 17:15:51 -0400136 return native_get_long(key, def);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 }
138
139 /**
140 * Get the value for the given key, returned as a boolean.
141 * Values 'n', 'no', '0', 'false' or 'off' are considered false.
142 * Values 'y', 'yes', '1', 'true' or 'on' are considered true.
Brad Fitzpatrickc1a968a2010-11-24 08:56:40 -0800143 * (case sensitive).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 * If the key does not exist, or has any other value, then the default
145 * result is returned.
146 * @param key the key to lookup
147 * @param def a default value to return
148 * @return the key parsed as a boolean, or def if the key isn't found or is
149 * not able to be parsed as a boolean.
150 * @throws IllegalArgumentException if the key exceeds 32 characters
151 */
152 public static boolean getBoolean(String key, boolean def) {
Mike Lockwoodd1945952009-08-12 17:15:51 -0400153 if (key.length() > PROP_NAME_MAX) {
154 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 }
John Reckaa67f682016-09-20 14:24:21 -0700156 if (TRACK_KEY_ACCESS) onKeyAccess(key);
Mike Lockwoodd1945952009-08-12 17:15:51 -0400157 return native_get_boolean(key, def);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 }
159
160 /**
161 * Set the value for the given key.
162 * @throws IllegalArgumentException if the key exceeds 32 characters
163 * @throws IllegalArgumentException if the value exceeds 92 characters
164 */
165 public static void set(String key, String val) {
166 if (key.length() > PROP_NAME_MAX) {
167 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
168 }
169 if (val != null && val.length() > PROP_VALUE_MAX) {
170 throw new IllegalArgumentException("val.length > " +
171 PROP_VALUE_MAX);
172 }
John Reckaa67f682016-09-20 14:24:21 -0700173 if (TRACK_KEY_ACCESS) onKeyAccess(key);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 native_set(key, val);
175 }
Dianne Hackborna53de062012-05-08 18:53:51 -0700176
177 public static void addChangeCallback(Runnable callback) {
178 synchronized (sChangeCallbacks) {
179 if (sChangeCallbacks.size() == 0) {
180 native_add_change_callback();
181 }
182 sChangeCallbacks.add(callback);
183 }
184 }
185
186 static void callChangeCallbacks() {
187 synchronized (sChangeCallbacks) {
188 //Log.i("foo", "Calling " + sChangeCallbacks.size() + " change callbacks!");
189 if (sChangeCallbacks.size() == 0) {
190 return;
191 }
192 ArrayList<Runnable> callbacks = new ArrayList<Runnable>(sChangeCallbacks);
193 for (int i=0; i<callbacks.size(); i++) {
194 callbacks.get(i).run();
195 }
196 }
197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198}