Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.provider; |
| 18 | |
Stanislav Zholnin | 596437f | 2018-12-28 15:34:23 +0000 | [diff] [blame] | 19 | import static android.Manifest.permission.READ_DEVICE_CONFIG; |
| 20 | import static android.Manifest.permission.WRITE_DEVICE_CONFIG; |
| 21 | |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 22 | import android.annotation.CallbackExecutor; |
| 23 | import android.annotation.NonNull; |
| 24 | import android.annotation.Nullable; |
Stanislav Zholnin | 596437f | 2018-12-28 15:34:23 +0000 | [diff] [blame] | 25 | import android.annotation.RequiresPermission; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 26 | import android.annotation.SystemApi; |
Philip P. Moltmann | 25995fe | 2019-01-25 14:33:11 -0800 | [diff] [blame] | 27 | import android.annotation.TestApi; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 28 | import android.app.ActivityThread; |
| 29 | import android.content.ContentResolver; |
Stanislav Zholnin | 5579950 | 2019-03-08 14:54:55 +0000 | [diff] [blame] | 30 | import android.content.Context; |
| 31 | import android.content.pm.PackageManager; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 32 | import android.database.ContentObserver; |
| 33 | import android.net.Uri; |
| 34 | import android.provider.Settings.ResetMode; |
Matt Pape | ea9881d | 2019-03-07 15:59:47 -0800 | [diff] [blame] | 35 | import android.util.ArrayMap; |
Stanislav Zholnin | ffea1de | 2019-03-07 18:06:40 +0000 | [diff] [blame] | 36 | import android.util.Log; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 37 | import android.util.Pair; |
| 38 | |
| 39 | import com.android.internal.annotations.GuardedBy; |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 40 | import com.android.internal.util.Preconditions; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 41 | |
Stanislav Zholnin | 5579950 | 2019-03-08 14:54:55 +0000 | [diff] [blame] | 42 | import java.util.Arrays; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 43 | import java.util.HashMap; |
| 44 | import java.util.List; |
| 45 | import java.util.Map; |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 46 | import java.util.Set; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 47 | import java.util.concurrent.Executor; |
| 48 | |
| 49 | /** |
| 50 | * Device level configuration parameters which can be tuned by a separate configuration service. |
Patrick Baumann | b816823 | 2019-06-11 10:17:49 -0700 | [diff] [blame] | 51 | * Namespaces that end in "_native" such as {@link #NAMESPACE_NETD_NATIVE} are intended to be used |
| 52 | * by native code and should be pushed to system properties to make them accessible. |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 53 | * |
| 54 | * @hide |
| 55 | */ |
| 56 | @SystemApi |
Philip P. Moltmann | 25995fe | 2019-01-25 14:33:11 -0800 | [diff] [blame] | 57 | @TestApi |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 58 | public final class DeviceConfig { |
| 59 | /** |
| 60 | * The content:// style URL for the config table. |
| 61 | * |
| 62 | * @hide |
| 63 | */ |
| 64 | public static final Uri CONTENT_URI = Uri.parse("content://" + Settings.AUTHORITY + "/config"); |
| 65 | |
Siarhei Vishniakou | 39a1a98 | 2019-01-11 09:22:32 -0800 | [diff] [blame] | 66 | /** |
Matt Pape | 40074da | 2019-02-12 13:53:26 -0800 | [diff] [blame] | 67 | * Namespace for activity manager related features. These features will be applied |
| 68 | * immediately upon change. |
| 69 | * |
| 70 | * @hide |
| 71 | */ |
| 72 | @SystemApi |
| 73 | public static final String NAMESPACE_ACTIVITY_MANAGER = "activity_manager"; |
| 74 | |
| 75 | /** |
Matt Pape | becd5aa | 2019-02-28 14:08:51 -0800 | [diff] [blame] | 76 | * Namespace for all activity manager related features that are used at the native level. |
| 77 | * These features are applied at reboot. |
Alex Salo | cbd05e6 | 2019-01-23 18:45:22 -0800 | [diff] [blame] | 78 | * |
Peiyong Lin | 31fa8ac | 2019-01-09 13:35:48 -0800 | [diff] [blame] | 79 | * @hide |
| 80 | */ |
| 81 | @SystemApi |
Matt Pape | becd5aa | 2019-02-28 14:08:51 -0800 | [diff] [blame] | 82 | public static final String NAMESPACE_ACTIVITY_MANAGER_NATIVE_BOOT = |
| 83 | "activity_manager_native_boot"; |
Peiyong Lin | 31fa8ac | 2019-01-09 13:35:48 -0800 | [diff] [blame] | 84 | |
| 85 | /** |
Andrei Onea | 3b433f5 | 2019-03-13 11:59:53 +0000 | [diff] [blame] | 86 | * Namespace for all app compat related features. These features will be applied |
| 87 | * immediately upon change. |
| 88 | * |
| 89 | * @hide |
| 90 | */ |
| 91 | @SystemApi |
| 92 | public static final String NAMESPACE_APP_COMPAT = "app_compat"; |
| 93 | |
| 94 | /** |
Matt Pape | aea2831 | 2019-03-07 13:51:38 -0800 | [diff] [blame] | 95 | * Namespace for AttentionManagerService related features. |
| 96 | * |
| 97 | * @hide |
| 98 | */ |
| 99 | @SystemApi |
| 100 | public static final String NAMESPACE_ATTENTION_MANAGER_SERVICE = "attention_manager_service"; |
| 101 | |
| 102 | /** |
Perumaal S | 5c016da | 2019-01-16 21:33:22 -0800 | [diff] [blame] | 103 | * Namespace for autofill feature that provides suggestions across all apps when |
| 104 | * the user interacts with input fields. |
| 105 | * |
| 106 | * @hide |
| 107 | */ |
| 108 | @SystemApi |
Felipe Leme | cc51027 | 2019-02-07 14:56:27 -0800 | [diff] [blame] | 109 | @TestApi |
Perumaal S | 5c016da | 2019-01-16 21:33:22 -0800 | [diff] [blame] | 110 | public static final String NAMESPACE_AUTOFILL = "autofill"; |
| 111 | |
| 112 | /** |
Xiao Ma | 61089ea | 2019-03-07 13:39:26 +0900 | [diff] [blame] | 113 | * Namespace for all networking connectivity related features. |
| 114 | * |
| 115 | * @hide |
| 116 | */ |
| 117 | @SystemApi |
| 118 | public static final String NAMESPACE_CONNECTIVITY = "connectivity"; |
| 119 | |
| 120 | /** |
Felipe Leme | 14ef461 | 2019-02-07 12:24:38 -0800 | [diff] [blame] | 121 | * Namespace for content capture feature used by on-device machine intelligence |
| 122 | * to provide suggestions in a privacy-safe manner. |
Felipe Leme | 70bcf38 | 2019-01-24 14:55:03 -0800 | [diff] [blame] | 123 | * |
| 124 | * @hide |
| 125 | */ |
| 126 | @SystemApi |
Felipe Leme | d03283d | 2019-02-05 09:42:42 -0800 | [diff] [blame] | 127 | @TestApi |
Felipe Leme | 14ef461 | 2019-02-07 12:24:38 -0800 | [diff] [blame] | 128 | public static final String NAMESPACE_CONTENT_CAPTURE = "content_capture"; |
Felipe Leme | 70bcf38 | 2019-01-24 14:55:03 -0800 | [diff] [blame] | 129 | |
| 130 | /** |
Matt Pape | 2933d3d | 2019-03-11 15:42:55 -0700 | [diff] [blame] | 131 | * Namespace for how dex runs. The feature requires a reboot to reach a clean state. |
| 132 | * |
Victor Hsieh | c6fff30 | 2019-10-11 10:13:50 -0700 | [diff] [blame] | 133 | * @deprecated No longer used |
Matt Pape | 2933d3d | 2019-03-11 15:42:55 -0700 | [diff] [blame] | 134 | * @hide |
| 135 | */ |
Victor Hsieh | c6fff30 | 2019-10-11 10:13:50 -0700 | [diff] [blame] | 136 | @Deprecated |
Matt Pape | 2933d3d | 2019-03-11 15:42:55 -0700 | [diff] [blame] | 137 | @SystemApi |
| 138 | public static final String NAMESPACE_DEX_BOOT = "dex_boot"; |
| 139 | |
| 140 | /** |
Long Ling | 260c524 | 2019-08-08 16:05:19 -0700 | [diff] [blame] | 141 | * Namespace for display manager related features. The names to access the properties in this |
| 142 | * namespace should be defined in {@link android.hardware.display.DisplayManager}. |
| 143 | * |
| 144 | * @hide |
| 145 | */ |
| 146 | @SystemApi |
| 147 | public static final String NAMESPACE_DISPLAY_MANAGER = "display_manager"; |
| 148 | |
| 149 | /** |
Matt Pape | becd5aa | 2019-02-28 14:08:51 -0800 | [diff] [blame] | 150 | * Namespace for all Game Driver features. |
| 151 | * |
| 152 | * @hide |
| 153 | */ |
| 154 | @SystemApi |
| 155 | public static final String NAMESPACE_GAME_DRIVER = "game_driver"; |
| 156 | |
| 157 | /** |
Siarhei Vishniakou | 39a1a98 | 2019-01-11 09:22:32 -0800 | [diff] [blame] | 158 | * Namespace for all input-related features that are used at the native level. |
| 159 | * These features are applied at reboot. |
| 160 | * |
| 161 | * @hide |
| 162 | */ |
| 163 | @SystemApi |
| 164 | public static final String NAMESPACE_INPUT_NATIVE_BOOT = "input_native_boot"; |
| 165 | |
chenbruce | db279e8 | 2018-12-27 17:12:31 +0800 | [diff] [blame] | 166 | /** |
Matt Pape | f82bb8b | 2019-03-12 09:56:28 -0700 | [diff] [blame] | 167 | * Namespace for attention-based features provided by on-device machine intelligence. |
| 168 | * |
| 169 | * @hide |
| 170 | */ |
| 171 | @SystemApi |
| 172 | public static final String NAMESPACE_INTELLIGENCE_ATTENTION = "intelligence_attention"; |
| 173 | |
| 174 | /** |
Anna Wasewicz | 3ecea55 | 2019-04-19 15:27:09 -0700 | [diff] [blame] | 175 | * Definitions for properties related to Content Suggestions. |
| 176 | * |
| 177 | * @hide |
| 178 | */ |
| 179 | public static final String NAMESPACE_INTELLIGENCE_CONTENT_SUGGESTIONS = |
| 180 | "intelligence_content_suggestions"; |
| 181 | |
| 182 | /** |
Matt Pape | becd5aa | 2019-02-28 14:08:51 -0800 | [diff] [blame] | 183 | * Namespace for all media native related features. |
| 184 | * |
| 185 | * @hide |
| 186 | */ |
| 187 | @SystemApi |
| 188 | public static final String NAMESPACE_MEDIA_NATIVE = "media_native"; |
| 189 | |
| 190 | /** |
chenbruce | db279e8 | 2018-12-27 17:12:31 +0800 | [diff] [blame] | 191 | * Namespace for all netd related features. |
| 192 | * |
| 193 | * @hide |
| 194 | */ |
| 195 | @SystemApi |
| 196 | public static final String NAMESPACE_NETD_NATIVE = "netd_native"; |
| 197 | |
Gustav Sennton | 1aebc9b | 2019-01-22 17:56:18 +0000 | [diff] [blame] | 198 | /** |
Carmen Agimof | 54a6c89 | 2019-01-25 16:50:11 +0000 | [diff] [blame] | 199 | * Namespace for features related to the Package Manager Service. |
| 200 | * |
| 201 | * @hide |
| 202 | */ |
| 203 | @SystemApi |
| 204 | public static final String NAMESPACE_PACKAGE_MANAGER_SERVICE = "package_manager_service"; |
| 205 | |
| 206 | /** |
Matt Pape | 12187ae | 2019-03-14 13:37:32 -0700 | [diff] [blame] | 207 | * Namespace for Rollback flags that are applied immediately. |
| 208 | * |
| 209 | * @hide |
| 210 | */ |
| 211 | @SystemApi @TestApi |
| 212 | public static final String NAMESPACE_ROLLBACK = "rollback"; |
| 213 | |
| 214 | /** |
| 215 | * Namespace for Rollback flags that are applied after a reboot. |
| 216 | * |
| 217 | * @hide |
| 218 | */ |
| 219 | @SystemApi @TestApi |
| 220 | public static final String NAMESPACE_ROLLBACK_BOOT = "rollback_boot"; |
| 221 | |
| 222 | /** |
Mathieu Chartier | 881c7a9 | 2019-03-14 16:04:45 -0700 | [diff] [blame] | 223 | * Namespace for all runtime related features that don't require a reboot to become active. |
| 224 | * There are no feature flags using NAMESPACE_RUNTIME. |
Matt Pape | e9b680a | 2019-03-05 15:53:13 -0800 | [diff] [blame] | 225 | * |
| 226 | * @hide |
| 227 | */ |
| 228 | @SystemApi |
| 229 | public static final String NAMESPACE_RUNTIME = "runtime"; |
| 230 | |
| 231 | /** |
Mathieu Chartier | 881c7a9 | 2019-03-14 16:04:45 -0700 | [diff] [blame] | 232 | * Namespace for all runtime related features that require system properties for accessing |
| 233 | * the feature flags from C++ or Java language code. One example is the app image startup |
| 234 | * cache feature use_app_image_startup_cache. |
Matt Pape | e9b680a | 2019-03-05 15:53:13 -0800 | [diff] [blame] | 235 | * |
| 236 | * @hide |
| 237 | */ |
| 238 | @SystemApi |
| 239 | public static final String NAMESPACE_RUNTIME_NATIVE = "runtime_native"; |
| 240 | |
| 241 | /** |
Matt Pape | becd5aa | 2019-02-28 14:08:51 -0800 | [diff] [blame] | 242 | * Namespace for all runtime native boot related features. Boot in this case refers to the |
| 243 | * fact that the properties only take affect after rebooting the device. |
| 244 | * |
| 245 | * @hide |
| 246 | */ |
| 247 | @SystemApi |
| 248 | public static final String NAMESPACE_RUNTIME_NATIVE_BOOT = "runtime_native_boot"; |
| 249 | |
| 250 | /** |
Matt Pape | f095f3c | 2019-03-15 13:17:45 -0700 | [diff] [blame] | 251 | * Namespace for system scheduler related features. These features will be applied |
| 252 | * immediately upon change. |
| 253 | * |
| 254 | * @hide |
| 255 | */ |
| 256 | @SystemApi |
| 257 | public static final String NAMESPACE_SCHEDULER = "scheduler"; |
| 258 | |
| 259 | /** |
Matt Pape | 19ea8a6 | 2019-03-18 10:38:25 -0700 | [diff] [blame] | 260 | * Namespace for storage-related features. |
| 261 | * |
shafik | 91b3461 | 2019-09-23 15:41:44 +0100 | [diff] [blame] | 262 | * @deprecated Replace storage namespace with storage_native_boot. |
| 263 | * @hide |
| 264 | */ |
| 265 | @Deprecated |
| 266 | @SystemApi |
| 267 | public static final String NAMESPACE_STORAGE = "storage"; |
| 268 | |
| 269 | /** |
| 270 | * Namespace for storage-related features, including native and boot. |
| 271 | * |
Matt Pape | 19ea8a6 | 2019-03-18 10:38:25 -0700 | [diff] [blame] | 272 | * @hide |
| 273 | */ |
| 274 | @SystemApi |
shafik | 91b3461 | 2019-09-23 15:41:44 +0100 | [diff] [blame] | 275 | public static final String NAMESPACE_STORAGE_NATIVE_BOOT = "storage_native_boot"; |
Matt Pape | 19ea8a6 | 2019-03-18 10:38:25 -0700 | [diff] [blame] | 276 | |
| 277 | /** |
Gustav Sennton | ddd78b2 | 2019-02-18 17:58:12 +0000 | [diff] [blame] | 278 | * Namespace for System UI related features. |
Gustav Sennton | 1aebc9b | 2019-01-22 17:56:18 +0000 | [diff] [blame] | 279 | * |
| 280 | * @hide |
| 281 | */ |
| 282 | @SystemApi |
Gustav Sennton | ddd78b2 | 2019-02-18 17:58:12 +0000 | [diff] [blame] | 283 | public static final String NAMESPACE_SYSTEMUI = "systemui"; |
Gustav Sennton | 1aebc9b | 2019-01-22 17:56:18 +0000 | [diff] [blame] | 284 | |
Yiwen Chen | 3d3ad02 | 2019-01-22 21:15:56 -0800 | [diff] [blame] | 285 | /** |
Matt Pape | 38ed4e7 | 2019-03-20 15:03:39 -0700 | [diff] [blame] | 286 | * Telephony related properties. |
| 287 | * |
| 288 | * @hide |
| 289 | */ |
| 290 | @SystemApi |
| 291 | public static final String NAMESPACE_TELEPHONY = "telephony"; |
| 292 | |
| 293 | /** |
Tony Mak | fc37457 | 2019-03-05 14:46:24 +0000 | [diff] [blame] | 294 | * Namespace for TextClassifier related features. |
| 295 | * |
| 296 | * @hide |
Abodunrinwa Toki | 0634af3 | 2019-04-04 13:10:59 +0100 | [diff] [blame] | 297 | * @see android.provider.Settings.Global.TEXT_CLASSIFIER_CONSTANTS |
Tony Mak | fc37457 | 2019-03-05 14:46:24 +0000 | [diff] [blame] | 298 | */ |
| 299 | @SystemApi |
| 300 | public static final String NAMESPACE_TEXTCLASSIFIER = "textclassifier"; |
| 301 | |
| 302 | /** |
Makoto Onuki | 87d3964 | 2019-04-04 14:42:23 -0700 | [diff] [blame] | 303 | * Namespace for contacts provider related features. |
| 304 | * |
| 305 | * @hide |
| 306 | */ |
| 307 | public static final String NAMESPACE_CONTACTS_PROVIDER = "contacts_provider"; |
| 308 | |
| 309 | /** |
Lei Yu | 5d3461b | 2019-04-11 13:50:50 -0700 | [diff] [blame] | 310 | * Namespace for settings ui related features |
| 311 | * |
| 312 | * @hide |
| 313 | */ |
| 314 | public static final String NAMESPACE_SETTINGS_UI = "settings_ui"; |
| 315 | |
| 316 | /** |
Adrian Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 317 | * Namespace for android related features, i.e. for flags that affect not just a single |
| 318 | * component, but the entire system. |
| 319 | * |
| 320 | * The keys for this namespace are defined in {@link AndroidDeviceConfig}. |
Adrian Roos | bf3bc1b | 2019-06-18 16:13:53 +0200 | [diff] [blame] | 321 | * |
| 322 | * @hide |
| 323 | */ |
| 324 | @TestApi |
Adrian Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 325 | public static final String NAMESPACE_ANDROID = "android"; |
| 326 | |
| 327 | /** |
| 328 | * Namespace for window manager related features. |
| 329 | * |
| 330 | * @hide |
| 331 | */ |
| 332 | public static final String NAMESPACE_WINDOW_MANAGER = "window_manager"; |
Adrian Roos | bf3bc1b | 2019-06-18 16:13:53 +0200 | [diff] [blame] | 333 | |
| 334 | /** |
Stanislav Zholnin | 5579950 | 2019-03-08 14:54:55 +0000 | [diff] [blame] | 335 | * List of namespaces which can be read without READ_DEVICE_CONFIG permission |
| 336 | * |
| 337 | * @hide |
| 338 | */ |
| 339 | @NonNull |
| 340 | private static final List<String> PUBLIC_NAMESPACES = |
| 341 | Arrays.asList(NAMESPACE_TEXTCLASSIFIER, NAMESPACE_RUNTIME); |
| 342 | /** |
Joel Galenson | 489239b | 2019-01-23 10:05:03 -0800 | [diff] [blame] | 343 | * Privacy related properties definitions. |
| 344 | * |
| 345 | * @hide |
| 346 | */ |
| 347 | @SystemApi |
Philip P. Moltmann | 25995fe | 2019-01-25 14:33:11 -0800 | [diff] [blame] | 348 | @TestApi |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 349 | public static final String NAMESPACE_PRIVACY = "privacy"; |
Joel Galenson | 489239b | 2019-01-23 10:05:03 -0800 | [diff] [blame] | 350 | |
Adrian Roos | bf3bc1b | 2019-06-18 16:13:53 +0200 | [diff] [blame] | 351 | /** |
Evan Severson | f7461f0 | 2019-10-18 10:51:23 -0700 | [diff] [blame] | 352 | * Permission related properties definitions. |
| 353 | * |
| 354 | * @hide |
| 355 | */ |
| 356 | @SystemApi |
| 357 | @TestApi |
| 358 | public static final String NAMESPACE_PERMISSIONS = "permissions"; |
| 359 | |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 360 | private static final Object sLock = new Object(); |
| 361 | @GuardedBy("sLock") |
Matt Pape | ea9881d | 2019-03-07 15:59:47 -0800 | [diff] [blame] | 362 | private static ArrayMap<OnPropertiesChangedListener, Pair<String, Executor>> sListeners = |
| 363 | new ArrayMap<>(); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 364 | @GuardedBy("sLock") |
| 365 | private static Map<String, Pair<ContentObserver, Integer>> sNamespaces = new HashMap<>(); |
Stanislav Zholnin | ffea1de | 2019-03-07 18:06:40 +0000 | [diff] [blame] | 366 | private static final String TAG = "DeviceConfig"; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 367 | |
| 368 | // Should never be invoked |
| 369 | private DeviceConfig() { |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Look up the value of a property for a particular namespace. |
| 374 | * |
| 375 | * @param namespace The namespace containing the property to look up. |
Alex Salo | cbd05e6 | 2019-01-23 18:45:22 -0800 | [diff] [blame] | 376 | * @param name The name of the property to look up. |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 377 | * @return the corresponding value, or null if not present. |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 378 | * @hide |
| 379 | */ |
| 380 | @SystemApi |
Felipe Leme | d03283d | 2019-02-05 09:42:42 -0800 | [diff] [blame] | 381 | @TestApi |
Stanislav Zholnin | 596437f | 2018-12-28 15:34:23 +0000 | [diff] [blame] | 382 | @RequiresPermission(READ_DEVICE_CONFIG) |
Matt Pape | c4ed9b3 | 2019-03-11 14:07:00 -0700 | [diff] [blame] | 383 | public static String getProperty(@NonNull String namespace, @NonNull String name) { |
Matt Pape | 793b15c | 2019-10-07 13:17:20 -0700 | [diff] [blame] | 384 | // Fetch all properties for the namespace at once and cache them in the local process, so we |
| 385 | // incur the cost of the IPC less often. Lookups happen much more frequently than updates, |
| 386 | // and we want to optimize the former. |
| 387 | return getProperties(namespace, name).getString(name, null); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Look up the values of multiple properties for a particular namespace. The lookup is atomic, |
| 392 | * such that the values of these properties cannot change between the time when the first is |
| 393 | * fetched and the time when the last is fetched. |
Matt Pape | 4bd0a6a | 2019-11-19 12:17:45 -0800 | [diff] [blame] | 394 | * <p> |
| 395 | * Each call to {@link #setProperties(Properties)} is also atomic and ensures that either none |
| 396 | * or all of the change is picked up here, but never only part of it. |
Matt Pape | 793b15c | 2019-10-07 13:17:20 -0700 | [diff] [blame] | 397 | * |
| 398 | * @param namespace The namespace containing the properties to look up. |
| 399 | * @param names The names of properties to look up, or empty to fetch all properties for the |
| 400 | * given namespace. |
| 401 | * @return {@link Properties} object containing the requested properties. This reflects the |
| 402 | * state of these properties at the time of the lookup, and is not updated to reflect any |
| 403 | * future changes. The keyset of this Properties object will contain only the intersection |
| 404 | * of properties already set and properties requested via the names parameter. Properties |
| 405 | * that are already set but were not requested will not be contained here. Properties that |
| 406 | * are not set, but were requested will not be contained here either. |
| 407 | * @hide |
| 408 | */ |
| 409 | @SystemApi |
| 410 | @NonNull |
| 411 | @RequiresPermission(READ_DEVICE_CONFIG) |
| 412 | public static Properties getProperties(@NonNull String namespace, @NonNull String ... names) { |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 413 | ContentResolver contentResolver = ActivityThread.currentApplication().getContentResolver(); |
Matt Pape | 793b15c | 2019-10-07 13:17:20 -0700 | [diff] [blame] | 414 | return new Properties(namespace, |
| 415 | Settings.Config.getStrings(contentResolver, namespace, Arrays.asList(names))); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /** |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 419 | * Look up the String value of a property for a particular namespace. |
| 420 | * |
Matt Pape | fc5389a | 2019-02-28 11:05:03 -0800 | [diff] [blame] | 421 | * @param namespace The namespace containing the property to look up. |
| 422 | * @param name The name of the property to look up. |
| 423 | * @param defaultValue The value to return if the property does not exist or has no non-null |
| 424 | * value. |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 425 | * @return the corresponding value, or defaultValue if none exists. |
| 426 | * @hide |
| 427 | */ |
| 428 | @SystemApi |
| 429 | @TestApi |
| 430 | @RequiresPermission(READ_DEVICE_CONFIG) |
Matt Pape | c4ed9b3 | 2019-03-11 14:07:00 -0700 | [diff] [blame] | 431 | public static String getString(@NonNull String namespace, @NonNull String name, |
| 432 | @Nullable String defaultValue) { |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 433 | String value = getProperty(namespace, name); |
| 434 | return value != null ? value : defaultValue; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Look up the boolean value of a property for a particular namespace. |
| 439 | * |
| 440 | * @param namespace The namespace containing the property to look up. |
| 441 | * @param name The name of the property to look up. |
Matt Pape | fc5389a | 2019-02-28 11:05:03 -0800 | [diff] [blame] | 442 | * @param defaultValue The value to return if the property does not exist or has no non-null |
| 443 | * value. |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 444 | * @return the corresponding value, or defaultValue if none exists. |
| 445 | * @hide |
| 446 | */ |
| 447 | @SystemApi |
| 448 | @TestApi |
| 449 | @RequiresPermission(READ_DEVICE_CONFIG) |
Matt Pape | c4ed9b3 | 2019-03-11 14:07:00 -0700 | [diff] [blame] | 450 | public static boolean getBoolean(@NonNull String namespace, @NonNull String name, |
| 451 | boolean defaultValue) { |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 452 | String value = getProperty(namespace, name); |
| 453 | return value != null ? Boolean.parseBoolean(value) : defaultValue; |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Look up the int value of a property for a particular namespace. |
| 458 | * |
| 459 | * @param namespace The namespace containing the property to look up. |
| 460 | * @param name The name of the property to look up. |
Matt Pape | fc5389a | 2019-02-28 11:05:03 -0800 | [diff] [blame] | 461 | * @param defaultValue The value to return if the property does not exist, has no non-null |
| 462 | * value, or fails to parse into an int. |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 463 | * @return the corresponding value, or defaultValue if either none exists or it does not parse. |
| 464 | * @hide |
| 465 | */ |
| 466 | @SystemApi |
| 467 | @TestApi |
| 468 | @RequiresPermission(READ_DEVICE_CONFIG) |
Matt Pape | c4ed9b3 | 2019-03-11 14:07:00 -0700 | [diff] [blame] | 469 | public static int getInt(@NonNull String namespace, @NonNull String name, int defaultValue) { |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 470 | String value = getProperty(namespace, name); |
Stanislav Zholnin | ffea1de | 2019-03-07 18:06:40 +0000 | [diff] [blame] | 471 | if (value == null) { |
| 472 | return defaultValue; |
| 473 | } |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 474 | try { |
| 475 | return Integer.parseInt(value); |
| 476 | } catch (NumberFormatException e) { |
Stanislav Zholnin | ffea1de | 2019-03-07 18:06:40 +0000 | [diff] [blame] | 477 | Log.e(TAG, "Parsing integer failed for " + namespace + ":" + name); |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 478 | return defaultValue; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Look up the long value of a property for a particular namespace. |
| 484 | * |
| 485 | * @param namespace The namespace containing the property to look up. |
| 486 | * @param name The name of the property to look up. |
Matt Pape | fc5389a | 2019-02-28 11:05:03 -0800 | [diff] [blame] | 487 | * @param defaultValue The value to return if the property does not exist, has no non-null |
| 488 | * value, or fails to parse into a long. |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 489 | * @return the corresponding value, or defaultValue if either none exists or it does not parse. |
| 490 | * @hide |
| 491 | */ |
| 492 | @SystemApi |
| 493 | @TestApi |
| 494 | @RequiresPermission(READ_DEVICE_CONFIG) |
Matt Pape | c4ed9b3 | 2019-03-11 14:07:00 -0700 | [diff] [blame] | 495 | public static long getLong(@NonNull String namespace, @NonNull String name, long defaultValue) { |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 496 | String value = getProperty(namespace, name); |
Stanislav Zholnin | ffea1de | 2019-03-07 18:06:40 +0000 | [diff] [blame] | 497 | if (value == null) { |
| 498 | return defaultValue; |
| 499 | } |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 500 | try { |
| 501 | return Long.parseLong(value); |
| 502 | } catch (NumberFormatException e) { |
Stanislav Zholnin | ffea1de | 2019-03-07 18:06:40 +0000 | [diff] [blame] | 503 | Log.e(TAG, "Parsing long failed for " + namespace + ":" + name); |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 504 | return defaultValue; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Look up the float value of a property for a particular namespace. |
| 510 | * |
| 511 | * @param namespace The namespace containing the property to look up. |
| 512 | * @param name The name of the property to look up. |
Matt Pape | fc5389a | 2019-02-28 11:05:03 -0800 | [diff] [blame] | 513 | * @param defaultValue The value to return if the property does not exist, has no non-null |
| 514 | * value, or fails to parse into a float. |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 515 | * @return the corresponding value, or defaultValue if either none exists or it does not parse. |
| 516 | * @hide |
| 517 | */ |
| 518 | @SystemApi |
| 519 | @TestApi |
| 520 | @RequiresPermission(READ_DEVICE_CONFIG) |
Matt Pape | c4ed9b3 | 2019-03-11 14:07:00 -0700 | [diff] [blame] | 521 | public static float getFloat(@NonNull String namespace, @NonNull String name, |
| 522 | float defaultValue) { |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 523 | String value = getProperty(namespace, name); |
Stanislav Zholnin | ffea1de | 2019-03-07 18:06:40 +0000 | [diff] [blame] | 524 | if (value == null) { |
| 525 | return defaultValue; |
| 526 | } |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 527 | try { |
| 528 | return Float.parseFloat(value); |
| 529 | } catch (NumberFormatException e) { |
Stanislav Zholnin | ffea1de | 2019-03-07 18:06:40 +0000 | [diff] [blame] | 530 | Log.e(TAG, "Parsing float failed for " + namespace + ":" + name); |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 531 | return defaultValue; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | /** |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 536 | * Create a new property with the the provided name and value in the provided namespace, or |
| 537 | * update the value of such a property if it already exists. The same name can exist in multiple |
| 538 | * namespaces and might have different values in any or all namespaces. |
| 539 | * <p> |
| 540 | * The method takes an argument indicating whether to make the value the default for this |
| 541 | * property. |
| 542 | * <p> |
| 543 | * All properties stored for a particular scope can be reverted to their default values |
| 544 | * by passing the namespace to {@link #resetToDefaults(int, String)}. |
| 545 | * |
Alex Salo | cbd05e6 | 2019-01-23 18:45:22 -0800 | [diff] [blame] | 546 | * @param namespace The namespace containing the property to create or update. |
| 547 | * @param name The name of the property to create or update. |
| 548 | * @param value The value to store for the property. |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 549 | * @param makeDefault Whether to make the new value the default one. |
| 550 | * @return True if the value was set, false if the storage implementation throws errors. |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 551 | * @hide |
Alex Salo | cbd05e6 | 2019-01-23 18:45:22 -0800 | [diff] [blame] | 552 | * @see #resetToDefaults(int, String). |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 553 | */ |
| 554 | @SystemApi |
Philip P. Moltmann | 25995fe | 2019-01-25 14:33:11 -0800 | [diff] [blame] | 555 | @TestApi |
Stanislav Zholnin | 596437f | 2018-12-28 15:34:23 +0000 | [diff] [blame] | 556 | @RequiresPermission(WRITE_DEVICE_CONFIG) |
Matt Pape | c4ed9b3 | 2019-03-11 14:07:00 -0700 | [diff] [blame] | 557 | public static boolean setProperty(@NonNull String namespace, @NonNull String name, |
| 558 | @Nullable String value, boolean makeDefault) { |
Matt Pape | c4ed9b3 | 2019-03-11 14:07:00 -0700 | [diff] [blame] | 559 | ContentResolver contentResolver = ActivityThread.currentApplication().getContentResolver(); |
Matt Pape | c600153 | 2019-10-24 15:50:32 -0700 | [diff] [blame] | 560 | return Settings.Config.putString(contentResolver, namespace, name, value, makeDefault); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /** |
Matt Pape | 4bd0a6a | 2019-11-19 12:17:45 -0800 | [diff] [blame] | 564 | * Set all of the properties for a specific namespace. Pre-existing properties will be updated |
| 565 | * and new properties will be added if necessary. Any pre-existing properties for the specific |
| 566 | * namespace which are not part of the provided {@link Properties} object will be deleted from |
| 567 | * the namespace. These changes are all applied atomically, such that no calls to read or reset |
| 568 | * these properties can happen in the middle of this update. |
| 569 | * <p> |
| 570 | * Each call to {@link #getProperties(String, String...)} is also atomic and ensures that either |
| 571 | * none or all of this update is picked up, but never only part of it. |
| 572 | * |
| 573 | * @param properties the complete set of properties to set for a specific namespace. |
| 574 | * @hide |
| 575 | */ |
| 576 | @SystemApi |
| 577 | @RequiresPermission(WRITE_DEVICE_CONFIG) |
| 578 | public static boolean setProperties(@NonNull Properties properties) { |
| 579 | ContentResolver contentResolver = ActivityThread.currentApplication().getContentResolver(); |
| 580 | return Settings.Config.setStrings(contentResolver, properties.getNamespace(), |
| 581 | properties.mMap); |
| 582 | } |
| 583 | |
| 584 | /** |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 585 | * Reset properties to their default values. |
| 586 | * <p> |
| 587 | * The method accepts an optional namespace parameter. If provided, only properties set within |
| 588 | * that namespace will be reset. Otherwise, all properties will be reset. |
| 589 | * |
| 590 | * @param resetMode The reset mode to use. |
| 591 | * @param namespace Optionally, the specific namespace which resets will be limited to. |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 592 | * @hide |
Alex Salo | cbd05e6 | 2019-01-23 18:45:22 -0800 | [diff] [blame] | 593 | * @see #setProperty(String, String, String, boolean) |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 594 | */ |
| 595 | @SystemApi |
Philip P. Moltmann | 25995fe | 2019-01-25 14:33:11 -0800 | [diff] [blame] | 596 | @TestApi |
Stanislav Zholnin | 596437f | 2018-12-28 15:34:23 +0000 | [diff] [blame] | 597 | @RequiresPermission(WRITE_DEVICE_CONFIG) |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 598 | public static void resetToDefaults(@ResetMode int resetMode, @Nullable String namespace) { |
| 599 | ContentResolver contentResolver = ActivityThread.currentApplication().getContentResolver(); |
| 600 | Settings.Config.resetToDefaults(contentResolver, resetMode, namespace); |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Add a listener for property changes. |
| 605 | * <p> |
| 606 | * This listener will be called whenever properties in the specified namespace change. Callbacks |
| 607 | * will be made on the specified executor. Future calls to this method with the same listener |
| 608 | * will replace the old namespace and executor. Remove the listener entirely by calling |
Matt Pape | ea9881d | 2019-03-07 15:59:47 -0800 | [diff] [blame] | 609 | * {@link #removeOnPropertiesChangedListener(OnPropertiesChangedListener)}. |
| 610 | * |
| 611 | * @param namespace The namespace containing properties to monitor. |
| 612 | * @param executor The executor which will be used to run callbacks. |
| 613 | * @param onPropertiesChangedListener The listener to add. |
| 614 | * @hide |
| 615 | * @see #removeOnPropertiesChangedListener(OnPropertiesChangedListener) |
| 616 | */ |
| 617 | @SystemApi |
| 618 | @TestApi |
| 619 | @RequiresPermission(READ_DEVICE_CONFIG) |
| 620 | public static void addOnPropertiesChangedListener( |
| 621 | @NonNull String namespace, |
| 622 | @NonNull @CallbackExecutor Executor executor, |
| 623 | @NonNull OnPropertiesChangedListener onPropertiesChangedListener) { |
Stanislav Zholnin | 5579950 | 2019-03-08 14:54:55 +0000 | [diff] [blame] | 624 | enforceReadPermission(ActivityThread.currentApplication().getApplicationContext(), |
| 625 | namespace); |
Matt Pape | ea9881d | 2019-03-07 15:59:47 -0800 | [diff] [blame] | 626 | synchronized (sLock) { |
| 627 | Pair<String, Executor> oldNamespace = sListeners.get(onPropertiesChangedListener); |
| 628 | if (oldNamespace == null) { |
| 629 | // Brand new listener, add it to the list. |
| 630 | sListeners.put(onPropertiesChangedListener, new Pair<>(namespace, executor)); |
| 631 | incrementNamespace(namespace); |
| 632 | } else if (namespace.equals(oldNamespace.first)) { |
| 633 | // Listener is already registered for this namespace, update executor just in case. |
| 634 | sListeners.put(onPropertiesChangedListener, new Pair<>(namespace, executor)); |
| 635 | } else { |
| 636 | // Update this listener from an old namespace to the new one. |
| 637 | decrementNamespace(sListeners.get(onPropertiesChangedListener).first); |
| 638 | sListeners.put(onPropertiesChangedListener, new Pair<>(namespace, executor)); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 639 | incrementNamespace(namespace); |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * Remove a listener for property changes. The listener will receive no further notification of |
| 646 | * property changes. |
| 647 | * |
Matt Pape | ea9881d | 2019-03-07 15:59:47 -0800 | [diff] [blame] | 648 | * @param onPropertiesChangedListener The listener to remove. |
| 649 | * @hide |
| 650 | * @see #addOnPropertiesChangedListener(String, Executor, OnPropertiesChangedListener) |
| 651 | */ |
| 652 | @SystemApi |
| 653 | @TestApi |
| 654 | public static void removeOnPropertiesChangedListener( |
| 655 | @NonNull OnPropertiesChangedListener onPropertiesChangedListener) { |
| 656 | Preconditions.checkNotNull(onPropertiesChangedListener); |
| 657 | synchronized (sLock) { |
| 658 | if (sListeners.containsKey(onPropertiesChangedListener)) { |
| 659 | decrementNamespace(sListeners.get(onPropertiesChangedListener).first); |
| 660 | sListeners.remove(onPropertiesChangedListener); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
Matt Pape | c4ed9b3 | 2019-03-11 14:07:00 -0700 | [diff] [blame] | 665 | private static Uri createNamespaceUri(@NonNull String namespace) { |
| 666 | Preconditions.checkNotNull(namespace); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 667 | return CONTENT_URI.buildUpon().appendPath(namespace).build(); |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Increment the count used to represent the number of listeners subscribed to the given |
| 672 | * namespace. If this is the first (i.e. incrementing from 0 to 1) for the given namespace, a |
| 673 | * ContentObserver is registered. |
| 674 | * |
| 675 | * @param namespace The namespace to increment the count for. |
| 676 | */ |
| 677 | @GuardedBy("sLock") |
Matt Pape | c4ed9b3 | 2019-03-11 14:07:00 -0700 | [diff] [blame] | 678 | private static void incrementNamespace(@NonNull String namespace) { |
| 679 | Preconditions.checkNotNull(namespace); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 680 | Pair<ContentObserver, Integer> namespaceCount = sNamespaces.get(namespace); |
| 681 | if (namespaceCount != null) { |
| 682 | sNamespaces.put(namespace, new Pair<>(namespaceCount.first, namespaceCount.second + 1)); |
| 683 | } else { |
| 684 | // This is a new namespace, register a ContentObserver for it. |
| 685 | ContentObserver contentObserver = new ContentObserver(null) { |
| 686 | @Override |
| 687 | public void onChange(boolean selfChange, Uri uri) { |
Matt Pape | c4ed9b3 | 2019-03-11 14:07:00 -0700 | [diff] [blame] | 688 | if (uri != null) { |
| 689 | handleChange(uri); |
| 690 | } |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 691 | } |
| 692 | }; |
| 693 | ActivityThread.currentApplication().getContentResolver() |
| 694 | .registerContentObserver(createNamespaceUri(namespace), true, contentObserver); |
| 695 | sNamespaces.put(namespace, new Pair<>(contentObserver, 1)); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /** |
Stanislav Zholnin | 5579950 | 2019-03-08 14:54:55 +0000 | [diff] [blame] | 700 | * Decrement the count used to represent the number of listeners subscribed to the given |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 701 | * namespace. If this is the final decrement call (i.e. decrementing from 1 to 0) for the given |
| 702 | * namespace, the ContentObserver that had been tracking it will be removed. |
| 703 | * |
| 704 | * @param namespace The namespace to decrement the count for. |
| 705 | */ |
| 706 | @GuardedBy("sLock") |
Matt Pape | c4ed9b3 | 2019-03-11 14:07:00 -0700 | [diff] [blame] | 707 | private static void decrementNamespace(@NonNull String namespace) { |
| 708 | Preconditions.checkNotNull(namespace); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 709 | Pair<ContentObserver, Integer> namespaceCount = sNamespaces.get(namespace); |
| 710 | if (namespaceCount == null) { |
| 711 | // This namespace is not registered and does not need to be decremented |
| 712 | return; |
| 713 | } else if (namespaceCount.second > 1) { |
| 714 | sNamespaces.put(namespace, new Pair<>(namespaceCount.first, namespaceCount.second - 1)); |
| 715 | } else { |
| 716 | // Decrementing a namespace to zero means we no longer need its ContentObserver. |
| 717 | ActivityThread.currentApplication().getContentResolver() |
| 718 | .unregisterContentObserver(namespaceCount.first); |
| 719 | sNamespaces.remove(namespace); |
| 720 | } |
| 721 | } |
| 722 | |
Matt Pape | c4ed9b3 | 2019-03-11 14:07:00 -0700 | [diff] [blame] | 723 | private static void handleChange(@NonNull Uri uri) { |
| 724 | Preconditions.checkNotNull(uri); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 725 | List<String> pathSegments = uri.getPathSegments(); |
| 726 | // pathSegments(0) is "config" |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 727 | final String namespace = pathSegments.get(1); |
Matt Pape | 4bd0a6a | 2019-11-19 12:17:45 -0800 | [diff] [blame] | 728 | Map<String, String> propertyMap = new ArrayMap<>(); |
Stanislav Zholnin | 5579950 | 2019-03-08 14:54:55 +0000 | [diff] [blame] | 729 | try { |
Matt Pape | 4bd0a6a | 2019-11-19 12:17:45 -0800 | [diff] [blame] | 730 | Properties allProperties = getProperties(namespace); |
| 731 | for (int i = 2; i < pathSegments.size(); ++i) { |
| 732 | String key = pathSegments.get(i); |
| 733 | propertyMap.put(key, allProperties.getString(key, null)); |
| 734 | } |
Stanislav Zholnin | 5579950 | 2019-03-08 14:54:55 +0000 | [diff] [blame] | 735 | } catch (SecurityException e) { |
| 736 | // Silently failing to not crash binder or listener threads. |
| 737 | Log.e(TAG, "OnPropertyChangedListener update failed: permission violation."); |
| 738 | return; |
| 739 | } |
Matt Pape | 4bd0a6a | 2019-11-19 12:17:45 -0800 | [diff] [blame] | 740 | Properties properties = new Properties(namespace, propertyMap); |
| 741 | |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 742 | synchronized (sLock) { |
Matt Pape | ea9881d | 2019-03-07 15:59:47 -0800 | [diff] [blame] | 743 | for (int i = 0; i < sListeners.size(); i++) { |
| 744 | if (namespace.equals(sListeners.valueAt(i).first)) { |
Pavel Grafov | c237073 | 2019-08-16 12:04:35 +0100 | [diff] [blame] | 745 | final OnPropertiesChangedListener listener = sListeners.keyAt(i); |
| 746 | sListeners.valueAt(i).second.execute(() -> { |
Matt Pape | 4bd0a6a | 2019-11-19 12:17:45 -0800 | [diff] [blame] | 747 | listener.onPropertiesChanged(properties); |
Matt Pape | ea9881d | 2019-03-07 15:59:47 -0800 | [diff] [blame] | 748 | }); |
| 749 | } |
| 750 | } |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
Stanislav Zholnin | 5579950 | 2019-03-08 14:54:55 +0000 | [diff] [blame] | 754 | /** |
| 755 | * Enforces READ_DEVICE_CONFIG permission if namespace is not one of public namespaces. |
| 756 | * @hide |
| 757 | */ |
| 758 | public static void enforceReadPermission(Context context, String namespace) { |
| 759 | if (context.checkCallingOrSelfPermission(READ_DEVICE_CONFIG) |
| 760 | != PackageManager.PERMISSION_GRANTED) { |
| 761 | if (!PUBLIC_NAMESPACES.contains(namespace)) { |
| 762 | throw new SecurityException("Permission denial: reading from settings requires:" |
| 763 | + READ_DEVICE_CONFIG); |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
Matt Pape | ea9881d | 2019-03-07 15:59:47 -0800 | [diff] [blame] | 768 | /** |
Matt Pape | 4bd0a6a | 2019-11-19 12:17:45 -0800 | [diff] [blame] | 769 | * Interface for monitoring changes to properties. Implementations will receive callbacks when |
| 770 | * properties change, including a {@link Properties} object which contains a single namespace |
| 771 | * and all of the properties which changed for that namespace. This includes properties which |
| 772 | * were added, updated, or deleted. This is not necessarily a complete list of all properties |
| 773 | * belonging to the namespace, as properties which don't change are omitted. |
Matt Pape | ea9881d | 2019-03-07 15:59:47 -0800 | [diff] [blame] | 774 | * <p> |
| 775 | * Override {@link #onPropertiesChanged(Properties)} to handle callbacks for changes. |
| 776 | * |
| 777 | * @hide |
| 778 | */ |
| 779 | @SystemApi |
| 780 | @TestApi |
| 781 | public interface OnPropertiesChangedListener { |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 782 | /** |
Matt Pape | 4bd0a6a | 2019-11-19 12:17:45 -0800 | [diff] [blame] | 783 | * Called when one or more properties have changed, providing a Properties object with all |
| 784 | * of the changed properties. This object will contain only properties which have changed, |
| 785 | * not the complete set of all properties belonging to the namespace. |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 786 | * |
| 787 | * @param properties Contains the complete collection of properties which have changed for a |
Matt Pape | 4bd0a6a | 2019-11-19 12:17:45 -0800 | [diff] [blame] | 788 | * single namespace. This includes only those which were added, updated, |
| 789 | * or deleted. |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 790 | */ |
Matt Pape | ea9881d | 2019-03-07 15:59:47 -0800 | [diff] [blame] | 791 | void onPropertiesChanged(@NonNull Properties properties); |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /** |
| 795 | * A mapping of properties to values, as well as a single namespace which they all belong to. |
| 796 | * |
| 797 | * @hide |
| 798 | */ |
| 799 | @SystemApi |
| 800 | @TestApi |
| 801 | public static class Properties { |
| 802 | private final String mNamespace; |
| 803 | private final HashMap<String, String> mMap; |
| 804 | |
| 805 | /** |
| 806 | * Create a mapping of properties to values and the namespace they belong to. |
| 807 | * |
| 808 | * @param namespace The namespace these properties belong to. |
| 809 | * @param keyValueMap A map between property names and property values. |
Dave Mankoff | 186bd74 | 2019-08-15 10:26:38 -0400 | [diff] [blame] | 810 | * @hide |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 811 | */ |
Dave Mankoff | 186bd74 | 2019-08-15 10:26:38 -0400 | [diff] [blame] | 812 | public Properties(@NonNull String namespace, @Nullable Map<String, String> keyValueMap) { |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 813 | Preconditions.checkNotNull(namespace); |
| 814 | mNamespace = namespace; |
| 815 | mMap = new HashMap(); |
| 816 | if (keyValueMap != null) { |
| 817 | mMap.putAll(keyValueMap); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * @return the namespace all properties within this instance belong to. |
| 823 | */ |
| 824 | @NonNull |
| 825 | public String getNamespace() { |
| 826 | return mNamespace; |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * @return the non-null set of property names. |
| 831 | */ |
| 832 | @NonNull |
| 833 | public Set<String> getKeyset() { |
| 834 | return mMap.keySet(); |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * Look up the String value of a property. |
| 839 | * |
| 840 | * @param name The name of the property to look up. |
| 841 | * @param defaultValue The value to return if the property has not been defined. |
| 842 | * @return the corresponding value, or defaultValue if none exists. |
| 843 | */ |
| 844 | @Nullable |
| 845 | public String getString(@NonNull String name, @Nullable String defaultValue) { |
| 846 | Preconditions.checkNotNull(name); |
| 847 | String value = mMap.get(name); |
| 848 | return value != null ? value : defaultValue; |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * Look up the boolean value of a property. |
| 853 | * |
| 854 | * @param name The name of the property to look up. |
| 855 | * @param defaultValue The value to return if the property has not been defined. |
| 856 | * @return the corresponding value, or defaultValue if none exists. |
| 857 | */ |
| 858 | public boolean getBoolean(@NonNull String name, boolean defaultValue) { |
| 859 | Preconditions.checkNotNull(name); |
| 860 | String value = mMap.get(name); |
| 861 | return value != null ? Boolean.parseBoolean(value) : defaultValue; |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * Look up the int value of a property. |
| 866 | * |
| 867 | * @param name The name of the property to look up. |
| 868 | * @param defaultValue The value to return if the property has not been defined or fails to |
| 869 | * parse into an int. |
| 870 | * @return the corresponding value, or defaultValue if no valid int is available. |
| 871 | */ |
| 872 | public int getInt(@NonNull String name, int defaultValue) { |
| 873 | Preconditions.checkNotNull(name); |
| 874 | String value = mMap.get(name); |
Stanislav Zholnin | ffea1de | 2019-03-07 18:06:40 +0000 | [diff] [blame] | 875 | if (value == null) { |
| 876 | return defaultValue; |
| 877 | } |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 878 | try { |
| 879 | return Integer.parseInt(value); |
| 880 | } catch (NumberFormatException e) { |
Stanislav Zholnin | ffea1de | 2019-03-07 18:06:40 +0000 | [diff] [blame] | 881 | Log.e(TAG, "Parsing int failed for " + name); |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 882 | return defaultValue; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | /** |
| 887 | * Look up the long value of a property. |
| 888 | * |
| 889 | * @param name The name of the property to look up. |
| 890 | * @param defaultValue The value to return if the property has not been defined. or fails to |
| 891 | * parse into a long. |
| 892 | * @return the corresponding value, or defaultValue if no valid long is available. |
| 893 | */ |
| 894 | public long getLong(@NonNull String name, long defaultValue) { |
| 895 | Preconditions.checkNotNull(name); |
| 896 | String value = mMap.get(name); |
Stanislav Zholnin | ffea1de | 2019-03-07 18:06:40 +0000 | [diff] [blame] | 897 | if (value == null) { |
| 898 | return defaultValue; |
| 899 | } |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 900 | try { |
| 901 | return Long.parseLong(value); |
| 902 | } catch (NumberFormatException e) { |
Stanislav Zholnin | ffea1de | 2019-03-07 18:06:40 +0000 | [diff] [blame] | 903 | Log.e(TAG, "Parsing long failed for " + name); |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 904 | return defaultValue; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | /** |
| 909 | * Look up the int value of a property. |
| 910 | * |
| 911 | * @param name The name of the property to look up. |
| 912 | * @param defaultValue The value to return if the property has not been defined. or fails to |
| 913 | * parse into a float. |
| 914 | * @return the corresponding value, or defaultValue if no valid float is available. |
| 915 | */ |
| 916 | public float getFloat(@NonNull String name, float defaultValue) { |
| 917 | Preconditions.checkNotNull(name); |
| 918 | String value = mMap.get(name); |
Stanislav Zholnin | ffea1de | 2019-03-07 18:06:40 +0000 | [diff] [blame] | 919 | if (value == null) { |
| 920 | return defaultValue; |
| 921 | } |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 922 | try { |
| 923 | return Float.parseFloat(value); |
| 924 | } catch (NumberFormatException e) { |
Stanislav Zholnin | ffea1de | 2019-03-07 18:06:40 +0000 | [diff] [blame] | 925 | Log.e(TAG, "Parsing float failed for " + name); |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 926 | return defaultValue; |
| 927 | } |
| 928 | } |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 929 | } |
| 930 | } |