blob: 4f6d322ba8710ca62b6266786f99530ae986525b [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
Andreas Gampea90534b2017-07-29 14:14:39 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
John Reckaa67f682016-09-20 14:24:21 -070021import android.util.Log;
22import android.util.MutableInt;
23
24import com.android.internal.annotations.GuardedBy;
25
Dianne Hackborna53de062012-05-08 18:53:51 -070026import java.util.ArrayList;
John Reckaa67f682016-09-20 14:24:21 -070027import java.util.HashMap;
Dianne Hackborna53de062012-05-08 18:53:51 -070028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
30/**
31 * Gives access to the system properties store. The system properties
32 * store contains a list of string key-value pairs.
33 *
34 * {@hide}
35 */
John Reckaa67f682016-09-20 14:24:21 -070036public class SystemProperties {
37 private static final String TAG = "SystemProperties";
38 private static final boolean TRACK_KEY_ACCESS = false;
39
Elliott Hughes70cfefa2017-03-15 13:01:53 -070040 /**
41 * Android O removed the property name length limit, but com.amazon.kindle 7.8.1.5
42 * uses reflection to read this whenever text is selected (http://b/36095274).
43 */
44 public static final int PROP_NAME_MAX = Integer.MAX_VALUE;
45
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 public static final int PROP_VALUE_MAX = 91;
47
Andreas Gampea90534b2017-07-29 14:14:39 -070048 @GuardedBy("sChangeCallbacks")
Dianne Hackborna53de062012-05-08 18:53:51 -070049 private static final ArrayList<Runnable> sChangeCallbacks = new ArrayList<Runnable>();
50
John Reckaa67f682016-09-20 14:24:21 -070051 @GuardedBy("sRoReads")
Andreas Gampea90534b2017-07-29 14:14:39 -070052 private static final HashMap<String, MutableInt> sRoReads =
53 TRACK_KEY_ACCESS ? new HashMap<>() : null;
John Reckaa67f682016-09-20 14:24:21 -070054
55 private static void onKeyAccess(String key) {
56 if (!TRACK_KEY_ACCESS) return;
57
58 if (key != null && key.startsWith("ro.")) {
59 synchronized (sRoReads) {
60 MutableInt numReads = sRoReads.getOrDefault(key, null);
61 if (numReads == null) {
62 numReads = new MutableInt(0);
63 sRoReads.put(key, numReads);
64 }
65 numReads.value++;
66 if (numReads.value > 3) {
67 Log.d(TAG, "Repeated read (count=" + numReads.value
68 + ") of a read-only system property '" + key + "'",
69 new Exception());
70 }
71 }
72 }
73 }
74
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 private static native String native_get(String key);
76 private static native String native_get(String key, String def);
Mike Lockwoodd1945952009-08-12 17:15:51 -040077 private static native int native_get_int(String key, int def);
78 private static native long native_get_long(String key, long def);
79 private static native boolean native_get_boolean(String key, boolean def);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 private static native void native_set(String key, String def);
Dianne Hackborna53de062012-05-08 18:53:51 -070081 private static native void native_add_change_callback();
Martijn Coenen0754b272016-11-17 14:06:38 +010082 private static native void native_report_sysprop_change();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
84 /**
Andreas Gampe33aea8d2017-07-28 18:20:37 -070085 * Get the String value for the given {@code key}.
86 *
Andreas Gampea90534b2017-07-29 14:14:39 -070087 * @param key the key to lookup
Andreas Gampe33aea8d2017-07-28 18:20:37 -070088 * @return an empty string if the {@code key} isn't found
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 */
Andreas Gampea90534b2017-07-29 14:14:39 -070090 @NonNull
91 public static String get(@NonNull String key) {
John Reckaa67f682016-09-20 14:24:21 -070092 if (TRACK_KEY_ACCESS) onKeyAccess(key);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 return native_get(key);
94 }
95
96 /**
Andreas Gampe33aea8d2017-07-28 18:20:37 -070097 * Get the String value for the given {@code key}.
98 *
Andreas Gampea90534b2017-07-29 14:14:39 -070099 * @param key the key to lookup
100 * @param def the default value in case the property is not set or empty
Andreas Gampe33aea8d2017-07-28 18:20:37 -0700101 * @return if the {@code key} isn't found, return {@code def} if it isn't null, or an empty
102 * string otherwise
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 */
Andreas Gampea90534b2017-07-29 14:14:39 -0700104 @NonNull
105 public static String get(@NonNull String key, @Nullable String def) {
John Reckaa67f682016-09-20 14:24:21 -0700106 if (TRACK_KEY_ACCESS) onKeyAccess(key);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 return native_get(key, def);
108 }
109
110 /**
Andreas Gampea90534b2017-07-29 14:14:39 -0700111 * Get the value for the given {@code key}, and return as an integer.
112 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 * @param key the key to lookup
114 * @param def a default value to return
115 * @return the key parsed as an integer, or def if the key isn't found or
116 * cannot be parsed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 */
Andreas Gampea90534b2017-07-29 14:14:39 -0700118 public static int getInt(@NonNull String key, int def) {
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 /**
Andreas Gampea90534b2017-07-29 14:14:39 -0700124 * Get the value for the given {@code key}, and return as a long.
125 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 * @param key the key to lookup
127 * @param def a default value to return
128 * @return the key parsed as a long, or def if the key isn't found or
129 * cannot be parsed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 */
Andreas Gampea90534b2017-07-29 14:14:39 -0700131 public static long getLong(@NonNull String key, long def) {
John Reckaa67f682016-09-20 14:24:21 -0700132 if (TRACK_KEY_ACCESS) onKeyAccess(key);
Mike Lockwoodd1945952009-08-12 17:15:51 -0400133 return native_get_long(key, def);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 }
135
136 /**
Andreas Gampea90534b2017-07-29 14:14:39 -0700137 * Get the value for the given {@code key}, returned as a boolean.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * Values 'n', 'no', '0', 'false' or 'off' are considered false.
139 * Values 'y', 'yes', '1', 'true' or 'on' are considered true.
Brad Fitzpatrickc1a968a2010-11-24 08:56:40 -0800140 * (case sensitive).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 * If the key does not exist, or has any other value, then the default
142 * result is returned.
Andreas Gampea90534b2017-07-29 14:14:39 -0700143 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 * @param key the key to lookup
145 * @param def a default value to return
146 * @return the key parsed as a boolean, or def if the key isn't found or is
147 * not able to be parsed as a boolean.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 */
Andreas Gampea90534b2017-07-29 14:14:39 -0700149 public static boolean getBoolean(@NonNull String key, boolean def) {
John Reckaa67f682016-09-20 14:24:21 -0700150 if (TRACK_KEY_ACCESS) onKeyAccess(key);
Mike Lockwoodd1945952009-08-12 17:15:51 -0400151 return native_get_boolean(key, def);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 }
153
154 /**
Andreas Gampea90534b2017-07-29 14:14:39 -0700155 * Set the value for the given {@code key} to {@code val}.
156 *
157 * @throws IllegalArgumentException if the {@code val} exceeds 91 characters
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 */
Andreas Gampea90534b2017-07-29 14:14:39 -0700159 public static void set(@NonNull String key, @Nullable String val) {
Tom Cherry38a77c42017-10-18 09:25:17 -0700160 if (val != null && !val.startsWith("ro.") && val.length() > PROP_VALUE_MAX) {
Andreas Gampea90534b2017-07-29 14:14:39 -0700161 throw new IllegalArgumentException("value of system property '" + key
162 + "' is longer than " + PROP_VALUE_MAX + " characters: " + val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 }
John Reckaa67f682016-09-20 14:24:21 -0700164 if (TRACK_KEY_ACCESS) onKeyAccess(key);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 native_set(key, val);
166 }
Dianne Hackborna53de062012-05-08 18:53:51 -0700167
Andreas Gampea90534b2017-07-29 14:14:39 -0700168 /**
169 * Add a callback that will be run whenever any system property changes.
170 *
171 * @param callback The {@link Runnable} that should be executed when a system property
172 * changes.
173 */
174 public static void addChangeCallback(@NonNull Runnable callback) {
Dianne Hackborna53de062012-05-08 18:53:51 -0700175 synchronized (sChangeCallbacks) {
176 if (sChangeCallbacks.size() == 0) {
177 native_add_change_callback();
178 }
179 sChangeCallbacks.add(callback);
180 }
181 }
182
Andreas Gampea90534b2017-07-29 14:14:39 -0700183 @SuppressWarnings("unused") // Called from native code.
184 private static void callChangeCallbacks() {
Dianne Hackborna53de062012-05-08 18:53:51 -0700185 synchronized (sChangeCallbacks) {
186 //Log.i("foo", "Calling " + sChangeCallbacks.size() + " change callbacks!");
187 if (sChangeCallbacks.size() == 0) {
188 return;
189 }
190 ArrayList<Runnable> callbacks = new ArrayList<Runnable>(sChangeCallbacks);
191 for (int i=0; i<callbacks.size(); i++) {
192 callbacks.get(i).run();
193 }
194 }
195 }
Martijn Coenen0754b272016-11-17 14:06:38 +0100196
197 /*
198 * Notifies listeners that a system property has changed
199 */
200 public static void reportSyspropChanged() {
201 native_report_sysprop_change();
202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203}