Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0acd013f authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add developer menu to enable/disable ART verifier."

parents e67344a5 787a92c9
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -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>
+5 −0
Original line number Diff line number Diff line
@@ -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"
+72 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -424,6 +424,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
        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));
+109 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}