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