The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.os; |
| 18 | |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
John Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 21 | import android.util.Log; |
| 22 | import android.util.MutableInt; |
| 23 | |
| 24 | import com.android.internal.annotations.GuardedBy; |
| 25 | |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 26 | import java.util.ArrayList; |
John Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 27 | import java.util.HashMap; |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 28 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
| 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 Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 36 | public class SystemProperties { |
| 37 | private static final String TAG = "SystemProperties"; |
| 38 | private static final boolean TRACK_KEY_ACCESS = false; |
| 39 | |
Elliott Hughes | 70cfefa | 2017-03-15 13:01:53 -0700 | [diff] [blame] | 40 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | public static final int PROP_VALUE_MAX = 91; |
| 47 | |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 48 | @GuardedBy("sChangeCallbacks") |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 49 | private static final ArrayList<Runnable> sChangeCallbacks = new ArrayList<Runnable>(); |
| 50 | |
John Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 51 | @GuardedBy("sRoReads") |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 52 | private static final HashMap<String, MutableInt> sRoReads = |
| 53 | TRACK_KEY_ACCESS ? new HashMap<>() : null; |
John Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 54 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | private static native String native_get(String key); |
| 76 | private static native String native_get(String key, String def); |
Mike Lockwood | d194595 | 2009-08-12 17:15:51 -0400 | [diff] [blame] | 77 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | private static native void native_set(String key, String def); |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 81 | private static native void native_add_change_callback(); |
Martijn Coenen | 0754b27 | 2016-11-17 14:06:38 +0100 | [diff] [blame] | 82 | private static native void native_report_sysprop_change(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
Andreas Gampe | 33aea8d | 2017-07-28 18:20:37 -0700 | [diff] [blame] | 85 | * Get the String value for the given {@code key}. |
| 86 | * |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 87 | * @param key the key to lookup |
Andreas Gampe | 33aea8d | 2017-07-28 18:20:37 -0700 | [diff] [blame] | 88 | * @return an empty string if the {@code key} isn't found |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | */ |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 90 | @NonNull |
| 91 | public static String get(@NonNull String key) { |
John Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 92 | if (TRACK_KEY_ACCESS) onKeyAccess(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | return native_get(key); |
| 94 | } |
| 95 | |
| 96 | /** |
Andreas Gampe | 33aea8d | 2017-07-28 18:20:37 -0700 | [diff] [blame] | 97 | * Get the String value for the given {@code key}. |
| 98 | * |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 99 | * @param key the key to lookup |
| 100 | * @param def the default value in case the property is not set or empty |
Andreas Gampe | 33aea8d | 2017-07-28 18:20:37 -0700 | [diff] [blame] | 101 | * @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | */ |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 104 | @NonNull |
| 105 | public static String get(@NonNull String key, @Nullable String def) { |
John Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 106 | if (TRACK_KEY_ACCESS) onKeyAccess(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | return native_get(key, def); |
| 108 | } |
| 109 | |
| 110 | /** |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 111 | * Get the value for the given {@code key}, and return as an integer. |
| 112 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | * @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | */ |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 118 | public static int getInt(@NonNull String key, int def) { |
John Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 119 | if (TRACK_KEY_ACCESS) onKeyAccess(key); |
Mike Lockwood | d194595 | 2009-08-12 17:15:51 -0400 | [diff] [blame] | 120 | return native_get_int(key, def); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 124 | * Get the value for the given {@code key}, and return as a long. |
| 125 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | * @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | */ |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 131 | public static long getLong(@NonNull String key, long def) { |
John Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 132 | if (TRACK_KEY_ACCESS) onKeyAccess(key); |
Mike Lockwood | d194595 | 2009-08-12 17:15:51 -0400 | [diff] [blame] | 133 | return native_get_long(key, def); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 137 | * Get the value for the given {@code key}, returned as a boolean. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | * Values 'n', 'no', '0', 'false' or 'off' are considered false. |
| 139 | * Values 'y', 'yes', '1', 'true' or 'on' are considered true. |
Brad Fitzpatrick | c1a968a | 2010-11-24 08:56:40 -0800 | [diff] [blame] | 140 | * (case sensitive). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | * If the key does not exist, or has any other value, then the default |
| 142 | * result is returned. |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 143 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | * @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | */ |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 149 | public static boolean getBoolean(@NonNull String key, boolean def) { |
John Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 150 | if (TRACK_KEY_ACCESS) onKeyAccess(key); |
Mike Lockwood | d194595 | 2009-08-12 17:15:51 -0400 | [diff] [blame] | 151 | return native_get_boolean(key, def); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 155 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | */ |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 159 | public static void set(@NonNull String key, @Nullable String val) { |
Tom Cherry | 38a77c4 | 2017-10-18 09:25:17 -0700 | [diff] [blame^] | 160 | if (val != null && !val.startsWith("ro.") && val.length() > PROP_VALUE_MAX) { |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 161 | throw new IllegalArgumentException("value of system property '" + key |
| 162 | + "' is longer than " + PROP_VALUE_MAX + " characters: " + val); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | } |
John Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 164 | if (TRACK_KEY_ACCESS) onKeyAccess(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | native_set(key, val); |
| 166 | } |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 167 | |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 168 | /** |
| 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 Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 175 | synchronized (sChangeCallbacks) { |
| 176 | if (sChangeCallbacks.size() == 0) { |
| 177 | native_add_change_callback(); |
| 178 | } |
| 179 | sChangeCallbacks.add(callback); |
| 180 | } |
| 181 | } |
| 182 | |
Andreas Gampe | a90534b | 2017-07-29 14:14:39 -0700 | [diff] [blame] | 183 | @SuppressWarnings("unused") // Called from native code. |
| 184 | private static void callChangeCallbacks() { |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 185 | 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 Coenen | 0754b27 | 2016-11-17 14:06:38 +0100 | [diff] [blame] | 196 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | } |