am cc729b6d: Merge "Refactor SubtypeLocaleUtils a bit"

* commit 'cc729b6df522e267d71445f60ad5b85d62688336':
  Refactor SubtypeLocaleUtils a bit
main
Tadashi G. Takaoka 2014-04-10 08:53:23 +00:00 committed by Android Git Automerger
commit bd77ec3bb1
3 changed files with 33 additions and 29 deletions

View File

@ -365,18 +365,12 @@
(Compact) can be an abbreviation to fit in the CHAR LIMIT.
TODO: Remove translatable=false once we are settled down with the naming. -->
<string name="subtype_generic_compact" translatable="false"><xliff:g id="LANGUAGE_NAME" example="Hindi">%s</xliff:g> (Compact)</string>
<!-- TODO: Uncomment once we can handle IETF language tag with script name specified.
Description for Serbian Cyrillic keyboard subtype [CHAR LIMIT=25]
(Cyrillic) can be an abbreviation to fit in the CHAR LIMIT.
<string name="subtype_serbian_cyrillic">Serbian (Cyrillic)</string>
Description for Serbian Latin keyboard subtype [CHAR LIMIT=25]
(Latin) can be an abbreviation to fit in the CHAR LIMIT.
<string name="subtype_serbian_latin">Serbian (Latin)</string>
Description for Serbian Latin keyboard subtype with explicit keyboard layout [CHAR LIMIT=25]
(Latin) can be an abbreviation to fit in the CHAR LIMIT.
This should be identical to subtype_serbian_latin aside from the trailing (%s).
<string name="subtype_with_layout_sr-Latn">Serbian (Latin) (<xliff:g id="KEYBOARD_LAYOUT" example="QWERTY">%s</xliff:g>)</string>
-->
<!-- Description for "LANGUAGE_NAME" (Cyrillic) keyboard subtype [CHAR LIMIT=25]
(Cyrillic) can be an abbreviation to fit in the CHAR LIMIT. -->
<string name="subtype_generic_cyrillic"><xliff:g id="LANGUAGE_NAME" example="Serbian">%s</xliff:g> (Cyrillic)</string>
<!-- Description for "LANGUAGE_NAME" (Latin) keyboard subtype [CHAR LIMIT=25]
(Latin) can be an abbreviation to fit in the CHAR LIMIT. -->
<string name="subtype_generic_latin"><xliff:g id="LANGUAGE_NAME" example="Serbian">%s</xliff:g> (Latin)</string>
<!-- This string is displayed in a language list that allows to choose a language for
suggestions in a software keyboard. This setting won't give suggestions in any particular
language, hence "No language".

View File

@ -18,8 +18,6 @@ package com.android.inputmethod.latin.utils;
import android.view.inputmethod.InputMethodSubtype;
import java.util.Locale;
public final class SpacebarLanguageUtils {
private SpacebarLanguageUtils() {
// Intentional empty constructor for utility class.
@ -55,7 +53,6 @@ public final class SpacebarLanguageUtils {
if (SubtypeLocaleUtils.isNoLanguage(subtype)) {
return SubtypeLocaleUtils.getKeyboardLayoutSetDisplayName(subtype);
}
final Locale locale = SubtypeLocaleUtils.getSubtypeLocale(subtype);
return SubtypeLocaleUtils.getSubtypeLocaleDisplayName(locale.getLanguage());
return SubtypeLocaleUtils.getSubtypeLanguageDisplayName(subtype.getLocale());
}
}

View File

@ -25,7 +25,7 @@ import android.os.Build;
import android.util.Log;
import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.latin.DictionaryFactory;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.R;
import java.util.Arrays;
@ -33,10 +33,10 @@ import java.util.HashMap;
import java.util.Locale;
public final class SubtypeLocaleUtils {
static final String TAG = SubtypeLocaleUtils.class.getSimpleName();
// This class must be located in the same package as LatinIME.java.
private static final String RESOURCE_PACKAGE_NAME =
DictionaryFactory.class.getPackage().getName();
private static final String TAG = SubtypeLocaleUtils.class.getSimpleName();
// This reference class {@link Constants} must be located in the same package as LatinIME.java.
private static final String RESOURCE_PACKAGE_NAME = Constants.class.getPackage().getName();
// Special language code to represent "no language".
public static final String NO_LANGUAGE = "zz";
@ -44,7 +44,8 @@ public final class SubtypeLocaleUtils {
public static final String EMOJI = "emoji";
public static final int UNKNOWN_KEYBOARD_LAYOUT = R.string.subtype_generic;
private static boolean sInitialized = false;
private static volatile boolean sInitialized = false;
private static final Object sInitializeLock = new Object();
private static Resources sResources;
private static String[] sPredefinedKeyboardLayoutSet;
// Keyboard layout to its display name map.
@ -77,9 +78,16 @@ public final class SubtypeLocaleUtils {
}
// Note that this initialization method can be called multiple times.
public static synchronized void init(final Context context) {
if (sInitialized) return;
public static void init(final Context context) {
synchronized (sInitializeLock) {
if (sInitialized == false) {
initLocked(context);
sInitialized = true;
}
}
}
private static void initLocked(final Context context) {
final Resources res = context.getResources();
sResources = res;
@ -122,8 +130,6 @@ public final class SubtypeLocaleUtils {
final String keyboardLayoutSet = keyboardLayoutSetMap[i + 1];
sLocaleAndExtraValueToKeyboardLayoutSetMap.put(key, keyboardLayoutSet);
}
sInitialized = true;
}
public static String[] getPredefinedKeyboardLayoutSet() {
@ -167,8 +173,18 @@ public final class SubtypeLocaleUtils {
return getSubtypeLocaleDisplayNameInternal(localeString, displayLocale);
}
public static String getSubtypeLanguageDisplayName(final String localeString) {
final Locale locale = LocaleUtils.constructLocaleFromString(localeString);
final Locale displayLocale = getDisplayLocaleOfSubtypeLocale(localeString);
return getSubtypeLocaleDisplayNameInternal(locale.getLanguage(), displayLocale);
}
private static String getSubtypeLocaleDisplayNameInternal(final String localeString,
final Locale displayLocale) {
if (NO_LANGUAGE.equals(localeString)) {
// No language subtype should be displayed in system locale.
return sResources.getString(R.string.subtype_no_language);
}
final Integer exceptionalNameResId = sExceptionalLocaleToNameIdsMap.get(localeString);
final String displayName;
if (exceptionalNameResId != null) {
@ -179,9 +195,6 @@ public final class SubtypeLocaleUtils {
}
};
displayName = getExceptionalName.runInLocale(sResources, displayLocale);
} else if (NO_LANGUAGE.equals(localeString)) {
// No language subtype should be displayed in system locale.
return sResources.getString(R.string.subtype_no_language);
} else {
final Locale locale = LocaleUtils.constructLocaleFromString(localeString);
displayName = locale.getDisplayName(displayLocale);