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