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

242 lines
9.2 KiB
Java
Raw Normal View History

2013-12-20 20:25:49 +01:00
/*
* This is the source code of Telegram for Android v. 1.3.2.
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Nikolai Kudashov, 2013.
*/
package org.telegram.ui;
2015-04-09 20:00:14 +02:00
import android.content.Context;
2014-11-13 21:10:14 +01:00
import android.os.Build;
import android.view.Gravity;
2013-12-20 20:25:49 +01:00
import android.view.LayoutInflater;
2014-10-07 22:14:27 +02:00
import android.view.MotionEvent;
2013-12-20 20:25:49 +01:00
import android.view.View;
import android.widget.AbsListView;
2013-12-20 20:25:49 +01:00
import android.widget.AdapterView;
import android.widget.EditText;
2014-11-13 21:10:14 +01:00
import android.widget.FrameLayout;
2014-11-17 03:44:57 +01:00
import android.widget.LinearLayout;
2014-11-13 21:10:14 +01:00
import android.widget.ListView;
2013-12-20 20:25:49 +01:00
import android.widget.TextView;
import org.telegram.android.AndroidUtilities;
import org.telegram.android.LocaleController;
2013-12-20 20:25:49 +01:00
import org.telegram.messenger.R;
2014-11-13 21:10:14 +01:00
import org.telegram.ui.Adapters.CountryAdapter;
import org.telegram.ui.Adapters.CountryAdapter.Country;
import org.telegram.ui.Adapters.CountrySearchAdapter;
import org.telegram.ui.ActionBar.ActionBar;
import org.telegram.ui.ActionBar.ActionBarMenu;
import org.telegram.ui.ActionBar.ActionBarMenuItem;
import org.telegram.ui.ActionBar.BaseFragment;
import org.telegram.ui.Components.LayoutHelper;
import org.telegram.ui.Components.LetterSectionsListView;
2013-12-20 20:25:49 +01:00
public class CountrySelectActivity extends BaseFragment {
public interface CountrySelectActivityDelegate {
void didSelectCountry(String name);
}
private LetterSectionsListView listView;
2014-03-25 01:25:32 +01:00
private TextView emptyTextView;
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;
2013-12-20 20:25:49 +01:00
2014-11-13 21:10:14 +01:00
private CountrySelectActivityDelegate delegate;
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-04-09 20:00:14 +02:00
public View createView(Context context, LayoutInflater inflater) {
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
public boolean onSearchCollapse() {
searchListViewAdapter.search(null);
searching = false;
searchWas = false;
listView.setAdapter(listViewAdapter);
if (android.os.Build.VERSION.SDK_INT >= 11) {
listView.setFastScrollAlwaysVisible(true);
}
listView.setFastScrollEnabled(true);
listView.setVerticalScrollBarEnabled(false);
2013-12-20 20:25:49 +01:00
2015-04-09 20:00:14 +02:00
emptyTextView.setText(LocaleController.getString("ChooseCountry", R.string.ChooseCountry));
2015-04-09 20:00:14 +02:00
return true;
}
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;
if (listView != null) {
listView.setAdapter(searchListViewAdapter);
if (android.os.Build.VERSION.SDK_INT >= 11) {
listView.setFastScrollAlwaysVisible(false);
}
2015-04-09 20:00:14 +02:00
listView.setFastScrollEnabled(false);
listView.setVerticalScrollBarEnabled(true);
}
if (emptyTextView != null) {
2014-11-13 21:10:14 +01:00
2013-12-20 20:25:49 +01:00
}
}
2014-11-13 21:10:14 +01:00
}
2015-04-09 20:00:14 +02:00
});
item.getSearchField().setHint(LocaleController.getString("Search", R.string.Search));
searching = false;
searchWas = false;
listViewAdapter = new CountryAdapter(context);
searchListViewAdapter = new CountrySearchAdapter(context, listViewAdapter.getCountries());
fragmentView = new FrameLayout(context);
LinearLayout emptyTextLayout = new LinearLayout(context);
emptyTextLayout.setVisibility(View.INVISIBLE);
emptyTextLayout.setOrientation(LinearLayout.VERTICAL);
((FrameLayout) fragmentView).addView(emptyTextLayout);
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) emptyTextLayout.getLayoutParams();
layoutParams.width = LayoutHelper.MATCH_PARENT;
layoutParams.height = LayoutHelper.MATCH_PARENT;
2015-04-09 20:00:14 +02:00
layoutParams.gravity = Gravity.TOP;
emptyTextLayout.setLayoutParams(layoutParams);
emptyTextLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
emptyTextView = new TextView(context);
emptyTextView.setTextColor(0xff808080);
emptyTextView.setTextSize(20);
emptyTextView.setGravity(Gravity.CENTER);
emptyTextView.setText(LocaleController.getString("NoResult", R.string.NoResult));
emptyTextLayout.addView(emptyTextView);
LinearLayout.LayoutParams layoutParams1 = (LinearLayout.LayoutParams) emptyTextView.getLayoutParams();
layoutParams1.width = LayoutHelper.MATCH_PARENT;
layoutParams1.height = LayoutHelper.MATCH_PARENT;
2015-04-09 20:00:14 +02:00
layoutParams1.weight = 0.5f;
emptyTextView.setLayoutParams(layoutParams1);
FrameLayout frameLayout = new FrameLayout(context);
emptyTextLayout.addView(frameLayout);
layoutParams1 = (LinearLayout.LayoutParams) frameLayout.getLayoutParams();
layoutParams1.width = LayoutHelper.MATCH_PARENT;
layoutParams1.height = LayoutHelper.MATCH_PARENT;
2015-04-09 20:00:14 +02:00
layoutParams1.weight = 0.5f;
frameLayout.setLayoutParams(layoutParams1);
listView = new LetterSectionsListView(context);
listView.setEmptyView(emptyTextLayout);
listView.setVerticalScrollBarEnabled(false);
listView.setDivider(null);
listView.setDividerHeight(0);
listView.setFastScrollEnabled(true);
listView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
listView.setAdapter(listViewAdapter);
if (Build.VERSION.SDK_INT >= 11) {
listView.setFastScrollAlwaysVisible(true);
listView.setVerticalScrollbarPosition(LocaleController.isRTL ? ListView.SCROLLBAR_POSITION_LEFT : ListView.SCROLLBAR_POSITION_RIGHT);
}
((FrameLayout) fragmentView).addView(listView);
layoutParams = (FrameLayout.LayoutParams) listView.getLayoutParams();
layoutParams.width = LayoutHelper.MATCH_PARENT;
layoutParams.height = LayoutHelper.MATCH_PARENT;
2015-04-09 20:00:14 +02:00
listView.setLayoutParams(layoutParams);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Country country = null;
if (searching && searchWas) {
country = searchListViewAdapter.getItem(i);
} else {
int section = listViewAdapter.getSectionForPosition(i);
int row = listViewAdapter.getPositionInSectionForPosition(i);
if (row < 0 || section < 0) {
2014-11-13 21:10:14 +01:00
return;
}
2015-04-09 20:00:14 +02:00
country = listViewAdapter.getItem(section, row);
}
if (i < 0) {
return;
}
if (country != null && delegate != null) {
delegate.didSelectCountry(country.name);
2013-12-20 20:25:49 +01:00
}
2015-04-09 20:00:14 +02:00
finishFragment();
}
});
2013-12-20 20:25:49 +01:00
2015-04-09 20:00:14 +02:00
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
if (i == SCROLL_STATE_TOUCH_SCROLL && searching && searchWas) {
AndroidUtilities.hideKeyboard(getParentActivity().getCurrentFocus());
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
@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (absListView.isFastScrollEnabled()) {
AndroidUtilities.clearDrawableAnimation(absListView);
}
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;
}
2013-12-20 20:25:49 +01:00
}