2013-10-25 15:19:00 +00:00
|
|
|
/*
|
2013-12-20 19:25:49 +00:00
|
|
|
* This is the source code of Telegram for Android v. 1.3.2.
|
2013-10-25 15:19:00 +00:00
|
|
|
* 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;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.Canvas;
|
2014-05-16 23:05:49 +00:00
|
|
|
import android.graphics.Paint;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
2014-05-16 23:05:49 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
2013-12-20 19:25:49 +00:00
|
|
|
import android.support.v4.internal.view.SupportMenuItem;
|
|
|
|
import android.support.v7.app.ActionBar;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.text.Editable;
|
|
|
|
import android.text.Spannable;
|
|
|
|
import android.text.SpannableString;
|
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
import android.text.TextWatcher;
|
|
|
|
import android.text.style.ImageSpan;
|
|
|
|
import android.view.LayoutInflater;
|
2013-12-20 19:25:49 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2014-03-24 21:10:35 +00:00
|
|
|
import org.telegram.PhoneFormat.PhoneFormat;
|
2014-03-22 22:31:55 +00:00
|
|
|
import org.telegram.messenger.LocaleController;
|
2014-02-28 22:28:25 +00:00
|
|
|
import org.telegram.messenger.TLRPC;
|
2013-10-25 15:19:00 +00:00
|
|
|
import org.telegram.messenger.ConnectionsManager;
|
2014-02-04 18:36:55 +00:00
|
|
|
import org.telegram.messenger.ContactsController;
|
2013-12-20 19:25:49 +00:00
|
|
|
import org.telegram.messenger.FileLog;
|
2013-10-25 15:19:00 +00:00
|
|
|
import org.telegram.messenger.MessagesController;
|
|
|
|
import org.telegram.messenger.NotificationCenter;
|
|
|
|
import org.telegram.messenger.R;
|
|
|
|
import org.telegram.messenger.UserConfig;
|
|
|
|
import org.telegram.messenger.Utilities;
|
2014-02-04 18:36:55 +00:00
|
|
|
import org.telegram.ui.Views.BackupImageView;
|
2013-10-25 15:19:00 +00:00
|
|
|
import org.telegram.ui.Views.BaseFragment;
|
|
|
|
import org.telegram.ui.Views.PinnedHeaderListView;
|
|
|
|
import org.telegram.ui.Views.SectionedBaseAdapter;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Timer;
|
|
|
|
import java.util.TimerTask;
|
|
|
|
|
|
|
|
public class GroupCreateActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate {
|
2014-05-16 23:05:49 +00:00
|
|
|
|
|
|
|
public static class XImageSpan extends ImageSpan {
|
|
|
|
public int uid;
|
|
|
|
|
|
|
|
public XImageSpan(Drawable d, int verticalAlignment) {
|
|
|
|
super(d, verticalAlignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
|
|
|
|
if (fm == null) {
|
|
|
|
fm = new Paint.FontMetricsInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
int sz = super.getSize(paint, text, start, end, fm);
|
|
|
|
|
|
|
|
int offset = Utilities.dp(6);
|
|
|
|
int w = (fm.bottom - fm.top) / 2;
|
|
|
|
fm.top = -w - offset;
|
|
|
|
fm.bottom = w - offset;
|
|
|
|
fm.ascent = -w - offset;
|
|
|
|
fm.leading = 0;
|
|
|
|
fm.descent = w - offset;
|
|
|
|
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
private SectionedBaseAdapter listViewAdapter;
|
|
|
|
private PinnedHeaderListView listView;
|
2014-03-25 00:25:32 +00:00
|
|
|
private TextView emptyTextView;
|
2013-10-25 15:19:00 +00:00
|
|
|
private EditText userSelectEditText;
|
|
|
|
private boolean ignoreChange = false;
|
|
|
|
|
2014-05-16 23:05:49 +00:00
|
|
|
private HashMap<Integer, XImageSpan> selectedContacts = new HashMap<Integer, XImageSpan>();
|
|
|
|
private ArrayList<XImageSpan> allSpans = new ArrayList<XImageSpan>();
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
private boolean searchWas;
|
|
|
|
private boolean searching;
|
2014-03-25 00:25:32 +00:00
|
|
|
private Timer searchTimer;
|
2013-10-25 15:19:00 +00:00
|
|
|
public ArrayList<TLRPC.User> searchResult;
|
|
|
|
public ArrayList<CharSequence> searchResultNames;
|
|
|
|
|
|
|
|
private CharSequence changeString;
|
|
|
|
private int beforeChangeIndex;
|
|
|
|
|
|
|
|
public GroupCreateActivity() {
|
|
|
|
animationType = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onFragmentCreate() {
|
|
|
|
super.onFragmentCreate();
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().addObserver(this, MessagesController.contactsDidLoaded);
|
|
|
|
NotificationCenter.getInstance().addObserver(this, MessagesController.updateInterfaces);
|
|
|
|
NotificationCenter.getInstance().addObserver(this, MessagesController.chatDidCreated);
|
2013-10-25 15:19:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFragmentDestroy() {
|
|
|
|
super.onFragmentDestroy();
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().removeObserver(this, MessagesController.contactsDidLoaded);
|
|
|
|
NotificationCenter.getInstance().removeObserver(this, MessagesController.updateInterfaces);
|
|
|
|
NotificationCenter.getInstance().removeObserver(this, MessagesController.chatDidCreated);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setHasOptionsMenu(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
if (fragmentView == null) {
|
2014-02-28 22:28:25 +00:00
|
|
|
|
|
|
|
searching = false;
|
|
|
|
searchWas = false;
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
fragmentView = inflater.inflate(R.layout.group_create_layout, container, false);
|
|
|
|
|
2014-03-25 00:25:32 +00:00
|
|
|
emptyTextView = (TextView)fragmentView.findViewById(R.id.searchEmptyView);
|
|
|
|
emptyTextView.setText(LocaleController.getString("NoContacts", R.string.NoContacts));
|
2013-10-25 15:19:00 +00:00
|
|
|
userSelectEditText = (EditText)fragmentView.findViewById(R.id.bubble_input_text);
|
2014-03-22 22:31:55 +00:00
|
|
|
userSelectEditText.setHint(LocaleController.getString("SendMessageTo", R.string.SendMessageTo));
|
2013-10-25 15:19:00 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= 11) {
|
|
|
|
userSelectEditText.setTextIsSelectable(false);
|
|
|
|
}
|
|
|
|
userSelectEditText.addTextChangedListener(new TextWatcher() {
|
|
|
|
@Override
|
|
|
|
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
|
|
|
|
if (!ignoreChange) {
|
|
|
|
beforeChangeIndex = userSelectEditText.getSelectionStart();
|
|
|
|
changeString = new SpannableString(charSequence);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable editable) {
|
|
|
|
if (!ignoreChange) {
|
|
|
|
boolean search = false;
|
|
|
|
int afterChangeIndex = userSelectEditText.getSelectionEnd();
|
|
|
|
if (editable.toString().length() < changeString.toString().length()) {
|
|
|
|
String deletedString = "";
|
|
|
|
try {
|
|
|
|
deletedString = changeString.toString().substring(afterChangeIndex, beforeChangeIndex);
|
|
|
|
} catch (Exception e) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", e);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (deletedString.length() > 0) {
|
|
|
|
if (searching && searchWas) {
|
|
|
|
search = true;
|
|
|
|
}
|
|
|
|
Spannable span = userSelectEditText.getText();
|
|
|
|
for (int a = 0; a < allSpans.size(); a++) {
|
2014-05-16 23:05:49 +00:00
|
|
|
XImageSpan sp = allSpans.get(a);
|
2013-10-25 15:19:00 +00:00
|
|
|
if (span.getSpanStart(sp) == -1) {
|
|
|
|
allSpans.remove(sp);
|
|
|
|
selectedContacts.remove(sp.uid);
|
|
|
|
}
|
|
|
|
}
|
2014-03-24 21:10:35 +00:00
|
|
|
if (parentActivity != null) {
|
|
|
|
ActionBar actionBar = parentActivity.getSupportActionBar();
|
|
|
|
actionBar.setSubtitle(String.format("%d/200 %s", selectedContacts.size(), LocaleController.getString("Members", R.string.Members)));
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
listView.invalidateViews();
|
|
|
|
} else {
|
|
|
|
search = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
search = true;
|
|
|
|
}
|
|
|
|
if (search) {
|
|
|
|
String text = userSelectEditText.getText().toString().replace("<", "");
|
|
|
|
if (text.length() != 0) {
|
|
|
|
searchDialogs(text);
|
|
|
|
searching = true;
|
|
|
|
searchWas = true;
|
2014-03-25 00:25:32 +00:00
|
|
|
emptyTextView.setText(LocaleController.getString("NoResult", R.string.NoResult));
|
2013-10-25 15:19:00 +00:00
|
|
|
listViewAdapter.notifyDataSetChanged();
|
|
|
|
} else {
|
|
|
|
searchResult = null;
|
|
|
|
searchResultNames = null;
|
|
|
|
searching = false;
|
|
|
|
searchWas = false;
|
2014-03-25 00:25:32 +00:00
|
|
|
emptyTextView.setText(LocaleController.getString("NoContacts", R.string.NoContacts));
|
2013-10-25 15:19:00 +00:00
|
|
|
listViewAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
listView = (PinnedHeaderListView)fragmentView.findViewById(R.id.listView);
|
2014-03-25 00:25:32 +00:00
|
|
|
listView.setEmptyView(emptyTextView);
|
2013-10-25 15:19:00 +00:00
|
|
|
listView.setVerticalScrollBarEnabled(false);
|
|
|
|
|
|
|
|
listView.setAdapter(listViewAdapter = new ListAdapter(parentActivity));
|
|
|
|
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
|
|
|
TLRPC.User user;
|
|
|
|
int section = listViewAdapter.getSectionForPosition(i);
|
|
|
|
int row = listViewAdapter.getPositionInSectionForPosition(i);
|
|
|
|
if (searching && searchWas) {
|
|
|
|
user = searchResult.get(row);
|
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
ArrayList<TLRPC.TL_contact> arr = ContactsController.getInstance().usersSectionsDict.get(ContactsController.getInstance().sortedUsersSectionsArray.get(section));
|
|
|
|
user = MessagesController.getInstance().users.get(arr.get(row).user_id);
|
2013-10-25 15:19:00 +00:00
|
|
|
listView.invalidateViews();
|
|
|
|
}
|
|
|
|
if (selectedContacts.containsKey(user.id)) {
|
2014-05-16 23:05:49 +00:00
|
|
|
XImageSpan span = selectedContacts.get(user.id);
|
2013-10-25 15:19:00 +00:00
|
|
|
selectedContacts.remove(user.id);
|
|
|
|
SpannableStringBuilder text = new SpannableStringBuilder(userSelectEditText.getText());
|
|
|
|
text.delete(text.getSpanStart(span), text.getSpanEnd(span));
|
|
|
|
allSpans.remove(span);
|
|
|
|
ignoreChange = true;
|
|
|
|
userSelectEditText.setText(text);
|
|
|
|
userSelectEditText.setSelection(text.length());
|
|
|
|
ignoreChange = false;
|
|
|
|
} else {
|
2013-11-04 12:31:01 +00:00
|
|
|
if (selectedContacts.size() == 200) {
|
2013-10-25 15:19:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ignoreChange = true;
|
2014-05-16 23:05:49 +00:00
|
|
|
XImageSpan span = createAndPutChipForUser(user);
|
2013-10-25 15:19:00 +00:00
|
|
|
span.uid = user.id;
|
|
|
|
ignoreChange = false;
|
|
|
|
}
|
2014-03-24 21:10:35 +00:00
|
|
|
if (parentActivity != null) {
|
|
|
|
ActionBar actionBar = parentActivity.getSupportActionBar();
|
|
|
|
actionBar.setSubtitle(String.format("%d/200 %s", selectedContacts.size(), LocaleController.getString("Members", R.string.Members)));
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (searching || searchWas) {
|
|
|
|
searching = false;
|
|
|
|
searchWas = false;
|
2014-03-25 00:25:32 +00:00
|
|
|
emptyTextView.setText(LocaleController.getString("NoContacts", R.string.NoContacts));
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
ignoreChange = true;
|
|
|
|
SpannableStringBuilder ssb = new SpannableStringBuilder("");
|
|
|
|
for (ImageSpan sp : allSpans) {
|
|
|
|
ssb.append("<<");
|
|
|
|
ssb.setSpan(sp, ssb.length() - 2, ssb.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
}
|
|
|
|
userSelectEditText.setText(ssb);
|
|
|
|
userSelectEditText.setSelection(ssb.length());
|
|
|
|
ignoreChange = false;
|
|
|
|
|
|
|
|
listViewAdapter.notifyDataSetChanged();
|
|
|
|
} else {
|
|
|
|
listView.invalidateViews();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
ViewGroup parent = (ViewGroup)fragmentView.getParent();
|
|
|
|
if (parent != null) {
|
|
|
|
parent.removeView(fragmentView);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fragmentView;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void applySelfActionBar() {
|
|
|
|
if (parentActivity == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ActionBar actionBar = parentActivity.getSupportActionBar();
|
|
|
|
actionBar.setDisplayShowTitleEnabled(true);
|
|
|
|
actionBar.setDisplayShowHomeEnabled(false);
|
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
actionBar.setDisplayUseLogoEnabled(false);
|
|
|
|
actionBar.setDisplayShowCustomEnabled(false);
|
|
|
|
actionBar.setCustomView(null);
|
2014-03-22 22:31:55 +00:00
|
|
|
actionBar.setTitle(LocaleController.getString("NewGroup", R.string.NewGroup));
|
2014-03-24 21:10:35 +00:00
|
|
|
actionBar.setSubtitle(String.format("%d/200 %s", selectedContacts.size(), LocaleController.getString("Members", R.string.Members)));
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
TextView title = (TextView)parentActivity.findViewById(R.id.action_bar_title);
|
2013-10-25 15:19:00 +00:00
|
|
|
if (title == null) {
|
|
|
|
final int subtitleId = parentActivity.getResources().getIdentifier("action_bar_title", "id", "android");
|
|
|
|
title = (TextView)parentActivity.findViewById(subtitleId);
|
|
|
|
}
|
|
|
|
if (title != null) {
|
|
|
|
title.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
|
|
|
|
title.setCompoundDrawablePadding(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
2013-12-20 19:25:49 +00:00
|
|
|
if (getActivity() == null) {
|
2013-10-25 15:19:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-03-04 20:04:57 +00:00
|
|
|
((LaunchActivity)parentActivity).showActionBar();
|
|
|
|
((LaunchActivity)parentActivity).updateActionBar();
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 23:05:49 +00:00
|
|
|
public XImageSpan createAndPutChipForUser(TLRPC.User user) {
|
2013-10-25 15:19:00 +00:00
|
|
|
LayoutInflater lf = (LayoutInflater)parentActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
|
|
|
|
View textView = lf.inflate(R.layout.group_create_bubble, null);
|
|
|
|
TextView text = (TextView)textView.findViewById(R.id.bubble_text_view);
|
2014-03-24 21:10:35 +00:00
|
|
|
String name = Utilities.formatName(user.first_name, user.last_name);
|
|
|
|
if (name.length() == 0 && user.phone != null && user.phone.length() != 0) {
|
|
|
|
name = PhoneFormat.getInstance().format("+" + user.phone);
|
|
|
|
}
|
|
|
|
text.setText(name + ", ");
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
|
|
|
|
textView.measure(spec, spec);
|
|
|
|
textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
|
|
|
|
Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888);
|
|
|
|
Canvas canvas = new Canvas(b);
|
|
|
|
canvas.translate(-textView.getScrollX(), -textView.getScrollY());
|
|
|
|
textView.draw(canvas);
|
|
|
|
textView.setDrawingCacheEnabled(true);
|
|
|
|
Bitmap cacheBmp = textView.getDrawingCache();
|
|
|
|
Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
|
|
|
|
textView.destroyDrawingCache();
|
|
|
|
|
|
|
|
final BitmapDrawable bmpDrawable = new BitmapDrawable(b);
|
|
|
|
bmpDrawable.setBounds(0, 0, b.getWidth(), b.getHeight());
|
|
|
|
|
|
|
|
SpannableStringBuilder ssb = new SpannableStringBuilder("");
|
2014-05-16 23:05:49 +00:00
|
|
|
XImageSpan span = new XImageSpan(bmpDrawable, ImageSpan.ALIGN_BASELINE);
|
2013-10-25 15:19:00 +00:00
|
|
|
allSpans.add(span);
|
|
|
|
selectedContacts.put(user.id, span);
|
|
|
|
for (ImageSpan sp : allSpans) {
|
|
|
|
ssb.append("<<");
|
|
|
|
ssb.setSpan(sp, ssb.length() - 2, ssb.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
}
|
|
|
|
userSelectEditText.setText(ssb);
|
|
|
|
userSelectEditText.setSelection(ssb.length());
|
|
|
|
return span;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void searchDialogs(final String query) {
|
|
|
|
if (query == null) {
|
|
|
|
searchResult = null;
|
|
|
|
searchResultNames = null;
|
|
|
|
} else {
|
|
|
|
try {
|
2014-03-25 00:25:32 +00:00
|
|
|
if (searchTimer != null) {
|
|
|
|
searchTimer.cancel();
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", e);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-03-25 00:25:32 +00:00
|
|
|
searchTimer = new Timer();
|
|
|
|
searchTimer.schedule(new TimerTask() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2014-03-25 00:25:32 +00:00
|
|
|
searchTimer.cancel();
|
|
|
|
searchTimer = null;
|
2013-10-25 15:19:00 +00:00
|
|
|
} catch (Exception e) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", e);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
processSearch(query);
|
|
|
|
}
|
|
|
|
}, 100, 300);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void processSearch(final String query) {
|
2014-03-10 09:27:49 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-10 09:27:49 +00:00
|
|
|
final ArrayList<TLRPC.TL_contact> contactsCopy = new ArrayList<TLRPC.TL_contact>();
|
2014-03-22 22:31:55 +00:00
|
|
|
contactsCopy.addAll(ContactsController.getInstance().contacts);
|
2014-03-10 09:27:49 +00:00
|
|
|
Utilities.globalQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (query.length() == 0) {
|
|
|
|
updateSearchResults(new ArrayList<TLRPC.User>(), new ArrayList<CharSequence>());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
long time = System.currentTimeMillis();
|
|
|
|
ArrayList<TLRPC.User> resultArray = new ArrayList<TLRPC.User>();
|
|
|
|
ArrayList<CharSequence> resultArrayNames = new ArrayList<CharSequence>();
|
|
|
|
String q = query.toLowerCase();
|
|
|
|
|
|
|
|
for (TLRPC.TL_contact contact : contactsCopy) {
|
2014-03-22 22:31:55 +00:00
|
|
|
TLRPC.User user = MessagesController.getInstance().users.get(contact.user_id);
|
2014-03-10 09:27:49 +00:00
|
|
|
if (user.first_name.toLowerCase().startsWith(q) || user.last_name.toLowerCase().startsWith(q)) {
|
|
|
|
if (user.id == UserConfig.clientUserId) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
resultArrayNames.add(Utilities.generateSearchName(user.first_name, user.last_name, q));
|
|
|
|
resultArray.add(user);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 09:27:49 +00:00
|
|
|
updateSearchResults(resultArray, resultArrayNames);
|
|
|
|
}
|
|
|
|
});
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateSearchResults(final ArrayList<TLRPC.User> users, final ArrayList<CharSequence> names) {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
searchResult = users;
|
|
|
|
searchResultNames = names;
|
|
|
|
listViewAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
int itemId = item.getItemId();
|
|
|
|
switch (itemId) {
|
|
|
|
case android.R.id.home:
|
|
|
|
finishFragment();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
2014-04-02 17:36:57 +00:00
|
|
|
SupportMenuItem doneItem = (SupportMenuItem)menu.add(Menu.NONE, 0, Menu.NONE, null);
|
|
|
|
doneItem.setShowAsAction(SupportMenuItem.SHOW_AS_ACTION_ALWAYS);
|
|
|
|
doneItem.setActionView(R.layout.group_create_done_layout);
|
|
|
|
|
2014-03-24 21:10:35 +00:00
|
|
|
TextView doneTextView = (TextView)doneItem.getActionView().findViewById(R.id.done_button);
|
|
|
|
doneTextView.setText(LocaleController.getString("Next", R.string.Next));
|
2013-10-25 15:19:00 +00:00
|
|
|
doneTextView.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
if (!selectedContacts.isEmpty()) {
|
|
|
|
ArrayList<Integer> result = new ArrayList<Integer>();
|
|
|
|
result.addAll(selectedContacts.keySet());
|
2014-03-26 20:16:28 +00:00
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putIntegerArrayList("result", result);
|
|
|
|
GroupCreateFinalActivity fragment = new GroupCreateFinalActivity();
|
|
|
|
fragment.setArguments(args);
|
|
|
|
((LaunchActivity)parentActivity).presentFragment(fragment, "group_craate_final", false);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void didReceivedNotification(int id, Object... args) {
|
|
|
|
if (id == MessagesController.contactsDidLoaded) {
|
|
|
|
if (listViewAdapter != null) {
|
|
|
|
listViewAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
} else if (id == MessagesController.updateInterfaces) {
|
2014-02-04 18:36:55 +00:00
|
|
|
int mask = (Integer)args[0];
|
|
|
|
if ((mask & MessagesController.UPDATE_MASK_AVATAR) != 0 || (mask & MessagesController.UPDATE_MASK_NAME) != 0 || (mask & MessagesController.UPDATE_MASK_STATUS) != 0) {
|
|
|
|
if (listView != null) {
|
|
|
|
listView.invalidateViews();
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
} else if (id == MessagesController.chatDidCreated) {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
removeSelfFromStack();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ListAdapter extends SectionedBaseAdapter {
|
|
|
|
private Context mContext;
|
|
|
|
|
|
|
|
public ListAdapter(Context context) {
|
|
|
|
mContext = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object getItem(int section, int position) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getItemId(int section, int position) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSectionCount() {
|
|
|
|
if (searching && searchWas) {
|
|
|
|
return searchResult == null || searchResult.isEmpty() ? 0 : 1;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
return ContactsController.getInstance().sortedUsersSectionsArray.size();
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCountForSection(int section) {
|
|
|
|
if (searching && searchWas) {
|
|
|
|
return searchResult == null ? 0 : searchResult.size();
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
ArrayList<TLRPC.TL_contact> arr = ContactsController.getInstance().usersSectionsDict.get(ContactsController.getInstance().sortedUsersSectionsArray.get(section));
|
2013-10-25 15:19:00 +00:00
|
|
|
return arr.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getItemView(int section, int position, View convertView, ViewGroup parent) {
|
|
|
|
TLRPC.User user;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if (searchWas && searching) {
|
2014-03-22 22:31:55 +00:00
|
|
|
user = MessagesController.getInstance().users.get(searchResult.get(position).id);
|
2013-10-25 15:19:00 +00:00
|
|
|
size = searchResult.size();
|
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
ArrayList<TLRPC.TL_contact> arr = ContactsController.getInstance().usersSectionsDict.get(ContactsController.getInstance().sortedUsersSectionsArray.get(section));
|
|
|
|
user = MessagesController.getInstance().users.get(arr.get(position).user_id);
|
2013-10-25 15:19:00 +00:00
|
|
|
size = arr.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (convertView == null) {
|
|
|
|
LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
convertView = li.inflate(R.layout.group_create_row_layout, parent, false);
|
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
ContactListRowHolder holder = (ContactListRowHolder)convertView.getTag();
|
2013-10-25 15:19:00 +00:00
|
|
|
if (holder == null) {
|
2014-02-04 18:36:55 +00:00
|
|
|
holder = new ContactListRowHolder(convertView);
|
2013-10-25 15:19:00 +00:00
|
|
|
convertView.setTag(holder);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageView checkButton = (ImageView)convertView.findViewById(R.id.settings_row_check_button);
|
|
|
|
if (selectedContacts.containsKey(user.id)) {
|
2014-03-24 21:10:35 +00:00
|
|
|
checkButton.setImageResource(R.drawable.btn_check_on_holo_light);
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
2014-03-24 21:10:35 +00:00
|
|
|
checkButton.setImageResource(R.drawable.btn_check_off_holo_light);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
View divider = convertView.findViewById(R.id.settings_row_divider);
|
|
|
|
if (position == size - 1) {
|
|
|
|
divider.setVisibility(View.INVISIBLE);
|
|
|
|
} else {
|
|
|
|
divider.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (searchWas && searching) {
|
|
|
|
holder.nameTextView.setText(searchResultNames.get(position));
|
|
|
|
} else {
|
2014-03-24 21:10:35 +00:00
|
|
|
String name = Utilities.formatName(user.first_name, user.last_name);
|
|
|
|
if (name.length() == 0) {
|
|
|
|
if (user.phone != null && user.phone.length() != 0) {
|
|
|
|
name = PhoneFormat.getInstance().format("+" + user.phone);
|
|
|
|
} else {
|
|
|
|
name = LocaleController.getString("HiddenName", R.string.HiddenName);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-03-24 21:10:35 +00:00
|
|
|
holder.nameTextView.setText(name);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TLRPC.FileLocation photo = null;
|
|
|
|
if (user.photo != null) {
|
|
|
|
photo = user.photo.photo_small;
|
|
|
|
}
|
|
|
|
int placeHolderId = Utilities.getUserAvatarForId(user.id);
|
|
|
|
holder.avatarImage.setImage(photo, "50_50", placeHolderId);
|
|
|
|
|
2014-05-16 23:05:49 +00:00
|
|
|
holder.messageTextView.setText(LocaleController.formatUserStatus(user));
|
|
|
|
if (user.status != null && user.status.expires > ConnectionsManager.getInstance().getCurrentTime()) {
|
|
|
|
holder.messageTextView.setTextColor(0xff357aa8);
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
2014-05-16 23:05:49 +00:00
|
|
|
holder.messageTextView.setTextColor(0xff808080);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return convertView;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int section, int position) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewTypeCount() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSectionHeaderViewType(int section) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSectionHeaderViewTypeCount() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
|
|
|
|
if (convertView == null) {
|
|
|
|
LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
convertView = li.inflate(R.layout.settings_section_layout, parent, false);
|
|
|
|
convertView.setBackgroundColor(0xffffffff);
|
|
|
|
}
|
|
|
|
TextView textView = (TextView)convertView.findViewById(R.id.settings_section_text);
|
|
|
|
if (searching && searchWas) {
|
2014-03-22 22:31:55 +00:00
|
|
|
textView.setText(LocaleController.getString("AllContacts", R.string.AllContacts));
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
textView.setText(ContactsController.getInstance().sortedUsersSectionsArray.get(section));
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
return convertView;
|
|
|
|
}
|
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
|
|
|
|
public static class ContactListRowHolder {
|
|
|
|
public BackupImageView avatarImage;
|
|
|
|
public TextView messageTextView;
|
|
|
|
public TextView nameTextView;
|
|
|
|
|
|
|
|
public ContactListRowHolder(View view) {
|
|
|
|
messageTextView = (TextView)view.findViewById(R.id.messages_list_row_message);
|
|
|
|
nameTextView = (TextView)view.findViewById(R.id.messages_list_row_name);
|
|
|
|
avatarImage = (BackupImageView)view.findViewById(R.id.messages_list_row_avatar);
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|