XmppConnectionService.java

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