XmppConnectionService.java

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