From 4dc91d8e9879ef8671d44975ca012d1002f8b272 Mon Sep 17 00:00:00 2001 From: Hassan Ali Date: Tue, 8 Nov 2022 17:24:42 +0000 Subject: [PATCH] Remove ActivityThread from deviceconfig.java As part of moving DeviceConfig.java to packages/modules/ConfigInfrastructure. Need to move activity thread dependency to setting.config as the new module will not have access to hidden apis (ActivityThread). Test: m Bug: 258220607 Change-Id: Ib7b20caaec128d64908349918ec7bce9a53cc2e6 --- core/java/android/provider/DeviceConfig.java | 36 ++---- core/java/android/provider/Settings.java | 109 +++++++++++++++--- .../internal/jank/InteractionJankMonitor.java | 2 - .../providers/settings/SettingsProvider.java | 8 +- .../systemui/util/DeviceConfigProxy.java | 5 +- .../systemui/util/DeviceConfigProxyFake.java | 3 +- 6 files changed, 115 insertions(+), 48 deletions(-) diff --git a/core/java/android/provider/DeviceConfig.java b/core/java/android/provider/DeviceConfig.java index 7095d1b20d840..b3a64c1fddd3a 100644 --- a/core/java/android/provider/DeviceConfig.java +++ b/core/java/android/provider/DeviceConfig.java @@ -25,9 +25,6 @@ import android.annotation.Nullable; import android.annotation.RequiresPermission; import android.annotation.SystemApi; import android.annotation.TestApi; -import android.app.ActivityThread; -import android.content.ContentResolver; -import android.content.Context; import android.content.pm.PackageManager; import android.database.ContentObserver; import android.net.Uri; @@ -875,9 +872,8 @@ public final class DeviceConfig { @NonNull @RequiresPermission(READ_DEVICE_CONFIG) public static Properties getProperties(@NonNull String namespace, @NonNull String ... names) { - ContentResolver contentResolver = ActivityThread.currentApplication().getContentResolver(); return new Properties(namespace, - Settings.Config.getStrings(contentResolver, namespace, Arrays.asList(names))); + Settings.Config.getStrings(namespace, Arrays.asList(names))); } /** @@ -1016,8 +1012,7 @@ public final class DeviceConfig { @RequiresPermission(WRITE_DEVICE_CONFIG) public static boolean setProperty(@NonNull String namespace, @NonNull String name, @Nullable String value, boolean makeDefault) { - ContentResolver contentResolver = ActivityThread.currentApplication().getContentResolver(); - return Settings.Config.putString(contentResolver, namespace, name, value, makeDefault); + return Settings.Config.putString(namespace, name, value, makeDefault); } /** @@ -1038,8 +1033,7 @@ public final class DeviceConfig { @SystemApi @RequiresPermission(WRITE_DEVICE_CONFIG) public static boolean setProperties(@NonNull Properties properties) throws BadConfigException { - ContentResolver contentResolver = ActivityThread.currentApplication().getContentResolver(); - return Settings.Config.setStrings(contentResolver, properties.getNamespace(), + return Settings.Config.setStrings(properties.getNamespace(), properties.mMap); } @@ -1055,8 +1049,7 @@ public final class DeviceConfig { @SystemApi @RequiresPermission(WRITE_DEVICE_CONFIG) public static boolean deleteProperty(@NonNull String namespace, @NonNull String name) { - ContentResolver contentResolver = ActivityThread.currentApplication().getContentResolver(); - return Settings.Config.deleteString(contentResolver, namespace, name); + return Settings.Config.deleteString(namespace, name); } /** @@ -1087,8 +1080,7 @@ public final class DeviceConfig { @SystemApi @RequiresPermission(WRITE_DEVICE_CONFIG) public static void resetToDefaults(@ResetMode int resetMode, @Nullable String namespace) { - ContentResolver contentResolver = ActivityThread.currentApplication().getContentResolver(); - Settings.Config.resetToDefaults(contentResolver, resetMode, namespace); + Settings.Config.resetToDefaults(resetMode, namespace); } /** @@ -1105,8 +1097,7 @@ public final class DeviceConfig { */ @RequiresPermission(WRITE_DEVICE_CONFIG) public static void setSyncDisabledMode(@SyncDisabledMode int syncDisabledMode) { - ContentResolver contentResolver = ActivityThread.currentApplication().getContentResolver(); - Settings.Config.setSyncDisabledMode(contentResolver, syncDisabledMode); + Settings.Config.setSyncDisabledMode(syncDisabledMode); } /** @@ -1117,8 +1108,7 @@ public final class DeviceConfig { */ @RequiresPermission(WRITE_DEVICE_CONFIG) public static @SyncDisabledMode int getSyncDisabledMode() { - ContentResolver contentResolver = ActivityThread.currentApplication().getContentResolver(); - return Settings.Config.getSyncDisabledMode(contentResolver); + return Settings.Config.getSyncDisabledMode(); } /** @@ -1141,8 +1131,7 @@ public final class DeviceConfig { @NonNull String namespace, @NonNull @CallbackExecutor Executor executor, @NonNull OnPropertiesChangedListener onPropertiesChangedListener) { - enforceReadPermission(ActivityThread.currentApplication().getApplicationContext(), - namespace); + enforceReadPermission(namespace); synchronized (sLock) { Pair oldNamespace = sListeners.get(onPropertiesChangedListener); if (oldNamespace == null) { @@ -1209,7 +1198,7 @@ public final class DeviceConfig { } } }; - ActivityThread.currentApplication().getContentResolver() + Settings.Config .registerContentObserver(createNamespaceUri(namespace), true, contentObserver); sNamespaces.put(namespace, new Pair<>(contentObserver, 1)); } @@ -1233,8 +1222,7 @@ public final class DeviceConfig { sNamespaces.put(namespace, new Pair<>(namespaceCount.first, namespaceCount.second - 1)); } else { // Decrementing a namespace to zero means we no longer need its ContentObserver. - ActivityThread.currentApplication().getContentResolver() - .unregisterContentObserver(namespaceCount.first); + Settings.Config.unregisterContentObserver(namespaceCount.first); sNamespaces.remove(namespace); } } @@ -1274,8 +1262,8 @@ public final class DeviceConfig { * Enforces READ_DEVICE_CONFIG permission if namespace is not one of public namespaces. * @hide */ - public static void enforceReadPermission(Context context, String namespace) { - if (context.checkCallingOrSelfPermission(READ_DEVICE_CONFIG) + public static void enforceReadPermission(String namespace) { + if (Settings.Config.checkCallingOrSelfPermission(READ_DEVICE_CONFIG) != PackageManager.PERMISSION_GRANTED) { if (!PUBLIC_NAMESPACES.contains(namespace)) { throw new SecurityException("Permission denial: reading from settings requires:" diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index fab6f7b977906..be5aab60ad65a 100644 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -47,9 +47,11 @@ import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; +import android.content.pm.PermissionName; import android.content.pm.ResolveInfo; import android.content.res.Configuration; import android.content.res.Resources; +import android.database.ContentObserver; import android.database.Cursor; import android.database.SQLException; import android.location.ILocationManager; @@ -3313,7 +3315,7 @@ public final class Settings { public ArrayMap getStringsForPrefix(ContentResolver cr, String prefix, List names) { String namespace = prefix.substring(0, prefix.length() - 1); - DeviceConfig.enforceReadPermission(ActivityThread.currentApplication(), namespace); + DeviceConfig.enforceReadPermission(namespace); ArrayMap keyValues = new ArrayMap<>(); int currentGeneration = -1; @@ -17971,17 +17973,33 @@ public final class Settings { /** * Look up a name in the database. - * @param resolver to access the database with * @param name to look up in the table * @return the corresponding value, or null if not present * * @hide */ @RequiresPermission(Manifest.permission.READ_DEVICE_CONFIG) - static String getString(ContentResolver resolver, String name) { + static String getString(String name) { + ContentResolver resolver = getContentResolver(); return sNameValueCache.getStringForUser(resolver, name, resolver.getUserId()); } + /** + * Look up a list of names in the database, within the specified namespace. + * + * @param namespace to which the names belong + * @param names to look up in the table + * @return a non null, but possibly empty, map from name to value for any of the names that + * were found during lookup. + * + * @hide + */ + @RequiresPermission(Manifest.permission.READ_DEVICE_CONFIG) + public static Map getStrings(@NonNull String namespace, + @NonNull List names) { + return getStrings(getContentResolver(), namespace, names); + } + /** * Look up a list of names in the database, within the specified namespace. * @@ -18022,7 +18040,6 @@ public final class Settings { * not be set as the default. *

* - * @param resolver to access the database with. * @param namespace to store the name/value pair in. * @param name to store. * @param value to associate with the name. @@ -18034,13 +18051,31 @@ public final class Settings { * @hide */ @RequiresPermission(Manifest.permission.WRITE_DEVICE_CONFIG) - static boolean putString(@NonNull ContentResolver resolver, @NonNull String namespace, + public static boolean putString(@NonNull String namespace, @NonNull String name, @Nullable String value, boolean makeDefault) { + ContentResolver resolver = getContentResolver(); return sNameValueCache.putStringForUser(resolver, createCompositeName(namespace, name), value, null, makeDefault, resolver.getUserId(), DEFAULT_OVERRIDEABLE_BY_RESTORE); } + /** + * Clear all name/value pairs for the provided namespace and save new name/value pairs in + * their place. + * + * @param namespace to which the names should be set. + * @param keyValues map of key names (without the prefix) to values. + * @return true if the name/value pairs were set, false if setting was blocked + * + * @hide + */ + @RequiresPermission(Manifest.permission.WRITE_DEVICE_CONFIG) + public static boolean setStrings(@NonNull String namespace, + @NonNull Map keyValues) + throws DeviceConfig.BadConfigException { + return setStrings(getContentResolver(), namespace, keyValues); + } + /** * Clear all name/value pairs for the provided namespace and save new name/value pairs in * their place. @@ -18075,7 +18110,6 @@ public final class Settings { /** * Delete a name/value pair from the database for the specified namespace. * - * @param resolver to access the database with. * @param namespace to delete the name/value pair from. * @param name to delete. * @return true if the value was deleted, false on database errors. If the name/value pair @@ -18086,8 +18120,9 @@ public final class Settings { * @hide */ @RequiresPermission(Manifest.permission.WRITE_DEVICE_CONFIG) - static boolean deleteString(@NonNull ContentResolver resolver, @NonNull String namespace, + static boolean deleteString(@NonNull String namespace, @NonNull String name) { + ContentResolver resolver = getContentResolver(); return sNameValueCache.deleteStringForUser(resolver, createCompositeName(namespace, name), resolver.getUserId()); } @@ -18098,7 +18133,6 @@ public final class Settings { * The method accepts an optional prefix parameter. If provided, only pairs with a name that * starts with the exact prefix will be reset. Otherwise all will be reset. * - * @param resolver Handle to the content resolver. * @param resetMode The reset mode to use. * @param namespace Optionally, to limit which which namespace is reset. * @@ -18107,9 +18141,10 @@ public final class Settings { * @hide */ @RequiresPermission(Manifest.permission.WRITE_DEVICE_CONFIG) - static void resetToDefaults(@NonNull ContentResolver resolver, @ResetMode int resetMode, + static void resetToDefaults(@ResetMode int resetMode, @Nullable String namespace) { try { + ContentResolver resolver = getContentResolver(); Bundle arg = new Bundle(); arg.putInt(CALL_METHOD_USER_KEY, resolver.getUserId()); arg.putInt(CALL_METHOD_RESET_MODE_KEY, resetMode); @@ -18132,9 +18167,9 @@ public final class Settings { */ @SuppressLint("AndroidFrameworkRequiresPermission") @RequiresPermission(Manifest.permission.WRITE_DEVICE_CONFIG) - static void setSyncDisabledMode( - @NonNull ContentResolver resolver, @SyncDisabledMode int disableSyncMode) { + static void setSyncDisabledMode(@SyncDisabledMode int disableSyncMode) { try { + ContentResolver resolver = getContentResolver(); Bundle args = new Bundle(); args.putInt(CALL_METHOD_SYNC_DISABLED_MODE_KEY, disableSyncMode); IContentProvider cp = sProviderHolder.getProvider(resolver); @@ -18153,8 +18188,9 @@ public final class Settings { */ @SuppressLint("AndroidFrameworkRequiresPermission") @RequiresPermission(Manifest.permission.WRITE_DEVICE_CONFIG) - static int getSyncDisabledMode(@NonNull ContentResolver resolver) { + static int getSyncDisabledMode() { try { + ContentResolver resolver = getContentResolver(); Bundle args = Bundle.EMPTY; IContentProvider cp = sProviderHolder.getProvider(resolver); Bundle bundle = cp.call(resolver.getAttributionSource(), @@ -18171,7 +18207,6 @@ public final class Settings { /** * Register callback for monitoring Config table. * - * @param resolver Handle to the content resolver. * @param callback callback to register * * @hide @@ -18182,6 +18217,50 @@ public final class Settings { registerMonitorCallbackAsUser(resolver, resolver.getUserId(), callback); } + + /** + * Register a content observer + * + * @hide + */ + public static void registerContentObserver(@NonNull Uri uri, boolean notifyForDescendants, + @NonNull ContentObserver observer) { + ActivityThread.currentApplication().getContentResolver() + .registerContentObserver(uri, notifyForDescendants, observer); + } + + /** + * Unregister a content observer + * + * @hide + */ + public static void unregisterContentObserver(@NonNull ContentObserver observer) { + ActivityThread.currentApplication().getContentResolver() + .unregisterContentObserver(observer); + } + + /** + * Determine whether the calling process of an IPC or you have been + * granted a particular permission. This is the same as + * {@link #checkCallingPermission}, except it grants your own permissions + * if you are not currently processing an IPC. Use with care! + * + * @param permission The name of the permission being checked. + * + * @return {@link PackageManager#PERMISSION_GRANTED} if the calling + * pid/uid is allowed that permission, or + * {@link PackageManager#PERMISSION_DENIED} if it is not. + * + * @see PackageManager#checkPermission(String, String) + * @see #checkPermission + * @see #checkCallingPermission + * @hide + */ + public static int checkCallingOrSelfPermission(@NonNull @PermissionName String permission) { + return ActivityThread.currentApplication() + .getApplicationContext().checkCallingOrSelfPermission(permission); + } + private static void registerMonitorCallbackAsUser( @NonNull ContentResolver resolver, @UserIdInt int userHandle, @NonNull RemoteCallback callback) { @@ -18214,6 +18293,10 @@ public final class Settings { Preconditions.checkNotNull(namespace); return namespace + "/"; } + + private static ContentResolver getContentResolver() { + return ActivityThread.currentApplication().getContentResolver(); + } } /** diff --git a/core/java/com/android/internal/jank/InteractionJankMonitor.java b/core/java/com/android/internal/jank/InteractionJankMonitor.java index b0d59226d4ecb..6c8e27a2d3cbd 100644 --- a/core/java/com/android/internal/jank/InteractionJankMonitor.java +++ b/core/java/com/android/internal/jank/InteractionJankMonitor.java @@ -90,7 +90,6 @@ import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.UiThread; import android.annotation.WorkerThread; -import android.app.ActivityThread; import android.content.Context; import android.os.Build; import android.os.Handler; @@ -413,7 +412,6 @@ public class InteractionJankMonitor { public InteractionJankMonitor(@NonNull HandlerThread worker) { // Check permission early. DeviceConfig.enforceReadPermission( - ActivityThread.currentApplication().getApplicationContext(), DeviceConfig.NAMESPACE_INTERACTION_JANK_MONITOR); mRunningTrackers = new SparseArray<>(); diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java index fa96a2f0ee7f9..0b7b2f935e915 100644 --- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java +++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java @@ -112,10 +112,10 @@ import com.android.internal.os.BackgroundThread; import com.android.internal.util.FrameworkStatsLog; import com.android.providers.settings.SettingsState.Setting; -import libcore.util.HexEncoding; - import com.google.android.collect.Sets; +import libcore.util.HexEncoding; + import java.io.File; import java.io.FileDescriptor; import java.io.FileNotFoundException; @@ -1144,7 +1144,7 @@ public class SettingsProvider extends ContentProvider { Slog.v(LOG_TAG, "getConfigSetting(" + name + ")"); } - DeviceConfig.enforceReadPermission(getContext(), /*namespace=*/name.split("/")[0]); + DeviceConfig.enforceReadPermission(/*namespace=*/name.split("/")[0]); // Get the value. synchronized (mLock) { @@ -1317,7 +1317,7 @@ public class SettingsProvider extends ContentProvider { Slog.v(LOG_TAG, "getAllConfigFlags() for " + prefix); } - DeviceConfig.enforceReadPermission(getContext(), + DeviceConfig.enforceReadPermission( prefix != null ? prefix.split("/")[0] : null); synchronized (mLock) { diff --git a/packages/SystemUI/src/com/android/systemui/util/DeviceConfigProxy.java b/packages/SystemUI/src/com/android/systemui/util/DeviceConfigProxy.java index 6b5556b3ea913..0f3eddf2eb7c1 100644 --- a/packages/SystemUI/src/com/android/systemui/util/DeviceConfigProxy.java +++ b/packages/SystemUI/src/com/android/systemui/util/DeviceConfigProxy.java @@ -19,7 +19,6 @@ package com.android.systemui.util; import android.annotation.CallbackExecutor; import android.annotation.NonNull; import android.annotation.Nullable; -import android.content.Context; import android.provider.DeviceConfig; import android.provider.Settings; @@ -53,8 +52,8 @@ public class DeviceConfigProxy { /** * Wrapped version of {@link DeviceConfig#enforceReadPermission}. */ - public void enforceReadPermission(Context context, String namespace) { - DeviceConfig.enforceReadPermission(context, namespace); + public void enforceReadPermission(String namespace) { + DeviceConfig.enforceReadPermission(namespace); } /** diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/util/DeviceConfigProxyFake.java b/packages/SystemUI/tests/utils/src/com/android/systemui/util/DeviceConfigProxyFake.java index 33ece00849063..21e16a1e7be48 100644 --- a/packages/SystemUI/tests/utils/src/com/android/systemui/util/DeviceConfigProxyFake.java +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/util/DeviceConfigProxyFake.java @@ -16,7 +16,6 @@ package com.android.systemui.util; -import android.content.Context; import android.provider.DeviceConfig; import android.provider.DeviceConfig.OnPropertiesChangedListener; import android.provider.DeviceConfig.Properties; @@ -83,7 +82,7 @@ public class DeviceConfigProxyFake extends DeviceConfigProxy { } @Override - public void enforceReadPermission(Context context, String namespace) { + public void enforceReadPermission(String namespace) { // no-op }