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