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