Merge "Fix suggestions in the language selector" into nyc-dev

This commit is contained in:
Mihai Niță
2016-04-02 03:24:52 +00:00
committed by Android (Google) Code Review
3 changed files with 30 additions and 4 deletions

View File

@@ -219,7 +219,7 @@ public class LocaleHelper {
public int compare(LocaleStore.LocaleInfo lhs, LocaleStore.LocaleInfo rhs) {
// We don't care about the various suggestion types, just "suggested" (!= 0)
// and "all others" (== 0)
if (mCountryMode || (lhs.isSuggested() == rhs.isSuggested())) {
if (lhs.isSuggested() == rhs.isSuggested()) {
// They are in the same "bucket" (suggested / others), so we compare the text
return mCollator.compare(
removePrefixForCompare(lhs.getLocale(), lhs.getLabel(mCountryMode)),

View File

@@ -31,8 +31,9 @@ public class LocaleStore {
private static boolean sFullyInitialized = false;
public static class LocaleInfo {
private static final int SUGGESTION_TYPE_NONE = 0x00;
private static final int SUGGESTION_TYPE_SIM = 0x01;
private static final int SUGGESTION_TYPE_NONE = 0;
private static final int SUGGESTION_TYPE_SIM = 1 << 0;
private static final int SUGGESTION_TYPE_CFG = 1 << 1;
private final Locale mLocale;
private final Locale mParent;
@@ -273,6 +274,22 @@ public class LocaleStore {
final HashSet<String> localizedLocales = new HashSet<>();
for (String localeId : LocalePicker.getSystemAssetLocales()) {
LocaleInfo li = new LocaleInfo(localeId);
final String country = li.getLocale().getCountry();
// All this is to figure out if we should suggest a country
if (!country.isEmpty()) {
LocaleInfo cachedLocale = null;
if (sLocaleCache.containsKey(li.getId())) { // the simple case, e.g. fr-CH
cachedLocale = sLocaleCache.get(li.getId());
} else { // e.g. zh-TW localized, zh-Hant-TW in cache
final String langScriptCtry = li.getLangScriptKey() + "-" + country;
if (sLocaleCache.containsKey(langScriptCtry)) {
cachedLocale = sLocaleCache.get(langScriptCtry);
}
}
if (cachedLocale != null) {
cachedLocale.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_CFG;
}
}
localizedLocales.add(li.getLangScriptKey());
}

View File

@@ -49,6 +49,7 @@ public class SuggestedLocaleAdapter extends BaseAdapter implements Filterable {
private static final int TYPE_HEADER_SUGGESTED = 0;
private static final int TYPE_HEADER_ALL_OTHERS = 1;
private static final int TYPE_LOCALE = 2;
private static final int MIN_REGIONS_FOR_SUGGESTIONS = 6;
private ArrayList<LocaleStore.LocaleInfo> mLocaleOptions;
private ArrayList<LocaleStore.LocaleInfo> mOriginalLocaleOptions;
@@ -171,7 +172,15 @@ public class SuggestedLocaleAdapter extends BaseAdapter implements Filterable {
}
private boolean showHeaders() {
if (mCountryMode) { // never show suggestions in country mode
// We don't want to show suggestions for locales with very few regions
// (e.g. Romanian, with 2 regions)
// So we put a (somewhat) arbitrary limit.
//
// The initial idea was to make that limit dependent on the screen height.
// But that would mean rotating the screen could make the suggestions disappear,
// as the number of countries that fits on the screen would be different in portrait
// and landscape mode.
if (mCountryMode && mLocaleOptions.size() < MIN_REGIONS_FOR_SUGGESTIONS) {
return false;
}
return mSuggestionCount != 0 && mSuggestionCount != mLocaleOptions.size();