2014-07-16 22:03:37 +00:00
|
|
|
package eu.siacs.conversations.ui;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
2015-03-17 14:00:50 +00:00
|
|
|
import android.view.ActionMode;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2014-07-16 22:03:37 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.inputmethod.InputMethodManager;
|
2015-03-17 14:00:50 +00:00
|
|
|
import android.widget.AbsListView.MultiChoiceModeListener;
|
2014-07-16 22:03:37 +00:00
|
|
|
import android.widget.AdapterView;
|
2015-03-17 14:00:50 +00:00
|
|
|
import android.widget.ListView;
|
2014-12-21 20:43:58 +00:00
|
|
|
|
2015-07-20 12:26:29 +00:00
|
|
|
import java.util.ArrayList;
|
2014-12-21 20:43:58 +00:00
|
|
|
import java.util.Collections;
|
2015-07-20 12:26:29 +00:00
|
|
|
import java.util.HashSet;
|
2015-03-17 14:00:50 +00:00
|
|
|
import java.util.List;
|
2015-07-20 12:26:29 +00:00
|
|
|
import java.util.Set;
|
2014-12-21 20:43:58 +00:00
|
|
|
|
2016-01-04 16:27:51 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2015-03-17 14:00:50 +00:00
|
|
|
import eu.siacs.conversations.R;
|
2014-07-16 22:03:37 +00:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Contact;
|
|
|
|
import eu.siacs.conversations.entities.ListItem;
|
2016-01-04 16:27:51 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-12-21 20:43:58 +00:00
|
|
|
public class ChooseContactActivity extends AbstractSearchableListItemActivity {
|
2016-01-04 16:27:51 +00:00
|
|
|
private List<String> mActivatedAccounts = new ArrayList<String>();
|
|
|
|
private List<String> mKnownHosts;
|
2015-03-17 14:00:50 +00:00
|
|
|
|
|
|
|
private Set<Contact> selected;
|
|
|
|
private Set<String> filterContacts;
|
|
|
|
|
2014-07-16 22:03:37 +00:00
|
|
|
@Override
|
2014-12-21 20:43:58 +00:00
|
|
|
public void onCreate(final Bundle savedInstanceState) {
|
2014-07-16 22:03:37 +00:00
|
|
|
super.onCreate(savedInstanceState);
|
2015-03-17 14:00:50 +00:00
|
|
|
filterContacts = new HashSet<>();
|
|
|
|
String[] contacts = getIntent().getStringArrayExtra("filter_contacts");
|
|
|
|
if (contacts != null) {
|
|
|
|
Collections.addAll(filterContacts, contacts);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getIntent().getBooleanExtra("multiple", false)) {
|
|
|
|
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
|
|
|
|
getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
|
|
|
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
imm.hideSoftInputFromWindow(getSearchEditText().getWindowToken(),
|
|
|
|
InputMethodManager.HIDE_IMPLICIT_ONLY);
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.select_multiple, menu);
|
|
|
|
selected = new HashSet<Contact>();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyActionMode(ActionMode mode) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
|
|
|
switch(item.getItemId()) {
|
|
|
|
case R.id.selection_submit:
|
|
|
|
final Intent request = getIntent();
|
|
|
|
final Intent data = new Intent();
|
|
|
|
data.putExtra("conversation",
|
|
|
|
request.getStringExtra("conversation"));
|
|
|
|
String[] selection = getSelectedContactJids();
|
|
|
|
data.putExtra("contacts", selection);
|
|
|
|
data.putExtra("multiple", true);
|
|
|
|
setResult(RESULT_OK, data);
|
|
|
|
finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
|
|
|
|
Contact item = (Contact) getListItems().get(position);
|
|
|
|
if (checked) {
|
|
|
|
selected.add(item);
|
|
|
|
} else {
|
|
|
|
selected.remove(item);
|
|
|
|
}
|
|
|
|
int numSelected = selected.size();
|
|
|
|
MenuItem selectButton = mode.getMenu().findItem(R.id.selection_submit);
|
|
|
|
String buttonText = getResources().getQuantityString(R.plurals.select_contact,
|
|
|
|
numSelected, numSelected);
|
|
|
|
selectButton.setTitle(buttonText);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-21 20:43:58 +00:00
|
|
|
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
2014-07-16 22:03:37 +00:00
|
|
|
|
|
|
|
@Override
|
2014-12-21 20:43:58 +00:00
|
|
|
public void onItemClick(final AdapterView<?> parent, final View view,
|
|
|
|
final int position, final long id) {
|
|
|
|
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
imm.hideSoftInputFromWindow(getSearchEditText().getWindowToken(),
|
2014-07-16 22:03:37 +00:00
|
|
|
InputMethodManager.HIDE_IMPLICIT_ONLY);
|
2014-12-21 20:43:58 +00:00
|
|
|
final Intent request = getIntent();
|
|
|
|
final Intent data = new Intent();
|
|
|
|
final ListItem mListItem = getListItems().get(position);
|
2014-11-06 19:45:38 +00:00
|
|
|
data.putExtra("contact", mListItem.getJid().toString());
|
2014-09-02 13:51:20 +00:00
|
|
|
String account = request.getStringExtra("account");
|
2014-09-08 21:58:37 +00:00
|
|
|
if (account == null && mListItem instanceof Contact) {
|
2014-11-09 15:57:22 +00:00
|
|
|
account = ((Contact) mListItem).getAccount().getJid().toBareJid().toString();
|
2014-09-02 13:51:20 +00:00
|
|
|
}
|
|
|
|
data.putExtra("account", account);
|
2014-08-31 14:28:21 +00:00
|
|
|
data.putExtra("conversation",
|
|
|
|
request.getStringExtra("conversation"));
|
2015-03-17 14:00:50 +00:00
|
|
|
data.putExtra("multiple", false);
|
2014-07-16 22:03:37 +00:00
|
|
|
setResult(RESULT_OK, data);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
});
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-07-16 22:03:37 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 16:27:51 +00:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(final Menu menu) {
|
|
|
|
super.onCreateOptionsMenu(menu);
|
|
|
|
final Intent i = getIntent();
|
|
|
|
boolean showEnterJid = i != null && i.getBooleanExtra("show_enter_jid", false);
|
|
|
|
menu.findItem(R.id.action_create_contact).setVisible(showEnterJid);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-21 20:43:58 +00:00
|
|
|
protected void filterContacts(final String needle) {
|
|
|
|
getListItems().clear();
|
|
|
|
for (final Account account : xmppConnectionService.getAccounts()) {
|
2014-11-15 16:09:02 +00:00
|
|
|
if (account.getStatus() != Account.State.DISABLED) {
|
2014-12-21 20:43:58 +00:00
|
|
|
for (final Contact contact : account.getRoster().getContacts()) {
|
2015-03-17 14:00:50 +00:00
|
|
|
if (contact.showInRoster() &&
|
|
|
|
!filterContacts.contains(contact.getJid().toBareJid().toString())
|
|
|
|
&& contact.match(needle)) {
|
2014-12-21 20:43:58 +00:00
|
|
|
getListItems().add(contact);
|
2014-07-16 22:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-21 20:43:58 +00:00
|
|
|
Collections.sort(getListItems());
|
|
|
|
getListItemAdapter().notifyDataSetChanged();
|
2014-07-16 22:03:37 +00:00
|
|
|
}
|
2015-03-17 14:00:50 +00:00
|
|
|
|
|
|
|
private String[] getSelectedContactJids() {
|
|
|
|
List<String> result = new ArrayList<>();
|
|
|
|
for (Contact contact : selected) {
|
|
|
|
result.add(contact.getJid().toString());
|
|
|
|
}
|
|
|
|
return result.toArray(new String[result.size()]);
|
|
|
|
}
|
|
|
|
|
2015-07-20 13:48:58 +00:00
|
|
|
|
|
|
|
public void refreshUiReal() {
|
|
|
|
//nothing to do. This Activity doesn't implement any listeners
|
|
|
|
}
|
2016-01-04 16:27:51 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.action_create_contact:
|
|
|
|
showEnterJidDialog();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void showEnterJidDialog() {
|
|
|
|
EnterJidDialog dialog = new EnterJidDialog(
|
|
|
|
this, mKnownHosts, mActivatedAccounts,
|
|
|
|
getString(R.string.enter_contact), getString(R.string.select),
|
|
|
|
null, getIntent().getStringExtra("account"), true
|
|
|
|
);
|
|
|
|
|
|
|
|
dialog.setOnEnterJidDialogPositiveListener(new EnterJidDialog.OnEnterJidDialogPositiveListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onEnterJidDialogPositive(Jid accountJid, Jid contactJid) throws EnterJidDialog.JidError {
|
|
|
|
final Intent request = getIntent();
|
|
|
|
final Intent data = new Intent();
|
|
|
|
data.putExtra("contact", contactJid.toString());
|
|
|
|
data.putExtra("account", accountJid.toString());
|
|
|
|
data.putExtra("conversation",
|
|
|
|
request.getStringExtra("conversation"));
|
|
|
|
data.putExtra("multiple", false);
|
|
|
|
setResult(RESULT_OK, data);
|
|
|
|
finish();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
dialog.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
void onBackendConnected() {
|
|
|
|
filterContacts();
|
|
|
|
|
|
|
|
this.mActivatedAccounts.clear();
|
|
|
|
for (Account account : xmppConnectionService.getAccounts()) {
|
|
|
|
if (account.getStatus() != Account.State.DISABLED) {
|
|
|
|
if (Config.DOMAIN_LOCK != null) {
|
|
|
|
this.mActivatedAccounts.add(account.getJid().getLocalpart());
|
|
|
|
} else {
|
|
|
|
this.mActivatedAccounts.add(account.getJid().toBareJid().toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.mKnownHosts = xmppConnectionService.getKnownHosts();
|
|
|
|
}
|
2014-07-16 22:03:37 +00:00
|
|
|
}
|