mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
430 lines
14 KiB
Java
430 lines
14 KiB
Java
/*******************************************************************************
|
|
*
|
|
* Messenger Android Frontend
|
|
* (C) 2013-2016 Nikolai Kudashov
|
|
* (C) 2017 Björn Petersen
|
|
* Contact: r10s@b44t.com, http://b44t.com
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
* version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
* details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with
|
|
* this program. If not, see http://www.gnu.org/licenses/ .
|
|
*
|
|
******************************************************************************/
|
|
|
|
|
|
package com.b44t.ui;
|
|
|
|
import android.content.Context;
|
|
import android.view.Gravity;
|
|
import android.view.MotionEvent;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.AbsListView;
|
|
import android.widget.AdapterView;
|
|
import android.widget.EditText;
|
|
import android.widget.FrameLayout;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.ListView;
|
|
import android.widget.TextView;
|
|
|
|
import com.b44t.messenger.AndroidUtilities;
|
|
import com.b44t.messenger.ApplicationLoader;
|
|
import com.b44t.messenger.FileLog;
|
|
import com.b44t.messenger.LocaleController;
|
|
import com.b44t.messenger.R;
|
|
import com.b44t.messenger.Utilities;
|
|
import com.b44t.ui.Adapters.BaseFragmentAdapter;
|
|
import com.b44t.ui.Cells.TextDetailSettingsCell;
|
|
import com.b44t.ui.ActionBar.ActionBar;
|
|
import com.b44t.ui.ActionBar.ActionBarMenu;
|
|
import com.b44t.ui.ActionBar.ActionBarMenuItem;
|
|
import com.b44t.ui.ActionBar.BaseFragment;
|
|
import com.b44t.ui.Components.LayoutHelper;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.Comparator;
|
|
import java.util.Timer;
|
|
import java.util.TimerTask;
|
|
|
|
public class LanguageSelectActivity extends BaseFragment {
|
|
private BaseFragmentAdapter listAdapter;
|
|
private ListView listView;
|
|
private boolean searchWas;
|
|
private boolean searching;
|
|
private BaseFragmentAdapter searchListViewAdapter;
|
|
private TextView emptyTextView;
|
|
private Timer searchTimer;
|
|
public ArrayList<LocaleController.LocaleInfo> searchResult;
|
|
private ArrayList<LocaleController.LocaleInfo> sortedLanguages = new ArrayList<>();
|
|
|
|
@Override
|
|
public View createView(Context context) {
|
|
|
|
for (LocaleController.LocaleInfo localInfo : LocaleController.getInstance().languagesDict.values()) {
|
|
sortedLanguages.add(localInfo);
|
|
}
|
|
Collections.sort(sortedLanguages, new Comparator<LocaleController.LocaleInfo>() {
|
|
@Override
|
|
public int compare(LocaleController.LocaleInfo o, LocaleController.LocaleInfo o2) {
|
|
return o.name.compareTo(o2.name);
|
|
}
|
|
});
|
|
LocaleController.LocaleInfo localeInfo = new LocaleController.LocaleInfo();
|
|
localeInfo.name = ApplicationLoader.applicationContext.getString(R.string.Default); // the "Default" option is interesting, because it not only sets the language to default but also clears the "override" flag - further system changes are then followed by Delta Chat again.
|
|
localeInfo.nameEnglish = "Default";
|
|
localeInfo.shortName = null;
|
|
sortedLanguages.add(0, localeInfo);
|
|
|
|
searching = false;
|
|
searchWas = false;
|
|
|
|
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
|
|
actionBar.setAllowOverlayTitle(true);
|
|
actionBar.setTitle(LocaleController.getString("Language", R.string.Language));
|
|
|
|
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
|
|
@Override
|
|
public void onItemClick(int id) {
|
|
if (id == -1) {
|
|
finishFragment();
|
|
}
|
|
}
|
|
});
|
|
|
|
ActionBarMenu menu = actionBar.createMenu();
|
|
ActionBarMenuItem item = menu.addItem(0, R.drawable.ic_ab_search).setIsSearchField(true, false).setActionBarMenuItemSearchListener(new ActionBarMenuItem.ActionBarMenuItemSearchListener() {
|
|
@Override
|
|
public void onSearchExpand() {
|
|
searching = true;
|
|
}
|
|
|
|
@Override
|
|
public void onSearchCollapse() {
|
|
search(null);
|
|
searching = false;
|
|
searchWas = false;
|
|
if (listView != null) {
|
|
emptyTextView.setVisibility(View.GONE);
|
|
listView.setAdapter(listAdapter);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onTextChanged(EditText editText) {
|
|
String text = editText.getText().toString();
|
|
search(text);
|
|
if (text.length() != 0) {
|
|
searchWas = true;
|
|
if (listView != null) {
|
|
listView.setAdapter(searchListViewAdapter);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
item.getSearchField().setHint(LocaleController.getString("Search", R.string.Search));
|
|
|
|
listAdapter = new ListAdapter(context);
|
|
searchListViewAdapter = new SearchAdapter(context);
|
|
|
|
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;
|
|
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;
|
|
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;
|
|
layoutParams1.weight = 0.5f;
|
|
frameLayout.setLayoutParams(layoutParams1);
|
|
|
|
listView = new ListView(context);
|
|
listView.setEmptyView(emptyTextLayout);
|
|
listView.setDrawSelectorOnTop(true);
|
|
listView.setDivider(null);
|
|
listView.setDividerHeight(0);
|
|
listView.setAdapter(listAdapter);
|
|
((FrameLayout) fragmentView).addView(listView);
|
|
layoutParams = (FrameLayout.LayoutParams) listView.getLayoutParams();
|
|
layoutParams.width = LayoutHelper.MATCH_PARENT;
|
|
layoutParams.height = LayoutHelper.MATCH_PARENT;
|
|
listView.setLayoutParams(layoutParams);
|
|
|
|
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
@Override
|
|
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
|
LocaleController.LocaleInfo localeInfo = null;
|
|
if (searching && searchWas) {
|
|
if (i >= 0 && i < searchResult.size()) {
|
|
localeInfo = searchResult.get(i);
|
|
}
|
|
} else {
|
|
if (i >= 0 && i < sortedLanguages.size()) {
|
|
localeInfo = sortedLanguages.get(i);
|
|
}
|
|
}
|
|
if (localeInfo != null) {
|
|
LocaleController.getInstance().applyLanguage(localeInfo, true);
|
|
parentLayout.rebuildAllFragmentViews(false);
|
|
}
|
|
finishFragment();
|
|
}
|
|
});
|
|
|
|
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());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
|
|
}
|
|
});
|
|
|
|
return fragmentView;
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
if (listAdapter != null) {
|
|
listAdapter.notifyDataSetChanged();
|
|
}
|
|
}
|
|
|
|
public void search(final String query) {
|
|
if (query == null) {
|
|
searchResult = null;
|
|
} else {
|
|
try {
|
|
if (searchTimer != null) {
|
|
searchTimer.cancel();
|
|
}
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
searchTimer = new Timer();
|
|
searchTimer.schedule(new TimerTask() {
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
searchTimer.cancel();
|
|
searchTimer = null;
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
processSearch(query);
|
|
}
|
|
}, 100, 300);
|
|
}
|
|
}
|
|
|
|
private void processSearch(final String query) {
|
|
Utilities.searchQueue.postRunnable(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
|
|
String q = query.trim().toLowerCase();
|
|
if (q.length() == 0) {
|
|
updateSearchResults(new ArrayList<LocaleController.LocaleInfo>());
|
|
return;
|
|
}
|
|
ArrayList<LocaleController.LocaleInfo> resultArray = new ArrayList<>();
|
|
|
|
for (LocaleController.LocaleInfo c : sortedLanguages) {
|
|
if (c.name.toLowerCase().startsWith(query) || c.nameEnglish.toLowerCase().startsWith(query)) {
|
|
resultArray.add(c);
|
|
}
|
|
}
|
|
|
|
updateSearchResults(resultArray);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void updateSearchResults(final ArrayList<LocaleController.LocaleInfo> arrCounties) {
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
searchResult = arrCounties;
|
|
searchListViewAdapter.notifyDataSetChanged();
|
|
}
|
|
});
|
|
}
|
|
|
|
private class SearchAdapter extends BaseFragmentAdapter {
|
|
private Context mContext;
|
|
|
|
public SearchAdapter(Context context) {
|
|
mContext = context;
|
|
}
|
|
|
|
@Override
|
|
public boolean areAllItemsEnabled() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean isEnabled(int i) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public int getCount() {
|
|
if (searchResult == null) {
|
|
return 0;
|
|
}
|
|
return searchResult.size();
|
|
}
|
|
|
|
@Override
|
|
public Object getItem(int i) {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public long getItemId(int i) {
|
|
return i;
|
|
}
|
|
|
|
@Override
|
|
public boolean hasStableIds() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public View getView(int i, View view, ViewGroup viewGroup) {
|
|
if (view == null) {
|
|
view = new TextDetailSettingsCell(mContext);
|
|
}
|
|
|
|
LocaleController.LocaleInfo c = searchResult.get(i);
|
|
((TextDetailSettingsCell) view).setTextAndValue(c.name, c.nameEnglish, i != searchResult.size() - 1);
|
|
|
|
return view;
|
|
}
|
|
|
|
@Override
|
|
public int getItemViewType(int i) {
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public int getViewTypeCount() {
|
|
return 1;
|
|
}
|
|
|
|
@Override
|
|
public boolean isEmpty() {
|
|
return searchResult == null || searchResult.size() == 0;
|
|
}
|
|
}
|
|
|
|
private class ListAdapter extends BaseFragmentAdapter {
|
|
private Context mContext;
|
|
|
|
public ListAdapter(Context context) {
|
|
mContext = context;
|
|
}
|
|
|
|
@Override
|
|
public boolean areAllItemsEnabled() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean isEnabled(int i) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public int getCount() {
|
|
if (sortedLanguages == null) {
|
|
return 0;
|
|
}
|
|
return sortedLanguages.size();
|
|
}
|
|
|
|
@Override
|
|
public Object getItem(int i) {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public long getItemId(int i) {
|
|
return i;
|
|
}
|
|
|
|
@Override
|
|
public boolean hasStableIds() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public View getView(int i, View view, ViewGroup viewGroup) {
|
|
if (view == null) {
|
|
view = new TextDetailSettingsCell(mContext);
|
|
}
|
|
|
|
LocaleController.LocaleInfo localeInfo = sortedLanguages.get(i);
|
|
((TextDetailSettingsCell) view).setTextAndValue(localeInfo.name, localeInfo.nameEnglish, i != sortedLanguages.size() - 1);
|
|
|
|
return view;
|
|
}
|
|
|
|
@Override
|
|
public int getItemViewType(int i) {
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public int getViewTypeCount() {
|
|
return 1;
|
|
}
|
|
|
|
@Override
|
|
public boolean isEmpty() {
|
|
return sortedLanguages == null || sortedLanguages.size() == 0;
|
|
}
|
|
}
|
|
}
|