XmppConnectionService.java

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