XmppConnectionService.java

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