aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/entities/Roster.java
blob: 13856c06873bb65e92a91939d24285c1012d52a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package de.pixart.messenger.entities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import de.pixart.messenger.android.AbstractPhoneContact;
import rocks.xmpp.addr.Jid;

public class Roster {
    private final Account account;
    private final HashMap<Jid, Contact> contacts = new HashMap<>();
    private String version = null;

    public Roster(Account account) {
        this.account = account;
    }

    public Contact getContactFromContactList(Jid jid) {
        if (jid == null) {
            return null;
        }
        synchronized (this.contacts) {
            Contact contact = contacts.get(jid.asBareJid());
            if (contact != null && contact.showInContactList()) {
                return contact;
            } else {
                return null;
            }
        }
    }

    public Contact getContact(final Jid jid) {
        synchronized (this.contacts) {
            if (!contacts.containsKey(jid.asBareJid())) {
                Contact contact = new Contact(jid.asBareJid());
                contact.setAccount(account);
                contacts.put(contact.getJid().asBareJid(), contact);
                return contact;
            }
            return contacts.get(jid.asBareJid());
        }
    }

    public void clearPresences() {
        for (Contact contact : getContacts()) {
            contact.clearPresences();
        }
    }

    public void markAllAsNotInRoster() {
        for (Contact contact : getContacts()) {
            contact.resetOption(Contact.Options.IN_ROSTER);
        }
    }

    public List<Contact> getWithSystemAccounts(Class<?extends AbstractPhoneContact> clazz) {
        int option = Contact.getOption(clazz);
        List<Contact> with = getContacts();
        for (Iterator<Contact> iterator = with.iterator(); iterator.hasNext(); ) {
            Contact contact = iterator.next();
            if (!contact.getOption(option)) {
                iterator.remove();
            }
        }
        return with;
    }

    public List<Contact> getContacts() {
        synchronized (this.contacts) {
            return new ArrayList<>(this.contacts.values());
        }
    }

    public void initContact(final Contact contact) {
        if (contact == null) {
            return;
        }
        contact.setAccount(account);
        synchronized (this.contacts) {
            contacts.put(contact.getJid().asBareJid(), contact);
        }
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getVersion() {
        return this.version;
    }

    public Account getAccount() {
        return this.account;
    }
}