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; |
| 30 | import android.database.ContentObserver; |
| 31 | import android.net.Uri; |
| 32 | import android.provider.Settings.ResetMode; |
| 33 | import android.util.Pair; |
| 34 | |
| 35 | import com.android.internal.annotations.GuardedBy; |
| 36 | |
| 37 | import java.util.HashMap; |
| 38 | import java.util.List; |
| 39 | import java.util.Map; |
| 40 | import java.util.concurrent.Executor; |
| 41 | |
| 42 | /** |
| 43 | * Device level configuration parameters which can be tuned by a separate configuration service. |
| 44 | * |
| 45 | * @hide |
| 46 | */ |
| 47 | @SystemApi |
Philip P. Moltmann | 25995fe | 2019-01-25 14:33:11 -0800 | [diff] [blame] | 48 | @TestApi |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 49 | public final class DeviceConfig { |
| 50 | /** |
| 51 | * The content:// style URL for the config table. |
| 52 | * |
| 53 | * @hide |
| 54 | */ |
| 55 | public static final Uri CONTENT_URI = Uri.parse("content://" + Settings.AUTHORITY + "/config"); |
| 56 | |
Siarhei Vishniakou | 39a1a98 | 2019-01-11 09:22:32 -0800 | [diff] [blame] | 57 | /** |
Peiyong Lin | 31fa8ac | 2019-01-09 13:35:48 -0800 | [diff] [blame] | 58 | * Namespace for all Game Driver features. |
Alex Salo | cbd05e6 | 2019-01-23 18:45:22 -0800 | [diff] [blame] | 59 | * |
Peiyong Lin | 31fa8ac | 2019-01-09 13:35:48 -0800 | [diff] [blame] | 60 | * @hide |
| 61 | */ |
| 62 | @SystemApi |
| 63 | public static final String NAMESPACE_GAME_DRIVER = "game_driver"; |
| 64 | |
| 65 | /** |
Perumaal S | 5c016da | 2019-01-16 21:33:22 -0800 | [diff] [blame] | 66 | * Namespace for autofill feature that provides suggestions across all apps when |
| 67 | * the user interacts with input fields. |
| 68 | * |
| 69 | * @hide |
| 70 | */ |
| 71 | @SystemApi |
Felipe Leme | cc51027 | 2019-02-07 14:56:27 -0800 | [diff] [blame] | 72 | @TestApi |
Perumaal S | 5c016da | 2019-01-16 21:33:22 -0800 | [diff] [blame] | 73 | public static final String NAMESPACE_AUTOFILL = "autofill"; |
| 74 | |
| 75 | /** |
Felipe Leme | 14ef461 | 2019-02-07 12:24:38 -0800 | [diff] [blame] | 76 | * Namespace for content capture feature used by on-device machine intelligence |
| 77 | * to provide suggestions in a privacy-safe manner. |
Felipe Leme | 70bcf38 | 2019-01-24 14:55:03 -0800 | [diff] [blame] | 78 | * |
| 79 | * @hide |
| 80 | */ |
| 81 | @SystemApi |
Felipe Leme | d03283d | 2019-02-05 09:42:42 -0800 | [diff] [blame] | 82 | @TestApi |
Felipe Leme | 14ef461 | 2019-02-07 12:24:38 -0800 | [diff] [blame] | 83 | public static final String NAMESPACE_CONTENT_CAPTURE = "content_capture"; |
Felipe Leme | 70bcf38 | 2019-01-24 14:55:03 -0800 | [diff] [blame] | 84 | |
| 85 | /** |
Siarhei Vishniakou | 39a1a98 | 2019-01-11 09:22:32 -0800 | [diff] [blame] | 86 | * Namespace for all input-related features that are used at the native level. |
| 87 | * These features are applied at reboot. |
| 88 | * |
| 89 | * @hide |
| 90 | */ |
| 91 | @SystemApi |
| 92 | public static final String NAMESPACE_INPUT_NATIVE_BOOT = "input_native_boot"; |
| 93 | |
chenbruce | db279e8 | 2018-12-27 17:12:31 +0800 | [diff] [blame] | 94 | /** |
| 95 | * Namespace for all netd related features. |
| 96 | * |
| 97 | * @hide |
| 98 | */ |
| 99 | @SystemApi |
| 100 | public static final String NAMESPACE_NETD_NATIVE = "netd_native"; |
| 101 | |
Gustav Sennton | 1aebc9b | 2019-01-22 17:56:18 +0000 | [diff] [blame] | 102 | /** |
| 103 | * Namespace for features related to the ExtServices Notification Assistant. |
| 104 | * These features are applied immediately. |
| 105 | * |
| 106 | * @hide |
| 107 | */ |
| 108 | @SystemApi |
Gustav Sennton | 73a8c1b | 2019-01-23 18:15:39 +0000 | [diff] [blame] | 109 | public interface NotificationAssistant { |
| 110 | String NAMESPACE = "notification_assistant"; |
| 111 | /** |
| 112 | * Whether the Notification Assistant should generate replies for notifications. |
| 113 | */ |
| 114 | String GENERATE_REPLIES = "generate_replies"; |
| 115 | /** |
| 116 | * Whether the Notification Assistant should generate contextual actions for notifications. |
| 117 | */ |
| 118 | String GENERATE_ACTIONS = "generate_actions"; |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 119 | |
| 120 | String MAX_MESSAGES_TO_EXTRACT = "max_messages_to_extract"; |
| 121 | |
| 122 | String MAX_SUGGESTIONS = "max_suggestions"; |
Gustav Sennton | 73a8c1b | 2019-01-23 18:15:39 +0000 | [diff] [blame] | 123 | } |
Gustav Sennton | 1aebc9b | 2019-01-22 17:56:18 +0000 | [diff] [blame] | 124 | |
Yiwen Chen | 3d3ad02 | 2019-01-22 21:15:56 -0800 | [diff] [blame] | 125 | /** |
Mathieu Chartier | 743b363 | 2019-02-07 13:14:19 -0800 | [diff] [blame] | 126 | * Namespace for all runtime related features. |
| 127 | * |
| 128 | * @hide |
| 129 | */ |
| 130 | @SystemApi |
| 131 | public interface Runtime { |
| 132 | String NAMESPACE = "runtime"; |
| 133 | |
| 134 | /** |
| 135 | * Whether or not we use the precompiled layout. |
| 136 | */ |
| 137 | String USE_PRECOMPILED_LAYOUT = "view.precompiled_layout_enabled"; |
| 138 | } |
| 139 | |
| 140 | /** |
Mathieu Chartier | 7f11eb9 | 2019-01-25 16:08:21 -0800 | [diff] [blame] | 141 | * Namespace for all runtime native related features. |
| 142 | * |
| 143 | * @hide |
| 144 | */ |
| 145 | @SystemApi |
| 146 | public interface RuntimeNative { |
| 147 | String NAMESPACE = "runtime_native"; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * Zygote flags. See {@link com.internal.os.Zygote}. |
| 151 | */ |
| 152 | |
| 153 | /** |
| 154 | * If {@code true}, enables the blastula pool feature. |
| 155 | * |
| 156 | * @hide for internal use only |
| 157 | */ |
| 158 | String BLASTULA_POOL_ENABLED = "blastula_pool_enabled"; |
| 159 | |
| 160 | /** |
| 161 | * The maximum number of processes to keep in the blastula pool. |
| 162 | * |
| 163 | * @hide for internal use only |
| 164 | */ |
| 165 | String BLASTULA_POOL_SIZE_MAX = "blastula_pool_size_max"; |
| 166 | |
| 167 | /** |
| 168 | * The minimum number of processes to keep in the blastula pool. |
| 169 | * |
| 170 | * @hide for internal use only |
| 171 | */ |
Chris Wailes | 2c953c8 | 2019-02-21 11:09:01 -0800 | [diff] [blame] | 172 | String BLASTULA_POOL_SIZE_MIN = "blastula_pool_size_min"; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 173 | |
| 174 | /** |
| 175 | * The threshold used to determine if the pool should be refilled. |
| 176 | * |
| 177 | * @hide for internal use only |
| 178 | */ |
| 179 | String BLASTULA_POOL_REFILL_THRESHOLD = "blastula_refill_threshold"; |
Mathieu Chartier | 7f11eb9 | 2019-01-25 16:08:21 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
Mathieu Chartier | b31138f | 2019-02-01 16:51:12 -0800 | [diff] [blame] | 183 | * Namespace for all runtime native boot related features. Boot in this case refers to the |
| 184 | * fact that the properties only take affect after rebooting the device. |
| 185 | * |
| 186 | * @hide |
| 187 | */ |
| 188 | @SystemApi |
| 189 | public interface RuntimeNativeBoot { |
| 190 | String NAMESPACE = "runtime_native_boot"; |
| 191 | } |
| 192 | |
| 193 | /** |
Dongwon Kang | 5ebb265 | 2019-01-30 15:26:46 -0800 | [diff] [blame] | 194 | * Namespace for all media native related features. |
| 195 | * |
| 196 | * @hide |
| 197 | */ |
| 198 | @SystemApi |
| 199 | public interface MediaNative { |
Dongwon Kang | 2ee0891 | 2019-02-26 14:16:08 -0800 | [diff] [blame] | 200 | /** The flag namespace for media native features. */ |
Dongwon Kang | 5ebb265 | 2019-01-30 15:26:46 -0800 | [diff] [blame] | 201 | String NAMESPACE = "media_native"; |
| 202 | } |
| 203 | |
| 204 | /** |
Ng Zhi An | e1bff3f | 2019-01-17 12:38:17 -0800 | [diff] [blame] | 205 | * Namespace for all activity manager related features that are used at the native level. |
| 206 | * These features are applied at reboot. |
| 207 | * |
| 208 | * @hide |
| 209 | */ |
| 210 | @SystemApi |
| 211 | public interface ActivityManagerNativeBoot { |
| 212 | String NAMESPACE = "activity_manager_native_boot"; |
| 213 | String OFFLOAD_QUEUE_ENABLED = "offload_queue_enabled"; |
| 214 | } |
| 215 | |
| 216 | /** |
Alex Salo | cbd05e6 | 2019-01-23 18:45:22 -0800 | [diff] [blame] | 217 | * Namespace for attention-based features provided by on-device machine intelligence. |
| 218 | * |
| 219 | * @hide |
| 220 | */ |
| 221 | @SystemApi |
| 222 | public interface IntelligenceAttention { |
| 223 | String NAMESPACE = "intelligence_attention"; |
Alex Salo | 440fe3d | 2019-01-25 11:50:38 -0800 | [diff] [blame] | 224 | |
| 225 | /** If {@code true}, enables the attention features. */ |
Alex Salo | c7b9c08 | 2019-01-29 13:10:55 -0800 | [diff] [blame] | 226 | String ATTENTION_ENABLED = "attention_enabled"; |
Alex Salo | 440fe3d | 2019-01-25 11:50:38 -0800 | [diff] [blame] | 227 | |
| 228 | /** Settings for the attention features. */ |
Alex Salo | c7b9c08 | 2019-01-29 13:10:55 -0800 | [diff] [blame] | 229 | String ATTENTION_SETTINGS = "attention_settings"; |
Alex Salo | cbd05e6 | 2019-01-23 18:45:22 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /** |
Joel Galenson | 489239b | 2019-01-23 10:05:03 -0800 | [diff] [blame] | 233 | * Privacy related properties definitions. |
| 234 | * |
| 235 | * @hide |
| 236 | */ |
| 237 | @SystemApi |
Philip P. Moltmann | 25995fe | 2019-01-25 14:33:11 -0800 | [diff] [blame] | 238 | @TestApi |
Joel Galenson | 489239b | 2019-01-23 10:05:03 -0800 | [diff] [blame] | 239 | public interface Privacy { |
| 240 | String NAMESPACE = "privacy"; |
| 241 | |
| 242 | /** |
| 243 | * Whether to show the Permissions Hub. |
Philip P. Moltmann | 25995fe | 2019-01-25 14:33:11 -0800 | [diff] [blame] | 244 | * |
| 245 | * @hide |
Joel Galenson | 489239b | 2019-01-23 10:05:03 -0800 | [diff] [blame] | 246 | */ |
Philip P. Moltmann | 25995fe | 2019-01-25 14:33:11 -0800 | [diff] [blame] | 247 | @SystemApi |
Joel Galenson | d382ad4 | 2019-01-29 15:38:39 -0800 | [diff] [blame] | 248 | String PROPERTY_PERMISSIONS_HUB_ENABLED = "permissions_hub_enabled"; |
Philip P. Moltmann | 25995fe | 2019-01-25 14:33:11 -0800 | [diff] [blame] | 249 | |
| 250 | /** |
| 251 | * Whether to show location access check notifications. |
| 252 | */ |
Joel Galenson | c4d8921 | 2019-01-30 10:02:24 -0800 | [diff] [blame] | 253 | String PROPERTY_LOCATION_ACCESS_CHECK_ENABLED = "location_access_check_enabled"; |
Michael Groover | 0dbc35d | 2019-01-29 17:34:41 -0800 | [diff] [blame] | 254 | |
| 255 | /** |
| 256 | * Whether to disable the new device identifier access restrictions. |
| 257 | * |
| 258 | * @hide |
| 259 | */ |
| 260 | String PROPERTY_DEVICE_IDENTIFIER_ACCESS_RESTRICTIONS_DISABLED = |
| 261 | "device_identifier_access_restrictions_disabled"; |
Joel Galenson | 489239b | 2019-01-23 10:05:03 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /** |
Yiwen Chen | 3d3ad02 | 2019-01-22 21:15:56 -0800 | [diff] [blame] | 265 | * Telephony related properties definitions. |
| 266 | * |
| 267 | * @hide |
| 268 | */ |
| 269 | @SystemApi |
| 270 | public interface Telephony { |
| 271 | String NAMESPACE = "telephony"; |
| 272 | /** |
Yiwen Chen | 3d3ad02 | 2019-01-22 21:15:56 -0800 | [diff] [blame] | 273 | * Ringer ramping time in milliseconds. |
| 274 | */ |
Yiwen Chen | 555b83d | 2019-01-28 14:13:25 -0800 | [diff] [blame] | 275 | String RAMPING_RINGER_DURATION = "ramping_ringer_duration"; |
| 276 | /** |
| 277 | * Whether to apply ramping ringer on incoming phone calls. |
| 278 | */ |
| 279 | String RAMPING_RINGER_ENABLED = "ramping_ringer_enabled"; |
| 280 | /** |
| 281 | * Vibration time in milliseconds before ramping ringer starts. |
| 282 | */ |
| 283 | String RAMPING_RINGER_VIBRATION_DURATION = "ramping_ringer_vibration_duration"; |
Yiwen Chen | 3d3ad02 | 2019-01-22 21:15:56 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Victor Hsieh | 293595f | 2019-01-10 09:33:21 -0800 | [diff] [blame] | 286 | /** |
Victor Hsieh | 51c4403 | 2019-01-29 11:57:00 -0800 | [diff] [blame] | 287 | * Namespace for how dex runs. The feature may requires reboot to a clean state. |
Victor Hsieh | 293595f | 2019-01-10 09:33:21 -0800 | [diff] [blame] | 288 | * |
| 289 | * @hide |
| 290 | */ |
| 291 | @SystemApi |
Victor Hsieh | 51c4403 | 2019-01-29 11:57:00 -0800 | [diff] [blame] | 292 | public interface DexBoot { |
| 293 | String NAMESPACE = "dex_boot"; |
| 294 | String PRIV_APPS_OOB_ENABLED = "priv_apps_oob_enabled"; |
| 295 | String PRIV_APPS_OOB_WHITELIST = "priv_apps_oob_whitelist"; |
Victor Hsieh | 293595f | 2019-01-10 09:33:21 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Ben Murdoch | c26a5a8 | 2019-01-16 10:05:58 +0000 | [diff] [blame] | 298 | /** |
| 299 | * Namespace for activity manager related features. These features will be applied |
| 300 | * immediately upon change. |
| 301 | * |
| 302 | * @hide |
| 303 | */ |
| 304 | @SystemApi |
| 305 | public interface ActivityManager { |
| 306 | String NAMESPACE = "activity_manager"; |
| 307 | |
| 308 | /** |
| 309 | * App compaction flags. See {@link com.android.server.am.AppCompactor}. |
| 310 | */ |
| 311 | String KEY_USE_COMPACTION = "use_compaction"; |
| 312 | String KEY_COMPACT_ACTION_1 = "compact_action_1"; |
| 313 | String KEY_COMPACT_ACTION_2 = "compact_action_2"; |
| 314 | String KEY_COMPACT_THROTTLE_1 = "compact_throttle_1"; |
| 315 | String KEY_COMPACT_THROTTLE_2 = "compact_throttle_2"; |
| 316 | String KEY_COMPACT_THROTTLE_3 = "compact_throttle_3"; |
| 317 | String KEY_COMPACT_THROTTLE_4 = "compact_throttle_4"; |
Ben Murdoch | 2384df7 | 2019-01-28 16:13:55 +0000 | [diff] [blame] | 318 | String KEY_COMPACT_STATSD_SAMPLE_RATE = "compact_statsd_sample_rate"; |
Ben Murdoch | c26a5a8 | 2019-01-16 10:05:58 +0000 | [diff] [blame] | 319 | |
| 320 | /** |
| 321 | * Maximum number of cached processes. See |
| 322 | * {@link com.android.server.am.ActivityManagerConstants}. |
| 323 | */ |
| 324 | String KEY_MAX_CACHED_PROCESSES = "max_cached_processes"; |
| 325 | } |
| 326 | |
Jeff Sharkey | 5d0c55c | 2019-01-24 14:32:31 -0700 | [diff] [blame] | 327 | /** |
Alex Salo | 440fe3d | 2019-01-25 11:50:38 -0800 | [diff] [blame] | 328 | * Namespace for {@link AttentionManagerService} related features. |
| 329 | * |
| 330 | * @hide |
| 331 | */ |
| 332 | @SystemApi |
| 333 | public interface AttentionManagerService { |
| 334 | String NAMESPACE = "attention_manager_service"; |
| 335 | |
| 336 | /** If {@code true}, enables {@link AttentionManagerService} features. */ |
Alex Salo | c7b9c08 | 2019-01-29 13:10:55 -0800 | [diff] [blame] | 337 | String SERVICE_ENABLED = "service_enabled"; |
Alex Salo | 440fe3d | 2019-01-25 11:50:38 -0800 | [diff] [blame] | 338 | |
| 339 | /** Allows a CTS to inject a fake implementation. */ |
Alex Salo | c7b9c08 | 2019-01-29 13:10:55 -0800 | [diff] [blame] | 340 | String COMPONENT_NAME = "component_name"; |
Alex Salo | 440fe3d | 2019-01-25 11:50:38 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** |
Mohammad Samiul Islam | 794f03d | 2019-01-11 15:43:15 +0000 | [diff] [blame] | 344 | * Namespace for Rollback. |
| 345 | * |
| 346 | * @hide |
| 347 | */ |
| 348 | @SystemApi |
| 349 | public interface Rollback { |
| 350 | String NAMESPACE = "rollback"; |
| 351 | |
shafik | 0ad18b8 | 2019-01-24 16:27:24 +0000 | [diff] [blame] | 352 | String BOOT_NAMESPACE = "rollback_boot"; |
| 353 | |
Mohammad Samiul Islam | 794f03d | 2019-01-11 15:43:15 +0000 | [diff] [blame] | 354 | /** |
| 355 | * Timeout in milliseconds for enabling package rollback. |
| 356 | */ |
| 357 | String ENABLE_ROLLBACK_TIMEOUT = "enable_rollback_timeout"; |
shafik | 0ad18b8 | 2019-01-24 16:27:24 +0000 | [diff] [blame] | 358 | |
| 359 | /** |
| 360 | * The lifetime duration of rollback packages in millis |
| 361 | */ |
| 362 | String ROLLBACK_LIFETIME_IN_MILLIS = "rollback_lifetime_in_millis"; |
Mohammad Samiul Islam | 794f03d | 2019-01-11 15:43:15 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /** |
Jeff Sharkey | 5d0c55c | 2019-01-24 14:32:31 -0700 | [diff] [blame] | 366 | * Namespace for storage-related features. |
| 367 | * |
| 368 | * @hide |
| 369 | */ |
| 370 | @SystemApi |
| 371 | public interface Storage { |
| 372 | String NAMESPACE = "storage"; |
| 373 | |
| 374 | /** |
| 375 | * If {@code 1}, enables the isolated storage feature. If {@code -1}, |
| 376 | * disables the isolated storage feature. If {@code 0}, uses the default |
| 377 | * value from the build system. |
| 378 | */ |
| 379 | String ISOLATED_STORAGE_ENABLED = "isolated_storage_enabled"; |
| 380 | } |
| 381 | |
Hongyi Zhang | 29301d3 | 2019-01-18 17:07:38 -0800 | [diff] [blame] | 382 | /** |
| 383 | * Namespace for system scheduler related features. These features will be applied |
| 384 | * immediately upon change. |
| 385 | * |
| 386 | * @hide |
| 387 | */ |
| 388 | @SystemApi |
| 389 | public interface Scheduler { |
| 390 | String NAMESPACE = "scheduler"; |
| 391 | |
| 392 | /** |
| 393 | * Flag for enabling fast metrics collection in system scheduler. |
| 394 | * A flag value of '' or '0' means the fast metrics collection is not |
| 395 | * enabled. Otherwise fast metrics collection is enabled and flag value |
| 396 | * is the order id. |
| 397 | */ |
| 398 | String ENABLE_FAST_METRICS_COLLECTION = "enable_fast_metrics_collection"; |
| 399 | } |
| 400 | |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 401 | private static final Object sLock = new Object(); |
| 402 | @GuardedBy("sLock") |
| 403 | private static Map<OnPropertyChangedListener, Pair<String, Executor>> sListeners = |
| 404 | new HashMap<>(); |
| 405 | @GuardedBy("sLock") |
| 406 | private static Map<String, Pair<ContentObserver, Integer>> sNamespaces = new HashMap<>(); |
| 407 | |
| 408 | // Should never be invoked |
| 409 | private DeviceConfig() { |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Look up the value of a property for a particular namespace. |
| 414 | * |
| 415 | * @param namespace The namespace containing the property to look up. |
Alex Salo | cbd05e6 | 2019-01-23 18:45:22 -0800 | [diff] [blame] | 416 | * @param name The name of the property to look up. |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 417 | * @return the corresponding value, or null if not present. |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 418 | * @hide |
| 419 | */ |
| 420 | @SystemApi |
Felipe Leme | d03283d | 2019-02-05 09:42:42 -0800 | [diff] [blame] | 421 | @TestApi |
Stanislav Zholnin | 596437f | 2018-12-28 15:34:23 +0000 | [diff] [blame] | 422 | @RequiresPermission(READ_DEVICE_CONFIG) |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 423 | public static String getProperty(String namespace, String name) { |
| 424 | ContentResolver contentResolver = ActivityThread.currentApplication().getContentResolver(); |
| 425 | String compositeName = createCompositeName(namespace, name); |
| 426 | return Settings.Config.getString(contentResolver, compositeName); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Create a new property with the the provided name and value in the provided namespace, or |
| 431 | * update the value of such a property if it already exists. The same name can exist in multiple |
| 432 | * namespaces and might have different values in any or all namespaces. |
| 433 | * <p> |
| 434 | * The method takes an argument indicating whether to make the value the default for this |
| 435 | * property. |
| 436 | * <p> |
| 437 | * All properties stored for a particular scope can be reverted to their default values |
| 438 | * by passing the namespace to {@link #resetToDefaults(int, String)}. |
| 439 | * |
Alex Salo | cbd05e6 | 2019-01-23 18:45:22 -0800 | [diff] [blame] | 440 | * @param namespace The namespace containing the property to create or update. |
| 441 | * @param name The name of the property to create or update. |
| 442 | * @param value The value to store for the property. |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 443 | * @param makeDefault Whether to make the new value the default one. |
| 444 | * @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] | 445 | * @hide |
Alex Salo | cbd05e6 | 2019-01-23 18:45:22 -0800 | [diff] [blame] | 446 | * @see #resetToDefaults(int, String). |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 447 | */ |
| 448 | @SystemApi |
Philip P. Moltmann | 25995fe | 2019-01-25 14:33:11 -0800 | [diff] [blame] | 449 | @TestApi |
Stanislav Zholnin | 596437f | 2018-12-28 15:34:23 +0000 | [diff] [blame] | 450 | @RequiresPermission(WRITE_DEVICE_CONFIG) |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 451 | public static boolean setProperty( |
| 452 | String namespace, String name, String value, boolean makeDefault) { |
| 453 | ContentResolver contentResolver = ActivityThread.currentApplication().getContentResolver(); |
| 454 | String compositeName = createCompositeName(namespace, name); |
| 455 | return Settings.Config.putString(contentResolver, compositeName, value, makeDefault); |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Reset properties to their default values. |
| 460 | * <p> |
| 461 | * The method accepts an optional namespace parameter. If provided, only properties set within |
| 462 | * that namespace will be reset. Otherwise, all properties will be reset. |
| 463 | * |
| 464 | * @param resetMode The reset mode to use. |
| 465 | * @param namespace Optionally, the specific namespace which resets will be limited to. |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 466 | * @hide |
Alex Salo | cbd05e6 | 2019-01-23 18:45:22 -0800 | [diff] [blame] | 467 | * @see #setProperty(String, String, String, boolean) |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 468 | */ |
| 469 | @SystemApi |
Philip P. Moltmann | 25995fe | 2019-01-25 14:33:11 -0800 | [diff] [blame] | 470 | @TestApi |
Stanislav Zholnin | 596437f | 2018-12-28 15:34:23 +0000 | [diff] [blame] | 471 | @RequiresPermission(WRITE_DEVICE_CONFIG) |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 472 | public static void resetToDefaults(@ResetMode int resetMode, @Nullable String namespace) { |
| 473 | ContentResolver contentResolver = ActivityThread.currentApplication().getContentResolver(); |
| 474 | Settings.Config.resetToDefaults(contentResolver, resetMode, namespace); |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Add a listener for property changes. |
| 479 | * <p> |
| 480 | * This listener will be called whenever properties in the specified namespace change. Callbacks |
| 481 | * will be made on the specified executor. Future calls to this method with the same listener |
| 482 | * will replace the old namespace and executor. Remove the listener entirely by calling |
| 483 | * {@link #removeOnPropertyChangedListener(OnPropertyChangedListener)}. |
| 484 | * |
Alex Salo | cbd05e6 | 2019-01-23 18:45:22 -0800 | [diff] [blame] | 485 | * @param namespace The namespace containing properties to monitor. |
| 486 | * @param executor The executor which will be used to run callbacks. |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 487 | * @param onPropertyChangedListener The listener to add. |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 488 | * @hide |
Alex Salo | cbd05e6 | 2019-01-23 18:45:22 -0800 | [diff] [blame] | 489 | * @see #removeOnPropertyChangedListener(OnPropertyChangedListener) |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 490 | */ |
| 491 | @SystemApi |
Felipe Leme | cc51027 | 2019-02-07 14:56:27 -0800 | [diff] [blame] | 492 | @TestApi |
Stanislav Zholnin | 596437f | 2018-12-28 15:34:23 +0000 | [diff] [blame] | 493 | @RequiresPermission(READ_DEVICE_CONFIG) |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 494 | public static void addOnPropertyChangedListener( |
| 495 | @NonNull String namespace, |
| 496 | @NonNull @CallbackExecutor Executor executor, |
| 497 | @NonNull OnPropertyChangedListener onPropertyChangedListener) { |
Stanislav Zholnin | 596437f | 2018-12-28 15:34:23 +0000 | [diff] [blame] | 498 | // TODO enforce READ_DEVICE_CONFIG permission |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 499 | synchronized (sLock) { |
| 500 | Pair<String, Executor> oldNamespace = sListeners.get(onPropertyChangedListener); |
| 501 | if (oldNamespace == null) { |
| 502 | // Brand new listener, add it to the list. |
| 503 | sListeners.put(onPropertyChangedListener, new Pair<>(namespace, executor)); |
| 504 | incrementNamespace(namespace); |
| 505 | } else if (namespace.equals(oldNamespace.first)) { |
| 506 | // Listener is already registered for this namespace, update executor just in case. |
| 507 | sListeners.put(onPropertyChangedListener, new Pair<>(namespace, executor)); |
| 508 | } else { |
| 509 | // Update this listener from an old namespace to the new one. |
| 510 | decrementNamespace(sListeners.get(onPropertyChangedListener).first); |
| 511 | sListeners.put(onPropertyChangedListener, new Pair<>(namespace, executor)); |
| 512 | incrementNamespace(namespace); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Remove a listener for property changes. The listener will receive no further notification of |
| 519 | * property changes. |
| 520 | * |
| 521 | * @param onPropertyChangedListener The listener to remove. |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 522 | * @hide |
Alex Salo | cbd05e6 | 2019-01-23 18:45:22 -0800 | [diff] [blame] | 523 | * @see #addOnPropertyChangedListener(String, Executor, OnPropertyChangedListener) |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 524 | */ |
| 525 | @SystemApi |
Felipe Leme | cc51027 | 2019-02-07 14:56:27 -0800 | [diff] [blame] | 526 | @TestApi |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 527 | public static void removeOnPropertyChangedListener( |
| 528 | OnPropertyChangedListener onPropertyChangedListener) { |
| 529 | synchronized (sLock) { |
| 530 | if (sListeners.containsKey(onPropertyChangedListener)) { |
| 531 | decrementNamespace(sListeners.get(onPropertyChangedListener).first); |
| 532 | sListeners.remove(onPropertyChangedListener); |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | private static String createCompositeName(String namespace, String name) { |
| 538 | return namespace + "/" + name; |
| 539 | } |
| 540 | |
| 541 | private static Uri createNamespaceUri(String namespace) { |
| 542 | return CONTENT_URI.buildUpon().appendPath(namespace).build(); |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Increment the count used to represent the number of listeners subscribed to the given |
| 547 | * namespace. If this is the first (i.e. incrementing from 0 to 1) for the given namespace, a |
| 548 | * ContentObserver is registered. |
| 549 | * |
| 550 | * @param namespace The namespace to increment the count for. |
| 551 | */ |
| 552 | @GuardedBy("sLock") |
| 553 | private static void incrementNamespace(String namespace) { |
| 554 | Pair<ContentObserver, Integer> namespaceCount = sNamespaces.get(namespace); |
| 555 | if (namespaceCount != null) { |
| 556 | sNamespaces.put(namespace, new Pair<>(namespaceCount.first, namespaceCount.second + 1)); |
| 557 | } else { |
| 558 | // This is a new namespace, register a ContentObserver for it. |
| 559 | ContentObserver contentObserver = new ContentObserver(null) { |
| 560 | @Override |
| 561 | public void onChange(boolean selfChange, Uri uri) { |
| 562 | handleChange(uri); |
| 563 | } |
| 564 | }; |
| 565 | ActivityThread.currentApplication().getContentResolver() |
| 566 | .registerContentObserver(createNamespaceUri(namespace), true, contentObserver); |
| 567 | sNamespaces.put(namespace, new Pair<>(contentObserver, 1)); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Decrement the count used to represent th enumber of listeners subscribed to the given |
| 573 | * namespace. If this is the final decrement call (i.e. decrementing from 1 to 0) for the given |
| 574 | * namespace, the ContentObserver that had been tracking it will be removed. |
| 575 | * |
| 576 | * @param namespace The namespace to decrement the count for. |
| 577 | */ |
| 578 | @GuardedBy("sLock") |
| 579 | private static void decrementNamespace(String namespace) { |
| 580 | Pair<ContentObserver, Integer> namespaceCount = sNamespaces.get(namespace); |
| 581 | if (namespaceCount == null) { |
| 582 | // This namespace is not registered and does not need to be decremented |
| 583 | return; |
| 584 | } else if (namespaceCount.second > 1) { |
| 585 | sNamespaces.put(namespace, new Pair<>(namespaceCount.first, namespaceCount.second - 1)); |
| 586 | } else { |
| 587 | // Decrementing a namespace to zero means we no longer need its ContentObserver. |
| 588 | ActivityThread.currentApplication().getContentResolver() |
| 589 | .unregisterContentObserver(namespaceCount.first); |
| 590 | sNamespaces.remove(namespace); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | private static void handleChange(Uri uri) { |
| 595 | List<String> pathSegments = uri.getPathSegments(); |
| 596 | // pathSegments(0) is "config" |
| 597 | String namespace = pathSegments.get(1); |
| 598 | String name = pathSegments.get(2); |
| 599 | String value = getProperty(namespace, name); |
| 600 | synchronized (sLock) { |
| 601 | for (OnPropertyChangedListener listener : sListeners.keySet()) { |
| 602 | if (namespace.equals(sListeners.get(listener).first)) { |
| 603 | sListeners.get(listener).second.execute(new Runnable() { |
| 604 | @Override |
| 605 | public void run() { |
| 606 | listener.onPropertyChanged(namespace, name, value); |
| 607 | } |
| 608 | |
| 609 | }); |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * Interface for monitoring to properties. |
| 617 | * <p> |
| 618 | * Override {@link #onPropertyChanged(String, String, String)} to handle callbacks for changes. |
Philip P. Moltmann | 25995fe | 2019-01-25 14:33:11 -0800 | [diff] [blame] | 619 | * |
| 620 | * @hide |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 621 | */ |
Philip P. Moltmann | 25995fe | 2019-01-25 14:33:11 -0800 | [diff] [blame] | 622 | @SystemApi |
Felipe Leme | cc51027 | 2019-02-07 14:56:27 -0800 | [diff] [blame] | 623 | @TestApi |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 624 | public interface OnPropertyChangedListener { |
| 625 | /** |
| 626 | * Called when a property has changed. |
| 627 | * |
| 628 | * @param namespace The namespace containing the property which has changed. |
Alex Salo | cbd05e6 | 2019-01-23 18:45:22 -0800 | [diff] [blame] | 629 | * @param name The name of the property which has changed. |
| 630 | * @param value The new value of the property which has changed. |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 631 | */ |
| 632 | void onPropertyChanged(String namespace, String name, String value); |
| 633 | } |
| 634 | } |