From 28b4b34f926c1a43bd280b63cac96800b1ad8771 Mon Sep 17 00:00:00 2001 From: Lauren Winston Date: Tue, 23 Aug 2022 21:32:49 +0000 Subject: [PATCH] Add toggles for two Software Cursor settings: keyboard shift and trigger hints. Bug: 242218807 Test: atest SoftwareCursorKeyboardShiftPreferenceControllerTest and SoftwareCursorTriggerHintsPreferenceControllerTest Change-Id: I872291ee90b54842b237868a72d61b38efbfd886 --- res/values/strings.xml | 7 + res/xml/accessibility_cursor_settings.xml | 10 ++ ...rsorKeyboardShiftPreferenceController.java | 78 ++++++++++++ ...ursorTriggerHintsPreferenceController.java | 76 +++++++++++ ...KeyboardShiftPreferenceControllerTest.java | 120 ++++++++++++++++++ ...rTriggerHintsPreferenceControllerTest.java | 120 ++++++++++++++++++ 6 files changed, 411 insertions(+) create mode 100644 src/com/android/settings/accessibility/SoftwareCursorKeyboardShiftPreferenceController.java create mode 100644 src/com/android/settings/accessibility/SoftwareCursorTriggerHintsPreferenceController.java create mode 100644 tests/unit/src/com/android/settings/accessibility/SoftwareCursorKeyboardShiftPreferenceControllerTest.java create mode 100644 tests/unit/src/com/android/settings/accessibility/SoftwareCursorTriggerHintsPreferenceControllerTest.java diff --git a/res/values/strings.xml b/res/values/strings.xml index c0348ee48a1..04121afa0f0 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -13708,4 +13708,11 @@ QR code isn\u0027t a valid format + + + + Gesture detection area hints + + Shift gesture detection region above keyboard + diff --git a/res/xml/accessibility_cursor_settings.xml b/res/xml/accessibility_cursor_settings.xml index 6bc50bb58ae..1d23051ebbd 100644 --- a/res/xml/accessibility_cursor_settings.xml +++ b/res/xml/accessibility_cursor_settings.xml @@ -22,4 +22,14 @@ android:key="screen_software_cursor_preference_switch" android:title="@string/accessibility_screen_software_cursor_title" settings:controller="com.android.settings.accessibility.SoftwareCursorTogglePreferenceController"/> + + + + diff --git a/src/com/android/settings/accessibility/SoftwareCursorKeyboardShiftPreferenceController.java b/src/com/android/settings/accessibility/SoftwareCursorKeyboardShiftPreferenceController.java new file mode 100644 index 00000000000..6ff49a3a853 --- /dev/null +++ b/src/com/android/settings/accessibility/SoftwareCursorKeyboardShiftPreferenceController.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2022 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.AccessibilityUtil.State.OFF; +import static com.android.settings.accessibility.AccessibilityUtil.State.ON; + +import android.content.ContentResolver; +import android.content.Context; +import android.provider.Settings; + +import androidx.preference.PreferenceScreen; +import androidx.preference.SwitchPreference; + +import com.android.settings.R; +import com.android.settings.core.TogglePreferenceController; + +/** Controller class for the Software Cursor keyboard shift setting. */ +public class SoftwareCursorKeyboardShiftPreferenceController extends + TogglePreferenceController { + + private static final String SETTINGS_VALUE = + Settings.Secure.ACCESSIBILITY_SOFTWARE_CURSOR_KEYBOARD_SHIFT_ENABLED; + + private final ContentResolver mContentResolver; + + + public SoftwareCursorKeyboardShiftPreferenceController(Context context, String preferenceKey) { + super(context, preferenceKey); + mContentResolver = context.getContentResolver(); + } + + @Override + public boolean isChecked() { + return Settings.Secure.getInt(mContentResolver, SETTINGS_VALUE, OFF) == ON; + } + + @Override + public boolean setChecked(boolean isChecked) { + Settings.Secure.putInt(mContentResolver, SETTINGS_VALUE, isChecked ? ON : OFF); + return true; + } + + @Override + public int getAvailabilityStatus() { + return AVAILABLE; + } + + @Override + public int getSliceHighlightMenuRes() { + return R.string.menu_key_accessibility; + } + + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + + SwitchPreference preference = screen.findPreference(getPreferenceKey()); + if (preference != null) { + preference.setOnPreferenceChangeListener(this); + preference.setChecked(isChecked()); + } + } +} diff --git a/src/com/android/settings/accessibility/SoftwareCursorTriggerHintsPreferenceController.java b/src/com/android/settings/accessibility/SoftwareCursorTriggerHintsPreferenceController.java new file mode 100644 index 00000000000..bd6e1470ac0 --- /dev/null +++ b/src/com/android/settings/accessibility/SoftwareCursorTriggerHintsPreferenceController.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2022 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.AccessibilityUtil.State.OFF; +import static com.android.settings.accessibility.AccessibilityUtil.State.ON; + +import android.content.ContentResolver; +import android.content.Context; +import android.provider.Settings; + +import androidx.preference.PreferenceScreen; +import androidx.preference.SwitchPreference; + +import com.android.settings.R; +import com.android.settings.core.TogglePreferenceController; + +/** Controller class that control accessibility software cursor trigger hints settings. */ +public class SoftwareCursorTriggerHintsPreferenceController extends TogglePreferenceController { + + private static final String SETTINGS_VALUE = + Settings.Secure.ACCESSIBILITY_SOFTWARE_CURSOR_TRIGGER_HINTS_ENABLED; + + private final ContentResolver mContentResolver; + + public SoftwareCursorTriggerHintsPreferenceController(Context context, String preferenceKey) { + super(context, preferenceKey); + mContentResolver = context.getContentResolver(); + } + + @Override + public boolean isChecked() { + return Settings.Secure.getInt(mContentResolver, SETTINGS_VALUE, OFF) == ON; + } + + @Override + public boolean setChecked(boolean isChecked) { + Settings.Secure.putInt(mContentResolver, SETTINGS_VALUE, isChecked ? ON : OFF); + return true; + } + + @Override + public int getSliceHighlightMenuRes() { + return R.string.menu_key_accessibility; + } + + @Override + public int getAvailabilityStatus() { + return AVAILABLE; + } + + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + + SwitchPreference preference = screen.findPreference(getPreferenceKey()); + if (preference != null) { + preference.setOnPreferenceChangeListener(this); + preference.setChecked(isChecked()); + } + } +} diff --git a/tests/unit/src/com/android/settings/accessibility/SoftwareCursorKeyboardShiftPreferenceControllerTest.java b/tests/unit/src/com/android/settings/accessibility/SoftwareCursorKeyboardShiftPreferenceControllerTest.java new file mode 100644 index 00000000000..2df291ab9bd --- /dev/null +++ b/tests/unit/src/com/android/settings/accessibility/SoftwareCursorKeyboardShiftPreferenceControllerTest.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2022 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.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.os.Looper; +import android.provider.Settings; + +import androidx.preference.PreferenceManager; +import androidx.preference.PreferenceScreen; +import androidx.preference.SwitchPreference; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import com.android.settings.core.BasePreferenceController; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** Tests for {@link SoftwareCursorKeyboardShiftPreferenceController}. */ +@RunWith(AndroidJUnit4.class) +public class SoftwareCursorKeyboardShiftPreferenceControllerTest { + + private PreferenceScreen mScreen; + private final Context mContext = ApplicationProvider.getApplicationContext(); + private SoftwareCursorKeyboardShiftPreferenceController mController; + private SwitchPreference mSwitchPreference; + + @Before + public void setUp() { + if (Looper.myLooper() == null) { + Looper.prepare(); + } + mController = new SoftwareCursorKeyboardShiftPreferenceController(mContext, + "cursor_trigger_keyboard_shift_enabled"); + mSwitchPreference = new SwitchPreference(mContext); + mSwitchPreference.setKey(mController.getPreferenceKey()); + PreferenceManager preferenceManager = new PreferenceManager(mContext); + mScreen = preferenceManager.createPreferenceScreen(mContext); + mScreen.addPreference(mSwitchPreference); + } + + @After + public void tearDown() { + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Secure.ACCESSIBILITY_SOFTWARE_CURSOR_KEYBOARD_SHIFT_ENABLED, OFF); + } + + @Test + public void getAvailabilityStatus_shouldReturnAvailable() { + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.AVAILABLE); + } + + @Test + public void performClick_cursorEnabled_shouldSetSettingDisabled() { + mController.setChecked(true); + mController.displayPreference(mScreen); + + mSwitchPreference.performClick(); + + assertThat(mSwitchPreference.isChecked()).isFalse(); + assertThat(isKeyboardShiftEnabled()).isFalse(); + } + + @Test + public void performClick_cursorDisabled_shouldSetSettingEnabled() { + mController.setChecked(false); + mController.displayPreference(mScreen); + + mSwitchPreference.performClick(); + + assertThat(mSwitchPreference.isChecked()).isTrue(); + assertThat(isKeyboardShiftEnabled()).isTrue(); + } + + @Test + public void setChecked_switchChecked_shouldSetSettingEnabled() { + mController.displayPreference(mScreen); + + mController.setChecked(/* isChecked= */ true); + + assertThat(isKeyboardShiftEnabled()).isTrue(); + } + + @Test + public void setChecked_switchUnchecked_shouldSetSettingDisabled() { + mController.displayPreference(mScreen); + + mController.setChecked(/* isChecked= */ false); + + assertThat(isKeyboardShiftEnabled()).isFalse(); + } + + private boolean isKeyboardShiftEnabled() { + return Settings.Secure.getInt(mContext.getContentResolver(), + Settings.Secure.ACCESSIBILITY_SOFTWARE_CURSOR_KEYBOARD_SHIFT_ENABLED, OFF) == ON; + } +} diff --git a/tests/unit/src/com/android/settings/accessibility/SoftwareCursorTriggerHintsPreferenceControllerTest.java b/tests/unit/src/com/android/settings/accessibility/SoftwareCursorTriggerHintsPreferenceControllerTest.java new file mode 100644 index 00000000000..73c118fa12d --- /dev/null +++ b/tests/unit/src/com/android/settings/accessibility/SoftwareCursorTriggerHintsPreferenceControllerTest.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2022 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.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.os.Looper; +import android.provider.Settings; + +import androidx.preference.PreferenceManager; +import androidx.preference.PreferenceScreen; +import androidx.preference.SwitchPreference; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import com.android.settings.core.BasePreferenceController; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** Tests for {@link SoftwareCursorTriggerHintsPreferenceController}. */ +@RunWith(AndroidJUnit4.class) +public class SoftwareCursorTriggerHintsPreferenceControllerTest { + + private PreferenceScreen mScreen; + private final Context mContext = ApplicationProvider.getApplicationContext(); + private SoftwareCursorTriggerHintsPreferenceController mController; + private SwitchPreference mSwitchPreference; + + @Before + public void setUp() { + if (Looper.myLooper() == null) { + Looper.prepare(); + } + mController = new SoftwareCursorTriggerHintsPreferenceController(mContext, + "cursor_trigger_hints_enabled"); + mSwitchPreference = new SwitchPreference(mContext); + mSwitchPreference.setKey(mController.getPreferenceKey()); + PreferenceManager preferenceManager = new PreferenceManager(mContext); + mScreen = preferenceManager.createPreferenceScreen(mContext); + mScreen.addPreference(mSwitchPreference); + } + + @After + public void tearDown() { + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Secure.ACCESSIBILITY_SOFTWARE_CURSOR_TRIGGER_HINTS_ENABLED, OFF); + } + + @Test + public void getAvailabilityStatus_shouldReturnAvailable() { + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.AVAILABLE); + } + + @Test + public void performClick_cursorEnabled_shouldSetSettingDisabled() { + mController.setChecked(true); + mController.displayPreference(mScreen); + + mSwitchPreference.performClick(); + + assertThat(mSwitchPreference.isChecked()).isFalse(); + assertThat(areTriggerHintsEnabled()).isFalse(); + } + + @Test + public void performClick_cursorDisabled_shouldSetSettingEnabled() { + mController.setChecked(false); + mController.displayPreference(mScreen); + + mSwitchPreference.performClick(); + + assertThat(mSwitchPreference.isChecked()).isTrue(); + assertThat(areTriggerHintsEnabled()).isTrue(); + } + + @Test + public void setChecked_switchChecked_shouldSetSettingEnabled() { + mController.displayPreference(mScreen); + + mController.setChecked(/* isChecked= */ true); + + assertThat(areTriggerHintsEnabled()).isTrue(); + } + + @Test + public void setChecked_switchUnchecked_shouldSetSettingDisabled() { + mController.displayPreference(mScreen); + + mController.setChecked(/* isChecked= */ false); + + assertThat(areTriggerHintsEnabled()).isFalse(); + } + + private boolean areTriggerHintsEnabled() { + return Settings.Secure.getInt(mContext.getContentResolver(), + Settings.Secure.ACCESSIBILITY_SOFTWARE_CURSOR_TRIGGER_HINTS_ENABLED, OFF) == ON; + } +}