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