XmppConnectionService.java

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