am 90b73b85: Merge "Get subtype name string resource from its own locale" into jb-dev

* commit '90b73b85e4d1846b84f97fb3b1a5f7897f89b985':
  Get subtype name string resource from its own locale
main
Tadashi G. Takaoka 2012-05-06 09:49:23 -07:00 committed by Android Git Automerger
commit 66c09113f6
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));
final RunInLocale<Void> tests = new RunInLocale<Void>() {
@Override
protected Void job(Resources res) {
assertEquals("en_US", "English (US)", assertEquals("en_US", "English (US)",
SubtypeLocale.getSubtypeDisplayName(EN_US, mRes)); SubtypeLocale.getSubtypeDisplayName(EN_US, res));
assertEquals("en_GB", "English (UK)", assertEquals("en_GB", "English (UK)",
SubtypeLocale.getSubtypeDisplayName(EN_GB, mRes)); SubtypeLocale.getSubtypeDisplayName(EN_GB, res));
assertEquals("fr ", "Français", assertEquals("fr ", "Français",
SubtypeLocale.getSubtypeDisplayName(FR, mRes)); SubtypeLocale.getSubtypeDisplayName(FR, res));
assertEquals("fr_CA", "Français (Canada)", assertEquals("fr_CA", "Français (Canada)",
SubtypeLocale.getSubtypeDisplayName(FR_CA, mRes)); SubtypeLocale.getSubtypeDisplayName(FR_CA, res));
assertEquals("de ", "Deutsch", assertEquals("de ", "Deutsch",
SubtypeLocale.getSubtypeDisplayName(DE, mRes)); SubtypeLocale.getSubtypeDisplayName(DE, res));
assertEquals("zz ", "No language (QWERTY)", assertEquals("zz ", "No language (QWERTY)",
SubtypeLocale.getSubtypeDisplayName(ZZ, mRes)); 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);
}
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)", 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;
} }