From 0e1bf4cff61c5bbca0c1251188be79aa693a49b8 Mon Sep 17 00:00:00 2001 From: Yongshun Liu Date: Tue, 11 Mar 2025 23:00:19 -0700 Subject: [PATCH] a11y: Only show the cursor following mode setting with detected mouse This makes sure the cursor following mode setting to be visible only when a mouse device is detected. Bug: b/398066000 Flag: com.android.settings.accessibility.enable_magnification_cursor_following_dialog Test: com.android.settings.accessibility.ToggleScreenMagnificationPreferenceFragmentTest Change-Id: I7dd27e9d8a841461900e28d8ae07d17f86a0b662 --- ...ScreenMagnificationPreferenceFragment.java | 19 ++++++- ...enMagnificationPreferenceFragmentTest.java | 49 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java b/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java index 71c95c0c7bf..dad9038b566 100644 --- a/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java +++ b/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java @@ -213,8 +213,8 @@ public class ToggleScreenMagnificationPreferenceFragment extends } private static boolean isMagnificationCursorFollowingModeDialogSupported() { - // TODO(b/398066000): Hide the setting when no pointer device exists for most form factors. - return com.android.settings.accessibility.Flags.enableMagnificationCursorFollowingDialog(); + return com.android.settings.accessibility.Flags.enableMagnificationCursorFollowingDialog() + && hasMouse(); } @Override @@ -679,6 +679,21 @@ public class ToggleScreenMagnificationPreferenceFragment extends getPrefContext(), MAGNIFICATION_CONTROLLER_NAME); } + /** + * Returns if a mouse is attached. + */ + private static boolean hasMouse() { + final int[] devices = InputDevice.getDeviceIds(); + for (int i = 0; i < devices.length; i++) { + InputDevice device = InputDevice.getDevice(devices[i]); + if (device != null && (device.getSources() & InputDevice.SOURCE_MOUSE) + == InputDevice.SOURCE_MOUSE) { + return true; + } + } + return false; + } + private boolean hasHardKeyboard() { final int[] devices = InputDevice.getDeviceIds(); for (int i = 0; i < devices.length; i++) { diff --git a/tests/robotests/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentTest.java b/tests/robotests/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentTest.java index 6407c081aea..0fd374b5000 100644 --- a/tests/robotests/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentTest.java +++ b/tests/robotests/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentTest.java @@ -546,6 +546,45 @@ public class ToggleScreenMagnificationPreferenceFragmentTest { assertThat(switchPreference).isNull(); } + @Test + @DisableFlags(com.android.settings.accessibility.Flags + .FLAG_ENABLE_MAGNIFICATION_CURSOR_FOLLOWING_DIALOG) + @Config(shadows = ShadowInputDevice.class) + public void onCreateView_cursorFollowingModeDisabled_settingsPreferenceIsNull() { + addMouseDevice(); + + mFragController.create(R.id.main_content, /* bundle= */null).start().resume().get(); + + final Preference preference = mFragController.get().findPreference( + MagnificationCursorFollowingModePreferenceController.PREF_KEY); + assertThat(preference).isNull(); + } + + @Test + @EnableFlags(com.android.settings.accessibility.Flags + .FLAG_ENABLE_MAGNIFICATION_CURSOR_FOLLOWING_DIALOG) + public void onCreateView_cursorFollowingModeEnabled_settingsPreferenceIsNullWithoutMouse() { + mFragController.create(R.id.main_content, /* bundle= */null).start().resume().get(); + + final Preference preference = mFragController.get().findPreference( + MagnificationCursorFollowingModePreferenceController.PREF_KEY); + assertThat(preference).isNull(); + } + + @Test + @EnableFlags(com.android.settings.accessibility.Flags + .FLAG_ENABLE_MAGNIFICATION_CURSOR_FOLLOWING_DIALOG) + @Config(shadows = ShadowInputDevice.class) + public void onCreateView_cursorFollowingModeEnabled_settingsPreferenceIsNotNullWithMouse() { + addMouseDevice(); + + mFragController.create(R.id.main_content, /* bundle= */null).start().resume().get(); + + final Preference preference = mFragController.get().findPreference( + MagnificationCursorFollowingModePreferenceController.PREF_KEY); + assertThat(preference).isNotNull(); + } + @Test public void onCreateView_setDialogDelegateAndAddTheControllerToLifeCycleObserver() { Correspondence instanceOf = Correspondence.transforming( @@ -903,11 +942,13 @@ public class ToggleScreenMagnificationPreferenceFragmentTest { Flags.FLAG_ENABLE_LOW_VISION_HATS, com.android.settings.accessibility.Flags .FLAG_ENABLE_MAGNIFICATION_CURSOR_FOLLOWING_DIALOG}) + @Config(shadows = ShadowInputDevice.class) public void getNonIndexableKeys_hasShortcutAndAllFeaturesEnabled_allItemsSearchable() { mShadowAccessibilityManager.setAccessibilityShortcutTargets( TRIPLETAP, List.of(MAGNIFICATION_CONTROLLER_NAME)); setAlwaysOnSupported(true); setJoystickSupported(true); + addMouseDevice(); final List niks = ToggleScreenMagnificationPreferenceFragment .SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); @@ -1026,6 +1067,14 @@ public class ToggleScreenMagnificationPreferenceFragmentTest { enabled ? ON : OFF); } + private void addMouseDevice() { + int deviceId = 1; + ShadowInputDevice.sDeviceIds = new int[]{deviceId}; + InputDevice device = ShadowInputDevice.makeInputDevicebyIdWithSources(deviceId, + InputDevice.SOURCE_MOUSE); + ShadowInputDevice.addDevice(deviceId, device); + } + private String getStringFromSettings(String key) { return Settings.Secure.getString(mContext.getContentResolver(), key); }