XmppConnectionService.java

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