Merge "Support overriding display locale for SuggestedLocaleAdapter" into oc-dev

This commit is contained in:
TreeHugger Robot
2017-04-14 23:22:07 +00:00
committed by Android (Google) Code Review

View File

@@ -16,6 +16,10 @@
package com.android.internal.app;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.content.res.Configuration;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
@@ -57,6 +61,10 @@ public class SuggestedLocaleAdapter extends BaseAdapter implements Filterable {
private final boolean mCountryMode;
private LayoutInflater mInflater;
private Locale mDisplayLocale = null;
// used to potentially cache a modified Context that uses mDisplayLocale
private Context mContextOverride = null;
public SuggestedLocaleAdapter(Set<LocaleStore.LocaleInfo> localeOptions, boolean countryMode) {
mCountryMode = countryMode;
mLocaleOptions = new ArrayList<>(localeOptions.size());
@@ -126,6 +134,31 @@ public class SuggestedLocaleAdapter extends BaseAdapter implements Filterable {
return position;
}
/**
* Overrides the locale used to display localized labels. Setting the locale to null will reset
* the Adapter to use the default locale for the labels.
*/
public void setDisplayLocale(@NonNull Context context, @Nullable Locale locale) {
if (locale == null) {
mDisplayLocale = null;
mContextOverride = null;
} else if (!locale.equals(mDisplayLocale)) {
mDisplayLocale = locale;
final Configuration configOverride = new Configuration();
configOverride.setLocale(locale);
mContextOverride = context.createConfigurationContext(configOverride);
}
}
private void setTextTo(@NonNull TextView textView, int resId) {
if (mContextOverride == null) {
textView.setText(resId);
} else {
textView.setText(mContextOverride.getText(resId));
// If mContextOverride is not null, mDisplayLocale can't be null either.
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null && mInflater == null) {
@@ -143,15 +176,16 @@ public class SuggestedLocaleAdapter extends BaseAdapter implements Filterable {
}
TextView textView = (TextView) convertView;
if (itemType == TYPE_HEADER_SUGGESTED) {
textView.setText(R.string.language_picker_section_suggested);
setTextTo(textView, R.string.language_picker_section_suggested);
} else {
if (mCountryMode) {
textView.setText(R.string.region_picker_section_all);
setTextTo(textView, R.string.region_picker_section_all);
} else {
textView.setText(R.string.language_picker_section_all);
setTextTo(textView, R.string.language_picker_section_all);
}
}
textView.setTextLocale(Locale.getDefault());
textView.setTextLocale(
mDisplayLocale != null ? mDisplayLocale : Locale.getDefault());
break;
default:
// Covers both null, and "reusing" a wrong kind of view