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