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;
|
2014-05-24 22:47:05 +00:00
|
|
|
import android.widget.AbsListView;
|
2013-12-20 19:25:49 +00:00
|
|
|
import android.widget.AdapterView;
|
2014-06-03 23:31:48 +00:00
|
|
|
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;
|
|
|
|
|
2014-07-02 22:39:05 +00:00
|
|
|
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
|
|
|
|
2014-06-03 23:31:48 +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
|
2014-06-03 23:31:48 +00:00
|
|
|
public boolean onFragmentCreate() {
|
|
|
|
return super.onFragmentCreate();
|
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
|
2014-06-03 23:31:48 +00:00
|
|
|
@Override
|
|
|
|
public void onFragmentDestroy() {
|
|
|
|
super.onFragmentDestroy();
|
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
|
2014-06-03 23:31:48 +00:00
|
|
|
@Override
|
2014-11-13 20:10:14 +00:00
|
|
|
public View createView(LayoutInflater inflater, final ViewGroup container) {
|
2014-06-03 23:31:48 +00:00
|
|
|
if (fragmentView == null) {
|
2014-11-11 22:16:17 +00:00
|
|
|
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
|
|
|
|
actionBar.setBackOverlay(R.layout.updating_state_layout);
|
|
|
|
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() {
|
2014-06-03 23:31:48 +00:00
|
|
|
@Override
|
|
|
|
public void onItemClick(int id) {
|
|
|
|
if (id == -1) {
|
|
|
|
finishFragment();
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-03 23:31:48 +00:00
|
|
|
});
|
2013-12-20 19:25:49 +00:00
|
|
|
|
2014-11-11 22:16:17 +00:00
|
|
|
ActionBarMenu menu = actionBar.createMenu();
|
2014-06-03 23:31:48 +00:00
|
|
|
menu.addItem(0, R.drawable.ic_ab_search).setIsSearchField(true).setActionBarMenuItemSearchListener(new ActionBarMenuItem.ActionBarMenuItemSearchListener() {
|
|
|
|
@Override
|
|
|
|
public void onSearchExpand() {
|
|
|
|
searching = true;
|
2014-05-24 22:47:05 +00:00
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
|
2014-06-03 23:31:48 +00:00
|
|
|
@Override
|
|
|
|
public void onSearchCollapse() {
|
2014-11-13 20:10:14 +00:00
|
|
|
searchListViewAdapter.search(null);
|
2014-06-03 23:31:48 +00:00
|
|
|
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
|
|
|
|
2014-06-03 23:31:48 +00:00
|
|
|
emptyTextView.setText(LocaleController.getString("ChooseCountry", R.string.ChooseCountry));
|
|
|
|
}
|
2014-04-04 17:58:33 +00:00
|
|
|
|
2014-06-03 23:31:48 +00:00
|
|
|
@Override
|
|
|
|
public void onTextChanged(EditText editText) {
|
|
|
|
String text = editText.getText().toString();
|
2014-11-13 20:10:14 +00:00
|
|
|
searchListViewAdapter.search(text);
|
2014-06-03 23:31:48 +00:00
|
|
|
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
|
|
|
|
2014-06-03 23:31:48 +00:00
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-03 23:31:48 +00:00
|
|
|
});
|
2013-12-20 19:25:49 +00:00
|
|
|
|
2014-06-03 23:31:48 +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);
|
2014-06-03 23:31:48 +00:00
|
|
|
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);
|
2014-06-03 23:31:48 +00:00
|
|
|
|
|
|
|
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;
|
2014-06-03 23:31:48 +00:00
|
|
|
if (searching && searchWas) {
|
2014-11-13 20:10:14 +00:00
|
|
|
country = searchListViewAdapter.getItem(i);
|
2014-06-03 23:31:48 +00:00
|
|
|
} 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
|
|
|
}
|
2014-06-03 23:31:48 +00:00
|
|
|
});
|
2013-12-20 19:25:49 +00:00
|
|
|
|
2014-06-03 23:31:48 +00:00
|
|
|
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
|
|
|
|
@Override
|
|
|
|
public void onScrollStateChanged(AbsListView absListView, int i) {
|
|
|
|
if (i == SCROLL_STATE_TOUCH_SCROLL && searching && searchWas) {
|
2014-07-02 22:39:05 +00:00
|
|
|
AndroidUtilities.hideKeyboard(getParentActivity().getCurrentFocus());
|
2014-06-03 23:31:48 +00:00
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
|
|
|
|
2014-06-03 23:31:48 +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);
|
|
|
|
}
|
2014-06-03 23:31:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
ViewGroup parent = (ViewGroup)fragmentView.getParent();
|
|
|
|
if (parent != null) {
|
|
|
|
parent.removeView(fragmentView);
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
2014-06-03 23:31:48 +00:00
|
|
|
}
|
|
|
|
return fragmentView;
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
|
|
|
|
2014-06-03 23:31:48 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
2014-06-04 16:00:42 +00:00
|
|
|
super.onResume();
|
2014-06-03 23:31:48 +00:00
|
|
|
if (listViewAdapter != null) {
|
|
|
|
listViewAdapter.notifyDataSetChanged();
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-03 23:31:48 +00:00
|
|
|
public void setCountrySelectActivityDelegate(CountrySelectActivityDelegate delegate) {
|
|
|
|
this.delegate = delegate;
|
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|