2014-07-16 22:03:37 +00:00
|
|
|
package eu.siacs.conversations.ui;
|
|
|
|
|
2018-02-27 17:08:09 +00:00
|
|
|
import android.app.Activity;
|
2014-07-16 22:03:37 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
2018-03-07 11:56:35 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2016-05-27 17:17:57 +00:00
|
|
|
import android.support.annotation.StringRes;
|
2018-02-16 11:30:46 +00:00
|
|
|
import android.support.v7.app.ActionBar;
|
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;
|
|
|
|
import android.widget.ListView;
|
2014-12-21 20:43:58 +00:00
|
|
|
|
2015-07-20 12:26:29 +00:00
|
|
|
import java.util.ArrayList;
|
2018-03-11 11:13:56 +00:00
|
|
|
import java.util.Collection;
|
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;
|
2018-02-27 17:08:09 +00:00
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
2014-07-16 22:03:37 +00:00
|
|
|
import eu.siacs.conversations.entities.ListItem;
|
2018-02-27 17:08:09 +00:00
|
|
|
import eu.siacs.conversations.entities.MucOptions;
|
2018-03-07 11:56:35 +00:00
|
|
|
import eu.siacs.conversations.ui.util.ActivityResult;
|
|
|
|
import eu.siacs.conversations.ui.util.PendingItem;
|
|
|
|
import eu.siacs.conversations.utils.XmppUri;
|
2018-03-05 17:30:40 +00:00
|
|
|
import rocks.xmpp.addr.Jid;
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-12-21 20:43:58 +00:00
|
|
|
public class ChooseContactActivity extends AbstractSearchableListItemActivity {
|
2018-03-06 10:18:29 +00:00
|
|
|
public static final String EXTRA_TITLE_RES_ID = "extra_title_res_id";
|
2017-04-08 04:35:13 +00:00
|
|
|
private List<String> mActivatedAccounts = new ArrayList<>();
|
2018-03-11 11:13:56 +00:00
|
|
|
private Collection<String> mKnownHosts;
|
2015-03-17 14:00:50 +00:00
|
|
|
private Set<Contact> selected;
|
|
|
|
private Set<String> filterContacts;
|
2018-03-06 10:18:29 +00:00
|
|
|
|
2018-03-07 11:56:35 +00:00
|
|
|
private PendingItem<ActivityResult> postponedActivityResult = new PendingItem<>();
|
|
|
|
|
2018-03-06 10:18:29 +00:00
|
|
|
public static Intent create(Activity activity, Conversation conversation) {
|
|
|
|
final Intent intent = new Intent(activity, ChooseContactActivity.class);
|
|
|
|
List<String> contacts = new ArrayList<>();
|
|
|
|
if (conversation.getMode() == Conversation.MODE_MULTI) {
|
|
|
|
for (MucOptions.User user : conversation.getMucOptions().getUsers(false)) {
|
|
|
|
Jid jid = user.getRealJid();
|
|
|
|
if (jid != null) {
|
|
|
|
contacts.add(jid.asBareJid().toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
contacts.add(conversation.getJid().asBareJid().toString());
|
|
|
|
}
|
|
|
|
intent.putExtra("filter_contacts", contacts.toArray(new String[contacts.size()]));
|
|
|
|
intent.putExtra("conversation", conversation.getUuid());
|
|
|
|
intent.putExtra("multiple", true);
|
|
|
|
intent.putExtra("show_enter_jid", true);
|
|
|
|
intent.putExtra(EXTRA_ACCOUNT, conversation.getAccount().getJid().asBareJid().toString());
|
|
|
|
return intent;
|
|
|
|
}
|
2015-03-17 14:00:50 +00:00
|
|
|
|
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
|
2018-03-06 10:18:29 +00:00
|
|
|
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
2015-03-17 14:00:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
|
|
|
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
2018-03-06 10:18:29 +00:00
|
|
|
imm.hideSoftInputFromWindow(getSearchEditText().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
|
2015-03-17 14:00:50 +00:00
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.select_multiple, menu);
|
2017-04-08 04:35:13 +00:00
|
|
|
selected = new HashSet<>();
|
2015-03-17 14:00:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyActionMode(ActionMode mode) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
2018-03-06 10:18:29 +00:00
|
|
|
switch (item.getItemId()) {
|
2015-03-17 14:00:50 +00:00
|
|
|
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);
|
2018-03-06 10:18:29 +00:00
|
|
|
data.putExtra(EXTRA_ACCOUNT, request.getStringExtra(EXTRA_ACCOUNT));
|
2016-05-26 20:53:55 +00:00
|
|
|
data.putExtra("subject", request.getStringExtra("subject"));
|
2015-03-17 14:00:50 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-06 10:18:29 +00:00
|
|
|
getListView().setOnItemClickListener((parent, view, position, id) -> {
|
|
|
|
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
imm.hideSoftInputFromWindow(getSearchEditText().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
|
|
|
|
final Intent request = getIntent();
|
|
|
|
final Intent data = new Intent();
|
|
|
|
final ListItem mListItem = getListItems().get(position);
|
|
|
|
data.putExtra("contact", mListItem.getJid().toString());
|
|
|
|
String account = request.getStringExtra(EXTRA_ACCOUNT);
|
|
|
|
if (account == null && mListItem instanceof Contact) {
|
|
|
|
account = ((Contact) mListItem).getAccount().getJid().asBareJid().toString();
|
2014-07-16 22:03:37 +00:00
|
|
|
}
|
2018-03-06 10:18:29 +00:00
|
|
|
data.putExtra(EXTRA_ACCOUNT, account);
|
|
|
|
data.putExtra("conversation", request.getStringExtra("conversation"));
|
|
|
|
data.putExtra("multiple", false);
|
|
|
|
data.putExtra("subject", request.getStringExtra("subject"));
|
|
|
|
setResult(RESULT_OK, data);
|
|
|
|
finish();
|
2014-07-16 22:03:37 +00:00
|
|
|
});
|
2018-03-06 10:18:29 +00:00
|
|
|
final Intent i = getIntent();
|
|
|
|
boolean showEnterJid = i != null && i.getBooleanExtra("show_enter_jid", false);
|
|
|
|
if (showEnterJid) {
|
2018-03-07 11:56:35 +00:00
|
|
|
this.binding.fab.setOnClickListener((v) -> showEnterJidDialog(null));
|
2018-03-06 10:18:29 +00:00
|
|
|
} else {
|
|
|
|
this.binding.fab.setVisibility(View.GONE);
|
|
|
|
}
|
2014-07-16 22:03:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 17:17:57 +00:00
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
Intent intent = getIntent();
|
|
|
|
@StringRes
|
2018-03-06 10:18:29 +00:00
|
|
|
int res = intent != null ? intent.getIntExtra(EXTRA_TITLE_RES_ID, R.string.title_activity_choose_contact) : R.string.title_activity_choose_contact;
|
2018-02-16 11:30:46 +00:00
|
|
|
ActionBar bar = getSupportActionBar();
|
2016-05-27 17:17:57 +00:00
|
|
|
if (bar != null) {
|
|
|
|
try {
|
|
|
|
bar.setTitle(res);
|
|
|
|
} catch (Exception e) {
|
|
|
|
bar.setTitle(R.string.title_activity_choose_contact);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2018-03-06 10:18:29 +00:00
|
|
|
menu.findItem(R.id.action_scan_qr_code).setVisible(showEnterJid);
|
2016-01-04 16:27:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-21 20:43:58 +00:00
|
|
|
protected void filterContacts(final String needle) {
|
|
|
|
getListItems().clear();
|
2017-08-28 16:30:39 +00:00
|
|
|
if (xmppConnectionService == null) {
|
|
|
|
getListItemAdapter().notifyDataSetChanged();
|
|
|
|
return;
|
|
|
|
}
|
2014-12-21 20:43:58 +00:00
|
|
|
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() &&
|
2018-03-05 17:30:40 +00:00
|
|
|
!filterContacts.contains(contact.getJid().asBareJid().toString())
|
2016-04-22 19:25:06 +00:00
|
|
|
&& contact.match(this, 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()) {
|
2018-03-06 10:18:29 +00:00
|
|
|
case R.id.action_scan_qr_code:
|
|
|
|
ScanActivity.scan(this);
|
2016-01-04 16:27:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
2018-03-07 11:56:35 +00:00
|
|
|
protected void showEnterJidDialog(XmppUri uri) {
|
|
|
|
Jid jid = uri == null ? null : uri.getJid();
|
2016-01-04 16:27:51 +00:00
|
|
|
EnterJidDialog dialog = new EnterJidDialog(
|
2018-03-07 11:56:35 +00:00
|
|
|
this,
|
|
|
|
mKnownHosts,
|
|
|
|
mActivatedAccounts,
|
|
|
|
getString(R.string.enter_contact),
|
|
|
|
getString(R.string.select),
|
|
|
|
jid == null ? null : jid.asBareJid().toString(),
|
|
|
|
getIntent().getStringExtra(EXTRA_ACCOUNT),
|
|
|
|
true
|
2016-01-04 16:27:51 +00:00
|
|
|
);
|
|
|
|
|
2018-03-06 10:18:29 +00:00
|
|
|
dialog.setOnEnterJidDialogPositiveListener((accountJid, contactJid) -> {
|
|
|
|
final Intent request = getIntent();
|
|
|
|
final Intent data = new Intent();
|
|
|
|
data.putExtra("contact", contactJid.toString());
|
|
|
|
data.putExtra(EXTRA_ACCOUNT, accountJid.toString());
|
|
|
|
data.putExtra("conversation",
|
|
|
|
request.getStringExtra("conversation"));
|
|
|
|
data.putExtra("multiple", false);
|
|
|
|
data.putExtra("subject", request.getStringExtra("subject"));
|
|
|
|
setResult(RESULT_OK, data);
|
|
|
|
finish();
|
2016-01-04 16:27:51 +00:00
|
|
|
|
2018-03-06 10:18:29 +00:00
|
|
|
return true;
|
2016-01-04 16:27:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
dialog.show();
|
|
|
|
}
|
|
|
|
|
2018-03-07 11:56:35 +00:00
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
|
|
|
super.onActivityResult(requestCode, requestCode, intent);
|
|
|
|
ActivityResult activityResult = ActivityResult.of(requestCode, resultCode, intent);
|
|
|
|
if (xmppConnectionService != null) {
|
|
|
|
handleActivityResult(activityResult);
|
|
|
|
} else {
|
|
|
|
this.postponedActivityResult.push(activityResult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleActivityResult(ActivityResult activityResult) {
|
|
|
|
if (activityResult.resultCode == RESULT_OK && activityResult.requestCode == ScanActivity.REQUEST_SCAN_QR_CODE) {
|
|
|
|
String result = activityResult.data.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT);
|
|
|
|
XmppUri uri = new XmppUri(result == null ? "" : result);
|
|
|
|
if (uri.isJidValid()) {
|
|
|
|
showEnterJidDialog(uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 16:27:51 +00:00
|
|
|
@Override
|
|
|
|
void onBackendConnected() {
|
|
|
|
filterContacts();
|
|
|
|
this.mActivatedAccounts.clear();
|
|
|
|
for (Account account : xmppConnectionService.getAccounts()) {
|
|
|
|
if (account.getStatus() != Account.State.DISABLED) {
|
|
|
|
if (Config.DOMAIN_LOCK != null) {
|
2018-03-05 17:30:40 +00:00
|
|
|
this.mActivatedAccounts.add(account.getJid().getLocal());
|
2016-01-04 16:27:51 +00:00
|
|
|
} else {
|
2018-03-05 17:30:40 +00:00
|
|
|
this.mActivatedAccounts.add(account.getJid().asBareJid().toString());
|
2016-01-04 16:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.mKnownHosts = xmppConnectionService.getKnownHosts();
|
2018-03-07 11:56:35 +00:00
|
|
|
ActivityResult activityResult = this.postponedActivityResult.pop();
|
|
|
|
if (activityResult != null) {
|
|
|
|
handleActivityResult(activityResult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
|
|
|
|
ScanActivity.onRequestPermissionResult(this, requestCode, grantResults);
|
2016-01-04 16:27:51 +00:00
|
|
|
}
|
2014-07-16 22:03:37 +00:00
|
|
|
}
|