1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.util.settings;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.content.ContentResolver;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.UserHandle;
import android.provider.Settings;
import com.android.systemui.settings.UserTracker;
/**
* Used to interact with Settings.Secure, Settings.Global, and Settings.System.
*
* This interface can be implemented to give instance method (instead of static method) versions
* of Settings.Secure, Settings.Global, and Settings.System. It can be injected into class
* constructors and then faked or mocked as needed in tests.
*
* You can ask for {@link SecureSettings}, {@link GlobalSettings}, or {@link SystemSettings} to be
* injected as needed.
*
* This class also provides {@link #registerContentObserver(String, ContentObserver)} methods,
* normally found on {@link ContentResolver} instances, unifying setting related actions in one
* place.
*/
public interface SettingsProxy {
/**
* Returns the {@link ContentResolver} this instance was constructed with.
*/
ContentResolver getContentResolver();
/**
* Returns that {@link UserTracker} this instance was constructed with.
*/
UserTracker getUserTracker();
/**
* Returns the user id for the associated {@link ContentResolver}.
*/
default int getUserId() {
return getContentResolver().getUserId();
}
/**
* Returns the actual current user handle when querying with the current user. Otherwise,
* returns the passed in user id.
*/
default int getRealUserHandle(int userHandle) {
if (userHandle != UserHandle.USER_CURRENT) {
return userHandle;
}
return getUserTracker().getUserId();
}
/**
* Construct the content URI for a particular name/value pair,
* useful for monitoring changes with a ContentObserver.
* @param name to look up in the table
* @return the corresponding content URI, or null if not present
*/
Uri getUriFor(String name);
/**
* Convenience wrapper around
* {@link ContentResolver#registerContentObserver(Uri, boolean, ContentObserver)}.'
*
* Implicitly calls {@link #getUriFor(String)} on the passed in name.
*/
default void registerContentObserver(String name, ContentObserver settingsObserver) {
registerContentObserver(getUriFor(name), settingsObserver);
}
/**
* Convenience wrapper around
* {@link ContentResolver#registerContentObserver(Uri, boolean, ContentObserver)}.'
*/
default void registerContentObserver(Uri uri, ContentObserver settingsObserver) {
registerContentObserverForUser(uri, settingsObserver, getUserId());
}
/**
* Convenience wrapper around
* {@link ContentResolver#registerContentObserver(Uri, boolean, ContentObserver)}.'
*
* Implicitly calls {@link #getUriFor(String)} on the passed in name.
*/
default void registerContentObserver(String name, boolean notifyForDescendants,
ContentObserver settingsObserver) {
registerContentObserver(getUriFor(name), notifyForDescendants, settingsObserver);
}
/**
* Convenience wrapper around
* {@link ContentResolver#registerContentObserver(Uri, boolean, ContentObserver)}.'
*/
default void registerContentObserver(Uri uri, boolean notifyForDescendants,
ContentObserver settingsObserver) {
registerContentObserverForUser(uri, notifyForDescendants, settingsObserver, getUserId());
}
/**
* Convenience wrapper around
* {@link ContentResolver#registerContentObserver(Uri, boolean, ContentObserver, int)}
*
* Implicitly calls {@link #getUriFor(String)} on the passed in name.
*/
default void registerContentObserverForUser(
String name, ContentObserver settingsObserver, int userHandle) {
registerContentObserverForUser(
getUriFor(name), settingsObserver, userHandle);
}
/**
* Convenience wrapper around
* {@link ContentResolver#registerContentObserver(Uri, boolean, ContentObserver, int)}
*/
default void registerContentObserverForUser(
Uri uri, ContentObserver settingsObserver, int userHandle) {
registerContentObserverForUser(
uri, false, settingsObserver, userHandle);
}
/**
* Convenience wrapper around
* {@link ContentResolver#registerContentObserver(Uri, boolean, ContentObserver, int)}
*
* Implicitly calls {@link #getUriFor(String)} on the passed in name.
*/
default void registerContentObserverForUser(
String name, boolean notifyForDescendants, ContentObserver settingsObserver,
int userHandle) {
registerContentObserverForUser(
getUriFor(name), notifyForDescendants, settingsObserver, userHandle);
}
/**
* Convenience wrapper around
* {@link ContentResolver#registerContentObserver(Uri, boolean, ContentObserver, int)}
*/
default void registerContentObserverForUser(
Uri uri, boolean notifyForDescendants, ContentObserver settingsObserver,
int userHandle) {
getContentResolver().registerContentObserver(
uri, notifyForDescendants, settingsObserver, getRealUserHandle(userHandle));
}
/** See {@link ContentResolver#unregisterContentObserver(ContentObserver)}. */
default void unregisterContentObserver(ContentObserver settingsObserver) {
getContentResolver().unregisterContentObserver(settingsObserver);
}
/**
* Look up a name in the database.
* @param name to look up in the table
* @return the corresponding value, or null if not present
*/
default String getString(String name) {
return getStringForUser(name, getUserId());
}
/**See {@link #getString(String)}. */
String getStringForUser(String name, int userHandle);
/**
* Store a name/value pair into the database. Values written by this method will be
* overridden if a restore happens in the future.
*
* @param name to store
* @param value to associate with the name
* @return true if the value was set, false on database errors
*/
boolean putString(String name, String value, boolean overrideableByRestore);
/**
* Store a name/value pair into the database.
* @param name to store
* @param value to associate with the name
* @return true if the value was set, false on database errors
*/
default boolean putString(String name, String value) {
return putStringForUser(name, value, getUserId());
}
/** See {@link #putString(String, String)}. */
boolean putStringForUser(String name, String value, int userHandle);
/** See {@link #putString(String, String)}. */
boolean putStringForUser(@NonNull String name, @Nullable String value, @Nullable String tag,
boolean makeDefault, @UserIdInt int userHandle, boolean overrideableByRestore);
/**
* Store a name/value pair into the database.
* <p>
* The method takes an optional tag to associate with the setting
* which can be used to clear only settings made by your package and
* associated with this tag by passing the tag to {@link
* #resetToDefaults(String)}. Anyone can override
* the current tag. Also if another package changes the setting
* then the tag will be set to the one specified in the set call
* which can be null. Also any of the settings setters that do not
* take a tag as an argument effectively clears the tag.
* </p><p>
* For example, if you set settings A and B with tags T1 and T2 and
* another app changes setting A (potentially to the same value), it
* can assign to it a tag T3 (note that now the package that changed
* the setting is not yours). Now if you reset your changes for T1 and
* T2 only setting B will be reset and A not (as it was changed by
* another package) but since A did not change you are in the desired
* initial state. Now if the other app changes the value of A (assuming
* you registered an observer in the beginning) you would detect that
* the setting was changed by another app and handle this appropriately
* (ignore, set back to some value, etc).
* </p><p>
* Also the method takes an argument whether to make the value the
* default for this setting. If the system already specified a default
* value, then the one passed in here will <strong>not</strong>
* be set as the default.
* </p>
*
* @param name to store.
* @param value to associate with the name.
* @param tag to associate with the setting.
* @param makeDefault whether to make the value the default one.
* @return true if the value was set, false on database errors.
*
* @see #resetToDefaults(String)
*
*/
boolean putString(@NonNull String name, @Nullable String value, @Nullable String tag,
boolean makeDefault);
/**
* Convenience function for retrieving a single secure settings value
* as an integer. Note that internally setting values are always
* stored as strings; this function converts the string to an integer
* for you. The default value will be returned if the setting is
* not defined or not an integer.
*
* @param name The name of the setting to retrieve.
* @param def Value to return if the setting is not defined.
*
* @return The setting's current value, or 'def' if it is not defined
* or not a valid integer.
*/
default int getInt(String name, int def) {
return getIntForUser(name, def, getUserId());
}
/** See {@link #getInt(String, int)}. */
default int getIntForUser(String name, int def, int userHandle) {
String v = getStringForUser(name, userHandle);
try {
return v != null ? Integer.parseInt(v) : def;
} catch (NumberFormatException e) {
return def;
}
}
/**
* Convenience function for retrieving a single secure settings value
* as an integer. Note that internally setting values are always
* stored as strings; this function converts the string to an integer
* for you.
* <p>
* This version does not take a default value. If the setting has not
* been set, or the string value is not a number,
* it throws {@link Settings.SettingNotFoundException}.
*
* @param name The name of the setting to retrieve.
*
* @throws Settings.SettingNotFoundException Thrown if a setting by the given
* name can't be found or the setting value is not an integer.
*
* @return The setting's current value.
*/
default int getInt(String name) throws Settings.SettingNotFoundException {
return getIntForUser(name, getUserId());
}
/** See {@link #getInt(String)}. */
default int getIntForUser(String name, int userHandle)
throws Settings.SettingNotFoundException {
String v = getStringForUser(name, userHandle);
try {
return Integer.parseInt(v);
} catch (NumberFormatException e) {
throw new Settings.SettingNotFoundException(name);
}
}
/**
* Convenience function for updating a single settings value as an
* integer. This will either create a new entry in the table if the
* given name does not exist, or modify the value of the existing row
* with that name. Note that internally setting values are always
* stored as strings, so this function converts the given value to a
* string before storing it.
*
* @param name The name of the setting to modify.
* @param value The new value for the setting.
* @return true if the value was set, false on database errors
*/
default boolean putInt(String name, int value) {
return putIntForUser(name, value, getUserId());
}
/** See {@link #putInt(String, int)}. */
default boolean putIntForUser(String name, int value, int userHandle) {
return putStringForUser(name, Integer.toString(value), userHandle);
}
/**
* Convenience function for retrieving a single secure settings value
* as a boolean. Note that internally setting values are always
* stored as strings; this function converts the string to a boolean
* for you. The default value will be returned if the setting is
* not defined or not a boolean.
*
* @param name The name of the setting to retrieve.
* @param def Value to return if the setting is not defined.
*
* @return The setting's current value, or 'def' if it is not defined
* or not a valid boolean.
*/
default boolean getBool(String name, boolean def) {
return getBoolForUser(name, def, getUserId());
}
/** See {@link #getBool(String, boolean)}. */
default boolean getBoolForUser(String name, boolean def, int userHandle) {
return getIntForUser(name, def ? 1 : 0, userHandle) != 0;
}
/**
* Convenience function for retrieving a single secure settings value
* as a boolean. Note that internally setting values are always
* stored as strings; this function converts the string to a boolean
* for you.
* <p>
* This version does not take a default value. If the setting has not
* been set, or the string value is not a number,
* it throws {@link Settings.SettingNotFoundException}.
*
* @param name The name of the setting to retrieve.
*
* @throws Settings.SettingNotFoundException Thrown if a setting by the given
* name can't be found or the setting value is not a boolean.
*
* @return The setting's current value.
*/
default boolean getBool(String name) throws Settings.SettingNotFoundException {
return getBoolForUser(name, getUserId());
}
/** See {@link #getBool(String)}. */
default boolean getBoolForUser(String name, int userHandle)
throws Settings.SettingNotFoundException {
return getIntForUser(name, userHandle) != 0;
}
/**
* Convenience function for updating a single settings value as a
* boolean. This will either create a new entry in the table if the
* given name does not exist, or modify the value of the existing row
* with that name. Note that internally setting values are always
* stored as strings, so this function converts the given value to a
* string before storing it.
*
* @param name The name of the setting to modify.
* @param value The new value for the setting.
* @return true if the value was set, false on database errors
*/
default boolean putBool(String name, boolean value) {
return putBoolForUser(name, value, getUserId());
}
/** See {@link #putBool(String, boolean)}. */
default boolean putBoolForUser(String name, boolean value, int userHandle) {
return putIntForUser(name, value ? 1 : 0, userHandle);
}
/**
* Convenience function for retrieving a single secure settings value
* as a {@code long}. Note that internally setting values are always
* stored as strings; this function converts the string to a {@code long}
* for you. The default value will be returned if the setting is
* not defined or not a {@code long}.
*
* @param name The name of the setting to retrieve.
* @param def Value to return if the setting is not defined.
*
* @return The setting's current value, or 'def' if it is not defined
* or not a valid {@code long}.
*/
default long getLong(String name, long def) {
return getLongForUser(name, def, getUserId());
}
/** See {@link #getLong(String, long)}. */
default long getLongForUser(String name, long def, int userHandle) {
String valString = getStringForUser(name, userHandle);
long value;
try {
value = valString != null ? Long.parseLong(valString) : def;
} catch (NumberFormatException e) {
value = def;
}
return value;
}
/**
* Convenience function for retrieving a single secure settings value
* as a {@code long}. Note that internally setting values are always
* stored as strings; this function converts the string to a {@code long}
* for you.
* <p>
* This version does not take a default value. If the setting has not
* been set, or the string value is not a number,
* it throws {@link Settings.SettingNotFoundException}.
*
* @param name The name of the setting to retrieve.
*
* @return The setting's current value.
* @throws Settings.SettingNotFoundException Thrown if a setting by the given
* name can't be found or the setting value is not an integer.
*/
default long getLong(String name) throws Settings.SettingNotFoundException {
return getLongForUser(name, getUserId());
}
/** See {@link #getLong(String)}. */
default long getLongForUser(String name, int userHandle)
throws Settings.SettingNotFoundException {
String valString = getStringForUser(name, userHandle);
try {
return Long.parseLong(valString);
} catch (NumberFormatException e) {
throw new Settings.SettingNotFoundException(name);
}
}
/**
* Convenience function for updating a secure settings value as a long
* integer. This will either create a new entry in the table if the
* given name does not exist, or modify the value of the existing row
* with that name. Note that internally setting values are always
* stored as strings, so this function converts the given value to a
* string before storing it.
*
* @param name The name of the setting to modify.
* @param value The new value for the setting.
* @return true if the value was set, false on database errors
*/
default boolean putLong(String name, long value) {
return putLongForUser(name, value, getUserId());
}
/** See {@link #putLong(String, long)}. */
default boolean putLongForUser(String name, long value, int userHandle) {
return putStringForUser(name, Long.toString(value), userHandle);
}
/**
* Convenience function for retrieving a single secure settings value
* as a floating point number. Note that internally setting values are
* always stored as strings; this function converts the string to an
* float for you. The default value will be returned if the setting
* is not defined or not a valid float.
*
* @param name The name of the setting to retrieve.
* @param def Value to return if the setting is not defined.
*
* @return The setting's current value, or 'def' if it is not defined
* or not a valid float.
*/
default float getFloat(String name, float def) {
return getFloatForUser(name, def, getUserId());
}
/** See {@link #getFloat(String)}. */
default float getFloatForUser(String name, float def, int userHandle) {
String v = getStringForUser(name, userHandle);
try {
return v != null ? Float.parseFloat(v) : def;
} catch (NumberFormatException e) {
return def;
}
}
/**
* Convenience function for retrieving a single secure settings value
* as a float. Note that internally setting values are always
* stored as strings; this function converts the string to a float
* for you.
* <p>
* This version does not take a default value. If the setting has not
* been set, or the string value is not a number,
* it throws {@link Settings.SettingNotFoundException}.
*
* @param name The name of the setting to retrieve.
*
* @throws Settings.SettingNotFoundException Thrown if a setting by the given
* name can't be found or the setting value is not a float.
*
* @return The setting's current value.
*/
default float getFloat(String name) throws Settings.SettingNotFoundException {
return getFloatForUser(name, getUserId());
}
/** See {@link #getFloat(String, float)}. */
default float getFloatForUser(String name, int userHandle)
throws Settings.SettingNotFoundException {
String v = getStringForUser(name, userHandle);
if (v == null) {
throw new Settings.SettingNotFoundException(name);
}
try {
return Float.parseFloat(v);
} catch (NumberFormatException e) {
throw new Settings.SettingNotFoundException(name);
}
}
/**
* Convenience function for updating a single settings value as a
* floating point number. This will either create a new entry in the
* table if the given name does not exist, or modify the value of the
* existing row with that name. Note that internally setting values
* are always stored as strings, so this function converts the given
* value to a string before storing it.
*
* @param name The name of the setting to modify.
* @param value The new value for the setting.
* @return true if the value was set, false on database errors
*/
default boolean putFloat(String name, float value) {
return putFloatForUser(name, value, getUserId());
}
/** See {@link #putFloat(String, float)} */
default boolean putFloatForUser(String name, float value, int userHandle) {
return putStringForUser(name, Float.toString(value), userHandle);
}
}
|