XmppConnectionService.java

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