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

Commit 1ceef728 authored by Amanda Lin Dietz's avatar Amanda Lin Dietz Committed by Android (Google) Code Review
Browse files

Merge "[A11y Magnification Keyboard Focus] Add toggle setting for...

Merge "[A11y Magnification Keyboard Focus] Add toggle setting for Magnification following keyboard focus" into main
parents d419986d 3ddf02ad
Loading
Loading
Loading
Loading
+4 −0
Original line number Original line Diff line number Diff line
@@ -5348,6 +5348,10 @@
    <string name="accessibility_screen_magnification_follow_typing_title">Magnify typing</string>
    <string name="accessibility_screen_magnification_follow_typing_title">Magnify typing</string>
    <!-- Summary for accessibility follow typing preference for magnification. [CHAR LIMIT=none] -->
    <!-- Summary for accessibility follow typing preference for magnification. [CHAR LIMIT=none] -->
    <string name="accessibility_screen_magnification_follow_typing_summary">Magnifier follows text as you type</string>
    <string name="accessibility_screen_magnification_follow_typing_summary">Magnifier follows text as you type</string>
    <!-- Title for focus following keyboard focus preference for magnification. [CHAR LIMIT=none] -->
    <string name="accessibility_screen_magnification_follow_keyboard_title">Magnify keyboard focus</string>
    <!-- Title for focus following keyboard focus preference for magnification. [CHAR LIMIT=none] -->
    <string name="accessibility_screen_magnification_follow_keyboard_summary">Magnifier follows keyboard focus</string>
    <!-- Title for accessibility magnifier preference allowing the magnification of navigation bar and IME. [CHAR LIMIT=35] -->
    <!-- Title for accessibility magnifier preference allowing the magnification of navigation bar and IME. [CHAR LIMIT=35] -->
    <string name="accessibility_screen_magnification_nav_ime_title">Magnify keyboard</string>
    <string name="accessibility_screen_magnification_nav_ime_title">Magnify keyboard</string>
    <!-- Summary for accessibility magnifier preference allowing the magnification of navigation bar and IME. [CHAR LIMIT=35] -->
    <!-- Summary for accessibility magnifier preference allowing the magnification of navigation bar and IME. [CHAR LIMIT=35] -->
+61 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2025 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.Context;
import android.provider.Settings;

import com.android.settings.R;

/** Controller that accesses and switches the preference status of following typing feature */
public class MagnificationFollowKeyboardPreferenceController extends
        MagnificationTogglePreferenceController {

    private static final String TAG =
            MagnificationFollowKeyboardPreferenceController.class.getSimpleName();
    static final String PREF_KEY = "magnification_focus_follow_keyboard";

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

    @Override
    public int getAvailabilityStatus() {
        return isInSetupWizard() ? CONDITIONALLY_UNAVAILABLE : AVAILABLE;
    }

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

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

    @Override
    public int getSliceHighlightMenuRes() {
        return R.string.menu_key_accessibility;
    }
}
+34 −0
Original line number Original line Diff line number Diff line
@@ -213,6 +213,10 @@ public class ToggleScreenMagnificationPreferenceFragment extends
                && hasMouse();
                && hasMouse();
    }
    }


    private static boolean isMagnificationKeyboardFollowingSupported() {
        return android.view.accessibility.Flags.requestRectangleWithSource();
    }

    private static boolean isMagnificationMagnifyNavAndImeSupported() {
    private static boolean isMagnificationMagnifyNavAndImeSupported() {
        return com.android.server.accessibility.Flags.enableMagnificationMagnifyNavBarAndIme();
        return com.android.server.accessibility.Flags.enableMagnificationMagnifyNavBarAndIme();
    }
    }
@@ -230,6 +234,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends
            addJoystickSetting(generalCategory);
            addJoystickSetting(generalCategory);
            // LINT.ThenChange(:search_data)
            // LINT.ThenChange(:search_data)
        }
        }
        addFollowKeyboardSetting(generalCategory);
        addCursorFollowingSetting(generalCategory);
        addCursorFollowingSetting(generalCategory);
    }
    }


@@ -371,6 +376,29 @@ public class ToggleScreenMagnificationPreferenceFragment extends
        addPreferenceController(followTypingPreferenceController);
        addPreferenceController(followTypingPreferenceController);
    }
    }


    private static Preference createFollowKeyboardPreference(Context context) {
        final Preference pref = new SwitchPreferenceCompat(context);
        pref.setTitle(R.string.accessibility_screen_magnification_follow_keyboard_title);
        pref.setSummary(R.string.accessibility_screen_magnification_follow_keyboard_summary);
        pref.setKey(MagnificationFollowKeyboardPreferenceController.PREF_KEY);
        return pref;
    }

    private void addFollowKeyboardSetting(PreferenceCategory generalCategory) {
        if (!isMagnificationKeyboardFollowingSupported()) {
            return;
        }

        generalCategory.addPreference(createFollowKeyboardPreference(getPrefContext()));

        var followKeyboardPreferenceController = new
                MagnificationFollowKeyboardPreferenceController(getContext(),
                MagnificationFollowKeyboardPreferenceController.PREF_KEY);
        followKeyboardPreferenceController.setInSetupWizard(mInSetupWizard);
        followKeyboardPreferenceController.displayPreference(getPreferenceScreen());
        addPreferenceController(followKeyboardPreferenceController);
    }

    private static boolean isAlwaysOnSupported(Context context) {
    private static boolean isAlwaysOnSupported(Context context) {
        final boolean defaultValue = context.getResources().getBoolean(
        final boolean defaultValue = context.getResources().getBoolean(
                com.android.internal.R.bool.config_magnification_always_on_enabled);
                com.android.internal.R.bool.config_magnification_always_on_enabled);
@@ -478,6 +506,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends


        var keysToObserve = List.of(
        var keysToObserve = List.of(
                Settings.Secure.ACCESSIBILITY_MAGNIFICATION_FOLLOW_TYPING_ENABLED,
                Settings.Secure.ACCESSIBILITY_MAGNIFICATION_FOLLOW_TYPING_ENABLED,
                Settings.Secure.ACCESSIBILITY_MAGNIFICATION_FOLLOW_KEYBOARD_ENABLED,
                Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MAGNIFY_NAV_AND_IME,
                Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MAGNIFY_NAV_AND_IME,
                Settings.Secure.ACCESSIBILITY_MAGNIFICATION_ALWAYS_ON_ENABLED,
                Settings.Secure.ACCESSIBILITY_MAGNIFICATION_ALWAYS_ON_ENABLED,
                Settings.Secure.ACCESSIBILITY_MAGNIFICATION_JOYSTICK_ENABLED
                Settings.Secure.ACCESSIBILITY_MAGNIFICATION_JOYSTICK_ENABLED
@@ -730,6 +759,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends
                    Stream.of(
                    Stream.of(
                                    createMagnificationModePreference(context),
                                    createMagnificationModePreference(context),
                                    createFollowTypingPreference(context),
                                    createFollowTypingPreference(context),
                                    createFollowKeyboardPreference(context),
                                    createOneFingerPanningPreference(context),
                                    createOneFingerPanningPreference(context),
                                    createAlwaysOnPreference(context),
                                    createAlwaysOnPreference(context),
                                    createJoystickPreference(context),
                                    createJoystickPreference(context),
@@ -774,6 +804,10 @@ public class ToggleScreenMagnificationPreferenceFragment extends
                        }
                        }
                    }
                    }


                    if (!isMagnificationKeyboardFollowingSupported()) {
                        niks.add(MagnificationFollowKeyboardPreferenceController.PREF_KEY);
                    }

                    if (!isMagnificationCursorFollowingModeDialogSupported()) {
                    if (!isMagnificationCursorFollowingModeDialogSupported()) {
                        niks.add(MagnificationCursorFollowingModePreferenceController.PREF_KEY);
                        niks.add(MagnificationCursorFollowingModePreferenceController.PREF_KEY);
                    }
                    }
+109 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2025 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.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;

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

import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

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

import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

/** Tests for {@link MagnificationFollowKeyboardPreferenceController}. */
@RunWith(RobolectricTestRunner.class)
public class MagnificationFollowKeyboardPreferenceControllerTest {

    private static final String KEY_FOLLOW_KEYBOARD =
            Settings.Secure.ACCESSIBILITY_MAGNIFICATION_FOLLOW_KEYBOARD_ENABLED;

    private final Context mContext = ApplicationProvider.getApplicationContext();
    private final SwitchPreference mSwitchPreference = spy(new SwitchPreference(mContext));
    private final MagnificationFollowKeyboardPreferenceController mController =
            new MagnificationFollowKeyboardPreferenceController(mContext,
                    MagnificationFollowKeyboardPreferenceController.PREF_KEY);

    @Before
    public void setUp() {
        final PreferenceManager preferenceManager = new PreferenceManager(mContext);
        final PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext);
        mSwitchPreference.setKey(MagnificationFollowKeyboardPreferenceController.PREF_KEY);
        screen.addPreference(mSwitchPreference);
        mController.displayPreference(screen);

        mController.updateState(mSwitchPreference);
        reset(mSwitchPreference);
    }

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

    @Test
    public void getAvailableStatus_inSetupWizard_returnConditionallyUnavailable() {
        mController.setInSetupWizard(true);
        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
    }

    @Test
    public void performClick_switchDefaultStateForFollowKeyboard_shouldReturnFalse() {
        mSwitchPreference.performClick();

        verify(mSwitchPreference).setChecked(false);
        assertThat(mController.isChecked()).isFalse();
        assertThat(mSwitchPreference.isChecked()).isFalse();
    }

    @Test
    public void updateState_enableFollowKeyboard_shouldReturnTrue() {
        Settings.Secure.putInt(mContext.getContentResolver(), KEY_FOLLOW_KEYBOARD, ON);

        mController.updateState(mSwitchPreference);

        verify(mSwitchPreference).setChecked(true);
        assertThat(mController.isChecked()).isTrue();
        assertThat(mSwitchPreference.isChecked()).isTrue();
    }

    @Test
    public void updateState_disableFollowKeyboard_shouldReturnFalse() {
        Settings.Secure.putInt(mContext.getContentResolver(), KEY_FOLLOW_KEYBOARD, OFF);

        mController.updateState(mSwitchPreference);

        verify(mSwitchPreference).setChecked(false);
        assertThat(mController.isChecked()).isFalse();
        assertThat(mSwitchPreference.isChecked()).isFalse();
    }
}
+39 −1
Original line number Original line Diff line number Diff line
@@ -138,6 +138,8 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
            Settings.Secure.ACCESSIBILITY_MAGNIFICATION_JOYSTICK_ENABLED;
            Settings.Secure.ACCESSIBILITY_MAGNIFICATION_JOYSTICK_ENABLED;
    private static final String KEY_MAGNIFY_NAV_AND_IME =
    private static final String KEY_MAGNIFY_NAV_AND_IME =
            Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MAGNIFY_NAV_AND_IME;
            Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MAGNIFY_NAV_AND_IME;
    private static final String KEY_FOLLOW_KEYBOARD =
            Settings.Secure.ACCESSIBILITY_MAGNIFICATION_FOLLOW_KEYBOARD_ENABLED;


    private FragmentController<ToggleScreenMagnificationPreferenceFragment> mFragController;
    private FragmentController<ToggleScreenMagnificationPreferenceFragment> mFragController;
    private Context mContext;
    private Context mContext;
@@ -370,6 +372,33 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
        assertThat(switchPreference.isChecked()).isFalse();
        assertThat(switchPreference.isChecked()).isFalse();
    }
    }



    @Test
    @EnableFlags(android.view.accessibility.Flags.FLAG_REQUEST_RECTANGLE_WITH_SOURCE)
    public void onResume_defaultStateForFollowingKeyboard_switchPreferenceShouldReturnFalse() {
        setKeyFollowKeyboardEnabled(false);

        mFragController.create(R.id.main_content, /* bundle= */ null).start().resume();

        final TwoStatePreference switchPreference = mFragController.get().findPreference(
                MagnificationFollowKeyboardPreferenceController.PREF_KEY);
        assertThat(switchPreference).isNotNull();
        assertThat(switchPreference.isChecked()).isFalse();
    }

    @Test
    @EnableFlags(android.view.accessibility.Flags.FLAG_REQUEST_RECTANGLE_WITH_SOURCE)
    public void onResume_enableFollowingKeyboard_switchPreferenceShouldReturnTrue() {
        setKeyFollowKeyboardEnabled(true);

        mFragController.create(R.id.main_content, /* bundle= */ null).start().resume();

        final TwoStatePreference switchPreference = mFragController.get().findPreference(
                MagnificationFollowKeyboardPreferenceController.PREF_KEY);
        assertThat(switchPreference).isNotNull();
        assertThat(switchPreference.isChecked()).isTrue();
    }

    @Test
    @Test
    public void onResume_haveRegisterToSpecificUris() {
    public void onResume_haveRegisterToSpecificUris() {
        ShadowContentResolver shadowContentResolver = Shadows.shadowOf(
        ShadowContentResolver shadowContentResolver = Shadows.shadowOf(
@@ -382,6 +411,8 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
                        Settings.Secure.ACCESSIBILITY_QS_TARGETS),
                        Settings.Secure.ACCESSIBILITY_QS_TARGETS),
                Settings.Secure.getUriFor(
                Settings.Secure.getUriFor(
                        Settings.Secure.ACCESSIBILITY_MAGNIFICATION_FOLLOW_TYPING_ENABLED),
                        Settings.Secure.ACCESSIBILITY_MAGNIFICATION_FOLLOW_TYPING_ENABLED),
                Settings.Secure.getUriFor(
                        Settings.Secure.ACCESSIBILITY_MAGNIFICATION_FOLLOW_KEYBOARD_ENABLED),
                Settings.Secure.getUriFor(
                Settings.Secure.getUriFor(
                        Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MAGNIFY_NAV_AND_IME),
                        Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MAGNIFY_NAV_AND_IME),
                Settings.Secure.getUriFor(
                Settings.Secure.getUriFor(
@@ -891,6 +922,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
                KEY_MAGNIFICATION_SHORTCUT_PREFERENCE,
                KEY_MAGNIFICATION_SHORTCUT_PREFERENCE,
                MagnificationModePreferenceController.PREF_KEY,
                MagnificationModePreferenceController.PREF_KEY,
                MagnificationFollowTypingPreferenceController.PREF_KEY,
                MagnificationFollowTypingPreferenceController.PREF_KEY,
                MagnificationFollowKeyboardPreferenceController.PREF_KEY,
                MagnificationOneFingerPanningPreferenceController.PREF_KEY,
                MagnificationOneFingerPanningPreferenceController.PREF_KEY,
                MagnificationAlwaysOnPreferenceController.PREF_KEY,
                MagnificationAlwaysOnPreferenceController.PREF_KEY,
                MagnificationJoystickPreferenceController.PREF_KEY,
                MagnificationJoystickPreferenceController.PREF_KEY,
@@ -926,7 +958,8 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
            Flags.FLAG_ENABLE_MAGNIFICATION_ONE_FINGER_PANNING_GESTURE,
            Flags.FLAG_ENABLE_MAGNIFICATION_ONE_FINGER_PANNING_GESTURE,
            Flags.FLAG_ENABLE_MAGNIFICATION_MAGNIFY_NAV_BAR_AND_IME,
            Flags.FLAG_ENABLE_MAGNIFICATION_MAGNIFY_NAV_BAR_AND_IME,
            com.android.settings.accessibility.Flags
            com.android.settings.accessibility.Flags
                    .FLAG_ENABLE_MAGNIFICATION_CURSOR_FOLLOWING_DIALOG})
                    .FLAG_ENABLE_MAGNIFICATION_CURSOR_FOLLOWING_DIALOG,
            android.view.accessibility.Flags.FLAG_REQUEST_RECTANGLE_WITH_SOURCE})
    @Config(shadows = ShadowInputDevice.class)
    @Config(shadows = ShadowInputDevice.class)
    public void getNonIndexableKeys_hasShortcutAndAllFeaturesEnabled_allItemsSearchable() {
    public void getNonIndexableKeys_hasShortcutAndAllFeaturesEnabled_allItemsSearchable() {
        mShadowAccessibilityManager.setAccessibilityShortcutTargets(
        mShadowAccessibilityManager.setAccessibilityShortcutTargets(
@@ -1039,6 +1072,11 @@ public class ToggleScreenMagnificationPreferenceFragmentTest {
                enabled ? ON : OFF);
                enabled ? ON : OFF);
    }
    }


    private void setKeyFollowKeyboardEnabled(boolean enabled) {
        Settings.Secure.putInt(mContext.getContentResolver(), KEY_FOLLOW_KEYBOARD,
                enabled ? ON : OFF);
    }

    private void addMouseDevice() {
    private void addMouseDevice() {
        int deviceId = 1;
        int deviceId = 1;
        ShadowInputDevice.sDeviceIds = new int[]{deviceId};
        ShadowInputDevice.sDeviceIds = new int[]{deviceId};