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