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

245 lines
10 KiB
Java
Raw Normal View History

2013-12-20 19:25:49 +00: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;
2014-11-13 20:10:14 +00:00
import android.os.Build;
import android.view.Gravity;
2013-12-20 19:25:49 +00:00
import android.view.LayoutInflater;
2014-10-07 20:14:27 +00:00
import android.view.MotionEvent;
2013-12-20 19:25:49 +00:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
2013-12-20 19:25:49 +00:00
import android.widget.AdapterView;
import android.widget.EditText;
2014-11-13 20:10:14 +00:00
import android.widget.FrameLayout;
2014-11-17 02:44:57 +00:00
import android.widget.LinearLayout;
2014-11-13 20:10:14 +00:00
import android.widget.ListView;
2013-12-20 19:25:49 +00:00
import android.widget.TextView;
import org.telegram.android.AndroidUtilities;
import org.telegram.android.LocaleController;
2013-12-20 19:25:49 +00:00
import org.telegram.messenger.R;
2014-11-13 20:10:14 +00: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.Views.SectionsListView;
2013-12-20 19:25:49 +00:00
public class CountrySelectActivity extends BaseFragment {
public static interface CountrySelectActivityDelegate {
public abstract void didSelectCountry(String name);
}
2014-11-13 20:10:14 +00:00
private SectionsListView listView;
2014-03-25 00:25:32 +00:00
private TextView emptyTextView;
2014-11-13 20:10:14 +00:00
private CountryAdapter listViewAdapter;
private CountrySearchAdapter searchListViewAdapter;
2013-12-20 19:25:49 +00:00
2014-11-13 20:10:14 +00:00
private boolean searchWas;
private boolean searching;
2013-12-20 19:25:49 +00:00
2014-11-13 20:10:14 +00:00
private CountrySelectActivityDelegate delegate;
2013-12-20 19:25:49 +00:00
@Override
public boolean onFragmentCreate() {
return super.onFragmentCreate();
}
2013-12-20 19:25:49 +00:00
@Override
public void onFragmentDestroy() {
super.onFragmentDestroy();
}
2013-12-20 19:25:49 +00:00
@Override
2014-11-13 20:10:14 +00:00
public View createView(LayoutInflater inflater, final ViewGroup container) {
if (fragmentView == null) {
2014-11-11 22:16:17 +00:00
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
2014-11-18 05:01:04 +00:00
actionBar.setAllowOverlayTitle(true);
2014-11-11 22:16:17 +00:00
actionBar.setTitle(LocaleController.getString("ChooseCountry", R.string.ChooseCountry));
2013-12-20 19:25:49 +00:00
2014-11-11 22:16:17 +00:00
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
@Override
public void onItemClick(int id) {
if (id == -1) {
finishFragment();
2013-12-20 19:25:49 +00:00
}
}
});
2013-12-20 19:25:49 +00:00
2014-11-11 22:16:17 +00:00
ActionBarMenu menu = actionBar.createMenu();
menu.addItem(0, R.drawable.ic_ab_search).setIsSearchField(true).setActionBarMenuItemSearchListener(new ActionBarMenuItem.ActionBarMenuItemSearchListener() {
@Override
public void onSearchExpand() {
searching = true;
}
2013-12-20 19:25:49 +00:00
@Override
public void onSearchCollapse() {
2014-11-13 20:10:14 +00:00
searchListViewAdapter.search(null);
searching = false;
searchWas = false;
ViewGroup group = (ViewGroup) listView.getParent();
listView.setAdapter(listViewAdapter);
if (android.os.Build.VERSION.SDK_INT >= 11) {
listView.setFastScrollAlwaysVisible(true);
}
listView.setFastScrollEnabled(true);
listView.setVerticalScrollBarEnabled(false);
2013-12-20 19:25:49 +00:00
emptyTextView.setText(LocaleController.getString("ChooseCountry", R.string.ChooseCountry));
}
@Override
public void onTextChanged(EditText editText) {
String text = editText.getText().toString();
2014-11-13 20:10:14 +00:00
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);
}
listView.setFastScrollEnabled(false);
listView.setVerticalScrollBarEnabled(true);
}
if (emptyTextView != null) {
2014-11-13 20:10:14 +00:00
}
2013-12-20 19:25:49 +00:00
}
}
});
2013-12-20 19:25:49 +00:00
searching = false;
searchWas = false;
2013-12-20 19:25:49 +00:00
2014-11-13 20:10:14 +00:00
listViewAdapter = new CountryAdapter(getParentActivity());
searchListViewAdapter = new CountrySearchAdapter(getParentActivity(), listViewAdapter.getCountries());
fragmentView = new FrameLayout(getParentActivity());
2014-11-17 02:44:57 +00:00
LinearLayout emptyTextLayout = new LinearLayout(getParentActivity());
emptyTextLayout.setVisibility(View.INVISIBLE);
emptyTextLayout.setOrientation(LinearLayout.VERTICAL);
((FrameLayout) fragmentView).addView(emptyTextLayout);
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) emptyTextLayout.getLayoutParams();
2014-11-13 20:10:14 +00:00
layoutParams.width = FrameLayout.LayoutParams.MATCH_PARENT;
layoutParams.height = FrameLayout.LayoutParams.MATCH_PARENT;
layoutParams.gravity = Gravity.TOP;
2014-11-17 02:44:57 +00:00
emptyTextLayout.setLayoutParams(layoutParams);
emptyTextLayout.setOnTouchListener(new View.OnTouchListener() {
2014-10-07 20:14:27 +00:00
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
2014-11-13 20:10:14 +00:00
2014-11-17 02:44:57 +00:00
emptyTextView = new TextView(getParentActivity());
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 = LinearLayout.LayoutParams.MATCH_PARENT;
layoutParams1.height = LinearLayout.LayoutParams.MATCH_PARENT;
layoutParams1.weight = 0.5f;
emptyTextView.setLayoutParams(layoutParams1);
FrameLayout frameLayout = new FrameLayout(getParentActivity());
emptyTextLayout.addView(frameLayout);
layoutParams1 = (LinearLayout.LayoutParams) frameLayout.getLayoutParams();
layoutParams1.width = LinearLayout.LayoutParams.MATCH_PARENT;
layoutParams1.height = LinearLayout.LayoutParams.MATCH_PARENT;
layoutParams1.weight = 0.5f;
frameLayout.setLayoutParams(layoutParams1);
2014-11-13 20:10:14 +00:00
listView = new SectionsListView(getParentActivity());
2014-11-17 02:44:57 +00:00
listView.setEmptyView(emptyTextLayout);
listView.setVerticalScrollBarEnabled(false);
2014-11-13 20:10:14 +00:00
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 = FrameLayout.LayoutParams.MATCH_PARENT;
layoutParams.height = FrameLayout.LayoutParams.MATCH_PARENT;
listView.setLayoutParams(layoutParams);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
2014-11-13 20:10:14 +00:00
Country country = null;
if (searching && searchWas) {
2014-11-13 20:10:14 +00:00
country = searchListViewAdapter.getItem(i);
} else {
int section = listViewAdapter.getSectionForPosition(i);
int row = listViewAdapter.getPositionInSectionForPosition(i);
2014-11-13 20:10:14 +00:00
if (row < 0 || section < 0) {
return;
2013-12-20 19:25:49 +00:00
}
2014-11-13 20:10:14 +00:00
country = listViewAdapter.getItem(section, row);
}
if (i < 0) {
return;
}
if (country != null && delegate != null) {
delegate.didSelectCountry(country.name);
2013-12-20 19:25:49 +00:00
}
2014-11-13 20:10:14 +00:00
finishFragment();
2013-12-20 19:25:49 +00:00
}
});
2013-12-20 19:25:49 +00: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 19:25:49 +00:00
}
@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
2014-11-17 02:44:57 +00:00
if (absListView.isFastScrollEnabled()) {
AndroidUtilities.clearDrawableAnimation(absListView);
}
}
});
} else {
ViewGroup parent = (ViewGroup)fragmentView.getParent();
if (parent != null) {
parent.removeView(fragmentView);
2013-12-20 19:25:49 +00:00
}
}
return fragmentView;
2013-12-20 19:25:49 +00:00
}
@Override
public void onResume() {
super.onResume();
if (listViewAdapter != null) {
listViewAdapter.notifyDataSetChanged();
2013-12-20 19:25:49 +00:00
}
}
public void setCountrySelectActivityDelegate(CountrySelectActivityDelegate delegate) {
this.delegate = delegate;
}
2013-12-20 19:25:49 +00:00
}