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