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

Commit f54622e4 authored by Jean Chalard's avatar Jean Chalard
Browse files

Cut a helper method to increase encapsulation.

Change-Id: Ie47503a99d455f7266e281914e92ed680b993551
parent 821a4d8c
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ import android.view.inputmethod.InputMethodManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class InputMethodAndLanguageSettings extends SettingsPreferenceFragment
        implements Preference.OnPreferenceChangeListener, InputManager.InputDeviceListener {
@@ -190,7 +190,7 @@ public class InputMethodAndLanguageSettings extends SettingsPreferenceFragment

    private void updateUserDictionaryPreference(Preference userDictionaryPreference) {
        final Activity activity = getActivity();
        final Set<String> localeList = UserDictionaryList.getUserDictionaryLocalesList(activity);
        final TreeSet<String> localeList = UserDictionaryList.getUserDictionaryLocalesSet(activity);
        if (null == localeList) {
            // The locale list is null if and only if the user dictionary service is
            // not present or disabled. In this case we need to remove the preference.
@@ -205,9 +205,9 @@ public class InputMethodAndLanguageSettings extends SettingsPreferenceFragment
            // If the size of localeList is 0, we don't set the locale parameter in the
            // extras. This will be interpreted by the UserDictionarySettings class as
            // meaning "the current locale".
            // Note that with the current code for UserDictionaryList#getUserDictionaryLocalesList()
            // Note that with the current code for UserDictionaryList#getUserDictionaryLocalesSet()
            // the locale list always has at least one element, since it always includes the current
            // locale explicitly. @see UserDictionaryList.getUserDictionaryLocalesList().
            // locale explicitly. @see UserDictionaryList.getUserDictionaryLocalesSet().
            if (localeList.size() == 1) {
                final String locale = (String)localeList.toArray()[0];
                userDictionaryPreference.getExtras().putString("locale", locale);
+3 −52
Original line number Diff line number Diff line
@@ -19,11 +19,10 @@ package com.android.settings.inputmethod;
import com.android.settings.R;
import com.android.settings.UserDictionarySettings;
import com.android.settings.Utils;
import com.android.settings.inputmethod.UserDictionaryAddWordContents.LocaleRenderer;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import android.animation.LayoutTransition;
import android.app.Activity;
@@ -120,36 +119,6 @@ public class UserDictionaryAddWordActivity extends Activity
        finish();
    }

    private static class LocaleRenderer {
        private final String mLocaleString;
        private final String mDescription;
        // LocaleString may NOT be null.
        public LocaleRenderer(final Context context, final String localeString) {
            mLocaleString = localeString;
            if (null == localeString) {
                mDescription = context.getString(R.string.user_dict_settings_more_languages);
            } else if ("".equals(localeString)) {
                mDescription = context.getString(R.string.user_dict_settings_all_languages);
            } else {
                mDescription = Utils.createLocaleFromString(localeString).getDisplayName();
            }
        }
        @Override
        public String toString() {
            return mDescription;
        }
        public String getLocaleString() {
            return mLocaleString;
        }
    }

    private static void addLocaleDisplayNameToList(final Context context,
            final List<LocaleRenderer> list, final String locale) {
        if (null != locale) {
            list.add(new LocaleRenderer(context, locale));
        }
    }

    public void onClickMoreOptions(final View v) {
        for (final int idToShow : IDS_SHOWN_ONLY_IN_MORE_OPTIONS_MODE) {
            final View viewToShow = findViewById(idToShow);
@@ -160,26 +129,8 @@ public class UserDictionaryAddWordActivity extends Activity
        findViewById(R.id.user_dictionary_settings_add_dialog_less_options)
                .setVisibility(View.VISIBLE);

        final Set<String> locales = UserDictionaryList.getUserDictionaryLocalesList(this);
        // Remove our locale if it's in, because we're always gonna put it at the top
        locales.remove(mContents.mLocale); // mLocale may not be null
        final String systemLocale = Locale.getDefault().toString();
        // The system locale should be inside. We want it at the 2nd spot.
        locales.remove(systemLocale); // system locale may not be null
        locales.remove(""); // Remove the empty string if it's there
        final ArrayList<LocaleRenderer> localesList = new ArrayList<LocaleRenderer>();
        // Add the passed locale, then the system locale at the top of the list. Add an
        // "all languages" entry at the bottom of the list.
        addLocaleDisplayNameToList(this, localesList, mContents.mLocale);
        if (!systemLocale.equals(mContents.mLocale)) {
            addLocaleDisplayNameToList(this, localesList, systemLocale);
        }
        for (final String l : locales) {
            // TODO: sort in unicode order
            addLocaleDisplayNameToList(this, localesList, l);
        }
        localesList.add(new LocaleRenderer(this, "")); // meaning: all languages
        localesList.add(new LocaleRenderer(this, null)); // meaning: select another locale
        final ArrayList<LocaleRenderer> localesList = mContents.getLocalesList(this);

        final Spinner localeSpinner =
                (Spinner)findViewById(R.id.user_dictionary_settings_add_dialog_locale);
        final ArrayAdapter<LocaleRenderer> adapter = new ArrayAdapter<LocaleRenderer>(this,
+58 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settings.inputmethod;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
@@ -28,7 +29,9 @@ import com.android.settings.R;
import com.android.settings.UserDictionarySettings;
import com.android.settings.Utils;

import java.util.ArrayList;
import java.util.Locale;
import java.util.TreeSet;

/**
 * A container class to factor common code to UserDictionaryAddWordFragment
@@ -87,4 +90,59 @@ public class UserDictionaryAddWordContents {
                FREQUENCY_FOR_USER_DICTIONARY_ADDS, null /* shortcut */,
                TextUtils.isEmpty(mLocale) ? null : Utils.createLocaleFromString(mLocale));
    }

    public static class LocaleRenderer {
        private final String mLocaleString;
        private final String mDescription;
        // LocaleString may NOT be null.
        public LocaleRenderer(final Context context, final String localeString) {
            mLocaleString = localeString;
            if (null == localeString) {
                mDescription = context.getString(R.string.user_dict_settings_more_languages);
            } else if ("".equals(localeString)) {
                mDescription = context.getString(R.string.user_dict_settings_all_languages);
            } else {
                mDescription = Utils.createLocaleFromString(localeString).getDisplayName();
            }
        }
        @Override
        public String toString() {
            return mDescription;
        }
        public String getLocaleString() {
            return mLocaleString;
        }
    }

    private static void addLocaleDisplayNameToList(final Context context,
            final ArrayList<LocaleRenderer> list, final String locale) {
        if (null != locale) {
            list.add(new LocaleRenderer(context, locale));
        }
    }

    // Helper method to get the list of locales to display for this word
    public ArrayList<LocaleRenderer> getLocalesList(final Activity activity) {
        final TreeSet<String> locales = UserDictionaryList.getUserDictionaryLocalesSet(activity);
        // Remove our locale if it's in, because we're always gonna put it at the top
        locales.remove(mLocale); // mLocale may not be null
        final String systemLocale = Locale.getDefault().toString();
        // The system locale should be inside. We want it at the 2nd spot.
        locales.remove(systemLocale); // system locale may not be null
        locales.remove(""); // Remove the empty string if it's there
        final ArrayList<LocaleRenderer> localesList = new ArrayList<LocaleRenderer>();
        // Add the passed locale, then the system locale at the top of the list. Add an
        // "all languages" entry at the bottom of the list.
        addLocaleDisplayNameToList(activity, localesList, mLocale);
        if (!systemLocale.equals(mLocale)) {
            addLocaleDisplayNameToList(activity, localesList, systemLocale);
        }
        for (final String l : locales) {
            // TODO: sort in unicode order
            addLocaleDisplayNameToList(activity, localesList, l);
        }
        localesList.add(new LocaleRenderer(activity, "")); // meaning: all languages
        localesList.add(new LocaleRenderer(activity, null)); // meaning: select another locale
        return localesList;
    }
}
+4 −4
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ import android.preference.PreferenceGroup;
import android.provider.UserDictionary;

import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;

public class UserDictionaryList extends SettingsPreferenceFragment {
@@ -43,12 +42,12 @@ public class UserDictionaryList extends SettingsPreferenceFragment {
        setPreferenceScreen(getPreferenceManager().createPreferenceScreen(getActivity()));
    }

    static Set<String> getUserDictionaryLocalesList(Activity activity) {
    static TreeSet<String> getUserDictionaryLocalesSet(Activity activity) {
        @SuppressWarnings("deprecation")
        final Cursor cursor = activity.managedQuery(UserDictionary.Words.CONTENT_URI,
                new String[] { UserDictionary.Words.LOCALE },
                null, null, null);
        final Set<String> localeList = new TreeSet<String>();
        final TreeSet<String> localeList = new TreeSet<String>();
        if (null == cursor) {
            // The user dictionary service is not present or disabled. Return null.
            return null;
@@ -70,7 +69,8 @@ public class UserDictionaryList extends SettingsPreferenceFragment {
    protected void createUserDictSettings(PreferenceGroup userDictGroup) {
        final Activity activity = getActivity();
        userDictGroup.removeAll();
        final Set<String> localeList = UserDictionaryList.getUserDictionaryLocalesList(activity);
        final TreeSet<String> localeList =
                UserDictionaryList.getUserDictionaryLocalesSet(activity);

        if (localeList.isEmpty()) {
            userDictGroup.addPreference(createUserDictionaryPreference(null, activity));