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