XmppConnectionService.java

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