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