feat(force invert): add force invert Settings preference in A11y > Color and Motion

Bug: 312289375
Test: atest packages/apps/Settings/tests/robotests/src/com/android/settings/accessibility/ColorAndMotionFragmentTest.java
  && atest packages/apps/Settings/tests/robotests/src/com/android/settings/accessibility/ToggleForceInvertPreferenceControllerTest.java
Flag: android.view.accessibility.Flags.forceInvertColor

Change-Id: I2739a1dfecbbddcd240a3113344987ade2b7c717
This commit is contained in:
Tyler Freeman
2023-11-28 15:04:54 -08:00
parent b75a956c10
commit e0f10d3fe2
6 changed files with 214 additions and 0 deletions

View File

@@ -4805,6 +4805,10 @@
<string name="accessibility_toggle_large_pointer_icon_title">Large mouse pointer</string>
<!-- Summary for the accessibility preference for enabling/disabling large icons for mouse/trackpad pointers. [CHAR LIMIT=60] -->
<string name="accessibility_toggle_large_pointer_icon_summary">Make the mouse pointer more noticeable</string>
<!-- Title for the accessibility preference for forcing all apps to use dark theme. [CHAR LIMIT=35] -->
<string name="accessibility_force_invert_title">Make all apps dark</string>
<!-- Summary for the accessibility preference for forcing all apps to use dark theme. [CHAR LIMIT=100] -->
<string name="accessibility_force_invert_summary">Applies to apps without their own dark theme. Some apps may have display issues, like inverted colors.</string>
<!-- Title for the accessibility preference for disabling animations. [CHAR LIMIT=35] -->
<string name="accessibility_disable_animations">Remove animations</string>
<!-- Summary for the accessibility preference for disabling animations. [CHAR LIMIT=60] -->

View File

@@ -49,6 +49,14 @@
settings:controller="com.android.settings.display.DarkUIPreferenceController"
settings:searchable="false"/>
<SwitchPreferenceCompat
android:icon="@drawable/ic_dark_ui"
android:key="toggle_force_invert"
android:persistent="false"
android:summary="@string/accessibility_force_invert_summary"
android:title="@string/accessibility_force_invert_title"
settings:controller="com.android.settings.accessibility.ToggleForceInvertPreferenceController"/>
<SwitchPreferenceCompat
android:icon="@drawable/ic_accessibility_animation"
android:key="toggle_disable_animations"

View File

@@ -21,11 +21,13 @@ import android.hardware.display.ColorDisplayManager;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.view.accessibility.Flags;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.TwoStatePreference;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
@@ -47,6 +49,8 @@ public class ColorAndMotionFragment extends DashboardFragment {
private static final String DISPLAY_DALTONIZER_PREFERENCE_SCREEN = "daltonizer_preference";
private static final String TOGGLE_DISABLE_ANIMATIONS = "toggle_disable_animations";
private static final String TOGGLE_LARGE_POINTER_ICON = "toggle_large_pointer_icon";
@VisibleForTesting
static final String TOGGLE_FORCE_INVERT = "toggle_force_invert";
private Preference mDisplayDaltonizerPreferenceScreen;
private TwoStatePreference mToggleDisableAnimationsPreference;
@@ -70,6 +74,9 @@ public class ColorAndMotionFragment extends DashboardFragment {
mShortcutFeatureKeys.add(Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED);
mShortcutFeatureKeys.add(Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE);
mShortcutFeatureKeys.add(Settings.Secure.ACCESSIBILITY_BUTTON_TARGETS);
if (Flags.forceInvertColor()) {
mShortcutFeatureKeys.add(ToggleForceInvertPreferenceController.SETTINGS_KEY);
}
mSettingsContentObserver = new AccessibilitySettingsContentObserver(new Handler());
mSettingsContentObserver.registerKeysToObserverCallback(mShortcutFeatureKeys,

View File

@@ -0,0 +1,63 @@
/*
* 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.accessibility;
import android.content.Context;
import android.provider.Settings;
import android.view.accessibility.Flags;
import androidx.annotation.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.core.TogglePreferenceController;
/** A toggle preference controller for force invert (force dark). */
public class ToggleForceInvertPreferenceController extends TogglePreferenceController {
public static final String SETTINGS_KEY =
Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED;
@VisibleForTesting
static final int ON = 1;
@VisibleForTesting
static final int OFF = 0;
public ToggleForceInvertPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
public boolean isChecked() {
return Settings.Secure.getInt(mContext.getContentResolver(), SETTINGS_KEY, OFF) != OFF;
}
@Override
public boolean setChecked(boolean isChecked) {
return Settings.Secure.putInt(mContext.getContentResolver(),
SETTINGS_KEY, isChecked ? ON : OFF);
}
@Override
public int getAvailabilityStatus() {
return Flags.forceInvertColor() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
@Override
public int getSliceHighlightMenuRes() {
return R.string.menu_key_accessibility;
}
}

View File

@@ -16,10 +16,16 @@
package com.android.settings.accessibility;
import static android.view.accessibility.Flags.FLAG_FORCE_INVERT_COLOR;
import static com.google.common.truth.Truth.assertThat;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import androidx.test.core.app.ApplicationProvider;
@@ -27,6 +33,7 @@ import com.android.settings.R;
import com.android.settings.testutils.XmlTestUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@@ -37,6 +44,9 @@ import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class ColorAndMotionFragmentTest {
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
private final Context mContext = ApplicationProvider.getApplicationContext();
private ColorAndMotionFragment mFragment;
@@ -63,6 +73,20 @@ public class ColorAndMotionFragmentTest {
}
@Test
@RequiresFlagsEnabled(FLAG_FORCE_INVERT_COLOR)
public void forceInvertEnabled_getNonIndexableKeys_existInXmlLayout() {
final List<String> niks = ColorAndMotionFragment.SEARCH_INDEX_DATA_PROVIDER
.getNonIndexableKeys(mContext);
final List<String> keys =
XmlTestUtils.getKeysFromPreferenceXml(mContext,
R.xml.accessibility_color_and_motion);
assertThat(niks).doesNotContain(ColorAndMotionFragment.TOGGLE_FORCE_INVERT);
assertThat(keys).containsAtLeastElementsIn(niks);
}
@Test
@RequiresFlagsDisabled(FLAG_FORCE_INVERT_COLOR)
public void getNonIndexableKeys_existInXmlLayout() {
final List<String> niks = ColorAndMotionFragment.SEARCH_INDEX_DATA_PROVIDER
.getNonIndexableKeys(mContext);
@@ -70,6 +94,7 @@ public class ColorAndMotionFragmentTest {
XmlTestUtils.getKeysFromPreferenceXml(mContext,
R.xml.accessibility_color_and_motion);
assertThat(niks).contains(ColorAndMotionFragment.TOGGLE_FORCE_INVERT);
assertThat(keys).containsAtLeastElementsIn(niks);
}
}

View File

@@ -0,0 +1,107 @@
/*
* 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.accessibility;
import static android.view.accessibility.Flags.FLAG_FORCE_INVERT_COLOR;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.provider.Settings;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
/** Tests for {@link ToggleForceInvertPreferenceController}. */
@RunWith(RobolectricTestRunner.class)
public class ToggleForceInvertPreferenceControllerTest {
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
private final Context mContext = ApplicationProvider.getApplicationContext();
private ToggleForceInvertPreferenceController mController;
@Before
public void setUp() {
mController = new ToggleForceInvertPreferenceController(
mContext,
ColorAndMotionFragment.TOGGLE_FORCE_INVERT
);
}
@Test
@RequiresFlagsDisabled(FLAG_FORCE_INVERT_COLOR)
public void flagOff_getAvailabilityStatus_shouldReturnUnsupported() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
}
@Test
@RequiresFlagsEnabled(FLAG_FORCE_INVERT_COLOR)
public void flagOn_getAvailabilityStatus_shouldReturnAvailable() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void settingOff_reflectsCorrectValue() {
setEnabled(false);
assertThat(mController.isChecked()).isFalse();
}
@Test
public void settingOn_reflectsCorrectValue() {
setEnabled(true);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void onCheck_settingChanges() {
setEnabled(false);
mController.setChecked(true);
assertThat(isEnabled()).isTrue();
mController.setChecked(false);
assertThat(isEnabled()).isFalse();
}
private boolean isEnabled() {
return Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED, /* def=*/ -1) == ON;
}
private void setEnabled(boolean enabled) {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED, enabled ? ON : OFF);
}
}