Merge "Expose Setting.Config APIs"

This commit is contained in:
Hassan Ali
2022-12-06 15:12:57 +00:00
committed by Android (Google) Code Review
8 changed files with 111 additions and 55 deletions

View File

@@ -425,6 +425,26 @@ package android.provider {
field public static final String NAMESPACE_DEVICE_IDLE = "device_idle";
}
public final class Settings {
field public static final int RESET_MODE_PACKAGE_DEFAULTS = 1; // 0x1
field public static final int RESET_MODE_TRUSTED_DEFAULTS = 4; // 0x4
field public static final int RESET_MODE_UNTRUSTED_CHANGES = 3; // 0x3
field public static final int RESET_MODE_UNTRUSTED_DEFAULTS = 2; // 0x2
}
public static final class Settings.Config extends android.provider.Settings.NameValueTable {
method @RequiresPermission(android.Manifest.permission.WRITE_DEVICE_CONFIG) public static boolean deleteString(@NonNull String, @NonNull String);
method @Nullable @RequiresPermission(android.Manifest.permission.READ_DEVICE_CONFIG) public static String getString(@NonNull String);
method @NonNull @RequiresPermission(android.Manifest.permission.READ_DEVICE_CONFIG) public static java.util.Map<java.lang.String,java.lang.String> getStrings(@NonNull String, @NonNull java.util.List<java.lang.String>);
method @RequiresPermission(android.Manifest.permission.WRITE_DEVICE_CONFIG) public static int getSyncDisabledMode();
method @RequiresPermission(android.Manifest.permission.WRITE_DEVICE_CONFIG) public static boolean putString(@NonNull String, @NonNull String, @Nullable String, boolean);
method public static void registerContentObserver(@Nullable String, boolean, @NonNull android.database.ContentObserver);
method @RequiresPermission(android.Manifest.permission.WRITE_DEVICE_CONFIG) public static void resetToDefaults(int, @Nullable String);
method @RequiresPermission(android.Manifest.permission.WRITE_DEVICE_CONFIG) public static boolean setStrings(@NonNull String, @NonNull java.util.Map<java.lang.String,java.lang.String>) throws android.provider.DeviceConfig.BadConfigException;
method @RequiresPermission(android.Manifest.permission.WRITE_DEVICE_CONFIG) public static void setSyncDisabledMode(int);
method public static void unregisterContentObserver(@NonNull android.database.ContentObserver);
}
public static final class Settings.Global extends android.provider.Settings.NameValueTable {
field public static final String BLE_SCAN_ALWAYS_AVAILABLE = "ble_scan_always_enabled";
field public static final String BLE_SCAN_BACKGROUND_MODE = "ble_scan_background_mode";

View File

@@ -54,13 +54,6 @@ import java.util.concurrent.Executor;
*/
@SystemApi
public final class DeviceConfig {
/**
* The content:// style URL for the config table.
*
* @hide
*/
public static final Uri CONTENT_URI = Uri.parse("content://" + Settings.AUTHORITY + "/config");
/**
* Namespace for activity manager related features. These features will be applied
* immediately upon change.
@@ -1194,11 +1187,6 @@ public final class DeviceConfig {
}
}
private static Uri createNamespaceUri(@NonNull String namespace) {
Preconditions.checkNotNull(namespace);
return CONTENT_URI.buildUpon().appendPath(namespace).build();
}
/**
* Increment the count used to represent the number of listeners subscribed to the given
* namespace. If this is the first (i.e. incrementing from 0 to 1) for the given namespace, a
@@ -1223,7 +1211,7 @@ public final class DeviceConfig {
}
};
Settings.Config
.registerContentObserver(createNamespaceUri(namespace), true, contentObserver);
.registerContentObserver(namespace, true, contentObserver);
sNamespaces.put(namespace, new Pair<>(contentObserver, 1));
}
}

View File

@@ -2819,6 +2819,7 @@ public final class Settings {
*/
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
@TestApi
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
public static final int RESET_MODE_PACKAGE_DEFAULTS = 1;
/**
@@ -2828,6 +2829,7 @@ public final class Settings {
* the setting will be deleted. This mode is only available to the system.
* @hide
*/
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
public static final int RESET_MODE_UNTRUSTED_DEFAULTS = 2;
/**
@@ -2838,6 +2840,7 @@ public final class Settings {
* This mode is only available to the system.
* @hide
*/
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
public static final int RESET_MODE_UNTRUSTED_CHANGES = 3;
/**
@@ -2849,6 +2852,7 @@ public final class Settings {
* to the system.
* @hide
*/
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
public static final int RESET_MODE_TRUSTED_DEFAULTS = 4;
/** @hide */
@@ -18001,6 +18005,7 @@ public final class Settings {
*
* @hide
*/
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
public static final class Config extends NameValueTable {
/**
@@ -18035,12 +18040,19 @@ public final class Settings {
*/
public static final int SYNC_DISABLED_MODE_UNTIL_REBOOT = 2;
/**
* The content:// style URL for the config table.
*
* @hide
*/
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/config");
private static final ContentProviderHolder sProviderHolder =
new ContentProviderHolder(DeviceConfig.CONTENT_URI);
new ContentProviderHolder(CONTENT_URI);
// Populated lazily, guarded by class object:
private static final NameValueCache sNameValueCache = new NameValueCache(
DeviceConfig.CONTENT_URI,
CONTENT_URI,
CALL_METHOD_GET_CONFIG,
CALL_METHOD_PUT_CONFIG,
CALL_METHOD_DELETE_CONFIG,
@@ -18049,6 +18061,10 @@ public final class Settings {
sProviderHolder,
Config.class);
// Should never be invoked
private Config() {
}
/**
* Look up a name in the database.
* @param name to look up in the table
@@ -18056,8 +18072,10 @@ public final class Settings {
*
* @hide
*/
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
@Nullable
@RequiresPermission(Manifest.permission.READ_DEVICE_CONFIG)
static String getString(String name) {
public static String getString(@NonNull String name) {
ContentResolver resolver = getContentResolver();
return sNameValueCache.getStringForUser(resolver, name, resolver.getUserId());
}
@@ -18072,6 +18090,8 @@ public final class Settings {
*
* @hide
*/
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
@NonNull
@RequiresPermission(Manifest.permission.READ_DEVICE_CONFIG)
public static Map<String, String> getStrings(@NonNull String namespace,
@NonNull List<String> names) {
@@ -18128,6 +18148,7 @@ public final class Settings {
*
* @hide
*/
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
@RequiresPermission(Manifest.permission.WRITE_DEVICE_CONFIG)
public static boolean putString(@NonNull String namespace,
@NonNull String name, @Nullable String value, boolean makeDefault) {
@@ -18147,6 +18168,7 @@ public final class Settings {
*
* @hide
*/
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
@RequiresPermission(Manifest.permission.WRITE_DEVICE_CONFIG)
public static boolean setStrings(@NonNull String namespace,
@NonNull Map<String, String> keyValues)
@@ -18197,8 +18219,9 @@ public final class Settings {
*
* @hide
*/
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
@RequiresPermission(Manifest.permission.WRITE_DEVICE_CONFIG)
static boolean deleteString(@NonNull String namespace,
public static boolean deleteString(@NonNull String namespace,
@NonNull String name) {
ContentResolver resolver = getContentResolver();
return sNameValueCache.deleteStringForUser(resolver,
@@ -18218,8 +18241,9 @@ public final class Settings {
*
* @hide
*/
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
@RequiresPermission(Manifest.permission.WRITE_DEVICE_CONFIG)
static void resetToDefaults(@ResetMode int resetMode,
public static void resetToDefaults(@ResetMode int resetMode,
@Nullable String namespace) {
try {
ContentResolver resolver = getContentResolver();
@@ -18233,7 +18257,7 @@ public final class Settings {
cp.call(resolver.getAttributionSource(),
sProviderHolder.mUri.getAuthority(), CALL_METHOD_RESET_CONFIG, null, arg);
} catch (RemoteException e) {
Log.w(TAG, "Can't reset to defaults for " + DeviceConfig.CONTENT_URI, e);
Log.w(TAG, "Can't reset to defaults for " + CONTENT_URI, e);
}
}
@@ -18243,9 +18267,10 @@ public final class Settings {
*
* @hide
*/
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
@SuppressLint("AndroidFrameworkRequiresPermission")
@RequiresPermission(Manifest.permission.WRITE_DEVICE_CONFIG)
static void setSyncDisabledMode(@SyncDisabledMode int disableSyncMode) {
public static void setSyncDisabledMode(@SyncDisabledMode int disableSyncMode) {
try {
ContentResolver resolver = getContentResolver();
Bundle args = new Bundle();
@@ -18254,7 +18279,7 @@ public final class Settings {
cp.call(resolver.getAttributionSource(), sProviderHolder.mUri.getAuthority(),
CALL_METHOD_SET_SYNC_DISABLED_MODE_CONFIG, null, args);
} catch (RemoteException e) {
Log.w(TAG, "Can't set sync disabled mode " + DeviceConfig.CONTENT_URI, e);
Log.w(TAG, "Can't set sync disabled mode " + CONTENT_URI, e);
}
}
@@ -18264,9 +18289,10 @@ public final class Settings {
*
* @hide
*/
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
@SuppressLint("AndroidFrameworkRequiresPermission")
@RequiresPermission(Manifest.permission.WRITE_DEVICE_CONFIG)
static int getSyncDisabledMode() {
public static int getSyncDisabledMode() {
try {
ContentResolver resolver = getContentResolver();
Bundle args = Bundle.EMPTY;
@@ -18277,7 +18303,7 @@ public final class Settings {
null, args);
return bundle.getInt(KEY_CONFIG_GET_SYNC_DISABLED_MODE_RETURN);
} catch (RemoteException e) {
Log.w(TAG, "Can't query sync disabled mode " + DeviceConfig.CONTENT_URI, e);
Log.w(TAG, "Can't query sync disabled mode " + CONTENT_URI, e);
}
return -1;
}
@@ -18297,21 +18323,26 @@ public final class Settings {
/**
* Register a content observer
* Register a content observer.
*
* @hide
*/
public static void registerContentObserver(@NonNull Uri uri, boolean notifyForDescendants,
@NonNull ContentObserver observer) {
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
public static void registerContentObserver(@Nullable String namespace,
boolean notifyForDescendants, @NonNull ContentObserver observer) {
ActivityThread.currentApplication().getContentResolver()
.registerContentObserver(uri, notifyForDescendants, observer);
.registerContentObserver(createNamespaceUri(namespace),
notifyForDescendants, observer);
}
/**
* Unregister a content observer
* Unregister a content observer.
* this may only be used with content observers registered through
* {@link Config#registerContentObserver}
*
* @hide
*/
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
public static void unregisterContentObserver(@NonNull ContentObserver observer) {
ActivityThread.currentApplication().getContentResolver()
.unregisterContentObserver(observer);
@@ -18372,6 +18403,11 @@ public final class Settings {
return namespace + "/";
}
private static Uri createNamespaceUri(@NonNull String namespace) {
Preconditions.checkNotNull(namespace);
return CONTENT_URI.buildUpon().appendPath(namespace).build();
}
private static ContentResolver getContentResolver() {
return ActivityThread.currentApplication().getContentResolver();
}

View File

@@ -857,7 +857,10 @@ public class DeviceConfigTest {
ContentResolver resolver = InstrumentationRegistry.getContext().getContentResolver();
String compositeName = namespace + "/" + key;
Bundle result = resolver.call(
DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_DELETE_CONFIG, compositeName, null);
Settings.Config.CONTENT_URI,
Settings.CALL_METHOD_DELETE_CONFIG,
compositeName,
null);
assertThat(result).isNotNull();
return compositeName.equals(result.getString(Settings.NameValueTable.VALUE));
}

View File

@@ -76,7 +76,7 @@ public class NameValueCacheTest {
when(mMockContentProvider.getIContentProvider()).thenReturn(mMockIContentProvider);
mMockContentResolver = new MockContentResolver(InstrumentationRegistry
.getInstrumentation().getContext());
mMockContentResolver.addProvider(DeviceConfig.CONTENT_URI.getAuthority(),
mMockContentResolver.addProvider(Settings.Config.CONTENT_URI.getAuthority(),
mMockContentProvider);
mCacheGenerationStore = new MemoryIntArray(1);
mStorage = new HashMap<>();
@@ -84,7 +84,7 @@ public class NameValueCacheTest {
// Stores keyValues for a given prefix and increments the generation. (Note that this
// increments the generation no matter what, it doesn't pay attention to if anything
// actually changed).
when(mMockIContentProvider.call(any(), eq(DeviceConfig.CONTENT_URI.getAuthority()),
when(mMockIContentProvider.call(any(), eq(Settings.Config.CONTENT_URI.getAuthority()),
eq(Settings.CALL_METHOD_SET_ALL_CONFIG),
any(), any(Bundle.class))).thenAnswer(invocationOnMock -> {
Bundle incomingBundle = invocationOnMock.getArgument(4);
@@ -104,7 +104,7 @@ public class NameValueCacheTest {
// Returns the keyValues corresponding to a namespace, or an empty map if the namespace
// doesn't have anything stored for it. Returns the generation key if the caller asked
// for one.
when(mMockIContentProvider.call(any(), eq(DeviceConfig.CONTENT_URI.getAuthority()),
when(mMockIContentProvider.call(any(), eq(Settings.Config.CONTENT_URI.getAuthority()),
eq(Settings.CALL_METHOD_LIST_CONFIG),
any(), any(Bundle.class))).thenAnswer(invocationOnMock -> {
Bundle incomingBundle = invocationOnMock.getArgument(4);

View File

@@ -345,27 +345,33 @@ public class SettingsProviderTest extends AndroidTestCase {
try {
// value is empty
Bundle results =
r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_GET_CONFIG, name, null);
r.call(Settings.Config.CONTENT_URI,
Settings.CALL_METHOD_GET_CONFIG, name, null);
assertNull(results.get(Settings.NameValueTable.VALUE));
// save value
results = r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_PUT_CONFIG, name, args);
results = r.call(Settings.Config.CONTENT_URI,
Settings.CALL_METHOD_PUT_CONFIG, name, args);
assertNull(results);
// value is no longer empty
results = r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_GET_CONFIG, name, null);
results = r.call(Settings.Config.CONTENT_URI,
Settings.CALL_METHOD_GET_CONFIG, name, null);
assertEquals(value, results.get(Settings.NameValueTable.VALUE));
// save new value
args.putString(Settings.NameValueTable.VALUE, newValue);
r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_PUT_CONFIG, name, args);
r.call(Settings.Config.CONTENT_URI,
Settings.CALL_METHOD_PUT_CONFIG, name, args);
// new value is returned
results = r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_GET_CONFIG, name, null);
results = r.call(Settings.Config.CONTENT_URI,
Settings.CALL_METHOD_GET_CONFIG, name, null);
assertEquals(newValue, results.get(Settings.NameValueTable.VALUE));
} finally {
// clean up
r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_DELETE_CONFIG, name, null);
r.call(Settings.Config.CONTENT_URI,
Settings.CALL_METHOD_DELETE_CONFIG, name, null);
}
}
@@ -379,23 +385,25 @@ public class SettingsProviderTest extends AndroidTestCase {
try {
// save value
r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_PUT_CONFIG, name, args);
r.call(Settings.Config.CONTENT_URI, Settings.CALL_METHOD_PUT_CONFIG, name, args);
// get value
Bundle results =
r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_GET_CONFIG, name, null);
r.call(Settings.Config.CONTENT_URI,
Settings.CALL_METHOD_GET_CONFIG, name, null);
assertEquals(value, results.get(Settings.NameValueTable.VALUE));
// delete value
results = r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_DELETE_CONFIG, name,
results = r.call(Settings.Config.CONTENT_URI, Settings.CALL_METHOD_DELETE_CONFIG, name,
null);
// value is empty now
results = r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_GET_CONFIG, name, null);
results = r.call(Settings.Config.CONTENT_URI,
Settings.CALL_METHOD_GET_CONFIG, name, null);
assertNull(results.get(Settings.NameValueTable.VALUE));
} finally {
// clean up
r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_DELETE_CONFIG, name, null);
r.call(Settings.Config.CONTENT_URI, Settings.CALL_METHOD_DELETE_CONFIG, name, null);
}
}
@@ -413,12 +421,12 @@ public class SettingsProviderTest extends AndroidTestCase {
try {
// save both values
r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_PUT_CONFIG, name, args);
r.call(Settings.Config.CONTENT_URI, Settings.CALL_METHOD_PUT_CONFIG, name, args);
args.putString(Settings.NameValueTable.VALUE, newValue);
r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_PUT_CONFIG, newName, args);
r.call(Settings.Config.CONTENT_URI, Settings.CALL_METHOD_PUT_CONFIG, newName, args);
// list all values
Bundle result = r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_LIST_CONFIG,
Bundle result = r.call(Settings.Config.CONTENT_URI, Settings.CALL_METHOD_LIST_CONFIG,
null, null);
Map<String, String> keyValueMap =
(HashMap) result.getSerializable(Settings.NameValueTable.VALUE);
@@ -428,14 +436,15 @@ public class SettingsProviderTest extends AndroidTestCase {
// list values for prefix
args.putString(Settings.CALL_METHOD_PREFIX_KEY, prefix);
result = r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_LIST_CONFIG, null, args);
result = r.call(Settings.Config.CONTENT_URI,
Settings.CALL_METHOD_LIST_CONFIG, null, args);
keyValueMap = (HashMap) result.getSerializable(Settings.NameValueTable.VALUE);
assertThat(keyValueMap, aMapWithSize(1));
assertEquals(value, keyValueMap.get(name));
} finally {
// clean up
r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_DELETE_CONFIG, name, null);
r.call(DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_DELETE_CONFIG, newName, null);
r.call(Settings.Config.CONTENT_URI, Settings.CALL_METHOD_DELETE_CONFIG, name, null);
r.call(Settings.Config.CONTENT_URI, Settings.CALL_METHOD_DELETE_CONFIG, newName, null);
}
}
}

View File

@@ -3595,8 +3595,8 @@ public class SettingsProvider extends ContentProvider {
private Uri getNotificationUriFor(int key, String name) {
if (isConfigSettingsKey(key)) {
return (name != null) ? Uri.withAppendedPath(DeviceConfig.CONTENT_URI, name)
: DeviceConfig.CONTENT_URI;
return (name != null) ? Uri.withAppendedPath(Settings.Config.CONTENT_URI, name)
: Settings.Config.CONTENT_URI;
} else if (isGlobalSettingsKey(key)) {
return (name != null) ? Uri.withAppendedPath(Settings.Global.CONTENT_URI, name)
: Settings.Global.CONTENT_URI;

View File

@@ -22,7 +22,6 @@ import static junit.framework.Assert.assertNull;
import android.content.ContentResolver;
import android.os.Bundle;
import android.provider.DeviceConfig;
import android.provider.Settings;
import androidx.test.InstrumentationRegistry;
@@ -180,14 +179,14 @@ public class DeviceConfigServiceTest {
args.putBoolean(Settings.CALL_METHOD_MAKE_DEFAULT_KEY, true);
}
resolver.call(
DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_PUT_CONFIG, compositeName, args);
Settings.Config.CONTENT_URI, Settings.CALL_METHOD_PUT_CONFIG, compositeName, args);
}
private static String getFromContentProvider(ContentResolver resolver, String namespace,
String key) {
String compositeName = namespace + "/" + key;
Bundle result = resolver.call(
DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_GET_CONFIG, compositeName, null);
Settings.Config.CONTENT_URI, Settings.CALL_METHOD_GET_CONFIG, compositeName, null);
assertNotNull(result);
return result.getString(Settings.NameValueTable.VALUE);
}
@@ -196,7 +195,8 @@ public class DeviceConfigServiceTest {
String key) {
String compositeName = namespace + "/" + key;
Bundle result = resolver.call(
DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_DELETE_CONFIG, compositeName, null);
Settings.Config.CONTENT_URI,
Settings.CALL_METHOD_DELETE_CONFIG, compositeName, null);
assertNotNull(result);
return compositeName.equals(result.getString(Settings.NameValueTable.VALUE));
}