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

Commit 10eaa9f1 authored by Zoey Chen's avatar Zoey Chen Committed by Android (Google) Code Review
Browse files

Merge "[Settings] 1. To show suggested preference, need to clear map before...

Merge "[Settings] 1. To show suggested preference, need to clear map before setup preference 2. Update the localeList and sort it for searching" into main
parents 88ba0ee8 f09bfd50
Loading
Loading
Loading
Loading
+11 −4
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;

import com.android.internal.app.AppLocaleCollector;
import com.android.internal.app.LocaleHelper;
import com.android.internal.app.LocaleStore;
import com.android.settings.R;
import com.android.settings.applications.manageapplications.ManageApplicationsUtil;
@@ -48,7 +49,9 @@ import com.android.settingslib.core.instrumentation.Instrumentable;
import com.android.settingslib.widget.SelectorWithWidgetPreference;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@@ -117,15 +120,13 @@ public class AppLocaleAllListPreferenceController extends
            return;
        }

        List<LocaleStore.LocaleInfo> result = LocaleUtils.getSortedLocaleList(
                getSupportedLocaleList(), mIsCountryMode);
        if (mIsCountryMode) {
            mPreferenceCategory.setTitle(
                    mContext.getString(R.string.all_supported_locales_regions_title));
        }
        final Map<String, Preference> existingSupportedPreferences = mSupportedPreferences;
        mSupportedPreferences = new ArrayMap<>();
        setupSupportedPreference(result, existingSupportedPreferences);
        setupSupportedPreference(getSupportedLocaleList(), existingSupportedPreferences);
        for (Preference pref : existingSupportedPreferences.values()) {
            mPreferenceCategory.removePreference(pref);
        }
@@ -134,11 +135,17 @@ public class AppLocaleAllListPreferenceController extends
    @Override
    public void onSearchListChanged(@NonNull List<LocaleStore.LocaleInfo> newList,
            @Nullable CharSequence prefix) {
        if (mPreferenceCategory == null) {
            Log.d(TAG, "onSearchListChanged, mPreferenceCategory is null");
            return;
        }

        mPreferenceCategory.removeAll();
        mSupportedPreferences.clear();
        final Map<String, Preference> existingSupportedPreferences = mSupportedPreferences;
        List<LocaleStore.LocaleInfo> sortedList = getSupportedLocaleList();
        newList = LocaleUtils.getSortedLocaleFromSearchList(newList, sortedList, mIsCountryMode);
        newList = LocaleUtils.getSortedLocaleFromSearchList(prefix, newList, sortedList,
                mIsCountryMode);
        setupSupportedPreference(newList, existingSupportedPreferences);
    }

+14 −4
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * A locale picker fragment to show app languages.
@@ -110,6 +111,7 @@ public class AppLocalePickerFragment extends DashboardFragment implements
    private ApplicationInfo mApplicationInfo;
    private boolean mIsNumberingMode;
    private CharSequence mPreviousSearch = null;
    @Nullable private CharSequence mPrefix;

    @Override
    public void onCreate(@NonNull Bundle icicle) {
@@ -271,7 +273,13 @@ public class AppLocalePickerFragment extends DashboardFragment implements
            mSearchFilter = new SearchFilter();
        }

        if (mSuggestedListPreferenceController != null
                && mAppLocaleAllListPreferenceController != null) {
            mOriginalLocaleInfos = mAppLocaleAllListPreferenceController.getSupportedLocaleList();
            mOriginalLocaleInfos.addAll(
                    mSuggestedListPreferenceController.getSuggestedLocaleList().stream().collect(
                            Collectors.toList()));
        }
        // If we haven't load apps list completely, don't filter anything.
        if (mOriginalLocaleInfos == null) {
            Log.w(TAG, "Locales haven't loaded completely yet, so nothing can be filtered");
@@ -285,7 +293,7 @@ public class AppLocalePickerFragment extends DashboardFragment implements
        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();

            mPrefix = prefix;
            if (mOriginalLocaleInfos == null) {
                mOriginalLocaleInfos = new ArrayList<>(mLocaleOptions);
            }
@@ -330,13 +338,15 @@ public class AppLocalePickerFragment extends DashboardFragment implements
            }

            mLocaleOptions = (ArrayList<LocaleStore.LocaleInfo>) results.values;
            List<LocaleStore.LocaleInfo> list = new ArrayList<>();
            list.addAll(mLocaleOptions);
            // Need to scroll to first preference when searching.
            if (mRecyclerView != null) {
                mRecyclerView.post(() -> mRecyclerView.scrollToPosition(0));
            }

            mAppLocaleAllListPreferenceController.onSearchListChanged(mLocaleOptions, null);
            mSuggestedListPreferenceController.onSearchListChanged(mLocaleOptions, null);
            mSuggestedListPreferenceController.onSearchListChanged(list, mPrefix);
            mAppLocaleAllListPreferenceController.onSearchListChanged(list, mPrefix);
        }

        // TODO: decide if this is enough, or we want to use a BreakIterator...
+11 −4
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;

import com.android.internal.app.AppLocaleCollector;
import com.android.internal.app.LocaleHelper;
import com.android.internal.app.LocaleStore;
import com.android.settings.R;
import com.android.settings.applications.manageapplications.ManageApplicationsUtil;
@@ -40,7 +41,9 @@ import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.widget.SelectorWithWidgetPreference;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@@ -108,11 +111,9 @@ public class AppLocaleSuggestedListPreferenceController extends
            return;
        }

        List<LocaleStore.LocaleInfo> result = LocaleUtils.getSortedLocaleList(
                getSuggestedLocaleList(), mIsCountryMode);
        final Map<String, Preference> existingSuggestedPreferences = mSuggestedPreferences;
        mSuggestedPreferences = new ArrayMap<>();
        setupSuggestedPreference(result, existingSuggestedPreferences);
        setupSuggestedPreference(getSuggestedLocaleList(), existingSuggestedPreferences);
        for (Preference pref : existingSuggestedPreferences.values()) {
            mPreferenceCategory.removePreference(pref);
        }
@@ -127,9 +128,11 @@ public class AppLocaleSuggestedListPreferenceController extends
        }

        mPreferenceCategory.removeAll();
        mSuggestedPreferences.clear();
        final Map<String, Preference> existingSuggestedPreferences = mSuggestedPreferences;
        List<LocaleStore.LocaleInfo> sortedList = getSuggestedLocaleList();
        newList = LocaleUtils.getSortedLocaleFromSearchList(newList, sortedList, mIsCountryMode);
        newList = LocaleUtils.getSortedLocaleFromSearchList(prefix, newList, sortedList,
                mIsCountryMode);
        setupSuggestedPreference(newList, existingSuggestedPreferences);
    }

@@ -192,6 +195,10 @@ public class AppLocaleSuggestedListPreferenceController extends
        } else {
            Log.d(TAG, "Can not get suggested locales because the locale list is null or empty.");
        }
        final Locale sortingLocale = Locale.getDefault();
        final LocaleHelper.LocaleInfoComparator comp = new LocaleHelper.LocaleInfoComparator(
                sortingLocale, mIsCountryMode);
        Collections.sort(mLocaleOptions, comp);
        return mLocaleOptions;
    }

+15 −9
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.os.SystemClock;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.android.internal.app.LocaleHelper;
@@ -244,7 +245,7 @@ public class LocaleUtils {
     * @param isCountryMode Whether the locale page is in country mode or not.
     * @return localeInfos list of locale Infos
     */
    public static @NonNull List<LocaleStore.LocaleInfo> getSortedLocaleList(
    private static @NonNull List<LocaleStore.LocaleInfo> getSortedLocaleList(
            @NonNull List<LocaleStore.LocaleInfo> localeInfos, boolean isCountryMode) {
        final Locale sortingLocale = Locale.getDefault();
        final LocaleHelper.LocaleInfoComparator comp = new LocaleHelper.LocaleInfoComparator(
@@ -262,16 +263,21 @@ public class LocaleUtils {
     * @return localeInfos list of locale Infos
     */
    public static @NonNull List<LocaleStore.LocaleInfo>  getSortedLocaleFromSearchList(
            @Nullable CharSequence prefix,
            @NonNull List<LocaleStore.LocaleInfo> searchList,
            @NonNull List<LocaleStore.LocaleInfo> localeList,
            boolean isCountryMode) {
        List<LocaleStore.LocaleInfo> searchItem = localeList.stream()
                .filter(suggested -> searchList.stream()
                        .anyMatch(option -> option.getLocale() != null
                                && option.getLocale().getLanguage().equals(
                                suggested.getLocale().getLanguage())))
                .distinct()
                .collect(Collectors.toList());

        List<LocaleStore.LocaleInfo> searchItem = new ArrayList<>();
        if (prefix == null || prefix.isEmpty()) {
            return getSortedLocaleList(localeList, isCountryMode);
        }

        for (LocaleStore.LocaleInfo option : searchList) {
            if (localeList.contains(option)) {
                searchItem.add(option);
            }
        }
        return getSortedLocaleList(searchItem, isCountryMode);
    }
}
+17 −14
Original line number Diff line number Diff line
@@ -133,10 +133,9 @@ public class RegionAndNumberingSystemPickerFragment extends DashboardFragment im
            topIntroPreference.setTitle(R.string.top_intro_numbering_system_title);
        }

        if (mSystemLocaleAllListPreferenceController != null) {
            mOriginalLocaleInfos =
                    mSystemLocaleAllListPreferenceController.getSupportedLocaleList();
        }
        mOriginalLocaleInfos = mSystemLocaleAllListPreferenceController != null
                ? mSystemLocaleAllListPreferenceController.getSupportedLocaleList()
                : mAppLocaleAllListPreferenceController.getSupportedLocaleList();
    }

    @Override
@@ -204,7 +203,10 @@ public class RegionAndNumberingSystemPickerFragment extends DashboardFragment im
            } else {
                // TODO: decide if we should use the string's locale
                List<LocaleStore.LocaleInfo> newList = new ArrayList<>(mOriginalLocaleInfos);
                newList.addAll(mSystemLocaleAllListPreferenceController.getSuggestedLocaleList());
                List<LocaleStore.LocaleInfo> suggestedList = TextUtils.isEmpty(mPackageName)
                        ? mSystemLocaleAllListPreferenceController.getSuggestedLocaleList()
                        : mAppLocaleSuggestedListPreferenceController.getSuggestedLocaleList();
                newList.addAll(suggestedList);
                Locale locale = Locale.getDefault();
                String prefixString = LocaleHelper.normalizeForSearch(prefix.toString(), locale);
                final int count = newList.size();
@@ -231,20 +233,21 @@ public class RegionAndNumberingSystemPickerFragment extends DashboardFragment im

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (mSystemLocaleAllListPreferenceController == null
                    || mSuggestedListPreferenceController == null) {
                Log.d(TAG, "publishResults(), can not get preference.");
                return;
            }

            mLocaleOptions = (ArrayList<LocaleStore.LocaleInfo>) results.values;
            // TODO: Need to scroll to first preference when searching.
            if (mRecyclerView != null) {
                mRecyclerView.post(() -> mRecyclerView.scrollToPosition(0));
            }

            mSystemLocaleAllListPreferenceController.onSearchListChanged(mLocaleOptions, mPrefix);
            if (TextUtils.isEmpty(mPackageName)) {
                mSystemLocaleAllListPreferenceController.onSearchListChanged(mLocaleOptions,
                        mPrefix);
                mSuggestedListPreferenceController.onSearchListChanged(mLocaleOptions, mPrefix);
            } else {
                mAppLocaleSuggestedListPreferenceController.onSearchListChanged(mLocaleOptions,
                        mPrefix);
                mAppLocaleAllListPreferenceController.onSearchListChanged(mLocaleOptions, mPrefix);
            }
        }

        // TODO: decide if this is enough, or we want to use a BreakIterator...
Loading