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