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

Commit 6980a434 authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Add InputMethodSubtype#isSuitableForPhysicalKeyboardLayoutMapping()

This is a preparation for implementing Subtype-keyed physical keyboard
layout switching.

This CL introduces an @hide method

  InputMethodSubtype#isSuitableForPhysicalKeyboardLayoutMapping()

so that later CLs can be written on the same rule about what kind of
InputMethodSubtype is valid for physical keyboard layout swithcing.

Here is the current rule but it's subject to change.

 * hashCode() != 0 (SUBTYPE_ID_NONE)
 * getMode() must be "keyboard"
 * not isAuxiliary()
 * getCanonicalizedLanguageTag() returns a valid (non "und") value.

See test cases to see how it works.

Bug: 252816846
Test: atest FrameworksCoreTests:InputMethodSubtypeTest
Change-Id: Ifc0247041a43ef64f8a76a23832da2ee058c6958
parent 1484b9e7
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
@@ -76,6 +76,10 @@ public final class InputMethodSubtype implements Parcelable {
    /** {@hide} */
    public static final int SUBTYPE_ID_NONE = 0;

    private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";

    private static final String UNDEFINED_LANGUAGE_TAG = "und";

    private final boolean mIsAuxiliary;
    private final boolean mOverridesImplicitlyEnabledSubtype;
    private final boolean mIsAsciiCapable;
@@ -432,6 +436,34 @@ public final class InputMethodSubtype implements Parcelable {
        return result;
    }

    /**
     * Determines whether this {@link InputMethodSubtype} can be used as the key of mapping rules
     * between {@link InputMethodSubtype} and hardware keyboard layout.
     *
     * <p>Note that in a future build may require different rules.  Design the system so that the
     * system can automatically take care of any rule changes upon OTAs.</p>
     *
     * @return {@code true} if this {@link InputMethodSubtype} can be used as the key of mapping
     *         rules between {@link InputMethodSubtype} and hardware keyboard layout.
     * @hide
     */
    public boolean isSuitableForPhysicalKeyboardLayoutMapping() {
        if (hashCode() == SUBTYPE_ID_NONE) {
            return false;
        }
        if (!TextUtils.equals(getMode(), SUBTYPE_MODE_KEYBOARD)) {
            return false;
        }
        if (isAuxiliary()) {
            return false;
        }
        final String langTag = getCanonicalizedLanguageTag();
        if (langTag.isEmpty() || TextUtils.equals(langTag, UNDEFINED_LANGUAGE_TAG)) {
            return false;
        }
        return true;
    }

    /**
     * @return The mode of the subtype.
     */
+29 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import org.junit.runner.RunWith;

import java.util.Locale;
import java.util.Objects;
import java.util.function.Supplier;

@SmallTest
@RunWith(AndroidJUnit4.class)
@@ -157,6 +158,34 @@ public class InputMethodSubtypeTest {
        assertEquals(subtype.getCanonicalizedLanguageTag(), expectedLanguageTag);
    }

    @Test
    public void testIsSuitableForPhysicalKeyboardLayoutMapping() {
        final Supplier<InputMethodSubtypeBuilder> getValidBuilder = () ->
                new InputMethodSubtypeBuilder()
                        .setLanguageTag("en-US")
                        .setIsAuxiliary(false)
                        .setSubtypeMode("keyboard")
                        .setSubtypeId(1);

        assertTrue(getValidBuilder.get().build().isSuitableForPhysicalKeyboardLayoutMapping());

        // mode == "voice" is not suitable.
        assertFalse(getValidBuilder.get().setSubtypeMode("voice").build()
                .isSuitableForPhysicalKeyboardLayoutMapping());

        // Auxiliary subtype not suitable.
        assertFalse(getValidBuilder.get().setIsAuxiliary(true).build()
                .isSuitableForPhysicalKeyboardLayoutMapping());

        // languageTag == null is not suitable.
        assertFalse(getValidBuilder.get().setLanguageTag(null).build()
                .isSuitableForPhysicalKeyboardLayoutMapping());

        // languageTag == "und" is not suitable.
        assertFalse(getValidBuilder.get().setLanguageTag("und").build()
                .isSuitableForPhysicalKeyboardLayoutMapping());
    }

    private static InputMethodSubtype cloneViaParcel(final InputMethodSubtype original) {
        Parcel parcel = null;
        try {