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