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