Get subtype name string resource from its own locale

Bug: 6443655
Change-Id: Ib9ee55178f0d0f885aa0640b9cba4a179cf0c566
main
Tadashi G. Takaoka 2012-05-05 16:14:04 +09:00
parent 03b423f313
commit 757c12556b
3 changed files with 114 additions and 43 deletions

View File

@ -22,6 +22,8 @@ import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.view.inputmethod.InputMethodSubtype; import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.latin.LocaleUtils.RunInLocale;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
@ -121,18 +123,27 @@ public class SubtypeLocale {
// fr azerty F Français // fr azerty F Français
// fr_CA qwerty F Français (Canada) // fr_CA qwerty F Français (Canada)
// de qwertz F Deutsch // de qwertz F Deutsch
// zz qwerty F No language (QWERTY) // zz qwerty F No language (QWERTY) in system locale
// fr qwertz T Français (QWERTZ) // fr qwertz T Français (QWERTZ)
// de qwerty T Deutsch (QWERTY) // de qwerty T Deutsch (QWERTY)
// en_US azerty T English (US) (AZERTY) // en_US azerty T English (US) (AZERTY)
// zz azerty T No language (AZERTY) // zz azerty T No language (AZERTY) in system locale
public static String getSubtypeDisplayName(InputMethodSubtype subtype, Resources res) { public static String getSubtypeDisplayName(InputMethodSubtype subtype, Resources res) {
// TODO: Remove this check when InputMethodManager.getLastInputMethodSubtype is // TODO: Remove this check when InputMethodManager.getLastInputMethodSubtype is
// fixed. // fixed.
if (!ImfUtils.checkIfSubtypeBelongsToThisIme(sContext, subtype)) return ""; if (!ImfUtils.checkIfSubtypeBelongsToThisIme(sContext, subtype)) return "";
final String language = getSubtypeLocaleDisplayName(subtype.getLocale()); final String language = getSubtypeLocaleDisplayName(subtype.getLocale());
return res.getString(subtype.getNameResId(), language); final int nameResId = subtype.getNameResId();
final RunInLocale<String> getSubtypeName = new RunInLocale<String>() {
@Override
protected String job(Resources res) {
return res.getString(nameResId, language);
}
};
final Locale locale = isNoLanguage(subtype)
? res.getConfiguration().locale : getSubtypeLocale(subtype);
return getSubtypeName.runInLocale(res, locale);
} }
public static boolean isNoLanguage(InputMethodSubtype subtype) { public static boolean isNoLanguage(InputMethodSubtype subtype) {

View File

@ -21,7 +21,6 @@ import android.content.res.Resources;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
import android.view.inputmethod.InputMethodSubtype; import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import com.android.inputmethod.latin.AdditionalSubtype; import com.android.inputmethod.latin.AdditionalSubtype;
import com.android.inputmethod.latin.ImfUtils; import com.android.inputmethod.latin.ImfUtils;
import com.android.inputmethod.latin.StringUtils; import com.android.inputmethod.latin.StringUtils;
@ -41,7 +40,6 @@ public class SpacebarTextTests extends AndroidTestCase {
super.setUp(); super.setUp();
final Context context = getContext(); final Context context = getContext();
mRes = context.getResources(); mRes = context.getResources();
InputMethodManagerCompatWrapper.init(context);
SubtypeLocale.init(context); SubtypeLocale.init(context);
} }

View File

@ -21,7 +21,6 @@ import android.content.res.Resources;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
import android.view.inputmethod.InputMethodSubtype; import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import com.android.inputmethod.latin.LocaleUtils.RunInLocale; import com.android.inputmethod.latin.LocaleUtils.RunInLocale;
import java.util.ArrayList; import java.util.ArrayList;
@ -31,14 +30,15 @@ public class SubtypeLocaleTests extends AndroidTestCase {
// Locale to subtypes list. // Locale to subtypes list.
private final ArrayList<InputMethodSubtype> mSubtypesList = new ArrayList<InputMethodSubtype>(); private final ArrayList<InputMethodSubtype> mSubtypesList = new ArrayList<InputMethodSubtype>();
private Context mContext;
private Resources mRes; private Resources mRes;
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
final Context context = getContext(); final Context context = getContext();
mContext = context;
mRes = context.getResources(); mRes = context.getResources();
InputMethodManagerCompatWrapper.init(context);
SubtypeLocale.init(context); SubtypeLocale.init(context);
} }
@ -65,13 +65,13 @@ public class SubtypeLocaleTests extends AndroidTestCase {
// fr azerty F Français // fr azerty F Français
// fr_CA qwerty F Français (Canada) // fr_CA qwerty F Français (Canada)
// de qwertz F Deutsch // de qwertz F Deutsch
// zz qwerty F No language (QWERTY) // zz qwerty F No language (QWERTY) in system locale
// fr qwertz T Français (QWERTZ) // fr qwertz T Français (QWERTZ)
// de qwerty T Deutsch (QWERTY) // de qwerty T Deutsch (QWERTY)
// en_US azerty T English (US) (AZERTY) // en_US azerty T English (US) (AZERTY)
// zz azerty T No language (AZERTY) // zz azerty T No language (AZERTY) in system locale
public void testPredefinedSubtypes() { public void testPredefinedSubtypesInEnglish() {
final Context context = getContext(); final Context context = getContext();
final InputMethodSubtype EN_US = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet( final InputMethodSubtype EN_US = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
context, Locale.US.toString(), "qwerty"); context, Locale.US.toString(), "qwerty");
@ -93,21 +93,28 @@ public class SubtypeLocaleTests extends AndroidTestCase {
assertEquals("de ", "qwertz", SubtypeLocale.getKeyboardLayoutSetName(DE)); assertEquals("de ", "qwertz", SubtypeLocale.getKeyboardLayoutSetName(DE));
assertEquals("zz ", "qwerty", SubtypeLocale.getKeyboardLayoutSetName(ZZ)); assertEquals("zz ", "qwerty", SubtypeLocale.getKeyboardLayoutSetName(ZZ));
assertEquals("en_US", "English (US)", final RunInLocale<Void> tests = new RunInLocale<Void>() {
SubtypeLocale.getSubtypeDisplayName(EN_US, mRes)); @Override
assertEquals("en_GB", "English (UK)", protected Void job(Resources res) {
SubtypeLocale.getSubtypeDisplayName(EN_GB, mRes)); assertEquals("en_US", "English (US)",
assertEquals("fr ", "Français", SubtypeLocale.getSubtypeDisplayName(EN_US, res));
SubtypeLocale.getSubtypeDisplayName(FR, mRes)); assertEquals("en_GB", "English (UK)",
assertEquals("fr_CA", "Français (Canada)", SubtypeLocale.getSubtypeDisplayName(EN_GB, res));
SubtypeLocale.getSubtypeDisplayName(FR_CA, mRes)); assertEquals("fr ", "Français",
assertEquals("de ", "Deutsch", SubtypeLocale.getSubtypeDisplayName(FR, res));
SubtypeLocale.getSubtypeDisplayName(DE, mRes)); assertEquals("fr_CA", "Français (Canada)",
assertEquals("zz ", "No language (QWERTY)", SubtypeLocale.getSubtypeDisplayName(FR_CA, res));
SubtypeLocale.getSubtypeDisplayName(ZZ, mRes)); assertEquals("de ", "Deutsch",
SubtypeLocale.getSubtypeDisplayName(DE, res));
assertEquals("zz ", "No language (QWERTY)",
SubtypeLocale.getSubtypeDisplayName(ZZ, res));
return null;
}
};
tests.runInLocale(mRes, Locale.ENGLISH);
} }
public void testAdditionalSubtype() { public void testAdditionalSubtypesInEnglish() {
final InputMethodSubtype DE_QWERTY = AdditionalSubtype.createAdditionalSubtype( final InputMethodSubtype DE_QWERTY = AdditionalSubtype.createAdditionalSubtype(
Locale.GERMAN.toString(), "qwerty", null); Locale.GERMAN.toString(), "qwerty", null);
final InputMethodSubtype FR_QWERTZ = AdditionalSubtype.createAdditionalSubtype( final InputMethodSubtype FR_QWERTZ = AdditionalSubtype.createAdditionalSubtype(
@ -117,32 +124,87 @@ public class SubtypeLocaleTests extends AndroidTestCase {
final InputMethodSubtype ZZ_AZERTY = AdditionalSubtype.createAdditionalSubtype( final InputMethodSubtype ZZ_AZERTY = AdditionalSubtype.createAdditionalSubtype(
SubtypeLocale.NO_LANGUAGE, "azerty", null); SubtypeLocale.NO_LANGUAGE, "azerty", null);
assertEquals("fr qwertz", "Français (QWERTZ)", ImfUtils.setAdditionalInputMethodSubtypes(mContext, new InputMethodSubtype[] {
SubtypeLocale.getSubtypeDisplayName(FR_QWERTZ, mRes)); DE_QWERTY, FR_QWERTZ, US_AZERTY, ZZ_AZERTY
assertEquals("de qwerty", "Deutsch (QWERTY)", });
SubtypeLocale.getSubtypeDisplayName(DE_QWERTY, mRes));
assertEquals("en_US azerty", "English (US) (AZERTY)",
SubtypeLocale.getSubtypeDisplayName(US_AZERTY, mRes));
assertEquals("zz azerty", "No language (AZERTY)",
SubtypeLocale.getSubtypeDisplayName(ZZ_AZERTY, mRes));
}
public void testNoLanguageInFrench() {
final Context context = getContext();
final InputMethodSubtype ZZ = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
context, SubtypeLocale.NO_LANGUAGE, "qwerty");
final InputMethodSubtype ZZ_AZERTY = AdditionalSubtype.createAdditionalSubtype(
SubtypeLocale.NO_LANGUAGE, "azerty", null);
final RunInLocale<Void> tests = new RunInLocale<Void>() { final RunInLocale<Void> tests = new RunInLocale<Void>() {
@Override @Override
protected Void job(Resources res) { protected Void job(Resources res) {
assertEquals("zz ", "qwerty", SubtypeLocale.getKeyboardLayoutSetName(ZZ)); assertEquals("fr qwertz", "Français (QWERTZ)",
assertEquals("zz ", "azerty", SubtypeLocale.getKeyboardLayoutSetName(ZZ_AZERTY)); SubtypeLocale.getSubtypeDisplayName(FR_QWERTZ, res));
assertEquals("de qwerty", "Deutsch (QWERTY)",
SubtypeLocale.getSubtypeDisplayName(DE_QWERTY, res));
assertEquals("en_US azerty", "English (US) (AZERTY)",
SubtypeLocale.getSubtypeDisplayName(US_AZERTY, res));
assertEquals("zz azerty", "No language (AZERTY)",
SubtypeLocale.getSubtypeDisplayName(ZZ_AZERTY, res));
return null;
}
};
tests.runInLocale(mRes, Locale.ENGLISH);
}
assertEquals("zz ", "Pas de langue (QWERTY)", public void testPredefinedSubtypesInFrench() {
final Context context = getContext();
final InputMethodSubtype EN_US = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
context, Locale.US.toString(), "qwerty");
final InputMethodSubtype EN_GB = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
context, Locale.UK.toString(), "qwerty");
final InputMethodSubtype FR = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
context, Locale.FRENCH.toString(), "azerty");
final InputMethodSubtype FR_CA = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
context, Locale.CANADA_FRENCH.toString(), "qwerty");
final InputMethodSubtype DE = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
context, Locale.GERMAN.toString(), "qwertz");
final InputMethodSubtype ZZ = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
context, SubtypeLocale.NO_LANGUAGE, "qwerty");
final RunInLocale<Void> tests = new RunInLocale<Void>() {
@Override
protected Void job(Resources res) {
assertEquals("en_US", "English (US)",
SubtypeLocale.getSubtypeDisplayName(EN_US, res));
assertEquals("en_GB", "English (UK)",
SubtypeLocale.getSubtypeDisplayName(EN_GB, res));
assertEquals("fr ", "Français",
SubtypeLocale.getSubtypeDisplayName(FR, res));
assertEquals("fr_CA", "Français (Canada)",
SubtypeLocale.getSubtypeDisplayName(FR_CA, res));
assertEquals("de ", "Deutsch",
SubtypeLocale.getSubtypeDisplayName(DE, res));
assertEquals("zz ", "Pas de langue (QWERTY)",
SubtypeLocale.getSubtypeDisplayName(ZZ, res)); SubtypeLocale.getSubtypeDisplayName(ZZ, res));
assertEquals("zz azerty", "Pas de langue (AZERTY)", return null;
}
};
tests.runInLocale(mRes, Locale.FRENCH);
}
public void testAdditionalSubtypesInFrench() {
final InputMethodSubtype DE_QWERTY = AdditionalSubtype.createAdditionalSubtype(
Locale.GERMAN.toString(), "qwerty", null);
final InputMethodSubtype FR_QWERTZ = AdditionalSubtype.createAdditionalSubtype(
Locale.FRENCH.toString(), "qwertz", null);
final InputMethodSubtype US_AZERTY = AdditionalSubtype.createAdditionalSubtype(
Locale.US.toString(), "azerty", null);
final InputMethodSubtype ZZ_AZERTY = AdditionalSubtype.createAdditionalSubtype(
SubtypeLocale.NO_LANGUAGE, "azerty", null);
ImfUtils.setAdditionalInputMethodSubtypes(mContext, new InputMethodSubtype[] {
DE_QWERTY, FR_QWERTZ, US_AZERTY, ZZ_AZERTY
});
final RunInLocale<Void> tests = new RunInLocale<Void>() {
@Override
protected Void job(Resources res) {
assertEquals("fr qwertz", "Français (QWERTZ)",
SubtypeLocale.getSubtypeDisplayName(FR_QWERTZ, res));
assertEquals("de qwerty", "Deutsch (QWERTY)",
SubtypeLocale.getSubtypeDisplayName(DE_QWERTY, res));
assertEquals("en_US azerty", "English (US) (AZERTY)",
SubtypeLocale.getSubtypeDisplayName(US_AZERTY, res));
assertEquals("zz azerty", "Aucune langue (AZERTY)",
SubtypeLocale.getSubtypeDisplayName(ZZ_AZERTY, res)); SubtypeLocale.getSubtypeDisplayName(ZZ_AZERTY, res));
return null; return null;
} }