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

Commit 06db37c0 authored by Yohei Yukawa's avatar Yohei Yukawa Committed by Android (Google) Code Review
Browse files

Merge "Add InputMethodSubtype#isSuitableForPhysicalKeyboardLayoutMapping()"

parents 6d4f24f1 6980a434
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 {