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

Commit bc644343 authored by hjchangliao's avatar hjchangliao
Browse files

Add TogglePrefController for Color Invert

Add ColorInversionPreferenceController,
and remove old control code in fragment.

Change-Id: I773e5aa2d6c9bf6b5140be45ee60567a7ed22318
Fixes: 67997748
Test: make RunSettingsRoboTests
parent 3df8494f
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -14,7 +14,9 @@
     limitations under the License.
-->

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:settings="http://schemas.android.com/apk/res-auto"
        android:key="accessibility_settings_screen"
        android:title="@string/accessibility_settings"
        android:persistent="true">
@@ -136,6 +138,7 @@
                android:title="@string/accessibility_display_inversion_preference_title"
                android:summary="@string/accessibility_display_inversion_preference_subtitle"
                android:persistent="false"
                android:icon="@drawable/ic_color_inversion"/>
                android:icon="@drawable/ic_color_inversion"
                settings:controller="com.android.settings.accessibility.ColorInversionPreferenceController"/>
    </PreferenceCategory>
</PreferenceScreen>
+5 −11
Original line number Diff line number Diff line
@@ -224,6 +224,7 @@ public class AccessibilitySettings extends SettingsPreferenceFragment implements
    private Preference mDisplayDaltonizerPreferenceScreen;
    private Preference mVibrationPreferenceScreen;
    private SwitchPreference mToggleInversionPreference;
    private ColorInversionPreferenceController mInversionPreferenceController;

    private int mLongPressTimeoutDefault;

@@ -304,9 +305,6 @@ public class AccessibilitySettings extends SettingsPreferenceFragment implements
        if (mSelectLongPressTimeoutPreference == preference) {
            handleLongPressTimeoutPreferenceChange((String) newValue);
            return true;
        } else if (mToggleInversionPreference == preference) {
            handleToggleInversionPreferenceChange((Boolean) newValue);
            return true;
        }
        return false;
    }
@@ -318,11 +316,6 @@ public class AccessibilitySettings extends SettingsPreferenceFragment implements
                mLongPressTimeoutValueToTitleMap.get(stringValue));
    }

    private void handleToggleInversionPreferenceChange(boolean checked) {
        Settings.Secure.putInt(getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, (checked ? 1 : 0));
    }

    @Override
    public boolean onPreferenceTreeClick(Preference preference) {
        if (mToggleHighTextContrastPreference == preference) {
@@ -412,7 +405,9 @@ public class AccessibilitySettings extends SettingsPreferenceFragment implements

        // Display inversion.
        mToggleInversionPreference = (SwitchPreference) findPreference(TOGGLE_INVERSION_PREFERENCE);
        mToggleInversionPreference.setOnPreferenceChangeListener(this);
        mInversionPreferenceController =
                new ColorInversionPreferenceController(getContext(), TOGGLE_INVERSION_PREFERENCE);
        mInversionPreferenceController.displayPreference(getPreferenceScreen());

        // Power button ends calls.
        mTogglePowerButtonEndsCallPreference =
@@ -652,8 +647,7 @@ public class AccessibilitySettings extends SettingsPreferenceFragment implements
                        Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED, 0) == 1);

        // If the quick setting is enabled, the preference MUST be enabled.
        mToggleInversionPreference.setChecked(Settings.Secure.getInt(getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, 0) == 1);
        mInversionPreferenceController.updateState(mToggleInversionPreference);

        // Power button ends calls.
        if (KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_POWER)
+54 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.accessibility;

import android.content.Context;
import android.provider.Settings;

import com.android.settings.core.TogglePreferenceController;

import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

public class ColorInversionPreferenceController extends TogglePreferenceController {
    @VisibleForTesting
    static final int ON = 1;
    @VisibleForTesting
    static final int OFF = 0;

    public ColorInversionPreferenceController(Context context, String preferenceKey) {
        super(context, preferenceKey);
    }

    @Override
    public boolean isChecked() {
        return Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, OFF) == ON;
    }

    @Override
    public boolean setChecked(boolean isChecked) {
        return Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, (isChecked ? ON : OFF));
    }

    @Override
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }
}
+97 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.accessibility;

import static com.android.settings.accessibility.ColorInversionPreferenceController.OFF;
import static com.android.settings.accessibility.ColorInversionPreferenceController.ON;

import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.provider.Settings;

import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.SettingsRobolectricTestRunner;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;

import androidx.preference.SwitchPreference;

@RunWith(SettingsRobolectricTestRunner.class)
public class ColorInversionPreferenceControllerTest {
    private static final int UNKNOWN = -1;
    private Context mContext;
    private ColorInversionPreferenceController mController;
    private SwitchPreference mPreference;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mContext = RuntimeEnvironment.application;
        mController = new ColorInversionPreferenceController(mContext, "pref_key");
        mPreference = new SwitchPreference(mContext);
        mController.updateState(mPreference);
    }

    @Test
    public void getAvailabilityStatus_available() {
        assertThat(mController.getAvailabilityStatus()).isEqualTo(
                BasePreferenceController.AVAILABLE);
    }

    @Test
    public void isChecked_enabled() {
        Settings.System.putInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, ON);

        mController.updateState(mPreference);

        assertThat(mController.isChecked()).isTrue();
        assertThat(mPreference.isChecked()).isTrue();
    }

    @Test
    public void isChecked_disabled() {
        Settings.System.putInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, OFF);

        mController.updateState(mPreference);

        assertThat(mController.isChecked()).isFalse();
        assertThat(mPreference.isChecked()).isFalse();
    }

    @Test
    public void setChecked_enabled() {
        mController.setChecked(true);

        assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, UNKNOWN)).isEqualTo(ON);
    }

    @Test
    public void setChecked_disabled() {
        mController.setChecked(false);

        assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, UNKNOWN)).isEqualTo(OFF);
    }
}