XmppConnectionService.java

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