Remove DeviceConfigInterface from Services & refactor FakeDeviceConfigInterface to use android.provider.DeviceConfigInterface

Test: manual atest
Bug: 142727848

Change-Id: I6bf41df5b30a52887f0f9755c1332c244f19c791
This commit is contained in:
Forrest Dunlap
2021-08-23 18:10:12 -07:00
parent 738b7e6ce5
commit 5ecbf52e31
8 changed files with 66 additions and 141 deletions

View File

@@ -207,10 +207,3 @@ prebuilt_etc {
name: "protolog.conf.json.gz",
src: ":services.core.json.gz",
}
filegroup {
name: "services.core-sources-deviceconfig-interface",
srcs: [
"java/com/android/server/utils/DeviceConfigInterface.java",
],
}

View File

@@ -41,6 +41,7 @@ import android.os.Message;
import android.os.SystemClock;
import android.os.UserHandle;
import android.provider.DeviceConfig;
import android.provider.DeviceConfigInterface;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.IndentingPrintWriter;
@@ -61,7 +62,6 @@ import com.android.server.display.utils.AmbientFilterFactory;
import com.android.server.sensors.SensorManagerInternal;
import com.android.server.sensors.SensorManagerInternal.ProximityActiveListener;
import com.android.server.statusbar.StatusBarManagerInternal;
import com.android.server.utils.DeviceConfigInterface;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;

View File

@@ -1,121 +0,0 @@
/*
* Copyright (C) 2019 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.server.utils;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.provider.DeviceConfig;
import java.util.concurrent.Executor;
/**
* Abstraction around {@link DeviceConfig} to allow faking device configuration in tests.
*/
public interface DeviceConfigInterface {
/**
* @see DeviceConfig#getProperty
*/
@Nullable
String getProperty(@NonNull String namespace, @NonNull String name);
/**
* @see DeviceConfig#getString
*/
@NonNull
String getString(@NonNull String namespace, @NonNull String name, @NonNull String defaultValue);
/**
* @see DeviceConfig#getInt
*/
int getInt(@NonNull String namespace, @NonNull String name, int defaultValue);
/**
* @see DeviceConfig#getLong
*/
long getLong(@NonNull String namespace, @NonNull String name, long defaultValue);
/**
* @see DeviceConfig#getBoolean
*/
boolean getBoolean(@NonNull String namespace, @NonNull String name, boolean defaultValue);
/**
* @see DeviceConfig#getFloat
*/
float getFloat(@NonNull String namespace, @NonNull String name, float defaultValue);
/**
* @see DeviceConfig#addOnPropertiesChangedListener
*/
void addOnPropertiesChangedListener(@NonNull String namespace, @NonNull Executor executor,
@NonNull DeviceConfig.OnPropertiesChangedListener listener);
/**
* @see DeviceConfig#removeOnPropertiesChangedListener
*/
void removeOnPropertiesChangedListener(
@NonNull DeviceConfig.OnPropertiesChangedListener listener);
/**
* Calls through to the real {@link DeviceConfig}.
*/
DeviceConfigInterface REAL = new DeviceConfigInterface() {
@Override
public String getProperty(String namespace, String name) {
return DeviceConfig.getProperty(namespace, name);
}
@Override
public String getString(String namespace, String name, String defaultValue) {
return DeviceConfig.getString(namespace, name, defaultValue);
}
@Override
public int getInt(String namespace, String name, int defaultValue) {
return DeviceConfig.getInt(namespace, name, defaultValue);
}
@Override
public long getLong(String namespace, String name, long defaultValue) {
return DeviceConfig.getLong(namespace, name, defaultValue);
}
@Override
public boolean getBoolean(@NonNull String namespace, @NonNull String name,
boolean defaultValue) {
return DeviceConfig.getBoolean(namespace, name, defaultValue);
}
@Override
public float getFloat(@NonNull String namespace, @NonNull String name,
float defaultValue) {
return DeviceConfig.getFloat(namespace, name, defaultValue);
}
@Override
public void addOnPropertiesChangedListener(String namespace, Executor executor,
DeviceConfig.OnPropertiesChangedListener listener) {
DeviceConfig.addOnPropertiesChangedListener(namespace, executor, listener);
}
@Override
public void removeOnPropertiesChangedListener(
DeviceConfig.OnPropertiesChangedListener listener) {
DeviceConfig.removeOnPropertiesChangedListener(listener);
}
};
}

View File

@@ -22,12 +22,12 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.res.Resources;
import android.provider.DeviceConfig;
import android.provider.DeviceConfigInterface;
import android.util.ArraySet;
import com.android.internal.R;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.os.BackgroundThread;
import com.android.server.utils.DeviceConfigInterface;
import java.io.PrintWriter;

View File

@@ -21,9 +21,9 @@ import static android.provider.AndroidDeviceConfig.KEY_SYSTEM_GESTURE_EXCLUSION_
import android.provider.AndroidDeviceConfig;
import android.provider.DeviceConfig;
import android.provider.DeviceConfigInterface;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.utils.DeviceConfigInterface;
import java.io.PrintWriter;
import java.util.Objects;

View File

@@ -206,6 +206,7 @@ import android.os.SystemService;
import android.os.Trace;
import android.os.UserHandle;
import android.os.WorkSource;
import android.provider.DeviceConfigInterface;
import android.provider.Settings;
import android.service.vr.IVrManager;
import android.service.vr.IVrStateCallbacks;
@@ -300,7 +301,6 @@ import com.android.server.input.InputManagerService;
import com.android.server.policy.WindowManagerPolicy;
import com.android.server.policy.WindowManagerPolicy.ScreenOffListener;
import com.android.server.power.ShutdownThread;
import com.android.server.utils.DeviceConfigInterface;
import com.android.server.utils.PriorityDump;
import java.io.BufferedWriter;

View File

@@ -144,7 +144,6 @@ java_library {
"utils/**/*.java",
"utils/**/*.kt",
"utils-mockito/**/*.kt",
":services.core-sources-deviceconfig-interface",
],
static_libs: [
"junit",
@@ -161,7 +160,6 @@ java_library {
"utils/**/*.java",
"utils/**/*.kt",
"utils-mockito/**/*.kt",
":services.core-sources-deviceconfig-interface",
],
static_libs: [
"junit",

View File

@@ -18,13 +18,14 @@ package com.android.server.testutils;
import android.annotation.NonNull;
import android.provider.DeviceConfig;
import android.provider.DeviceConfigInterface;
import android.util.ArrayMap;
import android.util.Pair;
import com.android.internal.util.Preconditions;
import com.android.server.utils.DeviceConfigInterface;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
@@ -33,10 +34,17 @@ import java.util.concurrent.TimeUnit;
public class FakeDeviceConfigInterface implements DeviceConfigInterface {
private static final String COMPOSITE_DELIMITER = "/";
private Map<String, String> mProperties = new HashMap<>();
private ArrayMap<DeviceConfig.OnPropertiesChangedListener, Pair<String, Executor>> mListeners =
new ArrayMap<>();
private static String createCompositeName(@NonNull String namespace, @NonNull String name) {
Preconditions.checkNotNull(namespace);
Preconditions.checkNotNull(name);
return namespace + COMPOSITE_DELIMITER + name;
}
public void clearProperties() {
mProperties.clear();
}
@@ -90,6 +98,59 @@ public class FakeDeviceConfigInterface implements DeviceConfigInterface {
return mProperties.get(createCompositeName(namespace, name));
}
@Override
public DeviceConfig.Properties getProperties(String namespace, String... names) {
if (!mProperties.keySet().contains(namespace)) {
return new DeviceConfig.Properties(namespace, null);
}
DeviceConfig.Properties.Builder propertiesBuilder = new DeviceConfig.Properties.Builder(
namespace);
for (String compositeName : mProperties.keySet()) {
if (compositeName.split(COMPOSITE_DELIMITER).length != 2) {
continue;
}
String existingPropertyNamespace = compositeName.split(COMPOSITE_DELIMITER)[0];
String existingPropertyName = compositeName.split(COMPOSITE_DELIMITER)[1];
if ((names.length == 0 && existingPropertyNamespace.equals(namespace)) || Arrays.asList(
names).contains(compositeName)) {
propertiesBuilder.setString(existingPropertyName, mProperties.get(compositeName));
}
}
return propertiesBuilder.build();
}
@Override
public boolean setProperty(String namespace, String name, String value, boolean makeDefault) {
putPropertyAndNotify(namespace, name, value);
return true;
}
@Override
public boolean setProperties(DeviceConfig.Properties properties)
throws DeviceConfig.BadConfigException {
for (String property : properties.getKeyset()) {
String compositeName = createCompositeName(properties.getNamespace(), property);
putPropertyAndNotify(properties.getNamespace(), compositeName,
properties.getString(property, ""));
}
return true;
}
@Override
public boolean deleteProperty(String namespace, String name) {
mProperties.remove(createCompositeName(namespace, name));
return true;
}
@Override
public void resetToDefaults(int resetMode, String namespace) {
clearProperties();
}
@Override
public String getString(String namespace, String name, String defaultValue) {
String value = getProperty(namespace, name);
@@ -166,10 +227,4 @@ public class FakeDeviceConfigInterface implements DeviceConfigInterface {
DeviceConfig.OnPropertiesChangedListener listener) {
mListeners.remove(listener);
}
private static String createCompositeName(@NonNull String namespace, @NonNull String name) {
Preconditions.checkNotNull(namespace);
Preconditions.checkNotNull(name);
return namespace + "/" + name;
}
}