summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Antonio Kantek <kanant@google.com> 2023-10-04 00:49:33 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2023-10-04 00:49:33 +0000
commit0e8f819fee42444ddd56a64bdb5a2123873d4225 (patch)
treebb04d9f530481897b1a6cbeb09321cadf3ee15f6
parent573c9bdc878c1697515f33f8f389cd6547d0e352 (diff)
parentd24bd53f88e82f64f8135289de61634aab69e93e (diff)
Merge "Remove duplicated code in InputMethodSubtypeSwitchingController" into main
-rw-r--r--services/core/java/com/android/server/inputmethod/InputMethodSubtypeSwitchingController.java27
-rw-r--r--services/core/java/com/android/server/inputmethod/LocaleUtils.java7
-rw-r--r--services/tests/servicestests/src/com/android/server/inputmethod/LocaleUtilsTest.java8
3 files changed, 18 insertions, 24 deletions
diff --git a/services/core/java/com/android/server/inputmethod/InputMethodSubtypeSwitchingController.java b/services/core/java/com/android/server/inputmethod/InputMethodSubtypeSwitchingController.java
index c83a96938325..431aabdc3018 100644
--- a/services/core/java/com/android/server/inputmethod/InputMethodSubtypeSwitchingController.java
+++ b/services/core/java/com/android/server/inputmethod/InputMethodSubtypeSwitchingController.java
@@ -69,27 +69,16 @@ final class InputMethodSubtypeSwitchingController {
mIsSystemLanguage = true;
} else {
// TODO: Use Locale#getLanguage or Locale#toLanguageTag
- final String systemLanguage = parseLanguageFromLocaleString(systemLocale);
- final String subtypeLanguage = parseLanguageFromLocaleString(subtypeLocale);
+ final String systemLanguage = LocaleUtils.getLanguageFromLocaleString(
+ systemLocale);
+ final String subtypeLanguage = LocaleUtils.getLanguageFromLocaleString(
+ subtypeLocale);
mIsSystemLanguage = systemLanguage.length() >= 2
&& systemLanguage.equals(subtypeLanguage);
}
}
}
- /**
- * Returns the language component of a given locale string.
- * TODO: Use {@link Locale#getLanguage()} instead.
- */
- private static String parseLanguageFromLocaleString(final String locale) {
- final int idx = locale.indexOf('_');
- if (idx < 0) {
- return locale;
- } else {
- return locale.substring(0, idx);
- }
- }
-
private static int compareNullableCharSequences(@Nullable CharSequence c1,
@Nullable CharSequence c2) {
// For historical reasons, an empty text needs to put at the last.
@@ -116,7 +105,7 @@ final class InputMethodSubtypeSwitchingController {
*
* @param other the object to be compared.
* @return a negative integer, zero, or positive integer as this object is less than, equal
- * to, or greater than the specified <code>other</code> object.
+ * to, or greater than the specified <code>other</code> object.
*/
@Override
public int compareTo(ImeSubtypeListItem other) {
@@ -253,9 +242,10 @@ final class InputMethodSubtypeSwitchingController {
/**
* Returns the index of the specified input method and subtype in the given list.
- * @param imi The {@link InputMethodInfo} to be searched.
+ *
+ * @param imi The {@link InputMethodInfo} to be searched.
* @param subtype The {@link InputMethodSubtype} to be searched. null if the input method
- * does not have a subtype.
+ * does not have a subtype.
* @return The index in the given list. -1 if not found.
*/
private int getIndex(InputMethodInfo imi, InputMethodSubtype subtype) {
@@ -327,6 +317,7 @@ final class InputMethodSubtypeSwitchingController {
* {@link #mUsageHistoryOfSubtypeListItemIndex}.
* <p>We call the index of {@link #mUsageHistoryOfSubtypeListItemIndex} as "Usage Rank"
* so as not to be confused with the index in {@link #mImeSubtypeList}.
+ *
* @return -1 when the specified item doesn't belong to {@link #mImeSubtypeList} actually.
*/
private int getUsageRank(final InputMethodInfo imi, InputMethodSubtype subtype) {
diff --git a/services/core/java/com/android/server/inputmethod/LocaleUtils.java b/services/core/java/com/android/server/inputmethod/LocaleUtils.java
index f865e6010580..7d090dbcc5d8 100644
--- a/services/core/java/com/android/server/inputmethod/LocaleUtils.java
+++ b/services/core/java/com/android/server/inputmethod/LocaleUtils.java
@@ -215,12 +215,7 @@ final class LocaleUtils {
* TODO: Use {@link Locale#toLanguageTag()} and {@link Locale#forLanguageTag(String)}
*/
static String getLanguageFromLocaleString(String locale) {
- final int idx = locale.indexOf('_');
- if (idx < 0) {
- return locale;
- } else {
- return locale.substring(0, idx);
- }
+ return Locale.forLanguageTag(locale).getLanguage();
}
static Locale getSystemLocaleFromContext(Context context) {
diff --git a/services/tests/servicestests/src/com/android/server/inputmethod/LocaleUtilsTest.java b/services/tests/servicestests/src/com/android/server/inputmethod/LocaleUtilsTest.java
index 3fc0e4fa4a2c..255cb6499d8a 100644
--- a/services/tests/servicestests/src/com/android/server/inputmethod/LocaleUtilsTest.java
+++ b/services/tests/servicestests/src/com/android/server/inputmethod/LocaleUtilsTest.java
@@ -16,6 +16,8 @@
package com.android.server.inputmethod;
+import static com.google.common.truth.Truth.assertThat;
+
import static org.junit.Assert.assertEquals;
import android.os.LocaleList;
@@ -386,4 +388,10 @@ public class LocaleUtilsTest {
assertEquals(availableLocales.get(3), dest.get(0));
}
}
+
+ @Test
+ public void testGetLanguageFromLocaleString() {
+ assertThat(LocaleUtils.getLanguageFromLocaleString("en")).isEqualTo("en");
+ assertThat(LocaleUtils.getLanguageFromLocaleString("en-US")).isEqualTo("en");
+ }
}