2014-02-28 17:46:01 +00:00
|
|
|
package eu.siacs.conversations.ui;
|
2014-01-24 01:04:05 +00:00
|
|
|
|
2014-01-28 21:21:08 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.List;
|
2014-01-24 01:04:05 +00:00
|
|
|
|
2014-02-28 17:46:01 +00:00
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Contact;
|
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
|
|
|
import eu.siacs.conversations.utils.UIHelper;
|
|
|
|
import eu.siacs.conversations.utils.Validator;
|
2014-02-01 00:25:56 +00:00
|
|
|
import android.net.Uri;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.text.Editable;
|
|
|
|
import android.text.TextWatcher;
|
|
|
|
import android.view.LayoutInflater;
|
2014-01-25 18:33:12 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.view.View;
|
2014-01-28 21:21:08 +00:00
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.AdapterView.OnItemClickListener;
|
2014-02-08 23:47:11 +00:00
|
|
|
import android.widget.AdapterView.OnItemLongClickListener;
|
2014-01-28 21:21:08 +00:00
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.ListView;
|
2014-02-07 01:57:36 +00:00
|
|
|
import android.widget.ProgressBar;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.ImageView;
|
2014-02-03 15:04:27 +00:00
|
|
|
import android.annotation.SuppressLint;
|
2014-01-28 22:15:30 +00:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.AlertDialog;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.content.Context;
|
2014-01-28 22:15:30 +00:00
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.DialogInterface.OnClickListener;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
|
2014-01-25 18:33:12 +00:00
|
|
|
public class NewConversationActivity extends XmppActivity {
|
2014-01-24 01:04:05 +00:00
|
|
|
|
2014-01-28 21:21:08 +00:00
|
|
|
protected List<Contact> rosterContacts = new ArrayList<Contact>();
|
|
|
|
protected List<Contact> aggregatedContacts = new ArrayList<Contact>();
|
|
|
|
protected ListView contactsView;
|
|
|
|
protected ArrayAdapter<Contact> contactsAdapter;
|
|
|
|
|
|
|
|
protected EditText search;
|
|
|
|
protected String searchString = "";
|
|
|
|
private TextView contactsHeader;
|
2014-02-02 15:05:15 +00:00
|
|
|
private List<Account> accounts;
|
2014-01-28 21:21:08 +00:00
|
|
|
|
|
|
|
protected void updateAggregatedContacts() {
|
|
|
|
|
|
|
|
aggregatedContacts.clear();
|
|
|
|
for (Contact contact : rosterContacts) {
|
|
|
|
if (contact.match(searchString))
|
|
|
|
aggregatedContacts.add(contact);
|
|
|
|
}
|
|
|
|
|
|
|
|
Collections.sort(aggregatedContacts, new Comparator<Contact>() {
|
|
|
|
|
2014-02-03 15:04:27 +00:00
|
|
|
@SuppressLint("DefaultLocale")
|
2014-01-28 21:21:08 +00:00
|
|
|
@Override
|
|
|
|
public int compare(Contact lhs, Contact rhs) {
|
2014-02-05 21:33:39 +00:00
|
|
|
return lhs.getDisplayName().toLowerCase()
|
|
|
|
.compareTo(rhs.getDisplayName().toLowerCase());
|
2014-01-28 21:21:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (aggregatedContacts.size() == 0) {
|
|
|
|
|
|
|
|
if (Validator.isValidJid(searchString)) {
|
|
|
|
String name = searchString.split("@")[0];
|
2014-02-05 21:33:39 +00:00
|
|
|
Contact newContact = new Contact(null, name, searchString, null);
|
2014-02-23 20:33:37 +00:00
|
|
|
newContact.flagAsNotInRoster();
|
2014-01-28 21:21:08 +00:00
|
|
|
aggregatedContacts.add(newContact);
|
|
|
|
contactsHeader.setText("Create new contact");
|
|
|
|
} else {
|
|
|
|
contactsHeader.setText("Contacts");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
contactsHeader.setText("Contacts");
|
|
|
|
}
|
|
|
|
|
|
|
|
contactsAdapter.notifyDataSetChanged();
|
2014-02-02 15:05:15 +00:00
|
|
|
contactsView.setScrollX(0);
|
2014-01-28 21:21:08 +00:00
|
|
|
}
|
2014-01-24 01:04:05 +00:00
|
|
|
|
2014-01-28 21:21:08 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
setContentView(R.layout.activity_new_conversation);
|
|
|
|
|
|
|
|
contactsHeader = (TextView) findViewById(R.id.contacts_header);
|
|
|
|
|
|
|
|
search = (EditText) findViewById(R.id.new_conversation_search);
|
|
|
|
search.addTextChangedListener(new TextWatcher() {
|
|
|
|
|
2014-01-24 01:04:05 +00:00
|
|
|
@Override
|
2014-01-28 21:21:08 +00:00
|
|
|
public void onTextChanged(CharSequence s, int start, int before,
|
|
|
|
int count) {
|
|
|
|
searchString = search.getText().toString();
|
|
|
|
updateAggregatedContacts();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count,
|
|
|
|
int after) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
contactsView = (ListView) findViewById(R.id.contactList);
|
|
|
|
contactsAdapter = new ArrayAdapter<Contact>(getApplicationContext(),
|
|
|
|
R.layout.contact, aggregatedContacts) {
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View view, ViewGroup parent) {
|
|
|
|
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
2014-02-07 15:50:29 +00:00
|
|
|
Contact contact = getItem(position);
|
2014-01-28 21:21:08 +00:00
|
|
|
if (view == null) {
|
|
|
|
view = (View) inflater.inflate(R.layout.contact, null);
|
2014-01-24 01:04:05 +00:00
|
|
|
}
|
2014-01-28 21:21:08 +00:00
|
|
|
|
|
|
|
((TextView) view.findViewById(R.id.contact_display_name))
|
|
|
|
.setText(getItem(position).getDisplayName());
|
2014-02-07 15:50:29 +00:00
|
|
|
TextView contactJid = (TextView) view
|
|
|
|
.findViewById(R.id.contact_jid);
|
|
|
|
contactJid.setText(contact.getJid());
|
2014-02-05 21:33:39 +00:00
|
|
|
ImageView imageView = (ImageView) view
|
|
|
|
.findViewById(R.id.contact_photo);
|
2014-03-13 12:28:16 +00:00
|
|
|
imageView.setImageBitmap(UIHelper.getContactPicture(contact,null,90,this.getContext()));
|
2014-01-28 21:21:08 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
contactsView.setAdapter(contactsAdapter);
|
2014-01-28 22:15:30 +00:00
|
|
|
final Activity activity = this;
|
2014-01-28 21:21:08 +00:00
|
|
|
contactsView.setOnItemClickListener(new OnItemClickListener() {
|
|
|
|
|
|
|
|
@Override
|
2014-02-05 21:33:39 +00:00
|
|
|
public void onItemClick(AdapterView<?> arg0, final View view,
|
|
|
|
int pos, long arg3) {
|
2014-01-28 22:15:30 +00:00
|
|
|
final Contact clickedContact = aggregatedContacts.get(pos);
|
2014-02-07 15:50:29 +00:00
|
|
|
|
|
|
|
if ((clickedContact.getAccount()==null)&&(accounts.size()>1)) {
|
2014-01-28 22:15:30 +00:00
|
|
|
String[] accountList = new String[accounts.size()];
|
2014-02-05 21:33:39 +00:00
|
|
|
for (int i = 0; i < accounts.size(); ++i) {
|
2014-02-07 15:50:29 +00:00
|
|
|
accountList[i] = accounts.get(i).getJid();
|
2014-01-28 22:15:30 +00:00
|
|
|
}
|
2014-02-05 21:33:39 +00:00
|
|
|
|
2014-02-07 15:50:29 +00:00
|
|
|
AlertDialog.Builder accountChooser = new AlertDialog.Builder(
|
|
|
|
activity);
|
|
|
|
accountChooser.setTitle("Choose account");
|
|
|
|
accountChooser.setItems(accountList, new OnClickListener() {
|
2014-02-05 21:33:39 +00:00
|
|
|
|
2014-02-07 15:50:29 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
clickedContact.setAccount(accounts.get(which));
|
|
|
|
showIsMucDialogIfNeeded(clickedContact);
|
|
|
|
}
|
2014-01-28 22:15:30 +00:00
|
|
|
});
|
2014-02-07 15:50:29 +00:00
|
|
|
accountChooser.create().show();
|
|
|
|
} else {
|
2014-02-09 13:10:52 +00:00
|
|
|
if (clickedContact.getAccount()==null) {
|
|
|
|
clickedContact.setAccount(accounts.get(0));
|
|
|
|
}
|
2014-02-07 15:50:29 +00:00
|
|
|
showIsMucDialogIfNeeded(clickedContact);
|
2014-01-28 22:15:30 +00:00
|
|
|
}
|
2014-01-24 01:04:05 +00:00
|
|
|
}
|
|
|
|
});
|
2014-02-08 23:47:11 +00:00
|
|
|
contactsView.setOnItemLongClickListener(new OnItemLongClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
|
|
|
|
int pos, long arg3) {
|
2014-03-05 14:41:14 +00:00
|
|
|
Intent intent = new Intent(activity,ContactDetailsActivity.class);
|
|
|
|
intent.setAction(ContactDetailsActivity.ACTION_VIEW_CONTACT);
|
|
|
|
intent.putExtra("uuid", aggregatedContacts.get(pos).getUuid());
|
|
|
|
startActivity(intent);
|
2014-02-08 23:47:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2014-01-24 01:04:05 +00:00
|
|
|
}
|
2014-02-07 15:50:29 +00:00
|
|
|
|
|
|
|
public void showIsMucDialogIfNeeded(final Contact clickedContact) {
|
|
|
|
if (clickedContact.couldBeMuc()) {
|
|
|
|
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
|
|
|
|
dialog.setTitle("Multi User Conference");
|
|
|
|
dialog.setMessage("Are you trying to join a conference?");
|
|
|
|
dialog.setPositiveButton("Yes", new OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
startConversation(clickedContact, clickedContact.getAccount(),true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
dialog.setNegativeButton("No", new OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
startConversation(clickedContact, clickedContact.getAccount(),false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
dialog.create().show();
|
|
|
|
} else {
|
|
|
|
startConversation(clickedContact, clickedContact.getAccount(),false);
|
|
|
|
}
|
|
|
|
}
|
2014-02-05 21:33:39 +00:00
|
|
|
|
2014-02-07 15:50:29 +00:00
|
|
|
public void startConversation(Contact contact, Account account, boolean muc) {
|
2014-02-23 20:33:37 +00:00
|
|
|
if (!contact.isInRoster()) {
|
|
|
|
xmppConnectionService.createContact(contact);
|
|
|
|
}
|
2014-01-28 22:15:30 +00:00
|
|
|
Conversation conversation = xmppConnectionService
|
2014-02-10 14:24:34 +00:00
|
|
|
.findOrCreateConversation(account, contact.getJid(), muc);
|
2014-01-28 22:15:30 +00:00
|
|
|
|
2014-02-05 21:33:39 +00:00
|
|
|
Intent viewConversationIntent = new Intent(this,
|
|
|
|
ConversationActivity.class);
|
2014-01-28 22:15:30 +00:00
|
|
|
viewConversationIntent.setAction(Intent.ACTION_VIEW);
|
2014-02-05 21:33:39 +00:00
|
|
|
viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
|
2014-01-28 22:15:30 +00:00
|
|
|
conversation.getUuid());
|
2014-02-05 21:33:39 +00:00
|
|
|
viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
|
|
|
|
viewConversationIntent.setFlags(viewConversationIntent.getFlags()
|
|
|
|
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
2014-01-28 22:15:30 +00:00
|
|
|
startActivity(viewConversationIntent);
|
|
|
|
}
|
2014-01-28 21:21:08 +00:00
|
|
|
|
2014-01-25 18:33:12 +00:00
|
|
|
@Override
|
2014-01-27 19:40:42 +00:00
|
|
|
void onBackendConnected() {
|
2014-01-28 21:21:08 +00:00
|
|
|
if (xmppConnectionService.getConversationCount() == 0) {
|
2014-01-28 18:21:54 +00:00
|
|
|
getActionBar().setDisplayHomeAsUpEnabled(false);
|
|
|
|
getActionBar().setHomeButtonEnabled(false);
|
|
|
|
}
|
2014-02-02 15:05:15 +00:00
|
|
|
this.accounts = xmppConnectionService.getAccounts();
|
|
|
|
this.rosterContacts.clear();
|
2014-02-07 15:50:29 +00:00
|
|
|
for (int i = 0; i < accounts.size(); ++i) {
|
|
|
|
xmppConnectionService.getRoster(accounts.get(i),
|
|
|
|
new OnRosterFetchedListener() {
|
|
|
|
|
2014-02-07 01:57:36 +00:00
|
|
|
@Override
|
2014-02-07 15:50:29 +00:00
|
|
|
public void onRosterFetched(List<Contact> roster) {
|
|
|
|
rosterContacts.addAll(roster);
|
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
updateAggregatedContacts();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-02-07 01:57:36 +00:00
|
|
|
}
|
|
|
|
});
|
2014-02-07 15:50:29 +00:00
|
|
|
}
|
2014-02-07 01:57:36 +00:00
|
|
|
}
|
2014-01-28 21:21:08 +00:00
|
|
|
|
2014-01-25 18:33:12 +00:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
|
|
|
getMenuInflater().inflate(R.menu.newconversation, menu);
|
|
|
|
return true;
|
|
|
|
}
|
2014-01-28 21:21:08 +00:00
|
|
|
|
2014-01-25 18:33:12 +00:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
2014-02-07 01:57:36 +00:00
|
|
|
case R.id.action_refresh_contacts:
|
|
|
|
refreshContacts();
|
|
|
|
break;
|
2014-01-25 18:33:12 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2014-01-28 21:21:08 +00:00
|
|
|
|
2014-02-07 01:57:36 +00:00
|
|
|
private void refreshContacts() {
|
|
|
|
final ProgressBar progress = (ProgressBar) findViewById(R.id.progressBar1);
|
|
|
|
final EditText searchBar = (EditText) findViewById(R.id.new_conversation_search);
|
|
|
|
final TextView contactsHeader = (TextView) findViewById(R.id.contacts_header);
|
|
|
|
final ListView contactList = (ListView) findViewById(R.id.contactList);
|
|
|
|
searchBar.setVisibility(View.GONE);
|
|
|
|
contactsHeader.setVisibility(View.GONE);
|
|
|
|
contactList.setVisibility(View.GONE);
|
|
|
|
progress.setVisibility(View.VISIBLE);
|
|
|
|
this.accounts = xmppConnectionService.getAccounts();
|
|
|
|
this.rosterContacts.clear();
|
|
|
|
for (int i = 0; i < accounts.size(); ++i) {
|
2014-02-07 15:50:29 +00:00
|
|
|
if (accounts.get(i).getStatus() == Account.STATUS_ONLINE) {
|
|
|
|
xmppConnectionService.updateRoster(accounts.get(i),
|
|
|
|
new OnRosterFetchedListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRosterFetched(
|
|
|
|
final List<Contact> roster) {
|
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
rosterContacts.addAll(roster);
|
|
|
|
progress.setVisibility(View.GONE);
|
|
|
|
searchBar.setVisibility(View.VISIBLE);
|
|
|
|
contactList.setVisibility(View.VISIBLE);
|
|
|
|
contactList.setVisibility(View.VISIBLE);
|
|
|
|
updateAggregatedContacts();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-02-07 01:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-24 01:04:05 +00:00
|
|
|
}
|