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