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 == null) {
1797 Log.d(Config.LOGTAG, account.getJid().asBareJid()+": no connection. ignoring bookmark creation");
1798 } else if (connection.getFeatures().bookmarks2()) {
1799 final Element item = mIqGenerator.publishBookmarkItem(bookmark);
1800 pushNodeAndEnforcePublishOptions(account, Namespace.BOOKMARKS2, item, bookmark.getJid().asBareJid().toEscapedString(), PublishOptions.persistentWhitelistAccessMaxItems());
1801 } else if (connection.getFeatures().bookmarksConversion()) {
1802 pushBookmarksPep(account);
1803 } else {
1804 pushBookmarksPrivateXml(account);
1805 }
1806 }
1807
1808 public void deleteBookmark(final Account account, final Bookmark bookmark) {
1809 account.removeBookmark(bookmark);
1810 final XmppConnection connection = account.getXmppConnection();
1811 if (connection.getFeatures().bookmarks2()) {
1812 IqPacket request = mIqGenerator.deleteItem(Namespace.BOOKMARKS2, bookmark.getJid().asBareJid().toEscapedString());
1813 sendIqPacket(account, request, (a, response) -> {
1814 if (response.getType() == IqPacket.TYPE.ERROR) {
1815 Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": unable to delete bookmark " + response.getErrorCondition());
1816 }
1817 });
1818 } else if (connection.getFeatures().bookmarksConversion()) {
1819 pushBookmarksPep(account);
1820 } else {
1821 pushBookmarksPrivateXml(account);
1822 }
1823 }
1824
1825 private void pushBookmarksPrivateXml(Account account) {
1826 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": pushing bookmarks via private xml");
1827 IqPacket iqPacket = new IqPacket(IqPacket.TYPE.SET);
1828 Element query = iqPacket.query("jabber:iq:private");
1829 Element storage = query.addChild("storage", "storage:bookmarks");
1830 for (Bookmark bookmark : account.getBookmarks()) {
1831 storage.addChild(bookmark);
1832 }
1833 sendIqPacket(account, iqPacket, mDefaultIqHandler);
1834 }
1835
1836 private void pushBookmarksPep(Account account) {
1837 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": pushing bookmarks via pep");
1838 Element storage = new Element("storage", "storage:bookmarks");
1839 for (Bookmark bookmark : account.getBookmarks()) {
1840 storage.addChild(bookmark);
1841 }
1842 pushNodeAndEnforcePublishOptions(account, Namespace.BOOKMARKS, storage, "current", PublishOptions.persistentWhitelistAccess());
1843
1844 }
1845
1846 private void pushNodeAndEnforcePublishOptions(final Account account, final String node, final Element element, final String id, final Bundle options) {
1847 pushNodeAndEnforcePublishOptions(account, node, element, id, options, true);
1848
1849 }
1850
1851 private void pushNodeAndEnforcePublishOptions(final Account account, final String node, final Element element, final String id, final Bundle options, final boolean retry) {
1852 final IqPacket packet = mIqGenerator.publishElement(node, element, id, options);
1853 sendIqPacket(account, packet, (a, response) -> {
1854 if (response.getType() == IqPacket.TYPE.RESULT) {
1855 return;
1856 }
1857 if (retry && PublishOptions.preconditionNotMet(response)) {
1858 pushNodeConfiguration(account, node, options, new OnConfigurationPushed() {
1859 @Override
1860 public void onPushSucceeded() {
1861 pushNodeAndEnforcePublishOptions(account, node, element, id, options, false);
1862 }
1863
1864 @Override
1865 public void onPushFailed() {
1866 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to push node configuration (" + node + ")");
1867 }
1868 });
1869 } else {
1870 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": error publishing bookmarks (retry=" + retry + ") " + response);
1871 }
1872 });
1873 }
1874
1875 private void restoreFromDatabase() {
1876 synchronized (this.conversations) {
1877 final Map<String, Account> accountLookupTable = new Hashtable<>();
1878 for (Account account : this.accounts) {
1879 accountLookupTable.put(account.getUuid(), account);
1880 }
1881 Log.d(Config.LOGTAG, "restoring conversations...");
1882 final long startTimeConversationsRestore = SystemClock.elapsedRealtime();
1883 this.conversations.addAll(databaseBackend.getConversations(Conversation.STATUS_AVAILABLE));
1884 for (Iterator<Conversation> iterator = conversations.listIterator(); iterator.hasNext(); ) {
1885 Conversation conversation = iterator.next();
1886 Account account = accountLookupTable.get(conversation.getAccountUuid());
1887 if (account != null) {
1888 conversation.setAccount(account);
1889 } else {
1890 Log.e(Config.LOGTAG, "unable to restore Conversations with " + conversation.getJid());
1891 iterator.remove();
1892 }
1893 }
1894 long diffConversationsRestore = SystemClock.elapsedRealtime() - startTimeConversationsRestore;
1895 Log.d(Config.LOGTAG, "finished restoring conversations in " + diffConversationsRestore + "ms");
1896 Runnable runnable = () -> {
1897 if (DatabaseBackend.requiresMessageIndexRebuild()) {
1898 DatabaseBackend.getInstance(this).rebuildMessagesIndex();
1899 }
1900 final long deletionDate = getAutomaticMessageDeletionDate();
1901 mLastExpiryRun.set(SystemClock.elapsedRealtime());
1902 if (deletionDate > 0) {
1903 Log.d(Config.LOGTAG, "deleting messages that are older than " + AbstractGenerator.getTimestamp(deletionDate));
1904 databaseBackend.expireOldMessages(deletionDate);
1905 }
1906 Log.d(Config.LOGTAG, "restoring roster...");
1907 for (Account account : accounts) {
1908 databaseBackend.readRoster(account.getRoster());
1909 account.initAccountServices(XmppConnectionService.this); //roster needs to be loaded at this stage
1910 }
1911 getBitmapCache().evictAll();
1912 loadPhoneContacts();
1913 Log.d(Config.LOGTAG, "restoring messages...");
1914 final long startMessageRestore = SystemClock.elapsedRealtime();
1915 final Conversation quickLoad = QuickLoader.get(this.conversations);
1916 if (quickLoad != null) {
1917 restoreMessages(quickLoad);
1918 updateConversationUi();
1919 final long diffMessageRestore = SystemClock.elapsedRealtime() - startMessageRestore;
1920 Log.d(Config.LOGTAG, "quickly restored " + quickLoad.getName() + " after " + diffMessageRestore + "ms");
1921 }
1922 for (Conversation conversation : this.conversations) {
1923 if (quickLoad != conversation) {
1924 restoreMessages(conversation);
1925 }
1926 }
1927 mNotificationService.finishBacklog(false);
1928 restoredFromDatabaseLatch.countDown();
1929 final long diffMessageRestore = SystemClock.elapsedRealtime() - startMessageRestore;
1930 Log.d(Config.LOGTAG, "finished restoring messages in " + diffMessageRestore + "ms");
1931 updateConversationUi();
1932 };
1933 mDatabaseReaderExecutor.execute(runnable); //will contain one write command (expiry) but that's fine
1934 }
1935 }
1936
1937 private void restoreMessages(Conversation conversation) {
1938 conversation.addAll(0, databaseBackend.getMessages(conversation, Config.PAGE_SIZE));
1939 conversation.findUnsentTextMessages(message -> markMessage(message, Message.STATUS_WAITING));
1940 conversation.findUnreadMessages(mNotificationService::pushFromBacklog);
1941 }
1942
1943 public void loadPhoneContacts() {
1944 mContactMergerExecutor.execute(() -> {
1945 Map<Jid, JabberIdContact> contacts = JabberIdContact.load(this);
1946 Log.d(Config.LOGTAG, "start merging phone contacts with roster");
1947 for (Account account : accounts) {
1948 List<Contact> withSystemAccounts = account.getRoster().getWithSystemAccounts(JabberIdContact.class);
1949 for (JabberIdContact jidContact : contacts.values()) {
1950 final Contact contact = account.getRoster().getContact(jidContact.getJid());
1951 boolean needsCacheClean = contact.setPhoneContact(jidContact);
1952 if (needsCacheClean) {
1953 getAvatarService().clear(contact);
1954 }
1955 withSystemAccounts.remove(contact);
1956 }
1957 for (Contact contact : withSystemAccounts) {
1958 boolean needsCacheClean = contact.unsetPhoneContact(JabberIdContact.class);
1959 if (needsCacheClean) {
1960 getAvatarService().clear(contact);
1961 }
1962 }
1963 }
1964 Log.d(Config.LOGTAG, "finished merging phone contacts");
1965 mShortcutService.refresh(mInitialAddressbookSyncCompleted.compareAndSet(false, true));
1966 updateRosterUi();
1967 mQuickConversationsService.considerSync();
1968 });
1969 }
1970
1971
1972 public void syncRoster(final Account account) {
1973 mRosterSyncTaskManager.execute(account, () -> databaseBackend.writeRoster(account.getRoster()));
1974 }
1975
1976 public List<Conversation> getConversations() {
1977 return this.conversations;
1978 }
1979
1980 private void markFileDeleted(final File file) {
1981 synchronized (FILENAMES_TO_IGNORE_DELETION) {
1982 if (FILENAMES_TO_IGNORE_DELETION.remove(file.getAbsolutePath())) {
1983 Log.d(Config.LOGTAG, "ignored deletion of " + file.getAbsolutePath());
1984 return;
1985 }
1986 }
1987 final boolean isInternalFile = fileBackend.isInternalFile(file);
1988 final List<String> uuids = databaseBackend.markFileAsDeleted(file, isInternalFile);
1989 Log.d(Config.LOGTAG, "deleted file " + file.getAbsolutePath() + " internal=" + isInternalFile + ", database hits=" + uuids.size());
1990 markUuidsAsDeletedFiles(uuids);
1991 }
1992
1993 private void markUuidsAsDeletedFiles(List<String> uuids) {
1994 boolean deleted = false;
1995 for (Conversation conversation : getConversations()) {
1996 deleted |= conversation.markAsDeleted(uuids);
1997 }
1998 for (final String uuid : uuids) {
1999 evictPreview(uuid);
2000 }
2001 if (deleted) {
2002 updateConversationUi();
2003 }
2004 }
2005
2006 private void markChangedFiles(List<DatabaseBackend.FilePathInfo> infos) {
2007 boolean changed = false;
2008 for (Conversation conversation : getConversations()) {
2009 changed |= conversation.markAsChanged(infos);
2010 }
2011 if (changed) {
2012 updateConversationUi();
2013 }
2014 }
2015
2016 public void populateWithOrderedConversations(final List<Conversation> list) {
2017 populateWithOrderedConversations(list, true, true);
2018 }
2019
2020 public void populateWithOrderedConversations(final List<Conversation> list, final boolean includeNoFileUpload) {
2021 populateWithOrderedConversations(list, includeNoFileUpload, true);
2022 }
2023
2024 public void populateWithOrderedConversations(final List<Conversation> list, final boolean includeNoFileUpload, final boolean sort) {
2025 final List<String> orderedUuids;
2026 if (sort) {
2027 orderedUuids = null;
2028 } else {
2029 orderedUuids = new ArrayList<>();
2030 for (Conversation conversation : list) {
2031 orderedUuids.add(conversation.getUuid());
2032 }
2033 }
2034 list.clear();
2035 if (includeNoFileUpload) {
2036 list.addAll(getConversations());
2037 } else {
2038 for (Conversation conversation : getConversations()) {
2039 if (conversation.getMode() == Conversation.MODE_SINGLE
2040 || (conversation.getAccount().httpUploadAvailable() && conversation.getMucOptions().participating())) {
2041 list.add(conversation);
2042 }
2043 }
2044 }
2045 try {
2046 if (orderedUuids != null) {
2047 Collections.sort(list, (a, b) -> {
2048 final int indexA = orderedUuids.indexOf(a.getUuid());
2049 final int indexB = orderedUuids.indexOf(b.getUuid());
2050 if (indexA == -1 || indexB == -1 || indexA == indexB) {
2051 return a.compareTo(b);
2052 }
2053 return indexA - indexB;
2054 });
2055 } else {
2056 Collections.sort(list);
2057 }
2058 } catch (IllegalArgumentException e) {
2059 //ignore
2060 }
2061 }
2062
2063 public void loadMoreMessages(final Conversation conversation, final long timestamp, final OnMoreMessagesLoaded callback) {
2064 if (XmppConnectionService.this.getMessageArchiveService().queryInProgress(conversation, callback)) {
2065 return;
2066 } else if (timestamp == 0) {
2067 return;
2068 }
2069 Log.d(Config.LOGTAG, "load more messages for " + conversation.getName() + " prior to " + MessageGenerator.getTimestamp(timestamp));
2070 final Runnable runnable = () -> {
2071 final Account account = conversation.getAccount();
2072 List<Message> messages = databaseBackend.getMessages(conversation, 50, timestamp);
2073 if (messages.size() > 0) {
2074 conversation.addAll(0, messages);
2075 callback.onMoreMessagesLoaded(messages.size(), conversation);
2076 } else if (conversation.hasMessagesLeftOnServer()
2077 && account.isOnlineAndConnected()
2078 && conversation.getLastClearHistory().getTimestamp() == 0) {
2079 final boolean mamAvailable;
2080 if (conversation.getMode() == Conversation.MODE_SINGLE) {
2081 mamAvailable = account.getXmppConnection().getFeatures().mam() && !conversation.getContact().isBlocked();
2082 } else {
2083 mamAvailable = conversation.getMucOptions().mamSupport();
2084 }
2085 if (mamAvailable) {
2086 MessageArchiveService.Query query = getMessageArchiveService().query(conversation, new MamReference(0), timestamp, false);
2087 if (query != null) {
2088 query.setCallback(callback);
2089 callback.informUser(R.string.fetching_history_from_server);
2090 } else {
2091 callback.informUser(R.string.not_fetching_history_retention_period);
2092 }
2093
2094 }
2095 }
2096 };
2097 mDatabaseReaderExecutor.execute(runnable);
2098 }
2099
2100 public List<Account> getAccounts() {
2101 return this.accounts;
2102 }
2103
2104
2105 /**
2106 * 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)
2107 */
2108 public List<Conversation> findAllConferencesWith(Contact contact) {
2109 final ArrayList<Conversation> results = new ArrayList<>();
2110 for (final Conversation c : conversations) {
2111 if (c.getMode() != Conversation.MODE_MULTI) {
2112 continue;
2113 }
2114 final MucOptions mucOptions = c.getMucOptions();
2115 if (c.getJid().asBareJid().equals(contact.getJid().asBareJid()) || (mucOptions != null && mucOptions.isContactInRoom(contact))) {
2116 results.add(c);
2117 }
2118 }
2119 return results;
2120 }
2121
2122 public Conversation find(final Iterable<Conversation> haystack, final Contact contact) {
2123 for (final Conversation conversation : haystack) {
2124 if (conversation.getContact() == contact) {
2125 return conversation;
2126 }
2127 }
2128 return null;
2129 }
2130
2131 public Conversation find(final Iterable<Conversation> haystack, final Account account, final Jid jid) {
2132 if (jid == null) {
2133 return null;
2134 }
2135 for (final Conversation conversation : haystack) {
2136 if ((account == null || conversation.getAccount() == account)
2137 && (conversation.getJid().asBareJid().equals(jid.asBareJid()))) {
2138 return conversation;
2139 }
2140 }
2141 return null;
2142 }
2143
2144 public boolean isConversationsListEmpty(final Conversation ignore) {
2145 synchronized (this.conversations) {
2146 final int size = this.conversations.size();
2147 return size == 0 || size == 1 && this.conversations.get(0) == ignore;
2148 }
2149 }
2150
2151 public boolean isConversationStillOpen(final Conversation conversation) {
2152 synchronized (this.conversations) {
2153 for (Conversation current : this.conversations) {
2154 if (current == conversation) {
2155 return true;
2156 }
2157 }
2158 }
2159 return false;
2160 }
2161
2162 public Conversation findOrCreateConversation(Account account, Jid jid, boolean muc, final boolean async) {
2163 return this.findOrCreateConversation(account, jid, muc, false, async);
2164 }
2165
2166 public Conversation findOrCreateConversation(final Account account, final Jid jid, final boolean muc, final boolean joinAfterCreate, final boolean async) {
2167 return this.findOrCreateConversation(account, jid, muc, joinAfterCreate, null, async);
2168 }
2169
2170 public Conversation findOrCreateConversation(final Account account, final Jid jid, final boolean muc, final boolean joinAfterCreate, final MessageArchiveService.Query query, final boolean async) {
2171 synchronized (this.conversations) {
2172 Conversation conversation = find(account, jid);
2173 if (conversation != null) {
2174 return conversation;
2175 }
2176 conversation = databaseBackend.findConversation(account, jid);
2177 final boolean loadMessagesFromDb;
2178 if (conversation != null) {
2179 conversation.setStatus(Conversation.STATUS_AVAILABLE);
2180 conversation.setAccount(account);
2181 if (muc) {
2182 conversation.setMode(Conversation.MODE_MULTI);
2183 conversation.setContactJid(jid);
2184 } else {
2185 conversation.setMode(Conversation.MODE_SINGLE);
2186 conversation.setContactJid(jid.asBareJid());
2187 }
2188 databaseBackend.updateConversation(conversation);
2189 loadMessagesFromDb = conversation.messagesLoaded.compareAndSet(true, false);
2190 } else {
2191 String conversationName;
2192 Contact contact = account.getRoster().getContact(jid);
2193 if (contact != null) {
2194 conversationName = contact.getDisplayName();
2195 } else {
2196 conversationName = jid.getLocal();
2197 }
2198 if (muc) {
2199 conversation = new Conversation(conversationName, account, jid,
2200 Conversation.MODE_MULTI);
2201 } else {
2202 conversation = new Conversation(conversationName, account, jid.asBareJid(),
2203 Conversation.MODE_SINGLE);
2204 }
2205 this.databaseBackend.createConversation(conversation);
2206 loadMessagesFromDb = false;
2207 }
2208 final Conversation c = conversation;
2209 final Runnable runnable = () -> {
2210 if (loadMessagesFromDb) {
2211 c.addAll(0, databaseBackend.getMessages(c, Config.PAGE_SIZE));
2212 updateConversationUi();
2213 c.messagesLoaded.set(true);
2214 }
2215 if (account.getXmppConnection() != null
2216 && !c.getContact().isBlocked()
2217 && account.getXmppConnection().getFeatures().mam()
2218 && !muc) {
2219 if (query == null) {
2220 mMessageArchiveService.query(c);
2221 } else {
2222 if (query.getConversation() == null) {
2223 mMessageArchiveService.query(c, query.getStart(), query.isCatchup());
2224 }
2225 }
2226 }
2227 if (joinAfterCreate) {
2228 joinMuc(c);
2229 }
2230 };
2231 if (async) {
2232 mDatabaseReaderExecutor.execute(runnable);
2233 } else {
2234 runnable.run();
2235 }
2236 this.conversations.add(conversation);
2237 updateConversationUi();
2238 return conversation;
2239 }
2240 }
2241
2242 public void archiveConversation(Conversation conversation) {
2243 archiveConversation(conversation, true);
2244 }
2245
2246 private void archiveConversation(Conversation conversation, final boolean maySynchronizeWithBookmarks) {
2247 getNotificationService().clear(conversation);
2248 conversation.setStatus(Conversation.STATUS_ARCHIVED);
2249 conversation.setNextMessage(null);
2250 synchronized (this.conversations) {
2251 getMessageArchiveService().kill(conversation);
2252 if (conversation.getMode() == Conversation.MODE_MULTI) {
2253 if (conversation.getAccount().getStatus() == Account.State.ONLINE) {
2254 final Bookmark bookmark = conversation.getBookmark();
2255 if (maySynchronizeWithBookmarks && bookmark != null && synchronizeWithBookmarks()) {
2256 if (conversation.getMucOptions().getError() == MucOptions.Error.DESTROYED) {
2257 Account account = bookmark.getAccount();
2258 bookmark.setConversation(null);
2259 deleteBookmark(account, bookmark);
2260 } else if (bookmark.autojoin()) {
2261 bookmark.setAutojoin(false);
2262 createBookmark(bookmark.getAccount(), bookmark);
2263 }
2264 }
2265 }
2266 leaveMuc(conversation);
2267 } else {
2268 if (conversation.getContact().getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
2269 stopPresenceUpdatesTo(conversation.getContact());
2270 }
2271 }
2272 updateConversation(conversation);
2273 this.conversations.remove(conversation);
2274 updateConversationUi();
2275 }
2276 }
2277
2278 public void stopPresenceUpdatesTo(Contact contact) {
2279 Log.d(Config.LOGTAG, "Canceling presence request from " + contact.getJid().toString());
2280 sendPresencePacket(contact.getAccount(), mPresenceGenerator.stopPresenceUpdatesTo(contact));
2281 contact.resetOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
2282 }
2283
2284 public void createAccount(final Account account) {
2285 account.initAccountServices(this);
2286 databaseBackend.createAccount(account);
2287 this.accounts.add(account);
2288 this.reconnectAccountInBackground(account);
2289 updateAccountUi();
2290 syncEnabledAccountSetting();
2291 toggleForegroundService();
2292 }
2293
2294 private void syncEnabledAccountSetting() {
2295 final boolean hasEnabledAccounts = hasEnabledAccounts();
2296 getPreferences().edit().putBoolean(EventReceiver.SETTING_ENABLED_ACCOUNTS, hasEnabledAccounts).apply();
2297 toggleSetProfilePictureActivity(hasEnabledAccounts);
2298 }
2299
2300 private void toggleSetProfilePictureActivity(final boolean enabled) {
2301 try {
2302 final ComponentName name = new ComponentName(this, ChooseAccountForProfilePictureActivity.class);
2303 final int targetState = enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
2304 getPackageManager().setComponentEnabledSetting(name, targetState, PackageManager.DONT_KILL_APP);
2305 } catch (IllegalStateException e) {
2306 Log.d(Config.LOGTAG, "unable to toggle profile picture actvitiy");
2307 }
2308 }
2309
2310 private void provisionAccount(final String address, final String password) {
2311 final Jid jid = Jid.ofEscaped(address);
2312 final Account account = new Account(jid, password);
2313 account.setOption(Account.OPTION_DISABLED, true);
2314 Log.d(Config.LOGTAG, jid.asBareJid().toEscapedString() + ": provisioning account");
2315 createAccount(account);
2316 }
2317
2318 public void createAccountFromKey(final String alias, final OnAccountCreated callback) {
2319 new Thread(() -> {
2320 try {
2321 final X509Certificate[] chain = KeyChain.getCertificateChain(this, alias);
2322 final X509Certificate cert = chain != null && chain.length > 0 ? chain[0] : null;
2323 if (cert == null) {
2324 callback.informUser(R.string.unable_to_parse_certificate);
2325 return;
2326 }
2327 Pair<Jid, String> info = CryptoHelper.extractJidAndName(cert);
2328 if (info == null) {
2329 callback.informUser(R.string.certificate_does_not_contain_jid);
2330 return;
2331 }
2332 if (findAccountByJid(info.first) == null) {
2333 final Account account = new Account(info.first, "");
2334 account.setPrivateKeyAlias(alias);
2335 account.setOption(Account.OPTION_DISABLED, true);
2336 account.setOption(Account.OPTION_FIXED_USERNAME, true);
2337 account.setDisplayName(info.second);
2338 createAccount(account);
2339 callback.onAccountCreated(account);
2340 if (Config.X509_VERIFICATION) {
2341 try {
2342 getMemorizingTrustManager().getNonInteractive(account.getServer()).checkClientTrusted(chain, "RSA");
2343 } catch (CertificateException e) {
2344 callback.informUser(R.string.certificate_chain_is_not_trusted);
2345 }
2346 }
2347 } else {
2348 callback.informUser(R.string.account_already_exists);
2349 }
2350 } catch (Exception e) {
2351 callback.informUser(R.string.unable_to_parse_certificate);
2352 }
2353 }).start();
2354
2355 }
2356
2357 public void updateKeyInAccount(final Account account, final String alias) {
2358 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": update key in account " + alias);
2359 try {
2360 X509Certificate[] chain = KeyChain.getCertificateChain(XmppConnectionService.this, alias);
2361 Log.d(Config.LOGTAG, account.getJid().asBareJid() + " loaded certificate chain");
2362 Pair<Jid, String> info = CryptoHelper.extractJidAndName(chain[0]);
2363 if (info == null) {
2364 showErrorToastInUi(R.string.certificate_does_not_contain_jid);
2365 return;
2366 }
2367 if (account.getJid().asBareJid().equals(info.first)) {
2368 account.setPrivateKeyAlias(alias);
2369 account.setDisplayName(info.second);
2370 databaseBackend.updateAccount(account);
2371 if (Config.X509_VERIFICATION) {
2372 try {
2373 getMemorizingTrustManager().getNonInteractive().checkClientTrusted(chain, "RSA");
2374 } catch (CertificateException e) {
2375 showErrorToastInUi(R.string.certificate_chain_is_not_trusted);
2376 }
2377 account.getAxolotlService().regenerateKeys(true);
2378 }
2379 } else {
2380 showErrorToastInUi(R.string.jid_does_not_match_certificate);
2381 }
2382 } catch (Exception e) {
2383 e.printStackTrace();
2384 }
2385 }
2386
2387 public boolean updateAccount(final Account account) {
2388 if (databaseBackend.updateAccount(account)) {
2389 account.setShowErrorNotification(true);
2390 this.statusListener.onStatusChanged(account);
2391 databaseBackend.updateAccount(account);
2392 reconnectAccountInBackground(account);
2393 updateAccountUi();
2394 getNotificationService().updateErrorNotification();
2395 toggleForegroundService();
2396 syncEnabledAccountSetting();
2397 mChannelDiscoveryService.cleanCache();
2398 return true;
2399 } else {
2400 return false;
2401 }
2402 }
2403
2404 public void updateAccountPasswordOnServer(final Account account, final String newPassword, final OnAccountPasswordChanged callback) {
2405 final IqPacket iq = getIqGenerator().generateSetPassword(account, newPassword);
2406 sendIqPacket(account, iq, (a, packet) -> {
2407 if (packet.getType() == IqPacket.TYPE.RESULT) {
2408 a.setPassword(newPassword);
2409 a.setOption(Account.OPTION_MAGIC_CREATE, false);
2410 databaseBackend.updateAccount(a);
2411 callback.onPasswordChangeSucceeded();
2412 } else {
2413 callback.onPasswordChangeFailed();
2414 }
2415 });
2416 }
2417
2418 public void deleteAccount(final Account account) {
2419 final boolean connected = account.getStatus() == Account.State.ONLINE;
2420 synchronized (this.conversations) {
2421 if (connected) {
2422 account.getAxolotlService().deleteOmemoIdentity();
2423 }
2424 for (final Conversation conversation : conversations) {
2425 if (conversation.getAccount() == account) {
2426 if (conversation.getMode() == Conversation.MODE_MULTI) {
2427 if (connected) {
2428 leaveMuc(conversation);
2429 }
2430 }
2431 conversations.remove(conversation);
2432 mNotificationService.clear(conversation);
2433 }
2434 }
2435 if (account.getXmppConnection() != null) {
2436 new Thread(() -> disconnect(account, !connected)).start();
2437 }
2438 final Runnable runnable = () -> {
2439 if (!databaseBackend.deleteAccount(account)) {
2440 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to delete account");
2441 }
2442 };
2443 mDatabaseWriterExecutor.execute(runnable);
2444 this.accounts.remove(account);
2445 this.mRosterSyncTaskManager.clear(account);
2446 updateAccountUi();
2447 mNotificationService.updateErrorNotification();
2448 syncEnabledAccountSetting();
2449 toggleForegroundService();
2450 }
2451 }
2452
2453 public void setOnConversationListChangedListener(OnConversationUpdate listener) {
2454 final boolean remainingListeners;
2455 synchronized (LISTENER_LOCK) {
2456 remainingListeners = checkListeners();
2457 if (!this.mOnConversationUpdates.add(listener)) {
2458 Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as ConversationListChangedListener");
2459 }
2460 this.mNotificationService.setIsInForeground(this.mOnConversationUpdates.size() > 0);
2461 }
2462 if (remainingListeners) {
2463 switchToForeground();
2464 }
2465 }
2466
2467 public void removeOnConversationListChangedListener(OnConversationUpdate listener) {
2468 final boolean remainingListeners;
2469 synchronized (LISTENER_LOCK) {
2470 this.mOnConversationUpdates.remove(listener);
2471 this.mNotificationService.setIsInForeground(this.mOnConversationUpdates.size() > 0);
2472 remainingListeners = checkListeners();
2473 }
2474 if (remainingListeners) {
2475 switchToBackground();
2476 }
2477 }
2478
2479 public void setOnShowErrorToastListener(OnShowErrorToast listener) {
2480 final boolean remainingListeners;
2481 synchronized (LISTENER_LOCK) {
2482 remainingListeners = checkListeners();
2483 if (!this.mOnShowErrorToasts.add(listener)) {
2484 Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnShowErrorToastListener");
2485 }
2486 }
2487 if (remainingListeners) {
2488 switchToForeground();
2489 }
2490 }
2491
2492 public void removeOnShowErrorToastListener(OnShowErrorToast onShowErrorToast) {
2493 final boolean remainingListeners;
2494 synchronized (LISTENER_LOCK) {
2495 this.mOnShowErrorToasts.remove(onShowErrorToast);
2496 remainingListeners = checkListeners();
2497 }
2498 if (remainingListeners) {
2499 switchToBackground();
2500 }
2501 }
2502
2503 public void setOnAccountListChangedListener(OnAccountUpdate listener) {
2504 final boolean remainingListeners;
2505 synchronized (LISTENER_LOCK) {
2506 remainingListeners = checkListeners();
2507 if (!this.mOnAccountUpdates.add(listener)) {
2508 Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnAccountListChangedtListener");
2509 }
2510 }
2511 if (remainingListeners) {
2512 switchToForeground();
2513 }
2514 }
2515
2516 public void removeOnAccountListChangedListener(OnAccountUpdate listener) {
2517 final boolean remainingListeners;
2518 synchronized (LISTENER_LOCK) {
2519 this.mOnAccountUpdates.remove(listener);
2520 remainingListeners = checkListeners();
2521 }
2522 if (remainingListeners) {
2523 switchToBackground();
2524 }
2525 }
2526
2527 public void setOnCaptchaRequestedListener(OnCaptchaRequested listener) {
2528 final boolean remainingListeners;
2529 synchronized (LISTENER_LOCK) {
2530 remainingListeners = checkListeners();
2531 if (!this.mOnCaptchaRequested.add(listener)) {
2532 Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnCaptchaRequestListener");
2533 }
2534 }
2535 if (remainingListeners) {
2536 switchToForeground();
2537 }
2538 }
2539
2540 public void removeOnCaptchaRequestedListener(OnCaptchaRequested listener) {
2541 final boolean remainingListeners;
2542 synchronized (LISTENER_LOCK) {
2543 this.mOnCaptchaRequested.remove(listener);
2544 remainingListeners = checkListeners();
2545 }
2546 if (remainingListeners) {
2547 switchToBackground();
2548 }
2549 }
2550
2551 public void setOnRosterUpdateListener(final OnRosterUpdate listener) {
2552 final boolean remainingListeners;
2553 synchronized (LISTENER_LOCK) {
2554 remainingListeners = checkListeners();
2555 if (!this.mOnRosterUpdates.add(listener)) {
2556 Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnRosterUpdateListener");
2557 }
2558 }
2559 if (remainingListeners) {
2560 switchToForeground();
2561 }
2562 }
2563
2564 public void removeOnRosterUpdateListener(final OnRosterUpdate listener) {
2565 final boolean remainingListeners;
2566 synchronized (LISTENER_LOCK) {
2567 this.mOnRosterUpdates.remove(listener);
2568 remainingListeners = checkListeners();
2569 }
2570 if (remainingListeners) {
2571 switchToBackground();
2572 }
2573 }
2574
2575 public void setOnUpdateBlocklistListener(final OnUpdateBlocklist listener) {
2576 final boolean remainingListeners;
2577 synchronized (LISTENER_LOCK) {
2578 remainingListeners = checkListeners();
2579 if (!this.mOnUpdateBlocklist.add(listener)) {
2580 Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnUpdateBlocklistListener");
2581 }
2582 }
2583 if (remainingListeners) {
2584 switchToForeground();
2585 }
2586 }
2587
2588 public void removeOnUpdateBlocklistListener(final OnUpdateBlocklist listener) {
2589 final boolean remainingListeners;
2590 synchronized (LISTENER_LOCK) {
2591 this.mOnUpdateBlocklist.remove(listener);
2592 remainingListeners = checkListeners();
2593 }
2594 if (remainingListeners) {
2595 switchToBackground();
2596 }
2597 }
2598
2599 public void setOnKeyStatusUpdatedListener(final OnKeyStatusUpdated listener) {
2600 final boolean remainingListeners;
2601 synchronized (LISTENER_LOCK) {
2602 remainingListeners = checkListeners();
2603 if (!this.mOnKeyStatusUpdated.add(listener)) {
2604 Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnKeyStatusUpdateListener");
2605 }
2606 }
2607 if (remainingListeners) {
2608 switchToForeground();
2609 }
2610 }
2611
2612 public void removeOnNewKeysAvailableListener(final OnKeyStatusUpdated listener) {
2613 final boolean remainingListeners;
2614 synchronized (LISTENER_LOCK) {
2615 this.mOnKeyStatusUpdated.remove(listener);
2616 remainingListeners = checkListeners();
2617 }
2618 if (remainingListeners) {
2619 switchToBackground();
2620 }
2621 }
2622
2623 public void setOnRtpConnectionUpdateListener(final OnJingleRtpConnectionUpdate listener) {
2624 final boolean remainingListeners;
2625 synchronized (LISTENER_LOCK) {
2626 remainingListeners = checkListeners();
2627 if (!this.onJingleRtpConnectionUpdate.add(listener)) {
2628 Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnJingleRtpConnectionUpdate");
2629 }
2630 }
2631 if (remainingListeners) {
2632 switchToForeground();
2633 }
2634 }
2635
2636 public void removeRtpConnectionUpdateListener(final OnJingleRtpConnectionUpdate listener) {
2637 final boolean remainingListeners;
2638 synchronized (LISTENER_LOCK) {
2639 this.onJingleRtpConnectionUpdate.remove(listener);
2640 remainingListeners = checkListeners();
2641 }
2642 if (remainingListeners) {
2643 switchToBackground();
2644 }
2645 }
2646
2647 public void setOnMucRosterUpdateListener(OnMucRosterUpdate listener) {
2648 final boolean remainingListeners;
2649 synchronized (LISTENER_LOCK) {
2650 remainingListeners = checkListeners();
2651 if (!this.mOnMucRosterUpdate.add(listener)) {
2652 Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnMucRosterListener");
2653 }
2654 }
2655 if (remainingListeners) {
2656 switchToForeground();
2657 }
2658 }
2659
2660 public void removeOnMucRosterUpdateListener(final OnMucRosterUpdate listener) {
2661 final boolean remainingListeners;
2662 synchronized (LISTENER_LOCK) {
2663 this.mOnMucRosterUpdate.remove(listener);
2664 remainingListeners = checkListeners();
2665 }
2666 if (remainingListeners) {
2667 switchToBackground();
2668 }
2669 }
2670
2671 public boolean checkListeners() {
2672 return (this.mOnAccountUpdates.size() == 0
2673 && this.mOnConversationUpdates.size() == 0
2674 && this.mOnRosterUpdates.size() == 0
2675 && this.mOnCaptchaRequested.size() == 0
2676 && this.mOnMucRosterUpdate.size() == 0
2677 && this.mOnUpdateBlocklist.size() == 0
2678 && this.mOnShowErrorToasts.size() == 0
2679 && this.onJingleRtpConnectionUpdate.size() == 0
2680 && this.mOnKeyStatusUpdated.size() == 0);
2681 }
2682
2683 private void switchToForeground() {
2684 final boolean broadcastLastActivity = broadcastLastActivity();
2685 for (Conversation conversation : getConversations()) {
2686 if (conversation.getMode() == Conversation.MODE_MULTI) {
2687 conversation.getMucOptions().resetChatState();
2688 } else {
2689 conversation.setIncomingChatState(Config.DEFAULT_CHAT_STATE);
2690 }
2691 }
2692 for (Account account : getAccounts()) {
2693 if (account.getStatus() == Account.State.ONLINE) {
2694 account.deactivateGracePeriod();
2695 final XmppConnection connection = account.getXmppConnection();
2696 if (connection != null) {
2697 if (connection.getFeatures().csi()) {
2698 connection.sendActive();
2699 }
2700 if (broadcastLastActivity) {
2701 sendPresence(account, false); //send new presence but don't include idle because we are not
2702 }
2703 }
2704 }
2705 }
2706 Log.d(Config.LOGTAG, "app switched into foreground");
2707 }
2708
2709 private void switchToBackground() {
2710 final boolean broadcastLastActivity = broadcastLastActivity();
2711 if (broadcastLastActivity) {
2712 mLastActivity = System.currentTimeMillis();
2713 final SharedPreferences.Editor editor = getPreferences().edit();
2714 editor.putLong(SETTING_LAST_ACTIVITY_TS, mLastActivity);
2715 editor.apply();
2716 }
2717 for (Account account : getAccounts()) {
2718 if (account.getStatus() == Account.State.ONLINE) {
2719 XmppConnection connection = account.getXmppConnection();
2720 if (connection != null) {
2721 if (broadcastLastActivity) {
2722 sendPresence(account, true);
2723 }
2724 if (connection.getFeatures().csi()) {
2725 connection.sendInactive();
2726 }
2727 }
2728 }
2729 }
2730 this.mNotificationService.setIsInForeground(false);
2731 Log.d(Config.LOGTAG, "app switched into background");
2732 }
2733
2734 private void connectMultiModeConversations(Account account) {
2735 List<Conversation> conversations = getConversations();
2736 for (Conversation conversation : conversations) {
2737 if (conversation.getMode() == Conversation.MODE_MULTI && conversation.getAccount() == account) {
2738 joinMuc(conversation);
2739 }
2740 }
2741 }
2742
2743 public void mucSelfPingAndRejoin(final Conversation conversation) {
2744 final Account account = conversation.getAccount();
2745 synchronized (account.inProgressConferenceJoins) {
2746 if (account.inProgressConferenceJoins.contains(conversation)) {
2747 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": canceling muc self ping because join is already under way");
2748 return;
2749 }
2750 }
2751 synchronized (account.inProgressConferencePings) {
2752 if (!account.inProgressConferencePings.add(conversation)) {
2753 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": canceling muc self ping because ping is already under way");
2754 return;
2755 }
2756 }
2757 final Jid self = conversation.getMucOptions().getSelf().getFullJid();
2758 final IqPacket ping = new IqPacket(IqPacket.TYPE.GET);
2759 ping.setTo(self);
2760 ping.addChild("ping", Namespace.PING);
2761 sendIqPacket(conversation.getAccount(), ping, (a, response) -> {
2762 if (response.getType() == IqPacket.TYPE.ERROR) {
2763 Element error = response.findChild("error");
2764 if (error == null || error.hasChild("service-unavailable") || error.hasChild("feature-not-implemented") || error.hasChild("item-not-found")) {
2765 Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": ping to " + self + " came back as ignorable error");
2766 } else {
2767 Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": ping to " + self + " failed. attempting rejoin");
2768 joinMuc(conversation);
2769 }
2770 } else if (response.getType() == IqPacket.TYPE.RESULT) {
2771 Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": ping to " + self + " came back fine");
2772 }
2773 synchronized (account.inProgressConferencePings) {
2774 account.inProgressConferencePings.remove(conversation);
2775 }
2776 });
2777 }
2778
2779 public void joinMuc(Conversation conversation) {
2780 joinMuc(conversation, null, false);
2781 }
2782
2783 public void joinMuc(Conversation conversation, boolean followedInvite) {
2784 joinMuc(conversation, null, followedInvite);
2785 }
2786
2787 private void joinMuc(Conversation conversation, final OnConferenceJoined onConferenceJoined) {
2788 joinMuc(conversation, onConferenceJoined, false);
2789 }
2790
2791 private void joinMuc(Conversation conversation, final OnConferenceJoined onConferenceJoined, final boolean followedInvite) {
2792 final Account account = conversation.getAccount();
2793 synchronized (account.pendingConferenceJoins) {
2794 account.pendingConferenceJoins.remove(conversation);
2795 }
2796 synchronized (account.pendingConferenceLeaves) {
2797 account.pendingConferenceLeaves.remove(conversation);
2798 }
2799 if (account.getStatus() == Account.State.ONLINE) {
2800 synchronized (account.inProgressConferenceJoins) {
2801 account.inProgressConferenceJoins.add(conversation);
2802 }
2803 if (Config.MUC_LEAVE_BEFORE_JOIN) {
2804 sendPresencePacket(account, mPresenceGenerator.leave(conversation.getMucOptions()));
2805 }
2806 conversation.resetMucOptions();
2807 if (onConferenceJoined != null) {
2808 conversation.getMucOptions().flagNoAutoPushConfiguration();
2809 }
2810 conversation.setHasMessagesLeftOnServer(false);
2811 fetchConferenceConfiguration(conversation, new OnConferenceConfigurationFetched() {
2812
2813 private void join(Conversation conversation) {
2814 Account account = conversation.getAccount();
2815 final MucOptions mucOptions = conversation.getMucOptions();
2816
2817 if (mucOptions.nonanonymous() && !mucOptions.membersOnly() && !conversation.getBooleanAttribute("accept_non_anonymous", false)) {
2818 synchronized (account.inProgressConferenceJoins) {
2819 account.inProgressConferenceJoins.remove(conversation);
2820 }
2821 mucOptions.setError(MucOptions.Error.NON_ANONYMOUS);
2822 updateConversationUi();
2823 if (onConferenceJoined != null) {
2824 onConferenceJoined.onConferenceJoined(conversation);
2825 }
2826 return;
2827 }
2828
2829 final Jid joinJid = mucOptions.getSelf().getFullJid();
2830 Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": joining conversation " + joinJid.toString());
2831 PresencePacket packet = mPresenceGenerator.selfPresence(account, Presence.Status.ONLINE, mucOptions.nonanonymous() || onConferenceJoined != null);
2832 packet.setTo(joinJid);
2833 Element x = packet.addChild("x", "http://jabber.org/protocol/muc");
2834 if (conversation.getMucOptions().getPassword() != null) {
2835 x.addChild("password").setContent(mucOptions.getPassword());
2836 }
2837
2838 if (mucOptions.mamSupport()) {
2839 // Use MAM instead of the limited muc history to get history
2840 x.addChild("history").setAttribute("maxchars", "0");
2841 } else {
2842 // Fallback to muc history
2843 x.addChild("history").setAttribute("since", PresenceGenerator.getTimestamp(conversation.getLastMessageTransmitted().getTimestamp()));
2844 }
2845 sendPresencePacket(account, packet);
2846 if (onConferenceJoined != null) {
2847 onConferenceJoined.onConferenceJoined(conversation);
2848 }
2849 if (!joinJid.equals(conversation.getJid())) {
2850 conversation.setContactJid(joinJid);
2851 databaseBackend.updateConversation(conversation);
2852 }
2853
2854 if (mucOptions.mamSupport()) {
2855 getMessageArchiveService().catchupMUC(conversation);
2856 }
2857 if (mucOptions.isPrivateAndNonAnonymous()) {
2858 fetchConferenceMembers(conversation);
2859
2860 if (followedInvite) {
2861 final Bookmark bookmark = conversation.getBookmark();
2862 if (bookmark != null) {
2863 if (!bookmark.autojoin()) {
2864 bookmark.setAutojoin(true);
2865 createBookmark(account, bookmark);
2866 }
2867 } else {
2868 saveConversationAsBookmark(conversation, null);
2869 }
2870 }
2871 }
2872 synchronized (account.inProgressConferenceJoins) {
2873 account.inProgressConferenceJoins.remove(conversation);
2874 sendUnsentMessages(conversation);
2875 }
2876 }
2877
2878 @Override
2879 public void onConferenceConfigurationFetched(Conversation conversation) {
2880 if (conversation.getStatus() == Conversation.STATUS_ARCHIVED) {
2881 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": conversation (" + conversation.getJid() + ") got archived before IQ result");
2882 return;
2883 }
2884 join(conversation);
2885 }
2886
2887 @Override
2888 public void onFetchFailed(final Conversation conversation, final String errorCondition) {
2889 if (conversation.getStatus() == Conversation.STATUS_ARCHIVED) {
2890 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": conversation (" + conversation.getJid() + ") got archived before IQ result");
2891 return;
2892 }
2893 if ("remote-server-not-found".equals(errorCondition)) {
2894 synchronized (account.inProgressConferenceJoins) {
2895 account.inProgressConferenceJoins.remove(conversation);
2896 }
2897 conversation.getMucOptions().setError(MucOptions.Error.SERVER_NOT_FOUND);
2898 updateConversationUi();
2899 } else {
2900 join(conversation);
2901 fetchConferenceConfiguration(conversation);
2902 }
2903 }
2904 });
2905 updateConversationUi();
2906 } else {
2907 synchronized (account.pendingConferenceJoins) {
2908 account.pendingConferenceJoins.add(conversation);
2909 }
2910 conversation.resetMucOptions();
2911 conversation.setHasMessagesLeftOnServer(false);
2912 updateConversationUi();
2913 }
2914 }
2915
2916 private void fetchConferenceMembers(final Conversation conversation) {
2917 final Account account = conversation.getAccount();
2918 final AxolotlService axolotlService = account.getAxolotlService();
2919 final String[] affiliations = {"member", "admin", "owner"};
2920 OnIqPacketReceived callback = new OnIqPacketReceived() {
2921
2922 private int i = 0;
2923 private boolean success = true;
2924
2925 @Override
2926 public void onIqPacketReceived(Account account, IqPacket packet) {
2927 final boolean omemoEnabled = conversation.getNextEncryption() == Message.ENCRYPTION_AXOLOTL;
2928 Element query = packet.query("http://jabber.org/protocol/muc#admin");
2929 if (packet.getType() == IqPacket.TYPE.RESULT && query != null) {
2930 for (Element child : query.getChildren()) {
2931 if ("item".equals(child.getName())) {
2932 MucOptions.User user = AbstractParser.parseItem(conversation, child);
2933 if (!user.realJidMatchesAccount()) {
2934 boolean isNew = conversation.getMucOptions().updateUser(user);
2935 Contact contact = user.getContact();
2936 if (omemoEnabled
2937 && isNew
2938 && user.getRealJid() != null
2939 && (contact == null || !contact.mutualPresenceSubscription())
2940 && axolotlService.hasEmptyDeviceList(user.getRealJid())) {
2941 axolotlService.fetchDeviceIds(user.getRealJid());
2942 }
2943 }
2944 }
2945 }
2946 } else {
2947 success = false;
2948 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not request affiliation " + affiliations[i] + " in " + conversation.getJid().asBareJid());
2949 }
2950 ++i;
2951 if (i >= affiliations.length) {
2952 List<Jid> members = conversation.getMucOptions().getMembers(true);
2953 if (success) {
2954 List<Jid> cryptoTargets = conversation.getAcceptedCryptoTargets();
2955 boolean changed = false;
2956 for (ListIterator<Jid> iterator = cryptoTargets.listIterator(); iterator.hasNext(); ) {
2957 Jid jid = iterator.next();
2958 if (!members.contains(jid) && !members.contains(jid.getDomain())) {
2959 iterator.remove();
2960 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": removed " + jid + " from crypto targets of " + conversation.getName());
2961 changed = true;
2962 }
2963 }
2964 if (changed) {
2965 conversation.setAcceptedCryptoTargets(cryptoTargets);
2966 updateConversation(conversation);
2967 }
2968 }
2969 getAvatarService().clear(conversation);
2970 updateMucRosterUi();
2971 updateConversationUi();
2972 }
2973 }
2974 };
2975 for (String affiliation : affiliations) {
2976 sendIqPacket(account, mIqGenerator.queryAffiliation(conversation, affiliation), callback);
2977 }
2978 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": fetching members for " + conversation.getName());
2979 }
2980
2981 public void providePasswordForMuc(Conversation conversation, String password) {
2982 if (conversation.getMode() == Conversation.MODE_MULTI) {
2983 conversation.getMucOptions().setPassword(password);
2984 if (conversation.getBookmark() != null) {
2985 final Bookmark bookmark = conversation.getBookmark();
2986 if (synchronizeWithBookmarks()) {
2987 bookmark.setAutojoin(true);
2988 }
2989 createBookmark(conversation.getAccount(), bookmark);
2990 }
2991 updateConversation(conversation);
2992 joinMuc(conversation);
2993 }
2994 }
2995
2996 private boolean hasEnabledAccounts() {
2997 if (this.accounts == null) {
2998 return false;
2999 }
3000 for (Account account : this.accounts) {
3001 if (account.isEnabled()) {
3002 return true;
3003 }
3004 }
3005 return false;
3006 }
3007
3008
3009 public void getAttachments(final Conversation conversation, int limit, final OnMediaLoaded onMediaLoaded) {
3010 getAttachments(conversation.getAccount(), conversation.getJid().asBareJid(), limit, onMediaLoaded);
3011 }
3012
3013 public void getAttachments(final Account account, final Jid jid, final int limit, final OnMediaLoaded onMediaLoaded) {
3014 getAttachments(account.getUuid(), jid.asBareJid(), limit, onMediaLoaded);
3015 }
3016
3017
3018 public void getAttachments(final String account, final Jid jid, final int limit, final OnMediaLoaded onMediaLoaded) {
3019 new Thread(() -> onMediaLoaded.onMediaLoaded(fileBackend.convertToAttachments(databaseBackend.getRelativeFilePaths(account, jid, limit)))).start();
3020 }
3021
3022 public void persistSelfNick(MucOptions.User self) {
3023 final Conversation conversation = self.getConversation();
3024 final boolean tookProposedNickFromBookmark = conversation.getMucOptions().isTookProposedNickFromBookmark();
3025 Jid full = self.getFullJid();
3026 if (!full.equals(conversation.getJid())) {
3027 Log.d(Config.LOGTAG, "nick changed. updating");
3028 conversation.setContactJid(full);
3029 databaseBackend.updateConversation(conversation);
3030 }
3031
3032 final Bookmark bookmark = conversation.getBookmark();
3033 final String bookmarkedNick = bookmark == null ? null : bookmark.getNick();
3034 if (bookmark != null && (tookProposedNickFromBookmark || TextUtils.isEmpty(bookmarkedNick)) && !full.getResource().equals(bookmarkedNick)) {
3035 final Account account = conversation.getAccount();
3036 final String defaultNick = MucOptions.defaultNick(account);
3037 if (TextUtils.isEmpty(bookmarkedNick) && full.getResource().equals(defaultNick)) {
3038 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": do not overwrite empty bookmark nick with default nick for " + conversation.getJid().asBareJid());
3039 return;
3040 }
3041 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": persist nick '" + full.getResource() + "' into bookmark for " + conversation.getJid().asBareJid());
3042 bookmark.setNick(full.getResource());
3043 createBookmark(bookmark.getAccount(), bookmark);
3044 }
3045 }
3046
3047 public boolean renameInMuc(final Conversation conversation, final String nick, final UiCallback<Conversation> callback) {
3048 final MucOptions options = conversation.getMucOptions();
3049 final Jid joinJid = options.createJoinJid(nick);
3050 if (joinJid == null) {
3051 return false;
3052 }
3053 if (options.online()) {
3054 Account account = conversation.getAccount();
3055 options.setOnRenameListener(new OnRenameListener() {
3056
3057 @Override
3058 public void onSuccess() {
3059 callback.success(conversation);
3060 }
3061
3062 @Override
3063 public void onFailure() {
3064 callback.error(R.string.nick_in_use, conversation);
3065 }
3066 });
3067
3068 final PresencePacket packet = mPresenceGenerator.selfPresence(account, Presence.Status.ONLINE, options.nonanonymous());
3069 packet.setTo(joinJid);
3070 sendPresencePacket(account, packet);
3071 } else {
3072 conversation.setContactJid(joinJid);
3073 databaseBackend.updateConversation(conversation);
3074 if (conversation.getAccount().getStatus() == Account.State.ONLINE) {
3075 Bookmark bookmark = conversation.getBookmark();
3076 if (bookmark != null) {
3077 bookmark.setNick(nick);
3078 createBookmark(bookmark.getAccount(), bookmark);
3079 }
3080 joinMuc(conversation);
3081 }
3082 }
3083 return true;
3084 }
3085
3086 public void leaveMuc(Conversation conversation) {
3087 leaveMuc(conversation, false);
3088 }
3089
3090 private void leaveMuc(Conversation conversation, boolean now) {
3091 final Account account = conversation.getAccount();
3092 synchronized (account.pendingConferenceJoins) {
3093 account.pendingConferenceJoins.remove(conversation);
3094 }
3095 synchronized (account.pendingConferenceLeaves) {
3096 account.pendingConferenceLeaves.remove(conversation);
3097 }
3098 if (account.getStatus() == Account.State.ONLINE || now) {
3099 sendPresencePacket(conversation.getAccount(), mPresenceGenerator.leave(conversation.getMucOptions()));
3100 conversation.getMucOptions().setOffline();
3101 Bookmark bookmark = conversation.getBookmark();
3102 if (bookmark != null) {
3103 bookmark.setConversation(null);
3104 }
3105 Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": leaving muc " + conversation.getJid());
3106 } else {
3107 synchronized (account.pendingConferenceLeaves) {
3108 account.pendingConferenceLeaves.add(conversation);
3109 }
3110 }
3111 }
3112
3113 public String findConferenceServer(final Account account) {
3114 String server;
3115 if (account.getXmppConnection() != null) {
3116 server = account.getXmppConnection().getMucServer();
3117 if (server != null) {
3118 return server;
3119 }
3120 }
3121 for (Account other : getAccounts()) {
3122 if (other != account && other.getXmppConnection() != null) {
3123 server = other.getXmppConnection().getMucServer();
3124 if (server != null) {
3125 return server;
3126 }
3127 }
3128 }
3129 return null;
3130 }
3131
3132
3133 public void createPublicChannel(final Account account, final String name, final Jid address, final UiCallback<Conversation> callback) {
3134 joinMuc(findOrCreateConversation(account, address, true, false, true), conversation -> {
3135 final Bundle configuration = IqGenerator.defaultChannelConfiguration();
3136 if (!TextUtils.isEmpty(name)) {
3137 configuration.putString("muc#roomconfig_roomname", name);
3138 }
3139 pushConferenceConfiguration(conversation, configuration, new OnConfigurationPushed() {
3140 @Override
3141 public void onPushSucceeded() {
3142 saveConversationAsBookmark(conversation, name);
3143 callback.success(conversation);
3144 }
3145
3146 @Override
3147 public void onPushFailed() {
3148 if (conversation.getMucOptions().getSelf().getAffiliation().ranks(MucOptions.Affiliation.OWNER)) {
3149 callback.error(R.string.unable_to_set_channel_configuration, conversation);
3150 } else {
3151 callback.error(R.string.joined_an_existing_channel, conversation);
3152 }
3153 }
3154 });
3155 });
3156 }
3157
3158 public boolean createAdhocConference(final Account account,
3159 final String name,
3160 final Iterable<Jid> jids,
3161 final UiCallback<Conversation> callback) {
3162 Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": creating adhoc conference with " + jids.toString());
3163 if (account.getStatus() == Account.State.ONLINE) {
3164 try {
3165 String server = findConferenceServer(account);
3166 if (server == null) {
3167 if (callback != null) {
3168 callback.error(R.string.no_conference_server_found, null);
3169 }
3170 return false;
3171 }
3172 final Jid jid = Jid.of(CryptoHelper.pronounceable(getRNG()), server, null);
3173 final Conversation conversation = findOrCreateConversation(account, jid, true, false, true);
3174 joinMuc(conversation, new OnConferenceJoined() {
3175 @Override
3176 public void onConferenceJoined(final Conversation conversation) {
3177 final Bundle configuration = IqGenerator.defaultGroupChatConfiguration();
3178 if (!TextUtils.isEmpty(name)) {
3179 configuration.putString("muc#roomconfig_roomname", name);
3180 }
3181 pushConferenceConfiguration(conversation, configuration, new OnConfigurationPushed() {
3182 @Override
3183 public void onPushSucceeded() {
3184 for (Jid invite : jids) {
3185 invite(conversation, invite);
3186 }
3187 for (String resource : account.getSelfContact().getPresences().toResourceArray()) {
3188 Jid other = account.getJid().withResource(resource);
3189 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": sending direct invite to " + other);
3190 directInvite(conversation, other);
3191 }
3192 saveConversationAsBookmark(conversation, name);
3193 if (callback != null) {
3194 callback.success(conversation);
3195 }
3196 }
3197
3198 @Override
3199 public void onPushFailed() {
3200 archiveConversation(conversation);
3201 if (callback != null) {
3202 callback.error(R.string.conference_creation_failed, conversation);
3203 }
3204 }
3205 });
3206 }
3207 });
3208 return true;
3209 } catch (IllegalArgumentException e) {
3210 if (callback != null) {
3211 callback.error(R.string.conference_creation_failed, null);
3212 }
3213 return false;
3214 }
3215 } else {
3216 if (callback != null) {
3217 callback.error(R.string.not_connected_try_again, null);
3218 }
3219 return false;
3220 }
3221 }
3222
3223 public void fetchConferenceConfiguration(final Conversation conversation) {
3224 fetchConferenceConfiguration(conversation, null);
3225 }
3226
3227 public void fetchConferenceConfiguration(final Conversation conversation, final OnConferenceConfigurationFetched callback) {
3228 IqPacket request = mIqGenerator.queryDiscoInfo(conversation.getJid().asBareJid());
3229 sendIqPacket(conversation.getAccount(), request, new OnIqPacketReceived() {
3230 @Override
3231 public void onIqPacketReceived(Account account, IqPacket packet) {
3232 if (packet.getType() == IqPacket.TYPE.RESULT) {
3233 final MucOptions mucOptions = conversation.getMucOptions();
3234 final Bookmark bookmark = conversation.getBookmark();
3235 final boolean sameBefore = StringUtils.equals(bookmark == null ? null : bookmark.getBookmarkName(), mucOptions.getName());
3236
3237 if (mucOptions.updateConfiguration(new ServiceDiscoveryResult(packet))) {
3238 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": muc configuration changed for " + conversation.getJid().asBareJid());
3239 updateConversation(conversation);
3240 }
3241
3242 if (bookmark != null && (sameBefore || bookmark.getBookmarkName() == null)) {
3243 if (bookmark.setBookmarkName(StringUtils.nullOnEmpty(mucOptions.getName()))) {
3244 createBookmark(account, bookmark);
3245 }
3246 }
3247
3248
3249 if (callback != null) {
3250 callback.onConferenceConfigurationFetched(conversation);
3251 }
3252
3253
3254 updateConversationUi();
3255 } else if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
3256 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received timeout waiting for conference configuration fetch");
3257 } else {
3258 if (callback != null) {
3259 callback.onFetchFailed(conversation, packet.getErrorCondition());
3260 }
3261 }
3262 }
3263 });
3264 }
3265
3266 public void pushNodeConfiguration(Account account, final String node, final Bundle options, final OnConfigurationPushed callback) {
3267 pushNodeConfiguration(account, account.getJid().asBareJid(), node, options, callback);
3268 }
3269
3270 public void pushNodeConfiguration(Account account, final Jid jid, final String node, final Bundle options, final OnConfigurationPushed callback) {
3271 Log.d(Config.LOGTAG, "pushing node configuration");
3272 sendIqPacket(account, mIqGenerator.requestPubsubConfiguration(jid, node), new OnIqPacketReceived() {
3273 @Override
3274 public void onIqPacketReceived(Account account, IqPacket packet) {
3275 if (packet.getType() == IqPacket.TYPE.RESULT) {
3276 Element pubsub = packet.findChild("pubsub", "http://jabber.org/protocol/pubsub#owner");
3277 Element configuration = pubsub == null ? null : pubsub.findChild("configure");
3278 Element x = configuration == null ? null : configuration.findChild("x", Namespace.DATA);
3279 if (x != null) {
3280 Data data = Data.parse(x);
3281 data.submit(options);
3282 sendIqPacket(account, mIqGenerator.publishPubsubConfiguration(jid, node, data), new OnIqPacketReceived() {
3283 @Override
3284 public void onIqPacketReceived(Account account, IqPacket packet) {
3285 if (packet.getType() == IqPacket.TYPE.RESULT && callback != null) {
3286 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": successfully changed node configuration for node " + node);
3287 callback.onPushSucceeded();
3288 } else if (packet.getType() == IqPacket.TYPE.ERROR && callback != null) {
3289 callback.onPushFailed();
3290 }
3291 }
3292 });
3293 } else if (callback != null) {
3294 callback.onPushFailed();
3295 }
3296 } else if (packet.getType() == IqPacket.TYPE.ERROR && callback != null) {
3297 callback.onPushFailed();
3298 }
3299 }
3300 });
3301 }
3302
3303 public void pushConferenceConfiguration(final Conversation conversation, final Bundle options, final OnConfigurationPushed callback) {
3304 if (options.getString("muc#roomconfig_whois", "moderators").equals("anyone")) {
3305 conversation.setAttribute("accept_non_anonymous", true);
3306 updateConversation(conversation);
3307 }
3308 if (options.containsKey("muc#roomconfig_moderatedroom")) {
3309 final boolean moderated = "1".equals(options.getString("muc#roomconfig_moderatedroom"));
3310 options.putString("members_by_default", moderated ? "0" : "1");
3311 }
3312 final IqPacket request = new IqPacket(IqPacket.TYPE.GET);
3313 request.setTo(conversation.getJid().asBareJid());
3314 request.query("http://jabber.org/protocol/muc#owner");
3315 sendIqPacket(conversation.getAccount(), request, new OnIqPacketReceived() {
3316 @Override
3317 public void onIqPacketReceived(Account account, IqPacket packet) {
3318 if (packet.getType() == IqPacket.TYPE.RESULT) {
3319 final Data data = Data.parse(packet.query().findChild("x", Namespace.DATA));
3320 data.submit(options);
3321 final IqPacket set = new IqPacket(IqPacket.TYPE.SET);
3322 set.setTo(conversation.getJid().asBareJid());
3323 set.query("http://jabber.org/protocol/muc#owner").addChild(data);
3324 sendIqPacket(account, set, new OnIqPacketReceived() {
3325 @Override
3326 public void onIqPacketReceived(Account account, IqPacket packet) {
3327 if (callback != null) {
3328 if (packet.getType() == IqPacket.TYPE.RESULT) {
3329 callback.onPushSucceeded();
3330 } else {
3331 callback.onPushFailed();
3332 }
3333 }
3334 }
3335 });
3336 } else {
3337 if (callback != null) {
3338 callback.onPushFailed();
3339 }
3340 }
3341 }
3342 });
3343 }
3344
3345 public void pushSubjectToConference(final Conversation conference, final String subject) {
3346 MessagePacket packet = this.getMessageGenerator().conferenceSubject(conference, StringUtils.nullOnEmpty(subject));
3347 this.sendMessagePacket(conference.getAccount(), packet);
3348 }
3349
3350 public void changeAffiliationInConference(final Conversation conference, Jid user, final MucOptions.Affiliation affiliation, final OnAffiliationChanged callback) {
3351 final Jid jid = user.asBareJid();
3352 final IqPacket request = this.mIqGenerator.changeAffiliation(conference, jid, affiliation.toString());
3353 sendIqPacket(conference.getAccount(), request, (account, response) -> {
3354 if (response.getType() == IqPacket.TYPE.RESULT) {
3355 conference.getMucOptions().changeAffiliation(jid, affiliation);
3356 getAvatarService().clear(conference);
3357 if (callback != null) {
3358 callback.onAffiliationChangedSuccessful(jid);
3359 } else {
3360 Log.d(Config.LOGTAG, "changed affiliation of " + user + " to " + affiliation);
3361 }
3362 } else if (callback != null) {
3363 callback.onAffiliationChangeFailed(jid, R.string.could_not_change_affiliation);
3364 } else {
3365 Log.d(Config.LOGTAG, "unable to change affiliation");
3366 }
3367 });
3368 }
3369
3370 public void changeRoleInConference(final Conversation conference, final String nick, MucOptions.Role role) {
3371 IqPacket request = this.mIqGenerator.changeRole(conference, nick, role.toString());
3372 sendIqPacket(conference.getAccount(), request, (account, packet) -> {
3373 if (packet.getType() != IqPacket.TYPE.RESULT) {
3374 Log.d(Config.LOGTAG, account.getJid().asBareJid() + " unable to change role of " + nick);
3375 }
3376 });
3377 }
3378
3379 public void destroyRoom(final Conversation conversation, final OnRoomDestroy callback) {
3380 IqPacket request = new IqPacket(IqPacket.TYPE.SET);
3381 request.setTo(conversation.getJid().asBareJid());
3382 request.query("http://jabber.org/protocol/muc#owner").addChild("destroy");
3383 sendIqPacket(conversation.getAccount(), request, new OnIqPacketReceived() {
3384 @Override
3385 public void onIqPacketReceived(Account account, IqPacket packet) {
3386 if (packet.getType() == IqPacket.TYPE.RESULT) {
3387 if (callback != null) {
3388 callback.onRoomDestroySucceeded();
3389 }
3390 } else if (packet.getType() == IqPacket.TYPE.ERROR) {
3391 if (callback != null) {
3392 callback.onRoomDestroyFailed();
3393 }
3394 }
3395 }
3396 });
3397 }
3398
3399 private void disconnect(Account account, boolean force) {
3400 if ((account.getStatus() == Account.State.ONLINE)
3401 || (account.getStatus() == Account.State.DISABLED)) {
3402 final XmppConnection connection = account.getXmppConnection();
3403 if (!force) {
3404 List<Conversation> conversations = getConversations();
3405 for (Conversation conversation : conversations) {
3406 if (conversation.getAccount() == account) {
3407 if (conversation.getMode() == Conversation.MODE_MULTI) {
3408 leaveMuc(conversation, true);
3409 }
3410 }
3411 }
3412 sendOfflinePresence(account);
3413 }
3414 connection.disconnect(force);
3415 }
3416 }
3417
3418 @Override
3419 public IBinder onBind(Intent intent) {
3420 return mBinder;
3421 }
3422
3423 public void updateMessage(Message message) {
3424 updateMessage(message, true);
3425 }
3426
3427 public void updateMessage(Message message, boolean includeBody) {
3428 databaseBackend.updateMessage(message, includeBody);
3429 updateConversationUi();
3430 }
3431
3432 public void createMessageAsync(final Message message) {
3433 mDatabaseWriterExecutor.execute(() -> databaseBackend.createMessage(message));
3434 }
3435
3436 public void updateMessage(Message message, String uuid) {
3437 if (!databaseBackend.updateMessage(message, uuid)) {
3438 Log.e(Config.LOGTAG, "error updated message in DB after edit");
3439 }
3440 updateConversationUi();
3441 }
3442
3443 protected void syncDirtyContacts(Account account) {
3444 for (Contact contact : account.getRoster().getContacts()) {
3445 if (contact.getOption(Contact.Options.DIRTY_PUSH)) {
3446 pushContactToServer(contact);
3447 }
3448 if (contact.getOption(Contact.Options.DIRTY_DELETE)) {
3449 deleteContactOnServer(contact);
3450 }
3451 }
3452 }
3453
3454 public void createContact(final Contact contact, final boolean autoGrant) {
3455 createContact(contact, autoGrant, null);
3456 }
3457
3458 public void createContact(final Contact contact, final boolean autoGrant, final String preAuth) {
3459 if (autoGrant) {
3460 contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
3461 contact.setOption(Contact.Options.ASKING);
3462 }
3463 pushContactToServer(contact, preAuth);
3464 }
3465
3466 public void pushContactToServer(final Contact contact) {
3467 pushContactToServer(contact, null);
3468 }
3469
3470 private void pushContactToServer(final Contact contact, final String preAuth) {
3471 contact.resetOption(Contact.Options.DIRTY_DELETE);
3472 contact.setOption(Contact.Options.DIRTY_PUSH);
3473 final Account account = contact.getAccount();
3474 if (account.getStatus() == Account.State.ONLINE) {
3475 final boolean ask = contact.getOption(Contact.Options.ASKING);
3476 final boolean sendUpdates = contact
3477 .getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)
3478 && contact.getOption(Contact.Options.PREEMPTIVE_GRANT);
3479 final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
3480 iq.query(Namespace.ROSTER).addChild(contact.asElement());
3481 account.getXmppConnection().sendIqPacket(iq, mDefaultIqHandler);
3482 if (sendUpdates) {
3483 sendPresencePacket(account, mPresenceGenerator.sendPresenceUpdatesTo(contact));
3484 }
3485 if (ask) {
3486 sendPresencePacket(account, mPresenceGenerator.requestPresenceUpdatesFrom(contact, preAuth));
3487 }
3488 } else {
3489 syncRoster(contact.getAccount());
3490 }
3491 }
3492
3493 public void publishMucAvatar(final Conversation conversation, final Uri image, final OnAvatarPublication callback) {
3494 new Thread(() -> {
3495 final Bitmap.CompressFormat format = Config.AVATAR_FORMAT;
3496 final int size = Config.AVATAR_SIZE;
3497 final Avatar avatar = getFileBackend().getPepAvatar(image, size, format);
3498 if (avatar != null) {
3499 if (!getFileBackend().save(avatar)) {
3500 callback.onAvatarPublicationFailed(R.string.error_saving_avatar);
3501 return;
3502 }
3503 avatar.owner = conversation.getJid().asBareJid();
3504 publishMucAvatar(conversation, avatar, callback);
3505 } else {
3506 callback.onAvatarPublicationFailed(R.string.error_publish_avatar_converting);
3507 }
3508 }).start();
3509 }
3510
3511 public void publishAvatar(final Account account, final Uri image, final OnAvatarPublication callback) {
3512 new Thread(() -> {
3513 final Bitmap.CompressFormat format = Config.AVATAR_FORMAT;
3514 final int size = Config.AVATAR_SIZE;
3515 final Avatar avatar = getFileBackend().getPepAvatar(image, size, format);
3516 if (avatar != null) {
3517 if (!getFileBackend().save(avatar)) {
3518 Log.d(Config.LOGTAG, "unable to save vcard");
3519 callback.onAvatarPublicationFailed(R.string.error_saving_avatar);
3520 return;
3521 }
3522 publishAvatar(account, avatar, callback);
3523 } else {
3524 callback.onAvatarPublicationFailed(R.string.error_publish_avatar_converting);
3525 }
3526 }).start();
3527
3528 }
3529
3530 private void publishMucAvatar(Conversation conversation, Avatar avatar, OnAvatarPublication callback) {
3531 final IqPacket retrieve = mIqGenerator.retrieveVcardAvatar(avatar);
3532 sendIqPacket(conversation.getAccount(), retrieve, (account, response) -> {
3533 boolean itemNotFound = response.getType() == IqPacket.TYPE.ERROR && response.hasChild("error") && response.findChild("error").hasChild("item-not-found");
3534 if (response.getType() == IqPacket.TYPE.RESULT || itemNotFound) {
3535 Element vcard = response.findChild("vCard", "vcard-temp");
3536 if (vcard == null) {
3537 vcard = new Element("vCard", "vcard-temp");
3538 }
3539 Element photo = vcard.findChild("PHOTO");
3540 if (photo == null) {
3541 photo = vcard.addChild("PHOTO");
3542 }
3543 photo.clearChildren();
3544 photo.addChild("TYPE").setContent(avatar.type);
3545 photo.addChild("BINVAL").setContent(avatar.image);
3546 IqPacket publication = new IqPacket(IqPacket.TYPE.SET);
3547 publication.setTo(conversation.getJid().asBareJid());
3548 publication.addChild(vcard);
3549 sendIqPacket(account, publication, (a1, publicationResponse) -> {
3550 if (publicationResponse.getType() == IqPacket.TYPE.RESULT) {
3551 callback.onAvatarPublicationSucceeded();
3552 } else {
3553 Log.d(Config.LOGTAG, "failed to publish vcard " + publicationResponse.getErrorCondition());
3554 callback.onAvatarPublicationFailed(R.string.error_publish_avatar_server_reject);
3555 }
3556 });
3557 } else {
3558 Log.d(Config.LOGTAG, "failed to request vcard " + response.toString());
3559 callback.onAvatarPublicationFailed(R.string.error_publish_avatar_no_server_support);
3560 }
3561 });
3562 }
3563
3564 public void publishAvatar(Account account, final Avatar avatar, final OnAvatarPublication callback) {
3565 final Bundle options;
3566 if (account.getXmppConnection().getFeatures().pepPublishOptions()) {
3567 options = PublishOptions.openAccess();
3568 } else {
3569 options = null;
3570 }
3571 publishAvatar(account, avatar, options, true, callback);
3572 }
3573
3574 public void publishAvatar(Account account, final Avatar avatar, final Bundle options, final boolean retry, final OnAvatarPublication callback) {
3575 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": publishing avatar. options=" + options);
3576 IqPacket packet = this.mIqGenerator.publishAvatar(avatar, options);
3577 this.sendIqPacket(account, packet, new OnIqPacketReceived() {
3578
3579 @Override
3580 public void onIqPacketReceived(Account account, IqPacket result) {
3581 if (result.getType() == IqPacket.TYPE.RESULT) {
3582 publishAvatarMetadata(account, avatar, options, true, callback);
3583 } else if (retry && PublishOptions.preconditionNotMet(result)) {
3584 pushNodeConfiguration(account, "urn:xmpp:avatar:data", options, new OnConfigurationPushed() {
3585 @Override
3586 public void onPushSucceeded() {
3587 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": changed node configuration for avatar node");
3588 publishAvatar(account, avatar, options, false, callback);
3589 }
3590
3591 @Override
3592 public void onPushFailed() {
3593 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to change node configuration for avatar node");
3594 publishAvatar(account, avatar, null, false, callback);
3595 }
3596 });
3597 } else {
3598 Element error = result.findChild("error");
3599 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server rejected avatar " + (avatar.size / 1024) + "KiB " + (error != null ? error.toString() : ""));
3600 if (callback != null) {
3601 callback.onAvatarPublicationFailed(R.string.error_publish_avatar_server_reject);
3602 }
3603 }
3604 }
3605 });
3606 }
3607
3608 public void publishAvatarMetadata(Account account, final Avatar avatar, final Bundle options, final boolean retry, final OnAvatarPublication callback) {
3609 final IqPacket packet = XmppConnectionService.this.mIqGenerator.publishAvatarMetadata(avatar, options);
3610 sendIqPacket(account, packet, new OnIqPacketReceived() {
3611 @Override
3612 public void onIqPacketReceived(Account account, IqPacket result) {
3613 if (result.getType() == IqPacket.TYPE.RESULT) {
3614 if (account.setAvatar(avatar.getFilename())) {
3615 getAvatarService().clear(account);
3616 databaseBackend.updateAccount(account);
3617 notifyAccountAvatarHasChanged(account);
3618 }
3619 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": published avatar " + (avatar.size / 1024) + "KiB");
3620 if (callback != null) {
3621 callback.onAvatarPublicationSucceeded();
3622 }
3623 } else if (retry && PublishOptions.preconditionNotMet(result)) {
3624 pushNodeConfiguration(account, "urn:xmpp:avatar:metadata", options, new OnConfigurationPushed() {
3625 @Override
3626 public void onPushSucceeded() {
3627 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": changed node configuration for avatar meta data node");
3628 publishAvatarMetadata(account, avatar, options, false, callback);
3629 }
3630
3631 @Override
3632 public void onPushFailed() {
3633 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to change node configuration for avatar meta data node");
3634 publishAvatarMetadata(account, avatar, null, false, callback);
3635 }
3636 });
3637 } else {
3638 if (callback != null) {
3639 callback.onAvatarPublicationFailed(R.string.error_publish_avatar_server_reject);
3640 }
3641 }
3642 }
3643 });
3644 }
3645
3646 public void republishAvatarIfNeeded(Account account) {
3647 if (account.getAxolotlService().isPepBroken()) {
3648 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": skipping republication of avatar because pep is broken");
3649 return;
3650 }
3651 IqPacket packet = this.mIqGenerator.retrieveAvatarMetaData(null);
3652 this.sendIqPacket(account, packet, new OnIqPacketReceived() {
3653
3654 private Avatar parseAvatar(IqPacket packet) {
3655 Element pubsub = packet.findChild("pubsub", "http://jabber.org/protocol/pubsub");
3656 if (pubsub != null) {
3657 Element items = pubsub.findChild("items");
3658 if (items != null) {
3659 return Avatar.parseMetadata(items);
3660 }
3661 }
3662 return null;
3663 }
3664
3665 private boolean errorIsItemNotFound(IqPacket packet) {
3666 Element error = packet.findChild("error");
3667 return packet.getType() == IqPacket.TYPE.ERROR
3668 && error != null
3669 && error.hasChild("item-not-found");
3670 }
3671
3672 @Override
3673 public void onIqPacketReceived(Account account, IqPacket packet) {
3674 if (packet.getType() == IqPacket.TYPE.RESULT || errorIsItemNotFound(packet)) {
3675 Avatar serverAvatar = parseAvatar(packet);
3676 if (serverAvatar == null && account.getAvatar() != null) {
3677 Avatar avatar = fileBackend.getStoredPepAvatar(account.getAvatar());
3678 if (avatar != null) {
3679 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": avatar on server was null. republishing");
3680 publishAvatar(account, fileBackend.getStoredPepAvatar(account.getAvatar()), null);
3681 } else {
3682 Log.e(Config.LOGTAG, account.getJid().asBareJid() + ": error rereading avatar");
3683 }
3684 }
3685 }
3686 }
3687 });
3688 }
3689
3690 public void fetchAvatar(Account account, Avatar avatar) {
3691 fetchAvatar(account, avatar, null);
3692 }
3693
3694 public void fetchAvatar(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
3695 final String KEY = generateFetchKey(account, avatar);
3696 synchronized (this.mInProgressAvatarFetches) {
3697 if (mInProgressAvatarFetches.add(KEY)) {
3698 switch (avatar.origin) {
3699 case PEP:
3700 this.mInProgressAvatarFetches.add(KEY);
3701 fetchAvatarPep(account, avatar, callback);
3702 break;
3703 case VCARD:
3704 this.mInProgressAvatarFetches.add(KEY);
3705 fetchAvatarVcard(account, avatar, callback);
3706 break;
3707 }
3708 } else if (avatar.origin == Avatar.Origin.PEP) {
3709 mOmittedPepAvatarFetches.add(KEY);
3710 } else {
3711 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": already fetching " + avatar.origin + " avatar for " + avatar.owner);
3712 }
3713 }
3714 }
3715
3716 private void fetchAvatarPep(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
3717 IqPacket packet = this.mIqGenerator.retrievePepAvatar(avatar);
3718 sendIqPacket(account, packet, (a, result) -> {
3719 synchronized (mInProgressAvatarFetches) {
3720 mInProgressAvatarFetches.remove(generateFetchKey(a, avatar));
3721 }
3722 final String ERROR = a.getJid().asBareJid() + ": fetching avatar for " + avatar.owner + " failed ";
3723 if (result.getType() == IqPacket.TYPE.RESULT) {
3724 avatar.image = mIqParser.avatarData(result);
3725 if (avatar.image != null) {
3726 if (getFileBackend().save(avatar)) {
3727 if (a.getJid().asBareJid().equals(avatar.owner)) {
3728 if (a.setAvatar(avatar.getFilename())) {
3729 databaseBackend.updateAccount(a);
3730 }
3731 getAvatarService().clear(a);
3732 updateConversationUi();
3733 updateAccountUi();
3734 } else {
3735 final Contact contact = a.getRoster().getContact(avatar.owner);
3736 contact.setAvatar(avatar);
3737 syncRoster(account);
3738 getAvatarService().clear(contact);
3739 updateConversationUi();
3740 updateRosterUi();
3741 }
3742 if (callback != null) {
3743 callback.success(avatar);
3744 }
3745 Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": successfully fetched pep avatar for " + avatar.owner);
3746 return;
3747 }
3748 } else {
3749
3750 Log.d(Config.LOGTAG, ERROR + "(parsing error)");
3751 }
3752 } else {
3753 Element error = result.findChild("error");
3754 if (error == null) {
3755 Log.d(Config.LOGTAG, ERROR + "(server error)");
3756 } else {
3757 Log.d(Config.LOGTAG, ERROR + error.toString());
3758 }
3759 }
3760 if (callback != null) {
3761 callback.error(0, null);
3762 }
3763
3764 });
3765 }
3766
3767 private void fetchAvatarVcard(final Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
3768 IqPacket packet = this.mIqGenerator.retrieveVcardAvatar(avatar);
3769 this.sendIqPacket(account, packet, new OnIqPacketReceived() {
3770 @Override
3771 public void onIqPacketReceived(Account account, IqPacket packet) {
3772 final boolean previouslyOmittedPepFetch;
3773 synchronized (mInProgressAvatarFetches) {
3774 final String KEY = generateFetchKey(account, avatar);
3775 mInProgressAvatarFetches.remove(KEY);
3776 previouslyOmittedPepFetch = mOmittedPepAvatarFetches.remove(KEY);
3777 }
3778 if (packet.getType() == IqPacket.TYPE.RESULT) {
3779 Element vCard = packet.findChild("vCard", "vcard-temp");
3780 Element photo = vCard != null ? vCard.findChild("PHOTO") : null;
3781 String image = photo != null ? photo.findChildContent("BINVAL") : null;
3782 if (image != null) {
3783 avatar.image = image;
3784 if (getFileBackend().save(avatar)) {
3785 Log.d(Config.LOGTAG, account.getJid().asBareJid()
3786 + ": successfully fetched vCard avatar for " + avatar.owner + " omittedPep=" + previouslyOmittedPepFetch);
3787 if (avatar.owner.isBareJid()) {
3788 if (account.getJid().asBareJid().equals(avatar.owner) && account.getAvatar() == null) {
3789 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": had no avatar. replacing with vcard");
3790 account.setAvatar(avatar.getFilename());
3791 databaseBackend.updateAccount(account);
3792 getAvatarService().clear(account);
3793 updateAccountUi();
3794 } else {
3795 final Contact contact = account.getRoster().getContact(avatar.owner);
3796 contact.setAvatar(avatar, previouslyOmittedPepFetch);
3797 syncRoster(account);
3798 getAvatarService().clear(contact);
3799 updateRosterUi();
3800 }
3801 updateConversationUi();
3802 } else {
3803 Conversation conversation = find(account, avatar.owner.asBareJid());
3804 if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
3805 MucOptions.User user = conversation.getMucOptions().findUserByFullJid(avatar.owner);
3806 if (user != null) {
3807 if (user.setAvatar(avatar)) {
3808 getAvatarService().clear(user);
3809 updateConversationUi();
3810 updateMucRosterUi();
3811 }
3812 if (user.getRealJid() != null) {
3813 Contact contact = account.getRoster().getContact(user.getRealJid());
3814 contact.setAvatar(avatar);
3815 syncRoster(account);
3816 getAvatarService().clear(contact);
3817 updateRosterUi();
3818 }
3819 }
3820 }
3821 }
3822 }
3823 }
3824 }
3825 }
3826 });
3827 }
3828
3829 public void checkForAvatar(Account account, final UiCallback<Avatar> callback) {
3830 IqPacket packet = this.mIqGenerator.retrieveAvatarMetaData(null);
3831 this.sendIqPacket(account, packet, new OnIqPacketReceived() {
3832
3833 @Override
3834 public void onIqPacketReceived(Account account, IqPacket packet) {
3835 if (packet.getType() == IqPacket.TYPE.RESULT) {
3836 Element pubsub = packet.findChild("pubsub", "http://jabber.org/protocol/pubsub");
3837 if (pubsub != null) {
3838 Element items = pubsub.findChild("items");
3839 if (items != null) {
3840 Avatar avatar = Avatar.parseMetadata(items);
3841 if (avatar != null) {
3842 avatar.owner = account.getJid().asBareJid();
3843 if (fileBackend.isAvatarCached(avatar)) {
3844 if (account.setAvatar(avatar.getFilename())) {
3845 databaseBackend.updateAccount(account);
3846 }
3847 getAvatarService().clear(account);
3848 callback.success(avatar);
3849 } else {
3850 fetchAvatarPep(account, avatar, callback);
3851 }
3852 return;
3853 }
3854 }
3855 }
3856 }
3857 callback.error(0, null);
3858 }
3859 });
3860 }
3861
3862 public void notifyAccountAvatarHasChanged(final Account account) {
3863 final XmppConnection connection = account.getXmppConnection();
3864 if (connection != null && connection.getFeatures().bookmarksConversion()) {
3865 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": avatar changed. resending presence to online group chats");
3866 for (Conversation conversation : conversations) {
3867 if (conversation.getAccount() == account && conversation.getMode() == Conversational.MODE_MULTI) {
3868 final MucOptions mucOptions = conversation.getMucOptions();
3869 if (mucOptions.online()) {
3870 PresencePacket packet = mPresenceGenerator.selfPresence(account, Presence.Status.ONLINE, mucOptions.nonanonymous());
3871 packet.setTo(mucOptions.getSelf().getFullJid());
3872 connection.sendPresencePacket(packet);
3873 }
3874 }
3875 }
3876 }
3877 }
3878
3879 public void deleteContactOnServer(Contact contact) {
3880 contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
3881 contact.resetOption(Contact.Options.DIRTY_PUSH);
3882 contact.setOption(Contact.Options.DIRTY_DELETE);
3883 Account account = contact.getAccount();
3884 if (account.getStatus() == Account.State.ONLINE) {
3885 IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
3886 Element item = iq.query(Namespace.ROSTER).addChild("item");
3887 item.setAttribute("jid", contact.getJid());
3888 item.setAttribute("subscription", "remove");
3889 account.getXmppConnection().sendIqPacket(iq, mDefaultIqHandler);
3890 }
3891 }
3892
3893 public void updateConversation(final Conversation conversation) {
3894 mDatabaseWriterExecutor.execute(() -> databaseBackend.updateConversation(conversation));
3895 }
3896
3897 private void reconnectAccount(final Account account, final boolean force, final boolean interactive) {
3898 synchronized (account) {
3899 XmppConnection connection = account.getXmppConnection();
3900 if (connection == null) {
3901 connection = createConnection(account);
3902 account.setXmppConnection(connection);
3903 }
3904 boolean hasInternet = hasInternetConnection();
3905 if (account.isEnabled() && hasInternet) {
3906 if (!force) {
3907 disconnect(account, false);
3908 }
3909 Thread thread = new Thread(connection);
3910 connection.setInteractive(interactive);
3911 connection.prepareNewConnection();
3912 connection.interrupt();
3913 thread.start();
3914 scheduleWakeUpCall(Config.CONNECT_DISCO_TIMEOUT, account.getUuid().hashCode());
3915 } else {
3916 disconnect(account, force || account.getTrueStatus().isError() || !hasInternet);
3917 account.getRoster().clearPresences();
3918 connection.resetEverything();
3919 final AxolotlService axolotlService = account.getAxolotlService();
3920 if (axolotlService != null) {
3921 axolotlService.resetBrokenness();
3922 }
3923 if (!hasInternet) {
3924 account.setStatus(Account.State.NO_INTERNET);
3925 }
3926 }
3927 }
3928 }
3929
3930 public void reconnectAccountInBackground(final Account account) {
3931 new Thread(() -> reconnectAccount(account, false, true)).start();
3932 }
3933
3934 public void invite(final Conversation conversation, final Jid contact) {
3935 Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": inviting " + contact + " to " + conversation.getJid().asBareJid());
3936 final MucOptions.User user = conversation.getMucOptions().findUserByRealJid(contact.asBareJid());
3937 if (user == null || user.getAffiliation() == MucOptions.Affiliation.OUTCAST) {
3938 changeAffiliationInConference(conversation, contact, MucOptions.Affiliation.NONE, null);
3939 }
3940 final MessagePacket packet = mMessageGenerator.invite(conversation, contact);
3941 sendMessagePacket(conversation.getAccount(), packet);
3942 }
3943
3944 public void directInvite(Conversation conversation, Jid jid) {
3945 MessagePacket packet = mMessageGenerator.directInvite(conversation, jid);
3946 sendMessagePacket(conversation.getAccount(), packet);
3947 }
3948
3949 public void resetSendingToWaiting(Account account) {
3950 for (Conversation conversation : getConversations()) {
3951 if (conversation.getAccount() == account) {
3952 conversation.findUnsentTextMessages(message -> markMessage(message, Message.STATUS_WAITING));
3953 }
3954 }
3955 }
3956
3957 public Message markMessage(final Account account, final Jid recipient, final String uuid, final int status) {
3958 return markMessage(account, recipient, uuid, status, null);
3959 }
3960
3961 public Message markMessage(final Account account, final Jid recipient, final String uuid, final int status, String errorMessage) {
3962 if (uuid == null) {
3963 return null;
3964 }
3965 for (Conversation conversation : getConversations()) {
3966 if (conversation.getJid().asBareJid().equals(recipient) && conversation.getAccount() == account) {
3967 final Message message = conversation.findSentMessageWithUuidOrRemoteId(uuid);
3968 if (message != null) {
3969 markMessage(message, status, errorMessage);
3970 }
3971 return message;
3972 }
3973 }
3974 return null;
3975 }
3976
3977 public boolean markMessage(final Conversation conversation, final String uuid, final int status, final String serverMessageId) {
3978 return markMessage(conversation, uuid, status, serverMessageId, null);
3979 }
3980
3981 public boolean markMessage(final Conversation conversation, final String uuid, final int status, final String serverMessageId, final LocalizedContent body) {
3982 if (uuid == null) {
3983 return false;
3984 } else {
3985 final Message message = conversation.findSentMessageWithUuid(uuid);
3986 if (message != null) {
3987 if (message.getServerMsgId() == null) {
3988 message.setServerMsgId(serverMessageId);
3989 }
3990 if (message.getEncryption() == Message.ENCRYPTION_NONE
3991 && message.isTypeText()
3992 && isBodyModified(message, body)) {
3993 message.setBody(body.content);
3994 if (body.count > 1) {
3995 message.setBodyLanguage(body.language);
3996 }
3997 markMessage(message, status, null, true);
3998 } else {
3999 markMessage(message, status);
4000 }
4001 return true;
4002 } else {
4003 return false;
4004 }
4005 }
4006 }
4007
4008 private static boolean isBodyModified(final Message message, final LocalizedContent body) {
4009 if (body == null || body.content == null) {
4010 return false;
4011 }
4012 return !body.content.equals(message.getBody());
4013 }
4014
4015 public void markMessage(Message message, int status) {
4016 markMessage(message, status, null);
4017 }
4018
4019
4020 public void markMessage(final Message message, final int status, final String errorMessage) {
4021 markMessage(message, status, errorMessage, false);
4022 }
4023
4024 public void markMessage(final Message message, final int status, final String errorMessage, final boolean includeBody) {
4025 final int oldStatus = message.getStatus();
4026 if (status == Message.STATUS_SEND_FAILED && (oldStatus == Message.STATUS_SEND_RECEIVED || oldStatus == Message.STATUS_SEND_DISPLAYED)) {
4027 return;
4028 }
4029 if (status == Message.STATUS_SEND_RECEIVED && oldStatus == Message.STATUS_SEND_DISPLAYED) {
4030 return;
4031 }
4032 message.setErrorMessage(errorMessage);
4033 message.setStatus(status);
4034 databaseBackend.updateMessage(message, includeBody);
4035 updateConversationUi();
4036 if (oldStatus != status && status == Message.STATUS_SEND_FAILED) {
4037 mNotificationService.pushFailedDelivery(message);
4038 }
4039 }
4040
4041 private SharedPreferences getPreferences() {
4042 return PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
4043 }
4044
4045 public long getAutomaticMessageDeletionDate() {
4046 final long timeout = getLongPreference(SettingsActivity.AUTOMATIC_MESSAGE_DELETION, R.integer.automatic_message_deletion);
4047 return timeout == 0 ? timeout : (System.currentTimeMillis() - (timeout * 1000));
4048 }
4049
4050 public long getLongPreference(String name, @IntegerRes int res) {
4051 long defaultValue = getResources().getInteger(res);
4052 try {
4053 return Long.parseLong(getPreferences().getString(name, String.valueOf(defaultValue)));
4054 } catch (NumberFormatException e) {
4055 return defaultValue;
4056 }
4057 }
4058
4059 public boolean getBooleanPreference(String name, @BoolRes int res) {
4060 return getPreferences().getBoolean(name, getResources().getBoolean(res));
4061 }
4062
4063 public boolean confirmMessages() {
4064 return getBooleanPreference("confirm_messages", R.bool.confirm_messages);
4065 }
4066
4067 public boolean allowMessageCorrection() {
4068 return getBooleanPreference("allow_message_correction", R.bool.allow_message_correction);
4069 }
4070
4071 public boolean sendChatStates() {
4072 return getBooleanPreference("chat_states", R.bool.chat_states);
4073 }
4074
4075 private boolean synchronizeWithBookmarks() {
4076 return getBooleanPreference("autojoin", R.bool.autojoin);
4077 }
4078
4079 public boolean useTorToConnect() {
4080 return QuickConversationsService.isConversations() && getBooleanPreference("use_tor", R.bool.use_tor);
4081 }
4082
4083 public boolean showExtendedConnectionOptions() {
4084 return QuickConversationsService.isConversations() && getBooleanPreference("show_connection_options", R.bool.show_connection_options);
4085 }
4086
4087 public boolean broadcastLastActivity() {
4088 return getBooleanPreference(SettingsActivity.BROADCAST_LAST_ACTIVITY, R.bool.last_activity);
4089 }
4090
4091 public int unreadCount() {
4092 int count = 0;
4093 for (Conversation conversation : getConversations()) {
4094 count += conversation.unreadCount();
4095 }
4096 return count;
4097 }
4098
4099
4100 private <T> List<T> threadSafeList(Set<T> set) {
4101 synchronized (LISTENER_LOCK) {
4102 return set.size() == 0 ? Collections.emptyList() : new ArrayList<>(set);
4103 }
4104 }
4105
4106 public void showErrorToastInUi(int resId) {
4107 for (OnShowErrorToast listener : threadSafeList(this.mOnShowErrorToasts)) {
4108 listener.onShowErrorToast(resId);
4109 }
4110 }
4111
4112 public void updateConversationUi() {
4113 for (OnConversationUpdate listener : threadSafeList(this.mOnConversationUpdates)) {
4114 listener.onConversationUpdate();
4115 }
4116 }
4117
4118 public void notifyJingleRtpConnectionUpdate(final Account account, final Jid with, final String sessionId, final RtpEndUserState state) {
4119 for (OnJingleRtpConnectionUpdate listener : threadSafeList(this.onJingleRtpConnectionUpdate)) {
4120 listener.onJingleRtpConnectionUpdate(account, with, sessionId, state);
4121 }
4122 }
4123
4124 public void notifyJingleRtpConnectionUpdate(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices) {
4125 for (OnJingleRtpConnectionUpdate listener : threadSafeList(this.onJingleRtpConnectionUpdate)) {
4126 listener.onAudioDeviceChanged(selectedAudioDevice, availableAudioDevices);
4127 }
4128 }
4129
4130 public void updateAccountUi() {
4131 for (final OnAccountUpdate listener : threadSafeList(this.mOnAccountUpdates)) {
4132 listener.onAccountUpdate();
4133 }
4134 }
4135
4136 public void updateRosterUi() {
4137 for (OnRosterUpdate listener : threadSafeList(this.mOnRosterUpdates)) {
4138 listener.onRosterUpdate();
4139 }
4140 }
4141
4142 public boolean displayCaptchaRequest(Account account, String id, Data data, Bitmap captcha) {
4143 if (mOnCaptchaRequested.size() > 0) {
4144 DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
4145 Bitmap scaled = Bitmap.createScaledBitmap(captcha, (int) (captcha.getWidth() * metrics.scaledDensity),
4146 (int) (captcha.getHeight() * metrics.scaledDensity), false);
4147 for (OnCaptchaRequested listener : threadSafeList(this.mOnCaptchaRequested)) {
4148 listener.onCaptchaRequested(account, id, data, scaled);
4149 }
4150 return true;
4151 }
4152 return false;
4153 }
4154
4155 public void updateBlocklistUi(final OnUpdateBlocklist.Status status) {
4156 for (OnUpdateBlocklist listener : threadSafeList(this.mOnUpdateBlocklist)) {
4157 listener.OnUpdateBlocklist(status);
4158 }
4159 }
4160
4161 public void updateMucRosterUi() {
4162 for (OnMucRosterUpdate listener : threadSafeList(this.mOnMucRosterUpdate)) {
4163 listener.onMucRosterUpdate();
4164 }
4165 }
4166
4167 public void keyStatusUpdated(AxolotlService.FetchStatus report) {
4168 for (OnKeyStatusUpdated listener : threadSafeList(this.mOnKeyStatusUpdated)) {
4169 listener.onKeyStatusUpdated(report);
4170 }
4171 }
4172
4173 public Account findAccountByJid(final Jid jid) {
4174 for (final Account account : this.accounts) {
4175 if (account.getJid().asBareJid().equals(jid.asBareJid())) {
4176 return account;
4177 }
4178 }
4179 return null;
4180 }
4181
4182 public Account findAccountByUuid(final String uuid) {
4183 for (Account account : this.accounts) {
4184 if (account.getUuid().equals(uuid)) {
4185 return account;
4186 }
4187 }
4188 return null;
4189 }
4190
4191 public Conversation findConversationByUuid(String uuid) {
4192 for (Conversation conversation : getConversations()) {
4193 if (conversation.getUuid().equals(uuid)) {
4194 return conversation;
4195 }
4196 }
4197 return null;
4198 }
4199
4200 public Conversation findUniqueConversationByJid(XmppUri xmppUri) {
4201 List<Conversation> findings = new ArrayList<>();
4202 for (Conversation c : getConversations()) {
4203 if (c.getAccount().isEnabled() && c.getJid().asBareJid().equals(xmppUri.getJid()) && ((c.getMode() == Conversational.MODE_MULTI) == xmppUri.isAction(XmppUri.ACTION_JOIN))) {
4204 findings.add(c);
4205 }
4206 }
4207 return findings.size() == 1 ? findings.get(0) : null;
4208 }
4209
4210 public boolean markRead(final Conversation conversation, boolean dismiss) {
4211 return markRead(conversation, null, dismiss).size() > 0;
4212 }
4213
4214 public void markRead(final Conversation conversation) {
4215 markRead(conversation, null, true);
4216 }
4217
4218 public List<Message> markRead(final Conversation conversation, String upToUuid, boolean dismiss) {
4219 if (dismiss) {
4220 mNotificationService.clear(conversation);
4221 }
4222 final List<Message> readMessages = conversation.markRead(upToUuid);
4223 if (readMessages.size() > 0) {
4224 Runnable runnable = () -> {
4225 for (Message message : readMessages) {
4226 databaseBackend.updateMessage(message, false);
4227 }
4228 };
4229 mDatabaseWriterExecutor.execute(runnable);
4230 updateConversationUi();
4231 updateUnreadCountBadge();
4232 return readMessages;
4233 } else {
4234 return readMessages;
4235 }
4236 }
4237
4238 public synchronized void updateUnreadCountBadge() {
4239 int count = unreadCount();
4240 if (unreadCount != count) {
4241 Log.d(Config.LOGTAG, "update unread count to " + count);
4242 if (count > 0) {
4243 ShortcutBadger.applyCount(getApplicationContext(), count);
4244 } else {
4245 ShortcutBadger.removeCount(getApplicationContext());
4246 }
4247 unreadCount = count;
4248 }
4249 }
4250
4251 public void sendReadMarker(final Conversation conversation, String upToUuid) {
4252 final boolean isPrivateAndNonAnonymousMuc = conversation.getMode() == Conversation.MODE_MULTI && conversation.isPrivateAndNonAnonymous();
4253 final List<Message> readMessages = this.markRead(conversation, upToUuid, true);
4254 if (readMessages.size() > 0) {
4255 updateConversationUi();
4256 }
4257 final Message markable = Conversation.getLatestMarkableMessage(readMessages, isPrivateAndNonAnonymousMuc);
4258 if (confirmMessages()
4259 && markable != null
4260 && (markable.trusted() || isPrivateAndNonAnonymousMuc)
4261 && markable.getRemoteMsgId() != null) {
4262 Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": sending read marker to " + markable.getCounterpart().toString());
4263 final Account account = conversation.getAccount();
4264 final MessagePacket packet = mMessageGenerator.confirm(markable);
4265 this.sendMessagePacket(account, packet);
4266 }
4267 }
4268
4269 public SecureRandom getRNG() {
4270 return this.mRandom;
4271 }
4272
4273 public MemorizingTrustManager getMemorizingTrustManager() {
4274 return this.mMemorizingTrustManager;
4275 }
4276
4277 public void setMemorizingTrustManager(MemorizingTrustManager trustManager) {
4278 this.mMemorizingTrustManager = trustManager;
4279 }
4280
4281 public void updateMemorizingTrustmanager() {
4282 final MemorizingTrustManager tm;
4283 final boolean dontTrustSystemCAs = getBooleanPreference("dont_trust_system_cas", R.bool.dont_trust_system_cas);
4284 if (dontTrustSystemCAs) {
4285 tm = new MemorizingTrustManager(getApplicationContext(), null);
4286 } else {
4287 tm = new MemorizingTrustManager(getApplicationContext());
4288 }
4289 setMemorizingTrustManager(tm);
4290 }
4291
4292 public LruCache<String, Bitmap> getBitmapCache() {
4293 return this.mBitmapCache;
4294 }
4295
4296 public Collection<String> getKnownHosts() {
4297 final Set<String> hosts = new HashSet<>();
4298 for (final Account account : getAccounts()) {
4299 hosts.add(account.getServer());
4300 for (final Contact contact : account.getRoster().getContacts()) {
4301 if (contact.showInRoster()) {
4302 final String server = contact.getServer();
4303 if (server != null) {
4304 hosts.add(server);
4305 }
4306 }
4307 }
4308 }
4309 if (Config.QUICKSY_DOMAIN != null) {
4310 hosts.remove(Config.QUICKSY_DOMAIN.toEscapedString()); //we only want to show this when we type a e164 number
4311 }
4312 if (Config.DOMAIN_LOCK != null) {
4313 hosts.add(Config.DOMAIN_LOCK);
4314 }
4315 if (Config.MAGIC_CREATE_DOMAIN != null) {
4316 hosts.add(Config.MAGIC_CREATE_DOMAIN);
4317 }
4318 return hosts;
4319 }
4320
4321 public Collection<String> getKnownConferenceHosts() {
4322 final Set<String> mucServers = new HashSet<>();
4323 for (final Account account : accounts) {
4324 if (account.getXmppConnection() != null) {
4325 mucServers.addAll(account.getXmppConnection().getMucServers());
4326 for (Bookmark bookmark : account.getBookmarks()) {
4327 final Jid jid = bookmark.getJid();
4328 final String s = jid == null ? null : jid.getDomain().toEscapedString();
4329 if (s != null) {
4330 mucServers.add(s);
4331 }
4332 }
4333 }
4334 }
4335 return mucServers;
4336 }
4337
4338 public void sendMessagePacket(Account account, MessagePacket packet) {
4339 final XmppConnection connection = account.getXmppConnection();
4340 if (connection != null) {
4341 connection.sendMessagePacket(packet);
4342 }
4343 }
4344
4345 public void sendPresencePacket(Account account, PresencePacket packet) {
4346 XmppConnection connection = account.getXmppConnection();
4347 if (connection != null) {
4348 connection.sendPresencePacket(packet);
4349 }
4350 }
4351
4352 public void sendCreateAccountWithCaptchaPacket(Account account, String id, Data data) {
4353 final XmppConnection connection = account.getXmppConnection();
4354 if (connection != null) {
4355 IqPacket request = mIqGenerator.generateCreateAccountWithCaptcha(account, id, data);
4356 connection.sendUnmodifiedIqPacket(request, connection.registrationResponseListener, true);
4357 }
4358 }
4359
4360 public void sendIqPacket(final Account account, final IqPacket packet, final OnIqPacketReceived callback) {
4361 final XmppConnection connection = account.getXmppConnection();
4362 if (connection != null) {
4363 connection.sendIqPacket(packet, callback);
4364 } else if (callback != null) {
4365 callback.onIqPacketReceived(account, new IqPacket(IqPacket.TYPE.TIMEOUT));
4366 }
4367 }
4368
4369 public void sendPresence(final Account account) {
4370 sendPresence(account, checkListeners() && broadcastLastActivity());
4371 }
4372
4373 private void sendPresence(final Account account, final boolean includeIdleTimestamp) {
4374 final Presence.Status status;
4375 if (manuallyChangePresence()) {
4376 status = account.getPresenceStatus();
4377 } else {
4378 status = getTargetPresence();
4379 }
4380 final PresencePacket packet = mPresenceGenerator.selfPresence(account, status);
4381 if (mLastActivity > 0 && includeIdleTimestamp) {
4382 long since = Math.min(mLastActivity, System.currentTimeMillis()); //don't send future dates
4383 packet.addChild("idle", Namespace.IDLE).setAttribute("since", AbstractGenerator.getTimestamp(since));
4384 }
4385 sendPresencePacket(account, packet);
4386 }
4387
4388 private void deactivateGracePeriod() {
4389 for (Account account : getAccounts()) {
4390 account.deactivateGracePeriod();
4391 }
4392 }
4393
4394 public void refreshAllPresences() {
4395 boolean includeIdleTimestamp = checkListeners() && broadcastLastActivity();
4396 for (Account account : getAccounts()) {
4397 if (account.isEnabled()) {
4398 sendPresence(account, includeIdleTimestamp);
4399 }
4400 }
4401 }
4402
4403 private void refreshAllFcmTokens() {
4404 for (Account account : getAccounts()) {
4405 if (account.isOnlineAndConnected() && mPushManagementService.available(account)) {
4406 mPushManagementService.registerPushTokenOnServer(account);
4407 //TODO renew mucs
4408 }
4409 }
4410 }
4411
4412 private void sendOfflinePresence(final Account account) {
4413 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": sending offline presence");
4414 sendPresencePacket(account, mPresenceGenerator.sendOfflinePresence(account));
4415 }
4416
4417 public MessageGenerator getMessageGenerator() {
4418 return this.mMessageGenerator;
4419 }
4420
4421 public PresenceGenerator getPresenceGenerator() {
4422 return this.mPresenceGenerator;
4423 }
4424
4425 public IqGenerator getIqGenerator() {
4426 return this.mIqGenerator;
4427 }
4428
4429 public IqParser getIqParser() {
4430 return this.mIqParser;
4431 }
4432
4433 public JingleConnectionManager getJingleConnectionManager() {
4434 return this.mJingleConnectionManager;
4435 }
4436
4437 public MessageArchiveService getMessageArchiveService() {
4438 return this.mMessageArchiveService;
4439 }
4440
4441 public QuickConversationsService getQuickConversationsService() {
4442 return this.mQuickConversationsService;
4443 }
4444
4445 public List<Contact> findContacts(Jid jid, String accountJid) {
4446 ArrayList<Contact> contacts = new ArrayList<>();
4447 for (Account account : getAccounts()) {
4448 if ((account.isEnabled() || accountJid != null)
4449 && (accountJid == null || accountJid.equals(account.getJid().asBareJid().toString()))) {
4450 Contact contact = account.getRoster().getContactFromContactList(jid);
4451 if (contact != null) {
4452 contacts.add(contact);
4453 }
4454 }
4455 }
4456 return contacts;
4457 }
4458
4459 public Conversation findFirstMuc(Jid jid) {
4460 for (Conversation conversation : getConversations()) {
4461 if (conversation.getAccount().isEnabled() && conversation.getJid().asBareJid().equals(jid.asBareJid()) && conversation.getMode() == Conversation.MODE_MULTI) {
4462 return conversation;
4463 }
4464 }
4465 return null;
4466 }
4467
4468 public NotificationService getNotificationService() {
4469 return this.mNotificationService;
4470 }
4471
4472 public HttpConnectionManager getHttpConnectionManager() {
4473 return this.mHttpConnectionManager;
4474 }
4475
4476 public void resendFailedMessages(final Message message) {
4477 final Collection<Message> messages = new ArrayList<>();
4478 Message current = message;
4479 while (current.getStatus() == Message.STATUS_SEND_FAILED) {
4480 messages.add(current);
4481 if (current.mergeable(current.next())) {
4482 current = current.next();
4483 } else {
4484 break;
4485 }
4486 }
4487 for (final Message msg : messages) {
4488 msg.setTime(System.currentTimeMillis());
4489 markMessage(msg, Message.STATUS_WAITING);
4490 this.resendMessage(msg, false);
4491 }
4492 if (message.getConversation() instanceof Conversation) {
4493 ((Conversation) message.getConversation()).sort();
4494 }
4495 updateConversationUi();
4496 }
4497
4498 public void clearConversationHistory(final Conversation conversation) {
4499 final long clearDate;
4500 final String reference;
4501 if (conversation.countMessages() > 0) {
4502 Message latestMessage = conversation.getLatestMessage();
4503 clearDate = latestMessage.getTimeSent() + 1000;
4504 reference = latestMessage.getServerMsgId();
4505 } else {
4506 clearDate = System.currentTimeMillis();
4507 reference = null;
4508 }
4509 conversation.clearMessages();
4510 conversation.setHasMessagesLeftOnServer(false); //avoid messages getting loaded through mam
4511 conversation.setLastClearHistory(clearDate, reference);
4512 Runnable runnable = () -> {
4513 databaseBackend.deleteMessagesInConversation(conversation);
4514 databaseBackend.updateConversation(conversation);
4515 };
4516 mDatabaseWriterExecutor.execute(runnable);
4517 }
4518
4519 public boolean sendBlockRequest(final Blockable blockable, boolean reportSpam) {
4520 if (blockable != null && blockable.getBlockedJid() != null) {
4521 final Jid jid = blockable.getBlockedJid();
4522 this.sendIqPacket(blockable.getAccount(), getIqGenerator().generateSetBlockRequest(jid, reportSpam), (a, response) -> {
4523 if (response.getType() == IqPacket.TYPE.RESULT) {
4524 a.getBlocklist().add(jid);
4525 updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
4526 }
4527 });
4528 if (blockable.getBlockedJid().isFullJid()) {
4529 return false;
4530 } else if (removeBlockedConversations(blockable.getAccount(), jid)) {
4531 updateConversationUi();
4532 return true;
4533 } else {
4534 return false;
4535 }
4536 } else {
4537 return false;
4538 }
4539 }
4540
4541 public boolean removeBlockedConversations(final Account account, final Jid blockedJid) {
4542 boolean removed = false;
4543 synchronized (this.conversations) {
4544 boolean domainJid = blockedJid.getLocal() == null;
4545 for (Conversation conversation : this.conversations) {
4546 boolean jidMatches = (domainJid && blockedJid.getDomain().equals(conversation.getJid().getDomain()))
4547 || blockedJid.equals(conversation.getJid().asBareJid());
4548 if (conversation.getAccount() == account
4549 && conversation.getMode() == Conversation.MODE_SINGLE
4550 && jidMatches) {
4551 this.conversations.remove(conversation);
4552 markRead(conversation);
4553 conversation.setStatus(Conversation.STATUS_ARCHIVED);
4554 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": archiving conversation " + conversation.getJid().asBareJid() + " because jid was blocked");
4555 updateConversation(conversation);
4556 removed = true;
4557 }
4558 }
4559 }
4560 return removed;
4561 }
4562
4563 public void sendUnblockRequest(final Blockable blockable) {
4564 if (blockable != null && blockable.getJid() != null) {
4565 final Jid jid = blockable.getBlockedJid();
4566 this.sendIqPacket(blockable.getAccount(), getIqGenerator().generateSetUnblockRequest(jid), new OnIqPacketReceived() {
4567 @Override
4568 public void onIqPacketReceived(final Account account, final IqPacket packet) {
4569 if (packet.getType() == IqPacket.TYPE.RESULT) {
4570 account.getBlocklist().remove(jid);
4571 updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
4572 }
4573 }
4574 });
4575 }
4576 }
4577
4578 public void publishDisplayName(Account account) {
4579 String displayName = account.getDisplayName();
4580 final IqPacket request;
4581 if (TextUtils.isEmpty(displayName)) {
4582 request = mIqGenerator.deleteNode(Namespace.NICK);
4583 } else {
4584 request = mIqGenerator.publishNick(displayName);
4585 }
4586 mAvatarService.clear(account);
4587 sendIqPacket(account, request, (account1, packet) -> {
4588 if (packet.getType() == IqPacket.TYPE.ERROR) {
4589 Log.d(Config.LOGTAG, account1.getJid().asBareJid() + ": unable to modify nick name " + packet.toString());
4590 }
4591 });
4592 }
4593
4594 public ServiceDiscoveryResult getCachedServiceDiscoveryResult(Pair<String, String> key) {
4595 ServiceDiscoveryResult result = discoCache.get(key);
4596 if (result != null) {
4597 return result;
4598 } else {
4599 result = databaseBackend.findDiscoveryResult(key.first, key.second);
4600 if (result != null) {
4601 discoCache.put(key, result);
4602 }
4603 return result;
4604 }
4605 }
4606
4607 public void fetchCaps(Account account, final Jid jid, final Presence presence) {
4608 final Pair<String, String> key = new Pair<>(presence.getHash(), presence.getVer());
4609 final ServiceDiscoveryResult disco = getCachedServiceDiscoveryResult(key);
4610 if (disco != null) {
4611 presence.setServiceDiscoveryResult(disco);
4612 final Contact contact = account.getRoster().getContact(jid);
4613 if (contact.refreshRtpCapability()) {
4614 syncRoster(account);
4615 }
4616 } else {
4617 final IqPacket request = new IqPacket(IqPacket.TYPE.GET);
4618 request.setTo(jid);
4619 final String node = presence.getNode();
4620 final String ver = presence.getVer();
4621 final Element query = request.query(Namespace.DISCO_INFO);
4622 if (node != null && ver != null) {
4623 query.setAttribute("node", node + "#" + ver);
4624 }
4625 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": making disco request for " + key.second + " to " + jid);
4626 sendIqPacket(account, request, (a, response) -> {
4627 if (response.getType() == IqPacket.TYPE.RESULT) {
4628 final ServiceDiscoveryResult discoveryResult = new ServiceDiscoveryResult(response);
4629 if (presence.getVer().equals(discoveryResult.getVer())) {
4630 databaseBackend.insertDiscoveryResult(discoveryResult);
4631 injectServiceDiscoveryResult(a.getRoster(), presence.getHash(), presence.getVer(), discoveryResult);
4632 } else {
4633 Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": mismatch in caps for contact " + jid + " " + presence.getVer() + " vs " + discoveryResult.getVer());
4634 }
4635 } else {
4636 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to fetch caps from " + jid);
4637 }
4638 });
4639 }
4640 }
4641
4642 private void injectServiceDiscoveryResult(Roster roster, String hash, String ver, ServiceDiscoveryResult disco) {
4643 boolean rosterNeedsSync = false;
4644 for (final Contact contact : roster.getContacts()) {
4645 boolean serviceDiscoverySet = false;
4646 for (final Presence presence : contact.getPresences().getPresences()) {
4647 if (hash.equals(presence.getHash()) && ver.equals(presence.getVer())) {
4648 presence.setServiceDiscoveryResult(disco);
4649 serviceDiscoverySet = true;
4650 }
4651 }
4652 if (serviceDiscoverySet) {
4653 rosterNeedsSync |= contact.refreshRtpCapability();
4654 }
4655 }
4656 if (rosterNeedsSync) {
4657 syncRoster(roster.getAccount());
4658 }
4659 }
4660
4661 public void fetchMamPreferences(Account account, final OnMamPreferencesFetched callback) {
4662 final MessageArchiveService.Version version = MessageArchiveService.Version.get(account);
4663 IqPacket request = new IqPacket(IqPacket.TYPE.GET);
4664 request.addChild("prefs", version.namespace);
4665 sendIqPacket(account, request, (account1, packet) -> {
4666 Element prefs = packet.findChild("prefs", version.namespace);
4667 if (packet.getType() == IqPacket.TYPE.RESULT && prefs != null) {
4668 callback.onPreferencesFetched(prefs);
4669 } else {
4670 callback.onPreferencesFetchFailed();
4671 }
4672 });
4673 }
4674
4675 public PushManagementService getPushManagementService() {
4676 return mPushManagementService;
4677 }
4678
4679 public void changeStatus(Account account, PresenceTemplate template, String signature) {
4680 if (!template.getStatusMessage().isEmpty()) {
4681 databaseBackend.insertPresenceTemplate(template);
4682 }
4683 account.setPgpSignature(signature);
4684 account.setPresenceStatus(template.getStatus());
4685 account.setPresenceStatusMessage(template.getStatusMessage());
4686 databaseBackend.updateAccount(account);
4687 sendPresence(account);
4688 }
4689
4690 public List<PresenceTemplate> getPresenceTemplates(Account account) {
4691 List<PresenceTemplate> templates = databaseBackend.getPresenceTemplates();
4692 for (PresenceTemplate template : account.getSelfContact().getPresences().asTemplates()) {
4693 if (!templates.contains(template)) {
4694 templates.add(0, template);
4695 }
4696 }
4697 return templates;
4698 }
4699
4700 public void saveConversationAsBookmark(Conversation conversation, String name) {
4701 final Account account = conversation.getAccount();
4702 final Bookmark bookmark = new Bookmark(account, conversation.getJid().asBareJid());
4703 final String nick = conversation.getJid().getResource();
4704 if (nick != null && !nick.isEmpty() && !nick.equals(MucOptions.defaultNick(account))) {
4705 bookmark.setNick(nick);
4706 }
4707 if (!TextUtils.isEmpty(name)) {
4708 bookmark.setBookmarkName(name);
4709 }
4710 bookmark.setAutojoin(getPreferences().getBoolean("autojoin", getResources().getBoolean(R.bool.autojoin)));
4711 createBookmark(account, bookmark);
4712 bookmark.setConversation(conversation);
4713 }
4714
4715 public boolean verifyFingerprints(Contact contact, List<XmppUri.Fingerprint> fingerprints) {
4716 boolean performedVerification = false;
4717 final AxolotlService axolotlService = contact.getAccount().getAxolotlService();
4718 for (XmppUri.Fingerprint fp : fingerprints) {
4719 if (fp.type == XmppUri.FingerprintType.OMEMO) {
4720 String fingerprint = "05" + fp.fingerprint.replaceAll("\\s", "");
4721 FingerprintStatus fingerprintStatus = axolotlService.getFingerprintTrust(fingerprint);
4722 if (fingerprintStatus != null) {
4723 if (!fingerprintStatus.isVerified()) {
4724 performedVerification = true;
4725 axolotlService.setFingerprintTrust(fingerprint, fingerprintStatus.toVerified());
4726 }
4727 } else {
4728 axolotlService.preVerifyFingerprint(contact, fingerprint);
4729 }
4730 }
4731 }
4732 return performedVerification;
4733 }
4734
4735 public boolean verifyFingerprints(Account account, List<XmppUri.Fingerprint> fingerprints) {
4736 final AxolotlService axolotlService = account.getAxolotlService();
4737 boolean verifiedSomething = false;
4738 for (XmppUri.Fingerprint fp : fingerprints) {
4739 if (fp.type == XmppUri.FingerprintType.OMEMO) {
4740 String fingerprint = "05" + fp.fingerprint.replaceAll("\\s", "");
4741 Log.d(Config.LOGTAG, "trying to verify own fp=" + fingerprint);
4742 FingerprintStatus fingerprintStatus = axolotlService.getFingerprintTrust(fingerprint);
4743 if (fingerprintStatus != null) {
4744 if (!fingerprintStatus.isVerified()) {
4745 axolotlService.setFingerprintTrust(fingerprint, fingerprintStatus.toVerified());
4746 verifiedSomething = true;
4747 }
4748 } else {
4749 axolotlService.preVerifyFingerprint(account, fingerprint);
4750 verifiedSomething = true;
4751 }
4752 }
4753 }
4754 return verifiedSomething;
4755 }
4756
4757 public boolean blindTrustBeforeVerification() {
4758 return getBooleanPreference(SettingsActivity.BLIND_TRUST_BEFORE_VERIFICATION, R.bool.btbv);
4759 }
4760
4761 public ShortcutService getShortcutService() {
4762 return mShortcutService;
4763 }
4764
4765 public void pushMamPreferences(Account account, Element prefs) {
4766 IqPacket set = new IqPacket(IqPacket.TYPE.SET);
4767 set.addChild(prefs);
4768 sendIqPacket(account, set, null);
4769 }
4770
4771 public void evictPreview(String uuid) {
4772 if (mBitmapCache.remove(uuid) != null) {
4773 Log.d(Config.LOGTAG, "deleted cached preview");
4774 }
4775 }
4776
4777 public interface OnMamPreferencesFetched {
4778 void onPreferencesFetched(Element prefs);
4779
4780 void onPreferencesFetchFailed();
4781 }
4782
4783 public interface OnAccountCreated {
4784 void onAccountCreated(Account account);
4785
4786 void informUser(int r);
4787 }
4788
4789 public interface OnMoreMessagesLoaded {
4790 void onMoreMessagesLoaded(int count, Conversation conversation);
4791
4792 void informUser(int r);
4793 }
4794
4795 public interface OnAccountPasswordChanged {
4796 void onPasswordChangeSucceeded();
4797
4798 void onPasswordChangeFailed();
4799 }
4800
4801 public interface OnRoomDestroy {
4802 void onRoomDestroySucceeded();
4803
4804 void onRoomDestroyFailed();
4805 }
4806
4807 public interface OnAffiliationChanged {
4808 void onAffiliationChangedSuccessful(Jid jid);
4809
4810 void onAffiliationChangeFailed(Jid jid, int resId);
4811 }
4812
4813 public interface OnConversationUpdate {
4814 void onConversationUpdate();
4815 }
4816
4817 public interface OnJingleRtpConnectionUpdate {
4818 void onJingleRtpConnectionUpdate(final Account account, final Jid with, final String sessionId, final RtpEndUserState state);
4819
4820 void onAudioDeviceChanged(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices);
4821 }
4822
4823 public interface OnAccountUpdate {
4824 void onAccountUpdate();
4825 }
4826
4827 public interface OnCaptchaRequested {
4828 void onCaptchaRequested(Account account, String id, Data data, Bitmap captcha);
4829 }
4830
4831 public interface OnRosterUpdate {
4832 void onRosterUpdate();
4833 }
4834
4835 public interface OnMucRosterUpdate {
4836 void onMucRosterUpdate();
4837 }
4838
4839 public interface OnConferenceConfigurationFetched {
4840 void onConferenceConfigurationFetched(Conversation conversation);
4841
4842 void onFetchFailed(Conversation conversation, String errorCondition);
4843 }
4844
4845 public interface OnConferenceJoined {
4846 void onConferenceJoined(Conversation conversation);
4847 }
4848
4849 public interface OnConfigurationPushed {
4850 void onPushSucceeded();
4851
4852 void onPushFailed();
4853 }
4854
4855 public interface OnShowErrorToast {
4856 void onShowErrorToast(int resId);
4857 }
4858
4859 public class XmppConnectionBinder extends Binder {
4860 public XmppConnectionService getService() {
4861 return XmppConnectionService.this;
4862 }
4863 }
4864
4865 private class InternalEventReceiver extends BroadcastReceiver {
4866
4867 @Override
4868 public void onReceive(Context context, Intent intent) {
4869 onStartCommand(intent, 0, 0);
4870 }
4871 }
4872
4873 public static class OngoingCall {
4874 public final AbstractJingleConnection.Id id;
4875 public final Set<Media> media;
4876 public final boolean reconnecting;
4877
4878 public OngoingCall(AbstractJingleConnection.Id id, Set<Media> media, final boolean reconnecting) {
4879 this.id = id;
4880 this.media = media;
4881 this.reconnecting = reconnecting;
4882 }
4883
4884 @Override
4885 public boolean equals(Object o) {
4886 if (this == o) return true;
4887 if (o == null || getClass() != o.getClass()) return false;
4888 OngoingCall that = (OngoingCall) o;
4889 return reconnecting == that.reconnecting && Objects.equal(id, that.id) && Objects.equal(media, that.media);
4890 }
4891
4892 @Override
4893 public int hashCode() {
4894 return Objects.hashCode(id, media, reconnecting);
4895 }
4896 }
4897}