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 | |
John Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 19 | import android.util.Log; |
| 20 | import android.util.MutableInt; |
| 21 | |
| 22 | import com.android.internal.annotations.GuardedBy; |
| 23 | |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 24 | import java.util.ArrayList; |
John Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 25 | import java.util.HashMap; |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 26 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
| 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 Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 34 | public class SystemProperties { |
| 35 | private static final String TAG = "SystemProperties"; |
| 36 | private static final boolean TRACK_KEY_ACCESS = false; |
| 37 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | public static final int PROP_NAME_MAX = 31; |
| 39 | public static final int PROP_VALUE_MAX = 91; |
| 40 | |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 41 | private static final ArrayList<Runnable> sChangeCallbacks = new ArrayList<Runnable>(); |
| 42 | |
John Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 43 | @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | private static native String native_get(String key); |
| 74 | private static native String native_get(String key, String def); |
Mike Lockwood | d194595 | 2009-08-12 17:15:51 -0400 | [diff] [blame] | 75 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | private static native void native_set(String key, String def); |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 79 | private static native void native_add_change_callback(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | |
| 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 Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 90 | if (TRACK_KEY_ACCESS) onKeyAccess(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | 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 Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 103 | if (TRACK_KEY_ACCESS) onKeyAccess(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | 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 Lockwood | d194595 | 2009-08-12 17:15:51 -0400 | [diff] [blame] | 116 | if (key.length() > PROP_NAME_MAX) { |
| 117 | throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | } |
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 | /** |
| 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 Lockwood | d194595 | 2009-08-12 17:15:51 -0400 | [diff] [blame] | 132 | if (key.length() > PROP_NAME_MAX) { |
| 133 | throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | } |
John Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 135 | if (TRACK_KEY_ACCESS) onKeyAccess(key); |
Mike Lockwood | d194595 | 2009-08-12 17:15:51 -0400 | [diff] [blame] | 136 | return native_get_long(key, def); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | } |
| 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 Fitzpatrick | c1a968a | 2010-11-24 08:56:40 -0800 | [diff] [blame] | 143 | * (case sensitive). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | * 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 Lockwood | d194595 | 2009-08-12 17:15:51 -0400 | [diff] [blame] | 153 | if (key.length() > PROP_NAME_MAX) { |
| 154 | throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | } |
John Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 156 | if (TRACK_KEY_ACCESS) onKeyAccess(key); |
Mike Lockwood | d194595 | 2009-08-12 17:15:51 -0400 | [diff] [blame] | 157 | return native_get_boolean(key, def); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | } |
| 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 Reck | aa67f68 | 2016-09-20 14:24:21 -0700 | [diff] [blame] | 173 | if (TRACK_KEY_ACCESS) onKeyAccess(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | native_set(key, val); |
| 175 | } |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 176 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | } |