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