anotherim/src/main/java/eu/siacs/conversations/entities/Roster.java

99 lines
2.2 KiB
Java
Raw Normal View History

2014-10-22 16:38:44 +00:00
package eu.siacs.conversations.entities;
import java.util.ArrayList;
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-10-22 16:38:44 +00:00
public class Roster {
final Account account;
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;
}
public Contact getContactFromRoster(Jid jid) {
2014-10-22 16:38:44 +00:00
if (jid == null) {
return null;
}
synchronized (this.contacts) {
2018-03-05 17:30:40 +00:00
Contact contact = contacts.get(jid.asBareJid());
if (contact != null && contact.showInRoster()) {
return contact;
} else {
return null;
}
2014-10-22 16:38:44 +00:00
}
}
public Contact getContact(final Jid jid) {
synchronized (this.contacts) {
2018-03-05 17:30:40 +00:00
if (!contacts.containsKey(jid.asBareJid())) {
Contact contact = new Contact(jid.asBareJid());
contact.setAccount(account);
2018-03-05 17:30:40 +00:00
contacts.put(contact.getJid().asBareJid(), contact);
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() {
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() {
synchronized (this.contacts) {
return new ArrayList<>(this.contacts.values());
}
2014-10-22 16:38:44 +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);
synchronized (this.contacts) {
2018-03-05 17:30:40 +00:00
contacts.put(contact.getJid().asBareJid(), contact);
}
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;
}
}