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