XmppConnectionService.java

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