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