Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3f01530a authored by Roozbeh Pournader's avatar Roozbeh Pournader
Browse files

Support overriding display locale for SuggestedLocaleAdapter

The method now accepts a locale for overriding the labels
"Suggested", "All regions", etc.

Fixes: 31257462
Test: Manual
Change-Id: I6e8c95d8d3e7b05a284b171f2cf2819d32e53d8d
parent 117ac766
Loading
Loading
Loading
Loading
+38 −4
Original line number Diff line number Diff line
@@ -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