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