Add developer menu to enable/disable ART verifier.

The new developer option modifies the global setting "art_verifier_verify_debuggable"; it allows to check if the bytecode of the Debuggable Apps is correct, e.g. if it's using the correct APIs.

Test: make RunSettingsRoboTests ROBOTEST_FILTER=development
Bug: 124403695
Change-Id: I8e721d62c5ca6fb5f26d9b989a8242eece8f55ab
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 9c29a6b..0d5a166 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1859,6 +1859,11 @@
     <!-- This string asks the user whether or not to allow an app to disable WiFi. [CHAR LIMIT=NONE] -->
     <string name="wifi_ask_disable"><xliff:g id="requester" example="FancyApp">%s</xliff:g> wants to turn off Wi-Fi</string>
 
+    <!-- Debugging developer settings: Enable ART verifier for Debuggable Apps [CHAR LIMIT=40] -->
+    <string name="art_verifier_for_debuggable_title">Verify bytecode of debuggable apps</string>
+    <!-- Debugging developer settings: Enable ART verifier for Debuggable Apps [CHAR LIMIT=NONE] -->
+    <string name="art_verifier_for_debuggable_summary">Allow ART to verify bytecode for debuggable apps</string>
+
     <!-- NFC settings -->
     <!-- Used in the 1st-level settings screen to turn on NFC -->
     <string name="nfc_quick_toggle_title">NFC</string>
diff --git a/res/xml/development_settings.xml b/res/xml/development_settings.xml
index d9b592c..032f622 100644
--- a/res/xml/development_settings.xml
+++ b/res/xml/development_settings.xml
@@ -175,6 +175,11 @@
             android:title="@string/verify_apps_over_usb_title"
             android:summary="@string/verify_apps_over_usb_summary" />
 
+        <SwitchPreference
+            android:key="art_verifier_for_debuggable"
+            android:title="@string/art_verifier_for_debuggable_title"
+            android:summary="@string/art_verifier_for_debuggable_summary" />
+
         <ListPreference
             android:key="select_logd_size"
             android:title="@string/select_logd_size_title"
diff --git a/src/com/android/settings/development/ArtVerifierPreferenceController.java b/src/com/android/settings/development/ArtVerifierPreferenceController.java
new file mode 100644
index 0000000..4b2f030
--- /dev/null
+++ b/src/com/android/settings/development/ArtVerifierPreferenceController.java
@@ -0,0 +1,72 @@
+/*
+ * 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.settings.development;
+
+import android.content.Context;
+import android.provider.Settings;
+
+import androidx.annotation.VisibleForTesting;
+import androidx.preference.Preference;
+import androidx.preference.SwitchPreference;
+
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settingslib.development.DeveloperOptionsPreferenceController;
+
+public class ArtVerifierPreferenceController extends DeveloperOptionsPreferenceController
+        implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
+
+    private static final String ART_VERIFIER_FOR_DEBUGGABLE = "art_verifier_for_debuggable";
+
+    @VisibleForTesting
+    static final int SETTING_VALUE_ON = 1;
+    @VisibleForTesting
+    static final int SETTING_VALUE_OFF = 0;
+
+    public ArtVerifierPreferenceController(Context context) {
+        super(context);
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        return ART_VERIFIER_FOR_DEBUGGABLE;
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        final boolean isEnabled = (Boolean) newValue;
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.ART_VERIFIER_VERIFY_DEBUGGABLE,
+                isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF);
+        return true;
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        final int verifyDebuggable = Settings.Global.getInt(
+                mContext.getContentResolver(),
+                Settings.Global.ART_VERIFIER_VERIFY_DEBUGGABLE, SETTING_VALUE_ON);
+        ((SwitchPreference) mPreference).setChecked(verifyDebuggable != SETTING_VALUE_OFF);
+    }
+
+    @Override
+    protected void onDeveloperOptionsSwitchDisabled() {
+        super.onDeveloperOptionsSwitchDisabled();
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.ART_VERIFIER_VERIFY_DEBUGGABLE, SETTING_VALUE_ON);
+        ((SwitchPreference) mPreference).setChecked(true);
+    }
+}
diff --git a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
index 78520c6..adc23a8 100644
--- a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
+++ b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
@@ -424,6 +424,7 @@
         controllers.add(new WaitForDebuggerPreferenceController(context));
         controllers.add(new EnableGpuDebugLayersPreferenceController(context));
         controllers.add(new VerifyAppsOverUsbPreferenceController(context));
+        controllers.add(new ArtVerifierPreferenceController(context));
         controllers.add(new LogdSizePreferenceController(context));
         controllers.add(new LogPersistPreferenceController(context, fragment, lifecycle));
         controllers.add(new CameraLaserSensorPreferenceController(context));
diff --git a/tests/robotests/src/com/android/settings/development/ArtVerifierPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/ArtVerifierPreferenceControllerTest.java
new file mode 100644
index 0000000..e5dade4
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/development/ArtVerifierPreferenceControllerTest.java
@@ -0,0 +1,109 @@
+/*
+ * 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.settings.development;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.provider.Settings;
+
+import androidx.preference.PreferenceScreen;
+import androidx.preference.SwitchPreference;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+@RunWith(RobolectricTestRunner.class)
+public class ArtVerifierPreferenceControllerTest {
+
+    @Mock
+    private SwitchPreference mPreference;
+    @Mock
+    private PreferenceScreen mPreferenceScreen;
+
+    private Context mContext;
+    private ArtVerifierPreferenceController mController;
+
+    @Before
+    public void setup() {
+        MockitoAnnotations.initMocks(this);
+        mContext = RuntimeEnvironment.application;
+        mController = new ArtVerifierPreferenceController(mContext);
+        when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
+            .thenReturn(mPreference);
+        mController.displayPreference(mPreferenceScreen);
+    }
+
+    @Test
+    public void onPreferenceChanged_settingEnabled_turnOnArtVerifier() {
+        mController.onPreferenceChange(mPreference, true /* new value */);
+
+        final int mode = Settings.Global.getInt(mContext.getContentResolver(),
+                Settings.Global.ART_VERIFIER_VERIFY_DEBUGGABLE, -1 /* default */);
+
+        assertThat(mode).isEqualTo(ArtVerifierPreferenceController.SETTING_VALUE_ON);
+    }
+
+    @Test
+    public void onPreferenceChanged_settingDisabled_turnOffArtVerifier() {
+        mController.onPreferenceChange(mPreference, false /* new value */);
+
+        final int mode = Settings.Global.getInt(mContext.getContentResolver(),
+                Settings.Global.ART_VERIFIER_VERIFY_DEBUGGABLE, -1 /* default */);
+
+        assertThat(mode).isEqualTo(ArtVerifierPreferenceController.SETTING_VALUE_OFF);
+    }
+
+    @Test
+    public void updateState_settingEnabled_preferenceShouldBeChecked() {
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.ART_VERIFIER_VERIFY_DEBUGGABLE,
+                ArtVerifierPreferenceController.SETTING_VALUE_ON);
+        mController.updateState(mPreference);
+
+        verify(mPreference).setChecked(true);
+    }
+
+    @Test
+    public void updateState_settingDisabled_preferenceShouldBeChecked() {
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.ART_VERIFIER_VERIFY_DEBUGGABLE,
+                ArtVerifierPreferenceController.SETTING_VALUE_OFF);
+        mController.updateState(mPreference);
+
+        verify(mPreference).setChecked(false);
+    }
+
+    @Test
+    public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() {
+        mController.onDeveloperOptionsSwitchDisabled();
+        final int mode = Settings.Global.getInt(mContext.getContentResolver(),
+                Settings.Global.ART_VERIFIER_VERIFY_DEBUGGABLE, -1 /* default */);
+
+        assertThat(mode).isEqualTo(ArtVerifierPreferenceController.SETTING_VALUE_ON);
+        verify(mPreference).setEnabled(false);
+        verify(mPreference).setChecked(true);
+    }
+}