XmppConnectionService.java

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