NekoX/TMessagesProj/src/main/java/org/telegram/ui/CountrySelectActivity.java

513 lines
20 KiB
Java
Raw Normal View History

2013-12-20 20:25:49 +01:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2013-12-20 20:25:49 +01:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
2013-12-20 20:25:49 +01:00
*/
package org.telegram.ui;
2015-04-09 20:00:14 +02:00
import android.content.Context;
2013-12-20 20:25:49 +01:00
import android.view.View;
2017-03-31 01:58:05 +02:00
import android.view.ViewGroup;
import android.widget.EditText;
2014-11-13 21:10:14 +01:00
import android.widget.FrameLayout;
2013-12-20 20:25:49 +01:00
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
2017-03-31 01:58:05 +02:00
import org.telegram.messenger.ApplicationLoader;
import org.telegram.messenger.FileLog;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.LocaleController;
2013-12-20 20:25:49 +01:00
import org.telegram.messenger.R;
2017-03-31 01:58:05 +02:00
import org.telegram.messenger.Utilities;
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.ActionBar.ThemeDescription;
2014-11-13 21:10:14 +01:00
import org.telegram.ui.ActionBar.ActionBar;
import org.telegram.ui.ActionBar.ActionBarMenu;
import org.telegram.ui.ActionBar.ActionBarMenuItem;
import org.telegram.ui.ActionBar.BaseFragment;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.Cells.DividerCell;
import org.telegram.ui.Cells.LetterSectionCell;
import org.telegram.ui.Cells.TextSettingsCell;
import org.telegram.ui.Components.EmptyTextProgressView;
import org.telegram.ui.Components.LayoutHelper;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.Components.RecyclerListView;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
2021-09-20 07:54:41 +02:00
import java.util.Objects;
2017-03-31 01:58:05 +02:00
import java.util.Timer;
import java.util.TimerTask;
2013-12-20 20:25:49 +01:00
2019-05-14 14:08:05 +02:00
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class CountrySelectActivity extends BaseFragment {
public interface CountrySelectActivityDelegate {
2021-09-20 07:54:41 +02:00
void didSelectCountry(Country country);
}
2017-03-31 01:58:05 +02:00
private RecyclerListView listView;
private EmptyTextProgressView emptyView;
2014-11-13 21:10:14 +01:00
private CountryAdapter listViewAdapter;
private CountrySearchAdapter searchListViewAdapter;
2013-12-20 20:25:49 +01:00
2014-11-13 21:10:14 +01:00
private boolean searchWas;
private boolean searching;
2017-03-31 01:58:05 +02:00
private boolean needPhoneCode;
2013-12-20 20:25:49 +01:00
2014-11-13 21:10:14 +01:00
private CountrySelectActivityDelegate delegate;
2021-09-20 07:54:41 +02:00
private ArrayList<Country> existingCountries;
2013-12-20 20:25:49 +01:00
2017-03-31 01:58:05 +02:00
public CountrySelectActivity(boolean phoneCode) {
2021-09-20 07:54:41 +02:00
this(phoneCode, null);
}
public CountrySelectActivity(boolean phoneCode, ArrayList<Country> existingCountries) {
2017-03-31 01:58:05 +02:00
super();
2021-09-20 07:54:41 +02:00
if (existingCountries != null && !existingCountries.isEmpty()) {
this.existingCountries = new ArrayList<>(existingCountries);
}
2017-03-31 01:58:05 +02:00
needPhoneCode = phoneCode;
}
2013-12-20 20:25:49 +01:00
@Override
public boolean onFragmentCreate() {
return super.onFragmentCreate();
}
2013-12-20 20:25:49 +01:00
@Override
public void onFragmentDestroy() {
super.onFragmentDestroy();
}
2013-12-20 20:25:49 +01:00
@Override
2015-07-22 20:56:37 +02:00
public View createView(Context context) {
2015-04-09 20:00:14 +02:00
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
actionBar.setAllowOverlayTitle(true);
actionBar.setTitle(LocaleController.getString("ChooseCountry", R.string.ChooseCountry));
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
@Override
public void onItemClick(int id) {
if (id == -1) {
finishFragment();
2013-12-20 20:25:49 +01:00
}
2015-04-09 20:00:14 +02:00
}
});
2013-12-20 20:25:49 +01:00
2015-04-09 20:00:14 +02:00
ActionBarMenu menu = actionBar.createMenu();
ActionBarMenuItem item = menu.addItem(0, R.drawable.ic_ab_search).setIsSearchField(true).setActionBarMenuItemSearchListener(new ActionBarMenuItem.ActionBarMenuItemSearchListener() {
@Override
public void onSearchExpand() {
searching = true;
}
2013-12-20 20:25:49 +01:00
2015-04-09 20:00:14 +02:00
@Override
2015-06-29 19:12:11 +02:00
public void onSearchCollapse() {
2015-04-09 20:00:14 +02:00
searchListViewAdapter.search(null);
searching = false;
searchWas = false;
listView.setAdapter(listViewAdapter);
2017-03-31 01:58:05 +02:00
listView.setFastScrollVisible(true);
2015-04-09 20:00:14 +02:00
}
2015-04-09 20:00:14 +02:00
@Override
public void onTextChanged(EditText editText) {
String text = editText.getText().toString();
searchListViewAdapter.search(text);
if (text.length() != 0) {
searchWas = true;
2013-12-20 20:25:49 +01:00
}
2014-11-13 21:10:14 +01:00
}
2015-04-09 20:00:14 +02:00
});
2019-01-23 18:03:33 +01:00
item.setSearchFieldHint(LocaleController.getString("Search", R.string.Search));
2015-04-09 20:00:14 +02:00
searching = false;
searchWas = false;
2021-09-20 07:54:41 +02:00
listViewAdapter = new CountryAdapter(context, existingCountries);
2015-04-09 20:00:14 +02:00
searchListViewAdapter = new CountrySearchAdapter(context, listViewAdapter.getCountries());
fragmentView = new FrameLayout(context);
2017-03-31 01:58:05 +02:00
FrameLayout frameLayout = (FrameLayout) fragmentView;
2015-04-09 20:00:14 +02:00
2017-03-31 01:58:05 +02:00
emptyView = new EmptyTextProgressView(context);
emptyView.showTextView();
emptyView.setShowAtCenter(true);
emptyView.setText(LocaleController.getString("NoResult", R.string.NoResult));
frameLayout.addView(emptyView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
2015-04-09 20:00:14 +02:00
2017-03-31 01:58:05 +02:00
listView = new RecyclerListView(context);
listView.setSectionsType(1);
listView.setEmptyView(emptyView);
2015-04-09 20:00:14 +02:00
listView.setVerticalScrollBarEnabled(false);
2021-11-05 11:06:49 +01:00
listView.setFastScrollEnabled(RecyclerListView.FastScroll.LETTER_TYPE);
2017-03-31 01:58:05 +02:00
listView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
2015-04-09 20:00:14 +02:00
listView.setAdapter(listViewAdapter);
2017-03-31 01:58:05 +02:00
listView.setVerticalScrollbarPosition(LocaleController.isRTL ? RecyclerListView.SCROLLBAR_POSITION_LEFT : RecyclerListView.SCROLLBAR_POSITION_RIGHT);
frameLayout.addView(listView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
2018-08-27 10:33:11 +02:00
listView.setOnItemClickListener((view, position) -> {
Country country;
if (searching && searchWas) {
country = searchListViewAdapter.getItem(position);
} else {
int section = listViewAdapter.getSectionForPosition(position);
int row = listViewAdapter.getPositionInSectionForPosition(position);
if (row < 0 || section < 0) {
2015-04-09 20:00:14 +02:00
return;
}
2018-08-27 10:33:11 +02:00
country = listViewAdapter.getItem(section, row);
}
if (position < 0) {
return;
}
finishFragment();
if (country != null && delegate != null) {
2021-09-20 07:54:41 +02:00
delegate.didSelectCountry(country);
2015-04-09 20:00:14 +02:00
}
});
2013-12-20 20:25:49 +01:00
2017-03-31 01:58:05 +02:00
listView.setOnScrollListener(new RecyclerView.OnScrollListener() {
2015-04-09 20:00:14 +02:00
@Override
2017-03-31 01:58:05 +02:00
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
2020-02-13 19:26:53 +01:00
if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
2015-04-09 20:00:14 +02:00
AndroidUtilities.hideKeyboard(getParentActivity().getCurrentFocus());
2013-12-20 20:25:49 +01:00
}
2015-04-09 20:00:14 +02:00
}
});
return fragmentView;
2013-12-20 20:25:49 +01:00
}
@Override
public void onResume() {
super.onResume();
if (listViewAdapter != null) {
listViewAdapter.notifyDataSetChanged();
2013-12-20 20:25:49 +01:00
}
}
public void setCountrySelectActivityDelegate(CountrySelectActivityDelegate delegate) {
this.delegate = delegate;
}
2017-03-31 01:58:05 +02:00
public static class Country {
public String name;
public String code;
public String shortname;
2021-09-20 07:54:41 +02:00
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Country that = (Country) o;
return Objects.equals(name, that.name) && Objects.equals(code, that.code);
}
@Override
public int hashCode() {
return Objects.hash(name, code);
}
2017-03-31 01:58:05 +02:00
}
public class CountryAdapter extends RecyclerListView.SectionsAdapter {
private Context mContext;
private HashMap<String, ArrayList<Country>> countries = new HashMap<>();
private ArrayList<String> sortedCountries = new ArrayList<>();
2021-09-20 07:54:41 +02:00
public CountryAdapter(Context context, ArrayList<Country> exisitingCountries) {
2017-03-31 01:58:05 +02:00
mContext = context;
2021-09-20 07:54:41 +02:00
if (exisitingCountries != null) {
for (int i = 0; i < exisitingCountries.size(); i++) {
Country c = exisitingCountries.get(i);
2017-03-31 01:58:05 +02:00
String n = c.name.substring(0, 1).toUpperCase();
ArrayList<Country> arr = countries.get(n);
if (arr == null) {
arr = new ArrayList<>();
countries.put(n, arr);
sortedCountries.add(n);
}
arr.add(c);
}
2021-09-20 07:54:41 +02:00
} else {
try {
InputStream stream = ApplicationLoader.applicationContext.getResources().getAssets().open("countries.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line;
while ((line = reader.readLine()) != null) {
String[] args = line.split(";");
Country c = new Country();
c.name = args[2];
c.code = args[0];
c.shortname = args[1];
String n = c.name.substring(0, 1).toUpperCase();
ArrayList<Country> arr = countries.get(n);
if (arr == null) {
arr = new ArrayList<>();
countries.put(n, arr);
sortedCountries.add(n);
}
arr.add(c);
}
reader.close();
stream.close();
} catch (Exception e) {
FileLog.e(e);
}
2017-03-31 01:58:05 +02:00
}
2018-08-27 10:33:11 +02:00
Collections.sort(sortedCountries, String::compareTo);
2017-03-31 01:58:05 +02:00
for (ArrayList<Country> arr : countries.values()) {
2018-08-27 10:33:11 +02:00
Collections.sort(arr, (country, country2) -> country.name.compareTo(country2.name));
2017-03-31 01:58:05 +02:00
}
}
public HashMap<String, ArrayList<Country>> getCountries() {
return countries;
}
@Override
public Country getItem(int section, int position) {
if (section < 0 || section >= sortedCountries.size()) {
return null;
}
ArrayList<Country> arr = countries.get(sortedCountries.get(section));
if (position < 0 || position >= arr.size()) {
return null;
}
return arr.get(position);
}
@Override
2021-06-25 02:43:10 +02:00
public boolean isEnabled(RecyclerView.ViewHolder holder, int section, int row) {
2017-03-31 01:58:05 +02:00
ArrayList<Country> arr = countries.get(sortedCountries.get(section));
return row < arr.size();
}
@Override
public int getSectionCount() {
return sortedCountries.size();
}
@Override
public int getCountForSection(int section) {
int count = countries.get(sortedCountries.get(section)).size();
if (section != sortedCountries.size() - 1) {
count++;
}
return count;
}
@Override
public View getSectionHeaderView(int section, View view) {
if (view == null) {
view = new LetterSectionCell(mContext);
((LetterSectionCell) view).setCellHeight(AndroidUtilities.dp(48));
}
((LetterSectionCell) view).setLetter(sortedCountries.get(section).toUpperCase());
return view;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case 0:
view = new TextSettingsCell(mContext);
view.setPadding(AndroidUtilities.dp(LocaleController.isRTL ? 16 : 54), 0, AndroidUtilities.dp(LocaleController.isRTL ? 54 : 16), 0);
break;
case 1:
default:
view = new DividerCell(mContext);
2019-01-23 18:03:33 +01:00
view.setPadding(AndroidUtilities.dp(LocaleController.isRTL ? 24 : 72), AndroidUtilities.dp(8), AndroidUtilities.dp(LocaleController.isRTL ? 72 : 24), AndroidUtilities.dp(8));
2017-03-31 01:58:05 +02:00
break;
}
return new RecyclerListView.Holder(view);
}
@Override
public void onBindViewHolder(int section, int position, RecyclerView.ViewHolder holder) {
if (holder.getItemViewType() == 0) {
ArrayList<Country> arr = countries.get(sortedCountries.get(section));
Country c = arr.get(position);
((TextSettingsCell) holder.itemView).setTextAndValue(c.name, needPhoneCode ? "+" + c.code : null, false);
}
}
@Override
public int getItemViewType(int section, int position) {
ArrayList<Country> arr = countries.get(sortedCountries.get(section));
return position < arr.size() ? 0 : 1;
}
@Override
public String getLetter(int position) {
int section = getSectionForPosition(position);
if (section == -1) {
section = sortedCountries.size() - 1;
}
return sortedCountries.get(section);
}
@Override
2021-11-05 11:06:49 +01:00
public void getPositionForScrollProgress(RecyclerListView listView, float progress, int[] position) {
position[0] = (int) (getItemCount() * progress);
position[1] = 0;
2017-03-31 01:58:05 +02:00
}
}
public class CountrySearchAdapter extends RecyclerListView.SelectionAdapter {
private Context mContext;
private Timer searchTimer;
private ArrayList<Country> searchResult;
private HashMap<String, ArrayList<Country>> countries;
public CountrySearchAdapter(Context context, HashMap<String, ArrayList<Country>> countries) {
mContext = context;
this.countries = countries;
}
public void search(final String query) {
if (query == null) {
searchResult = null;
} else {
try {
if (searchTimer != null) {
searchTimer.cancel();
}
} catch (Exception e) {
FileLog.e(e);
}
searchTimer = new Timer();
searchTimer.schedule(new TimerTask() {
@Override
public void run() {
try {
searchTimer.cancel();
searchTimer = null;
} catch (Exception e) {
FileLog.e(e);
}
processSearch(query);
}
}, 100, 300);
}
}
private void processSearch(final String query) {
2018-08-27 10:33:11 +02:00
Utilities.searchQueue.postRunnable(() -> {
String q = query.trim().toLowerCase();
if (q.length() == 0) {
updateSearchResults(new ArrayList<>());
return;
}
ArrayList<Country> resultArray = new ArrayList<>();
String n = query.substring(0, 1);
ArrayList<Country> arr = countries.get(n.toUpperCase());
if (arr != null) {
for (Country c : arr) {
if (c.name.toLowerCase().startsWith(query)) {
resultArray.add(c);
2017-03-31 01:58:05 +02:00
}
}
}
2018-08-27 10:33:11 +02:00
updateSearchResults(resultArray);
2017-03-31 01:58:05 +02:00
});
}
private void updateSearchResults(final ArrayList<Country> arrCounties) {
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
2019-12-31 14:08:08 +01:00
if (!searching) {
return;
}
2018-08-27 10:33:11 +02:00
searchResult = arrCounties;
2019-12-31 14:08:08 +01:00
if (searchWas && listView != null && listView.getAdapter() != searchListViewAdapter) {
listView.setAdapter(searchListViewAdapter);
listView.setFastScrollVisible(false);
}
2018-08-27 10:33:11 +02:00
notifyDataSetChanged();
2017-03-31 01:58:05 +02:00
});
}
@Override
public boolean isEnabled(RecyclerView.ViewHolder holder) {
return true;
}
@Override
public int getItemCount() {
if (searchResult == null) {
return 0;
}
return searchResult.size();
}
public Country getItem(int i) {
if (i < 0 || i >= searchResult.size()) {
return null;
}
return searchResult.get(i);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new RecyclerListView.Holder(new TextSettingsCell(mContext));
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Country c = searchResult.get(position);
((TextSettingsCell) holder.itemView).setTextAndValue(c.name, needPhoneCode ? "+" + c.code : null, position != searchResult.size() - 1);
}
@Override
public int getItemViewType(int i) {
return 0;
}
}
@Override
2020-04-24 11:21:58 +02:00
public ArrayList<ThemeDescription> getThemeDescriptions() {
ArrayList<ThemeDescription> themeDescriptions = new ArrayList<>();
2017-03-31 01:58:05 +02:00
2020-04-24 11:21:58 +02:00
themeDescriptions.add(new ThemeDescription(fragmentView, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_windowBackgroundWhite));
2017-03-31 01:58:05 +02:00
2020-04-24 11:21:58 +02:00
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_actionBarDefault));
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_LISTGLOWCOLOR, null, null, null, null, Theme.key_actionBarDefault));
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_ITEMSCOLOR, null, null, null, null, Theme.key_actionBarDefaultIcon));
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_TITLECOLOR, null, null, null, null, Theme.key_actionBarDefaultTitle));
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SELECTORCOLOR, null, null, null, null, Theme.key_actionBarDefaultSelector));
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SEARCH, null, null, null, null, Theme.key_actionBarDefaultSearch));
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SEARCHPLACEHOLDER, null, null, null, null, Theme.key_actionBarDefaultSearchPlaceholder));
2017-03-31 01:58:05 +02:00
2020-04-24 11:21:58 +02:00
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_SELECTOR, null, null, null, null, Theme.key_listSelector));
2017-03-31 01:58:05 +02:00
2020-04-24 11:21:58 +02:00
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{View.class}, Theme.dividerPaint, null, null, Theme.key_divider));
2017-03-31 01:58:05 +02:00
2020-04-24 11:21:58 +02:00
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_FASTSCROLL, null, null, null, null, Theme.key_fastScrollActive));
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_FASTSCROLL, null, null, null, null, Theme.key_fastScrollInactive));
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_FASTSCROLL, null, null, null, null, Theme.key_fastScrollText));
2017-03-31 01:58:05 +02:00
2020-04-24 11:21:58 +02:00
themeDescriptions.add(new ThemeDescription(emptyView, ThemeDescription.FLAG_TEXTCOLOR, null, null, null, null, Theme.key_emptyListPlaceholder));
2017-03-31 01:58:05 +02:00
2020-04-24 11:21:58 +02:00
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextSettingsCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText));
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextSettingsCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteValueText));
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_SECTIONS, new Class[]{LetterSectionCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText4));
return themeDescriptions;
2017-03-31 01:58:05 +02:00
}
2013-12-20 20:25:49 +01:00
}