Migrate to DeviceConfig in TextClassifier
ConfigParser is introduced to read the flags from DeviceConfig.
If the flag is missing, fallback to Settings.
Also, adds a new setting key: TEXT_CLASSIFIER_ACTION_MODEL_PARAMS
Test: atest frameworks/base/core/tests/coretests/src/android/view/textclassifier/
Test: adb shell cmd device_config put textclassifier system_textclassifier_enabled false
adb shell dumpsys textclassification, observed that the flag is updated.
BUG: 123389900
Change-Id: Icbd26ec7ed223e40b60696d12327cb123b96c4fd
diff --git a/core/tests/coretests/src/android/provider/SettingsBackupTest.java b/core/tests/coretests/src/android/provider/SettingsBackupTest.java
index ebc6be7..3da7f72 100644
--- a/core/tests/coretests/src/android/provider/SettingsBackupTest.java
+++ b/core/tests/coretests/src/android/provider/SettingsBackupTest.java
@@ -467,6 +467,7 @@
Settings.Global.TETHER_SUPPORTED,
Settings.Global.TETHER_ENABLE_LEGACY_DHCP_SERVER,
Settings.Global.TEXT_CLASSIFIER_CONSTANTS,
+ Settings.Global.TEXT_CLASSIFIER_ACTION_MODEL_PARAMS,
Settings.Global.THEATER_MODE_ON,
Settings.Global.TIME_ONLY_MODE_CONSTANTS,
Settings.Global.TRANSITION_ANIMATION_SCALE,
diff --git a/core/tests/coretests/src/android/view/textclassifier/ConfigParserTest.java b/core/tests/coretests/src/android/view/textclassifier/ConfigParserTest.java
new file mode 100644
index 0000000..1b3c724
--- /dev/null
+++ b/core/tests/coretests/src/android/view/textclassifier/ConfigParserTest.java
@@ -0,0 +1,136 @@
+/*
+ * 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 android.view.textclassifier;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.provider.DeviceConfig;
+import android.support.test.uiautomator.UiDevice;
+
+import androidx.test.InstrumentationRegistry;
+import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.io.IOException;
+
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class ConfigParserTest {
+ private static final String SETTINGS = "int=42,float=12.3,boolean=true,string=abc";
+ private static final String CLEAR_DEVICE_CONFIG_KEY_CMD =
+ "device_config delete " + DeviceConfig.NAMESPACE_TEXTCLASSIFIER;
+ private static final String[] DEVICE_CONFIG_KEYS = new String[]{
+ "boolean",
+ "string",
+ "int",
+ "float"
+ };
+
+ private ConfigParser mConfigParser;
+
+ @Before
+ public void setup() throws IOException {
+ mConfigParser = new ConfigParser(SETTINGS);
+ clearDeviceConfig();
+ }
+
+ @After
+ public void tearDown() throws IOException {
+ clearDeviceConfig();
+ }
+
+ @Test
+ public void getBoolean_deviceConfig() {
+ DeviceConfig.setProperty(
+ DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
+ "boolean",
+ "false",
+ false);
+ boolean value = mConfigParser.getBoolean("boolean", true);
+ assertThat(value).isFalse();
+ }
+
+ @Test
+ public void getBoolean_settings() {
+ boolean value = mConfigParser.getBoolean(
+ "boolean",
+ false);
+ assertThat(value).isTrue();
+ }
+
+ @Test
+ public void getInt_deviceConfig() {
+ DeviceConfig.setProperty(
+ DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
+ "int",
+ "1",
+ false);
+ int value = mConfigParser.getInt("int", 0);
+ assertThat(value).isEqualTo(1);
+ }
+
+ @Test
+ public void getInt_settings() {
+ int value = mConfigParser.getInt("int", 0);
+ assertThat(value).isEqualTo(42);
+ }
+
+ @Test
+ public void getFloat_deviceConfig() {
+ DeviceConfig.setProperty(
+ DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
+ "float",
+ "3.14",
+ false);
+ float value = mConfigParser.getFloat("float", 0);
+ assertThat(value).isWithin(0.0001f).of(3.14f);
+ }
+
+ @Test
+ public void getFloat_settings() {
+ float value = mConfigParser.getFloat("float", 0);
+ assertThat(value).isWithin(0.0001f).of(12.3f);
+ }
+
+ @Test
+ public void getString_deviceConfig() {
+ DeviceConfig.setProperty(
+ DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
+ "string",
+ "hello",
+ false);
+ String value = mConfigParser.getString("string", "");
+ assertThat(value).isEqualTo("hello");
+ }
+
+ @Test
+ public void getString_settings() {
+ String value = mConfigParser.getString("string", "");
+ assertThat(value).isEqualTo("abc");
+ }
+
+ private static void clearDeviceConfig() throws IOException {
+ UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
+ for (String key : DEVICE_CONFIG_KEYS) {
+ uiDevice.executeShellCommand(CLEAR_DEVICE_CONFIG_KEY_CMD + " " + key);
+ }
+ }
+}