1package eu.siacs.conversations.services;
2
3import android.annotation.SuppressLint;
4import android.annotation.TargetApi;
5import android.app.AlarmManager;
6import android.app.PendingIntent;
7import android.app.Service;
8import android.content.Context;
9import android.content.Intent;
10import android.content.IntentFilter;
11import android.content.SharedPreferences;
12import android.database.ContentObserver;
13import android.graphics.Bitmap;
14import android.media.AudioManager;
15import android.net.ConnectivityManager;
16import android.net.NetworkInfo;
17import android.net.Uri;
18import android.os.Binder;
19import android.os.Build;
20import android.os.Bundle;
21import android.os.Environment;
22import android.os.IBinder;
23import android.os.PowerManager;
24import android.os.PowerManager.WakeLock;
25import android.os.SystemClock;
26import android.preference.PreferenceManager;
27import android.provider.ContactsContract;
28import android.security.KeyChain;
29import android.support.annotation.BoolRes;
30import android.support.annotation.IntegerRes;
31import android.support.v4.app.RemoteInput;
32import android.util.DisplayMetrics;
33import android.util.Log;
34import android.util.LruCache;
35import android.util.Pair;
36
37import org.openintents.openpgp.IOpenPgpService2;
38import org.openintents.openpgp.util.OpenPgpApi;
39import org.openintents.openpgp.util.OpenPgpServiceConnection;
40
41import java.math.BigInteger;
42import java.net.URL;
43import java.security.SecureRandom;
44import java.security.cert.CertificateException;
45import java.security.cert.X509Certificate;
46import java.util.ArrayList;
47import java.util.Arrays;
48import java.util.Collection;
49import java.util.Collections;
50import java.util.HashMap;
51import java.util.HashSet;
52import java.util.Hashtable;
53import java.util.Iterator;
54import java.util.List;
55import java.util.ListIterator;
56import java.util.Locale;
57import java.util.Map;
58import java.util.Set;
59import java.util.concurrent.CopyOnWriteArrayList;
60import java.util.concurrent.CountDownLatch;
61import java.util.concurrent.atomic.AtomicBoolean;
62import java.util.concurrent.atomic.AtomicLong;
63
64import eu.siacs.conversations.Config;
65import eu.siacs.conversations.R;
66import eu.siacs.conversations.crypto.OmemoSetting;
67import eu.siacs.conversations.crypto.PgpDecryptionService;
68import eu.siacs.conversations.crypto.PgpEngine;
69import eu.siacs.conversations.crypto.axolotl.AxolotlService;
70import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
71import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
72import eu.siacs.conversations.entities.Account;
73import eu.siacs.conversations.entities.Blockable;
74import eu.siacs.conversations.entities.Bookmark;
75import eu.siacs.conversations.entities.Contact;
76import eu.siacs.conversations.entities.Conversation;
77import eu.siacs.conversations.entities.DownloadableFile;
78import eu.siacs.conversations.entities.Message;
79import eu.siacs.conversations.entities.MucOptions;
80import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
81import eu.siacs.conversations.entities.Presence;
82import eu.siacs.conversations.entities.PresenceTemplate;
83import eu.siacs.conversations.entities.Roster;
84import eu.siacs.conversations.entities.ServiceDiscoveryResult;
85import eu.siacs.conversations.entities.Transferable;
86import eu.siacs.conversations.entities.TransferablePlaceholder;
87import eu.siacs.conversations.generator.AbstractGenerator;
88import eu.siacs.conversations.generator.IqGenerator;
89import eu.siacs.conversations.generator.MessageGenerator;
90import eu.siacs.conversations.generator.PresenceGenerator;
91import eu.siacs.conversations.http.HttpConnectionManager;
92import eu.siacs.conversations.http.AesGcmURLStreamHandlerFactory;
93import eu.siacs.conversations.parser.AbstractParser;
94import eu.siacs.conversations.parser.IqParser;
95import eu.siacs.conversations.parser.MessageParser;
96import eu.siacs.conversations.parser.PresenceParser;
97import eu.siacs.conversations.persistance.DatabaseBackend;
98import eu.siacs.conversations.persistance.FileBackend;
99import eu.siacs.conversations.ui.SettingsActivity;
100import eu.siacs.conversations.ui.UiCallback;
101import eu.siacs.conversations.ui.interfaces.OnSearchResultsAvailable;
102import eu.siacs.conversations.utils.ConversationsFileObserver;
103import eu.siacs.conversations.utils.CryptoHelper;
104import eu.siacs.conversations.utils.ExceptionHelper;
105import eu.siacs.conversations.utils.MimeUtils;
106import eu.siacs.conversations.utils.OnPhoneContactsLoadedListener;
107import eu.siacs.conversations.utils.PRNGFixes;
108import eu.siacs.conversations.utils.PhoneHelper;
109import eu.siacs.conversations.utils.QuickLoader;
110import eu.siacs.conversations.utils.ReplacingSerialSingleThreadExecutor;
111import eu.siacs.conversations.utils.ReplacingTaskManager;
112import eu.siacs.conversations.utils.Resolver;
113import eu.siacs.conversations.utils.SerialSingleThreadExecutor;
114import eu.siacs.conversations.utils.WakeLockHelper;
115import eu.siacs.conversations.xml.Namespace;
116import eu.siacs.conversations.utils.XmppUri;
117import eu.siacs.conversations.xml.Element;
118import eu.siacs.conversations.xmpp.OnBindListener;
119import eu.siacs.conversations.xmpp.OnContactStatusChanged;
120import eu.siacs.conversations.xmpp.OnIqPacketReceived;
121import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
122import eu.siacs.conversations.xmpp.OnMessageAcknowledged;
123import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
124import eu.siacs.conversations.xmpp.OnPresencePacketReceived;
125import eu.siacs.conversations.xmpp.OnStatusChanged;
126import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
127import eu.siacs.conversations.xmpp.Patches;
128import eu.siacs.conversations.xmpp.XmppConnection;
129import eu.siacs.conversations.xmpp.chatstate.ChatState;
130import eu.siacs.conversations.xmpp.forms.Data;
131import eu.siacs.conversations.xmpp.jingle.JingleConnectionManager;
132import eu.siacs.conversations.xmpp.jingle.OnJinglePacketReceived;
133import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
134import eu.siacs.conversations.xmpp.mam.MamReference;
135import eu.siacs.conversations.xmpp.pep.Avatar;
136import eu.siacs.conversations.xmpp.stanzas.IqPacket;
137import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
138import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
139import me.leolin.shortcutbadger.ShortcutBadger;
140import rocks.xmpp.addr.Jid;
141
142public class XmppConnectionService extends Service {
143
144 public static final String ACTION_REPLY_TO_CONVERSATION = "reply_to_conversations";
145 public static final String ACTION_MARK_AS_READ = "mark_as_read";
146 public static final String ACTION_SNOOZE = "snooze";
147 public static final String ACTION_CLEAR_NOTIFICATION = "clear_notification";
148 public static final String ACTION_DISMISS_ERROR_NOTIFICATIONS = "dismiss_error";
149 public static final String ACTION_TRY_AGAIN = "try_again";
150 public static final String ACTION_IDLE_PING = "idle_ping";
151 public static final String ACTION_GCM_TOKEN_REFRESH = "gcm_token_refresh";
152 public static final String ACTION_GCM_MESSAGE_RECEIVED = "gcm_message_received";
153 private static final String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts";
154
155 static {
156 URL.setURLStreamHandlerFactory(new AesGcmURLStreamHandlerFactory());
157 }
158
159 public final CountDownLatch restoredFromDatabaseLatch = new CountDownLatch(1);
160 private final SerialSingleThreadExecutor mFileAddingExecutor = new SerialSingleThreadExecutor("FileAdding");
161 private final SerialSingleThreadExecutor mVideoCompressionExecutor = new SerialSingleThreadExecutor("VideoCompression");
162 private final SerialSingleThreadExecutor mDatabaseWriterExecutor = new SerialSingleThreadExecutor("DatabaseWriter");
163 private final SerialSingleThreadExecutor mDatabaseReaderExecutor = new SerialSingleThreadExecutor("DatabaseReader");
164 private final SerialSingleThreadExecutor mNotificationExecutor = new SerialSingleThreadExecutor("NotificationExecutor");
165 private final ReplacingTaskManager mRosterSyncTaskManager = new ReplacingTaskManager();
166 private final IBinder mBinder = new XmppConnectionBinder();
167 private final List<Conversation> conversations = new CopyOnWriteArrayList<>();
168 private final IqGenerator mIqGenerator = new IqGenerator(this);
169 private final List<String> mInProgressAvatarFetches = new ArrayList<>();
170 private final HashSet<Jid> mLowPingTimeoutMode = new HashSet<>();
171 private final OnIqPacketReceived mDefaultIqHandler = (account, packet) -> {
172 if (packet.getType() != IqPacket.TYPE.RESULT) {
173 Element error = packet.findChild("error");
174 String text = error != null ? error.findChildContent("text") : null;
175 if (text != null) {
176 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received iq error - " + text);
177 }
178 }
179 };
180 public DatabaseBackend databaseBackend;
181 private ReplacingSerialSingleThreadExecutor mContactMergerExecutor = new ReplacingSerialSingleThreadExecutor(true);
182 private long mLastActivity = 0;
183 private ContentObserver contactObserver = new ContentObserver(null) {
184 @Override
185 public void onChange(boolean selfChange) {
186 super.onChange(selfChange);
187 Intent intent = new Intent(getApplicationContext(),
188 XmppConnectionService.class);
189 intent.setAction(ACTION_MERGE_PHONE_CONTACTS);
190 startService(intent);
191 }
192 };
193 private FileBackend fileBackend = new FileBackend(this);
194 private MemorizingTrustManager mMemorizingTrustManager;
195 private NotificationService mNotificationService = new NotificationService(this);
196 private ShortcutService mShortcutService = new ShortcutService(this);
197 private AtomicBoolean mInitialAddressbookSyncCompleted = new AtomicBoolean(false);
198 private AtomicBoolean mForceForegroundService = new AtomicBoolean(false);
199 private OnMessagePacketReceived mMessageParser = new MessageParser(this);
200 private OnPresencePacketReceived mPresenceParser = new PresenceParser(this);
201 private IqParser mIqParser = new IqParser(this);
202 private MessageGenerator mMessageGenerator = new MessageGenerator(this);
203 public OnContactStatusChanged onContactStatusChanged = (contact, online) -> {
204 Conversation conversation = find(getConversations(), contact);
205 if (conversation != null) {
206 if (online) {
207 if (contact.getPresences().size() == 1) {
208 sendUnsentMessages(conversation);
209 }
210 }
211 }
212 };
213 private PresenceGenerator mPresenceGenerator = new PresenceGenerator(this);
214 private List<Account> accounts;
215 private JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager(
216 this);
217 private final OnJinglePacketReceived jingleListener = new OnJinglePacketReceived() {
218
219 @Override
220 public void onJinglePacketReceived(Account account, JinglePacket packet) {
221 mJingleConnectionManager.deliverPacket(account, packet);
222 }
223 };
224 private HttpConnectionManager mHttpConnectionManager = new HttpConnectionManager(
225 this);
226 private AvatarService mAvatarService = new AvatarService(this);
227 private MessageArchiveService mMessageArchiveService = new MessageArchiveService(this);
228 private PushManagementService mPushManagementService = new PushManagementService(this);
229 private OnConversationUpdate mOnConversationUpdate = null;
230 private final ConversationsFileObserver fileObserver = new ConversationsFileObserver(
231 Environment.getExternalStorageDirectory().getAbsolutePath()
232 ) {
233 @Override
234 public void onEvent(int event, String path) {
235 markFileDeleted(path);
236 }
237 };
238 private final OnMessageAcknowledged mOnMessageAcknowledgedListener = new OnMessageAcknowledged() {
239
240 @Override
241 public void onMessageAcknowledged(Account account, String uuid) {
242 for (final Conversation conversation : getConversations()) {
243 if (conversation.getAccount() == account) {
244 Message message = conversation.findUnsentMessageWithUuid(uuid);
245 if (message != null) {
246 markMessage(message, Message.STATUS_SEND);
247 }
248 }
249 }
250 }
251 };
252 private int convChangedListenerCount = 0;
253 private OnShowErrorToast mOnShowErrorToast = null;
254 private int showErrorToastListenerCount = 0;
255 private int unreadCount = -1;
256 private OnAccountUpdate mOnAccountUpdate = null;
257 private OnCaptchaRequested mOnCaptchaRequested = null;
258 private int accountChangedListenerCount = 0;
259 private int captchaRequestedListenerCount = 0;
260 private OnRosterUpdate mOnRosterUpdate = null;
261 private OnUpdateBlocklist mOnUpdateBlocklist = null;
262 private int updateBlocklistListenerCount = 0;
263 private int rosterChangedListenerCount = 0;
264 private OnMucRosterUpdate mOnMucRosterUpdate = null;
265 private int mucRosterChangedListenerCount = 0;
266 private OnKeyStatusUpdated mOnKeyStatusUpdated = null;
267 private final OnBindListener mOnBindListener = new OnBindListener() {
268
269 @Override
270 public void onBind(final Account account) {
271 synchronized (mInProgressAvatarFetches) {
272 for (Iterator<String> iterator = mInProgressAvatarFetches.iterator(); iterator.hasNext(); ) {
273 final String KEY = iterator.next();
274 if (KEY.startsWith(account.getJid().asBareJid() + "_")) {
275 iterator.remove();
276 }
277 }
278 }
279 boolean needsUpdating = account.setOption(Account.OPTION_LOGGED_IN_SUCCESSFULLY, true);
280 needsUpdating |= account.setOption(Account.OPTION_HTTP_UPLOAD_AVAILABLE, account.getXmppConnection().getFeatures().httpUpload(0));
281 if (needsUpdating) {
282 databaseBackend.updateAccount(account);
283 }
284 account.getRoster().clearPresences();
285 mJingleConnectionManager.cancelInTransmission();
286 fetchRosterFromServer(account);
287 fetchBookmarks(account);
288 final boolean flexible = account.getXmppConnection().getFeatures().flexibleOfflineMessageRetrieval();
289 final boolean catchup = getMessageArchiveService().inCatchup(account);
290 if (flexible && catchup) {
291 sendIqPacket(account, mIqGenerator.purgeOfflineMessages(), (acc, packet) -> {
292 if (packet.getType() == IqPacket.TYPE.RESULT) {
293 Log.d(Config.LOGTAG, acc.getJid().asBareJid() + ": successfully purged offline messages");
294 }
295 });
296 }
297 sendPresence(account);
298 if (mPushManagementService.available(account)) {
299 mPushManagementService.registerPushTokenOnServer(account);
300 }
301 connectMultiModeConversations(account);
302 syncDirtyContacts(account);
303 }
304 };
305 private int keyStatusUpdatedListenerCount = 0;
306 private AtomicLong mLastExpiryRun = new AtomicLong(0);
307 private SecureRandom mRandom;
308 private LruCache<Pair<String, String>, ServiceDiscoveryResult> discoCache = new LruCache<>(20);
309 private OnStatusChanged statusListener = new OnStatusChanged() {
310
311 @Override
312 public void onStatusChanged(final Account account) {
313 XmppConnection connection = account.getXmppConnection();
314 if (mOnAccountUpdate != null) {
315 mOnAccountUpdate.onAccountUpdate();
316 }
317 if (account.getStatus() == Account.State.ONLINE) {
318 synchronized (mLowPingTimeoutMode) {
319 if (mLowPingTimeoutMode.remove(account.getJid().asBareJid())) {
320 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": leaving low ping timeout mode");
321 }
322 }
323 if (account.setShowErrorNotification(true)) {
324 databaseBackend.updateAccount(account);
325 }
326 mMessageArchiveService.executePendingQueries(account);
327 if (connection != null && connection.getFeatures().csi()) {
328 if (checkListeners()) {
329 Log.d(Config.LOGTAG, account.getJid().asBareJid() + " sending csi//inactive");
330 connection.sendInactive();
331 } else {
332 Log.d(Config.LOGTAG, account.getJid().asBareJid() + " sending csi//active");
333 connection.sendActive();
334 }
335 }
336 List<Conversation> conversations = getConversations();
337 for (Conversation conversation : conversations) {
338 if (conversation.getAccount() == account && !account.pendingConferenceJoins.contains(conversation)) {
339 sendUnsentMessages(conversation);
340 }
341 }
342 for (Conversation conversation : account.pendingConferenceLeaves) {
343 leaveMuc(conversation);
344 }
345 account.pendingConferenceLeaves.clear();
346 for (Conversation conversation : account.pendingConferenceJoins) {
347 joinMuc(conversation);
348 }
349 account.pendingConferenceJoins.clear();
350 scheduleWakeUpCall(Config.PING_MAX_INTERVAL, account.getUuid().hashCode());
351 } else if (account.getStatus() == Account.State.OFFLINE || account.getStatus() == Account.State.DISABLED) {
352 resetSendingToWaiting(account);
353 if (account.isEnabled() && isInLowPingTimeoutMode(account)) {
354 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": went into offline state during low ping mode. reconnecting now");
355 reconnectAccount(account, true, false);
356 } else {
357 int timeToReconnect = mRandom.nextInt(10) + 2;
358 scheduleWakeUpCall(timeToReconnect, account.getUuid().hashCode());
359 }
360 } else if (account.getStatus() == Account.State.REGISTRATION_SUCCESSFUL) {
361 databaseBackend.updateAccount(account);
362 reconnectAccount(account, true, false);
363 } else if (account.getStatus() != Account.State.CONNECTING && account.getStatus() != Account.State.NO_INTERNET) {
364 resetSendingToWaiting(account);
365 if (connection != null && account.getStatus().isAttemptReconnect()) {
366 final int next = connection.getTimeToNextAttempt();
367 final boolean lowPingTimeoutMode = isInLowPingTimeoutMode(account);
368 if (next <= 0) {
369 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": error connecting account. reconnecting now. lowPingTimeout=" + Boolean.toString(lowPingTimeoutMode));
370 reconnectAccount(account, true, false);
371 } else {
372 final int attempt = connection.getAttempt() + 1;
373 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": error connecting account. try again in " + next + "s for the " + attempt + " time. lowPingTimeout=" + Boolean.toString(lowPingTimeoutMode));
374 scheduleWakeUpCall(next, account.getUuid().hashCode());
375 }
376 }
377 }
378 getNotificationService().updateErrorNotification();
379 }
380 };
381 private OpenPgpServiceConnection pgpServiceConnection;
382 private PgpEngine mPgpEngine = null;
383 private WakeLock wakeLock;
384 private PowerManager pm;
385 private LruCache<String, Bitmap> mBitmapCache;
386 private EventReceiver mEventReceiver = new EventReceiver();
387
388 private static String generateFetchKey(Account account, final Avatar avatar) {
389 return account.getJid().asBareJid() + "_" + avatar.owner + "_" + avatar.sha1sum;
390 }
391
392 private boolean isInLowPingTimeoutMode(Account account) {
393 synchronized (mLowPingTimeoutMode) {
394 return mLowPingTimeoutMode.contains(account.getJid().asBareJid());
395 }
396 }
397
398 public void startForcingForegroundNotification() {
399 mForceForegroundService.set(true);
400 toggleForegroundService();
401 }
402
403 public void stopForcingForegroundNotification() {
404 mForceForegroundService.set(false);
405 toggleForegroundService();
406 }
407
408 public boolean areMessagesInitialized() {
409 return this.restoredFromDatabaseLatch.getCount() == 0;
410 }
411
412 public PgpEngine getPgpEngine() {
413 if (!Config.supportOpenPgp()) {
414 return null;
415 } else if (pgpServiceConnection != null && pgpServiceConnection.isBound()) {
416 if (this.mPgpEngine == null) {
417 this.mPgpEngine = new PgpEngine(new OpenPgpApi(
418 getApplicationContext(),
419 pgpServiceConnection.getService()), this);
420 }
421 return mPgpEngine;
422 } else {
423 return null;
424 }
425
426 }
427
428 public OpenPgpApi getOpenPgpApi() {
429 if (!Config.supportOpenPgp()) {
430 return null;
431 } else if (pgpServiceConnection != null && pgpServiceConnection.isBound()) {
432 return new OpenPgpApi(this, pgpServiceConnection.getService());
433 } else {
434 return null;
435 }
436 }
437
438 public FileBackend getFileBackend() {
439 return this.fileBackend;
440 }
441
442 public AvatarService getAvatarService() {
443 return this.mAvatarService;
444 }
445
446 public void attachLocationToConversation(final Conversation conversation, final Uri uri, final UiCallback<Message> callback) {
447 int encryption = conversation.getNextEncryption();
448 if (encryption == Message.ENCRYPTION_PGP) {
449 encryption = Message.ENCRYPTION_DECRYPTED;
450 }
451 Message message = new Message(conversation, uri.toString(), encryption);
452 if (conversation.getNextCounterpart() != null) {
453 message.setCounterpart(conversation.getNextCounterpart());
454 }
455 if (encryption == Message.ENCRYPTION_DECRYPTED) {
456 getPgpEngine().encrypt(message, callback);
457 } else {
458 sendMessage(message);
459 callback.success(message);
460 }
461 }
462
463 public void attachFileToConversation(final Conversation conversation, final Uri uri, final String type, final UiCallback<Message> callback) {
464 if (FileBackend.weOwnFile(this, uri)) {
465 Log.d(Config.LOGTAG, "trying to attach file that belonged to us");
466 callback.error(R.string.security_error_invalid_file_access, null);
467 return;
468 }
469 final Message message;
470 if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
471 message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
472 } else {
473 message = new Message(conversation, "", conversation.getNextEncryption());
474 }
475 message.setCounterpart(conversation.getNextCounterpart());
476 message.setType(Message.TYPE_FILE);
477 final AttachFileToConversationRunnable runnable = new AttachFileToConversationRunnable(this, uri, type, message, callback);
478 if (runnable.isVideoMessage()) {
479 mVideoCompressionExecutor.execute(runnable);
480 } else {
481 mFileAddingExecutor.execute(runnable);
482 }
483 }
484
485 public void attachImageToConversation(final Conversation conversation, final Uri uri, final UiCallback<Message> callback) {
486 if (FileBackend.weOwnFile(this, uri)) {
487 Log.d(Config.LOGTAG, "trying to attach file that belonged to us");
488 callback.error(R.string.security_error_invalid_file_access, null);
489 return;
490 }
491
492 final String mimeType = MimeUtils.guessMimeTypeFromUri(this, uri);
493 final String compressPictures = getCompressPicturesPreference();
494
495 if ("never".equals(compressPictures)
496 || ("auto".equals(compressPictures) && getFileBackend().useImageAsIs(uri))
497 || (mimeType != null && mimeType.endsWith("/gif"))) {
498 Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": not compressing picture. sending as file");
499 attachFileToConversation(conversation, uri, mimeType, callback);
500 return;
501 }
502 final Message message;
503 if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
504 message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
505 } else {
506 message = new Message(conversation, "", conversation.getNextEncryption());
507 }
508 message.setCounterpart(conversation.getNextCounterpart());
509 message.setType(Message.TYPE_IMAGE);
510 mFileAddingExecutor.execute(() -> {
511 try {
512 getFileBackend().copyImageToPrivateStorage(message, uri);
513 if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
514 final PgpEngine pgpEngine = getPgpEngine();
515 if (pgpEngine != null) {
516 pgpEngine.encrypt(message, callback);
517 } else if (callback != null) {
518 callback.error(R.string.unable_to_connect_to_keychain, null);
519 }
520 } else {
521 sendMessage(message);
522 callback.success(message);
523 }
524 } catch (final FileBackend.FileCopyException e) {
525 callback.error(e.getResId(), message);
526 }
527 });
528 }
529
530 public Conversation find(Bookmark bookmark) {
531 return find(bookmark.getAccount(), bookmark.getJid());
532 }
533
534 public Conversation find(final Account account, final Jid jid) {
535 return find(getConversations(), account, jid);
536 }
537
538 public void search(List<String> term, OnSearchResultsAvailable onSearchResultsAvailable) {
539 MessageSearchTask.search(this, term, onSearchResultsAvailable);
540 }
541
542 @Override
543 public int onStartCommand(Intent intent, int flags, int startId) {
544 final String action = intent == null ? null : intent.getAction();
545 String pushedAccountHash = null;
546 boolean interactive = false;
547 if (action != null) {
548 final String uuid = intent.getStringExtra("uuid");
549 switch (action) {
550 case ConnectivityManager.CONNECTIVITY_ACTION:
551 if (hasInternetConnection() && Config.RESET_ATTEMPT_COUNT_ON_NETWORK_CHANGE) {
552 resetAllAttemptCounts(true, false);
553 }
554 break;
555 case ACTION_MERGE_PHONE_CONTACTS:
556 if (restoredFromDatabaseLatch.getCount() == 0) {
557 loadPhoneContacts();
558 }
559 return START_STICKY;
560 case Intent.ACTION_SHUTDOWN:
561 logoutAndSave(true);
562 return START_NOT_STICKY;
563 case ACTION_CLEAR_NOTIFICATION:
564 mNotificationExecutor.execute(() -> {
565 try {
566 final Conversation c = findConversationByUuid(uuid);
567 if (c != null) {
568 mNotificationService.clear(c);
569 } else {
570 mNotificationService.clear();
571 }
572 restoredFromDatabaseLatch.await();
573
574 } catch (InterruptedException e) {
575 Log.d(Config.LOGTAG, "unable to process clear notification");
576 }
577 });
578 break;
579 case ACTION_DISMISS_ERROR_NOTIFICATIONS:
580 dismissErrorNotifications();
581 break;
582 case ACTION_TRY_AGAIN:
583 resetAllAttemptCounts(false, true);
584 interactive = true;
585 break;
586 case ACTION_REPLY_TO_CONVERSATION:
587 Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
588 if (remoteInput == null) {
589 break;
590 }
591 final CharSequence body = remoteInput.getCharSequence("text_reply");
592 final boolean dismissNotification = intent.getBooleanExtra("dismiss_notification", false);
593 if (body == null || body.length() <= 0) {
594 break;
595 }
596 mNotificationExecutor.execute(() -> {
597 try {
598 restoredFromDatabaseLatch.await();
599 final Conversation c = findConversationByUuid(uuid);
600 if (c != null) {
601 directReply(c, body.toString(), dismissNotification);
602 }
603 } catch (InterruptedException e) {
604 Log.d(Config.LOGTAG, "unable to process direct reply");
605 }
606 });
607 break;
608 case ACTION_MARK_AS_READ:
609 mNotificationExecutor.execute(() -> {
610 final Conversation c = findConversationByUuid(uuid);
611 if (c == null) {
612 Log.d(Config.LOGTAG, "received mark read intent for unknown conversation (" + uuid + ")");
613 return;
614 }
615 try {
616 restoredFromDatabaseLatch.await();
617 sendReadMarker(c, null);
618 } catch (InterruptedException e) {
619 Log.d(Config.LOGTAG, "unable to process notification read marker for conversation " + c.getName());
620 }
621
622 });
623 break;
624 case ACTION_SNOOZE:
625 mNotificationExecutor.execute(() -> {
626 final Conversation c = findConversationByUuid(uuid);
627 if (c == null) {
628 Log.d(Config.LOGTAG, "received snooze intent for unknown conversation (" + uuid + ")");
629 return;
630 }
631 c.setMutedTill(System.currentTimeMillis() + 30 * 60 * 1000);
632 mNotificationService.clear(c);
633 updateConversation(c);
634 });
635 case AudioManager.RINGER_MODE_CHANGED_ACTION:
636 if (dndOnSilentMode()) {
637 refreshAllPresences();
638 }
639 break;
640 case Intent.ACTION_SCREEN_ON:
641 deactivateGracePeriod();
642 case Intent.ACTION_SCREEN_OFF:
643 if (awayWhenScreenOff()) {
644 refreshAllPresences();
645 }
646 break;
647 case ACTION_GCM_TOKEN_REFRESH:
648 refreshAllGcmTokens();
649 break;
650 case ACTION_IDLE_PING:
651 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
652 scheduleNextIdlePing();
653 }
654 break;
655 case ACTION_GCM_MESSAGE_RECEIVED:
656 Log.d(Config.LOGTAG, "gcm push message arrived in service. extras=" + intent.getExtras());
657 pushedAccountHash = intent.getStringExtra("account");
658 break;
659 case Intent.ACTION_SEND:
660 Uri uri = intent.getData();
661 if (uri != null) {
662 Log.d(Config.LOGTAG, "received uri permission for " + uri.toString());
663 }
664 return START_STICKY;
665 }
666 }
667 synchronized (this) {
668 WakeLockHelper.acquire(wakeLock);
669 boolean pingNow = ConnectivityManager.CONNECTIVITY_ACTION.equals(action);
670 HashSet<Account> pingCandidates = new HashSet<>();
671 for (Account account : accounts) {
672 pingNow |= processAccountState(account,
673 interactive,
674 "ui".equals(action),
675 CryptoHelper.getAccountFingerprint(account).equals(pushedAccountHash),
676 pingCandidates);
677 }
678 if (pingNow) {
679 for (Account account : pingCandidates) {
680 final boolean lowTimeout = isInLowPingTimeoutMode(account);
681 account.getXmppConnection().sendPing();
682 Log.d(Config.LOGTAG, account.getJid().asBareJid() + " send ping (action=" + action + ",lowTimeout=" + Boolean.toString(lowTimeout) + ")");
683 scheduleWakeUpCall(lowTimeout ? Config.LOW_PING_TIMEOUT : Config.PING_TIMEOUT, account.getUuid().hashCode());
684 }
685 }
686 WakeLockHelper.release(wakeLock);
687 }
688 if (SystemClock.elapsedRealtime() - mLastExpiryRun.get() >= Config.EXPIRY_INTERVAL) {
689 expireOldMessages();
690 }
691 return START_STICKY;
692 }
693
694 private boolean processAccountState(Account account, boolean interactive, boolean isUiAction, boolean isAccountPushed, HashSet<Account> pingCandidates) {
695 boolean pingNow = false;
696 if (account.getStatus().isAttemptReconnect()) {
697 if (!hasInternetConnection()) {
698 account.setStatus(Account.State.NO_INTERNET);
699 if (statusListener != null) {
700 statusListener.onStatusChanged(account);
701 }
702 } else {
703 if (account.getStatus() == Account.State.NO_INTERNET) {
704 account.setStatus(Account.State.OFFLINE);
705 if (statusListener != null) {
706 statusListener.onStatusChanged(account);
707 }
708 }
709 if (account.getStatus() == Account.State.ONLINE) {
710 synchronized (mLowPingTimeoutMode) {
711 long lastReceived = account.getXmppConnection().getLastPacketReceived();
712 long lastSent = account.getXmppConnection().getLastPingSent();
713 long pingInterval = isUiAction ? Config.PING_MIN_INTERVAL * 1000 : Config.PING_MAX_INTERVAL * 1000;
714 long msToNextPing = (Math.max(lastReceived, lastSent) + pingInterval) - SystemClock.elapsedRealtime();
715 int pingTimeout = mLowPingTimeoutMode.contains(account.getJid().asBareJid()) ? Config.LOW_PING_TIMEOUT * 1000 : Config.PING_TIMEOUT * 1000;
716 long pingTimeoutIn = (lastSent + pingTimeout) - SystemClock.elapsedRealtime();
717 if (lastSent > lastReceived) {
718 if (pingTimeoutIn < 0) {
719 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ping timeout");
720 this.reconnectAccount(account, true, interactive);
721 } else {
722 int secs = (int) (pingTimeoutIn / 1000);
723 this.scheduleWakeUpCall(secs, account.getUuid().hashCode());
724 }
725 } else {
726 pingCandidates.add(account);
727 if (isAccountPushed) {
728 pingNow = true;
729 if (mLowPingTimeoutMode.add(account.getJid().asBareJid())) {
730 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": entering low ping timeout mode");
731 }
732 } else if (msToNextPing <= 0) {
733 pingNow = true;
734 } else {
735 this.scheduleWakeUpCall((int) (msToNextPing / 1000), account.getUuid().hashCode());
736 if (mLowPingTimeoutMode.remove(account.getJid().asBareJid())) {
737 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": leaving low ping timeout mode");
738 }
739 }
740 }
741 }
742 } else if (account.getStatus() == Account.State.OFFLINE) {
743 reconnectAccount(account, true, interactive);
744 } else if (account.getStatus() == Account.State.CONNECTING) {
745 long secondsSinceLastConnect = (SystemClock.elapsedRealtime() - account.getXmppConnection().getLastConnect()) / 1000;
746 long secondsSinceLastDisco = (SystemClock.elapsedRealtime() - account.getXmppConnection().getLastDiscoStarted()) / 1000;
747 long discoTimeout = Config.CONNECT_DISCO_TIMEOUT - secondsSinceLastDisco;
748 long timeout = Config.CONNECT_TIMEOUT - secondsSinceLastConnect;
749 if (timeout < 0) {
750 Log.d(Config.LOGTAG, account.getJid() + ": time out during connect reconnecting (secondsSinceLast=" + secondsSinceLastConnect + ")");
751 account.getXmppConnection().resetAttemptCount(false);
752 reconnectAccount(account, true, interactive);
753 } else if (discoTimeout < 0) {
754 account.getXmppConnection().sendDiscoTimeout();
755 scheduleWakeUpCall((int) Math.min(timeout, discoTimeout), account.getUuid().hashCode());
756 } else {
757 scheduleWakeUpCall((int) Math.min(timeout, discoTimeout), account.getUuid().hashCode());
758 }
759 } else {
760 if (account.getXmppConnection().getTimeToNextAttempt() <= 0) {
761 reconnectAccount(account, true, interactive);
762 }
763 }
764 }
765 }
766 return pingNow;
767 }
768
769 public boolean isDataSaverDisabled() {
770 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
771 ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
772 return !connectivityManager.isActiveNetworkMetered()
773 || connectivityManager.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED;
774 } else {
775 return true;
776 }
777 }
778
779 private void directReply(Conversation conversation, String body, final boolean dismissAfterReply) {
780 Message message = new Message(conversation, body, conversation.getNextEncryption());
781 message.markUnread();
782 if (message.getEncryption() == Message.ENCRYPTION_PGP) {
783 getPgpEngine().encrypt(message, new UiCallback<Message>() {
784 @Override
785 public void success(Message message) {
786 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
787 sendMessage(message);
788 if (dismissAfterReply) {
789 markRead((Conversation) message.getConversation(), true);
790 } else {
791 mNotificationService.pushFromDirectReply(message);
792 }
793 }
794
795 @Override
796 public void error(int errorCode, Message object) {
797
798 }
799
800 @Override
801 public void userInputRequried(PendingIntent pi, Message object) {
802
803 }
804 });
805 } else {
806 sendMessage(message);
807 if (dismissAfterReply) {
808 markRead(conversation, true);
809 } else {
810 mNotificationService.pushFromDirectReply(message);
811 }
812 }
813 }
814
815 private boolean dndOnSilentMode() {
816 return getBooleanPreference(SettingsActivity.DND_ON_SILENT_MODE, R.bool.dnd_on_silent_mode);
817 }
818
819 private boolean manuallyChangePresence() {
820 return getBooleanPreference(SettingsActivity.MANUALLY_CHANGE_PRESENCE, R.bool.manually_change_presence);
821 }
822
823 private boolean treatVibrateAsSilent() {
824 return getBooleanPreference(SettingsActivity.TREAT_VIBRATE_AS_SILENT, R.bool.treat_vibrate_as_silent);
825 }
826
827 private boolean awayWhenScreenOff() {
828 return getBooleanPreference(SettingsActivity.AWAY_WHEN_SCREEN_IS_OFF, R.bool.away_when_screen_off);
829 }
830
831 private String getCompressPicturesPreference() {
832 return getPreferences().getString("picture_compression", getResources().getString(R.string.picture_compression));
833 }
834
835 private Presence.Status getTargetPresence() {
836 if (dndOnSilentMode() && isPhoneSilenced()) {
837 return Presence.Status.DND;
838 } else if (awayWhenScreenOff() && !isInteractive()) {
839 return Presence.Status.AWAY;
840 } else {
841 return Presence.Status.ONLINE;
842 }
843 }
844
845 @SuppressLint("NewApi")
846 @SuppressWarnings("deprecation")
847 public boolean isInteractive() {
848 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
849
850 final boolean isScreenOn;
851 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
852 isScreenOn = pm.isScreenOn();
853 } else {
854 isScreenOn = pm.isInteractive();
855 }
856 return isScreenOn;
857 }
858
859 private boolean isPhoneSilenced() {
860 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
861 try {
862 if (treatVibrateAsSilent()) {
863 return audioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
864 } else {
865 return audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT;
866 }
867 } catch (Throwable throwable) {
868 Log.d(Config.LOGTAG, "platform bug in isPhoneSilenced (" + throwable.getMessage() + ")");
869 return false;
870 }
871 }
872
873 private void resetAllAttemptCounts(boolean reallyAll, boolean retryImmediately) {
874 Log.d(Config.LOGTAG, "resetting all attempt counts");
875 for (Account account : accounts) {
876 if (account.hasErrorStatus() || reallyAll) {
877 final XmppConnection connection = account.getXmppConnection();
878 if (connection != null) {
879 connection.resetAttemptCount(retryImmediately);
880 }
881 }
882 if (account.setShowErrorNotification(true)) {
883 databaseBackend.updateAccount(account);
884 }
885 }
886 mNotificationService.updateErrorNotification();
887 }
888
889 private void dismissErrorNotifications() {
890 for (final Account account : this.accounts) {
891 if (account.hasErrorStatus()) {
892 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": dismissing error notification");
893 if (account.setShowErrorNotification(false)) {
894 databaseBackend.updateAccount(account);
895 }
896 }
897 }
898 }
899
900 private void expireOldMessages() {
901 expireOldMessages(false);
902 }
903
904 public void expireOldMessages(final boolean resetHasMessagesLeftOnServer) {
905 mLastExpiryRun.set(SystemClock.elapsedRealtime());
906 mDatabaseWriterExecutor.execute(() -> {
907 long timestamp = getAutomaticMessageDeletionDate();
908 if (timestamp > 0) {
909 databaseBackend.expireOldMessages(timestamp);
910 synchronized (XmppConnectionService.this.conversations) {
911 for (Conversation conversation : XmppConnectionService.this.conversations) {
912 conversation.expireOldMessages(timestamp);
913 if (resetHasMessagesLeftOnServer) {
914 conversation.messagesLoaded.set(true);
915 conversation.setHasMessagesLeftOnServer(true);
916 }
917 }
918 }
919 updateConversationUi();
920 }
921 });
922 }
923
924 public boolean hasInternetConnection() {
925 final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
926 try {
927 final NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
928 return activeNetwork != null && activeNetwork.isConnected();
929 } catch (RuntimeException e) {
930 Log.d(Config.LOGTAG, "unable to check for internet connection", e);
931 return true; //if internet connection can not be checked it is probably best to just try
932 }
933 }
934
935 @SuppressLint("TrulyRandom")
936 @Override
937 public void onCreate() {
938 OmemoSetting.load(this);
939 ExceptionHelper.init(getApplicationContext());
940 PRNGFixes.apply();
941 Resolver.init(this);
942 this.mRandom = new SecureRandom();
943 updateMemorizingTrustmanager();
944 final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
945 final int cacheSize = maxMemory / 8;
946 this.mBitmapCache = new LruCache<String, Bitmap>(cacheSize) {
947 @Override
948 protected int sizeOf(final String key, final Bitmap bitmap) {
949 return bitmap.getByteCount() / 1024;
950 }
951 };
952
953 Log.d(Config.LOGTAG, "initializing database...");
954 this.databaseBackend = DatabaseBackend.getInstance(getApplicationContext());
955 Log.d(Config.LOGTAG, "restoring accounts...");
956 this.accounts = databaseBackend.getAccounts();
957 final SharedPreferences.Editor editor = getPreferences().edit();
958 if (this.accounts.size() == 0 && Arrays.asList("Sony", "Sony Ericsson").contains(Build.MANUFACTURER)) {
959 editor.putBoolean(SettingsActivity.KEEP_FOREGROUND_SERVICE, true);
960 Log.d(Config.LOGTAG, Build.MANUFACTURER + " is on blacklist. enabling foreground service");
961 }
962 editor.putBoolean(EventReceiver.SETTING_ENABLED_ACCOUNTS, hasEnabledAccounts()).apply();
963 editor.apply();
964
965 restoreFromDatabase();
966
967 getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactObserver);
968 new Thread(fileObserver::startWatching).start();
969 if (Config.supportOpenPgp()) {
970 this.pgpServiceConnection = new OpenPgpServiceConnection(this, "org.sufficientlysecure.keychain", new OpenPgpServiceConnection.OnBound() {
971 @Override
972 public void onBound(IOpenPgpService2 service) {
973 for (Account account : accounts) {
974 final PgpDecryptionService pgp = account.getPgpDecryptionService();
975 if (pgp != null) {
976 pgp.continueDecryption(true);
977 }
978 }
979 }
980
981 @Override
982 public void onError(Exception e) {
983 }
984 });
985 this.pgpServiceConnection.bindToService();
986 }
987
988 this.pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
989 this.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "XmppConnectionService");
990
991 toggleForegroundService();
992 updateUnreadCountBadge();
993 toggleScreenEventReceiver();
994 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
995 scheduleNextIdlePing();
996 }
997 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
998 registerReceiver(this.mEventReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
999 }
1000 }
1001
1002 @Override
1003 public void onTrimMemory(int level) {
1004 super.onTrimMemory(level);
1005 if (level >= TRIM_MEMORY_COMPLETE) {
1006 Log.d(Config.LOGTAG, "clear cache due to low memory");
1007 getBitmapCache().evictAll();
1008 }
1009 }
1010
1011 @Override
1012 public void onDestroy() {
1013 try {
1014 unregisterReceiver(this.mEventReceiver);
1015 } catch (IllegalArgumentException e) {
1016 //ignored
1017 }
1018 fileObserver.stopWatching();
1019 super.onDestroy();
1020 }
1021
1022 public void toggleScreenEventReceiver() {
1023 if (awayWhenScreenOff() && !manuallyChangePresence()) {
1024 final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
1025 filter.addAction(Intent.ACTION_SCREEN_OFF);
1026 registerReceiver(this.mEventReceiver, filter);
1027 } else {
1028 try {
1029 unregisterReceiver(this.mEventReceiver);
1030 } catch (IllegalArgumentException e) {
1031 //ignored
1032 }
1033 }
1034 }
1035
1036 public void toggleForegroundService() {
1037 if (mForceForegroundService.get() || (keepForegroundService() && hasEnabledAccounts())) {
1038 startForeground(NotificationService.FOREGROUND_NOTIFICATION_ID, this.mNotificationService.createForegroundNotification());
1039 Log.d(Config.LOGTAG, "started foreground service");
1040 } else {
1041 stopForeground(true);
1042 Log.d(Config.LOGTAG, "stopped foreground service");
1043 }
1044 }
1045
1046 public boolean keepForegroundService() {
1047 return getBooleanPreference(SettingsActivity.KEEP_FOREGROUND_SERVICE, R.bool.enable_foreground_service);
1048 }
1049
1050 @Override
1051 public void onTaskRemoved(final Intent rootIntent) {
1052 super.onTaskRemoved(rootIntent);
1053 if (keepForegroundService() || mForceForegroundService.get()) {
1054 Log.d(Config.LOGTAG, "ignoring onTaskRemoved because foreground service is activated");
1055 } else {
1056 this.logoutAndSave(false);
1057 }
1058 }
1059
1060 private void logoutAndSave(boolean stop) {
1061 int activeAccounts = 0;
1062 for (final Account account : accounts) {
1063 if (account.getStatus() != Account.State.DISABLED) {
1064 databaseBackend.writeRoster(account.getRoster());
1065 activeAccounts++;
1066 }
1067 if (account.getXmppConnection() != null) {
1068 new Thread(() -> disconnect(account, false)).start();
1069 }
1070 }
1071 if (stop || activeAccounts == 0) {
1072 Log.d(Config.LOGTAG, "good bye");
1073 stopSelf();
1074 }
1075 }
1076
1077 public void scheduleWakeUpCall(int seconds, int requestCode) {
1078 final long timeToWake = SystemClock.elapsedRealtime() + (seconds < 0 ? 1 : seconds + 1) * 1000;
1079 final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
1080 Intent intent = new Intent(this, EventReceiver.class);
1081 intent.setAction("ping");
1082 PendingIntent pendingIntent = PendingIntent.getBroadcast(this, requestCode, intent, 0);
1083 try {
1084 alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeToWake, pendingIntent);
1085 } catch (RuntimeException e) {
1086 Log.e(Config.LOGTAG, "unable to schedule alarm for ping", e);
1087 }
1088 }
1089
1090 @TargetApi(Build.VERSION_CODES.M)
1091 private void scheduleNextIdlePing() {
1092 final long timeToWake = SystemClock.elapsedRealtime() + (Config.IDLE_PING_INTERVAL * 1000);
1093 final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
1094 Intent intent = new Intent(this, EventReceiver.class);
1095 intent.setAction(ACTION_IDLE_PING);
1096 PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
1097 try {
1098 alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeToWake, pendingIntent);
1099 } catch (RuntimeException e) {
1100 Log.d(Config.LOGTAG, "unable to schedule alarm for idle ping", e);
1101 }
1102 }
1103
1104 public XmppConnection createConnection(final Account account) {
1105 final XmppConnection connection = new XmppConnection(account, this);
1106 connection.setOnMessagePacketReceivedListener(this.mMessageParser);
1107 connection.setOnStatusChangedListener(this.statusListener);
1108 connection.setOnPresencePacketReceivedListener(this.mPresenceParser);
1109 connection.setOnUnregisteredIqPacketReceivedListener(this.mIqParser);
1110 connection.setOnJinglePacketReceivedListener(this.jingleListener);
1111 connection.setOnBindListener(this.mOnBindListener);
1112 connection.setOnMessageAcknowledgeListener(this.mOnMessageAcknowledgedListener);
1113 connection.addOnAdvancedStreamFeaturesAvailableListener(this.mMessageArchiveService);
1114 connection.addOnAdvancedStreamFeaturesAvailableListener(this.mAvatarService);
1115 AxolotlService axolotlService = account.getAxolotlService();
1116 if (axolotlService != null) {
1117 connection.addOnAdvancedStreamFeaturesAvailableListener(axolotlService);
1118 }
1119 return connection;
1120 }
1121
1122 public void sendChatState(Conversation conversation) {
1123 if (sendChatStates()) {
1124 MessagePacket packet = mMessageGenerator.generateChatState(conversation);
1125 sendMessagePacket(conversation.getAccount(), packet);
1126 }
1127 }
1128
1129 private void sendFileMessage(final Message message, final boolean delay) {
1130 Log.d(Config.LOGTAG, "send file message");
1131 final Account account = message.getConversation().getAccount();
1132 if (account.httpUploadAvailable(fileBackend.getFile(message, false).getSize())
1133 || message.getConversation().getMode() == Conversation.MODE_MULTI) {
1134 mHttpConnectionManager.createNewUploadConnection(message, delay);
1135 } else {
1136 mJingleConnectionManager.createNewConnection(message);
1137 }
1138 }
1139
1140 public void sendMessage(final Message message) {
1141 sendMessage(message, false, false);
1142 }
1143
1144 private void sendMessage(final Message message, final boolean resend, final boolean delay) {
1145 final Account account = message.getConversation().getAccount();
1146 if (account.setShowErrorNotification(true)) {
1147 databaseBackend.updateAccount(account);
1148 mNotificationService.updateErrorNotification();
1149 }
1150 final Conversation conversation = (Conversation) message.getConversation();
1151 account.deactivateGracePeriod();
1152 MessagePacket packet = null;
1153 final boolean addToConversation = (conversation.getMode() != Conversation.MODE_MULTI
1154 || !Patches.BAD_MUC_REFLECTION.contains(account.getServerIdentity()))
1155 && !message.edited();
1156 boolean saveInDb = addToConversation;
1157 message.setStatus(Message.STATUS_WAITING);
1158
1159 if (account.isOnlineAndConnected()) {
1160 switch (message.getEncryption()) {
1161 case Message.ENCRYPTION_NONE:
1162 if (message.needsUploading()) {
1163 if (account.httpUploadAvailable(fileBackend.getFile(message, false).getSize())
1164 || conversation.getMode() == Conversation.MODE_MULTI
1165 || message.fixCounterpart()) {
1166 this.sendFileMessage(message, delay);
1167 } else {
1168 break;
1169 }
1170 } else {
1171 packet = mMessageGenerator.generateChat(message);
1172 }
1173 break;
1174 case Message.ENCRYPTION_PGP:
1175 case Message.ENCRYPTION_DECRYPTED:
1176 if (message.needsUploading()) {
1177 if (account.httpUploadAvailable(fileBackend.getFile(message, false).getSize())
1178 || conversation.getMode() == Conversation.MODE_MULTI
1179 || message.fixCounterpart()) {
1180 this.sendFileMessage(message, delay);
1181 } else {
1182 break;
1183 }
1184 } else {
1185 packet = mMessageGenerator.generatePgpChat(message);
1186 }
1187 break;
1188 case Message.ENCRYPTION_AXOLOTL:
1189 message.setFingerprint(account.getAxolotlService().getOwnFingerprint());
1190 if (message.needsUploading()) {
1191 if (account.httpUploadAvailable(fileBackend.getFile(message, false).getSize())
1192 || conversation.getMode() == Conversation.MODE_MULTI
1193 || message.fixCounterpart()) {
1194 this.sendFileMessage(message, delay);
1195 } else {
1196 break;
1197 }
1198 } else {
1199 XmppAxolotlMessage axolotlMessage = account.getAxolotlService().fetchAxolotlMessageFromCache(message);
1200 if (axolotlMessage == null) {
1201 account.getAxolotlService().preparePayloadMessage(message, delay);
1202 } else {
1203 packet = mMessageGenerator.generateAxolotlChat(message, axolotlMessage);
1204 }
1205 }
1206 break;
1207
1208 }
1209 if (packet != null) {
1210 if (account.getXmppConnection().getFeatures().sm()
1211 || (conversation.getMode() == Conversation.MODE_MULTI && message.getCounterpart().isBareJid())) {
1212 message.setStatus(Message.STATUS_UNSEND);
1213 } else {
1214 message.setStatus(Message.STATUS_SEND);
1215 }
1216 }
1217 } else {
1218 switch (message.getEncryption()) {
1219 case Message.ENCRYPTION_DECRYPTED:
1220 if (!message.needsUploading()) {
1221 String pgpBody = message.getEncryptedBody();
1222 String decryptedBody = message.getBody();
1223 message.setBody(pgpBody); //TODO might throw NPE
1224 message.setEncryption(Message.ENCRYPTION_PGP);
1225 if (message.edited()) {
1226 message.setBody(decryptedBody);
1227 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
1228 databaseBackend.updateMessage(message, message.getEditedId());
1229 updateConversationUi();
1230 return;
1231 } else {
1232 databaseBackend.createMessage(message);
1233 saveInDb = false;
1234 message.setBody(decryptedBody);
1235 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
1236 }
1237 }
1238 break;
1239 case Message.ENCRYPTION_AXOLOTL:
1240 message.setFingerprint(account.getAxolotlService().getOwnFingerprint());
1241 break;
1242 }
1243 }
1244
1245
1246 boolean mucMessage = conversation.getMode() == Conversation.MODE_MULTI && message.getType() != Message.TYPE_PRIVATE;
1247 if (mucMessage) {
1248 message.setCounterpart(conversation.getMucOptions().getSelf().getFullJid());
1249 }
1250
1251 if (resend) {
1252 if (packet != null && addToConversation) {
1253 if (account.getXmppConnection().getFeatures().sm() || mucMessage) {
1254 markMessage(message, Message.STATUS_UNSEND);
1255 } else {
1256 markMessage(message, Message.STATUS_SEND);
1257 }
1258 }
1259 } else {
1260 if (addToConversation) {
1261 conversation.add(message);
1262 }
1263 if (saveInDb) {
1264 databaseBackend.createMessage(message);
1265 } else if (message.edited()) {
1266 databaseBackend.updateMessage(message, message.getEditedId());
1267 }
1268 updateConversationUi();
1269 }
1270 if (packet != null) {
1271 if (delay) {
1272 mMessageGenerator.addDelay(packet, message.getTimeSent());
1273 }
1274 if (conversation.setOutgoingChatState(Config.DEFAULT_CHATSTATE)) {
1275 if (this.sendChatStates()) {
1276 packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
1277 }
1278 }
1279 sendMessagePacket(account, packet);
1280 }
1281 }
1282
1283 private void sendUnsentMessages(final Conversation conversation) {
1284 conversation.findWaitingMessages(new Conversation.OnMessageFound() {
1285
1286 @Override
1287 public void onMessageFound(Message message) {
1288 resendMessage(message, true);
1289 }
1290 });
1291 }
1292
1293 public void resendMessage(final Message message, final boolean delay) {
1294 sendMessage(message, true, delay);
1295 }
1296
1297 public void fetchRosterFromServer(final Account account) {
1298 final IqPacket iqPacket = new IqPacket(IqPacket.TYPE.GET);
1299 if (!"".equals(account.getRosterVersion())) {
1300 Log.d(Config.LOGTAG, account.getJid().asBareJid()
1301 + ": fetching roster version " + account.getRosterVersion());
1302 } else {
1303 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": fetching roster");
1304 }
1305 iqPacket.query(Namespace.ROSTER).setAttribute("ver", account.getRosterVersion());
1306 sendIqPacket(account, iqPacket, mIqParser);
1307 }
1308
1309 public void fetchBookmarks(final Account account) {
1310 final IqPacket iqPacket = new IqPacket(IqPacket.TYPE.GET);
1311 final Element query = iqPacket.query("jabber:iq:private");
1312 query.addChild("storage", "storage:bookmarks");
1313 final OnIqPacketReceived callback = new OnIqPacketReceived() {
1314
1315 @Override
1316 public void onIqPacketReceived(final Account account, final IqPacket packet) {
1317 if (packet.getType() == IqPacket.TYPE.RESULT) {
1318 final Element query = packet.query();
1319 final HashMap<Jid, Bookmark> bookmarks = new HashMap<>();
1320 final Element storage = query.findChild("storage", "storage:bookmarks");
1321 final boolean autojoin = respectAutojoin();
1322 if (storage != null) {
1323 for (final Element item : storage.getChildren()) {
1324 if (item.getName().equals("conference")) {
1325 final Bookmark bookmark = Bookmark.parse(item, account);
1326 Bookmark old = bookmarks.put(bookmark.getJid(), bookmark);
1327 if (old != null && old.getBookmarkName() != null && bookmark.getBookmarkName() == null) {
1328 bookmark.setBookmarkName(old.getBookmarkName());
1329 }
1330 Conversation conversation = find(bookmark);
1331 if (conversation != null) {
1332 bookmark.setConversation(conversation);
1333 } else if (bookmark.autojoin() && bookmark.getJid() != null && autojoin) {
1334 conversation = findOrCreateConversation(account, bookmark.getJid(), true, true, false);
1335 bookmark.setConversation(conversation);
1336 }
1337 }
1338 }
1339 }
1340 account.setBookmarks(new CopyOnWriteArrayList<>(bookmarks.values()));
1341 } else {
1342 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not fetch bookmarks");
1343 }
1344 }
1345 };
1346 sendIqPacket(account, iqPacket, callback);
1347 }
1348
1349 public void pushBookmarks(Account account) {
1350 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": pushing bookmarks");
1351 IqPacket iqPacket = new IqPacket(IqPacket.TYPE.SET);
1352 Element query = iqPacket.query("jabber:iq:private");
1353 Element storage = query.addChild("storage", "storage:bookmarks");
1354 for (Bookmark bookmark : account.getBookmarks()) {
1355 storage.addChild(bookmark);
1356 }
1357 sendIqPacket(account, iqPacket, mDefaultIqHandler);
1358 }
1359
1360 private void restoreFromDatabase() {
1361 synchronized (this.conversations) {
1362 final Map<String, Account> accountLookupTable = new Hashtable<>();
1363 for (Account account : this.accounts) {
1364 accountLookupTable.put(account.getUuid(), account);
1365 }
1366 Log.d(Config.LOGTAG, "restoring conversations...");
1367 final long startTimeConversationsRestore = SystemClock.elapsedRealtime();
1368 this.conversations.addAll(databaseBackend.getConversations(Conversation.STATUS_AVAILABLE));
1369 for (Iterator<Conversation> iterator = conversations.listIterator(); iterator.hasNext(); ) {
1370 Conversation conversation = iterator.next();
1371 Account account = accountLookupTable.get(conversation.getAccountUuid());
1372 if (account != null) {
1373 conversation.setAccount(account);
1374 } else {
1375 Log.e(Config.LOGTAG, "unable to restore Conversations with " + conversation.getJid());
1376 iterator.remove();
1377 }
1378 }
1379 long diffConversationsRestore = SystemClock.elapsedRealtime() - startTimeConversationsRestore;
1380 Log.d(Config.LOGTAG, "finished restoring conversations in " + diffConversationsRestore + "ms");
1381 Runnable runnable = () -> {
1382 long deletionDate = getAutomaticMessageDeletionDate();
1383 mLastExpiryRun.set(SystemClock.elapsedRealtime());
1384 if (deletionDate > 0) {
1385 Log.d(Config.LOGTAG, "deleting messages that are older than " + AbstractGenerator.getTimestamp(deletionDate));
1386 databaseBackend.expireOldMessages(deletionDate);
1387 }
1388 Log.d(Config.LOGTAG, "restoring roster...");
1389 for (Account account : accounts) {
1390 databaseBackend.readRoster(account.getRoster());
1391 account.initAccountServices(XmppConnectionService.this); //roster needs to be loaded at this stage
1392 }
1393 getBitmapCache().evictAll();
1394 loadPhoneContacts();
1395 Log.d(Config.LOGTAG, "restoring messages...");
1396 final long startMessageRestore = SystemClock.elapsedRealtime();
1397 final Conversation quickLoad = QuickLoader.get(this.conversations);
1398 if (quickLoad != null) {
1399 restoreMessages(quickLoad);
1400 updateConversationUi();
1401 final long diffMessageRestore = SystemClock.elapsedRealtime() - startMessageRestore;
1402 Log.d(Config.LOGTAG,"quickly restored "+quickLoad.getName()+" after " + diffMessageRestore + "ms");
1403 }
1404 for (Conversation conversation : this.conversations) {
1405 if (quickLoad != conversation) {
1406 restoreMessages(conversation);
1407 }
1408 }
1409 mNotificationService.finishBacklog(false);
1410 restoredFromDatabaseLatch.countDown();
1411 final long diffMessageRestore = SystemClock.elapsedRealtime() - startMessageRestore;
1412 Log.d(Config.LOGTAG, "finished restoring messages in " + diffMessageRestore + "ms");
1413 updateConversationUi();
1414 };
1415 mDatabaseReaderExecutor.execute(runnable); //will contain one write command (expiry) but that's fine
1416 }
1417 }
1418
1419 private void restoreMessages(Conversation conversation) {
1420 conversation.addAll(0, databaseBackend.getMessages(conversation, Config.PAGE_SIZE));
1421 checkDeletedFiles(conversation);
1422 conversation.findUnsentTextMessages(message -> markMessage(message, Message.STATUS_WAITING));
1423 conversation.findUnreadMessages(message -> mNotificationService.pushFromBacklog(message));
1424 }
1425
1426 public void loadPhoneContacts() {
1427 mContactMergerExecutor.execute(() -> PhoneHelper.loadPhoneContacts(XmppConnectionService.this, new OnPhoneContactsLoadedListener() {
1428 @Override
1429 public void onPhoneContactsLoaded(List<Bundle> phoneContacts) {
1430 Log.d(Config.LOGTAG, "start merging phone contacts with roster");
1431 for (Account account : accounts) {
1432 List<Contact> withSystemAccounts = account.getRoster().getWithSystemAccounts();
1433 for (Bundle phoneContact : phoneContacts) {
1434 Jid jid;
1435 try {
1436 jid = Jid.of(phoneContact.getString("jid"));
1437 } catch (final IllegalArgumentException e) {
1438 continue;
1439 }
1440 final Contact contact = account.getRoster().getContact(jid);
1441 String systemAccount = phoneContact.getInt("phoneid")
1442 + "#"
1443 + phoneContact.getString("lookup");
1444 contact.setSystemAccount(systemAccount);
1445 boolean needsCacheClean = contact.setPhotoUri(phoneContact.getString("photouri"));
1446 needsCacheClean |= contact.setSystemName(phoneContact.getString("displayname"));
1447 if (needsCacheClean) {
1448 getAvatarService().clear(contact);
1449 }
1450 withSystemAccounts.remove(contact);
1451 }
1452 for (Contact contact : withSystemAccounts) {
1453 contact.setSystemAccount(null);
1454 boolean needsCacheClean = contact.setPhotoUri(null);
1455 needsCacheClean |= contact.setSystemName(null);
1456 if (needsCacheClean) {
1457 getAvatarService().clear(contact);
1458 }
1459 }
1460 }
1461 Log.d(Config.LOGTAG, "finished merging phone contacts");
1462 mShortcutService.refresh(mInitialAddressbookSyncCompleted.compareAndSet(false, true));
1463 updateAccountUi();
1464 }
1465 }));
1466 }
1467
1468
1469 public void syncRoster(final Account account) {
1470 mRosterSyncTaskManager.execute(account, () -> databaseBackend.writeRoster(account.getRoster()));
1471 }
1472
1473 public List<Conversation> getConversations() {
1474 return this.conversations;
1475 }
1476
1477 private void checkDeletedFiles(Conversation conversation) {
1478 conversation.findMessagesWithFiles(message -> {
1479 if (!getFileBackend().isFileAvailable(message)) {
1480 message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_DELETED));
1481 final int s = message.getStatus();
1482 if (s == Message.STATUS_WAITING || s == Message.STATUS_OFFERED || s == Message.STATUS_UNSEND) {
1483 markMessage(message, Message.STATUS_SEND_FAILED);
1484 }
1485 }
1486 });
1487 }
1488
1489 private void markFileDeleted(final String path) {
1490 Log.d(Config.LOGTAG, "deleted file " + path);
1491 for (Conversation conversation : getConversations()) {
1492 conversation.findMessagesWithFiles(message -> {
1493 DownloadableFile file = fileBackend.getFile(message);
1494 if (file.getAbsolutePath().equals(path)) {
1495 if (!file.exists()) {
1496 message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_DELETED));
1497 final int s = message.getStatus();
1498 if (s == Message.STATUS_WAITING || s == Message.STATUS_OFFERED || s == Message.STATUS_UNSEND) {
1499 markMessage(message, Message.STATUS_SEND_FAILED);
1500 } else {
1501 updateConversationUi();
1502 }
1503 } else {
1504 Log.d(Config.LOGTAG, "found matching message for file " + path + " but file still exists");
1505 }
1506 }
1507 });
1508 }
1509 }
1510
1511 public void populateWithOrderedConversations(final List<Conversation> list) {
1512 populateWithOrderedConversations(list, true);
1513 }
1514
1515 public void populateWithOrderedConversations(final List<Conversation> list, boolean includeNoFileUpload) {
1516 list.clear();
1517 if (includeNoFileUpload) {
1518 list.addAll(getConversations());
1519 } else {
1520 for (Conversation conversation : getConversations()) {
1521 if (conversation.getMode() == Conversation.MODE_SINGLE
1522 || conversation.getAccount().httpUploadAvailable()) {
1523 list.add(conversation);
1524 }
1525 }
1526 }
1527 try {
1528 Collections.sort(list);
1529 } catch (IllegalArgumentException e) {
1530 //ignore
1531 }
1532 }
1533
1534 public void loadMoreMessages(final Conversation conversation, final long timestamp, final OnMoreMessagesLoaded callback) {
1535 if (XmppConnectionService.this.getMessageArchiveService().queryInProgress(conversation, callback)) {
1536 return;
1537 } else if (timestamp == 0) {
1538 return;
1539 }
1540 Log.d(Config.LOGTAG, "load more messages for " + conversation.getName() + " prior to " + MessageGenerator.getTimestamp(timestamp));
1541 final Runnable runnable = () -> {
1542 final Account account = conversation.getAccount();
1543 List<Message> messages = databaseBackend.getMessages(conversation, 50, timestamp);
1544 if (messages.size() > 0) {
1545 conversation.addAll(0, messages);
1546 checkDeletedFiles(conversation);
1547 callback.onMoreMessagesLoaded(messages.size(), conversation);
1548 } else if (conversation.hasMessagesLeftOnServer()
1549 && account.isOnlineAndConnected()
1550 && conversation.getLastClearHistory().getTimestamp() == 0) {
1551 final boolean mamAvailable;
1552 if (conversation.getMode() == Conversation.MODE_SINGLE) {
1553 mamAvailable = account.getXmppConnection().getFeatures().mam() && !conversation.getContact().isBlocked();
1554 } else {
1555 mamAvailable = conversation.getMucOptions().mamSupport();
1556 }
1557 if (mamAvailable) {
1558 MessageArchiveService.Query query = getMessageArchiveService().query(conversation, new MamReference(0), timestamp, false);
1559 if (query != null) {
1560 query.setCallback(callback);
1561 callback.informUser(R.string.fetching_history_from_server);
1562 } else {
1563 callback.informUser(R.string.not_fetching_history_retention_period);
1564 }
1565
1566 }
1567 }
1568 };
1569 mDatabaseReaderExecutor.execute(runnable);
1570 }
1571
1572 public List<Account> getAccounts() {
1573 return this.accounts;
1574 }
1575
1576 public List<Conversation> findAllConferencesWith(Contact contact) {
1577 ArrayList<Conversation> results = new ArrayList<>();
1578 for (final Conversation c : conversations) {
1579 if (c.getMode() == Conversation.MODE_MULTI
1580 && (c.getJid().asBareJid().equals(c.getJid().asBareJid()) || c.getMucOptions().isContactInRoom(contact))) {
1581 results.add(c);
1582 }
1583 }
1584 return results;
1585 }
1586
1587 public Conversation find(final Iterable<Conversation> haystack, final Contact contact) {
1588 for (final Conversation conversation : haystack) {
1589 if (conversation.getContact() == contact) {
1590 return conversation;
1591 }
1592 }
1593 return null;
1594 }
1595
1596 public Conversation find(final Iterable<Conversation> haystack, final Account account, final Jid jid) {
1597 if (jid == null) {
1598 return null;
1599 }
1600 for (final Conversation conversation : haystack) {
1601 if ((account == null || conversation.getAccount() == account)
1602 && (conversation.getJid().asBareJid().equals(jid.asBareJid()))) {
1603 return conversation;
1604 }
1605 }
1606 return null;
1607 }
1608
1609 public boolean isConversationsListEmpty(final Conversation ignore) {
1610 synchronized (this.conversations) {
1611 final int size = this.conversations.size();
1612 return size == 0 || size == 1 && this.conversations.get(0) == ignore;
1613 }
1614 }
1615
1616 public boolean isConversationStillOpen(final Conversation conversation) {
1617 synchronized (this.conversations) {
1618 for (Conversation current : this.conversations) {
1619 if (current == conversation) {
1620 return true;
1621 }
1622 }
1623 }
1624 return false;
1625 }
1626
1627 public Conversation findOrCreateConversation(Account account, Jid jid, boolean muc, final boolean async) {
1628 return this.findOrCreateConversation(account, jid, muc, false, async);
1629 }
1630
1631 public Conversation findOrCreateConversation(final Account account, final Jid jid, final boolean muc, final boolean joinAfterCreate, final boolean async) {
1632 return this.findOrCreateConversation(account, jid, muc, joinAfterCreate, null, async);
1633 }
1634
1635 public Conversation findOrCreateConversation(final Account account, final Jid jid, final boolean muc, final boolean joinAfterCreate, final MessageArchiveService.Query query, final boolean async) {
1636 synchronized (this.conversations) {
1637 Conversation conversation = find(account, jid);
1638 if (conversation != null) {
1639 return conversation;
1640 }
1641 conversation = databaseBackend.findConversation(account, jid);
1642 final boolean loadMessagesFromDb;
1643 if (conversation != null) {
1644 conversation.setStatus(Conversation.STATUS_AVAILABLE);
1645 conversation.setAccount(account);
1646 if (muc) {
1647 conversation.setMode(Conversation.MODE_MULTI);
1648 conversation.setContactJid(jid);
1649 } else {
1650 conversation.setMode(Conversation.MODE_SINGLE);
1651 conversation.setContactJid(jid.asBareJid());
1652 }
1653 databaseBackend.updateConversation(conversation);
1654 loadMessagesFromDb = conversation.messagesLoaded.compareAndSet(true, false);
1655 } else {
1656 String conversationName;
1657 Contact contact = account.getRoster().getContact(jid);
1658 if (contact != null) {
1659 conversationName = contact.getDisplayName();
1660 } else {
1661 conversationName = jid.getLocal();
1662 }
1663 if (muc) {
1664 conversation = new Conversation(conversationName, account, jid,
1665 Conversation.MODE_MULTI);
1666 } else {
1667 conversation = new Conversation(conversationName, account, jid.asBareJid(),
1668 Conversation.MODE_SINGLE);
1669 }
1670 this.databaseBackend.createConversation(conversation);
1671 loadMessagesFromDb = false;
1672 }
1673 final Conversation c = conversation;
1674 final Runnable runnable = () -> {
1675 if (loadMessagesFromDb) {
1676 c.addAll(0, databaseBackend.getMessages(c, Config.PAGE_SIZE));
1677 updateConversationUi();
1678 c.messagesLoaded.set(true);
1679 }
1680 if (account.getXmppConnection() != null
1681 && !c.getContact().isBlocked()
1682 && account.getXmppConnection().getFeatures().mam()
1683 && !muc) {
1684 if (query == null) {
1685 mMessageArchiveService.query(c);
1686 } else {
1687 if (query.getConversation() == null) {
1688 mMessageArchiveService.query(c, query.getStart(), query.isCatchup());
1689 }
1690 }
1691 }
1692 checkDeletedFiles(c);
1693 if (joinAfterCreate) {
1694 joinMuc(c);
1695 }
1696 };
1697 if (async) {
1698 mDatabaseReaderExecutor.execute(runnable);
1699 } else {
1700 runnable.run();
1701 }
1702 this.conversations.add(conversation);
1703 updateConversationUi();
1704 return conversation;
1705 }
1706 }
1707
1708 public void archiveConversation(Conversation conversation) {
1709 getNotificationService().clear(conversation);
1710 conversation.setStatus(Conversation.STATUS_ARCHIVED);
1711 conversation.setNextMessage(null);
1712 synchronized (this.conversations) {
1713 getMessageArchiveService().kill(conversation);
1714 if (conversation.getMode() == Conversation.MODE_MULTI) {
1715 if (conversation.getAccount().getStatus() == Account.State.ONLINE) {
1716 Bookmark bookmark = conversation.getBookmark();
1717 if (bookmark != null && bookmark.autojoin() && respectAutojoin()) {
1718 bookmark.setAutojoin(false);
1719 pushBookmarks(bookmark.getAccount());
1720 }
1721 }
1722 leaveMuc(conversation);
1723 } else {
1724 if (conversation.getContact().getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
1725 Log.d(Config.LOGTAG, "Canceling presence request from " + conversation.getJid().toString());
1726 sendPresencePacket(
1727 conversation.getAccount(),
1728 mPresenceGenerator.stopPresenceUpdatesTo(conversation.getContact())
1729 );
1730 }
1731 }
1732 updateConversation(conversation);
1733 this.conversations.remove(conversation);
1734 updateConversationUi();
1735 }
1736 }
1737
1738 public void createAccount(final Account account) {
1739 account.initAccountServices(this);
1740 databaseBackend.createAccount(account);
1741 this.accounts.add(account);
1742 this.reconnectAccountInBackground(account);
1743 updateAccountUi();
1744 syncEnabledAccountSetting();
1745 toggleForegroundService();
1746 }
1747
1748 private void syncEnabledAccountSetting() {
1749 getPreferences().edit().putBoolean(EventReceiver.SETTING_ENABLED_ACCOUNTS, hasEnabledAccounts()).apply();
1750 }
1751
1752 public void createAccountFromKey(final String alias, final OnAccountCreated callback) {
1753 new Thread(() -> {
1754 try {
1755 final X509Certificate[] chain = KeyChain.getCertificateChain(this, alias);
1756 final X509Certificate cert = chain != null && chain.length > 0 ? chain[0] : null;
1757 if (cert == null) {
1758 callback.informUser(R.string.unable_to_parse_certificate);
1759 return;
1760 }
1761 Pair<Jid, String> info = CryptoHelper.extractJidAndName(cert);
1762 if (info == null) {
1763 callback.informUser(R.string.certificate_does_not_contain_jid);
1764 return;
1765 }
1766 if (findAccountByJid(info.first) == null) {
1767 Account account = new Account(info.first, "");
1768 account.setPrivateKeyAlias(alias);
1769 account.setOption(Account.OPTION_DISABLED, true);
1770 account.setDisplayName(info.second);
1771 createAccount(account);
1772 callback.onAccountCreated(account);
1773 if (Config.X509_VERIFICATION) {
1774 try {
1775 getMemorizingTrustManager().getNonInteractive(account.getJid().getDomain()).checkClientTrusted(chain, "RSA");
1776 } catch (CertificateException e) {
1777 callback.informUser(R.string.certificate_chain_is_not_trusted);
1778 }
1779 }
1780 } else {
1781 callback.informUser(R.string.account_already_exists);
1782 }
1783 } catch (Exception e) {
1784 e.printStackTrace();
1785 callback.informUser(R.string.unable_to_parse_certificate);
1786 }
1787 }).start();
1788
1789 }
1790
1791 public void updateKeyInAccount(final Account account, final String alias) {
1792 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": update key in account " + alias);
1793 try {
1794 X509Certificate[] chain = KeyChain.getCertificateChain(XmppConnectionService.this, alias);
1795 Log.d(Config.LOGTAG, account.getJid().asBareJid() + " loaded certificate chain");
1796 Pair<Jid, String> info = CryptoHelper.extractJidAndName(chain[0]);
1797 if (info == null) {
1798 showErrorToastInUi(R.string.certificate_does_not_contain_jid);
1799 return;
1800 }
1801 if (account.getJid().asBareJid().equals(info.first)) {
1802 account.setPrivateKeyAlias(alias);
1803 account.setDisplayName(info.second);
1804 databaseBackend.updateAccount(account);
1805 if (Config.X509_VERIFICATION) {
1806 try {
1807 getMemorizingTrustManager().getNonInteractive().checkClientTrusted(chain, "RSA");
1808 } catch (CertificateException e) {
1809 showErrorToastInUi(R.string.certificate_chain_is_not_trusted);
1810 }
1811 account.getAxolotlService().regenerateKeys(true);
1812 }
1813 } else {
1814 showErrorToastInUi(R.string.jid_does_not_match_certificate);
1815 }
1816 } catch (Exception e) {
1817 e.printStackTrace();
1818 }
1819 }
1820
1821 public boolean updateAccount(final Account account) {
1822 if (databaseBackend.updateAccount(account)) {
1823 account.setShowErrorNotification(true);
1824 this.statusListener.onStatusChanged(account);
1825 databaseBackend.updateAccount(account);
1826 reconnectAccountInBackground(account);
1827 updateAccountUi();
1828 getNotificationService().updateErrorNotification();
1829 toggleForegroundService();
1830 syncEnabledAccountSetting();
1831 return true;
1832 } else {
1833 return false;
1834 }
1835 }
1836
1837 public void updateAccountPasswordOnServer(final Account account, final String newPassword, final OnAccountPasswordChanged callback) {
1838 final IqPacket iq = getIqGenerator().generateSetPassword(account, newPassword);
1839 sendIqPacket(account, iq, (a, packet) -> {
1840 if (packet.getType() == IqPacket.TYPE.RESULT) {
1841 a.setPassword(newPassword);
1842 a.setOption(Account.OPTION_MAGIC_CREATE, false);
1843 databaseBackend.updateAccount(a);
1844 callback.onPasswordChangeSucceeded();
1845 } else {
1846 callback.onPasswordChangeFailed();
1847 }
1848 });
1849 }
1850
1851 public void deleteAccount(final Account account) {
1852 synchronized (this.conversations) {
1853 for (final Conversation conversation : conversations) {
1854 if (conversation.getAccount() == account) {
1855 if (conversation.getMode() == Conversation.MODE_MULTI) {
1856 leaveMuc(conversation);
1857 }
1858 conversations.remove(conversation);
1859 }
1860 }
1861 if (account.getXmppConnection() != null) {
1862 new Thread(() -> disconnect(account, true)).start();
1863 }
1864 final Runnable runnable = () -> {
1865 if (!databaseBackend.deleteAccount(account)) {
1866 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to delete account");
1867 }
1868 };
1869 mDatabaseWriterExecutor.execute(runnable);
1870 this.accounts.remove(account);
1871 this.mRosterSyncTaskManager.clear(account);
1872 updateAccountUi();
1873 getNotificationService().updateErrorNotification();
1874 syncEnabledAccountSetting();
1875 }
1876 }
1877
1878 public void setOnConversationListChangedListener(OnConversationUpdate listener) {
1879 synchronized (this) {
1880 this.mLastActivity = System.currentTimeMillis();
1881 if (checkListeners()) {
1882 switchToForeground();
1883 }
1884 this.mOnConversationUpdate = listener;
1885 this.mNotificationService.setIsInForeground(true);
1886 if (this.convChangedListenerCount < 2) {
1887 this.convChangedListenerCount++;
1888 }
1889 }
1890 }
1891
1892 public void removeOnConversationListChangedListener() {
1893 synchronized (this) {
1894 this.convChangedListenerCount--;
1895 if (this.convChangedListenerCount <= 0) {
1896 this.convChangedListenerCount = 0;
1897 this.mOnConversationUpdate = null;
1898 this.mNotificationService.setIsInForeground(false);
1899 if (checkListeners()) {
1900 switchToBackground();
1901 }
1902 }
1903 }
1904 }
1905
1906 public void setOnShowErrorToastListener(OnShowErrorToast onShowErrorToast) {
1907 synchronized (this) {
1908 if (checkListeners()) {
1909 switchToForeground();
1910 }
1911 this.mOnShowErrorToast = onShowErrorToast;
1912 if (this.showErrorToastListenerCount < 2) {
1913 this.showErrorToastListenerCount++;
1914 }
1915 }
1916 this.mOnShowErrorToast = onShowErrorToast;
1917 }
1918
1919 public void removeOnShowErrorToastListener() {
1920 synchronized (this) {
1921 this.showErrorToastListenerCount--;
1922 if (this.showErrorToastListenerCount <= 0) {
1923 this.showErrorToastListenerCount = 0;
1924 this.mOnShowErrorToast = null;
1925 if (checkListeners()) {
1926 switchToBackground();
1927 }
1928 }
1929 }
1930 }
1931
1932 public void setOnAccountListChangedListener(OnAccountUpdate listener) {
1933 synchronized (this) {
1934 if (checkListeners()) {
1935 switchToForeground();
1936 }
1937 this.mOnAccountUpdate = listener;
1938 if (this.accountChangedListenerCount < 2) {
1939 this.accountChangedListenerCount++;
1940 }
1941 }
1942 }
1943
1944 public void removeOnAccountListChangedListener() {
1945 synchronized (this) {
1946 this.accountChangedListenerCount--;
1947 if (this.accountChangedListenerCount <= 0) {
1948 this.mOnAccountUpdate = null;
1949 this.accountChangedListenerCount = 0;
1950 if (checkListeners()) {
1951 switchToBackground();
1952 }
1953 }
1954 }
1955 }
1956
1957 public void setOnCaptchaRequestedListener(OnCaptchaRequested listener) {
1958 synchronized (this) {
1959 if (checkListeners()) {
1960 switchToForeground();
1961 }
1962 this.mOnCaptchaRequested = listener;
1963 if (this.captchaRequestedListenerCount < 2) {
1964 this.captchaRequestedListenerCount++;
1965 }
1966 }
1967 }
1968
1969 public void removeOnCaptchaRequestedListener() {
1970 synchronized (this) {
1971 this.captchaRequestedListenerCount--;
1972 if (this.captchaRequestedListenerCount <= 0) {
1973 this.mOnCaptchaRequested = null;
1974 this.captchaRequestedListenerCount = 0;
1975 if (checkListeners()) {
1976 switchToBackground();
1977 }
1978 }
1979 }
1980 }
1981
1982 public void setOnRosterUpdateListener(final OnRosterUpdate listener) {
1983 synchronized (this) {
1984 if (checkListeners()) {
1985 switchToForeground();
1986 }
1987 this.mOnRosterUpdate = listener;
1988 if (this.rosterChangedListenerCount < 2) {
1989 this.rosterChangedListenerCount++;
1990 }
1991 }
1992 }
1993
1994 public void removeOnRosterUpdateListener() {
1995 synchronized (this) {
1996 this.rosterChangedListenerCount--;
1997 if (this.rosterChangedListenerCount <= 0) {
1998 this.rosterChangedListenerCount = 0;
1999 this.mOnRosterUpdate = null;
2000 if (checkListeners()) {
2001 switchToBackground();
2002 }
2003 }
2004 }
2005 }
2006
2007 public void setOnUpdateBlocklistListener(final OnUpdateBlocklist listener) {
2008 synchronized (this) {
2009 if (checkListeners()) {
2010 switchToForeground();
2011 }
2012 this.mOnUpdateBlocklist = listener;
2013 if (this.updateBlocklistListenerCount < 2) {
2014 this.updateBlocklistListenerCount++;
2015 }
2016 }
2017 }
2018
2019 public void removeOnUpdateBlocklistListener() {
2020 synchronized (this) {
2021 this.updateBlocklistListenerCount--;
2022 if (this.updateBlocklistListenerCount <= 0) {
2023 this.updateBlocklistListenerCount = 0;
2024 this.mOnUpdateBlocklist = null;
2025 if (checkListeners()) {
2026 switchToBackground();
2027 }
2028 }
2029 }
2030 }
2031
2032 public void setOnKeyStatusUpdatedListener(final OnKeyStatusUpdated listener) {
2033 synchronized (this) {
2034 if (checkListeners()) {
2035 switchToForeground();
2036 }
2037 this.mOnKeyStatusUpdated = listener;
2038 if (this.keyStatusUpdatedListenerCount < 2) {
2039 this.keyStatusUpdatedListenerCount++;
2040 }
2041 }
2042 }
2043
2044 public void removeOnNewKeysAvailableListener() {
2045 synchronized (this) {
2046 this.keyStatusUpdatedListenerCount--;
2047 if (this.keyStatusUpdatedListenerCount <= 0) {
2048 this.keyStatusUpdatedListenerCount = 0;
2049 this.mOnKeyStatusUpdated = null;
2050 if (checkListeners()) {
2051 switchToBackground();
2052 }
2053 }
2054 }
2055 }
2056
2057 public void setOnMucRosterUpdateListener(OnMucRosterUpdate listener) {
2058 synchronized (this) {
2059 if (checkListeners()) {
2060 switchToForeground();
2061 }
2062 this.mOnMucRosterUpdate = listener;
2063 if (this.mucRosterChangedListenerCount < 2) {
2064 this.mucRosterChangedListenerCount++;
2065 }
2066 }
2067 }
2068
2069 public void removeOnMucRosterUpdateListener() {
2070 synchronized (this) {
2071 this.mucRosterChangedListenerCount--;
2072 if (this.mucRosterChangedListenerCount <= 0) {
2073 this.mucRosterChangedListenerCount = 0;
2074 this.mOnMucRosterUpdate = null;
2075 if (checkListeners()) {
2076 switchToBackground();
2077 }
2078 }
2079 }
2080 }
2081
2082 public boolean checkListeners() {
2083 return (this.mOnAccountUpdate == null
2084 && this.mOnConversationUpdate == null
2085 && this.mOnRosterUpdate == null
2086 && this.mOnCaptchaRequested == null
2087 && this.mOnUpdateBlocklist == null
2088 && this.mOnShowErrorToast == null
2089 && this.mOnKeyStatusUpdated == null);
2090 }
2091
2092 private void switchToForeground() {
2093 final boolean broadcastLastActivity = broadcastLastActivity();
2094 for (Conversation conversation : getConversations()) {
2095 if (conversation.getMode() == Conversation.MODE_MULTI) {
2096 conversation.getMucOptions().resetChatState();
2097 } else {
2098 conversation.setIncomingChatState(Config.DEFAULT_CHATSTATE);
2099 }
2100 }
2101 for (Account account : getAccounts()) {
2102 if (account.getStatus() == Account.State.ONLINE) {
2103 account.deactivateGracePeriod();
2104 final XmppConnection connection = account.getXmppConnection();
2105 if (connection != null) {
2106 if (connection.getFeatures().csi()) {
2107 connection.sendActive();
2108 }
2109 if (broadcastLastActivity) {
2110 sendPresence(account, false); //send new presence but don't include idle because we are not
2111 }
2112 }
2113 }
2114 }
2115 Log.d(Config.LOGTAG, "app switched into foreground");
2116 }
2117
2118 private void switchToBackground() {
2119 final boolean broadcastLastActivity = broadcastLastActivity();
2120 for (Account account : getAccounts()) {
2121 if (account.getStatus() == Account.State.ONLINE) {
2122 XmppConnection connection = account.getXmppConnection();
2123 if (connection != null) {
2124 if (broadcastLastActivity) {
2125 sendPresence(account, true);
2126 }
2127 if (connection.getFeatures().csi()) {
2128 connection.sendInactive();
2129 }
2130 }
2131 }
2132 }
2133 this.mNotificationService.setIsInForeground(false);
2134 Log.d(Config.LOGTAG, "app switched into background");
2135 }
2136
2137 private void connectMultiModeConversations(Account account) {
2138 List<Conversation> conversations = getConversations();
2139 for (Conversation conversation : conversations) {
2140 if (conversation.getMode() == Conversation.MODE_MULTI && conversation.getAccount() == account) {
2141 joinMuc(conversation);
2142 }
2143 }
2144 }
2145
2146 public void joinMuc(Conversation conversation) {
2147 joinMuc(conversation, null, false);
2148 }
2149
2150 public void joinMuc(Conversation conversation, boolean followedInvite) {
2151 joinMuc(conversation, null, followedInvite);
2152 }
2153
2154 private void joinMuc(Conversation conversation, final OnConferenceJoined onConferenceJoined) {
2155 joinMuc(conversation, onConferenceJoined, false);
2156 }
2157
2158 private void joinMuc(Conversation conversation, final OnConferenceJoined onConferenceJoined, final boolean followedInvite) {
2159 Account account = conversation.getAccount();
2160 account.pendingConferenceJoins.remove(conversation);
2161 account.pendingConferenceLeaves.remove(conversation);
2162 if (account.getStatus() == Account.State.ONLINE) {
2163 sendPresencePacket(account, mPresenceGenerator.leave(conversation.getMucOptions()));
2164 conversation.resetMucOptions();
2165 if (onConferenceJoined != null) {
2166 conversation.getMucOptions().flagNoAutoPushConfiguration();
2167 }
2168 conversation.setHasMessagesLeftOnServer(false);
2169 fetchConferenceConfiguration(conversation, new OnConferenceConfigurationFetched() {
2170
2171 private void join(Conversation conversation) {
2172 Account account = conversation.getAccount();
2173 final MucOptions mucOptions = conversation.getMucOptions();
2174 final Jid joinJid = mucOptions.getSelf().getFullJid();
2175 Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": joining conversation " + joinJid.toString());
2176 PresencePacket packet = mPresenceGenerator.selfPresence(account, Presence.Status.ONLINE, mucOptions.nonanonymous() || onConferenceJoined != null);
2177 packet.setTo(joinJid);
2178 Element x = packet.addChild("x", "http://jabber.org/protocol/muc");
2179 if (conversation.getMucOptions().getPassword() != null) {
2180 x.addChild("password").setContent(mucOptions.getPassword());
2181 }
2182
2183 if (mucOptions.mamSupport()) {
2184 // Use MAM instead of the limited muc history to get history
2185 x.addChild("history").setAttribute("maxchars", "0");
2186 } else {
2187 // Fallback to muc history
2188 x.addChild("history").setAttribute("since", PresenceGenerator.getTimestamp(conversation.getLastMessageTransmitted().getTimestamp()));
2189 }
2190 sendPresencePacket(account, packet);
2191 if (onConferenceJoined != null) {
2192 onConferenceJoined.onConferenceJoined(conversation);
2193 }
2194 if (!joinJid.equals(conversation.getJid())) {
2195 conversation.setContactJid(joinJid);
2196 databaseBackend.updateConversation(conversation);
2197 }
2198
2199 if (mucOptions.mamSupport()) {
2200 getMessageArchiveService().catchupMUC(conversation);
2201 }
2202 if (mucOptions.isPrivateAndNonAnonymous()) {
2203 fetchConferenceMembers(conversation);
2204 if (followedInvite && conversation.getBookmark() == null) {
2205 saveConversationAsBookmark(conversation, null);
2206 }
2207 }
2208 sendUnsentMessages(conversation);
2209 }
2210
2211 @Override
2212 public void onConferenceConfigurationFetched(Conversation conversation) {
2213 join(conversation);
2214 }
2215
2216 @Override
2217 public void onFetchFailed(final Conversation conversation, Element error) {
2218 if (error != null && "remote-server-not-found".equals(error.getName())) {
2219 conversation.getMucOptions().setError(MucOptions.Error.SERVER_NOT_FOUND);
2220 updateConversationUi();
2221 } else {
2222 join(conversation);
2223 fetchConferenceConfiguration(conversation);
2224 }
2225 }
2226 });
2227 updateConversationUi();
2228 } else {
2229 account.pendingConferenceJoins.add(conversation);
2230 conversation.resetMucOptions();
2231 conversation.setHasMessagesLeftOnServer(false);
2232 updateConversationUi();
2233 }
2234 }
2235
2236 private void fetchConferenceMembers(final Conversation conversation) {
2237 final Account account = conversation.getAccount();
2238 final AxolotlService axolotlService = account.getAxolotlService();
2239 final String[] affiliations = {"member", "admin", "owner"};
2240 OnIqPacketReceived callback = new OnIqPacketReceived() {
2241
2242 private int i = 0;
2243 private boolean success = true;
2244
2245 @Override
2246 public void onIqPacketReceived(Account account, IqPacket packet) {
2247
2248 Element query = packet.query("http://jabber.org/protocol/muc#admin");
2249 if (packet.getType() == IqPacket.TYPE.RESULT && query != null) {
2250 for (Element child : query.getChildren()) {
2251 if ("item".equals(child.getName())) {
2252 MucOptions.User user = AbstractParser.parseItem(conversation, child);
2253 if (!user.realJidMatchesAccount()) {
2254 boolean isNew = conversation.getMucOptions().updateUser(user);
2255 Contact contact = user.getContact();
2256 if (isNew
2257 && user.getRealJid() != null
2258 && (contact == null || !contact.mutualPresenceSubscription())
2259 && axolotlService.hasEmptyDeviceList(user.getRealJid())) {
2260 axolotlService.fetchDeviceIds(user.getRealJid());
2261 }
2262 }
2263 }
2264 }
2265 } else {
2266 success = false;
2267 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not request affiliation " + affiliations[i] + " in " + conversation.getJid().asBareJid());
2268 }
2269 ++i;
2270 if (i >= affiliations.length) {
2271 List<Jid> members = conversation.getMucOptions().getMembers();
2272 if (success) {
2273 List<Jid> cryptoTargets = conversation.getAcceptedCryptoTargets();
2274 boolean changed = false;
2275 for (ListIterator<Jid> iterator = cryptoTargets.listIterator(); iterator.hasNext(); ) {
2276 Jid jid = iterator.next();
2277 if (!members.contains(jid)) {
2278 iterator.remove();
2279 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": removed " + jid + " from crypto targets of " + conversation.getName());
2280 changed = true;
2281 }
2282 }
2283 if (changed) {
2284 conversation.setAcceptedCryptoTargets(cryptoTargets);
2285 updateConversation(conversation);
2286 }
2287 }
2288 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": retrieved members for " + conversation.getJid().asBareJid() + ": " + conversation.getMucOptions().getMembers());
2289 getAvatarService().clear(conversation);
2290 updateMucRosterUi();
2291 updateConversationUi();
2292 }
2293 }
2294 };
2295 for (String affiliation : affiliations) {
2296 sendIqPacket(account, mIqGenerator.queryAffiliation(conversation, affiliation), callback);
2297 }
2298 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": fetching members for " + conversation.getName());
2299 }
2300
2301 public void providePasswordForMuc(Conversation conversation, String password) {
2302 if (conversation.getMode() == Conversation.MODE_MULTI) {
2303 conversation.getMucOptions().setPassword(password);
2304 if (conversation.getBookmark() != null) {
2305 if (respectAutojoin()) {
2306 conversation.getBookmark().setAutojoin(true);
2307 }
2308 pushBookmarks(conversation.getAccount());
2309 }
2310 updateConversation(conversation);
2311 joinMuc(conversation);
2312 }
2313 }
2314
2315 private boolean hasEnabledAccounts() {
2316 for (Account account : this.accounts) {
2317 if (account.isEnabled()) {
2318 return true;
2319 }
2320 }
2321 return false;
2322 }
2323
2324 public void persistSelfNick(MucOptions.User self) {
2325 final Conversation conversation = self.getConversation();
2326 Jid full = self.getFullJid();
2327 if (!full.equals(conversation.getJid())) {
2328 Log.d(Config.LOGTAG, "nick changed. updating");
2329 conversation.setContactJid(full);
2330 databaseBackend.updateConversation(conversation);
2331 }
2332
2333 Bookmark bookmark = conversation.getBookmark();
2334 if (bookmark != null && !full.getResource().equals(bookmark.getNick())) {
2335 bookmark.setNick(full.getResource());
2336 pushBookmarks(bookmark.getAccount());
2337 }
2338 }
2339
2340 public boolean renameInMuc(final Conversation conversation, final String nick, final UiCallback<Conversation> callback) {
2341 final MucOptions options = conversation.getMucOptions();
2342 final Jid joinJid = options.createJoinJid(nick);
2343 if (joinJid == null) {
2344 return false;
2345 }
2346 if (options.online()) {
2347 Account account = conversation.getAccount();
2348 options.setOnRenameListener(new OnRenameListener() {
2349
2350 @Override
2351 public void onSuccess() {
2352 callback.success(conversation);
2353 }
2354
2355 @Override
2356 public void onFailure() {
2357 callback.error(R.string.nick_in_use, conversation);
2358 }
2359 });
2360
2361 PresencePacket packet = new PresencePacket();
2362 packet.setTo(joinJid);
2363 packet.setFrom(conversation.getAccount().getJid());
2364
2365 String sig = account.getPgpSignature();
2366 if (sig != null) {
2367 packet.addChild("status").setContent("online");
2368 packet.addChild("x", "jabber:x:signed").setContent(sig);
2369 }
2370 sendPresencePacket(account, packet);
2371 } else {
2372 conversation.setContactJid(joinJid);
2373 databaseBackend.updateConversation(conversation);
2374 if (conversation.getAccount().getStatus() == Account.State.ONLINE) {
2375 Bookmark bookmark = conversation.getBookmark();
2376 if (bookmark != null) {
2377 bookmark.setNick(nick);
2378 pushBookmarks(bookmark.getAccount());
2379 }
2380 joinMuc(conversation);
2381 }
2382 }
2383 return true;
2384 }
2385
2386 public void leaveMuc(Conversation conversation) {
2387 leaveMuc(conversation, false);
2388 }
2389
2390 private void leaveMuc(Conversation conversation, boolean now) {
2391 Account account = conversation.getAccount();
2392 account.pendingConferenceJoins.remove(conversation);
2393 account.pendingConferenceLeaves.remove(conversation);
2394 if (account.getStatus() == Account.State.ONLINE || now) {
2395 sendPresencePacket(conversation.getAccount(), mPresenceGenerator.leave(conversation.getMucOptions()));
2396 conversation.getMucOptions().setOffline();
2397 Bookmark bookmark = conversation.getBookmark();
2398 if (bookmark != null) {
2399 bookmark.setConversation(null);
2400 }
2401 Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": leaving muc " + conversation.getJid());
2402 } else {
2403 account.pendingConferenceLeaves.add(conversation);
2404 }
2405 }
2406
2407 public String findConferenceServer(final Account account) {
2408 String server;
2409 if (account.getXmppConnection() != null) {
2410 server = account.getXmppConnection().getMucServer();
2411 if (server != null) {
2412 return server;
2413 }
2414 }
2415 for (Account other : getAccounts()) {
2416 if (other != account && other.getXmppConnection() != null) {
2417 server = other.getXmppConnection().getMucServer();
2418 if (server != null) {
2419 return server;
2420 }
2421 }
2422 }
2423 return null;
2424 }
2425
2426 public boolean createAdhocConference(final Account account,
2427 final String subject,
2428 final Iterable<Jid> jids,
2429 final UiCallback<Conversation> callback) {
2430 Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": creating adhoc conference with " + jids.toString());
2431 if (account.getStatus() == Account.State.ONLINE) {
2432 try {
2433 String server = findConferenceServer(account);
2434 if (server == null) {
2435 if (callback != null) {
2436 callback.error(R.string.no_conference_server_found, null);
2437 }
2438 return false;
2439 }
2440 final Jid jid = Jid.of(new BigInteger(64, getRNG()).toString(Character.MAX_RADIX), server, null);
2441 final Conversation conversation = findOrCreateConversation(account, jid, true, false, true);
2442 joinMuc(conversation, new OnConferenceJoined() {
2443 @Override
2444 public void onConferenceJoined(final Conversation conversation) {
2445 pushConferenceConfiguration(conversation, IqGenerator.defaultRoomConfiguration(), new OnConfigurationPushed() {
2446 @Override
2447 public void onPushSucceeded() {
2448 if (subject != null && !subject.trim().isEmpty()) {
2449 pushSubjectToConference(conversation, subject.trim());
2450 }
2451 for (Jid invite : jids) {
2452 invite(conversation, invite);
2453 }
2454 if (account.countPresences() > 1) {
2455 directInvite(conversation, account.getJid().asBareJid());
2456 }
2457 saveConversationAsBookmark(conversation, subject);
2458 if (callback != null) {
2459 callback.success(conversation);
2460 }
2461 }
2462
2463 @Override
2464 public void onPushFailed() {
2465 archiveConversation(conversation);
2466 if (callback != null) {
2467 callback.error(R.string.conference_creation_failed, conversation);
2468 }
2469 }
2470 });
2471 }
2472 });
2473 return true;
2474 } catch (IllegalArgumentException e) {
2475 if (callback != null) {
2476 callback.error(R.string.conference_creation_failed, null);
2477 }
2478 return false;
2479 }
2480 } else {
2481 if (callback != null) {
2482 callback.error(R.string.not_connected_try_again, null);
2483 }
2484 return false;
2485 }
2486 }
2487
2488 public void fetchConferenceConfiguration(final Conversation conversation) {
2489 fetchConferenceConfiguration(conversation, null);
2490 }
2491
2492 public void fetchConferenceConfiguration(final Conversation conversation, final OnConferenceConfigurationFetched callback) {
2493 IqPacket request = new IqPacket(IqPacket.TYPE.GET);
2494 request.setTo(conversation.getJid().asBareJid());
2495 request.query("http://jabber.org/protocol/disco#info");
2496 sendIqPacket(conversation.getAccount(), request, new OnIqPacketReceived() {
2497 @Override
2498 public void onIqPacketReceived(Account account, IqPacket packet) {
2499 Element query = packet.findChild("query", "http://jabber.org/protocol/disco#info");
2500 if (packet.getType() == IqPacket.TYPE.RESULT && query != null) {
2501 ArrayList<String> features = new ArrayList<>();
2502 for (Element child : query.getChildren()) {
2503 if (child != null && child.getName().equals("feature")) {
2504 String var = child.getAttribute("var");
2505 if (var != null) {
2506 features.add(var);
2507 }
2508 }
2509 }
2510 Element form = query.findChild("x", Namespace.DATA);
2511 Data data = form == null ? null : Data.parse(form);
2512 if (conversation.getMucOptions().updateConfiguration(features, data)) {
2513 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": muc configuration changed for " + conversation.getJid().asBareJid());
2514 updateConversation(conversation);
2515 }
2516 if (callback != null) {
2517 callback.onConferenceConfigurationFetched(conversation);
2518 }
2519 updateConversationUi();
2520 } else if (packet.getType() == IqPacket.TYPE.ERROR) {
2521 if (callback != null) {
2522 callback.onFetchFailed(conversation, packet.getError());
2523 }
2524 }
2525 }
2526 });
2527 }
2528
2529 public void pushNodeConfiguration(Account account, final String node, final Bundle options, final OnConfigurationPushed callback) {
2530 pushNodeConfiguration(account, account.getJid().asBareJid(), node, options, callback);
2531 }
2532
2533 public void pushNodeConfiguration(Account account, final Jid jid, final String node, final Bundle options, final OnConfigurationPushed callback) {
2534 sendIqPacket(account, mIqGenerator.requestPubsubConfiguration(jid, node), new OnIqPacketReceived() {
2535 @Override
2536 public void onIqPacketReceived(Account account, IqPacket packet) {
2537 if (packet.getType() == IqPacket.TYPE.RESULT) {
2538 Element pubsub = packet.findChild("pubsub", "http://jabber.org/protocol/pubsub#owner");
2539 Element configuration = pubsub == null ? null : pubsub.findChild("configure");
2540 Element x = configuration == null ? null : configuration.findChild("x", Namespace.DATA);
2541 if (x != null) {
2542 Data data = Data.parse(x);
2543 data.submit(options);
2544 sendIqPacket(account, mIqGenerator.publishPubsubConfiguration(jid, node, data), new OnIqPacketReceived() {
2545 @Override
2546 public void onIqPacketReceived(Account account, IqPacket packet) {
2547 if (packet.getType() == IqPacket.TYPE.RESULT && callback != null) {
2548 callback.onPushSucceeded();
2549 } else if (packet.getType() == IqPacket.TYPE.ERROR && callback != null) {
2550 callback.onPushFailed();
2551 }
2552 }
2553 });
2554 } else if (callback != null) {
2555 callback.onPushFailed();
2556 }
2557 } else if (packet.getType() == IqPacket.TYPE.ERROR && callback != null) {
2558 callback.onPushFailed();
2559 }
2560 }
2561 });
2562 }
2563
2564 public void pushConferenceConfiguration(final Conversation conversation, final Bundle options, final OnConfigurationPushed callback) {
2565 IqPacket request = new IqPacket(IqPacket.TYPE.GET);
2566 request.setTo(conversation.getJid().asBareJid());
2567 request.query("http://jabber.org/protocol/muc#owner");
2568 sendIqPacket(conversation.getAccount(), request, new OnIqPacketReceived() {
2569 @Override
2570 public void onIqPacketReceived(Account account, IqPacket packet) {
2571 if (packet.getType() == IqPacket.TYPE.RESULT) {
2572 Data data = Data.parse(packet.query().findChild("x", Namespace.DATA));
2573 data.submit(options);
2574 IqPacket set = new IqPacket(IqPacket.TYPE.SET);
2575 set.setTo(conversation.getJid().asBareJid());
2576 set.query("http://jabber.org/protocol/muc#owner").addChild(data);
2577 sendIqPacket(account, set, new OnIqPacketReceived() {
2578 @Override
2579 public void onIqPacketReceived(Account account, IqPacket packet) {
2580 if (callback != null) {
2581 if (packet.getType() == IqPacket.TYPE.RESULT) {
2582 callback.onPushSucceeded();
2583 } else {
2584 callback.onPushFailed();
2585 }
2586 }
2587 }
2588 });
2589 } else {
2590 if (callback != null) {
2591 callback.onPushFailed();
2592 }
2593 }
2594 }
2595 });
2596 }
2597
2598 public void pushSubjectToConference(final Conversation conference, final String subject) {
2599 MessagePacket packet = this.getMessageGenerator().conferenceSubject(conference, subject);
2600 this.sendMessagePacket(conference.getAccount(), packet);
2601 final MucOptions mucOptions = conference.getMucOptions();
2602 final MucOptions.User self = mucOptions.getSelf();
2603 if (self.getAffiliation().ranks(MucOptions.Affiliation.OWNER)) {
2604 Bundle options = new Bundle();
2605 options.putString("muc#roomconfig_persistentroom", "1");
2606 options.putString("muc#roomconfig_roomname", subject);
2607 this.pushConferenceConfiguration(conference, options, null);
2608 }
2609 }
2610
2611 public void changeAffiliationInConference(final Conversation conference, Jid user, final MucOptions.Affiliation affiliation, final OnAffiliationChanged callback) {
2612 final Jid jid = user.asBareJid();
2613 IqPacket request = this.mIqGenerator.changeAffiliation(conference, jid, affiliation.toString());
2614 sendIqPacket(conference.getAccount(), request, new OnIqPacketReceived() {
2615 @Override
2616 public void onIqPacketReceived(Account account, IqPacket packet) {
2617 if (packet.getType() == IqPacket.TYPE.RESULT) {
2618 conference.getMucOptions().changeAffiliation(jid, affiliation);
2619 getAvatarService().clear(conference);
2620 callback.onAffiliationChangedSuccessful(jid);
2621 } else {
2622 callback.onAffiliationChangeFailed(jid, R.string.could_not_change_affiliation);
2623 }
2624 }
2625 });
2626 }
2627
2628 public void changeAffiliationsInConference(final Conversation conference, MucOptions.Affiliation before, MucOptions.Affiliation after) {
2629 List<Jid> jids = new ArrayList<>();
2630 for (MucOptions.User user : conference.getMucOptions().getUsers()) {
2631 if (user.getAffiliation() == before && user.getRealJid() != null) {
2632 jids.add(user.getRealJid());
2633 }
2634 }
2635 IqPacket request = this.mIqGenerator.changeAffiliation(conference, jids, after.toString());
2636 sendIqPacket(conference.getAccount(), request, mDefaultIqHandler);
2637 }
2638
2639 public void changeRoleInConference(final Conversation conference, final String nick, MucOptions.Role role, final OnRoleChanged callback) {
2640 IqPacket request = this.mIqGenerator.changeRole(conference, nick, role.toString());
2641 Log.d(Config.LOGTAG, request.toString());
2642 sendIqPacket(conference.getAccount(), request, new OnIqPacketReceived() {
2643 @Override
2644 public void onIqPacketReceived(Account account, IqPacket packet) {
2645 Log.d(Config.LOGTAG, packet.toString());
2646 if (packet.getType() == IqPacket.TYPE.RESULT) {
2647 callback.onRoleChangedSuccessful(nick);
2648 } else {
2649 callback.onRoleChangeFailed(nick, R.string.could_not_change_role);
2650 }
2651 }
2652 });
2653 }
2654
2655 private void disconnect(Account account, boolean force) {
2656 if ((account.getStatus() == Account.State.ONLINE)
2657 || (account.getStatus() == Account.State.DISABLED)) {
2658 final XmppConnection connection = account.getXmppConnection();
2659 if (!force) {
2660 List<Conversation> conversations = getConversations();
2661 for (Conversation conversation : conversations) {
2662 if (conversation.getAccount() == account) {
2663 if (conversation.getMode() == Conversation.MODE_MULTI) {
2664 leaveMuc(conversation, true);
2665 }
2666 }
2667 }
2668 sendOfflinePresence(account);
2669 }
2670 connection.disconnect(force);
2671 }
2672 }
2673
2674 @Override
2675 public IBinder onBind(Intent intent) {
2676 return mBinder;
2677 }
2678
2679 public void updateMessage(Message message) {
2680 databaseBackend.updateMessage(message);
2681 updateConversationUi();
2682 }
2683
2684 public void updateMessage(Message message, String uuid) {
2685 databaseBackend.updateMessage(message, uuid);
2686 updateConversationUi();
2687 }
2688
2689 protected void syncDirtyContacts(Account account) {
2690 for (Contact contact : account.getRoster().getContacts()) {
2691 if (contact.getOption(Contact.Options.DIRTY_PUSH)) {
2692 pushContactToServer(contact);
2693 }
2694 if (contact.getOption(Contact.Options.DIRTY_DELETE)) {
2695 deleteContactOnServer(contact);
2696 }
2697 }
2698 }
2699
2700 public void createContact(Contact contact, boolean autoGrant) {
2701 if (autoGrant) {
2702 contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
2703 contact.setOption(Contact.Options.ASKING);
2704 }
2705 pushContactToServer(contact);
2706 }
2707
2708 public void pushContactToServer(final Contact contact) {
2709 contact.resetOption(Contact.Options.DIRTY_DELETE);
2710 contact.setOption(Contact.Options.DIRTY_PUSH);
2711 final Account account = contact.getAccount();
2712 if (account.getStatus() == Account.State.ONLINE) {
2713 final boolean ask = contact.getOption(Contact.Options.ASKING);
2714 final boolean sendUpdates = contact
2715 .getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)
2716 && contact.getOption(Contact.Options.PREEMPTIVE_GRANT);
2717 final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
2718 iq.query(Namespace.ROSTER).addChild(contact.asElement());
2719 account.getXmppConnection().sendIqPacket(iq, mDefaultIqHandler);
2720 if (sendUpdates) {
2721 sendPresencePacket(account, mPresenceGenerator.sendPresenceUpdatesTo(contact));
2722 }
2723 if (ask) {
2724 sendPresencePacket(account, mPresenceGenerator.requestPresenceUpdatesFrom(contact));
2725 }
2726 } else {
2727 syncRoster(contact.getAccount());
2728 }
2729 }
2730
2731 public void publishAvatar(final Account account, final Uri image, final UiCallback<Avatar> callback) {
2732 new Thread(() -> {
2733 final Bitmap.CompressFormat format = Config.AVATAR_FORMAT;
2734 final int size = Config.AVATAR_SIZE;
2735 final Avatar avatar = getFileBackend().getPepAvatar(image, size, format);
2736 if (avatar != null) {
2737 if (!getFileBackend().save(avatar)) {
2738 callback.error(R.string.error_saving_avatar, avatar);
2739 return;
2740 }
2741 publishAvatar(account, avatar, callback);
2742 } else {
2743 callback.error(R.string.error_publish_avatar_converting, null);
2744 }
2745 }).start();
2746
2747 }
2748
2749 public void publishAvatar(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
2750 IqPacket packet = this.mIqGenerator.publishAvatar(avatar);
2751 this.sendIqPacket(account, packet, new OnIqPacketReceived() {
2752
2753 @Override
2754 public void onIqPacketReceived(Account account, IqPacket result) {
2755 if (result.getType() == IqPacket.TYPE.RESULT) {
2756 final IqPacket packet = XmppConnectionService.this.mIqGenerator.publishAvatarMetadata(avatar);
2757 sendIqPacket(account, packet, new OnIqPacketReceived() {
2758 @Override
2759 public void onIqPacketReceived(Account account, IqPacket result) {
2760 if (result.getType() == IqPacket.TYPE.RESULT) {
2761 if (account.setAvatar(avatar.getFilename())) {
2762 getAvatarService().clear(account);
2763 databaseBackend.updateAccount(account);
2764 }
2765 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": published avatar " + (avatar.size / 1024) + "KiB");
2766 if (callback != null) {
2767 callback.success(avatar);
2768 }
2769 } else {
2770 if (callback != null) {
2771 callback.error(R.string.error_publish_avatar_server_reject, avatar);
2772 }
2773 }
2774 }
2775 });
2776 } else {
2777 Element error = result.findChild("error");
2778 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server rejected avatar " + (avatar.size / 1024) + "KiB " + (error != null ? error.toString() : ""));
2779 if (callback != null) {
2780 callback.error(R.string.error_publish_avatar_server_reject, avatar);
2781 }
2782 }
2783 }
2784 });
2785 }
2786
2787 public void republishAvatarIfNeeded(Account account) {
2788 if (account.getAxolotlService().isPepBroken()) {
2789 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": skipping republication of avatar because pep is broken");
2790 return;
2791 }
2792 IqPacket packet = this.mIqGenerator.retrieveAvatarMetaData(null);
2793 this.sendIqPacket(account, packet, new OnIqPacketReceived() {
2794
2795 private Avatar parseAvatar(IqPacket packet) {
2796 Element pubsub = packet.findChild("pubsub", "http://jabber.org/protocol/pubsub");
2797 if (pubsub != null) {
2798 Element items = pubsub.findChild("items");
2799 if (items != null) {
2800 return Avatar.parseMetadata(items);
2801 }
2802 }
2803 return null;
2804 }
2805
2806 private boolean errorIsItemNotFound(IqPacket packet) {
2807 Element error = packet.findChild("error");
2808 return packet.getType() == IqPacket.TYPE.ERROR
2809 && error != null
2810 && error.hasChild("item-not-found");
2811 }
2812
2813 @Override
2814 public void onIqPacketReceived(Account account, IqPacket packet) {
2815 if (packet.getType() == IqPacket.TYPE.RESULT || errorIsItemNotFound(packet)) {
2816 Avatar serverAvatar = parseAvatar(packet);
2817 if (serverAvatar == null && account.getAvatar() != null) {
2818 Avatar avatar = fileBackend.getStoredPepAvatar(account.getAvatar());
2819 if (avatar != null) {
2820 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": avatar on server was null. republishing");
2821 publishAvatar(account, fileBackend.getStoredPepAvatar(account.getAvatar()), null);
2822 } else {
2823 Log.e(Config.LOGTAG, account.getJid().asBareJid() + ": error rereading avatar");
2824 }
2825 }
2826 }
2827 }
2828 });
2829 }
2830
2831 public void fetchAvatar(Account account, Avatar avatar) {
2832 fetchAvatar(account, avatar, null);
2833 }
2834
2835 public void fetchAvatar(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
2836 final String KEY = generateFetchKey(account, avatar);
2837 synchronized (this.mInProgressAvatarFetches) {
2838 if (!this.mInProgressAvatarFetches.contains(KEY)) {
2839 switch (avatar.origin) {
2840 case PEP:
2841 this.mInProgressAvatarFetches.add(KEY);
2842 fetchAvatarPep(account, avatar, callback);
2843 break;
2844 case VCARD:
2845 this.mInProgressAvatarFetches.add(KEY);
2846 fetchAvatarVcard(account, avatar, callback);
2847 break;
2848 }
2849 }
2850 }
2851 }
2852
2853 private void fetchAvatarPep(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
2854 IqPacket packet = this.mIqGenerator.retrievePepAvatar(avatar);
2855 sendIqPacket(account, packet, (a, result) -> {
2856 synchronized (mInProgressAvatarFetches) {
2857 mInProgressAvatarFetches.remove(generateFetchKey(a, avatar));
2858 }
2859 final String ERROR = a.getJid().asBareJid() + ": fetching avatar for " + avatar.owner + " failed ";
2860 if (result.getType() == IqPacket.TYPE.RESULT) {
2861 avatar.image = mIqParser.avatarData(result);
2862 if (avatar.image != null) {
2863 if (getFileBackend().save(avatar)) {
2864 if (a.getJid().asBareJid().equals(avatar.owner)) {
2865 if (a.setAvatar(avatar.getFilename())) {
2866 databaseBackend.updateAccount(a);
2867 }
2868 getAvatarService().clear(a);
2869 updateConversationUi();
2870 updateAccountUi();
2871 } else {
2872 Contact contact = a.getRoster().getContact(avatar.owner);
2873 contact.setAvatar(avatar);
2874 getAvatarService().clear(contact);
2875 updateConversationUi();
2876 updateRosterUi();
2877 }
2878 if (callback != null) {
2879 callback.success(avatar);
2880 }
2881 Log.d(Config.LOGTAG, a.getJid().asBareJid()
2882 + ": successfully fetched pep avatar for " + avatar.owner);
2883 return;
2884 }
2885 } else {
2886
2887 Log.d(Config.LOGTAG, ERROR + "(parsing error)");
2888 }
2889 } else {
2890 Element error = result.findChild("error");
2891 if (error == null) {
2892 Log.d(Config.LOGTAG, ERROR + "(server error)");
2893 } else {
2894 Log.d(Config.LOGTAG, ERROR + error.toString());
2895 }
2896 }
2897 if (callback != null) {
2898 callback.error(0, null);
2899 }
2900
2901 });
2902 }
2903
2904 private void fetchAvatarVcard(final Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
2905 IqPacket packet = this.mIqGenerator.retrieveVcardAvatar(avatar);
2906 this.sendIqPacket(account, packet, new OnIqPacketReceived() {
2907 @Override
2908 public void onIqPacketReceived(Account account, IqPacket packet) {
2909 synchronized (mInProgressAvatarFetches) {
2910 mInProgressAvatarFetches.remove(generateFetchKey(account, avatar));
2911 }
2912 if (packet.getType() == IqPacket.TYPE.RESULT) {
2913 Element vCard = packet.findChild("vCard", "vcard-temp");
2914 Element photo = vCard != null ? vCard.findChild("PHOTO") : null;
2915 String image = photo != null ? photo.findChildContent("BINVAL") : null;
2916 if (image != null) {
2917 avatar.image = image;
2918 if (getFileBackend().save(avatar)) {
2919 Log.d(Config.LOGTAG, account.getJid().asBareJid()
2920 + ": successfully fetched vCard avatar for " + avatar.owner);
2921 if (avatar.owner.isBareJid()) {
2922 if (account.getJid().asBareJid().equals(avatar.owner) && account.getAvatar() == null) {
2923 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": had no avatar. replacing with vcard");
2924 account.setAvatar(avatar.getFilename());
2925 databaseBackend.updateAccount(account);
2926 getAvatarService().clear(account);
2927 updateAccountUi();
2928 } else {
2929 Contact contact = account.getRoster().getContact(avatar.owner);
2930 contact.setAvatar(avatar);
2931 getAvatarService().clear(contact);
2932 updateRosterUi();
2933 }
2934 updateConversationUi();
2935 } else {
2936 Conversation conversation = find(account, avatar.owner.asBareJid());
2937 if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
2938 MucOptions.User user = conversation.getMucOptions().findUserByFullJid(avatar.owner);
2939 if (user != null) {
2940 if (user.setAvatar(avatar)) {
2941 getAvatarService().clear(user);
2942 updateConversationUi();
2943 updateMucRosterUi();
2944 }
2945 }
2946 }
2947 }
2948 }
2949 }
2950 }
2951 }
2952 });
2953 }
2954
2955 public void checkForAvatar(Account account, final UiCallback<Avatar> callback) {
2956 IqPacket packet = this.mIqGenerator.retrieveAvatarMetaData(null);
2957 this.sendIqPacket(account, packet, new OnIqPacketReceived() {
2958
2959 @Override
2960 public void onIqPacketReceived(Account account, IqPacket packet) {
2961 if (packet.getType() == IqPacket.TYPE.RESULT) {
2962 Element pubsub = packet.findChild("pubsub", "http://jabber.org/protocol/pubsub");
2963 if (pubsub != null) {
2964 Element items = pubsub.findChild("items");
2965 if (items != null) {
2966 Avatar avatar = Avatar.parseMetadata(items);
2967 if (avatar != null) {
2968 avatar.owner = account.getJid().asBareJid();
2969 if (fileBackend.isAvatarCached(avatar)) {
2970 if (account.setAvatar(avatar.getFilename())) {
2971 databaseBackend.updateAccount(account);
2972 }
2973 getAvatarService().clear(account);
2974 callback.success(avatar);
2975 } else {
2976 fetchAvatarPep(account, avatar, callback);
2977 }
2978 return;
2979 }
2980 }
2981 }
2982 }
2983 callback.error(0, null);
2984 }
2985 });
2986 }
2987
2988 public void deleteContactOnServer(Contact contact) {
2989 contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
2990 contact.resetOption(Contact.Options.DIRTY_PUSH);
2991 contact.setOption(Contact.Options.DIRTY_DELETE);
2992 Account account = contact.getAccount();
2993 if (account.getStatus() == Account.State.ONLINE) {
2994 IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
2995 Element item = iq.query(Namespace.ROSTER).addChild("item");
2996 item.setAttribute("jid", contact.getJid().toString());
2997 item.setAttribute("subscription", "remove");
2998 account.getXmppConnection().sendIqPacket(iq, mDefaultIqHandler);
2999 }
3000 }
3001
3002 public void updateConversation(final Conversation conversation) {
3003 mDatabaseWriterExecutor.execute(() -> databaseBackend.updateConversation(conversation));
3004 }
3005
3006 private void reconnectAccount(final Account account, final boolean force, final boolean interactive) {
3007 synchronized (account) {
3008 XmppConnection connection = account.getXmppConnection();
3009 if (connection == null) {
3010 connection = createConnection(account);
3011 account.setXmppConnection(connection);
3012 }
3013 boolean hasInternet = hasInternetConnection();
3014 if (account.isEnabled() && hasInternet) {
3015 if (!force) {
3016 disconnect(account, false);
3017 }
3018 Thread thread = new Thread(connection);
3019 connection.setInteractive(interactive);
3020 connection.prepareNewConnection();
3021 connection.interrupt();
3022 thread.start();
3023 scheduleWakeUpCall(Config.CONNECT_DISCO_TIMEOUT, account.getUuid().hashCode());
3024 } else {
3025 disconnect(account, force || account.getTrueStatus().isError() || !hasInternet);
3026 account.getRoster().clearPresences();
3027 connection.resetEverything();
3028 final AxolotlService axolotlService = account.getAxolotlService();
3029 if (axolotlService != null) {
3030 axolotlService.resetBrokenness();
3031 }
3032 if (!hasInternet) {
3033 account.setStatus(Account.State.NO_INTERNET);
3034 }
3035 }
3036 }
3037 }
3038
3039 public void reconnectAccountInBackground(final Account account) {
3040 new Thread(() -> reconnectAccount(account, false, true)).start();
3041 }
3042
3043 public void invite(Conversation conversation, Jid contact) {
3044 Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": inviting " + contact + " to " + conversation.getJid().asBareJid());
3045 MessagePacket packet = mMessageGenerator.invite(conversation, contact);
3046 sendMessagePacket(conversation.getAccount(), packet);
3047 }
3048
3049 public void directInvite(Conversation conversation, Jid jid) {
3050 MessagePacket packet = mMessageGenerator.directInvite(conversation, jid);
3051 sendMessagePacket(conversation.getAccount(), packet);
3052 }
3053
3054 public void resetSendingToWaiting(Account account) {
3055 for (Conversation conversation : getConversations()) {
3056 if (conversation.getAccount() == account) {
3057 conversation.findUnsentTextMessages(new Conversation.OnMessageFound() {
3058
3059 @Override
3060 public void onMessageFound(Message message) {
3061 markMessage(message, Message.STATUS_WAITING);
3062 }
3063 });
3064 }
3065 }
3066 }
3067
3068 public Message markMessage(final Account account, final Jid recipient, final String uuid, final int status) {
3069 return markMessage(account, recipient, uuid, status, null);
3070 }
3071
3072 public Message markMessage(final Account account, final Jid recipient, final String uuid, final int status, String errorMessage) {
3073 if (uuid == null) {
3074 return null;
3075 }
3076 for (Conversation conversation : getConversations()) {
3077 if (conversation.getJid().asBareJid().equals(recipient) && conversation.getAccount() == account) {
3078 final Message message = conversation.findSentMessageWithUuidOrRemoteId(uuid);
3079 if (message != null) {
3080 markMessage(message, status, errorMessage);
3081 }
3082 return message;
3083 }
3084 }
3085 return null;
3086 }
3087
3088 public boolean markMessage(Conversation conversation, String uuid, int status, String serverMessageId) {
3089 if (uuid == null) {
3090 return false;
3091 } else {
3092 Message message = conversation.findSentMessageWithUuid(uuid);
3093 if (message != null) {
3094 if (message.getServerMsgId() == null) {
3095 message.setServerMsgId(serverMessageId);
3096 }
3097 markMessage(message, status);
3098 return true;
3099 } else {
3100 return false;
3101 }
3102 }
3103 }
3104
3105 public void markMessage(Message message, int status) {
3106 markMessage(message, status, null);
3107 }
3108
3109
3110 public void markMessage(Message message, int status, String errorMessage) {
3111 final int c = message.getStatus();
3112 if (status == Message.STATUS_SEND_FAILED && (c == Message.STATUS_SEND_RECEIVED || c == Message.STATUS_SEND_DISPLAYED)) {
3113 return;
3114 }
3115 if (status == Message.STATUS_SEND_RECEIVED && c == Message.STATUS_SEND_DISPLAYED) {
3116 return;
3117 }
3118 message.setErrorMessage(errorMessage);
3119 message.setStatus(status);
3120 databaseBackend.updateMessage(message);
3121 updateConversationUi();
3122 }
3123
3124 private SharedPreferences getPreferences() {
3125 return PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
3126 }
3127
3128 public long getAutomaticMessageDeletionDate() {
3129 final long timeout = getLongPreference(SettingsActivity.AUTOMATIC_MESSAGE_DELETION, R.integer.automatic_message_deletion);
3130 return timeout == 0 ? timeout : (System.currentTimeMillis() - (timeout * 1000));
3131 }
3132
3133 public long getLongPreference(String name, @IntegerRes int res) {
3134 long defaultValue = getResources().getInteger(res);
3135 try {
3136 return Long.parseLong(getPreferences().getString(name, String.valueOf(defaultValue)));
3137 } catch (NumberFormatException e) {
3138 return defaultValue;
3139 }
3140 }
3141
3142 public boolean getBooleanPreference(String name, @BoolRes int res) {
3143 return getPreferences().getBoolean(name, getResources().getBoolean(res));
3144 }
3145
3146 public boolean confirmMessages() {
3147 return getBooleanPreference("confirm_messages", R.bool.confirm_messages);
3148 }
3149
3150 public boolean allowMessageCorrection() {
3151 return getBooleanPreference("allow_message_correction", R.bool.allow_message_correction);
3152 }
3153
3154 public boolean sendChatStates() {
3155 return getBooleanPreference("chat_states", R.bool.chat_states);
3156 }
3157
3158 private boolean respectAutojoin() {
3159 return getBooleanPreference("autojoin", R.bool.autojoin);
3160 }
3161
3162 public boolean indicateReceived() {
3163 return getBooleanPreference("indicate_received", R.bool.indicate_received);
3164 }
3165
3166 public boolean useTorToConnect() {
3167 return Config.FORCE_ORBOT || getBooleanPreference("use_tor", R.bool.use_tor);
3168 }
3169
3170 public boolean showExtendedConnectionOptions() {
3171 return getBooleanPreference("show_connection_options", R.bool.show_connection_options);
3172 }
3173
3174 public boolean broadcastLastActivity() {
3175 return getBooleanPreference(SettingsActivity.BROADCAST_LAST_ACTIVITY, R.bool.last_activity);
3176 }
3177
3178 public int unreadCount() {
3179 int count = 0;
3180 for (Conversation conversation : getConversations()) {
3181 count += conversation.unreadCount();
3182 }
3183 return count;
3184 }
3185
3186
3187 public void showErrorToastInUi(int resId) {
3188 if (mOnShowErrorToast != null) {
3189 mOnShowErrorToast.onShowErrorToast(resId);
3190 }
3191 }
3192
3193 public void updateConversationUi() {
3194 if (mOnConversationUpdate != null) {
3195 mOnConversationUpdate.onConversationUpdate();
3196 }
3197 }
3198
3199 public void updateAccountUi() {
3200 if (mOnAccountUpdate != null) {
3201 mOnAccountUpdate.onAccountUpdate();
3202 }
3203 }
3204
3205 public void updateRosterUi() {
3206 if (mOnRosterUpdate != null) {
3207 mOnRosterUpdate.onRosterUpdate();
3208 }
3209 }
3210
3211 public boolean displayCaptchaRequest(Account account, String id, Data data, Bitmap captcha) {
3212 if (mOnCaptchaRequested != null) {
3213 DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
3214 Bitmap scaled = Bitmap.createScaledBitmap(captcha, (int) (captcha.getWidth() * metrics.scaledDensity),
3215 (int) (captcha.getHeight() * metrics.scaledDensity), false);
3216
3217 mOnCaptchaRequested.onCaptchaRequested(account, id, data, scaled);
3218 return true;
3219 }
3220 return false;
3221 }
3222
3223 public void updateBlocklistUi(final OnUpdateBlocklist.Status status) {
3224 if (mOnUpdateBlocklist != null) {
3225 mOnUpdateBlocklist.OnUpdateBlocklist(status);
3226 }
3227 }
3228
3229 public void updateMucRosterUi() {
3230 if (mOnMucRosterUpdate != null) {
3231 mOnMucRosterUpdate.onMucRosterUpdate();
3232 }
3233 }
3234
3235 public void keyStatusUpdated(AxolotlService.FetchStatus report) {
3236 if (mOnKeyStatusUpdated != null) {
3237 mOnKeyStatusUpdated.onKeyStatusUpdated(report);
3238 }
3239 }
3240
3241 public Account findAccountByJid(final Jid accountJid) {
3242 for (Account account : this.accounts) {
3243 if (account.getJid().asBareJid().equals(accountJid.asBareJid())) {
3244 return account;
3245 }
3246 }
3247 return null;
3248 }
3249
3250 public Account findAccountByUuid(final String uuid) {
3251 for(Account account : this.accounts) {
3252 if (account.getUuid().equals(uuid)) {
3253 return account;
3254 }
3255 }
3256 return null;
3257 }
3258
3259 public Conversation findConversationByUuid(String uuid) {
3260 for (Conversation conversation : getConversations()) {
3261 if (conversation.getUuid().equals(uuid)) {
3262 return conversation;
3263 }
3264 }
3265 return null;
3266 }
3267
3268 public boolean markRead(final Conversation conversation, boolean dismiss) {
3269 return markRead(conversation, null, dismiss).size() > 0;
3270 }
3271
3272 public void markRead(final Conversation conversation) {
3273 markRead(conversation, null, true);
3274 }
3275
3276 public List<Message> markRead(final Conversation conversation, String upToUuid, boolean dismiss) {
3277 if (dismiss) {
3278 mNotificationService.clear(conversation);
3279 }
3280 final List<Message> readMessages = conversation.markRead(upToUuid);
3281 if (readMessages.size() > 0) {
3282 Runnable runnable = () -> {
3283 for (Message message : readMessages) {
3284 databaseBackend.updateMessage(message);
3285 }
3286 };
3287 mDatabaseWriterExecutor.execute(runnable);
3288 updateUnreadCountBadge();
3289 return readMessages;
3290 } else {
3291 return readMessages;
3292 }
3293 }
3294
3295 public synchronized void updateUnreadCountBadge() {
3296 int count = unreadCount();
3297 if (unreadCount != count) {
3298 Log.d(Config.LOGTAG, "update unread count to " + count);
3299 if (count > 0) {
3300 ShortcutBadger.applyCount(getApplicationContext(), count);
3301 } else {
3302 ShortcutBadger.removeCount(getApplicationContext());
3303 }
3304 unreadCount = count;
3305 }
3306 }
3307
3308 public void sendReadMarker(final Conversation conversation, String upToUuid) {
3309 final boolean isPrivateAndNonAnonymousMuc = conversation.getMode() == Conversation.MODE_MULTI && conversation.isPrivateAndNonAnonymous();
3310 final List<Message> readMessages = this.markRead(conversation, upToUuid, true);
3311 if (readMessages.size() > 0) {
3312 updateConversationUi();
3313 }
3314 final Message markable = Conversation.getLatestMarkableMessage(readMessages, isPrivateAndNonAnonymousMuc);
3315 if (confirmMessages()
3316 && markable != null
3317 && (markable.trusted() || isPrivateAndNonAnonymousMuc)
3318 && markable.getRemoteMsgId() != null) {
3319 Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": sending read marker to " + markable.getCounterpart().toString());
3320 Account account = conversation.getAccount();
3321 final Jid to = markable.getCounterpart();
3322 final boolean groupChat = conversation.getMode() == Conversation.MODE_MULTI;
3323 MessagePacket packet = mMessageGenerator.confirm(account, to, markable.getRemoteMsgId(), markable.getCounterpart(), groupChat);
3324 this.sendMessagePacket(conversation.getAccount(), packet);
3325 }
3326 }
3327
3328 public SecureRandom getRNG() {
3329 return this.mRandom;
3330 }
3331
3332 public MemorizingTrustManager getMemorizingTrustManager() {
3333 return this.mMemorizingTrustManager;
3334 }
3335
3336 public void setMemorizingTrustManager(MemorizingTrustManager trustManager) {
3337 this.mMemorizingTrustManager = trustManager;
3338 }
3339
3340 public void updateMemorizingTrustmanager() {
3341 final MemorizingTrustManager tm;
3342 final boolean dontTrustSystemCAs = getBooleanPreference("dont_trust_system_cas", R.bool.dont_trust_system_cas);
3343 if (dontTrustSystemCAs) {
3344 tm = new MemorizingTrustManager(getApplicationContext(), null);
3345 } else {
3346 tm = new MemorizingTrustManager(getApplicationContext());
3347 }
3348 setMemorizingTrustManager(tm);
3349 }
3350
3351 public LruCache<String, Bitmap> getBitmapCache() {
3352 return this.mBitmapCache;
3353 }
3354
3355 public Collection<String> getKnownHosts() {
3356 final Set<String> hosts = new HashSet<>();
3357 for (final Account account : getAccounts()) {
3358 hosts.add(account.getServer());
3359 for (final Contact contact : account.getRoster().getContacts()) {
3360 if (contact.showInRoster()) {
3361 final String server = contact.getServer();
3362 if (server != null && !hosts.contains(server)) {
3363 hosts.add(server);
3364 }
3365 }
3366 }
3367 }
3368 if (Config.DOMAIN_LOCK != null) {
3369 hosts.add(Config.DOMAIN_LOCK);
3370 }
3371 if (Config.MAGIC_CREATE_DOMAIN != null) {
3372 hosts.add(Config.MAGIC_CREATE_DOMAIN);
3373 }
3374 return hosts;
3375 }
3376
3377 public Collection<String> getKnownConferenceHosts() {
3378 final Set<String> mucServers = new HashSet<>();
3379 for (final Account account : accounts) {
3380 if (account.getXmppConnection() != null) {
3381 mucServers.addAll(account.getXmppConnection().getMucServers());
3382 for (Bookmark bookmark : account.getBookmarks()) {
3383 final Jid jid = bookmark.getJid();
3384 final String s = jid == null ? null : jid.getDomain();
3385 if (s != null) {
3386 mucServers.add(s);
3387 }
3388 }
3389 }
3390 }
3391 return mucServers;
3392 }
3393
3394 public void sendMessagePacket(Account account, MessagePacket packet) {
3395 XmppConnection connection = account.getXmppConnection();
3396 if (connection != null) {
3397 connection.sendMessagePacket(packet);
3398 }
3399 }
3400
3401 public void sendPresencePacket(Account account, PresencePacket packet) {
3402 XmppConnection connection = account.getXmppConnection();
3403 if (connection != null) {
3404 connection.sendPresencePacket(packet);
3405 }
3406 }
3407
3408 public void sendCreateAccountWithCaptchaPacket(Account account, String id, Data data) {
3409 final XmppConnection connection = account.getXmppConnection();
3410 if (connection != null) {
3411 IqPacket request = mIqGenerator.generateCreateAccountWithCaptcha(account, id, data);
3412 connection.sendUnmodifiedIqPacket(request, connection.registrationResponseListener, true);
3413 }
3414 }
3415
3416 public void sendIqPacket(final Account account, final IqPacket packet, final OnIqPacketReceived callback) {
3417 final XmppConnection connection = account.getXmppConnection();
3418 if (connection != null) {
3419 connection.sendIqPacket(packet, callback);
3420 }
3421 }
3422
3423 public void sendPresence(final Account account) {
3424 sendPresence(account, checkListeners() && broadcastLastActivity());
3425 }
3426
3427 private void sendPresence(final Account account, final boolean includeIdleTimestamp) {
3428 Presence.Status status;
3429 if (manuallyChangePresence()) {
3430 status = account.getPresenceStatus();
3431 } else {
3432 status = getTargetPresence();
3433 }
3434 PresencePacket packet = mPresenceGenerator.selfPresence(account, status);
3435 String message = account.getPresenceStatusMessage();
3436 if (message != null && !message.isEmpty()) {
3437 packet.addChild(new Element("status").setContent(message));
3438 }
3439 if (mLastActivity > 0 && includeIdleTimestamp) {
3440 long since = Math.min(mLastActivity, System.currentTimeMillis()); //don't send future dates
3441 packet.addChild("idle", Namespace.IDLE).setAttribute("since", AbstractGenerator.getTimestamp(since));
3442 }
3443 sendPresencePacket(account, packet);
3444 }
3445
3446 private void deactivateGracePeriod() {
3447 for (Account account : getAccounts()) {
3448 account.deactivateGracePeriod();
3449 }
3450 }
3451
3452 public void refreshAllPresences() {
3453 boolean includeIdleTimestamp = checkListeners() && broadcastLastActivity();
3454 for (Account account : getAccounts()) {
3455 if (account.isEnabled()) {
3456 sendPresence(account, includeIdleTimestamp);
3457 }
3458 }
3459 }
3460
3461 private void refreshAllGcmTokens() {
3462 for (Account account : getAccounts()) {
3463 if (account.isOnlineAndConnected() && mPushManagementService.available(account)) {
3464 mPushManagementService.registerPushTokenOnServer(account);
3465 }
3466 }
3467 }
3468
3469 private void sendOfflinePresence(final Account account) {
3470 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": sending offline presence");
3471 sendPresencePacket(account, mPresenceGenerator.sendOfflinePresence(account));
3472 }
3473
3474 public MessageGenerator getMessageGenerator() {
3475 return this.mMessageGenerator;
3476 }
3477
3478 public PresenceGenerator getPresenceGenerator() {
3479 return this.mPresenceGenerator;
3480 }
3481
3482 public IqGenerator getIqGenerator() {
3483 return this.mIqGenerator;
3484 }
3485
3486 public IqParser getIqParser() {
3487 return this.mIqParser;
3488 }
3489
3490 public JingleConnectionManager getJingleConnectionManager() {
3491 return this.mJingleConnectionManager;
3492 }
3493
3494 public MessageArchiveService getMessageArchiveService() {
3495 return this.mMessageArchiveService;
3496 }
3497
3498 public List<Contact> findContacts(Jid jid, String accountJid) {
3499 ArrayList<Contact> contacts = new ArrayList<>();
3500 for (Account account : getAccounts()) {
3501 if ((account.isEnabled() || accountJid != null)
3502 && (accountJid == null || accountJid.equals(account.getJid().asBareJid().toString()))) {
3503 Contact contact = account.getRoster().getContactFromRoster(jid);
3504 if (contact != null) {
3505 contacts.add(contact);
3506 }
3507 }
3508 }
3509 return contacts;
3510 }
3511
3512 public Conversation findFirstMuc(Jid jid) {
3513 for (Conversation conversation : getConversations()) {
3514 if (conversation.getAccount().isEnabled() && conversation.getJid().asBareJid().equals(jid.asBareJid()) && conversation.getMode() == Conversation.MODE_MULTI) {
3515 return conversation;
3516 }
3517 }
3518 return null;
3519 }
3520
3521 public NotificationService getNotificationService() {
3522 return this.mNotificationService;
3523 }
3524
3525 public HttpConnectionManager getHttpConnectionManager() {
3526 return this.mHttpConnectionManager;
3527 }
3528
3529 public void resendFailedMessages(final Message message) {
3530 final Collection<Message> messages = new ArrayList<>();
3531 Message current = message;
3532 while (current.getStatus() == Message.STATUS_SEND_FAILED) {
3533 messages.add(current);
3534 if (current.mergeable(current.next())) {
3535 current = current.next();
3536 } else {
3537 break;
3538 }
3539 }
3540 for (final Message msg : messages) {
3541 msg.setTime(System.currentTimeMillis());
3542 markMessage(msg, Message.STATUS_WAITING);
3543 this.resendMessage(msg, false);
3544 }
3545 if (message.getConversation() instanceof Conversation) {
3546 ((Conversation) message.getConversation()).sort();
3547 }
3548 updateConversationUi();
3549 }
3550
3551 public void clearConversationHistory(final Conversation conversation) {
3552 final long clearDate;
3553 final String reference;
3554 if (conversation.countMessages() > 0) {
3555 Message latestMessage = conversation.getLatestMessage();
3556 clearDate = latestMessage.getTimeSent() + 1000;
3557 reference = latestMessage.getServerMsgId();
3558 } else {
3559 clearDate = System.currentTimeMillis();
3560 reference = null;
3561 }
3562 conversation.clearMessages();
3563 conversation.setHasMessagesLeftOnServer(false); //avoid messages getting loaded through mam
3564 conversation.setLastClearHistory(clearDate, reference);
3565 Runnable runnable = () -> {
3566 databaseBackend.deleteMessagesInConversation(conversation);
3567 databaseBackend.updateConversation(conversation);
3568 };
3569 mDatabaseWriterExecutor.execute(runnable);
3570 }
3571
3572 public boolean sendBlockRequest(final Blockable blockable, boolean reportSpam) {
3573 if (blockable != null && blockable.getBlockedJid() != null) {
3574 final Jid jid = blockable.getBlockedJid();
3575 this.sendIqPacket(blockable.getAccount(), getIqGenerator().generateSetBlockRequest(jid, reportSpam), new OnIqPacketReceived() {
3576
3577 @Override
3578 public void onIqPacketReceived(final Account account, final IqPacket packet) {
3579 if (packet.getType() == IqPacket.TYPE.RESULT) {
3580 account.getBlocklist().add(jid);
3581 updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
3582 }
3583 }
3584 });
3585 if (removeBlockedConversations(blockable.getAccount(), jid)) {
3586 updateConversationUi();
3587 return true;
3588 } else {
3589 return false;
3590 }
3591 } else {
3592 return false;
3593 }
3594 }
3595
3596 public boolean removeBlockedConversations(final Account account, final Jid blockedJid) {
3597 boolean removed = false;
3598 synchronized (this.conversations) {
3599 boolean domainJid = blockedJid.getLocal() == null;
3600 for (Conversation conversation : this.conversations) {
3601 boolean jidMatches = (domainJid && blockedJid.getDomain().equals(conversation.getJid().getDomain()))
3602 || blockedJid.equals(conversation.getJid().asBareJid());
3603 if (conversation.getAccount() == account
3604 && conversation.getMode() == Conversation.MODE_SINGLE
3605 && jidMatches) {
3606 this.conversations.remove(conversation);
3607 markRead(conversation);
3608 conversation.setStatus(Conversation.STATUS_ARCHIVED);
3609 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": archiving conversation " + conversation.getJid().asBareJid() + " because jid was blocked");
3610 updateConversation(conversation);
3611 removed = true;
3612 }
3613 }
3614 }
3615 return removed;
3616 }
3617
3618 public void sendUnblockRequest(final Blockable blockable) {
3619 if (blockable != null && blockable.getJid() != null) {
3620 final Jid jid = blockable.getBlockedJid();
3621 this.sendIqPacket(blockable.getAccount(), getIqGenerator().generateSetUnblockRequest(jid), new OnIqPacketReceived() {
3622 @Override
3623 public void onIqPacketReceived(final Account account, final IqPacket packet) {
3624 if (packet.getType() == IqPacket.TYPE.RESULT) {
3625 account.getBlocklist().remove(jid);
3626 updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
3627 }
3628 }
3629 });
3630 }
3631 }
3632
3633 public void publishDisplayName(Account account) {
3634 String displayName = account.getDisplayName();
3635 if (displayName != null && !displayName.isEmpty()) {
3636 IqPacket publish = mIqGenerator.publishNick(displayName);
3637 sendIqPacket(account, publish, (account1, packet) -> {
3638 if (packet.getType() == IqPacket.TYPE.ERROR) {
3639 Log.d(Config.LOGTAG, account1.getJid().asBareJid() + ": could not publish nick");
3640 }
3641 });
3642 }
3643 }
3644
3645 public ServiceDiscoveryResult getCachedServiceDiscoveryResult(Pair<String, String> key) {
3646 ServiceDiscoveryResult result = discoCache.get(key);
3647 if (result != null) {
3648 return result;
3649 } else {
3650 result = databaseBackend.findDiscoveryResult(key.first, key.second);
3651 if (result != null) {
3652 discoCache.put(key, result);
3653 }
3654 return result;
3655 }
3656 }
3657
3658 public void fetchCaps(Account account, final Jid jid, final Presence presence) {
3659 final Pair<String, String> key = new Pair<>(presence.getHash(), presence.getVer());
3660 ServiceDiscoveryResult disco = getCachedServiceDiscoveryResult(key);
3661 if (disco != null) {
3662 presence.setServiceDiscoveryResult(disco);
3663 } else {
3664 if (!account.inProgressDiscoFetches.contains(key)) {
3665 account.inProgressDiscoFetches.add(key);
3666 IqPacket request = new IqPacket(IqPacket.TYPE.GET);
3667 request.setTo(jid);
3668 final String node = presence.getNode();
3669 final String ver = presence.getVer();
3670 final Element query = request.query("http://jabber.org/protocol/disco#info");
3671 if (node != null && ver != null) {
3672 query.setAttribute("node",node+"#"+ver);
3673 }
3674 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": making disco request for " + key.second + " to " + jid);
3675 sendIqPacket(account, request, (a, response) -> {
3676 if (response.getType() == IqPacket.TYPE.RESULT) {
3677 ServiceDiscoveryResult discoveryResult = new ServiceDiscoveryResult(response);
3678 if (presence.getVer().equals(discoveryResult.getVer())) {
3679 databaseBackend.insertDiscoveryResult(discoveryResult);
3680 injectServiceDiscorveryResult(a.getRoster(), presence.getHash(), presence.getVer(), discoveryResult);
3681 } else {
3682 Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": mismatch in caps for contact " + jid + " " + presence.getVer() + " vs " + discoveryResult.getVer());
3683 }
3684 }
3685 a.inProgressDiscoFetches.remove(key);
3686 });
3687 }
3688 }
3689 }
3690
3691 private void injectServiceDiscorveryResult(Roster roster, String hash, String ver, ServiceDiscoveryResult disco) {
3692 for (Contact contact : roster.getContacts()) {
3693 for (Presence presence : contact.getPresences().getPresences().values()) {
3694 if (hash.equals(presence.getHash()) && ver.equals(presence.getVer())) {
3695 presence.setServiceDiscoveryResult(disco);
3696 }
3697 }
3698 }
3699 }
3700
3701 public void fetchMamPreferences(Account account, final OnMamPreferencesFetched callback) {
3702 final boolean legacy = account.getXmppConnection().getFeatures().mamLegacy();
3703 IqPacket request = new IqPacket(IqPacket.TYPE.GET);
3704 request.addChild("prefs", legacy ? Namespace.MAM_LEGACY : Namespace.MAM);
3705 sendIqPacket(account, request, (account1, packet) -> {
3706 Element prefs = packet.findChild("prefs", legacy ? Namespace.MAM_LEGACY : Namespace.MAM);
3707 if (packet.getType() == IqPacket.TYPE.RESULT && prefs != null) {
3708 callback.onPreferencesFetched(prefs);
3709 } else {
3710 callback.onPreferencesFetchFailed();
3711 }
3712 });
3713 }
3714
3715 public PushManagementService getPushManagementService() {
3716 return mPushManagementService;
3717 }
3718
3719 public Account getPendingAccount() {
3720 Account pending = null;
3721 for (Account account : getAccounts()) {
3722 if (!account.isOptionSet(Account.OPTION_LOGGED_IN_SUCCESSFULLY)) {
3723 pending = account;
3724 } else {
3725 return null;
3726 }
3727 }
3728 return pending;
3729 }
3730
3731 public void changeStatus(Account account, PresenceTemplate template, String signature) {
3732 if (!template.getStatusMessage().isEmpty()) {
3733 databaseBackend.insertPresenceTemplate(template);
3734 }
3735 account.setPgpSignature(signature);
3736 account.setPresenceStatus(template.getStatus());
3737 account.setPresenceStatusMessage(template.getStatusMessage());
3738 databaseBackend.updateAccount(account);
3739 sendPresence(account);
3740 }
3741
3742 public List<PresenceTemplate> getPresenceTemplates(Account account) {
3743 List<PresenceTemplate> templates = databaseBackend.getPresenceTemplates();
3744 for (PresenceTemplate template : account.getSelfContact().getPresences().asTemplates()) {
3745 if (!templates.contains(template)) {
3746 templates.add(0, template);
3747 }
3748 }
3749 return templates;
3750 }
3751
3752 public void saveConversationAsBookmark(Conversation conversation, String name) {
3753 Account account = conversation.getAccount();
3754 Bookmark bookmark = new Bookmark(account, conversation.getJid().asBareJid());
3755 if (!conversation.getJid().isBareJid()) {
3756 bookmark.setNick(conversation.getJid().getResource());
3757 }
3758 if (name != null && !name.trim().isEmpty()) {
3759 bookmark.setBookmarkName(name.trim());
3760 }
3761 bookmark.setAutojoin(getPreferences().getBoolean("autojoin", getResources().getBoolean(R.bool.autojoin)));
3762 account.getBookmarks().add(bookmark);
3763 pushBookmarks(account);
3764 bookmark.setConversation(conversation);
3765 }
3766
3767 public boolean verifyFingerprints(Contact contact, List<XmppUri.Fingerprint> fingerprints) {
3768 boolean performedVerification = false;
3769 final AxolotlService axolotlService = contact.getAccount().getAxolotlService();
3770 for (XmppUri.Fingerprint fp : fingerprints) {
3771 if (fp.type == XmppUri.FingerprintType.OMEMO) {
3772 String fingerprint = "05" + fp.fingerprint.replaceAll("\\s", "");
3773 FingerprintStatus fingerprintStatus = axolotlService.getFingerprintTrust(fingerprint);
3774 if (fingerprintStatus != null) {
3775 if (!fingerprintStatus.isVerified()) {
3776 performedVerification = true;
3777 axolotlService.setFingerprintTrust(fingerprint, fingerprintStatus.toVerified());
3778 }
3779 } else {
3780 axolotlService.preVerifyFingerprint(contact, fingerprint);
3781 }
3782 }
3783 }
3784 return performedVerification;
3785 }
3786
3787 public boolean verifyFingerprints(Account account, List<XmppUri.Fingerprint> fingerprints) {
3788 final AxolotlService axolotlService = account.getAxolotlService();
3789 boolean verifiedSomething = false;
3790 for (XmppUri.Fingerprint fp : fingerprints) {
3791 if (fp.type == XmppUri.FingerprintType.OMEMO) {
3792 String fingerprint = "05" + fp.fingerprint.replaceAll("\\s", "");
3793 Log.d(Config.LOGTAG, "trying to verify own fp=" + fingerprint);
3794 FingerprintStatus fingerprintStatus = axolotlService.getFingerprintTrust(fingerprint);
3795 if (fingerprintStatus != null) {
3796 if (!fingerprintStatus.isVerified()) {
3797 axolotlService.setFingerprintTrust(fingerprint, fingerprintStatus.toVerified());
3798 verifiedSomething = true;
3799 }
3800 } else {
3801 axolotlService.preVerifyFingerprint(account, fingerprint);
3802 verifiedSomething = true;
3803 }
3804 }
3805 }
3806 return verifiedSomething;
3807 }
3808
3809 public boolean blindTrustBeforeVerification() {
3810 return getBooleanPreference(SettingsActivity.BLIND_TRUST_BEFORE_VERIFICATION, R.bool.btbv);
3811 }
3812
3813 public ShortcutService getShortcutService() {
3814 return mShortcutService;
3815 }
3816
3817 public void pushMamPreferences(Account account, Element prefs) {
3818 IqPacket set = new IqPacket(IqPacket.TYPE.SET);
3819 set.addChild(prefs);
3820 sendIqPacket(account, set, null);
3821 }
3822
3823 public interface OnMamPreferencesFetched {
3824 void onPreferencesFetched(Element prefs);
3825
3826 void onPreferencesFetchFailed();
3827 }
3828
3829 public interface OnAccountCreated {
3830 void onAccountCreated(Account account);
3831
3832 void informUser(int r);
3833 }
3834
3835 public interface OnMoreMessagesLoaded {
3836 void onMoreMessagesLoaded(int count, Conversation conversation);
3837
3838 void informUser(int r);
3839 }
3840
3841 public interface OnAccountPasswordChanged {
3842 void onPasswordChangeSucceeded();
3843
3844 void onPasswordChangeFailed();
3845 }
3846
3847 public interface OnAffiliationChanged {
3848 void onAffiliationChangedSuccessful(Jid jid);
3849
3850 void onAffiliationChangeFailed(Jid jid, int resId);
3851 }
3852
3853 public interface OnRoleChanged {
3854 void onRoleChangedSuccessful(String nick);
3855
3856 void onRoleChangeFailed(String nick, int resid);
3857 }
3858
3859 public interface OnConversationUpdate {
3860 void onConversationUpdate();
3861 }
3862
3863 public interface OnAccountUpdate {
3864 void onAccountUpdate();
3865 }
3866
3867 public interface OnCaptchaRequested {
3868 void onCaptchaRequested(Account account, String id, Data data, Bitmap captcha);
3869 }
3870
3871 public interface OnRosterUpdate {
3872 void onRosterUpdate();
3873 }
3874
3875 public interface OnMucRosterUpdate {
3876 void onMucRosterUpdate();
3877 }
3878
3879 public interface OnConferenceConfigurationFetched {
3880 void onConferenceConfigurationFetched(Conversation conversation);
3881
3882 void onFetchFailed(Conversation conversation, Element error);
3883 }
3884
3885 public interface OnConferenceJoined {
3886 void onConferenceJoined(Conversation conversation);
3887 }
3888
3889 public interface OnConfigurationPushed {
3890 void onPushSucceeded();
3891
3892 void onPushFailed();
3893 }
3894
3895 public interface OnShowErrorToast {
3896 void onShowErrorToast(int resId);
3897 }
3898
3899 public class XmppConnectionBinder extends Binder {
3900 public XmppConnectionService getService() {
3901 return XmppConnectionService.this;
3902 }
3903 }
3904}