2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.entities;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2015-01-03 23:14:40 +00:00
|
|
|
import java.util.HashMap;
|
2015-06-03 12:05:54 +00:00
|
|
|
import java.util.Iterator;
|
2014-10-22 16:38:44 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2018-10-28 18:05:16 +00:00
|
|
|
import eu.siacs.conversations.android.AbstractPhoneContact;
|
2018-03-05 17:30:40 +00:00
|
|
|
import rocks.xmpp.addr.Jid;
|
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public class Roster {
|
2014-12-21 20:43:58 +00:00
|
|
|
final Account account;
|
2016-02-28 19:45:50 +00:00
|
|
|
final HashMap<Jid, Contact> contacts = new HashMap<>();
|
2014-10-22 16:38:44 +00:00
|
|
|
private String version = null;
|
|
|
|
|
|
|
|
public Roster(Account account) {
|
|
|
|
this.account = account;
|
|
|
|
}
|
|
|
|
|
2014-11-17 19:02:46 +00:00
|
|
|
public Contact getContactFromRoster(Jid jid) {
|
2014-10-22 16:38:44 +00:00
|
|
|
if (jid == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-01-03 23:14:40 +00:00
|
|
|
synchronized (this.contacts) {
|
2018-03-05 17:30:40 +00:00
|
|
|
Contact contact = contacts.get(jid.asBareJid());
|
2015-01-03 23:14:40 +00:00
|
|
|
if (contact != null && contact.showInRoster()) {
|
|
|
|
return contact;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
public Contact getContact(final Jid jid) {
|
2015-01-03 23:14:40 +00:00
|
|
|
synchronized (this.contacts) {
|
2018-03-05 17:30:40 +00:00
|
|
|
if (!contacts.containsKey(jid.asBareJid())) {
|
|
|
|
Contact contact = new Contact(jid.asBareJid());
|
2015-01-03 23:14:40 +00:00
|
|
|
contact.setAccount(account);
|
2018-03-05 17:30:40 +00:00
|
|
|
contacts.put(contact.getJid().asBareJid(), contact);
|
2015-01-03 23:14:40 +00:00
|
|
|
return contact;
|
|
|
|
}
|
2018-03-05 17:30:40 +00:00
|
|
|
return contacts.get(jid.asBareJid());
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearPresences() {
|
|
|
|
for (Contact contact : getContacts()) {
|
|
|
|
contact.clearPresences();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void markAllAsNotInRoster() {
|
2015-01-03 23:14:40 +00:00
|
|
|
for (Contact contact : getContacts()) {
|
2014-10-22 16:38:44 +00:00
|
|
|
contact.resetOption(Contact.Options.IN_ROSTER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-28 18:05:16 +00:00
|
|
|
public List<Contact> getWithSystemAccounts(Class<?extends AbstractPhoneContact> clazz) {
|
|
|
|
int option = Contact.getOption(clazz);
|
2015-06-03 12:05:54 +00:00
|
|
|
List<Contact> with = getContacts();
|
|
|
|
for(Iterator<Contact> iterator = with.iterator(); iterator.hasNext();) {
|
|
|
|
Contact contact = iterator.next();
|
2018-10-28 18:05:16 +00:00
|
|
|
if (!contact.getOption(option)) {
|
2015-06-03 12:05:54 +00:00
|
|
|
iterator.remove();
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2015-06-03 12:05:54 +00:00
|
|
|
return with;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<Contact> getContacts() {
|
2015-01-03 23:14:40 +00:00
|
|
|
synchronized (this.contacts) {
|
|
|
|
return new ArrayList<>(this.contacts.values());
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
public void initContact(final Contact contact) {
|
2015-07-03 19:32:46 +00:00
|
|
|
if (contact == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
contact.setAccount(account);
|
2015-01-03 23:14:40 +00:00
|
|
|
synchronized (this.contacts) {
|
2018-03-05 17:30:40 +00:00
|
|
|
contacts.put(contact.getJid().asBareJid(), contact);
|
2015-01-03 23:14:40 +00:00
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setVersion(String version) {
|
|
|
|
this.version = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getVersion() {
|
|
|
|
return this.version;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Account getAccount() {
|
|
|
|
return this.account;
|
|
|
|
}
|
|
|
|
}
|