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