From 6980a434cda9d1e7d9e496d91bb9a7edb41990a3 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Mon, 24 Oct 2022 23:11:40 -0700 Subject: [PATCH] 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 --- .../view/inputmethod/InputMethodSubtype.java | 32 +++++++++++++++++++ .../inputmethod/InputMethodSubtypeTest.java | 29 +++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/core/java/android/view/inputmethod/InputMethodSubtype.java b/core/java/android/view/inputmethod/InputMethodSubtype.java index 96b6b437a64b9..bdfcb03286327 100644 --- a/core/java/android/view/inputmethod/InputMethodSubtype.java +++ b/core/java/android/view/inputmethod/InputMethodSubtype.java @@ -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. + * + *

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.

+ * + * @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. */ diff --git a/core/tests/coretests/src/android/view/inputmethod/InputMethodSubtypeTest.java b/core/tests/coretests/src/android/view/inputmethod/InputMethodSubtypeTest.java index bd292f04ba96e..297b07fac3404 100644 --- a/core/tests/coretests/src/android/view/inputmethod/InputMethodSubtypeTest.java +++ b/core/tests/coretests/src/android/view/inputmethod/InputMethodSubtypeTest.java @@ -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 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 {