XmppConnectionService.java

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