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