XmppConnectionService.java

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