[Settings] Add search icon in region picker
Bug: 38526548 Flag: EXEMPT refactor Change-Id: I1016f5f812d8f0c43a2b3a899bd70b6672f44481
This commit is contained in:
@@ -19,33 +19,39 @@ package com.android.settings.localepicker;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Filter;
|
||||
import android.widget.SearchView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.android.internal.app.LocaleHelper;
|
||||
import com.android.internal.app.LocaleStore;
|
||||
import com.android.internal.app.SystemLocaleCollector;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settings.widget.PreferenceCategoryController;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import com.android.settingslib.widget.TopIntroPreference;
|
||||
|
||||
import com.google.android.material.appbar.AppBarLayout;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* A locale picker fragment to show region country and numbering system.
|
||||
@@ -54,7 +60,8 @@ import java.util.Set;
|
||||
* Allows the user to search for locales using both their native name and their name in the
|
||||
* default locale.</p>
|
||||
*/
|
||||
public class RegionAndNumberingSystemPickerFragment extends DashboardFragment {
|
||||
public class RegionAndNumberingSystemPickerFragment extends DashboardFragment implements
|
||||
SearchView.OnQueryTextListener, MenuItem.OnActionExpandListener {
|
||||
|
||||
public static final String EXTRA_TARGET_LOCALE = "extra_target_locale";
|
||||
public static final String EXTRA_IS_NUMBERING_SYSTEM = "extra_is_numbering_system";
|
||||
@@ -63,17 +70,30 @@ public class RegionAndNumberingSystemPickerFragment extends DashboardFragment {
|
||||
private static final String KEY_PREFERENCE_SYSTEM_LOCALE_LIST = "system_locale_list";
|
||||
private static final String KEY_PREFERENCE_SYSTEM_LOCALE_SUGGESTED_LIST =
|
||||
"system_locale_suggested_list";
|
||||
private static final String KEY_TOP_INTRO_PREFERENCE = "top_intro_region";
|
||||
private static final String EXTRA_EXPAND_SEARCH_VIEW = "expand_search_view";
|
||||
|
||||
@Nullable
|
||||
private SystemLocaleAllListPreferenceController mSystemLocaleAllListPreferenceController;
|
||||
private SearchView mSearchView = null;
|
||||
@Nullable
|
||||
private SearchFilter mSearchFilter = null;
|
||||
@SuppressWarnings("NullAway")
|
||||
private SystemLocaleAllListPreferenceController mSystemLocaleAllListPreferenceController;
|
||||
@SuppressWarnings("NullAway")
|
||||
private SystemLocaleSuggestedListPreferenceController mSuggestedListPreferenceController;
|
||||
@Nullable
|
||||
private LocaleStore.LocaleInfo mLocaleInfo;
|
||||
private RecyclerView mRecyclerView;
|
||||
@Nullable
|
||||
private List<LocaleStore.LocaleInfo> mLocaleOptions;
|
||||
@SuppressWarnings("NullAway")
|
||||
private List<LocaleStore.LocaleInfo> mOriginalLocaleInfos;
|
||||
private AppBarLayout mAppBarLayout;
|
||||
private RecyclerView mRecyclerView;
|
||||
private Activity mActivity;
|
||||
private boolean mExpandSearch;
|
||||
private boolean mIsNumberingMode;
|
||||
@Nullable
|
||||
private CharSequence mPrefix;
|
||||
|
||||
@Override
|
||||
public void onCreate(@NonNull Bundle icicle) {
|
||||
@@ -83,13 +103,27 @@ public class RegionAndNumberingSystemPickerFragment extends DashboardFragment {
|
||||
Log.d(TAG, "onCreate, no activity or activity is finishing");
|
||||
return;
|
||||
}
|
||||
setHasOptionsMenu(true);
|
||||
|
||||
if (mLocaleInfo == null) {
|
||||
Log.d(TAG, "onCreate, can not get localeInfo");
|
||||
return;
|
||||
mExpandSearch = mActivity.getIntent().getBooleanExtra(EXTRA_EXPAND_SEARCH_VIEW, false);
|
||||
if (icicle != null) {
|
||||
mExpandSearch = icicle.getBoolean(EXTRA_EXPAND_SEARCH_VIEW);
|
||||
}
|
||||
|
||||
mActivity.setTitle(mLocaleInfo.getFullNameNative());
|
||||
Log.d(TAG, "onCreate, mIsNumberingMode = " + mIsNumberingMode);
|
||||
if (!mIsNumberingMode) {
|
||||
mActivity.setTitle(R.string.region_selection_title);
|
||||
}
|
||||
|
||||
TopIntroPreference topIntroPreference = findPreference(KEY_TOP_INTRO_PREFERENCE);
|
||||
if (topIntroPreference != null) {
|
||||
topIntroPreference.setVisible(!mIsNumberingMode);
|
||||
}
|
||||
|
||||
if (mSystemLocaleAllListPreferenceController != null) {
|
||||
mOriginalLocaleInfos =
|
||||
mSystemLocaleAllListPreferenceController.getSupportedLocaleList();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -106,6 +140,151 @@ public class RegionAndNumberingSystemPickerFragment extends DashboardFragment {
|
||||
mRecyclerView = view.findViewById(R.id.recycler_view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(@NonNull Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
if (mSearchView != null) {
|
||||
outState.putBoolean(EXTRA_EXPAND_SEARCH_VIEW, !mSearchView.isIconified());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
|
||||
super.onCreateOptionsMenu(menu, inflater);
|
||||
inflater.inflate(R.menu.language_selection_list, menu);
|
||||
final MenuItem searchMenuItem = menu.findItem(R.id.locale_search_menu);
|
||||
if (searchMenuItem != null) {
|
||||
searchMenuItem.setOnActionExpandListener(this);
|
||||
mSearchView = (SearchView) searchMenuItem.getActionView();
|
||||
mSearchView.setQueryHint(
|
||||
getContext().getResources().getText(R.string.search_region_hint));
|
||||
mSearchView.setOnQueryTextListener(this);
|
||||
mSearchView.setMaxWidth(Integer.MAX_VALUE);
|
||||
if (mExpandSearch) {
|
||||
searchMenuItem.expandActionView();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void filterSearch(@Nullable String query) {
|
||||
if (mSearchFilter == null) {
|
||||
mSearchFilter = new SearchFilter();
|
||||
}
|
||||
|
||||
// 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");
|
||||
return;
|
||||
}
|
||||
mSearchFilter.filter(query);
|
||||
}
|
||||
|
||||
private class SearchFilter extends Filter {
|
||||
|
||||
@Override
|
||||
protected FilterResults performFiltering(CharSequence prefix) {
|
||||
FilterResults results = new FilterResults();
|
||||
mPrefix = prefix;
|
||||
if (TextUtils.isEmpty(prefix)) {
|
||||
results.values = mOriginalLocaleInfos;
|
||||
results.count = mOriginalLocaleInfos.size();
|
||||
} else {
|
||||
// TODO: decide if we should use the string's locale
|
||||
List<LocaleStore.LocaleInfo> newList = new ArrayList<>(mOriginalLocaleInfos);
|
||||
newList.addAll(mSystemLocaleAllListPreferenceController.getSuggestedLocaleList());
|
||||
Locale locale = Locale.getDefault();
|
||||
String prefixString = LocaleHelper.normalizeForSearch(prefix.toString(), locale);
|
||||
final int count = newList.size();
|
||||
final ArrayList<LocaleStore.LocaleInfo> newValues = new ArrayList<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
final LocaleStore.LocaleInfo value = newList.get(i);
|
||||
final String nameToCheck = LocaleHelper.normalizeForSearch(
|
||||
value.getFullNameInUiLanguage(), locale);
|
||||
final String nativeNameToCheck = LocaleHelper.normalizeForSearch(
|
||||
value.getFullNameNative(), locale);
|
||||
if ((wordMatches(nativeNameToCheck, prefixString)
|
||||
|| wordMatches(nameToCheck, prefixString)) && !newValues.contains(
|
||||
value)) {
|
||||
newValues.add(value);
|
||||
}
|
||||
}
|
||||
|
||||
results.values = newValues;
|
||||
results.count = newValues.size();
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
@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);
|
||||
mSuggestedListPreferenceController.onSearchListChanged(mLocaleOptions, mPrefix);
|
||||
}
|
||||
|
||||
// TODO: decide if this is enough, or we want to use a BreakIterator...
|
||||
private boolean wordMatches(String valueText, String prefixString) {
|
||||
if (valueText == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// First match against the whole, non-split value
|
||||
if (valueText.startsWith(prefixString)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// For example: English (Australia), Arabic (Egypt)
|
||||
Pattern pattern = Pattern.compile("^.*?\\((.*)");
|
||||
Matcher matcher = pattern.matcher(valueText);
|
||||
if (matcher.find()) {
|
||||
String region = matcher.group(1);
|
||||
return region.startsWith(prefixString);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onMenuItemActionExpand(@NonNull MenuItem item) {
|
||||
// To prevent a large space on tool bar.
|
||||
mAppBarLayout.setExpanded(false /*expanded*/, false /*animate*/);
|
||||
// To prevent user can expand the collapsing tool bar view.
|
||||
ViewCompat.setNestedScrollingEnabled(mRecyclerView, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onMenuItemActionCollapse(@NonNull MenuItem item) {
|
||||
// We keep the collapsed status after user cancel the search function.
|
||||
mAppBarLayout.setExpanded(false /*expanded*/, false /*animate*/);
|
||||
ViewCompat.setNestedScrollingEnabled(mRecyclerView, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onQueryTextSubmit(@Nullable String query) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onQueryTextChange(@Nullable String newText) {
|
||||
filterSearch(newText);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getLogTag() {
|
||||
return TAG;
|
||||
|
||||
Reference in New Issue
Block a user