1package eu.siacs.conversations.services;
2
3import java.util.concurrent.atomic.AtomicInteger;
4import java.util.Collection;
5import java.util.Collections;
6import java.util.HashMap;
7import java.util.Objects;
8import java.util.ArrayList;
9import java.util.List;
10import java.util.Map;
11
12import com.google.common.collect.ImmutableMap;
13
14import android.content.Intent;
15import android.os.SystemClock;
16import android.net.Uri;
17import android.util.Log;
18
19import eu.siacs.conversations.Config;
20import eu.siacs.conversations.android.PhoneNumberContact;
21import eu.siacs.conversations.entities.Account;
22import eu.siacs.conversations.entities.Contact;
23import eu.siacs.conversations.utils.SerialSingleThreadExecutor;
24import eu.siacs.conversations.xmpp.Jid;
25
26public class QuickConversationsService extends AbstractQuickConversationsService {
27
28 protected final AtomicInteger mRunningSyncJobs = new AtomicInteger(0);
29 protected final SerialSingleThreadExecutor mSerialSingleThreadExecutor = new SerialSingleThreadExecutor(QuickConversationsService.class.getSimpleName());
30 protected Attempt mLastSyncAttempt = Attempt.NULL;
31
32 QuickConversationsService(XmppConnectionService xmppConnectionService) {
33 super(xmppConnectionService);
34 }
35
36 @Override
37 public void considerSync() {
38 considerSync(false);
39 }
40
41 @Override
42 public void signalAccountStateChange() {
43
44 }
45
46 @Override
47 public boolean isSynchronizing() {
48 return mRunningSyncJobs.get() > 0;
49 }
50
51 @Override
52 public void considerSyncBackground(boolean force) {
53 mRunningSyncJobs.incrementAndGet();
54 mSerialSingleThreadExecutor.execute(() -> {
55 considerSync(force);
56 if (mRunningSyncJobs.decrementAndGet() == 0) {
57 service.updateRosterUi();
58 }
59 });
60 }
61
62 @Override
63 public void handleSmsReceived(Intent intent) {
64 Log.d(Config.LOGTAG,"ignoring received SMS");
65 }
66
67 protected static String getNumber(final List<String> gateways, final Contact contact) {
68 final Jid jid = contact.getJid();
69 if (jid.getLocal() != null && ("quicksy.im".equals(jid.getDomain()) || gateways.contains(jid.getDomain()))) {
70 return jid.getLocal();
71 }
72 return null;
73 }
74
75 protected void refresh(Account account, final List<String> gateways, Collection<PhoneNumberContact> phoneNumberContacts) {
76 for (Contact contact : account.getRoster().getWithSystemAccounts(PhoneNumberContact.class)) {
77 final Uri uri = contact.getSystemAccount();
78 if (uri == null) {
79 continue;
80 }
81 final String number = getNumber(gateways, contact);
82 final PhoneNumberContact phoneNumberContact = PhoneNumberContact.findByUriOrNumber(phoneNumberContacts, uri, number);
83 final boolean needsCacheClean;
84 if (phoneNumberContact != null) {
85 if (!uri.equals(phoneNumberContact.getLookupUri())) {
86 Log.d(Config.LOGTAG, "lookupUri has changed from " + uri + " to " + phoneNumberContact.getLookupUri());
87 }
88 needsCacheClean = contact.setPhoneContact(phoneNumberContact);
89 } else {
90 needsCacheClean = contact.unsetPhoneContact(PhoneNumberContact.class);
91 Log.d(Config.LOGTAG, uri.toString() + " vanished from address book");
92 }
93 if (needsCacheClean) {
94 service.getAvatarService().clear(contact);
95 }
96 }
97 }
98
99 protected void considerSync(boolean forced) {
100 ImmutableMap<String, PhoneNumberContact> allContacts = null;
101 for (final Account account : service.getAccounts()) {
102 List<String> gateways = gateways(account);
103 if (gateways.size() < 1) continue;
104 if (allContacts == null) allContacts = PhoneNumberContact.load(service);
105 refresh(account, gateways, allContacts.values());
106 if (!considerSync(account, gateways, allContacts, forced)) {
107 service.syncRoster(account);
108 }
109 }
110 }
111
112 protected List<String> gateways(final Account account) {
113 List<String> gateways = new ArrayList();
114 for (final Contact contact : account.getRoster().getContacts()) {
115 if (contact.showInRoster() && (contact.getPresences().anyIdentity("gateway", "pstn") || contact.getPresences().anyIdentity("gateway", "sms"))) {
116 gateways.add(contact.getJid().asBareJid().toString());
117 }
118 }
119 return gateways;
120 }
121
122 protected boolean considerSync(final Account account, final List<String> gateways, final Map<String, PhoneNumberContact> contacts, final boolean forced) {
123 final int hash = Objects.hash(contacts.keySet(), gateways);
124 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": consider sync of " + hash);
125 if (!mLastSyncAttempt.retry(hash) && !forced) {
126 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": do not attempt sync");
127 return false;
128 }
129 mRunningSyncJobs.incrementAndGet();
130
131 mLastSyncAttempt = Attempt.create(hash);
132 final List<Contact> withSystemAccounts = account.getRoster().getWithSystemAccounts(PhoneNumberContact.class);
133 for (Map.Entry<String, PhoneNumberContact> item : contacts.entrySet()) {
134 PhoneNumberContact phoneContact = item.getValue();
135 for(String gateway : gateways) {
136 final Jid jid = Jid.ofLocalAndDomain(phoneContact.getPhoneNumber(), gateway);
137 final Contact contact = account.getRoster().getContact(jid);
138 boolean needsCacheClean = contact.setPhoneContact(phoneContact);
139 needsCacheClean |= contact.setSystemTags(Collections.singleton(phoneContact.getTypeLabel()));
140 if (needsCacheClean) {
141 service.getAvatarService().clear(contact);
142 }
143 withSystemAccounts.remove(contact);
144 }
145 }
146 for (final Contact contact : withSystemAccounts) {
147 final boolean needsCacheClean = contact.unsetPhoneContact(PhoneNumberContact.class);
148 if (needsCacheClean) {
149 service.getAvatarService().clear(contact);
150 }
151 }
152
153 mRunningSyncJobs.decrementAndGet();
154 service.syncRoster(account);
155 service.updateRosterUi();
156 return true;
157 }
158
159 protected static class Attempt {
160 private final long timestamp;
161 private final int hash;
162
163 private static final Attempt NULL = new Attempt(0, 0);
164
165 private Attempt(long timestamp, int hash) {
166 this.timestamp = timestamp;
167 this.hash = hash;
168 }
169
170 public static Attempt create(int hash) {
171 return new Attempt(SystemClock.elapsedRealtime(), hash);
172 }
173
174 public boolean retry(int hash) {
175 return hash != this.hash || SystemClock.elapsedRealtime() - timestamp >= Config.CONTACT_SYNC_RETRY_INTERVAL;
176 }
177 }
178}