diff --git a/core/java/com/android/internal/app/SuggestedLocaleAdapter.java b/core/java/com/android/internal/app/SuggestedLocaleAdapter.java index cb2b0191b8831..46f47a31441c0 100644 --- a/core/java/com/android/internal/app/SuggestedLocaleAdapter.java +++ b/core/java/com/android/internal/app/SuggestedLocaleAdapter.java @@ -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 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