2014-03-25 01:25:32 +01:00
|
|
|
/*
|
|
|
|
* This is the source code of Telegram for Android v. 1.3.x.
|
|
|
|
* It is licensed under GNU GPL v. 2 or later.
|
|
|
|
* You should have received a copy of the license in this archive (see LICENSE).
|
|
|
|
*
|
2017-03-31 01:58:05 +02:00
|
|
|
* Copyright Nikolai Kudashov, 2013-2017.
|
2014-03-25 01:25:32 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.telegram.ui;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2014-04-02 19:36:57 +02:00
|
|
|
import android.content.DialogInterface;
|
2014-03-25 01:25:32 +01:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2014-06-04 01:31:48 +02:00
|
|
|
import android.widget.EditText;
|
2014-11-13 21:10:14 +01:00
|
|
|
import android.widget.FrameLayout;
|
2014-03-25 01:25:32 +01:00
|
|
|
|
2015-09-24 22:52:02 +02:00
|
|
|
import org.telegram.messenger.AndroidUtilities;
|
2014-03-25 01:25:32 +01:00
|
|
|
import org.telegram.messenger.FileLog;
|
2015-09-24 22:52:02 +02:00
|
|
|
import org.telegram.messenger.LocaleController;
|
2014-03-25 01:25:32 +01:00
|
|
|
import org.telegram.messenger.R;
|
|
|
|
import org.telegram.messenger.Utilities;
|
2017-03-31 01:58:05 +02:00
|
|
|
import org.telegram.messenger.support.widget.LinearLayoutManager;
|
|
|
|
import org.telegram.messenger.support.widget.RecyclerView;
|
|
|
|
import org.telegram.ui.ActionBar.AlertDialog;
|
|
|
|
import org.telegram.ui.ActionBar.Theme;
|
|
|
|
import org.telegram.ui.ActionBar.ThemeDescription;
|
2014-11-13 21:10:14 +01:00
|
|
|
import org.telegram.ui.Cells.TextSettingsCell;
|
|
|
|
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.Components.EmptyTextProgressView;
|
2015-05-03 13:48:36 +02:00
|
|
|
import org.telegram.ui.Components.LayoutHelper;
|
2017-03-31 01:58:05 +02:00
|
|
|
import org.telegram.ui.Components.RecyclerListView;
|
2014-03-25 01:25:32 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Timer;
|
|
|
|
import java.util.TimerTask;
|
|
|
|
|
|
|
|
public class LanguageSelectActivity extends BaseFragment {
|
2017-03-31 01:58:05 +02:00
|
|
|
|
|
|
|
private ListAdapter listAdapter;
|
|
|
|
private RecyclerListView listView;
|
|
|
|
private ListAdapter searchListViewAdapter;
|
|
|
|
private EmptyTextProgressView emptyView;
|
|
|
|
|
2014-03-25 01:25:32 +01:00
|
|
|
private boolean searchWas;
|
|
|
|
private boolean searching;
|
|
|
|
|
|
|
|
private Timer searchTimer;
|
|
|
|
public ArrayList<LocaleController.LocaleInfo> searchResult;
|
|
|
|
|
|
|
|
@Override
|
2015-07-22 20:56:37 +02:00
|
|
|
public View createView(Context context) {
|
2015-04-09 20:00:14 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-11-13 21:10:14 +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;
|
|
|
|
}
|
2014-03-25 01:25:32 +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
|
|
|
search(null);
|
|
|
|
searching = false;
|
|
|
|
searchWas = false;
|
|
|
|
if (listView != null) {
|
2017-03-31 01:58:05 +02:00
|
|
|
emptyView.setVisibility(View.GONE);
|
2015-04-09 20:00:14 +02:00
|
|
|
listView.setAdapter(listAdapter);
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
2015-04-09 20:00:14 +02:00
|
|
|
}
|
2014-06-04 01:31:48 +02:00
|
|
|
|
2015-04-09 20:00:14 +02:00
|
|
|
@Override
|
|
|
|
public void onTextChanged(EditText editText) {
|
|
|
|
String text = editText.getText().toString();
|
|
|
|
search(text);
|
|
|
|
if (text.length() != 0) {
|
|
|
|
searchWas = true;
|
2014-06-04 01:31:48 +02:00
|
|
|
if (listView != null) {
|
2015-04-09 20:00:14 +02:00
|
|
|
listView.setAdapter(searchListViewAdapter);
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-09 20:00:14 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
item.getSearchField().setHint(LocaleController.getString("Search", R.string.Search));
|
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
listAdapter = new ListAdapter(context, false);
|
|
|
|
searchListViewAdapter = new ListAdapter(context, true);
|
2015-04-09 20:00:14 +02:00
|
|
|
|
|
|
|
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.setText(LocaleController.getString("NoResult", R.string.NoResult));
|
|
|
|
emptyView.showTextView();
|
|
|
|
emptyView.setShowAtCenter(true);
|
|
|
|
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.setEmptyView(emptyView);
|
|
|
|
listView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
|
2015-04-09 20:00:14 +02:00
|
|
|
listView.setVerticalScrollBarEnabled(false);
|
|
|
|
listView.setAdapter(listAdapter);
|
2017-03-31 01:58:05 +02:00
|
|
|
frameLayout.addView(listView, 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.setOnItemClickListener(new RecyclerListView.OnItemClickListener() {
|
2015-04-09 20:00:14 +02:00
|
|
|
@Override
|
2017-03-31 01:58:05 +02:00
|
|
|
public void onItemClick(View view, int position) {
|
2015-04-09 20:00:14 +02:00
|
|
|
LocaleController.LocaleInfo localeInfo = null;
|
|
|
|
if (searching && searchWas) {
|
2017-03-31 01:58:05 +02:00
|
|
|
if (position >= 0 && position < searchResult.size()) {
|
|
|
|
localeInfo = searchResult.get(position);
|
2015-04-09 20:00:14 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-31 01:58:05 +02:00
|
|
|
if (position >= 0 && position < LocaleController.getInstance().sortedLanguages.size()) {
|
|
|
|
localeInfo = LocaleController.getInstance().sortedLanguages.get(position);
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-09 20:00:14 +02:00
|
|
|
if (localeInfo != null) {
|
|
|
|
LocaleController.getInstance().applyLanguage(localeInfo, true);
|
|
|
|
parentLayout.rebuildAllFragmentViews(false);
|
2014-10-07 22:14:27 +02:00
|
|
|
}
|
2015-04-09 20:00:14 +02:00
|
|
|
finishFragment();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
listView.setOnItemLongClickListener(new RecyclerListView.OnItemLongClickListener() {
|
2015-04-09 20:00:14 +02:00
|
|
|
@Override
|
2017-03-31 01:58:05 +02:00
|
|
|
public boolean onItemClick(View view, int position) {
|
2015-04-09 20:00:14 +02:00
|
|
|
LocaleController.LocaleInfo localeInfo = null;
|
|
|
|
if (searching && searchWas) {
|
2017-03-31 01:58:05 +02:00
|
|
|
if (position >= 0 && position < searchResult.size()) {
|
|
|
|
localeInfo = searchResult.get(position);
|
2014-03-25 01:25:32 +01:00
|
|
|
}
|
2015-04-09 20:00:14 +02:00
|
|
|
} else {
|
2017-03-31 01:58:05 +02:00
|
|
|
if (position >= 0 && position < LocaleController.getInstance().sortedLanguages.size()) {
|
|
|
|
localeInfo = LocaleController.getInstance().sortedLanguages.get(position);
|
2014-04-02 19:36:57 +02:00
|
|
|
}
|
2014-03-25 01:25:32 +01:00
|
|
|
}
|
2015-04-09 20:00:14 +02:00
|
|
|
if (localeInfo == null || localeInfo.pathToFile == null || getParentActivity() == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
final LocaleController.LocaleInfo finalLocaleInfo = localeInfo;
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
|
|
|
|
builder.setMessage(LocaleController.getString("DeleteLocalization", R.string.DeleteLocalization));
|
|
|
|
builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
|
|
|
|
builder.setPositiveButton(LocaleController.getString("Delete", R.string.Delete), new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialogInterface, int i) {
|
|
|
|
if (LocaleController.getInstance().deleteLanguage(finalLocaleInfo)) {
|
|
|
|
if (searchResult != null) {
|
|
|
|
searchResult.remove(finalLocaleInfo);
|
|
|
|
}
|
|
|
|
if (listAdapter != null) {
|
|
|
|
listAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
if (searchListViewAdapter != null) {
|
|
|
|
searchListViewAdapter.notifyDataSetChanged();
|
2014-04-02 19:36:57 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-25 00:47:05 +02:00
|
|
|
}
|
2015-04-09 20:00:14 +02:00
|
|
|
});
|
|
|
|
builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
|
2015-05-21 23:27:27 +02:00
|
|
|
showDialog(builder.create());
|
2015-04-09 20:00:14 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2014-05-25 00:47:05 +02: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) {
|
|
|
|
if (newState == RecyclerView.SCROLL_STATE_DRAGGING && searching && searchWas) {
|
2015-04-09 20:00:14 +02:00
|
|
|
AndroidUtilities.hideKeyboard(getParentActivity().getCurrentFocus());
|
2014-05-25 00:47:05 +02:00
|
|
|
}
|
2014-03-25 01:25:32 +01:00
|
|
|
}
|
2015-04-09 20:00:14 +02:00
|
|
|
});
|
|
|
|
|
2014-03-25 01:25:32 +01:00
|
|
|
return fragmentView;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
2014-06-04 18:00:42 +02:00
|
|
|
super.onResume();
|
2014-06-04 01:31:48 +02:00
|
|
|
if (listAdapter != null) {
|
2014-03-25 01:25:32 +01:00
|
|
|
listAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void search(final String query) {
|
|
|
|
if (query == null) {
|
|
|
|
searchResult = null;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
if (searchTimer != null) {
|
|
|
|
searchTimer.cancel();
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2017-03-31 01:58:05 +02:00
|
|
|
FileLog.e(e);
|
2014-03-25 01:25:32 +01:00
|
|
|
}
|
|
|
|
searchTimer = new Timer();
|
|
|
|
searchTimer.schedule(new TimerTask() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
searchTimer.cancel();
|
|
|
|
searchTimer = null;
|
|
|
|
} catch (Exception e) {
|
2017-03-31 01:58:05 +02:00
|
|
|
FileLog.e(e);
|
2014-03-25 01:25:32 +01:00
|
|
|
}
|
|
|
|
processSearch(query);
|
|
|
|
}
|
|
|
|
}, 100, 300);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void processSearch(final String query) {
|
2014-06-13 12:42:21 +02:00
|
|
|
Utilities.searchQueue.postRunnable(new Runnable() {
|
2014-03-25 01:25:32 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
String q = query.trim().toLowerCase();
|
|
|
|
if (q.length() == 0) {
|
|
|
|
updateSearchResults(new ArrayList<LocaleController.LocaleInfo>());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
long time = System.currentTimeMillis();
|
2015-02-26 02:32:51 +01:00
|
|
|
ArrayList<LocaleController.LocaleInfo> resultArray = new ArrayList<>();
|
2014-03-25 01:25:32 +01:00
|
|
|
|
|
|
|
for (LocaleController.LocaleInfo c : LocaleController.getInstance().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) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-03-25 01:25:32 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
searchResult = arrCounties;
|
|
|
|
searchListViewAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
private class ListAdapter extends RecyclerListView.SelectionAdapter {
|
|
|
|
|
2014-03-25 01:25:32 +01:00
|
|
|
private Context mContext;
|
2017-03-31 01:58:05 +02:00
|
|
|
private boolean search;
|
2014-03-25 01:25:32 +01:00
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
public ListAdapter(Context context, boolean isSearch) {
|
2014-03-25 01:25:32 +01:00
|
|
|
mContext = context;
|
2017-03-31 01:58:05 +02:00
|
|
|
search = isSearch;
|
2014-03-25 01:25:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-31 01:58:05 +02:00
|
|
|
public boolean isEnabled(RecyclerView.ViewHolder holder) {
|
2014-03-25 01:25:32 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-31 01:58:05 +02:00
|
|
|
public int getItemCount() {
|
|
|
|
if (search) {
|
|
|
|
if (searchResult == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return searchResult.size();
|
|
|
|
} else {
|
|
|
|
if (LocaleController.getInstance().sortedLanguages == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return LocaleController.getInstance().sortedLanguages.size();
|
2014-03-25 01:25:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-31 01:58:05 +02:00
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
|
|
return new RecyclerListView.Holder(new TextSettingsCell(mContext));
|
2014-03-25 01:25:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-31 01:58:05 +02:00
|
|
|
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
|
|
|
|
TextSettingsCell textSettingsCell = (TextSettingsCell) holder.itemView;
|
|
|
|
LocaleController.LocaleInfo localeInfo;
|
|
|
|
boolean last;
|
|
|
|
if (search) {
|
|
|
|
localeInfo = searchResult.get(position);
|
|
|
|
last = position == searchResult.size() - 1;
|
|
|
|
} else {
|
|
|
|
localeInfo = LocaleController.getInstance().sortedLanguages.get(position);
|
|
|
|
last = position == LocaleController.getInstance().sortedLanguages.size() - 1;
|
2014-03-25 01:25:32 +01:00
|
|
|
}
|
2017-03-31 01:58:05 +02:00
|
|
|
textSettingsCell.setText(localeInfo.name, !last);
|
2014-03-25 01:25:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int i) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
@Override
|
|
|
|
public ThemeDescription[] getThemeDescriptions() {
|
|
|
|
return new ThemeDescription[]{
|
|
|
|
new ThemeDescription(fragmentView, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_windowBackgroundWhite),
|
2014-03-25 01:25:32 +01:00
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
new ThemeDescription(actionBar, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_actionBarDefault),
|
|
|
|
new ThemeDescription(listView, ThemeDescription.FLAG_LISTGLOWCOLOR, null, null, null, null, Theme.key_actionBarDefault),
|
|
|
|
new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_ITEMSCOLOR, null, null, null, null, Theme.key_actionBarDefaultIcon),
|
|
|
|
new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_TITLECOLOR, null, null, null, null, Theme.key_actionBarDefaultTitle),
|
|
|
|
new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SELECTORCOLOR, null, null, null, null, Theme.key_actionBarDefaultSelector),
|
|
|
|
new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SEARCH, null, null, null, null, Theme.key_actionBarDefaultSearch),
|
|
|
|
new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SEARCHPLACEHOLDER, null, null, null, null, Theme.key_actionBarDefaultSearchPlaceholder),
|
2014-03-25 01:25:32 +01:00
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
new ThemeDescription(listView, ThemeDescription.FLAG_SELECTOR, null, null, null, null, Theme.key_listSelector),
|
2014-03-25 01:25:32 +01:00
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
new ThemeDescription(emptyView, ThemeDescription.FLAG_TEXTCOLOR, null, null, null, null, Theme.key_emptyListPlaceholder),
|
2014-03-25 01:25:32 +01:00
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
new ThemeDescription(listView, 0, new Class[]{View.class}, Theme.dividerPaint, null, null, Theme.key_divider),
|
2014-03-25 01:25:32 +01:00
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
new ThemeDescription(listView, 0, new Class[]{TextSettingsCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText),
|
|
|
|
};
|
2014-03-25 01:25:32 +01:00
|
|
|
}
|
|
|
|
}
|