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

556 lines
24 KiB
Java
Raw Normal View History

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;
2013-10-25 15:19:00 +00:00
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
2013-10-25 15:19:00 +00:00
import android.net.Uri;
import android.os.Bundle;
2013-12-20 19:25:49 +00:00
import android.support.v4.internal.view.SupportMenuItem;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.SearchView;
2013-10-25 15:19:00 +00:00
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.ImageView;
import android.widget.TextView;
import org.telegram.messenger.TLObject;
import org.telegram.messenger.TLRPC;
import org.telegram.messenger.ConnectionsManager;
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.RPCRequest;
2013-10-25 15:19:00 +00:00
import org.telegram.messenger.UserConfig;
import org.telegram.messenger.Utilities;
import org.telegram.ui.Adapters.ContactsActivityAdapter;
import org.telegram.ui.Adapters.ContactsActivitySearchAdapter;
import org.telegram.ui.Cells.ChatOrUserCell;
2013-10-25 15:19:00 +00:00
import org.telegram.ui.Views.BaseFragment;
import org.telegram.ui.Views.OnSwipeTouchListener;
import org.telegram.ui.Views.PinnedHeaderListView;
import org.telegram.ui.Views.SectionedBaseAdapter;
2013-12-20 19:25:49 +00:00
import java.lang.reflect.Field;
2013-10-25 15:19:00 +00:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
2013-10-25 15:19:00 +00:00
public class ContactsActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate {
private SectionedBaseAdapter listViewAdapter;
private PinnedHeaderListView listView;
private ContactsActivitySearchAdapter searchListViewAdapter;
2013-10-25 15:19:00 +00:00
private boolean searchWas;
private boolean searching;
private boolean onlyUsers;
private boolean usersAsSections;
private boolean destroyAfterSelect;
private boolean returnAsResult;
private boolean createSecretChat;
private boolean creatingChat = false;
public int selectAlertString = 0;
private SearchView searchView;
private TextView epmtyTextView;
private HashMap<Integer, TLRPC.User> ignoreUsers;
2013-12-20 19:25:49 +00:00
private SupportMenuItem searchItem;
2013-10-25 15:19:00 +00:00
private String inviteText;
private boolean updatingInviteText = false;
2013-10-25 15:19:00 +00:00
public ContactsActivityDelegate delegate;
public static interface ContactsActivityDelegate {
public abstract void didSelectContact(TLRPC.User user);
2013-10-25 15:19:00 +00:00
}
@SuppressWarnings("unchecked")
@Override
public boolean onFragmentCreate() {
super.onFragmentCreate();
NotificationCenter.Instance.addObserver(this, MessagesController.contactsDidLoaded);
NotificationCenter.Instance.addObserver(this, MessagesController.updateInterfaces);
NotificationCenter.Instance.addObserver(this, MessagesController.encryptedChatCreated);
if (getArguments() != null) {
onlyUsers = getArguments().getBoolean("onlyUsers", false);
destroyAfterSelect = getArguments().getBoolean("destroyAfterSelect", false);
usersAsSections = getArguments().getBoolean("usersAsSections", false);
returnAsResult = getArguments().getBoolean("returnAsResult", false);
createSecretChat = getArguments().getBoolean("createSecretChat", false);
if (destroyAfterSelect) {
ignoreUsers = (HashMap<Integer, TLRPC.User>)NotificationCenter.Instance.getFromMemCache(7);
}
}
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
inviteText = preferences.getString("invitetext", null);
int time = preferences.getInt("invitetexttime", 0);
if (inviteText == null || time + 86400 < (int)(System.currentTimeMillis() / 1000)) {
updateInviteText();
}
2013-10-25 15:19:00 +00:00
return true;
}
@Override
public void onFragmentDestroy() {
super.onFragmentDestroy();
NotificationCenter.Instance.removeObserver(this, MessagesController.contactsDidLoaded);
NotificationCenter.Instance.removeObserver(this, MessagesController.updateInterfaces);
NotificationCenter.Instance.removeObserver(this, MessagesController.encryptedChatCreated);
delegate = null;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void willBeHidden() {
if (searchItem != null) {
if (searchItem.isActionViewExpanded()) {
searchItem.collapseActionView();
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (fragmentView == null) {
searching = false;
searchWas = false;
2013-10-25 15:19:00 +00:00
fragmentView = inflater.inflate(R.layout.contacts_layout, container, false);
epmtyTextView = (TextView)fragmentView.findViewById(R.id.searchEmptyView);
searchListViewAdapter = new ContactsActivitySearchAdapter(parentActivity, ignoreUsers);
2013-10-25 15:19:00 +00:00
listView = (PinnedHeaderListView)fragmentView.findViewById(R.id.listView);
listView.setEmptyView(epmtyTextView);
listView.setVerticalScrollBarEnabled(false);
listViewAdapter = new ContactsActivityAdapter(parentActivity, onlyUsers, usersAsSections, ignoreUsers);
listView.setAdapter(listViewAdapter);
2013-10-25 15:19:00 +00:00
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (searching && searchWas) {
TLRPC.User user = searchListViewAdapter.getItem(i);
if (user == null || user.id == UserConfig.clientUserId) {
2013-10-25 15:19:00 +00:00
return;
}
if (returnAsResult) {
if (ignoreUsers != null && ignoreUsers.containsKey(user.id)) {
2013-10-25 15:19:00 +00:00
return;
}
didSelectResult(user, true);
2013-10-25 15:19:00 +00:00
} else {
if (createSecretChat) {
creatingChat = true;
MessagesController.Instance.startSecretChat(parentActivity, user);
2013-10-25 15:19:00 +00:00
} else {
ChatActivity fragment = new ChatActivity();
Bundle bundle = new Bundle();
bundle.putInt("user_id", user.id);
2013-10-25 15:19:00 +00:00
fragment.setArguments(bundle);
2013-12-20 19:25:49 +00:00
((ApplicationActivity)parentActivity).presentFragment(fragment, "chat" + Math.random(), destroyAfterSelect, false);
2013-10-25 15:19:00 +00:00
}
}
} else {
int section = listViewAdapter.getSectionForPosition(i);
int row = listViewAdapter.getPositionInSectionForPosition(i);
if (row < 0 || section < 0) {
return;
}
TLRPC.User user = null;
2013-10-25 15:19:00 +00:00
if (usersAsSections) {
if (section < ContactsController.Instance.sortedUsersSectionsArray.size()) {
ArrayList<TLRPC.TL_contact> arr = ContactsController.Instance.usersSectionsDict.get(ContactsController.Instance.sortedUsersSectionsArray.get(section));
if (row < arr.size()) {
TLRPC.TL_contact contact = arr.get(row);
user = MessagesController.Instance.users.get(contact.user_id);
} else {
return;
}
2013-10-25 15:19:00 +00:00
}
} else {
if (section == 0) {
if (row == 0) {
try {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, inviteText != null ? inviteText : getStringEntry(R.string.InviteText));
2013-10-25 15:19:00 +00:00
startActivity(intent);
} catch (Exception e) {
2013-12-20 19:25:49 +00:00
FileLog.e("tmessages", e);
2013-10-25 15:19:00 +00:00
}
return;
} else {
if (row - 1 < ContactsController.Instance.contacts.size()) {
user = MessagesController.Instance.users.get(ContactsController.Instance.contacts.get(row - 1).user_id);
2013-10-25 15:19:00 +00:00
} else {
return;
}
}
}
}
if (user != null) {
if (user.id == UserConfig.clientUserId) {
2013-10-25 15:19:00 +00:00
return;
}
if (returnAsResult) {
if (ignoreUsers != null && ignoreUsers.containsKey(user.id)) {
2013-10-25 15:19:00 +00:00
return;
}
didSelectResult(user, true);
2013-10-25 15:19:00 +00:00
} else {
if (createSecretChat) {
creatingChat = true;
MessagesController.Instance.startSecretChat(parentActivity, user);
2013-10-25 15:19:00 +00:00
} else {
ChatActivity fragment = new ChatActivity();
Bundle bundle = new Bundle();
bundle.putInt("user_id", user.id);
2013-10-25 15:19:00 +00:00
fragment.setArguments(bundle);
2013-12-20 19:25:49 +00:00
((ApplicationActivity)parentActivity).presentFragment(fragment, "chat" + Math.random(), destroyAfterSelect, false);
2013-10-25 15:19:00 +00:00
}
}
} else {
ArrayList<ContactsController.Contact> arr = ContactsController.Instance.contactsSectionsDict.get(ContactsController.Instance.sortedContactsSectionsArray.get(section - 1));
ContactsController.Contact contact = arr.get(row);
2013-10-25 15:19:00 +00:00
String usePhone = null;
if (!contact.phones.isEmpty()) {
usePhone = contact.phones.get(0);
}
if (usePhone == null) {
return;
2013-10-25 15:19:00 +00:00
}
AlertDialog.Builder builder = new AlertDialog.Builder(parentActivity);
builder.setMessage(getStringEntry(R.string.InviteUser));
builder.setTitle(getStringEntry(R.string.AppName));
final String arg1 = usePhone;
builder.setPositiveButton(getStringEntry(R.string.OK), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
2013-12-20 19:25:49 +00:00
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", arg1, null));
2013-10-25 15:19:00 +00:00
intent.putExtra("sms_body", getStringEntry(R.string.InviteText));
startActivity(intent);
} catch (Exception e) {
2013-12-20 19:25:49 +00:00
FileLog.e("tmessages", e);
2013-10-25 15:19:00 +00:00
}
}
});
builder.setNegativeButton(getStringEntry(R.string.Cancel), null);
builder.show().setCanceledOnTouchOutside(true);
}
}
}
});
listView.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeRight() {
finishFragment(true);
if (searchItem != null) {
if (searchItem.isActionViewExpanded()) {
searchItem.collapseActionView();
}
}
}
});
epmtyTextView.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeRight() {
finishFragment(true);
if (searchItem != null) {
if (searchItem.isActionViewExpanded()) {
searchItem.collapseActionView();
}
}
}
});
} else {
ViewGroup parent = (ViewGroup)fragmentView.getParent();
if (parent != null) {
parent.removeView(fragmentView);
}
}
return fragmentView;
}
private void didSelectResult(final TLRPC.User user, boolean useAlert) {
2013-10-25 15:19:00 +00:00
if (useAlert && selectAlertString != 0) {
AlertDialog.Builder builder = new AlertDialog.Builder(parentActivity);
builder.setTitle(R.string.AppName);
builder.setMessage(String.format(getStringEntry(selectAlertString), Utilities.formatName(user.first_name, user.last_name)));
builder.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
didSelectResult(user, false);
2013-10-25 15:19:00 +00:00
}
});
builder.setNegativeButton(R.string.Cancel, null);
builder.show().setCanceledOnTouchOutside(true);
} else {
if (delegate != null) {
delegate.didSelectContact(user);
2013-10-25 15:19:00 +00:00
delegate = null;
}
finishFragment();
if (searchItem != null) {
if (searchItem.isActionViewExpanded()) {
searchItem.collapseActionView();
}
}
}
}
@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);
actionBar.setSubtitle(null);
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);
}
if (destroyAfterSelect) {
2013-12-20 19:25:49 +00:00
actionBar.setTitle(getStringEntry(R.string.SelectContact));
2013-10-25 15:19:00 +00:00
} else {
2013-12-20 19:25:49 +00:00
actionBar.setTitle(getStringEntry(R.string.Contacts));
2013-10-25 15:19:00 +00:00
}
2013-12-20 19:25:49 +00:00
((ApplicationActivity)parentActivity).fixBackButton();
2013-10-25 15:19:00 +00:00
}
@Override
public void onResume() {
super.onResume();
if (isFinish) {
return;
}
2013-12-20 19:25:49 +00:00
if (getActivity() == null) {
2013-10-25 15:19:00 +00:00
return;
}
if (!firstStart && listViewAdapter != null) {
listViewAdapter.notifyDataSetChanged();
}
firstStart = false;
((ApplicationActivity)parentActivity).showActionBar();
((ApplicationActivity)parentActivity).updateActionBar();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
if (searchItem != null) {
if (searchItem.isActionViewExpanded()) {
searchItem.collapseActionView();
}
}
finishFragment();
break;
}
return true;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.contacts_menu, menu);
2013-12-20 19:25:49 +00:00
searchItem = (SupportMenuItem)menu.findItem(R.id.messages_list_menu_search);
searchView = (SearchView)searchItem.getActionView();
TextView textView = (TextView) searchView.findViewById(R.id.search_src_text);
if (textView != null) {
textView.setTextColor(0xffffffff);
try {
Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
mCursorDrawableRes.setAccessible(true);
mCursorDrawableRes.set(textView, R.drawable.search_carret);
} catch (Exception e) {
e.printStackTrace();
}
}
2013-10-25 15:19:00 +00:00
2013-12-20 19:25:49 +00:00
ImageView img = (ImageView) searchView.findViewById(R.id.search_close_btn);
2013-10-25 15:19:00 +00:00
if (img != null) {
2013-12-20 19:25:49 +00:00
img.setImageResource(R.drawable.ic_msg_btn_cross_custom);
2013-10-25 15:19:00 +00:00
}
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return true;
}
@Override
public boolean onQueryTextChange(String s) {
if (searchListViewAdapter == null) {
return true;
}
searchListViewAdapter.searchDialogs(s);
2013-10-25 15:19:00 +00:00
if (s.length() != 0) {
searchWas = true;
if (listView != null) {
2013-12-26 16:46:13 +00:00
listView.setPadding(Utilities.dp(16), listView.getPaddingTop(), Utilities.dp(16), listView.getPaddingBottom());
2013-10-25 15:19:00 +00:00
listView.setAdapter(searchListViewAdapter);
if(android.os.Build.VERSION.SDK_INT >= 11) {
listView.setFastScrollAlwaysVisible(false);
}
listView.setFastScrollEnabled(false);
listView.setVerticalScrollBarEnabled(true);
}
if (epmtyTextView != null) {
epmtyTextView.setText(getStringEntry(R.string.NoResult));
}
}
return true;
}
});
2013-12-20 19:25:49 +00:00
searchItem.setSupportOnActionExpandListener(new MenuItemCompat.OnActionExpandListener() {
2013-10-25 15:19:00 +00:00
@Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
2013-12-20 19:25:49 +00:00
if (parentActivity != null) {
ActionBar actionBar = parentActivity.getSupportActionBar();
actionBar.setIcon(R.drawable.ic_ab_search);
}
2013-10-25 15:19:00 +00:00
searching = true;
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
searchView.setQuery("", false);
searchListViewAdapter.searchDialogs(null);
2013-10-25 15:19:00 +00:00
searching = false;
searchWas = false;
2013-12-20 19:25:49 +00:00
ViewGroup group = (ViewGroup) listView.getParent();
2013-10-25 15:19:00 +00:00
listView.setAdapter(listViewAdapter);
if (!Utilities.isRTL) {
2013-12-26 16:46:13 +00:00
listView.setPadding(Utilities.dp(16), listView.getPaddingTop(), Utilities.dp(30), listView.getPaddingBottom());
2013-10-25 15:19:00 +00:00
} else {
2013-12-26 16:46:13 +00:00
listView.setPadding(Utilities.dp(30), listView.getPaddingTop(), Utilities.dp(16), listView.getPaddingBottom());
2013-10-25 15:19:00 +00:00
}
2013-12-20 19:25:49 +00:00
if (android.os.Build.VERSION.SDK_INT >= 11) {
2013-10-25 15:19:00 +00:00
listView.setFastScrollAlwaysVisible(true);
}
listView.setFastScrollEnabled(true);
listView.setVerticalScrollBarEnabled(false);
2013-12-20 19:25:49 +00:00
((ApplicationActivity)parentActivity).updateActionBar();
2013-10-25 15:19:00 +00:00
epmtyTextView.setText(getStringEntry(R.string.NoContacts));
return true;
}
});
}
@Override
public void didReceivedNotification(int id, Object... args) {
if (id == MessagesController.contactsDidLoaded) {
2013-10-25 15:19:00 +00:00
if (listViewAdapter != null) {
listViewAdapter.notifyDataSetChanged();
}
} else if (id == MessagesController.updateInterfaces) {
int mask = (Integer)args[0];
if ((mask & MessagesController.UPDATE_MASK_AVATAR) != 0 || (mask & MessagesController.UPDATE_MASK_NAME) != 0 || (mask & MessagesController.UPDATE_MASK_STATUS) != 0) {
updateVisibleRows(mask);
2013-10-25 15:19:00 +00:00
}
} else if (id == MessagesController.encryptedChatCreated) {
if (createSecretChat && creatingChat) {
TLRPC.EncryptedChat encryptedChat = (TLRPC.EncryptedChat)args[0];
ChatActivity fragment = new ChatActivity();
Bundle bundle = new Bundle();
bundle.putInt("enc_id", encryptedChat.id);
fragment.setArguments(bundle);
2013-12-20 19:25:49 +00:00
((ApplicationActivity)parentActivity).presentFragment(fragment, "chat" + Math.random(), true, false);
2013-10-25 15:19:00 +00:00
}
}
}
private void updateInviteText() {
if (!updatingInviteText) {
updatingInviteText = true;
TLRPC.TL_help_getInviteText req = new TLRPC.TL_help_getInviteText();
req.lang_code = Locale.getDefault().getCountry();
if (req.lang_code == null || req.lang_code.length() == 0) {
req.lang_code = "en";
}
ConnectionsManager.Instance.performRpc(req, new RPCRequest.RPCRequestDelegate() {
@Override
public void run(TLObject response, TLRPC.TL_error error) {
if (error == null) {
final TLRPC.TL_help_inviteText res = (TLRPC.TL_help_inviteText)response;
if (res.message.length() != 0) {
Utilities.RunOnUIThread(new Runnable() {
@Override
public void run() {
updatingInviteText = false;
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("invitetext", res.message);
editor.putInt("invitetexttime", (int) (System.currentTimeMillis() / 1000));
editor.commit();
}
});
2013-10-25 15:19:00 +00:00
}
}
}
}, null, true, RPCRequest.RPCRequestClassGeneric | RPCRequest.RPCRequestClassFailOnServerErrors);
2013-10-25 15:19:00 +00:00
}
}
2013-10-25 15:19:00 +00:00
private void updateVisibleRows(int mask) {
if (listView != null) {
int count = listView.getChildCount();
for (int a = 0; a < count; a++) {
View child = listView.getChildAt(a);
if (child instanceof ChatOrUserCell) {
((ChatOrUserCell) child).update(mask);
2013-10-25 15:19:00 +00:00
}
}
}
}
}