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

Commit 7db25ab2 authored by Prabir Pradhan's avatar Prabir Pradhan Committed by Android (Google) Code Review
Browse files

Merge "Stylus: Introduce Show Stylus Hover Pointer Setting (2/2)" into main

parents d91824db e221abaa
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -12882,4 +12882,7 @@
    <!-- Authority of the content provider that support methods restartPhoneProcess and restartRild. Will be overlaid by OEM.-->
    <string name="reset_telephony_stack_content_provider_authority" translatable="false"></string>
    <!--Text for Stylus Pointer Icon preference -->
    <string name="show_stylus_pointer_icon">Show pointer while hovering</string>
</resources>
+35 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.hardware.input.InputSettings;
import android.os.Process;
import android.os.UserHandle;
import android.os.UserManager;
@@ -73,6 +74,8 @@ public class StylusDevicesController extends AbstractPreferenceController implem
    static final String KEY_IGNORE_BUTTON = "ignore_button";
    @VisibleForTesting
    static final String KEY_DEFAULT_NOTES = "default_notes";
    @VisibleForTesting
    static final String KEY_SHOW_STYLUS_POINTER_ICON = "show_stylus_pointer_icon";

    private static final String TAG = "StylusDevicesController";

@@ -181,6 +184,26 @@ public class StylusDevicesController extends AbstractPreferenceController implem
        return pref;
    }

    @Nullable
    private SwitchPreferenceCompat createShowStylusPointerIconPreference(
            SwitchPreferenceCompat preference) {
        if (!mContext.getResources()
                .getBoolean(com.android.internal.R.bool.config_enableStylusPointerIcon)) {
            // If the config is not enabled, no need to show the preference to user
            return null;
        }
        SwitchPreferenceCompat pref = preference == null ? new SwitchPreferenceCompat(mContext)
                : preference;
        pref.setKey(KEY_SHOW_STYLUS_POINTER_ICON);
        pref.setTitle(mContext.getString(R.string.show_stylus_pointer_icon));
        pref.setIcon(R.drawable.ic_stylus);
        pref.setOnPreferenceClickListener(this);
        pref.setChecked(Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.STYLUS_POINTER_ICON_ENABLED,
                InputSettings.DEFAULT_STYLUS_POINTER_ICON_ENABLED) == 1);
        return pref;
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        String key = preference.getKey();
@@ -213,6 +236,11 @@ public class StylusDevicesController extends AbstractPreferenceController implem
                        Secure.STYLUS_BUTTONS_ENABLED,
                        ((TwoStatePreference) preference).isChecked() ? 0 : 1);
                break;
            case KEY_SHOW_STYLUS_POINTER_ICON:
                Settings.Secure.putInt(mContext.getContentResolver(),
                        Secure.STYLUS_POINTER_ICON_ENABLED,
                        ((SwitchPreferenceCompat) preference).isChecked() ? 1 : 0);
                break;
        }
        return true;
    }
@@ -268,6 +296,13 @@ public class StylusDevicesController extends AbstractPreferenceController implem
        if (buttonPref == null) {
            mPreferencesContainer.addPreference(createButtonPressPreference());
        }
        SwitchPreferenceCompat currShowStylusPointerIconPref = mPreferencesContainer
                .findPreference(KEY_SHOW_STYLUS_POINTER_ICON);
        Preference showStylusPointerIconPref =
                createShowStylusPointerIconPreference(currShowStylusPointerIconPref);
        if (currShowStylusPointerIconPref == null && showStylusPointerIconPref != null) {
            mPreferencesContainer.addPreference(showStylusPointerIconPref);
        }
    }

    private boolean currentInputMethodSupportsHandwriting() {
+46 −2
Original line number Diff line number Diff line
@@ -221,7 +221,7 @@ public class StylusDevicesControllerTest {

        showScreen(controller);

        assertThat(mPreferenceContainer.getPreferenceCount()).isEqualTo(3);
        assertThat(mPreferenceContainer.getPreferenceCount()).isEqualTo(4);
    }

    @Test
@@ -249,11 +249,12 @@ public class StylusDevicesControllerTest {
    @Test
    public void btStylusInputDevice_showsAllPreferences() {
        showScreen(mController);

        Preference defaultNotesPref = mPreferenceContainer.getPreference(0);
        Preference handwritingPref = mPreferenceContainer.getPreference(1);
        Preference buttonPref = mPreferenceContainer.getPreference(2);
        Preference stylusPointerIconPref = mPreferenceContainer.getPreference(3);

        assertThat(mPreferenceContainer.getPreferenceCount()).isEqualTo(3);
        assertThat(defaultNotesPref.getTitle().toString()).isEqualTo(
                mContext.getString(R.string.stylus_default_notes_app));
        assertThat(defaultNotesPref.isVisible()).isTrue();
@@ -263,6 +264,9 @@ public class StylusDevicesControllerTest {
        assertThat(buttonPref.getTitle().toString()).isEqualTo(
                mContext.getString(R.string.stylus_ignore_button));
        assertThat(buttonPref.isVisible()).isTrue();
        assertThat(stylusPointerIconPref.getTitle().toString()).isEqualTo(
                mContext.getString(R.string.show_stylus_pointer_icon));
        assertThat(stylusPointerIconPref.isVisible()).isTrue();
    }

    @Test
@@ -551,6 +555,46 @@ public class StylusDevicesControllerTest {
                Secure.STYLUS_BUTTONS_ENABLED, -1)).isEqualTo(1);
    }

    @Test
    public void stylusPointerIconPreference_checkedWhenFlagTrue() {
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.STYLUS_POINTER_ICON_ENABLED, 1);

        showScreen(mController);
        SwitchPreferenceCompat stylusPointerIconPref =
                (SwitchPreferenceCompat) mPreferenceContainer.getPreference(3);

        assertThat(stylusPointerIconPref.isChecked()).isEqualTo(true);
    }

    @Test
    public void stylusPointerIconPreference_uncheckedWhenFlagFalse() {
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.STYLUS_POINTER_ICON_ENABLED, 0);

        showScreen(mController);
        SwitchPreferenceCompat stylusPointerIconPref =
                (SwitchPreferenceCompat) mPreferenceContainer.getPreference(3);

        assertThat(stylusPointerIconPref.isChecked()).isEqualTo(false);
    }

    @Test
    public void stylusPointerIconPreference_updatesFlagOnClick() {
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.STYLUS_POINTER_ICON_ENABLED, 0);

        showScreen(mController);
        SwitchPreferenceCompat stylusPointerIconPref =
                (SwitchPreferenceCompat) mPreferenceContainer.getPreference(3);

        stylusPointerIconPref.performClick();

        assertThat(stylusPointerIconPref.isChecked()).isEqualTo(true);
        assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
                Secure.STYLUS_POINTER_ICON_ENABLED, -1)).isEqualTo(1);
    }

    private void showScreen(StylusDevicesController controller) {
        controller.displayPreference(mScreen);
    }