Merge "Show locale in region list even if suggested in language list" into nyc-dev

am: ee172e7bcb

* commit 'ee172e7bcb0afbcd236b822e0ce798844b5b8471':
  Show locale in region list even if suggested in language list
This commit is contained in:
Mihai Nita
2016-03-03 22:06:26 +00:00
committed by android-build-merger
4 changed files with 26 additions and 38 deletions

View File

@@ -180,14 +180,16 @@ public class LocaleHelper {
*/
public static final class LocaleInfoComparator implements Comparator<LocaleStore.LocaleInfo> {
private final Collator mCollator;
private final boolean mCountryMode;
/**
* Constructor.
*
* @param sortLocale the locale to be used for sorting.
*/
public LocaleInfoComparator(Locale sortLocale) {
public LocaleInfoComparator(Locale sortLocale, boolean countryMode) {
mCollator = Collator.getInstance(sortLocale);
mCountryMode = countryMode;
}
/**
@@ -202,9 +204,9 @@ 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 (lhs.isSuggested() == rhs.isSuggested()) {
if (mCountryMode || (lhs.isSuggested() == rhs.isSuggested())) {
// They are in the same "bucket" (suggested / others), so we compare the text
return mCollator.compare(lhs.getLabel(), rhs.getLabel());
return mCollator.compare(lhs.getLabel(mCountryMode), rhs.getLabel(mCountryMode));
} else {
// One locale is suggested and one is not, so we put them in different "buckets"
return lhs.isSuggested() ? -1 : 1;

View File

@@ -50,7 +50,6 @@ public class LocalePickerWithRegion extends ListFragment implements SearchView.O
private Set<LocaleStore.LocaleInfo> mLocaleList;
private LocaleStore.LocaleInfo mParentLocale;
private boolean mTranslatedOnly = false;
private boolean mCountryMode = false;
/**
* Other classes can register to be notified when a locale was selected.
@@ -70,15 +69,14 @@ public class LocalePickerWithRegion extends ListFragment implements SearchView.O
boolean translatedOnly) {
LocalePickerWithRegion localePicker = new LocalePickerWithRegion();
boolean shouldShowTheList = localePicker.setListener(context, listener, parent,
true /* country mode */, translatedOnly);
translatedOnly);
return shouldShowTheList ? localePicker : null;
}
public static LocalePickerWithRegion createLanguagePicker(Context context,
LocaleSelectedListener listener, boolean translatedOnly) {
LocalePickerWithRegion localePicker = new LocalePickerWithRegion();
localePicker.setListener(context, listener, null,
false /* language mode */, translatedOnly);
localePicker.setListener(context, listener, /* parent */ null, translatedOnly);
return localePicker;
}
@@ -96,14 +94,7 @@ public class LocalePickerWithRegion extends ListFragment implements SearchView.O
* "pretending" it was selected, and return false.</p>
*/
private boolean setListener(Context context, LocaleSelectedListener listener,
LocaleStore.LocaleInfo parent, boolean countryMode, boolean translatedOnly) {
if (countryMode && (parent == null || parent.getLocale() == null)) {
// The list of countries is determined as all the countries where the parent language
// is used.
throw new IllegalArgumentException("The country selection list needs a parent.");
}
this.mCountryMode = countryMode;
LocaleStore.LocaleInfo parent, boolean translatedOnly) {
this.mParentLocale = parent;
this.mListener = listener;
this.mTranslatedOnly = translatedOnly;
@@ -116,7 +107,7 @@ public class LocalePickerWithRegion extends ListFragment implements SearchView.O
Collections.addAll(langTagsToIgnore, langTags);
}
if (countryMode) {
if (parent != null) {
mLocaleList = LocaleStore.getLevelLocales(context,
langTagsToIgnore, parent, translatedOnly);
if (mLocaleList.size() <= 1) {
@@ -138,13 +129,11 @@ public class LocalePickerWithRegion extends ListFragment implements SearchView.O
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
final Locale sortingLocale = (mCountryMode && mParentLocale != null)
? mParentLocale.getLocale()
: Locale.getDefault();
mAdapter = new SuggestedLocaleAdapter(mLocaleList, mCountryMode);
final boolean countryMode = mParentLocale != null;
final Locale sortingLocale = countryMode ? mParentLocale.getLocale() : Locale.getDefault();
mAdapter = new SuggestedLocaleAdapter(mLocaleList, countryMode);
final LocaleHelper.LocaleInfoComparator comp =
new LocaleHelper.LocaleInfoComparator(sortingLocale);
new LocaleHelper.LocaleInfoComparator(sortingLocale, countryMode);
mAdapter.sort(comp);
setListAdapter(mAdapter);
}
@@ -164,12 +153,8 @@ public class LocalePickerWithRegion extends ListFragment implements SearchView.O
public void onResume() {
super.onResume();
if (mCountryMode) {
if (mParentLocale == null) {
this.getActivity().setTitle(R.string.country_selection_title);
} else {
this.getActivity().setTitle(mParentLocale.getFullNameNative());
}
if (mParentLocale != null) {
this.getActivity().setTitle(mParentLocale.getFullNameNative());
} else {
this.getActivity().setTitle(R.string.language_selection_title);
}
@@ -182,7 +167,7 @@ public class LocalePickerWithRegion extends ListFragment implements SearchView.O
final LocaleStore.LocaleInfo locale =
(LocaleStore.LocaleInfo) getListAdapter().getItem(position);
if (mCountryMode || locale.getParent() != null) {
if (locale.getParent() != null) {
if (mListener != null) {
mListener.onLocaleSelected(locale);
}
@@ -205,7 +190,7 @@ public class LocalePickerWithRegion extends ListFragment implements SearchView.O
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (!mCountryMode) {
if (mParentLocale == null) {
inflater.inflate(R.menu.language_selection_list, menu);
MenuItem mSearchMenuItem = menu.findItem(R.id.locale_search_menu);

View File

@@ -145,11 +145,11 @@ public class LocaleStore {
return mLangScriptKey;
}
String getLabel() {
if (getParent() == null || this.isSuggestionOfType(SUGGESTION_TYPE_SIM)) {
return getFullNameNative();
} else {
String getLabel(boolean countryMode) {
if (countryMode) {
return getFullCountryNameNative();
} else {
return getFullNameNative();
}
}
@@ -311,9 +311,7 @@ public class LocaleStore {
if (level == 2) {
if (parent != null) { // region selection
if (parentId.equals(li.getParent().toLanguageTag())) {
if (!li.isSuggestionOfType(LocaleInfo.SUGGESTION_TYPE_SIM)) {
result.add(li);
}
result.add(li);
}
} else { // language selection
if (li.isSuggestionOfType(LocaleInfo.SUGGESTION_TYPE_SIM)) {

View File

@@ -156,7 +156,7 @@ public class SuggestedLocaleAdapter extends BaseAdapter implements Filterable {
TextView text = (TextView) convertView.findViewById(R.id.locale);
LocaleStore.LocaleInfo item = (LocaleStore.LocaleInfo) getItem(position);
text.setText(item.getLabel());
text.setText(item.getLabel(mCountryMode));
text.setTextLocale(item.getLocale());
if (mCountryMode) {
int layoutDir = TextUtils.getLayoutDirectionFromLocale(item.getParent());
@@ -171,6 +171,9 @@ public class SuggestedLocaleAdapter extends BaseAdapter implements Filterable {
}
private boolean showHeaders() {
if (mCountryMode) { // never show suggestions in country mode
return false;
}
return mSuggestionCount != 0 && mSuggestionCount != mLocaleOptions.size();
}