XmppConnectionService.java

   1package eu.siacs.conversations.services;
   2
   3import android.Manifest;
   4import android.annotation.SuppressLint;
   5import android.annotation.TargetApi;
   6import android.app.AlarmManager;
   7import android.app.KeyguardManager;
   8import android.app.Notification;
   9import android.app.NotificationManager;
  10import android.app.PendingIntent;
  11import android.app.Service;
  12import android.content.BroadcastReceiver;
  13import android.content.ComponentName;
  14import android.content.Context;
  15import android.content.Intent;
  16import android.content.IntentFilter;
  17import android.content.SharedPreferences;
  18import android.content.pm.PackageManager;
  19import android.database.ContentObserver;
  20import android.graphics.Bitmap;
  21import android.media.AudioManager;
  22import android.net.ConnectivityManager;
  23import android.net.Network;
  24import android.net.NetworkCapabilities;
  25import android.net.NetworkInfo;
  26import android.net.Uri;
  27import android.os.Binder;
  28import android.os.Build;
  29import android.os.Bundle;
  30import android.os.Environment;
  31import android.os.IBinder;
  32import android.os.PowerManager;
  33import android.os.PowerManager.WakeLock;
  34import android.os.SystemClock;
  35import android.preference.PreferenceManager;
  36import android.provider.ContactsContract;
  37import android.security.KeyChain;
  38import android.telephony.PhoneStateListener;
  39import android.telephony.TelephonyManager;
  40import android.text.TextUtils;
  41import android.util.DisplayMetrics;
  42import android.util.Log;
  43import android.util.LruCache;
  44import android.util.Pair;
  45
  46import androidx.annotation.BoolRes;
  47import androidx.annotation.IntegerRes;
  48import androidx.core.app.RemoteInput;
  49import androidx.core.content.ContextCompat;
  50
  51import com.google.common.base.Objects;
  52import com.google.common.base.Strings;
  53
  54import org.conscrypt.Conscrypt;
  55import org.openintents.openpgp.IOpenPgpService2;
  56import org.openintents.openpgp.util.OpenPgpApi;
  57import org.openintents.openpgp.util.OpenPgpServiceConnection;
  58
  59import java.io.File;
  60import java.security.SecureRandom;
  61import java.security.Security;
  62import java.security.cert.CertificateException;
  63import java.security.cert.X509Certificate;
  64import java.util.ArrayList;
  65import java.util.Arrays;
  66import java.util.Collection;
  67import java.util.Collections;
  68import java.util.HashSet;
  69import java.util.Hashtable;
  70import java.util.Iterator;
  71import java.util.List;
  72import java.util.ListIterator;
  73import java.util.Map;
  74import java.util.Set;
  75import java.util.WeakHashMap;
  76import java.util.concurrent.CopyOnWriteArrayList;
  77import java.util.concurrent.CountDownLatch;
  78import java.util.concurrent.atomic.AtomicBoolean;
  79import java.util.concurrent.atomic.AtomicLong;
  80import java.util.concurrent.atomic.AtomicReference;
  81
  82import eu.siacs.conversations.Config;
  83import eu.siacs.conversations.R;
  84import eu.siacs.conversations.android.JabberIdContact;
  85import eu.siacs.conversations.crypto.OmemoSetting;
  86import eu.siacs.conversations.crypto.PgpDecryptionService;
  87import eu.siacs.conversations.crypto.PgpEngine;
  88import eu.siacs.conversations.crypto.axolotl.AxolotlService;
  89import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
  90import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
  91import eu.siacs.conversations.entities.Account;
  92import eu.siacs.conversations.entities.Blockable;
  93import eu.siacs.conversations.entities.Bookmark;
  94import eu.siacs.conversations.entities.Contact;
  95import eu.siacs.conversations.entities.Conversation;
  96import eu.siacs.conversations.entities.Conversational;
  97import eu.siacs.conversations.entities.Message;
  98import eu.siacs.conversations.entities.MucOptions;
  99import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
 100import eu.siacs.conversations.entities.Presence;
 101import eu.siacs.conversations.entities.PresenceTemplate;
 102import eu.siacs.conversations.entities.Roster;
 103import eu.siacs.conversations.entities.ServiceDiscoveryResult;
 104import eu.siacs.conversations.generator.AbstractGenerator;
 105import eu.siacs.conversations.generator.IqGenerator;
 106import eu.siacs.conversations.generator.MessageGenerator;
 107import eu.siacs.conversations.generator.PresenceGenerator;
 108import eu.siacs.conversations.http.HttpConnectionManager;
 109import eu.siacs.conversations.parser.AbstractParser;
 110import eu.siacs.conversations.parser.IqParser;
 111import eu.siacs.conversations.parser.MessageParser;
 112import eu.siacs.conversations.parser.PresenceParser;
 113import eu.siacs.conversations.persistance.DatabaseBackend;
 114import eu.siacs.conversations.persistance.FileBackend;
 115import eu.siacs.conversations.ui.ChooseAccountForProfilePictureActivity;
 116import eu.siacs.conversations.ui.RtpSessionActivity;
 117import eu.siacs.conversations.ui.SettingsActivity;
 118import eu.siacs.conversations.ui.UiCallback;
 119import eu.siacs.conversations.ui.interfaces.OnAvatarPublication;
 120import eu.siacs.conversations.ui.interfaces.OnMediaLoaded;
 121import eu.siacs.conversations.ui.interfaces.OnSearchResultsAvailable;
 122import eu.siacs.conversations.utils.Compatibility;
 123import eu.siacs.conversations.utils.ConversationsFileObserver;
 124import eu.siacs.conversations.utils.CryptoHelper;
 125import eu.siacs.conversations.utils.EasyOnboardingInvite;
 126import eu.siacs.conversations.utils.ExceptionHelper;
 127import eu.siacs.conversations.utils.MimeUtils;
 128import eu.siacs.conversations.utils.PhoneHelper;
 129import eu.siacs.conversations.utils.QuickLoader;
 130import eu.siacs.conversations.utils.ReplacingSerialSingleThreadExecutor;
 131import eu.siacs.conversations.utils.ReplacingTaskManager;
 132import eu.siacs.conversations.utils.Resolver;
 133import eu.siacs.conversations.utils.SerialSingleThreadExecutor;
 134import eu.siacs.conversations.utils.StringUtils;
 135import eu.siacs.conversations.utils.TorServiceUtils;
 136import eu.siacs.conversations.utils.WakeLockHelper;
 137import eu.siacs.conversations.utils.XmppUri;
 138import eu.siacs.conversations.xml.Element;
 139import eu.siacs.conversations.xml.LocalizedContent;
 140import eu.siacs.conversations.xml.Namespace;
 141import eu.siacs.conversations.xmpp.Jid;
 142import eu.siacs.conversations.xmpp.OnBindListener;
 143import eu.siacs.conversations.xmpp.OnContactStatusChanged;
 144import eu.siacs.conversations.xmpp.OnIqPacketReceived;
 145import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
 146import eu.siacs.conversations.xmpp.OnMessageAcknowledged;
 147import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
 148import eu.siacs.conversations.xmpp.OnPresencePacketReceived;
 149import eu.siacs.conversations.xmpp.OnStatusChanged;
 150import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
 151import eu.siacs.conversations.xmpp.Patches;
 152import eu.siacs.conversations.xmpp.XmppConnection;
 153import eu.siacs.conversations.xmpp.chatstate.ChatState;
 154import eu.siacs.conversations.xmpp.forms.Data;
 155import eu.siacs.conversations.xmpp.jingle.AbstractJingleConnection;
 156import eu.siacs.conversations.xmpp.jingle.JingleConnectionManager;
 157import eu.siacs.conversations.xmpp.jingle.JingleRtpConnection;
 158import eu.siacs.conversations.xmpp.jingle.Media;
 159import eu.siacs.conversations.xmpp.jingle.RtpEndUserState;
 160import eu.siacs.conversations.xmpp.mam.MamReference;
 161import eu.siacs.conversations.xmpp.pep.Avatar;
 162import eu.siacs.conversations.xmpp.pep.PublishOptions;
 163import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 164import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
 165import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
 166import me.leolin.shortcutbadger.ShortcutBadger;
 167
 168public class XmppConnectionService extends Service {
 169
 170    public static final String ACTION_REPLY_TO_CONVERSATION = "reply_to_conversations";
 171    public static final String ACTION_MARK_AS_READ = "mark_as_read";
 172    public static final String ACTION_SNOOZE = "snooze";
 173    public static final String ACTION_CLEAR_NOTIFICATION = "clear_notification";
 174    public static final String ACTION_DISMISS_ERROR_NOTIFICATIONS = "dismiss_error";
 175    public static final String ACTION_TRY_AGAIN = "try_again";
 176    public static final String ACTION_IDLE_PING = "idle_ping";
 177    public static final String ACTION_FCM_TOKEN_REFRESH = "fcm_token_refresh";
 178    public static final String ACTION_FCM_MESSAGE_RECEIVED = "fcm_message_received";
 179    public static final String ACTION_DISMISS_CALL = "dismiss_call";
 180    public static final String ACTION_END_CALL = "end_call";
 181    public static final String ACTION_PROVISION_ACCOUNT = "provision_account";
 182    private static final String ACTION_POST_CONNECTIVITY_CHANGE = "eu.siacs.conversations.POST_CONNECTIVITY_CHANGE";
 183
 184    private static final String SETTING_LAST_ACTIVITY_TS = "last_activity_timestamp";
 185
 186    public final CountDownLatch restoredFromDatabaseLatch = new CountDownLatch(1);
 187    private final SerialSingleThreadExecutor mFileAddingExecutor = new SerialSingleThreadExecutor("FileAdding");
 188    private final SerialSingleThreadExecutor mVideoCompressionExecutor = new SerialSingleThreadExecutor("VideoCompression");
 189    private final SerialSingleThreadExecutor mDatabaseWriterExecutor = new SerialSingleThreadExecutor("DatabaseWriter");
 190    private final SerialSingleThreadExecutor mDatabaseReaderExecutor = new SerialSingleThreadExecutor("DatabaseReader");
 191    private final SerialSingleThreadExecutor mNotificationExecutor = new SerialSingleThreadExecutor("NotificationExecutor");
 192    private final ReplacingTaskManager mRosterSyncTaskManager = new ReplacingTaskManager();
 193    private final IBinder mBinder = new XmppConnectionBinder();
 194    private final List<Conversation> conversations = new CopyOnWriteArrayList<>();
 195    private final IqGenerator mIqGenerator = new IqGenerator(this);
 196    private final Set<String> mInProgressAvatarFetches = new HashSet<>();
 197    private final Set<String> mOmittedPepAvatarFetches = new HashSet<>();
 198    private final HashSet<Jid> mLowPingTimeoutMode = new HashSet<>();
 199    private final OnIqPacketReceived mDefaultIqHandler = (account, packet) -> {
 200        if (packet.getType() != IqPacket.TYPE.RESULT) {
 201            Element error = packet.findChild("error");
 202            String text = error != null ? error.findChildContent("text") : null;
 203            if (text != null) {
 204                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received iq error - " + text);
 205            }
 206        }
 207    };
 208    public DatabaseBackend databaseBackend;
 209    private final ReplacingSerialSingleThreadExecutor mContactMergerExecutor = new ReplacingSerialSingleThreadExecutor("ContactMerger");
 210    private long mLastActivity = 0;
 211    private final FileBackend fileBackend = new FileBackend(this);
 212    private MemorizingTrustManager mMemorizingTrustManager;
 213    private final NotificationService mNotificationService = new NotificationService(this);
 214    private final ChannelDiscoveryService mChannelDiscoveryService = new ChannelDiscoveryService(this);
 215    private final ShortcutService mShortcutService = new ShortcutService(this);
 216    private final AtomicBoolean mInitialAddressbookSyncCompleted = new AtomicBoolean(false);
 217    private final AtomicBoolean mForceForegroundService = new AtomicBoolean(false);
 218    private final AtomicBoolean mForceDuringOnCreate = new AtomicBoolean(false);
 219    private final AtomicReference<OngoingCall> ongoingCall = new AtomicReference<>();
 220    private final OnMessagePacketReceived mMessageParser = new MessageParser(this);
 221    private final OnPresencePacketReceived mPresenceParser = new PresenceParser(this);
 222    private final IqParser mIqParser = new IqParser(this);
 223    private final MessageGenerator mMessageGenerator = new MessageGenerator(this);
 224    public OnContactStatusChanged onContactStatusChanged = (contact, online) -> {
 225        Conversation conversation = find(getConversations(), contact);
 226        if (conversation != null) {
 227            if (online) {
 228                if (contact.getPresences().size() == 1) {
 229                    sendUnsentMessages(conversation);
 230                }
 231            }
 232        }
 233    };
 234    private final PresenceGenerator mPresenceGenerator = new PresenceGenerator(this);
 235    private List<Account> accounts;
 236    private final JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager(this);
 237    private final HttpConnectionManager mHttpConnectionManager = new HttpConnectionManager(this);
 238    private final AvatarService mAvatarService = new AvatarService(this);
 239    private final MessageArchiveService mMessageArchiveService = new MessageArchiveService(this);
 240    private final PushManagementService mPushManagementService = new PushManagementService(this);
 241    private final QuickConversationsService mQuickConversationsService = new QuickConversationsService(this);
 242    private final ConversationsFileObserver fileObserver = new ConversationsFileObserver(
 243            Environment.getExternalStorageDirectory().getAbsolutePath()
 244    ) {
 245        @Override
 246        public void onEvent(final int event, final File file) {
 247            markFileDeleted(file);
 248        }
 249    };
 250    private final OnMessageAcknowledged mOnMessageAcknowledgedListener = new OnMessageAcknowledged() {
 251
 252        @Override
 253        public boolean onMessageAcknowledged(final Account account, final Jid to, final String id) {
 254            if (id.startsWith(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX)) {
 255                final String sessionId = id.substring(JingleRtpConnection.JINGLE_MESSAGE_PROPOSE_ID_PREFIX.length());
 256                mJingleConnectionManager.updateProposedSessionDiscovered(
 257                        account,
 258                        to,
 259                        sessionId,
 260                        JingleConnectionManager.DeviceDiscoveryState.SEARCHING_ACKNOWLEDGED
 261                );
 262            }
 263
 264
 265            final Jid bare = to.asBareJid();
 266
 267            for (final Conversation conversation : getConversations()) {
 268                if (conversation.getAccount() == account && conversation.getJid().asBareJid().equals(bare)) {
 269                    final Message message = conversation.findUnsentMessageWithUuid(id);
 270                    if (message != null) {
 271                        message.setStatus(Message.STATUS_SEND);
 272                        message.setErrorMessage(null);
 273                        databaseBackend.updateMessage(message, false);
 274                        return true;
 275                    }
 276                }
 277            }
 278            return false;
 279        }
 280    };
 281    private final AtomicBoolean isPhoneInCall = new AtomicBoolean(false);
 282    private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
 283        @Override
 284        public void onCallStateChanged(final int state, final String phoneNumber) {
 285            isPhoneInCall.set(state != TelephonyManager.CALL_STATE_IDLE);
 286            if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
 287                mJingleConnectionManager.notifyPhoneCallStarted();
 288            }
 289        }
 290    };
 291
 292    private boolean destroyed = false;
 293
 294    private int unreadCount = -1;
 295
 296    //Ui callback listeners
 297    private final Set<OnConversationUpdate> mOnConversationUpdates = Collections.newSetFromMap(new WeakHashMap<OnConversationUpdate, Boolean>());
 298    private final Set<OnShowErrorToast> mOnShowErrorToasts = Collections.newSetFromMap(new WeakHashMap<OnShowErrorToast, Boolean>());
 299    private final Set<OnAccountUpdate> mOnAccountUpdates = Collections.newSetFromMap(new WeakHashMap<OnAccountUpdate, Boolean>());
 300    private final Set<OnCaptchaRequested> mOnCaptchaRequested = Collections.newSetFromMap(new WeakHashMap<OnCaptchaRequested, Boolean>());
 301    private final Set<OnRosterUpdate> mOnRosterUpdates = Collections.newSetFromMap(new WeakHashMap<OnRosterUpdate, Boolean>());
 302    private final Set<OnUpdateBlocklist> mOnUpdateBlocklist = Collections.newSetFromMap(new WeakHashMap<OnUpdateBlocklist, Boolean>());
 303    private final Set<OnMucRosterUpdate> mOnMucRosterUpdate = Collections.newSetFromMap(new WeakHashMap<OnMucRosterUpdate, Boolean>());
 304    private final Set<OnKeyStatusUpdated> mOnKeyStatusUpdated = Collections.newSetFromMap(new WeakHashMap<OnKeyStatusUpdated, Boolean>());
 305    private final Set<OnJingleRtpConnectionUpdate> onJingleRtpConnectionUpdate = Collections.newSetFromMap(new WeakHashMap<OnJingleRtpConnectionUpdate, Boolean>());
 306
 307    private final Object LISTENER_LOCK = new Object();
 308
 309
 310    public final Set<String> FILENAMES_TO_IGNORE_DELETION = new HashSet<>();
 311
 312
 313    private final OnBindListener mOnBindListener = new OnBindListener() {
 314
 315        @Override
 316        public void onBind(final Account account) {
 317            synchronized (mInProgressAvatarFetches) {
 318                for (Iterator<String> iterator = mInProgressAvatarFetches.iterator(); iterator.hasNext(); ) {
 319                    final String KEY = iterator.next();
 320                    if (KEY.startsWith(account.getJid().asBareJid() + "_")) {
 321                        iterator.remove();
 322                    }
 323                }
 324            }
 325            boolean loggedInSuccessfully = account.setOption(Account.OPTION_LOGGED_IN_SUCCESSFULLY, true);
 326            boolean gainedFeature = account.setOption(Account.OPTION_HTTP_UPLOAD_AVAILABLE, account.getXmppConnection().getFeatures().httpUpload(0));
 327            if (loggedInSuccessfully || gainedFeature) {
 328                databaseBackend.updateAccount(account);
 329            }
 330
 331            if (loggedInSuccessfully) {
 332                if (!TextUtils.isEmpty(account.getDisplayName())) {
 333                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": display name wasn't empty on first log in. publishing");
 334                    publishDisplayName(account);
 335                }
 336            }
 337
 338            account.getRoster().clearPresences();
 339            synchronized (account.inProgressConferenceJoins) {
 340                account.inProgressConferenceJoins.clear();
 341            }
 342            synchronized (account.inProgressConferencePings) {
 343                account.inProgressConferencePings.clear();
 344            }
 345            mJingleConnectionManager.notifyRebound(account);
 346            mQuickConversationsService.considerSyncBackground(false);
 347            fetchRosterFromServer(account);
 348
 349            final XmppConnection connection = account.getXmppConnection();
 350
 351            if (connection.getFeatures().bookmarks2()) {
 352                fetchBookmarks2(account);
 353            } else if (!account.getXmppConnection().getFeatures().bookmarksConversion()) {
 354                fetchBookmarks(account);
 355            }
 356            final boolean flexible = account.getXmppConnection().getFeatures().flexibleOfflineMessageRetrieval();
 357            final boolean catchup = getMessageArchiveService().inCatchup(account);
 358            if (flexible && catchup && account.getXmppConnection().isMamPreferenceAlways()) {
 359                sendIqPacket(account, mIqGenerator.purgeOfflineMessages(), (acc, packet) -> {
 360                    if (packet.getType() == IqPacket.TYPE.RESULT) {
 361                        Log.d(Config.LOGTAG, acc.getJid().asBareJid() + ": successfully purged offline messages");
 362                    }
 363                });
 364            }
 365            sendPresence(account);
 366            if (mPushManagementService.available(account)) {
 367                mPushManagementService.registerPushTokenOnServer(account);
 368            }
 369            connectMultiModeConversations(account);
 370            syncDirtyContacts(account);
 371
 372        }
 373    };
 374    private final AtomicLong mLastExpiryRun = new AtomicLong(0);
 375    private SecureRandom mRandom;
 376    private final LruCache<Pair<String, String>, ServiceDiscoveryResult> discoCache = new LruCache<>(20);
 377    private final OnStatusChanged statusListener = new OnStatusChanged() {
 378
 379        @Override
 380        public void onStatusChanged(final Account account) {
 381            XmppConnection connection = account.getXmppConnection();
 382            updateAccountUi();
 383
 384            if (account.getStatus() == Account.State.ONLINE || account.getStatus().isError()) {
 385                mQuickConversationsService.signalAccountStateChange();
 386            }
 387
 388            if (account.getStatus() == Account.State.ONLINE) {
 389                synchronized (mLowPingTimeoutMode) {
 390                    if (mLowPingTimeoutMode.remove(account.getJid().asBareJid())) {
 391                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": leaving low ping timeout mode");
 392                    }
 393                }
 394                if (account.setShowErrorNotification(true)) {
 395                    databaseBackend.updateAccount(account);
 396                }
 397                mMessageArchiveService.executePendingQueries(account);
 398                if (connection != null && connection.getFeatures().csi()) {
 399                    if (checkListeners()) {
 400                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + " sending csi//inactive");
 401                        connection.sendInactive();
 402                    } else {
 403                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + " sending csi//active");
 404                        connection.sendActive();
 405                    }
 406                }
 407                List<Conversation> conversations = getConversations();
 408                for (Conversation conversation : conversations) {
 409                    final boolean inProgressJoin;
 410                    synchronized (account.inProgressConferenceJoins) {
 411                        inProgressJoin = account.inProgressConferenceJoins.contains(conversation);
 412                    }
 413                    final boolean pendingJoin;
 414                    synchronized (account.pendingConferenceJoins) {
 415                        pendingJoin = account.pendingConferenceJoins.contains(conversation);
 416                    }
 417                    if (conversation.getAccount() == account
 418                            && !pendingJoin
 419                            && !inProgressJoin) {
 420                        sendUnsentMessages(conversation);
 421                    }
 422                }
 423                final List<Conversation> pendingLeaves;
 424                synchronized (account.pendingConferenceLeaves) {
 425                    pendingLeaves = new ArrayList<>(account.pendingConferenceLeaves);
 426                    account.pendingConferenceLeaves.clear();
 427
 428                }
 429                for (Conversation conversation : pendingLeaves) {
 430                    leaveMuc(conversation);
 431                }
 432                final List<Conversation> pendingJoins;
 433                synchronized (account.pendingConferenceJoins) {
 434                    pendingJoins = new ArrayList<>(account.pendingConferenceJoins);
 435                    account.pendingConferenceJoins.clear();
 436                }
 437                for (Conversation conversation : pendingJoins) {
 438                    joinMuc(conversation);
 439                }
 440                scheduleWakeUpCall(Config.PING_MAX_INTERVAL, account.getUuid().hashCode());
 441            } else if (account.getStatus() == Account.State.OFFLINE || account.getStatus() == Account.State.DISABLED) {
 442                resetSendingToWaiting(account);
 443                if (account.isEnabled() && isInLowPingTimeoutMode(account)) {
 444                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": went into offline state during low ping mode. reconnecting now");
 445                    reconnectAccount(account, true, false);
 446                } else {
 447                    int timeToReconnect = mRandom.nextInt(10) + 2;
 448                    scheduleWakeUpCall(timeToReconnect, account.getUuid().hashCode());
 449                }
 450            } else if (account.getStatus() == Account.State.REGISTRATION_SUCCESSFUL) {
 451                databaseBackend.updateAccount(account);
 452                reconnectAccount(account, true, false);
 453            } else if (account.getStatus() != Account.State.CONNECTING && account.getStatus() != Account.State.NO_INTERNET) {
 454                resetSendingToWaiting(account);
 455                if (connection != null && account.getStatus().isAttemptReconnect()) {
 456                    final int next = connection.getTimeToNextAttempt();
 457                    final boolean lowPingTimeoutMode = isInLowPingTimeoutMode(account);
 458                    if (next <= 0) {
 459                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": error connecting account. reconnecting now. lowPingTimeout=" + lowPingTimeoutMode);
 460                        reconnectAccount(account, true, false);
 461                    } else {
 462                        final int attempt = connection.getAttempt() + 1;
 463                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": error connecting account. try again in " + next + "s for the " + attempt + " time. lowPingTimeout=" + lowPingTimeoutMode);
 464                        scheduleWakeUpCall(next, account.getUuid().hashCode());
 465                    }
 466                }
 467            }
 468            getNotificationService().updateErrorNotification();
 469        }
 470    };
 471    private OpenPgpServiceConnection pgpServiceConnection;
 472    private PgpEngine mPgpEngine = null;
 473    private WakeLock wakeLock;
 474    private PowerManager pm;
 475    private LruCache<String, Bitmap> mBitmapCache;
 476    private final BroadcastReceiver mInternalEventReceiver = new InternalEventReceiver();
 477    private final BroadcastReceiver mInternalScreenEventReceiver = new InternalEventReceiver();
 478
 479    private static String generateFetchKey(Account account, final Avatar avatar) {
 480        return account.getJid().asBareJid() + "_" + avatar.owner + "_" + avatar.sha1sum;
 481    }
 482
 483    private boolean isInLowPingTimeoutMode(Account account) {
 484        synchronized (mLowPingTimeoutMode) {
 485            return mLowPingTimeoutMode.contains(account.getJid().asBareJid());
 486        }
 487    }
 488
 489    public void startForcingForegroundNotification() {
 490        mForceForegroundService.set(true);
 491        toggleForegroundService();
 492    }
 493
 494    public void stopForcingForegroundNotification() {
 495        mForceForegroundService.set(false);
 496        toggleForegroundService();
 497    }
 498
 499    public boolean areMessagesInitialized() {
 500        return this.restoredFromDatabaseLatch.getCount() == 0;
 501    }
 502
 503    public PgpEngine getPgpEngine() {
 504        if (!Config.supportOpenPgp()) {
 505            return null;
 506        } else if (pgpServiceConnection != null && pgpServiceConnection.isBound()) {
 507            if (this.mPgpEngine == null) {
 508                this.mPgpEngine = new PgpEngine(new OpenPgpApi(
 509                        getApplicationContext(),
 510                        pgpServiceConnection.getService()), this);
 511            }
 512            return mPgpEngine;
 513        } else {
 514            return null;
 515        }
 516
 517    }
 518
 519    public OpenPgpApi getOpenPgpApi() {
 520        if (!Config.supportOpenPgp()) {
 521            return null;
 522        } else if (pgpServiceConnection != null && pgpServiceConnection.isBound()) {
 523            return new OpenPgpApi(this, pgpServiceConnection.getService());
 524        } else {
 525            return null;
 526        }
 527    }
 528
 529    public FileBackend getFileBackend() {
 530        return this.fileBackend;
 531    }
 532
 533    public AvatarService getAvatarService() {
 534        return this.mAvatarService;
 535    }
 536
 537    public void attachLocationToConversation(final Conversation conversation, final Uri uri, final UiCallback<Message> callback) {
 538        int encryption = conversation.getNextEncryption();
 539        if (encryption == Message.ENCRYPTION_PGP) {
 540            encryption = Message.ENCRYPTION_DECRYPTED;
 541        }
 542        Message message = new Message(conversation, uri.toString(), encryption);
 543        Message.configurePrivateMessage(message);
 544        if (encryption == Message.ENCRYPTION_DECRYPTED) {
 545            getPgpEngine().encrypt(message, callback);
 546        } else {
 547            sendMessage(message);
 548            callback.success(message);
 549        }
 550    }
 551
 552    public void attachFileToConversation(final Conversation conversation, final Uri uri, final String type, final UiCallback<Message> callback) {
 553        final Message message;
 554        if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
 555            message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
 556        } else {
 557            message = new Message(conversation, "", conversation.getNextEncryption());
 558        }
 559        if (!Message.configurePrivateFileMessage(message)) {
 560            message.setCounterpart(conversation.getNextCounterpart());
 561            message.setType(Message.TYPE_FILE);
 562        }
 563        Log.d(Config.LOGTAG, "attachFile: type=" + message.getType());
 564        Log.d(Config.LOGTAG, "counterpart=" + message.getCounterpart());
 565        final AttachFileToConversationRunnable runnable = new AttachFileToConversationRunnable(this, uri, type, message, callback);
 566        if (runnable.isVideoMessage()) {
 567            mVideoCompressionExecutor.execute(runnable);
 568        } else {
 569            mFileAddingExecutor.execute(runnable);
 570        }
 571    }
 572
 573    public void attachImageToConversation(final Conversation conversation, final Uri uri, final UiCallback<Message> callback) {
 574        final String mimeType = MimeUtils.guessMimeTypeFromUri(this, uri);
 575        final String compressPictures = getCompressPicturesPreference();
 576
 577        if ("never".equals(compressPictures)
 578                || ("auto".equals(compressPictures) && getFileBackend().useImageAsIs(uri))
 579                || (mimeType != null && mimeType.endsWith("/gif"))
 580                || getFileBackend().unusualBounds(uri)) {
 581            Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": not compressing picture. sending as file");
 582            attachFileToConversation(conversation, uri, mimeType, callback);
 583            return;
 584        }
 585        final Message message;
 586        if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
 587            message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
 588        } else {
 589            message = new Message(conversation, "", conversation.getNextEncryption());
 590        }
 591        if (!Message.configurePrivateFileMessage(message)) {
 592            message.setCounterpart(conversation.getNextCounterpart());
 593            message.setType(Message.TYPE_IMAGE);
 594        }
 595        Log.d(Config.LOGTAG, "attachImage: type=" + message.getType());
 596        mFileAddingExecutor.execute(() -> {
 597            try {
 598                getFileBackend().copyImageToPrivateStorage(message, uri);
 599            } catch (FileBackend.ImageCompressionException e) {
 600                Log.d(Config.LOGTAG, "unable to compress image. fall back to file transfer", e);
 601                attachFileToConversation(conversation, uri, mimeType, callback);
 602                return;
 603            } catch (final FileBackend.FileCopyException e) {
 604                callback.error(e.getResId(), message);
 605                return;
 606            }
 607            if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
 608                final PgpEngine pgpEngine = getPgpEngine();
 609                if (pgpEngine != null) {
 610                    pgpEngine.encrypt(message, callback);
 611                } else if (callback != null) {
 612                    callback.error(R.string.unable_to_connect_to_keychain, null);
 613                }
 614            } else {
 615                sendMessage(message);
 616                callback.success(message);
 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    public boolean isMuc(final Account account, final Jid jid) {
 630        final Conversation c = find(account, jid);
 631        return c != null && c.getMode() == Conversational.MODE_MULTI;
 632    }
 633
 634    public void search(final List<String> term, final String uuid, final OnSearchResultsAvailable onSearchResultsAvailable) {
 635        MessageSearchTask.search(this, term, uuid, onSearchResultsAvailable);
 636    }
 637
 638    @Override
 639    public int onStartCommand(Intent intent, int flags, int startId) {
 640        final String action = intent == null ? null : intent.getAction();
 641        final boolean needsForegroundService = intent != null && intent.getBooleanExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, false);
 642        if (needsForegroundService) {
 643            Log.d(Config.LOGTAG, "toggle forced foreground service after receiving event (action=" + action + ")");
 644            toggleForegroundService(true);
 645        }
 646        String pushedAccountHash = null;
 647        boolean interactive = false;
 648        if (action != null) {
 649            final String uuid = intent.getStringExtra("uuid");
 650            switch (action) {
 651                case QuickConversationsService.SMS_RETRIEVED_ACTION:
 652                    mQuickConversationsService.handleSmsReceived(intent);
 653                    break;
 654                case ConnectivityManager.CONNECTIVITY_ACTION:
 655                    if (hasInternetConnection()) {
 656                        if (Config.POST_CONNECTIVITY_CHANGE_PING_INTERVAL > 0) {
 657                            schedulePostConnectivityChange();
 658                        }
 659                        if (Config.RESET_ATTEMPT_COUNT_ON_NETWORK_CHANGE) {
 660                            resetAllAttemptCounts(true, false);
 661                        }
 662                        Resolver.clearCache();
 663                    }
 664                    break;
 665                case Intent.ACTION_SHUTDOWN:
 666                    logoutAndSave(true);
 667                    return START_NOT_STICKY;
 668                case ACTION_CLEAR_NOTIFICATION:
 669                    mNotificationExecutor.execute(() -> {
 670                        try {
 671                            final Conversation c = findConversationByUuid(uuid);
 672                            if (c != null) {
 673                                mNotificationService.clear(c);
 674                            } else {
 675                                mNotificationService.clear();
 676                            }
 677                            restoredFromDatabaseLatch.await();
 678
 679                        } catch (InterruptedException e) {
 680                            Log.d(Config.LOGTAG, "unable to process clear notification");
 681                        }
 682                    });
 683                    break;
 684                case ACTION_DISMISS_CALL: {
 685                    final String sessionId = intent.getStringExtra(RtpSessionActivity.EXTRA_SESSION_ID);
 686                    Log.d(Config.LOGTAG, "received intent to dismiss call with session id " + sessionId);
 687                    mJingleConnectionManager.rejectRtpSession(sessionId);
 688                    break;
 689                }
 690                case TorServiceUtils.ACTION_STATUS:
 691                    final String status = intent.getStringExtra(TorServiceUtils.EXTRA_STATUS);
 692                    //TODO port and host are in 'extras' - but this may not be a reliable source?
 693                    if ("ON".equals(status)) {
 694                        handleOrbotStartedEvent();
 695                        return START_STICKY;
 696                    }
 697                    break;
 698                case ACTION_END_CALL: {
 699                    final String sessionId = intent.getStringExtra(RtpSessionActivity.EXTRA_SESSION_ID);
 700                    Log.d(Config.LOGTAG, "received intent to end call with session id " + sessionId);
 701                    mJingleConnectionManager.endRtpSession(sessionId);
 702                }
 703                break;
 704                case ACTION_PROVISION_ACCOUNT: {
 705                    final String address = intent.getStringExtra("address");
 706                    final String password = intent.getStringExtra("password");
 707                    if (QuickConversationsService.isQuicksy() || Strings.isNullOrEmpty(address) || Strings.isNullOrEmpty(password)) {
 708                        break;
 709                    }
 710                    provisionAccount(address, password);
 711                    break;
 712                }
 713                case ACTION_DISMISS_ERROR_NOTIFICATIONS:
 714                    dismissErrorNotifications();
 715                    break;
 716                case ACTION_TRY_AGAIN:
 717                    resetAllAttemptCounts(false, true);
 718                    interactive = true;
 719                    break;
 720                case ACTION_REPLY_TO_CONVERSATION:
 721                    Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
 722                    if (remoteInput == null) {
 723                        break;
 724                    }
 725                    final CharSequence body = remoteInput.getCharSequence("text_reply");
 726                    final boolean dismissNotification = intent.getBooleanExtra("dismiss_notification", false);
 727                    if (body == null || body.length() <= 0) {
 728                        break;
 729                    }
 730                    mNotificationExecutor.execute(() -> {
 731                        try {
 732                            restoredFromDatabaseLatch.await();
 733                            final Conversation c = findConversationByUuid(uuid);
 734                            if (c != null) {
 735                                directReply(c, body.toString(), dismissNotification);
 736                            }
 737                        } catch (InterruptedException e) {
 738                            Log.d(Config.LOGTAG, "unable to process direct reply");
 739                        }
 740                    });
 741                    break;
 742                case ACTION_MARK_AS_READ:
 743                    mNotificationExecutor.execute(() -> {
 744                        final Conversation c = findConversationByUuid(uuid);
 745                        if (c == null) {
 746                            Log.d(Config.LOGTAG, "received mark read intent for unknown conversation (" + uuid + ")");
 747                            return;
 748                        }
 749                        try {
 750                            restoredFromDatabaseLatch.await();
 751                            sendReadMarker(c, null);
 752                        } catch (InterruptedException e) {
 753                            Log.d(Config.LOGTAG, "unable to process notification read marker for conversation " + c.getName());
 754                        }
 755
 756                    });
 757                    break;
 758                case ACTION_SNOOZE:
 759                    mNotificationExecutor.execute(() -> {
 760                        final Conversation c = findConversationByUuid(uuid);
 761                        if (c == null) {
 762                            Log.d(Config.LOGTAG, "received snooze intent for unknown conversation (" + uuid + ")");
 763                            return;
 764                        }
 765                        c.setMutedTill(System.currentTimeMillis() + 30 * 60 * 1000);
 766                        mNotificationService.clear(c);
 767                        updateConversation(c);
 768                    });
 769                case AudioManager.RINGER_MODE_CHANGED_ACTION:
 770                case NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED:
 771                    if (dndOnSilentMode()) {
 772                        refreshAllPresences();
 773                    }
 774                    break;
 775                case Intent.ACTION_SCREEN_ON:
 776                    deactivateGracePeriod();
 777                case Intent.ACTION_USER_PRESENT:
 778                case Intent.ACTION_SCREEN_OFF:
 779                    if (awayWhenScreenLocked()) {
 780                        refreshAllPresences();
 781                    }
 782                    break;
 783                case ACTION_FCM_TOKEN_REFRESH:
 784                    refreshAllFcmTokens();
 785                    break;
 786                case ACTION_IDLE_PING:
 787                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
 788                        scheduleNextIdlePing();
 789                    }
 790                    break;
 791                case ACTION_FCM_MESSAGE_RECEIVED:
 792                    pushedAccountHash = intent.getStringExtra("account");
 793                    Log.d(Config.LOGTAG, "push message arrived in service. account=" + pushedAccountHash);
 794                    break;
 795                case Intent.ACTION_SEND:
 796                    Uri uri = intent.getData();
 797                    if (uri != null) {
 798                        Log.d(Config.LOGTAG, "received uri permission for " + uri.toString());
 799                    }
 800                    return START_STICKY;
 801            }
 802        }
 803        synchronized (this) {
 804            WakeLockHelper.acquire(wakeLock);
 805            boolean pingNow = ConnectivityManager.CONNECTIVITY_ACTION.equals(action) || (Config.POST_CONNECTIVITY_CHANGE_PING_INTERVAL > 0 && ACTION_POST_CONNECTIVITY_CHANGE.equals(action));
 806            final HashSet<Account> pingCandidates = new HashSet<>();
 807            final String androidId = PhoneHelper.getAndroidId(this);
 808            for (Account account : accounts) {
 809                final boolean pushWasMeantForThisAccount = CryptoHelper.getAccountFingerprint(account, androidId).equals(pushedAccountHash);
 810                pingNow |= processAccountState(account,
 811                        interactive,
 812                        "ui".equals(action),
 813                        pushWasMeantForThisAccount,
 814                        pingCandidates);
 815            }
 816            if (pingNow) {
 817                for (Account account : pingCandidates) {
 818                    final boolean lowTimeout = isInLowPingTimeoutMode(account);
 819                    account.getXmppConnection().sendPing();
 820                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + " send ping (action=" + action + ",lowTimeout=" + lowTimeout + ")");
 821                    scheduleWakeUpCall(lowTimeout ? Config.LOW_PING_TIMEOUT : Config.PING_TIMEOUT, account.getUuid().hashCode());
 822                }
 823            }
 824            WakeLockHelper.release(wakeLock);
 825        }
 826        if (SystemClock.elapsedRealtime() - mLastExpiryRun.get() >= Config.EXPIRY_INTERVAL) {
 827            expireOldMessages();
 828        }
 829        return START_STICKY;
 830    }
 831
 832    private void handleOrbotStartedEvent() {
 833        for (final Account account : accounts) {
 834            if (account.getStatus() == Account.State.TOR_NOT_AVAILABLE) {
 835                reconnectAccount(account, true, false);
 836            }
 837        }
 838    }
 839
 840    private boolean processAccountState(Account account, boolean interactive, boolean isUiAction, boolean isAccountPushed, HashSet<Account> pingCandidates) {
 841        boolean pingNow = false;
 842        if (account.getStatus().isAttemptReconnect()) {
 843            if (!hasInternetConnection()) {
 844                account.setStatus(Account.State.NO_INTERNET);
 845                if (statusListener != null) {
 846                    statusListener.onStatusChanged(account);
 847                }
 848            } else {
 849                if (account.getStatus() == Account.State.NO_INTERNET) {
 850                    account.setStatus(Account.State.OFFLINE);
 851                    if (statusListener != null) {
 852                        statusListener.onStatusChanged(account);
 853                    }
 854                }
 855                if (account.getStatus() == Account.State.ONLINE) {
 856                    synchronized (mLowPingTimeoutMode) {
 857                        long lastReceived = account.getXmppConnection().getLastPacketReceived();
 858                        long lastSent = account.getXmppConnection().getLastPingSent();
 859                        long pingInterval = isUiAction ? Config.PING_MIN_INTERVAL * 1000 : Config.PING_MAX_INTERVAL * 1000;
 860                        long msToNextPing = (Math.max(lastReceived, lastSent) + pingInterval) - SystemClock.elapsedRealtime();
 861                        int pingTimeout = mLowPingTimeoutMode.contains(account.getJid().asBareJid()) ? Config.LOW_PING_TIMEOUT * 1000 : Config.PING_TIMEOUT * 1000;
 862                        long pingTimeoutIn = (lastSent + pingTimeout) - SystemClock.elapsedRealtime();
 863                        if (lastSent > lastReceived) {
 864                            if (pingTimeoutIn < 0) {
 865                                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ping timeout");
 866                                this.reconnectAccount(account, true, interactive);
 867                            } else {
 868                                int secs = (int) (pingTimeoutIn / 1000);
 869                                this.scheduleWakeUpCall(secs, account.getUuid().hashCode());
 870                            }
 871                        } else {
 872                            pingCandidates.add(account);
 873                            if (isAccountPushed) {
 874                                pingNow = true;
 875                                if (mLowPingTimeoutMode.add(account.getJid().asBareJid())) {
 876                                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": entering low ping timeout mode");
 877                                }
 878                            } else if (msToNextPing <= 0) {
 879                                pingNow = true;
 880                            } else {
 881                                this.scheduleWakeUpCall((int) (msToNextPing / 1000), account.getUuid().hashCode());
 882                                if (mLowPingTimeoutMode.remove(account.getJid().asBareJid())) {
 883                                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": leaving low ping timeout mode");
 884                                }
 885                            }
 886                        }
 887                    }
 888                } else if (account.getStatus() == Account.State.OFFLINE) {
 889                    reconnectAccount(account, true, interactive);
 890                } else if (account.getStatus() == Account.State.CONNECTING) {
 891                    long secondsSinceLastConnect = (SystemClock.elapsedRealtime() - account.getXmppConnection().getLastConnect()) / 1000;
 892                    long secondsSinceLastDisco = (SystemClock.elapsedRealtime() - account.getXmppConnection().getLastDiscoStarted()) / 1000;
 893                    long discoTimeout = Config.CONNECT_DISCO_TIMEOUT - secondsSinceLastDisco;
 894                    long timeout = Config.CONNECT_TIMEOUT - secondsSinceLastConnect;
 895                    if (timeout < 0) {
 896                        Log.d(Config.LOGTAG, account.getJid() + ": time out during connect reconnecting (secondsSinceLast=" + secondsSinceLastConnect + ")");
 897                        account.getXmppConnection().resetAttemptCount(false);
 898                        reconnectAccount(account, true, interactive);
 899                    } else if (discoTimeout < 0) {
 900                        account.getXmppConnection().sendDiscoTimeout();
 901                        scheduleWakeUpCall((int) Math.min(timeout, discoTimeout), account.getUuid().hashCode());
 902                    } else {
 903                        scheduleWakeUpCall((int) Math.min(timeout, discoTimeout), account.getUuid().hashCode());
 904                    }
 905                } else {
 906                    if (account.getXmppConnection().getTimeToNextAttempt() <= 0) {
 907                        reconnectAccount(account, true, interactive);
 908                    }
 909                }
 910            }
 911        }
 912        return pingNow;
 913    }
 914
 915    public void reinitializeMuclumbusService() {
 916        mChannelDiscoveryService.initializeMuclumbusService();
 917    }
 918
 919    public void discoverChannels(String query, ChannelDiscoveryService.Method method, ChannelDiscoveryService.OnChannelSearchResultsFound onChannelSearchResultsFound) {
 920        mChannelDiscoveryService.discover(Strings.nullToEmpty(query).trim(), method, onChannelSearchResultsFound);
 921    }
 922
 923    public boolean isDataSaverDisabled() {
 924        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
 925            ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
 926            return !connectivityManager.isActiveNetworkMetered()
 927                    || connectivityManager.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED;
 928        } else {
 929            return true;
 930        }
 931    }
 932
 933    private void directReply(Conversation conversation, String body, final boolean dismissAfterReply) {
 934        Message message = new Message(conversation, body, conversation.getNextEncryption());
 935        message.markUnread();
 936        if (message.getEncryption() == Message.ENCRYPTION_PGP) {
 937            getPgpEngine().encrypt(message, new UiCallback<Message>() {
 938                @Override
 939                public void success(Message message) {
 940                    if (dismissAfterReply) {
 941                        markRead((Conversation) message.getConversation(), true);
 942                    } else {
 943                        mNotificationService.pushFromDirectReply(message);
 944                    }
 945                }
 946
 947                @Override
 948                public void error(int errorCode, Message object) {
 949
 950                }
 951
 952                @Override
 953                public void userInputRequired(PendingIntent pi, Message object) {
 954
 955                }
 956            });
 957        } else {
 958            sendMessage(message);
 959            if (dismissAfterReply) {
 960                markRead(conversation, true);
 961            } else {
 962                mNotificationService.pushFromDirectReply(message);
 963            }
 964        }
 965    }
 966
 967    private boolean dndOnSilentMode() {
 968        return getBooleanPreference(SettingsActivity.DND_ON_SILENT_MODE, R.bool.dnd_on_silent_mode);
 969    }
 970
 971    private boolean manuallyChangePresence() {
 972        return getBooleanPreference(SettingsActivity.MANUALLY_CHANGE_PRESENCE, R.bool.manually_change_presence);
 973    }
 974
 975    private boolean treatVibrateAsSilent() {
 976        return getBooleanPreference(SettingsActivity.TREAT_VIBRATE_AS_SILENT, R.bool.treat_vibrate_as_silent);
 977    }
 978
 979    private boolean awayWhenScreenLocked() {
 980        return getBooleanPreference(SettingsActivity.AWAY_WHEN_SCREEN_IS_OFF, R.bool.away_when_screen_off);
 981    }
 982
 983    private String getCompressPicturesPreference() {
 984        return getPreferences().getString("picture_compression", getResources().getString(R.string.picture_compression));
 985    }
 986
 987    private Presence.Status getTargetPresence() {
 988        if (dndOnSilentMode() && isPhoneSilenced()) {
 989            return Presence.Status.DND;
 990        } else if (awayWhenScreenLocked() && isScreenLocked()) {
 991            return Presence.Status.AWAY;
 992        } else {
 993            return Presence.Status.ONLINE;
 994        }
 995    }
 996
 997    public boolean isScreenLocked() {
 998        final KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
 999        final PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
1000        final boolean locked = keyguardManager != null && keyguardManager.isKeyguardLocked();
1001        final boolean interactive = powerManager != null && powerManager.isInteractive();
1002        return locked || !interactive;
1003    }
1004
1005    private boolean isPhoneSilenced() {
1006        final boolean notificationDnd;
1007        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
1008            final NotificationManager notificationManager = getSystemService(NotificationManager.class);
1009            final int filter = notificationManager == null ? NotificationManager.INTERRUPTION_FILTER_UNKNOWN : notificationManager.getCurrentInterruptionFilter();
1010            notificationDnd = filter >= NotificationManager.INTERRUPTION_FILTER_PRIORITY;
1011        } else {
1012            notificationDnd = false;
1013        }
1014        final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1015        final int ringerMode = audioManager == null ? AudioManager.RINGER_MODE_NORMAL : audioManager.getRingerMode();
1016        try {
1017            if (treatVibrateAsSilent()) {
1018                return notificationDnd || ringerMode != AudioManager.RINGER_MODE_NORMAL;
1019            } else {
1020                return notificationDnd || ringerMode == AudioManager.RINGER_MODE_SILENT;
1021            }
1022        } catch (Throwable throwable) {
1023            Log.d(Config.LOGTAG, "platform bug in isPhoneSilenced (" + throwable.getMessage() + ")");
1024            return notificationDnd;
1025        }
1026    }
1027
1028    private void resetAllAttemptCounts(boolean reallyAll, boolean retryImmediately) {
1029        Log.d(Config.LOGTAG, "resetting all attempt counts");
1030        for (Account account : accounts) {
1031            if (account.hasErrorStatus() || reallyAll) {
1032                final XmppConnection connection = account.getXmppConnection();
1033                if (connection != null) {
1034                    connection.resetAttemptCount(retryImmediately);
1035                }
1036            }
1037            if (account.setShowErrorNotification(true)) {
1038                mDatabaseWriterExecutor.execute(() -> databaseBackend.updateAccount(account));
1039            }
1040        }
1041        mNotificationService.updateErrorNotification();
1042    }
1043
1044    private void dismissErrorNotifications() {
1045        for (final Account account : this.accounts) {
1046            if (account.hasErrorStatus()) {
1047                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": dismissing error notification");
1048                if (account.setShowErrorNotification(false)) {
1049                    mDatabaseWriterExecutor.execute(() -> databaseBackend.updateAccount(account));
1050                }
1051            }
1052        }
1053    }
1054
1055    private void expireOldMessages() {
1056        expireOldMessages(false);
1057    }
1058
1059    public void expireOldMessages(final boolean resetHasMessagesLeftOnServer) {
1060        mLastExpiryRun.set(SystemClock.elapsedRealtime());
1061        mDatabaseWriterExecutor.execute(() -> {
1062            long timestamp = getAutomaticMessageDeletionDate();
1063            if (timestamp > 0) {
1064                databaseBackend.expireOldMessages(timestamp);
1065                synchronized (XmppConnectionService.this.conversations) {
1066                    for (Conversation conversation : XmppConnectionService.this.conversations) {
1067                        conversation.expireOldMessages(timestamp);
1068                        if (resetHasMessagesLeftOnServer) {
1069                            conversation.messagesLoaded.set(true);
1070                            conversation.setHasMessagesLeftOnServer(true);
1071                        }
1072                    }
1073                }
1074                updateConversationUi();
1075            }
1076        });
1077    }
1078
1079    public boolean hasInternetConnection() {
1080        final ConnectivityManager cm = ContextCompat.getSystemService(this, ConnectivityManager.class);
1081        if (cm == null) {
1082            return true; //if internet connection can not be checked it is probably best to just try
1083        }
1084        try {
1085            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
1086                final Network activeNetwork = cm.getActiveNetwork();
1087                final NetworkCapabilities capabilities = activeNetwork == null ? null : cm.getNetworkCapabilities(activeNetwork);
1088                return capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
1089            } else {
1090                final NetworkInfo networkInfo = cm.getActiveNetworkInfo();
1091                return networkInfo != null && (networkInfo.isConnected() || networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET);
1092            }
1093        } catch (final RuntimeException e) {
1094            Log.d(Config.LOGTAG, "unable to check for internet connection", e);
1095            return true; //if internet connection can not be checked it is probably best to just try
1096        }
1097    }
1098
1099    @SuppressLint("TrulyRandom")
1100    @Override
1101    public void onCreate() {
1102        if (Compatibility.runsTwentySix()) {
1103            mNotificationService.initializeChannels();
1104        }
1105        mChannelDiscoveryService.initializeMuclumbusService();
1106        mForceDuringOnCreate.set(Compatibility.runsAndTargetsTwentySix(this));
1107        toggleForegroundService();
1108        this.destroyed = false;
1109        OmemoSetting.load(this);
1110        ExceptionHelper.init(getApplicationContext());
1111        try {
1112            Security.insertProviderAt(Conscrypt.newProvider(), 1);
1113        } catch (Throwable throwable) {
1114            Log.e(Config.LOGTAG, "unable to initialize security provider", throwable);
1115        }
1116        Resolver.init(this);
1117        this.mRandom = new SecureRandom();
1118        updateMemorizingTrustmanager();
1119        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
1120        final int cacheSize = maxMemory / 8;
1121        this.mBitmapCache = new LruCache<String, Bitmap>(cacheSize) {
1122            @Override
1123            protected int sizeOf(final String key, final Bitmap bitmap) {
1124                return bitmap.getByteCount() / 1024;
1125            }
1126        };
1127        if (mLastActivity == 0) {
1128            mLastActivity = getPreferences().getLong(SETTING_LAST_ACTIVITY_TS, System.currentTimeMillis());
1129        }
1130
1131        Log.d(Config.LOGTAG, "initializing database...");
1132        this.databaseBackend = DatabaseBackend.getInstance(getApplicationContext());
1133        Log.d(Config.LOGTAG, "restoring accounts...");
1134        this.accounts = databaseBackend.getAccounts();
1135        final SharedPreferences.Editor editor = getPreferences().edit();
1136        if (this.accounts.size() == 0 && Arrays.asList("Sony", "Sony Ericsson").contains(Build.MANUFACTURER)) {
1137            editor.putBoolean(SettingsActivity.KEEP_FOREGROUND_SERVICE, true);
1138            Log.d(Config.LOGTAG, Build.MANUFACTURER + " is on blacklist. enabling foreground service");
1139        }
1140        final boolean hasEnabledAccounts = hasEnabledAccounts();
1141        editor.putBoolean(EventReceiver.SETTING_ENABLED_ACCOUNTS, hasEnabledAccounts).apply();
1142        editor.apply();
1143        toggleSetProfilePictureActivity(hasEnabledAccounts);
1144
1145        restoreFromDatabase();
1146
1147        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
1148            startContactObserver();
1149        }
1150        mFileAddingExecutor.execute(fileBackend::deleteHistoricAvatarPath);
1151        if (Compatibility.hasStoragePermission(this)) {
1152            Log.d(Config.LOGTAG, "starting file observer");
1153            mFileAddingExecutor.execute(this.fileObserver::startWatching);
1154            mFileAddingExecutor.execute(this::checkForDeletedFiles);
1155        }
1156        if (Config.supportOpenPgp()) {
1157            this.pgpServiceConnection = new OpenPgpServiceConnection(this, "org.sufficientlysecure.keychain", new OpenPgpServiceConnection.OnBound() {
1158                @Override
1159                public void onBound(IOpenPgpService2 service) {
1160                    for (Account account : accounts) {
1161                        final PgpDecryptionService pgp = account.getPgpDecryptionService();
1162                        if (pgp != null) {
1163                            pgp.continueDecryption(true);
1164                        }
1165                    }
1166                }
1167
1168                @Override
1169                public void onError(Exception e) {
1170                }
1171            });
1172            this.pgpServiceConnection.bindToService();
1173        }
1174
1175        this.pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
1176        this.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Conversations:Service");
1177
1178        toggleForegroundService();
1179        updateUnreadCountBadge();
1180        toggleScreenEventReceiver();
1181        final IntentFilter intentFilter = new IntentFilter();
1182        intentFilter.addAction(TorServiceUtils.ACTION_STATUS);
1183        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
1184            scheduleNextIdlePing();
1185            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
1186                intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
1187            }
1188            intentFilter.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED);
1189        }
1190        registerReceiver(this.mInternalEventReceiver, intentFilter);
1191        mForceDuringOnCreate.set(false);
1192        toggleForegroundService();
1193        setupPhoneStateListener();
1194    }
1195
1196
1197    private void setupPhoneStateListener() {
1198        final TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
1199        if (telephonyManager != null) {
1200            telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
1201        }
1202    }
1203
1204    public boolean isPhoneInCall() {
1205        return isPhoneInCall.get();
1206    }
1207
1208    private void checkForDeletedFiles() {
1209        if (destroyed) {
1210            Log.d(Config.LOGTAG, "Do not check for deleted files because service has been destroyed");
1211            return;
1212        }
1213        final long start = SystemClock.elapsedRealtime();
1214        final List<DatabaseBackend.FilePathInfo> relativeFilePaths = databaseBackend.getFilePathInfo();
1215        final List<DatabaseBackend.FilePathInfo> changed = new ArrayList<>();
1216        for (final DatabaseBackend.FilePathInfo filePath : relativeFilePaths) {
1217            if (destroyed) {
1218                Log.d(Config.LOGTAG, "Stop checking for deleted files because service has been destroyed");
1219                return;
1220            }
1221            final File file = fileBackend.getFileForPath(filePath.path);
1222            if (filePath.setDeleted(!file.exists())) {
1223                changed.add(filePath);
1224            }
1225        }
1226        final long duration = SystemClock.elapsedRealtime() - start;
1227        Log.d(Config.LOGTAG, "found " + changed.size() + " changed files on start up. total=" + relativeFilePaths.size() + ". (" + duration + "ms)");
1228        if (changed.size() > 0) {
1229            databaseBackend.markFilesAsChanged(changed);
1230            markChangedFiles(changed);
1231        }
1232    }
1233
1234    public void startContactObserver() {
1235        getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, new ContentObserver(null) {
1236            @Override
1237            public void onChange(boolean selfChange) {
1238                super.onChange(selfChange);
1239                if (restoredFromDatabaseLatch.getCount() == 0) {
1240                    loadPhoneContacts();
1241                }
1242            }
1243        });
1244    }
1245
1246    @Override
1247    public void onTrimMemory(int level) {
1248        super.onTrimMemory(level);
1249        if (level >= TRIM_MEMORY_COMPLETE) {
1250            Log.d(Config.LOGTAG, "clear cache due to low memory");
1251            getBitmapCache().evictAll();
1252        }
1253    }
1254
1255    @Override
1256    public void onDestroy() {
1257        try {
1258            unregisterReceiver(this.mInternalEventReceiver);
1259            unregisterReceiver(this.mInternalScreenEventReceiver);
1260        } catch (final IllegalArgumentException e) {
1261            //ignored
1262        }
1263        destroyed = false;
1264        fileObserver.stopWatching();
1265        super.onDestroy();
1266    }
1267
1268    public void restartFileObserver() {
1269        Log.d(Config.LOGTAG, "restarting file observer");
1270        mFileAddingExecutor.execute(this.fileObserver::restartWatching);
1271        mFileAddingExecutor.execute(this::checkForDeletedFiles);
1272    }
1273
1274    public void toggleScreenEventReceiver() {
1275        if (awayWhenScreenLocked() && !manuallyChangePresence()) {
1276            final IntentFilter filter = new IntentFilter();
1277            filter.addAction(Intent.ACTION_SCREEN_ON);
1278            filter.addAction(Intent.ACTION_SCREEN_OFF);
1279            filter.addAction(Intent.ACTION_USER_PRESENT);
1280            registerReceiver(this.mInternalScreenEventReceiver, filter);
1281        } else {
1282            try {
1283                unregisterReceiver(this.mInternalScreenEventReceiver);
1284            } catch (IllegalArgumentException e) {
1285                //ignored
1286            }
1287        }
1288    }
1289
1290    public void toggleForegroundService() {
1291        toggleForegroundService(false);
1292    }
1293
1294    public void setOngoingCall(AbstractJingleConnection.Id id, Set<Media> media) {
1295        ongoingCall.set(new OngoingCall(id, media));
1296        toggleForegroundService(false);
1297    }
1298
1299    public void removeOngoingCall() {
1300        ongoingCall.set(null);
1301        toggleForegroundService(false);
1302    }
1303
1304    private void toggleForegroundService(boolean force) {
1305        final boolean status;
1306        final OngoingCall ongoing = ongoingCall.get();
1307        if (force || mForceDuringOnCreate.get() || mForceForegroundService.get() || ongoing != null || (Compatibility.keepForegroundService(this) && hasEnabledAccounts())) {
1308            final Notification notification;
1309            final int id;
1310            if (ongoing != null) {
1311                notification = this.mNotificationService.getOngoingCallNotification(ongoing.id, ongoing.media);
1312                id = NotificationService.ONGOING_CALL_NOTIFICATION_ID;
1313                startForeground(id, notification);
1314                mNotificationService.cancel(NotificationService.FOREGROUND_NOTIFICATION_ID);
1315            } else {
1316                notification = this.mNotificationService.createForegroundNotification();
1317                id = NotificationService.FOREGROUND_NOTIFICATION_ID;
1318                startForeground(id, notification);
1319            }
1320
1321            if (!mForceForegroundService.get()) {
1322                mNotificationService.notify(id, notification);
1323            }
1324            status = true;
1325        } else {
1326            stopForeground(true);
1327            status = false;
1328        }
1329        if (!mForceForegroundService.get()) {
1330            mNotificationService.cancel(NotificationService.FOREGROUND_NOTIFICATION_ID);
1331        }
1332        if (ongoing == null) {
1333            mNotificationService.cancel(NotificationService.ONGOING_CALL_NOTIFICATION_ID);
1334        }
1335        Log.d(Config.LOGTAG, "ForegroundService: " + (status ? "on" : "off"));
1336    }
1337
1338    public boolean foregroundNotificationNeedsUpdatingWhenErrorStateChanges() {
1339        return !mForceForegroundService.get() && ongoingCall.get() == null && Compatibility.keepForegroundService(this) && hasEnabledAccounts();
1340    }
1341
1342    @Override
1343    public void onTaskRemoved(final Intent rootIntent) {
1344        super.onTaskRemoved(rootIntent);
1345        if ((Compatibility.keepForegroundService(this) && hasEnabledAccounts()) || mForceForegroundService.get() || ongoingCall.get() != null) {
1346            Log.d(Config.LOGTAG, "ignoring onTaskRemoved because foreground service is activated");
1347        } else {
1348            this.logoutAndSave(false);
1349        }
1350    }
1351
1352    private void logoutAndSave(boolean stop) {
1353        int activeAccounts = 0;
1354        for (final Account account : accounts) {
1355            if (account.getStatus() != Account.State.DISABLED) {
1356                databaseBackend.writeRoster(account.getRoster());
1357                activeAccounts++;
1358            }
1359            if (account.getXmppConnection() != null) {
1360                new Thread(() -> disconnect(account, false)).start();
1361            }
1362        }
1363        if (stop || activeAccounts == 0) {
1364            Log.d(Config.LOGTAG, "good bye");
1365            stopSelf();
1366        }
1367    }
1368
1369    private void schedulePostConnectivityChange() {
1370        final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
1371        if (alarmManager == null) {
1372            return;
1373        }
1374        final long triggerAtMillis = SystemClock.elapsedRealtime() + (Config.POST_CONNECTIVITY_CHANGE_PING_INTERVAL * 1000);
1375        final Intent intent = new Intent(this, EventReceiver.class);
1376        intent.setAction(ACTION_POST_CONNECTIVITY_CHANGE);
1377        try {
1378            final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
1379            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
1380                alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtMillis, pendingIntent);
1381            } else {
1382                alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtMillis, pendingIntent);
1383            }
1384        } catch (RuntimeException e) {
1385            Log.e(Config.LOGTAG, "unable to schedule alarm for post connectivity change", e);
1386        }
1387    }
1388
1389    public void scheduleWakeUpCall(int seconds, int requestCode) {
1390        final long timeToWake = SystemClock.elapsedRealtime() + (seconds < 0 ? 1 : seconds + 1) * 1000;
1391        final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
1392        if (alarmManager == null) {
1393            return;
1394        }
1395        final Intent intent = new Intent(this, EventReceiver.class);
1396        intent.setAction("ping");
1397        try {
1398            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, requestCode, intent, 0);
1399            alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeToWake, pendingIntent);
1400        } catch (RuntimeException e) {
1401            Log.e(Config.LOGTAG, "unable to schedule alarm for ping", e);
1402        }
1403    }
1404
1405    @TargetApi(Build.VERSION_CODES.M)
1406    private void scheduleNextIdlePing() {
1407        final long timeToWake = SystemClock.elapsedRealtime() + (Config.IDLE_PING_INTERVAL * 1000);
1408        final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
1409        if (alarmManager == null) {
1410            return;
1411        }
1412        final Intent intent = new Intent(this, EventReceiver.class);
1413        intent.setAction(ACTION_IDLE_PING);
1414        try {
1415            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
1416            alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeToWake, pendingIntent);
1417        } catch (RuntimeException e) {
1418            Log.d(Config.LOGTAG, "unable to schedule alarm for idle ping", e);
1419        }
1420    }
1421
1422    public XmppConnection createConnection(final Account account) {
1423        final XmppConnection connection = new XmppConnection(account, this);
1424        connection.setOnMessagePacketReceivedListener(this.mMessageParser);
1425        connection.setOnStatusChangedListener(this.statusListener);
1426        connection.setOnPresencePacketReceivedListener(this.mPresenceParser);
1427        connection.setOnUnregisteredIqPacketReceivedListener(this.mIqParser);
1428        connection.setOnJinglePacketReceivedListener(((a, jp) -> mJingleConnectionManager.deliverPacket(a, jp)));
1429        connection.setOnBindListener(this.mOnBindListener);
1430        connection.setOnMessageAcknowledgeListener(this.mOnMessageAcknowledgedListener);
1431        connection.addOnAdvancedStreamFeaturesAvailableListener(this.mMessageArchiveService);
1432        connection.addOnAdvancedStreamFeaturesAvailableListener(this.mAvatarService);
1433        AxolotlService axolotlService = account.getAxolotlService();
1434        if (axolotlService != null) {
1435            connection.addOnAdvancedStreamFeaturesAvailableListener(axolotlService);
1436        }
1437        return connection;
1438    }
1439
1440    public void sendChatState(Conversation conversation) {
1441        if (sendChatStates()) {
1442            MessagePacket packet = mMessageGenerator.generateChatState(conversation);
1443            sendMessagePacket(conversation.getAccount(), packet);
1444        }
1445    }
1446
1447    private void sendFileMessage(final Message message, final boolean delay) {
1448        Log.d(Config.LOGTAG, "send file message");
1449        final Account account = message.getConversation().getAccount();
1450        if (account.httpUploadAvailable(fileBackend.getFile(message, false).getSize())
1451                || message.getConversation().getMode() == Conversation.MODE_MULTI) {
1452            mHttpConnectionManager.createNewUploadConnection(message, delay);
1453        } else {
1454            mJingleConnectionManager.startJingleFileTransfer(message);
1455        }
1456    }
1457
1458    public void sendMessage(final Message message) {
1459        sendMessage(message, false, false);
1460    }
1461
1462    private void sendMessage(final Message message, final boolean resend, final boolean delay) {
1463        final Account account = message.getConversation().getAccount();
1464        if (account.setShowErrorNotification(true)) {
1465            databaseBackend.updateAccount(account);
1466            mNotificationService.updateErrorNotification();
1467        }
1468        final Conversation conversation = (Conversation) message.getConversation();
1469        account.deactivateGracePeriod();
1470
1471
1472        if (QuickConversationsService.isQuicksy() && conversation.getMode() == Conversation.MODE_SINGLE) {
1473            final Contact contact = conversation.getContact();
1474            if (!contact.showInRoster() && contact.getOption(Contact.Options.SYNCED_VIA_OTHER)) {
1475                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": adding " + contact.getJid() + " on sending message");
1476                createContact(contact, true);
1477            }
1478        }
1479
1480        MessagePacket packet = null;
1481        final boolean addToConversation = (conversation.getMode() != Conversation.MODE_MULTI
1482                || !Patches.BAD_MUC_REFLECTION.contains(account.getServerIdentity()))
1483                && !message.edited();
1484        boolean saveInDb = addToConversation;
1485        message.setStatus(Message.STATUS_WAITING);
1486
1487        if (message.getEncryption() != Message.ENCRYPTION_NONE && conversation.getMode() == Conversation.MODE_MULTI && conversation.isPrivateAndNonAnonymous()) {
1488            if (conversation.setAttribute(Conversation.ATTRIBUTE_FORMERLY_PRIVATE_NON_ANONYMOUS, true)) {
1489                databaseBackend.updateConversation(conversation);
1490            }
1491        }
1492
1493        final boolean inProgressJoin = isJoinInProgress(conversation);
1494
1495
1496        if (account.isOnlineAndConnected() && !inProgressJoin) {
1497            switch (message.getEncryption()) {
1498                case Message.ENCRYPTION_NONE:
1499                    if (message.needsUploading()) {
1500                        if (account.httpUploadAvailable(fileBackend.getFile(message, false).getSize())
1501                                || conversation.getMode() == Conversation.MODE_MULTI
1502                                || message.fixCounterpart()) {
1503                            this.sendFileMessage(message, delay);
1504                        } else {
1505                            break;
1506                        }
1507                    } else {
1508                        packet = mMessageGenerator.generateChat(message);
1509                    }
1510                    break;
1511                case Message.ENCRYPTION_PGP:
1512                case Message.ENCRYPTION_DECRYPTED:
1513                    if (message.needsUploading()) {
1514                        if (account.httpUploadAvailable(fileBackend.getFile(message, false).getSize())
1515                                || conversation.getMode() == Conversation.MODE_MULTI
1516                                || message.fixCounterpart()) {
1517                            this.sendFileMessage(message, delay);
1518                        } else {
1519                            break;
1520                        }
1521                    } else {
1522                        packet = mMessageGenerator.generatePgpChat(message);
1523                    }
1524                    break;
1525                case Message.ENCRYPTION_AXOLOTL:
1526                    message.setFingerprint(account.getAxolotlService().getOwnFingerprint());
1527                    if (message.needsUploading()) {
1528                        if (account.httpUploadAvailable(fileBackend.getFile(message, false).getSize())
1529                                || conversation.getMode() == Conversation.MODE_MULTI
1530                                || message.fixCounterpart()) {
1531                            this.sendFileMessage(message, delay);
1532                        } else {
1533                            break;
1534                        }
1535                    } else {
1536                        XmppAxolotlMessage axolotlMessage = account.getAxolotlService().fetchAxolotlMessageFromCache(message);
1537                        if (axolotlMessage == null) {
1538                            account.getAxolotlService().preparePayloadMessage(message, delay);
1539                        } else {
1540                            packet = mMessageGenerator.generateAxolotlChat(message, axolotlMessage);
1541                        }
1542                    }
1543                    break;
1544
1545            }
1546            if (packet != null) {
1547                if (account.getXmppConnection().getFeatures().sm()
1548                        || (conversation.getMode() == Conversation.MODE_MULTI && message.getCounterpart().isBareJid())) {
1549                    message.setStatus(Message.STATUS_UNSEND);
1550                } else {
1551                    message.setStatus(Message.STATUS_SEND);
1552                }
1553            }
1554        } else {
1555            switch (message.getEncryption()) {
1556                case Message.ENCRYPTION_DECRYPTED:
1557                    if (!message.needsUploading()) {
1558                        String pgpBody = message.getEncryptedBody();
1559                        String decryptedBody = message.getBody();
1560                        message.setBody(pgpBody); //TODO might throw NPE
1561                        message.setEncryption(Message.ENCRYPTION_PGP);
1562                        if (message.edited()) {
1563                            message.setBody(decryptedBody);
1564                            message.setEncryption(Message.ENCRYPTION_DECRYPTED);
1565                            if (!databaseBackend.updateMessage(message, message.getEditedId())) {
1566                                Log.e(Config.LOGTAG, "error updated message in DB after edit");
1567                            }
1568                            updateConversationUi();
1569                            return;
1570                        } else {
1571                            databaseBackend.createMessage(message);
1572                            saveInDb = false;
1573                            message.setBody(decryptedBody);
1574                            message.setEncryption(Message.ENCRYPTION_DECRYPTED);
1575                        }
1576                    }
1577                    break;
1578                case Message.ENCRYPTION_AXOLOTL:
1579                    message.setFingerprint(account.getAxolotlService().getOwnFingerprint());
1580                    break;
1581            }
1582        }
1583
1584
1585        boolean mucMessage = conversation.getMode() == Conversation.MODE_MULTI && !message.isPrivateMessage();
1586        if (mucMessage) {
1587            message.setCounterpart(conversation.getMucOptions().getSelf().getFullJid());
1588        }
1589
1590        if (resend) {
1591            if (packet != null && addToConversation) {
1592                if (account.getXmppConnection().getFeatures().sm() || mucMessage) {
1593                    markMessage(message, Message.STATUS_UNSEND);
1594                } else {
1595                    markMessage(message, Message.STATUS_SEND);
1596                }
1597            }
1598        } else {
1599            if (addToConversation) {
1600                conversation.add(message);
1601            }
1602            if (saveInDb) {
1603                databaseBackend.createMessage(message);
1604            } else if (message.edited()) {
1605                if (!databaseBackend.updateMessage(message, message.getEditedId())) {
1606                    Log.e(Config.LOGTAG, "error updated message in DB after edit");
1607                }
1608            }
1609            updateConversationUi();
1610        }
1611        if (packet != null) {
1612            if (delay) {
1613                mMessageGenerator.addDelay(packet, message.getTimeSent());
1614            }
1615            if (conversation.setOutgoingChatState(Config.DEFAULT_CHAT_STATE)) {
1616                if (this.sendChatStates()) {
1617                    packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
1618                }
1619            }
1620            sendMessagePacket(account, packet);
1621        }
1622    }
1623
1624    private boolean isJoinInProgress(final Conversation conversation) {
1625        final Account account = conversation.getAccount();
1626        synchronized (account.inProgressConferenceJoins) {
1627            if (conversation.getMode() == Conversational.MODE_MULTI) {
1628                final boolean inProgress = account.inProgressConferenceJoins.contains(conversation);
1629                final boolean pending = account.pendingConferenceJoins.contains(conversation);
1630                final boolean inProgressJoin = inProgress || pending;
1631                if (inProgressJoin) {
1632                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": holding back message to group. inProgress=" + inProgress + ", pending=" + pending);
1633                }
1634                return inProgressJoin;
1635            } else {
1636                return false;
1637            }
1638        }
1639    }
1640
1641    private void sendUnsentMessages(final Conversation conversation) {
1642        conversation.findWaitingMessages(message -> resendMessage(message, true));
1643    }
1644
1645    public void resendMessage(final Message message, final boolean delay) {
1646        sendMessage(message, true, delay);
1647    }
1648
1649    public void requestEasyOnboardingInvite(final Account account, final EasyOnboardingInvite.OnInviteRequested callback) {
1650        final XmppConnection connection = account.getXmppConnection();
1651        final Jid jid = connection == null ? null : connection.getJidForCommand(Namespace.EASY_ONBOARDING_INVITE);
1652        if (jid == null) {
1653            callback.inviteRequestFailed(getString(R.string.server_does_not_support_easy_onboarding_invites));
1654            return;
1655        }
1656        final IqPacket request = new IqPacket(IqPacket.TYPE.SET);
1657        request.setTo(jid);
1658        final Element command = request.addChild("command", Namespace.COMMANDS);
1659        command.setAttribute("node", Namespace.EASY_ONBOARDING_INVITE);
1660        command.setAttribute("action", "execute");
1661        sendIqPacket(account, request, (a, response) -> {
1662            if (response.getType() == IqPacket.TYPE.RESULT) {
1663                final Element resultCommand = response.findChild("command", Namespace.COMMANDS);
1664                final Element x = resultCommand == null ? null : resultCommand.findChild("x", Namespace.DATA);
1665                if (x != null) {
1666                    final Data data = Data.parse(x);
1667                    final String uri = data.getValue("uri");
1668                    final String landingUrl = data.getValue("landing-url");
1669                    if (uri != null) {
1670                        final EasyOnboardingInvite invite = new EasyOnboardingInvite(jid.getDomain().toEscapedString(), uri, landingUrl);
1671                        callback.inviteRequested(invite);
1672                        return;
1673                    }
1674                }
1675                callback.inviteRequestFailed(getString(R.string.unable_to_parse_invite));
1676                Log.d(Config.LOGTAG, response.toString());
1677            } else if (response.getType() == IqPacket.TYPE.ERROR) {
1678                callback.inviteRequestFailed(IqParser.errorMessage(response));
1679            } else {
1680                callback.inviteRequestFailed(getString(R.string.remote_server_timeout));
1681            }
1682        });
1683
1684    }
1685
1686    public void fetchRosterFromServer(final Account account) {
1687        final IqPacket iqPacket = new IqPacket(IqPacket.TYPE.GET);
1688        if (!"".equals(account.getRosterVersion())) {
1689            Log.d(Config.LOGTAG, account.getJid().asBareJid()
1690                    + ": fetching roster version " + account.getRosterVersion());
1691        } else {
1692            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": fetching roster");
1693        }
1694        iqPacket.query(Namespace.ROSTER).setAttribute("ver", account.getRosterVersion());
1695        sendIqPacket(account, iqPacket, mIqParser);
1696    }
1697
1698    public void fetchBookmarks(final Account account) {
1699        final IqPacket iqPacket = new IqPacket(IqPacket.TYPE.GET);
1700        final Element query = iqPacket.query("jabber:iq:private");
1701        query.addChild("storage", Namespace.BOOKMARKS);
1702        final OnIqPacketReceived callback = (a, response) -> {
1703            if (response.getType() == IqPacket.TYPE.RESULT) {
1704                final Element query1 = response.query();
1705                final Element storage = query1.findChild("storage", "storage:bookmarks");
1706                Map<Jid, Bookmark> bookmarks = Bookmark.parseFromStorage(storage, account);
1707                processBookmarksInitial(a, bookmarks, false);
1708            } else {
1709                Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": could not fetch bookmarks");
1710            }
1711        };
1712        sendIqPacket(account, iqPacket, callback);
1713    }
1714
1715    public void fetchBookmarks2(final Account account) {
1716        final IqPacket retrieve = mIqGenerator.retrieveBookmarks();
1717        sendIqPacket(account, retrieve, new OnIqPacketReceived() {
1718            @Override
1719            public void onIqPacketReceived(final Account account, final IqPacket response) {
1720                if (response.getType() == IqPacket.TYPE.RESULT) {
1721                    final Element pubsub = response.findChild("pubsub", Namespace.PUBSUB);
1722                    final Map<Jid, Bookmark> bookmarks = Bookmark.parseFromPubsub(pubsub, account);
1723                    processBookmarksInitial(account, bookmarks, true);
1724                }
1725            }
1726        });
1727    }
1728
1729    public void processBookmarksInitial(Account account, Map<Jid, Bookmark> bookmarks, final boolean pep) {
1730        final Set<Jid> previousBookmarks = account.getBookmarkedJids();
1731        final boolean synchronizeWithBookmarks = synchronizeWithBookmarks();
1732        for (Bookmark bookmark : bookmarks.values()) {
1733            previousBookmarks.remove(bookmark.getJid().asBareJid());
1734            processModifiedBookmark(bookmark, pep, synchronizeWithBookmarks);
1735        }
1736        if (pep && synchronizeWithBookmarks) {
1737            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + previousBookmarks.size() + " bookmarks have been removed");
1738            for (Jid jid : previousBookmarks) {
1739                processDeletedBookmark(account, jid);
1740            }
1741        }
1742        account.setBookmarks(bookmarks);
1743    }
1744
1745    public void processDeletedBookmark(Account account, Jid jid) {
1746        final Conversation conversation = find(account, jid);
1747        if (conversation != null && conversation.getMucOptions().getError() == MucOptions.Error.DESTROYED) {
1748            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": archiving destroyed conference (" + conversation.getJid() + ") after receiving pep");
1749            archiveConversation(conversation, false);
1750        }
1751    }
1752
1753    private void processModifiedBookmark(Bookmark bookmark, final boolean pep, final boolean synchronizeWithBookmarks) {
1754        final Account account = bookmark.getAccount();
1755        Conversation conversation = find(bookmark);
1756        if (conversation != null) {
1757            if (conversation.getMode() != Conversation.MODE_MULTI) {
1758                return;
1759            }
1760            bookmark.setConversation(conversation);
1761            if (pep && synchronizeWithBookmarks && !bookmark.autojoin()) {
1762                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": archiving conference (" + conversation.getJid() + ") after receiving pep");
1763                archiveConversation(conversation, false);
1764            } else {
1765                final MucOptions mucOptions = conversation.getMucOptions();
1766                if (mucOptions.getError() == MucOptions.Error.NICK_IN_USE) {
1767                    final String current = mucOptions.getActualNick();
1768                    final String proposed = mucOptions.getProposedNick();
1769                    if (current != null && !current.equals(proposed)) {
1770                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": proposed nick changed after bookmark push " + current + "->" + proposed);
1771                        joinMuc(conversation);
1772                    }
1773                }
1774            }
1775        } else if (synchronizeWithBookmarks && bookmark.autojoin()) {
1776            conversation = findOrCreateConversation(account, bookmark.getFullJid(), true, true, false);
1777            bookmark.setConversation(conversation);
1778        }
1779    }
1780
1781    public void processModifiedBookmark(Bookmark bookmark) {
1782        final boolean synchronizeWithBookmarks = synchronizeWithBookmarks();
1783        processModifiedBookmark(bookmark, true, synchronizeWithBookmarks);
1784    }
1785
1786    public void createBookmark(final Account account, final Bookmark bookmark) {
1787        account.putBookmark(bookmark);
1788        final XmppConnection connection = account.getXmppConnection();
1789        if (connection.getFeatures().bookmarks2()) {
1790            final Element item = mIqGenerator.publishBookmarkItem(bookmark);
1791            pushNodeAndEnforcePublishOptions(account, Namespace.BOOKMARKS2, item, bookmark.getJid().asBareJid().toEscapedString(), PublishOptions.persistentWhitelistAccessMaxItems());
1792        } else if (connection.getFeatures().bookmarksConversion()) {
1793            pushBookmarksPep(account);
1794        } else {
1795            pushBookmarksPrivateXml(account);
1796        }
1797    }
1798
1799    public void deleteBookmark(final Account account, final Bookmark bookmark) {
1800        account.removeBookmark(bookmark);
1801        final XmppConnection connection = account.getXmppConnection();
1802        if (connection.getFeatures().bookmarks2()) {
1803            IqPacket request = mIqGenerator.deleteItem(Namespace.BOOKMARKS2, bookmark.getJid().asBareJid().toEscapedString());
1804            sendIqPacket(account, request, (a, response) -> {
1805                if (response.getType() == IqPacket.TYPE.ERROR) {
1806                    Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": unable to delete bookmark " + response.getErrorCondition());
1807                }
1808            });
1809        } else if (connection.getFeatures().bookmarksConversion()) {
1810            pushBookmarksPep(account);
1811        } else {
1812            pushBookmarksPrivateXml(account);
1813        }
1814    }
1815
1816    private void pushBookmarksPrivateXml(Account account) {
1817        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": pushing bookmarks via private xml");
1818        IqPacket iqPacket = new IqPacket(IqPacket.TYPE.SET);
1819        Element query = iqPacket.query("jabber:iq:private");
1820        Element storage = query.addChild("storage", "storage:bookmarks");
1821        for (Bookmark bookmark : account.getBookmarks()) {
1822            storage.addChild(bookmark);
1823        }
1824        sendIqPacket(account, iqPacket, mDefaultIqHandler);
1825    }
1826
1827    private void pushBookmarksPep(Account account) {
1828        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": pushing bookmarks via pep");
1829        Element storage = new Element("storage", "storage:bookmarks");
1830        for (Bookmark bookmark : account.getBookmarks()) {
1831            storage.addChild(bookmark);
1832        }
1833        pushNodeAndEnforcePublishOptions(account, Namespace.BOOKMARKS, storage, "current", PublishOptions.persistentWhitelistAccess());
1834
1835    }
1836
1837    private void pushNodeAndEnforcePublishOptions(final Account account, final String node, final Element element, final String id, final Bundle options) {
1838        pushNodeAndEnforcePublishOptions(account, node, element, id, options, true);
1839
1840    }
1841
1842    private void pushNodeAndEnforcePublishOptions(final Account account, final String node, final Element element, final String id, final Bundle options, final boolean retry) {
1843        final IqPacket packet = mIqGenerator.publishElement(node, element, id, options);
1844        sendIqPacket(account, packet, (a, response) -> {
1845            if (response.getType() == IqPacket.TYPE.RESULT) {
1846                return;
1847            }
1848            if (retry && PublishOptions.preconditionNotMet(response)) {
1849                pushNodeConfiguration(account, node, options, new OnConfigurationPushed() {
1850                    @Override
1851                    public void onPushSucceeded() {
1852                        pushNodeAndEnforcePublishOptions(account, node, element, id, options, false);
1853                    }
1854
1855                    @Override
1856                    public void onPushFailed() {
1857                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to push node configuration (" + node + ")");
1858                    }
1859                });
1860            } else {
1861                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": error publishing bookmarks (retry=" + retry + ") " + response);
1862            }
1863        });
1864    }
1865
1866    private void restoreFromDatabase() {
1867        synchronized (this.conversations) {
1868            final Map<String, Account> accountLookupTable = new Hashtable<>();
1869            for (Account account : this.accounts) {
1870                accountLookupTable.put(account.getUuid(), account);
1871            }
1872            Log.d(Config.LOGTAG, "restoring conversations...");
1873            final long startTimeConversationsRestore = SystemClock.elapsedRealtime();
1874            this.conversations.addAll(databaseBackend.getConversations(Conversation.STATUS_AVAILABLE));
1875            for (Iterator<Conversation> iterator = conversations.listIterator(); iterator.hasNext(); ) {
1876                Conversation conversation = iterator.next();
1877                Account account = accountLookupTable.get(conversation.getAccountUuid());
1878                if (account != null) {
1879                    conversation.setAccount(account);
1880                } else {
1881                    Log.e(Config.LOGTAG, "unable to restore Conversations with " + conversation.getJid());
1882                    iterator.remove();
1883                }
1884            }
1885            long diffConversationsRestore = SystemClock.elapsedRealtime() - startTimeConversationsRestore;
1886            Log.d(Config.LOGTAG, "finished restoring conversations in " + diffConversationsRestore + "ms");
1887            Runnable runnable = () -> {
1888                long deletionDate = getAutomaticMessageDeletionDate();
1889                mLastExpiryRun.set(SystemClock.elapsedRealtime());
1890                if (deletionDate > 0) {
1891                    Log.d(Config.LOGTAG, "deleting messages that are older than " + AbstractGenerator.getTimestamp(deletionDate));
1892                    databaseBackend.expireOldMessages(deletionDate);
1893                }
1894                Log.d(Config.LOGTAG, "restoring roster...");
1895                for (Account account : accounts) {
1896                    databaseBackend.readRoster(account.getRoster());
1897                    account.initAccountServices(XmppConnectionService.this); //roster needs to be loaded at this stage
1898                }
1899                getBitmapCache().evictAll();
1900                loadPhoneContacts();
1901                Log.d(Config.LOGTAG, "restoring messages...");
1902                final long startMessageRestore = SystemClock.elapsedRealtime();
1903                final Conversation quickLoad = QuickLoader.get(this.conversations);
1904                if (quickLoad != null) {
1905                    restoreMessages(quickLoad);
1906                    updateConversationUi();
1907                    final long diffMessageRestore = SystemClock.elapsedRealtime() - startMessageRestore;
1908                    Log.d(Config.LOGTAG, "quickly restored " + quickLoad.getName() + " after " + diffMessageRestore + "ms");
1909                }
1910                for (Conversation conversation : this.conversations) {
1911                    if (quickLoad != conversation) {
1912                        restoreMessages(conversation);
1913                    }
1914                }
1915                mNotificationService.finishBacklog(false);
1916                restoredFromDatabaseLatch.countDown();
1917                final long diffMessageRestore = SystemClock.elapsedRealtime() - startMessageRestore;
1918                Log.d(Config.LOGTAG, "finished restoring messages in " + diffMessageRestore + "ms");
1919                updateConversationUi();
1920            };
1921            mDatabaseReaderExecutor.execute(runnable); //will contain one write command (expiry) but that's fine
1922        }
1923    }
1924
1925    private void restoreMessages(Conversation conversation) {
1926        conversation.addAll(0, databaseBackend.getMessages(conversation, Config.PAGE_SIZE));
1927        conversation.findUnsentTextMessages(message -> markMessage(message, Message.STATUS_WAITING));
1928        conversation.findUnreadMessages(message -> mNotificationService.pushFromBacklog(message));
1929    }
1930
1931    public void loadPhoneContacts() {
1932        mContactMergerExecutor.execute(() -> {
1933            Map<Jid, JabberIdContact> contacts = JabberIdContact.load(this);
1934            Log.d(Config.LOGTAG, "start merging phone contacts with roster");
1935            for (Account account : accounts) {
1936                List<Contact> withSystemAccounts = account.getRoster().getWithSystemAccounts(JabberIdContact.class);
1937                for (JabberIdContact jidContact : contacts.values()) {
1938                    final Contact contact = account.getRoster().getContact(jidContact.getJid());
1939                    boolean needsCacheClean = contact.setPhoneContact(jidContact);
1940                    if (needsCacheClean) {
1941                        getAvatarService().clear(contact);
1942                    }
1943                    withSystemAccounts.remove(contact);
1944                }
1945                for (Contact contact : withSystemAccounts) {
1946                    boolean needsCacheClean = contact.unsetPhoneContact(JabberIdContact.class);
1947                    if (needsCacheClean) {
1948                        getAvatarService().clear(contact);
1949                    }
1950                }
1951            }
1952            Log.d(Config.LOGTAG, "finished merging phone contacts");
1953            mShortcutService.refresh(mInitialAddressbookSyncCompleted.compareAndSet(false, true));
1954            updateRosterUi();
1955            mQuickConversationsService.considerSync();
1956        });
1957    }
1958
1959
1960    public void syncRoster(final Account account) {
1961        mRosterSyncTaskManager.execute(account, () -> databaseBackend.writeRoster(account.getRoster()));
1962    }
1963
1964    public List<Conversation> getConversations() {
1965        return this.conversations;
1966    }
1967
1968    private void markFileDeleted(final File file) {
1969        synchronized (FILENAMES_TO_IGNORE_DELETION) {
1970            if (FILENAMES_TO_IGNORE_DELETION.remove(file.getAbsolutePath())) {
1971                Log.d(Config.LOGTAG, "ignored deletion of " + file.getAbsolutePath());
1972                return;
1973            }
1974        }
1975        final boolean isInternalFile = fileBackend.isInternalFile(file);
1976        final List<String> uuids = databaseBackend.markFileAsDeleted(file, isInternalFile);
1977        Log.d(Config.LOGTAG, "deleted file " + file.getAbsolutePath() + " internal=" + isInternalFile + ", database hits=" + uuids.size());
1978        markUuidsAsDeletedFiles(uuids);
1979    }
1980
1981    private void markUuidsAsDeletedFiles(List<String> uuids) {
1982        boolean deleted = false;
1983        for (Conversation conversation : getConversations()) {
1984            deleted |= conversation.markAsDeleted(uuids);
1985        }
1986        for (final String uuid : uuids) {
1987            evictPreview(uuid);
1988        }
1989        if (deleted) {
1990            updateConversationUi();
1991        }
1992    }
1993
1994    private void markChangedFiles(List<DatabaseBackend.FilePathInfo> infos) {
1995        boolean changed = false;
1996        for (Conversation conversation : getConversations()) {
1997            changed |= conversation.markAsChanged(infos);
1998        }
1999        if (changed) {
2000            updateConversationUi();
2001        }
2002    }
2003
2004    public void populateWithOrderedConversations(final List<Conversation> list) {
2005        populateWithOrderedConversations(list, true, true);
2006    }
2007
2008    public void populateWithOrderedConversations(final List<Conversation> list, final boolean includeNoFileUpload) {
2009        populateWithOrderedConversations(list, includeNoFileUpload, true);
2010    }
2011
2012    public void populateWithOrderedConversations(final List<Conversation> list, final boolean includeNoFileUpload, final boolean sort) {
2013        final List<String> orderedUuids;
2014        if (sort) {
2015            orderedUuids = null;
2016        } else {
2017            orderedUuids = new ArrayList<>();
2018            for (Conversation conversation : list) {
2019                orderedUuids.add(conversation.getUuid());
2020            }
2021        }
2022        list.clear();
2023        if (includeNoFileUpload) {
2024            list.addAll(getConversations());
2025        } else {
2026            for (Conversation conversation : getConversations()) {
2027                if (conversation.getMode() == Conversation.MODE_SINGLE
2028                        || (conversation.getAccount().httpUploadAvailable() && conversation.getMucOptions().participating())) {
2029                    list.add(conversation);
2030                }
2031            }
2032        }
2033        try {
2034            if (orderedUuids != null) {
2035                Collections.sort(list, (a, b) -> {
2036                    final int indexA = orderedUuids.indexOf(a.getUuid());
2037                    final int indexB = orderedUuids.indexOf(b.getUuid());
2038                    if (indexA == -1 || indexB == -1 || indexA == indexB) {
2039                        return a.compareTo(b);
2040                    }
2041                    return indexA - indexB;
2042                });
2043            } else {
2044                Collections.sort(list);
2045            }
2046        } catch (IllegalArgumentException e) {
2047            //ignore
2048        }
2049    }
2050
2051    public void loadMoreMessages(final Conversation conversation, final long timestamp, final OnMoreMessagesLoaded callback) {
2052        if (XmppConnectionService.this.getMessageArchiveService().queryInProgress(conversation, callback)) {
2053            return;
2054        } else if (timestamp == 0) {
2055            return;
2056        }
2057        Log.d(Config.LOGTAG, "load more messages for " + conversation.getName() + " prior to " + MessageGenerator.getTimestamp(timestamp));
2058        final Runnable runnable = () -> {
2059            final Account account = conversation.getAccount();
2060            List<Message> messages = databaseBackend.getMessages(conversation, 50, timestamp);
2061            if (messages.size() > 0) {
2062                conversation.addAll(0, messages);
2063                callback.onMoreMessagesLoaded(messages.size(), conversation);
2064            } else if (conversation.hasMessagesLeftOnServer()
2065                    && account.isOnlineAndConnected()
2066                    && conversation.getLastClearHistory().getTimestamp() == 0) {
2067                final boolean mamAvailable;
2068                if (conversation.getMode() == Conversation.MODE_SINGLE) {
2069                    mamAvailable = account.getXmppConnection().getFeatures().mam() && !conversation.getContact().isBlocked();
2070                } else {
2071                    mamAvailable = conversation.getMucOptions().mamSupport();
2072                }
2073                if (mamAvailable) {
2074                    MessageArchiveService.Query query = getMessageArchiveService().query(conversation, new MamReference(0), timestamp, false);
2075                    if (query != null) {
2076                        query.setCallback(callback);
2077                        callback.informUser(R.string.fetching_history_from_server);
2078                    } else {
2079                        callback.informUser(R.string.not_fetching_history_retention_period);
2080                    }
2081
2082                }
2083            }
2084        };
2085        mDatabaseReaderExecutor.execute(runnable);
2086    }
2087
2088    public List<Account> getAccounts() {
2089        return this.accounts;
2090    }
2091
2092
2093    /**
2094     * This will find all conferences with the contact as member and also the conference that is the contact (that 'fake' contact is used to store the avatar)
2095     */
2096    public List<Conversation> findAllConferencesWith(Contact contact) {
2097        final ArrayList<Conversation> results = new ArrayList<>();
2098        for (final Conversation c : conversations) {
2099            if (c.getMode() != Conversation.MODE_MULTI) {
2100                continue;
2101            }
2102            final MucOptions mucOptions = c.getMucOptions();
2103            if (c.getJid().asBareJid().equals(contact.getJid().asBareJid()) || (mucOptions != null && mucOptions.isContactInRoom(contact))) {
2104                results.add(c);
2105            }
2106        }
2107        return results;
2108    }
2109
2110    public Conversation find(final Iterable<Conversation> haystack, final Contact contact) {
2111        for (final Conversation conversation : haystack) {
2112            if (conversation.getContact() == contact) {
2113                return conversation;
2114            }
2115        }
2116        return null;
2117    }
2118
2119    public Conversation find(final Iterable<Conversation> haystack, final Account account, final Jid jid) {
2120        if (jid == null) {
2121            return null;
2122        }
2123        for (final Conversation conversation : haystack) {
2124            if ((account == null || conversation.getAccount() == account)
2125                    && (conversation.getJid().asBareJid().equals(jid.asBareJid()))) {
2126                return conversation;
2127            }
2128        }
2129        return null;
2130    }
2131
2132    public boolean isConversationsListEmpty(final Conversation ignore) {
2133        synchronized (this.conversations) {
2134            final int size = this.conversations.size();
2135            return size == 0 || size == 1 && this.conversations.get(0) == ignore;
2136        }
2137    }
2138
2139    public boolean isConversationStillOpen(final Conversation conversation) {
2140        synchronized (this.conversations) {
2141            for (Conversation current : this.conversations) {
2142                if (current == conversation) {
2143                    return true;
2144                }
2145            }
2146        }
2147        return false;
2148    }
2149
2150    public Conversation findOrCreateConversation(Account account, Jid jid, boolean muc, final boolean async) {
2151        return this.findOrCreateConversation(account, jid, muc, false, async);
2152    }
2153
2154    public Conversation findOrCreateConversation(final Account account, final Jid jid, final boolean muc, final boolean joinAfterCreate, final boolean async) {
2155        return this.findOrCreateConversation(account, jid, muc, joinAfterCreate, null, async);
2156    }
2157
2158    public Conversation findOrCreateConversation(final Account account, final Jid jid, final boolean muc, final boolean joinAfterCreate, final MessageArchiveService.Query query, final boolean async) {
2159        synchronized (this.conversations) {
2160            Conversation conversation = find(account, jid);
2161            if (conversation != null) {
2162                return conversation;
2163            }
2164            conversation = databaseBackend.findConversation(account, jid);
2165            final boolean loadMessagesFromDb;
2166            if (conversation != null) {
2167                conversation.setStatus(Conversation.STATUS_AVAILABLE);
2168                conversation.setAccount(account);
2169                if (muc) {
2170                    conversation.setMode(Conversation.MODE_MULTI);
2171                    conversation.setContactJid(jid);
2172                } else {
2173                    conversation.setMode(Conversation.MODE_SINGLE);
2174                    conversation.setContactJid(jid.asBareJid());
2175                }
2176                databaseBackend.updateConversation(conversation);
2177                loadMessagesFromDb = conversation.messagesLoaded.compareAndSet(true, false);
2178            } else {
2179                String conversationName;
2180                Contact contact = account.getRoster().getContact(jid);
2181                if (contact != null) {
2182                    conversationName = contact.getDisplayName();
2183                } else {
2184                    conversationName = jid.getLocal();
2185                }
2186                if (muc) {
2187                    conversation = new Conversation(conversationName, account, jid,
2188                            Conversation.MODE_MULTI);
2189                } else {
2190                    conversation = new Conversation(conversationName, account, jid.asBareJid(),
2191                            Conversation.MODE_SINGLE);
2192                }
2193                this.databaseBackend.createConversation(conversation);
2194                loadMessagesFromDb = false;
2195            }
2196            final Conversation c = conversation;
2197            final Runnable runnable = () -> {
2198                if (loadMessagesFromDb) {
2199                    c.addAll(0, databaseBackend.getMessages(c, Config.PAGE_SIZE));
2200                    updateConversationUi();
2201                    c.messagesLoaded.set(true);
2202                }
2203                if (account.getXmppConnection() != null
2204                        && !c.getContact().isBlocked()
2205                        && account.getXmppConnection().getFeatures().mam()
2206                        && !muc) {
2207                    if (query == null) {
2208                        mMessageArchiveService.query(c);
2209                    } else {
2210                        if (query.getConversation() == null) {
2211                            mMessageArchiveService.query(c, query.getStart(), query.isCatchup());
2212                        }
2213                    }
2214                }
2215                if (joinAfterCreate) {
2216                    joinMuc(c);
2217                }
2218            };
2219            if (async) {
2220                mDatabaseReaderExecutor.execute(runnable);
2221            } else {
2222                runnable.run();
2223            }
2224            this.conversations.add(conversation);
2225            updateConversationUi();
2226            return conversation;
2227        }
2228    }
2229
2230    public void archiveConversation(Conversation conversation) {
2231        archiveConversation(conversation, true);
2232    }
2233
2234    private void archiveConversation(Conversation conversation, final boolean maySynchronizeWithBookmarks) {
2235        getNotificationService().clear(conversation);
2236        conversation.setStatus(Conversation.STATUS_ARCHIVED);
2237        conversation.setNextMessage(null);
2238        synchronized (this.conversations) {
2239            getMessageArchiveService().kill(conversation);
2240            if (conversation.getMode() == Conversation.MODE_MULTI) {
2241                if (conversation.getAccount().getStatus() == Account.State.ONLINE) {
2242                    final Bookmark bookmark = conversation.getBookmark();
2243                    if (maySynchronizeWithBookmarks && bookmark != null && synchronizeWithBookmarks()) {
2244                        if (conversation.getMucOptions().getError() == MucOptions.Error.DESTROYED) {
2245                            Account account = bookmark.getAccount();
2246                            bookmark.setConversation(null);
2247                            deleteBookmark(account, bookmark);
2248                        } else if (bookmark.autojoin()) {
2249                            bookmark.setAutojoin(false);
2250                            createBookmark(bookmark.getAccount(), bookmark);
2251                        }
2252                    }
2253                }
2254                leaveMuc(conversation);
2255            } else {
2256                if (conversation.getContact().getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
2257                    stopPresenceUpdatesTo(conversation.getContact());
2258                }
2259            }
2260            updateConversation(conversation);
2261            this.conversations.remove(conversation);
2262            updateConversationUi();
2263        }
2264    }
2265
2266    public void stopPresenceUpdatesTo(Contact contact) {
2267        Log.d(Config.LOGTAG, "Canceling presence request from " + contact.getJid().toString());
2268        sendPresencePacket(contact.getAccount(), mPresenceGenerator.stopPresenceUpdatesTo(contact));
2269        contact.resetOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
2270    }
2271
2272    public void createAccount(final Account account) {
2273        account.initAccountServices(this);
2274        databaseBackend.createAccount(account);
2275        this.accounts.add(account);
2276        this.reconnectAccountInBackground(account);
2277        updateAccountUi();
2278        syncEnabledAccountSetting();
2279        toggleForegroundService();
2280    }
2281
2282    private void syncEnabledAccountSetting() {
2283        final boolean hasEnabledAccounts = hasEnabledAccounts();
2284        getPreferences().edit().putBoolean(EventReceiver.SETTING_ENABLED_ACCOUNTS, hasEnabledAccounts).apply();
2285        toggleSetProfilePictureActivity(hasEnabledAccounts);
2286    }
2287
2288    private void toggleSetProfilePictureActivity(final boolean enabled) {
2289        try {
2290            final ComponentName name = new ComponentName(this, ChooseAccountForProfilePictureActivity.class);
2291            final int targetState = enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
2292            getPackageManager().setComponentEnabledSetting(name, targetState, PackageManager.DONT_KILL_APP);
2293        } catch (IllegalStateException e) {
2294            Log.d(Config.LOGTAG, "unable to toggle profile picture actvitiy");
2295        }
2296    }
2297
2298    private void provisionAccount(final String address, final String password) {
2299        final Jid jid = Jid.ofEscaped(address);
2300        final Account account = new Account(jid, password);
2301        account.setOption(Account.OPTION_DISABLED, true);
2302        Log.d(Config.LOGTAG, jid.asBareJid().toEscapedString() + ": provisioning account");
2303        createAccount(account);
2304    }
2305
2306    public void createAccountFromKey(final String alias, final OnAccountCreated callback) {
2307        new Thread(() -> {
2308            try {
2309                final X509Certificate[] chain = KeyChain.getCertificateChain(this, alias);
2310                final X509Certificate cert = chain != null && chain.length > 0 ? chain[0] : null;
2311                if (cert == null) {
2312                    callback.informUser(R.string.unable_to_parse_certificate);
2313                    return;
2314                }
2315                Pair<Jid, String> info = CryptoHelper.extractJidAndName(cert);
2316                if (info == null) {
2317                    callback.informUser(R.string.certificate_does_not_contain_jid);
2318                    return;
2319                }
2320                if (findAccountByJid(info.first) == null) {
2321                    final Account account = new Account(info.first, "");
2322                    account.setPrivateKeyAlias(alias);
2323                    account.setOption(Account.OPTION_DISABLED, true);
2324                    account.setOption(Account.OPTION_FIXED_USERNAME, true);
2325                    account.setDisplayName(info.second);
2326                    createAccount(account);
2327                    callback.onAccountCreated(account);
2328                    if (Config.X509_VERIFICATION) {
2329                        try {
2330                            getMemorizingTrustManager().getNonInteractive(account.getServer()).checkClientTrusted(chain, "RSA");
2331                        } catch (CertificateException e) {
2332                            callback.informUser(R.string.certificate_chain_is_not_trusted);
2333                        }
2334                    }
2335                } else {
2336                    callback.informUser(R.string.account_already_exists);
2337                }
2338            } catch (Exception e) {
2339                callback.informUser(R.string.unable_to_parse_certificate);
2340            }
2341        }).start();
2342
2343    }
2344
2345    public void updateKeyInAccount(final Account account, final String alias) {
2346        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": update key in account " + alias);
2347        try {
2348            X509Certificate[] chain = KeyChain.getCertificateChain(XmppConnectionService.this, alias);
2349            Log.d(Config.LOGTAG, account.getJid().asBareJid() + " loaded certificate chain");
2350            Pair<Jid, String> info = CryptoHelper.extractJidAndName(chain[0]);
2351            if (info == null) {
2352                showErrorToastInUi(R.string.certificate_does_not_contain_jid);
2353                return;
2354            }
2355            if (account.getJid().asBareJid().equals(info.first)) {
2356                account.setPrivateKeyAlias(alias);
2357                account.setDisplayName(info.second);
2358                databaseBackend.updateAccount(account);
2359                if (Config.X509_VERIFICATION) {
2360                    try {
2361                        getMemorizingTrustManager().getNonInteractive().checkClientTrusted(chain, "RSA");
2362                    } catch (CertificateException e) {
2363                        showErrorToastInUi(R.string.certificate_chain_is_not_trusted);
2364                    }
2365                    account.getAxolotlService().regenerateKeys(true);
2366                }
2367            } else {
2368                showErrorToastInUi(R.string.jid_does_not_match_certificate);
2369            }
2370        } catch (Exception e) {
2371            e.printStackTrace();
2372        }
2373    }
2374
2375    public boolean updateAccount(final Account account) {
2376        if (databaseBackend.updateAccount(account)) {
2377            account.setShowErrorNotification(true);
2378            this.statusListener.onStatusChanged(account);
2379            databaseBackend.updateAccount(account);
2380            reconnectAccountInBackground(account);
2381            updateAccountUi();
2382            getNotificationService().updateErrorNotification();
2383            toggleForegroundService();
2384            syncEnabledAccountSetting();
2385            mChannelDiscoveryService.cleanCache();
2386            return true;
2387        } else {
2388            return false;
2389        }
2390    }
2391
2392    public void updateAccountPasswordOnServer(final Account account, final String newPassword, final OnAccountPasswordChanged callback) {
2393        final IqPacket iq = getIqGenerator().generateSetPassword(account, newPassword);
2394        sendIqPacket(account, iq, (a, packet) -> {
2395            if (packet.getType() == IqPacket.TYPE.RESULT) {
2396                a.setPassword(newPassword);
2397                a.setOption(Account.OPTION_MAGIC_CREATE, false);
2398                databaseBackend.updateAccount(a);
2399                callback.onPasswordChangeSucceeded();
2400            } else {
2401                callback.onPasswordChangeFailed();
2402            }
2403        });
2404    }
2405
2406    public void deleteAccount(final Account account) {
2407        final boolean connected = account.getStatus() == Account.State.ONLINE;
2408        synchronized (this.conversations) {
2409            if (connected) {
2410                account.getAxolotlService().deleteOmemoIdentity();
2411            }
2412            for (final Conversation conversation : conversations) {
2413                if (conversation.getAccount() == account) {
2414                    if (conversation.getMode() == Conversation.MODE_MULTI) {
2415                        if (connected) {
2416                            leaveMuc(conversation);
2417                        }
2418                    }
2419                    conversations.remove(conversation);
2420                    mNotificationService.clear(conversation);
2421                }
2422            }
2423            if (account.getXmppConnection() != null) {
2424                new Thread(() -> disconnect(account, !connected)).start();
2425            }
2426            final Runnable runnable = () -> {
2427                if (!databaseBackend.deleteAccount(account)) {
2428                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to delete account");
2429                }
2430            };
2431            mDatabaseWriterExecutor.execute(runnable);
2432            this.accounts.remove(account);
2433            this.mRosterSyncTaskManager.clear(account);
2434            updateAccountUi();
2435            mNotificationService.updateErrorNotification();
2436            syncEnabledAccountSetting();
2437            toggleForegroundService();
2438        }
2439    }
2440
2441    public void setOnConversationListChangedListener(OnConversationUpdate listener) {
2442        final boolean remainingListeners;
2443        synchronized (LISTENER_LOCK) {
2444            remainingListeners = checkListeners();
2445            if (!this.mOnConversationUpdates.add(listener)) {
2446                Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as ConversationListChangedListener");
2447            }
2448            this.mNotificationService.setIsInForeground(this.mOnConversationUpdates.size() > 0);
2449        }
2450        if (remainingListeners) {
2451            switchToForeground();
2452        }
2453    }
2454
2455    public void removeOnConversationListChangedListener(OnConversationUpdate listener) {
2456        final boolean remainingListeners;
2457        synchronized (LISTENER_LOCK) {
2458            this.mOnConversationUpdates.remove(listener);
2459            this.mNotificationService.setIsInForeground(this.mOnConversationUpdates.size() > 0);
2460            remainingListeners = checkListeners();
2461        }
2462        if (remainingListeners) {
2463            switchToBackground();
2464        }
2465    }
2466
2467    public void setOnShowErrorToastListener(OnShowErrorToast listener) {
2468        final boolean remainingListeners;
2469        synchronized (LISTENER_LOCK) {
2470            remainingListeners = checkListeners();
2471            if (!this.mOnShowErrorToasts.add(listener)) {
2472                Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnShowErrorToastListener");
2473            }
2474        }
2475        if (remainingListeners) {
2476            switchToForeground();
2477        }
2478    }
2479
2480    public void removeOnShowErrorToastListener(OnShowErrorToast onShowErrorToast) {
2481        final boolean remainingListeners;
2482        synchronized (LISTENER_LOCK) {
2483            this.mOnShowErrorToasts.remove(onShowErrorToast);
2484            remainingListeners = checkListeners();
2485        }
2486        if (remainingListeners) {
2487            switchToBackground();
2488        }
2489    }
2490
2491    public void setOnAccountListChangedListener(OnAccountUpdate listener) {
2492        final boolean remainingListeners;
2493        synchronized (LISTENER_LOCK) {
2494            remainingListeners = checkListeners();
2495            if (!this.mOnAccountUpdates.add(listener)) {
2496                Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnAccountListChangedtListener");
2497            }
2498        }
2499        if (remainingListeners) {
2500            switchToForeground();
2501        }
2502    }
2503
2504    public void removeOnAccountListChangedListener(OnAccountUpdate listener) {
2505        final boolean remainingListeners;
2506        synchronized (LISTENER_LOCK) {
2507            this.mOnAccountUpdates.remove(listener);
2508            remainingListeners = checkListeners();
2509        }
2510        if (remainingListeners) {
2511            switchToBackground();
2512        }
2513    }
2514
2515    public void setOnCaptchaRequestedListener(OnCaptchaRequested listener) {
2516        final boolean remainingListeners;
2517        synchronized (LISTENER_LOCK) {
2518            remainingListeners = checkListeners();
2519            if (!this.mOnCaptchaRequested.add(listener)) {
2520                Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnCaptchaRequestListener");
2521            }
2522        }
2523        if (remainingListeners) {
2524            switchToForeground();
2525        }
2526    }
2527
2528    public void removeOnCaptchaRequestedListener(OnCaptchaRequested listener) {
2529        final boolean remainingListeners;
2530        synchronized (LISTENER_LOCK) {
2531            this.mOnCaptchaRequested.remove(listener);
2532            remainingListeners = checkListeners();
2533        }
2534        if (remainingListeners) {
2535            switchToBackground();
2536        }
2537    }
2538
2539    public void setOnRosterUpdateListener(final OnRosterUpdate listener) {
2540        final boolean remainingListeners;
2541        synchronized (LISTENER_LOCK) {
2542            remainingListeners = checkListeners();
2543            if (!this.mOnRosterUpdates.add(listener)) {
2544                Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnRosterUpdateListener");
2545            }
2546        }
2547        if (remainingListeners) {
2548            switchToForeground();
2549        }
2550    }
2551
2552    public void removeOnRosterUpdateListener(final OnRosterUpdate listener) {
2553        final boolean remainingListeners;
2554        synchronized (LISTENER_LOCK) {
2555            this.mOnRosterUpdates.remove(listener);
2556            remainingListeners = checkListeners();
2557        }
2558        if (remainingListeners) {
2559            switchToBackground();
2560        }
2561    }
2562
2563    public void setOnUpdateBlocklistListener(final OnUpdateBlocklist listener) {
2564        final boolean remainingListeners;
2565        synchronized (LISTENER_LOCK) {
2566            remainingListeners = checkListeners();
2567            if (!this.mOnUpdateBlocklist.add(listener)) {
2568                Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnUpdateBlocklistListener");
2569            }
2570        }
2571        if (remainingListeners) {
2572            switchToForeground();
2573        }
2574    }
2575
2576    public void removeOnUpdateBlocklistListener(final OnUpdateBlocklist listener) {
2577        final boolean remainingListeners;
2578        synchronized (LISTENER_LOCK) {
2579            this.mOnUpdateBlocklist.remove(listener);
2580            remainingListeners = checkListeners();
2581        }
2582        if (remainingListeners) {
2583            switchToBackground();
2584        }
2585    }
2586
2587    public void setOnKeyStatusUpdatedListener(final OnKeyStatusUpdated listener) {
2588        final boolean remainingListeners;
2589        synchronized (LISTENER_LOCK) {
2590            remainingListeners = checkListeners();
2591            if (!this.mOnKeyStatusUpdated.add(listener)) {
2592                Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnKeyStatusUpdateListener");
2593            }
2594        }
2595        if (remainingListeners) {
2596            switchToForeground();
2597        }
2598    }
2599
2600    public void removeOnNewKeysAvailableListener(final OnKeyStatusUpdated listener) {
2601        final boolean remainingListeners;
2602        synchronized (LISTENER_LOCK) {
2603            this.mOnKeyStatusUpdated.remove(listener);
2604            remainingListeners = checkListeners();
2605        }
2606        if (remainingListeners) {
2607            switchToBackground();
2608        }
2609    }
2610
2611    public void setOnRtpConnectionUpdateListener(final OnJingleRtpConnectionUpdate listener) {
2612        final boolean remainingListeners;
2613        synchronized (LISTENER_LOCK) {
2614            remainingListeners = checkListeners();
2615            if (!this.onJingleRtpConnectionUpdate.add(listener)) {
2616                Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnJingleRtpConnectionUpdate");
2617            }
2618        }
2619        if (remainingListeners) {
2620            switchToForeground();
2621        }
2622    }
2623
2624    public void removeRtpConnectionUpdateListener(final OnJingleRtpConnectionUpdate listener) {
2625        final boolean remainingListeners;
2626        synchronized (LISTENER_LOCK) {
2627            this.onJingleRtpConnectionUpdate.remove(listener);
2628            remainingListeners = checkListeners();
2629        }
2630        if (remainingListeners) {
2631            switchToBackground();
2632        }
2633    }
2634
2635    public void setOnMucRosterUpdateListener(OnMucRosterUpdate listener) {
2636        final boolean remainingListeners;
2637        synchronized (LISTENER_LOCK) {
2638            remainingListeners = checkListeners();
2639            if (!this.mOnMucRosterUpdate.add(listener)) {
2640                Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnMucRosterListener");
2641            }
2642        }
2643        if (remainingListeners) {
2644            switchToForeground();
2645        }
2646    }
2647
2648    public void removeOnMucRosterUpdateListener(final OnMucRosterUpdate listener) {
2649        final boolean remainingListeners;
2650        synchronized (LISTENER_LOCK) {
2651            this.mOnMucRosterUpdate.remove(listener);
2652            remainingListeners = checkListeners();
2653        }
2654        if (remainingListeners) {
2655            switchToBackground();
2656        }
2657    }
2658
2659    public boolean checkListeners() {
2660        return (this.mOnAccountUpdates.size() == 0
2661                && this.mOnConversationUpdates.size() == 0
2662                && this.mOnRosterUpdates.size() == 0
2663                && this.mOnCaptchaRequested.size() == 0
2664                && this.mOnMucRosterUpdate.size() == 0
2665                && this.mOnUpdateBlocklist.size() == 0
2666                && this.mOnShowErrorToasts.size() == 0
2667                && this.onJingleRtpConnectionUpdate.size() == 0
2668                && this.mOnKeyStatusUpdated.size() == 0);
2669    }
2670
2671    private void switchToForeground() {
2672        final boolean broadcastLastActivity = broadcastLastActivity();
2673        for (Conversation conversation : getConversations()) {
2674            if (conversation.getMode() == Conversation.MODE_MULTI) {
2675                conversation.getMucOptions().resetChatState();
2676            } else {
2677                conversation.setIncomingChatState(Config.DEFAULT_CHAT_STATE);
2678            }
2679        }
2680        for (Account account : getAccounts()) {
2681            if (account.getStatus() == Account.State.ONLINE) {
2682                account.deactivateGracePeriod();
2683                final XmppConnection connection = account.getXmppConnection();
2684                if (connection != null) {
2685                    if (connection.getFeatures().csi()) {
2686                        connection.sendActive();
2687                    }
2688                    if (broadcastLastActivity) {
2689                        sendPresence(account, false); //send new presence but don't include idle because we are not
2690                    }
2691                }
2692            }
2693        }
2694        Log.d(Config.LOGTAG, "app switched into foreground");
2695    }
2696
2697    private void switchToBackground() {
2698        final boolean broadcastLastActivity = broadcastLastActivity();
2699        if (broadcastLastActivity) {
2700            mLastActivity = System.currentTimeMillis();
2701            final SharedPreferences.Editor editor = getPreferences().edit();
2702            editor.putLong(SETTING_LAST_ACTIVITY_TS, mLastActivity);
2703            editor.apply();
2704        }
2705        for (Account account : getAccounts()) {
2706            if (account.getStatus() == Account.State.ONLINE) {
2707                XmppConnection connection = account.getXmppConnection();
2708                if (connection != null) {
2709                    if (broadcastLastActivity) {
2710                        sendPresence(account, true);
2711                    }
2712                    if (connection.getFeatures().csi()) {
2713                        connection.sendInactive();
2714                    }
2715                }
2716            }
2717        }
2718        this.mNotificationService.setIsInForeground(false);
2719        Log.d(Config.LOGTAG, "app switched into background");
2720    }
2721
2722    private void connectMultiModeConversations(Account account) {
2723        List<Conversation> conversations = getConversations();
2724        for (Conversation conversation : conversations) {
2725            if (conversation.getMode() == Conversation.MODE_MULTI && conversation.getAccount() == account) {
2726                joinMuc(conversation);
2727            }
2728        }
2729    }
2730
2731    public void mucSelfPingAndRejoin(final Conversation conversation) {
2732        final Account account = conversation.getAccount();
2733        synchronized (account.inProgressConferenceJoins) {
2734            if (account.inProgressConferenceJoins.contains(conversation)) {
2735                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": canceling muc self ping because join is already under way");
2736                return;
2737            }
2738        }
2739        synchronized (account.inProgressConferencePings) {
2740            if (!account.inProgressConferencePings.add(conversation)) {
2741                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": canceling muc self ping because ping is already under way");
2742                return;
2743            }
2744        }
2745        final Jid self = conversation.getMucOptions().getSelf().getFullJid();
2746        final IqPacket ping = new IqPacket(IqPacket.TYPE.GET);
2747        ping.setTo(self);
2748        ping.addChild("ping", Namespace.PING);
2749        sendIqPacket(conversation.getAccount(), ping, (a, response) -> {
2750            if (response.getType() == IqPacket.TYPE.ERROR) {
2751                Element error = response.findChild("error");
2752                if (error == null || error.hasChild("service-unavailable") || error.hasChild("feature-not-implemented") || error.hasChild("item-not-found")) {
2753                    Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": ping to " + self + " came back as ignorable error");
2754                } else {
2755                    Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": ping to " + self + " failed. attempting rejoin");
2756                    joinMuc(conversation);
2757                }
2758            } else if (response.getType() == IqPacket.TYPE.RESULT) {
2759                Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": ping to " + self + " came back fine");
2760            }
2761            synchronized (account.inProgressConferencePings) {
2762                account.inProgressConferencePings.remove(conversation);
2763            }
2764        });
2765    }
2766
2767    public void joinMuc(Conversation conversation) {
2768        joinMuc(conversation, null, false);
2769    }
2770
2771    public void joinMuc(Conversation conversation, boolean followedInvite) {
2772        joinMuc(conversation, null, followedInvite);
2773    }
2774
2775    private void joinMuc(Conversation conversation, final OnConferenceJoined onConferenceJoined) {
2776        joinMuc(conversation, onConferenceJoined, false);
2777    }
2778
2779    private void joinMuc(Conversation conversation, final OnConferenceJoined onConferenceJoined, final boolean followedInvite) {
2780        final Account account = conversation.getAccount();
2781        synchronized (account.pendingConferenceJoins) {
2782            account.pendingConferenceJoins.remove(conversation);
2783        }
2784        synchronized (account.pendingConferenceLeaves) {
2785            account.pendingConferenceLeaves.remove(conversation);
2786        }
2787        if (account.getStatus() == Account.State.ONLINE) {
2788            synchronized (account.inProgressConferenceJoins) {
2789                account.inProgressConferenceJoins.add(conversation);
2790            }
2791            if (Config.MUC_LEAVE_BEFORE_JOIN) {
2792                sendPresencePacket(account, mPresenceGenerator.leave(conversation.getMucOptions()));
2793            }
2794            conversation.resetMucOptions();
2795            if (onConferenceJoined != null) {
2796                conversation.getMucOptions().flagNoAutoPushConfiguration();
2797            }
2798            conversation.setHasMessagesLeftOnServer(false);
2799            fetchConferenceConfiguration(conversation, new OnConferenceConfigurationFetched() {
2800
2801                private void join(Conversation conversation) {
2802                    Account account = conversation.getAccount();
2803                    final MucOptions mucOptions = conversation.getMucOptions();
2804
2805                    if (mucOptions.nonanonymous() && !mucOptions.membersOnly() && !conversation.getBooleanAttribute("accept_non_anonymous", false)) {
2806                        synchronized (account.inProgressConferenceJoins) {
2807                            account.inProgressConferenceJoins.remove(conversation);
2808                        }
2809                        mucOptions.setError(MucOptions.Error.NON_ANONYMOUS);
2810                        updateConversationUi();
2811                        if (onConferenceJoined != null) {
2812                            onConferenceJoined.onConferenceJoined(conversation);
2813                        }
2814                        return;
2815                    }
2816
2817                    final Jid joinJid = mucOptions.getSelf().getFullJid();
2818                    Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": joining conversation " + joinJid.toString());
2819                    PresencePacket packet = mPresenceGenerator.selfPresence(account, Presence.Status.ONLINE, mucOptions.nonanonymous() || onConferenceJoined != null);
2820                    packet.setTo(joinJid);
2821                    Element x = packet.addChild("x", "http://jabber.org/protocol/muc");
2822                    if (conversation.getMucOptions().getPassword() != null) {
2823                        x.addChild("password").setContent(mucOptions.getPassword());
2824                    }
2825
2826                    if (mucOptions.mamSupport()) {
2827                        // Use MAM instead of the limited muc history to get history
2828                        x.addChild("history").setAttribute("maxchars", "0");
2829                    } else {
2830                        // Fallback to muc history
2831                        x.addChild("history").setAttribute("since", PresenceGenerator.getTimestamp(conversation.getLastMessageTransmitted().getTimestamp()));
2832                    }
2833                    sendPresencePacket(account, packet);
2834                    if (onConferenceJoined != null) {
2835                        onConferenceJoined.onConferenceJoined(conversation);
2836                    }
2837                    if (!joinJid.equals(conversation.getJid())) {
2838                        conversation.setContactJid(joinJid);
2839                        databaseBackend.updateConversation(conversation);
2840                    }
2841
2842                    if (mucOptions.mamSupport()) {
2843                        getMessageArchiveService().catchupMUC(conversation);
2844                    }
2845                    if (mucOptions.isPrivateAndNonAnonymous()) {
2846                        fetchConferenceMembers(conversation);
2847
2848                        if (followedInvite) {
2849                            final Bookmark bookmark = conversation.getBookmark();
2850                            if (bookmark != null) {
2851                                if (!bookmark.autojoin()) {
2852                                    bookmark.setAutojoin(true);
2853                                    createBookmark(account, bookmark);
2854                                }
2855                            } else {
2856                                saveConversationAsBookmark(conversation, null);
2857                            }
2858                        }
2859                    }
2860                    synchronized (account.inProgressConferenceJoins) {
2861                        account.inProgressConferenceJoins.remove(conversation);
2862                        sendUnsentMessages(conversation);
2863                    }
2864                }
2865
2866                @Override
2867                public void onConferenceConfigurationFetched(Conversation conversation) {
2868                    if (conversation.getStatus() == Conversation.STATUS_ARCHIVED) {
2869                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": conversation (" + conversation.getJid() + ") got archived before IQ result");
2870                        return;
2871                    }
2872                    join(conversation);
2873                }
2874
2875                @Override
2876                public void onFetchFailed(final Conversation conversation, final String errorCondition) {
2877                    if (conversation.getStatus() == Conversation.STATUS_ARCHIVED) {
2878                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": conversation (" + conversation.getJid() + ") got archived before IQ result");
2879                        return;
2880                    }
2881                    if ("remote-server-not-found".equals(errorCondition)) {
2882                        synchronized (account.inProgressConferenceJoins) {
2883                            account.inProgressConferenceJoins.remove(conversation);
2884                        }
2885                        conversation.getMucOptions().setError(MucOptions.Error.SERVER_NOT_FOUND);
2886                        updateConversationUi();
2887                    } else {
2888                        join(conversation);
2889                        fetchConferenceConfiguration(conversation);
2890                    }
2891                }
2892            });
2893            updateConversationUi();
2894        } else {
2895            synchronized (account.pendingConferenceJoins) {
2896                account.pendingConferenceJoins.add(conversation);
2897            }
2898            conversation.resetMucOptions();
2899            conversation.setHasMessagesLeftOnServer(false);
2900            updateConversationUi();
2901        }
2902    }
2903
2904    private void fetchConferenceMembers(final Conversation conversation) {
2905        final Account account = conversation.getAccount();
2906        final AxolotlService axolotlService = account.getAxolotlService();
2907        final String[] affiliations = {"member", "admin", "owner"};
2908        OnIqPacketReceived callback = new OnIqPacketReceived() {
2909
2910            private int i = 0;
2911            private boolean success = true;
2912
2913            @Override
2914            public void onIqPacketReceived(Account account, IqPacket packet) {
2915                final boolean omemoEnabled = conversation.getNextEncryption() == Message.ENCRYPTION_AXOLOTL;
2916                Element query = packet.query("http://jabber.org/protocol/muc#admin");
2917                if (packet.getType() == IqPacket.TYPE.RESULT && query != null) {
2918                    for (Element child : query.getChildren()) {
2919                        if ("item".equals(child.getName())) {
2920                            MucOptions.User user = AbstractParser.parseItem(conversation, child);
2921                            if (!user.realJidMatchesAccount()) {
2922                                boolean isNew = conversation.getMucOptions().updateUser(user);
2923                                Contact contact = user.getContact();
2924                                if (omemoEnabled
2925                                        && isNew
2926                                        && user.getRealJid() != null
2927                                        && (contact == null || !contact.mutualPresenceSubscription())
2928                                        && axolotlService.hasEmptyDeviceList(user.getRealJid())) {
2929                                    axolotlService.fetchDeviceIds(user.getRealJid());
2930                                }
2931                            }
2932                        }
2933                    }
2934                } else {
2935                    success = false;
2936                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not request affiliation " + affiliations[i] + " in " + conversation.getJid().asBareJid());
2937                }
2938                ++i;
2939                if (i >= affiliations.length) {
2940                    List<Jid> members = conversation.getMucOptions().getMembers(true);
2941                    if (success) {
2942                        List<Jid> cryptoTargets = conversation.getAcceptedCryptoTargets();
2943                        boolean changed = false;
2944                        for (ListIterator<Jid> iterator = cryptoTargets.listIterator(); iterator.hasNext(); ) {
2945                            Jid jid = iterator.next();
2946                            if (!members.contains(jid) && !members.contains(jid.getDomain())) {
2947                                iterator.remove();
2948                                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": removed " + jid + " from crypto targets of " + conversation.getName());
2949                                changed = true;
2950                            }
2951                        }
2952                        if (changed) {
2953                            conversation.setAcceptedCryptoTargets(cryptoTargets);
2954                            updateConversation(conversation);
2955                        }
2956                    }
2957                    getAvatarService().clear(conversation);
2958                    updateMucRosterUi();
2959                    updateConversationUi();
2960                }
2961            }
2962        };
2963        for (String affiliation : affiliations) {
2964            sendIqPacket(account, mIqGenerator.queryAffiliation(conversation, affiliation), callback);
2965        }
2966        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": fetching members for " + conversation.getName());
2967    }
2968
2969    public void providePasswordForMuc(Conversation conversation, String password) {
2970        if (conversation.getMode() == Conversation.MODE_MULTI) {
2971            conversation.getMucOptions().setPassword(password);
2972            if (conversation.getBookmark() != null) {
2973                final Bookmark bookmark = conversation.getBookmark();
2974                if (synchronizeWithBookmarks()) {
2975                    bookmark.setAutojoin(true);
2976                }
2977                createBookmark(conversation.getAccount(), bookmark);
2978            }
2979            updateConversation(conversation);
2980            joinMuc(conversation);
2981        }
2982    }
2983
2984    private boolean hasEnabledAccounts() {
2985        if (this.accounts == null) {
2986            return false;
2987        }
2988        for (Account account : this.accounts) {
2989            if (account.isEnabled()) {
2990                return true;
2991            }
2992        }
2993        return false;
2994    }
2995
2996
2997    public void getAttachments(final Conversation conversation, int limit, final OnMediaLoaded onMediaLoaded) {
2998        getAttachments(conversation.getAccount(), conversation.getJid().asBareJid(), limit, onMediaLoaded);
2999    }
3000
3001    public void getAttachments(final Account account, final Jid jid, final int limit, final OnMediaLoaded onMediaLoaded) {
3002        getAttachments(account.getUuid(), jid.asBareJid(), limit, onMediaLoaded);
3003    }
3004
3005
3006    public void getAttachments(final String account, final Jid jid, final int limit, final OnMediaLoaded onMediaLoaded) {
3007        new Thread(() -> onMediaLoaded.onMediaLoaded(fileBackend.convertToAttachments(databaseBackend.getRelativeFilePaths(account, jid, limit)))).start();
3008    }
3009
3010    public void persistSelfNick(MucOptions.User self) {
3011        final Conversation conversation = self.getConversation();
3012        final boolean tookProposedNickFromBookmark = conversation.getMucOptions().isTookProposedNickFromBookmark();
3013        Jid full = self.getFullJid();
3014        if (!full.equals(conversation.getJid())) {
3015            Log.d(Config.LOGTAG, "nick changed. updating");
3016            conversation.setContactJid(full);
3017            databaseBackend.updateConversation(conversation);
3018        }
3019
3020        final Bookmark bookmark = conversation.getBookmark();
3021        final String bookmarkedNick = bookmark == null ? null : bookmark.getNick();
3022        if (bookmark != null && (tookProposedNickFromBookmark || TextUtils.isEmpty(bookmarkedNick)) && !full.getResource().equals(bookmarkedNick)) {
3023            final Account account = conversation.getAccount();
3024            final String defaultNick = MucOptions.defaultNick(account);
3025            if (TextUtils.isEmpty(bookmarkedNick) && full.getResource().equals(defaultNick)) {
3026                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": do not overwrite empty bookmark nick with default nick for " + conversation.getJid().asBareJid());
3027                return;
3028            }
3029            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": persist nick '" + full.getResource() + "' into bookmark for " + conversation.getJid().asBareJid());
3030            bookmark.setNick(full.getResource());
3031            createBookmark(bookmark.getAccount(), bookmark);
3032        }
3033    }
3034
3035    public boolean renameInMuc(final Conversation conversation, final String nick, final UiCallback<Conversation> callback) {
3036        final MucOptions options = conversation.getMucOptions();
3037        final Jid joinJid = options.createJoinJid(nick);
3038        if (joinJid == null) {
3039            return false;
3040        }
3041        if (options.online()) {
3042            Account account = conversation.getAccount();
3043            options.setOnRenameListener(new OnRenameListener() {
3044
3045                @Override
3046                public void onSuccess() {
3047                    callback.success(conversation);
3048                }
3049
3050                @Override
3051                public void onFailure() {
3052                    callback.error(R.string.nick_in_use, conversation);
3053                }
3054            });
3055
3056            final PresencePacket packet = mPresenceGenerator.selfPresence(account, Presence.Status.ONLINE, options.nonanonymous());
3057            packet.setTo(joinJid);
3058            sendPresencePacket(account, packet);
3059        } else {
3060            conversation.setContactJid(joinJid);
3061            databaseBackend.updateConversation(conversation);
3062            if (conversation.getAccount().getStatus() == Account.State.ONLINE) {
3063                Bookmark bookmark = conversation.getBookmark();
3064                if (bookmark != null) {
3065                    bookmark.setNick(nick);
3066                    createBookmark(bookmark.getAccount(), bookmark);
3067                }
3068                joinMuc(conversation);
3069            }
3070        }
3071        return true;
3072    }
3073
3074    public void leaveMuc(Conversation conversation) {
3075        leaveMuc(conversation, false);
3076    }
3077
3078    private void leaveMuc(Conversation conversation, boolean now) {
3079        final Account account = conversation.getAccount();
3080        synchronized (account.pendingConferenceJoins) {
3081            account.pendingConferenceJoins.remove(conversation);
3082        }
3083        synchronized (account.pendingConferenceLeaves) {
3084            account.pendingConferenceLeaves.remove(conversation);
3085        }
3086        if (account.getStatus() == Account.State.ONLINE || now) {
3087            sendPresencePacket(conversation.getAccount(), mPresenceGenerator.leave(conversation.getMucOptions()));
3088            conversation.getMucOptions().setOffline();
3089            Bookmark bookmark = conversation.getBookmark();
3090            if (bookmark != null) {
3091                bookmark.setConversation(null);
3092            }
3093            Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": leaving muc " + conversation.getJid());
3094        } else {
3095            synchronized (account.pendingConferenceLeaves) {
3096                account.pendingConferenceLeaves.add(conversation);
3097            }
3098        }
3099    }
3100
3101    public String findConferenceServer(final Account account) {
3102        String server;
3103        if (account.getXmppConnection() != null) {
3104            server = account.getXmppConnection().getMucServer();
3105            if (server != null) {
3106                return server;
3107            }
3108        }
3109        for (Account other : getAccounts()) {
3110            if (other != account && other.getXmppConnection() != null) {
3111                server = other.getXmppConnection().getMucServer();
3112                if (server != null) {
3113                    return server;
3114                }
3115            }
3116        }
3117        return null;
3118    }
3119
3120
3121    public void createPublicChannel(final Account account, final String name, final Jid address, final UiCallback<Conversation> callback) {
3122        joinMuc(findOrCreateConversation(account, address, true, false, true), conversation -> {
3123            final Bundle configuration = IqGenerator.defaultChannelConfiguration();
3124            if (!TextUtils.isEmpty(name)) {
3125                configuration.putString("muc#roomconfig_roomname", name);
3126            }
3127            pushConferenceConfiguration(conversation, configuration, new OnConfigurationPushed() {
3128                @Override
3129                public void onPushSucceeded() {
3130                    saveConversationAsBookmark(conversation, name);
3131                    callback.success(conversation);
3132                }
3133
3134                @Override
3135                public void onPushFailed() {
3136                    if (conversation.getMucOptions().getSelf().getAffiliation().ranks(MucOptions.Affiliation.OWNER)) {
3137                        callback.error(R.string.unable_to_set_channel_configuration, conversation);
3138                    } else {
3139                        callback.error(R.string.joined_an_existing_channel, conversation);
3140                    }
3141                }
3142            });
3143        });
3144    }
3145
3146    public boolean createAdhocConference(final Account account,
3147                                         final String name,
3148                                         final Iterable<Jid> jids,
3149                                         final UiCallback<Conversation> callback) {
3150        Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": creating adhoc conference with " + jids.toString());
3151        if (account.getStatus() == Account.State.ONLINE) {
3152            try {
3153                String server = findConferenceServer(account);
3154                if (server == null) {
3155                    if (callback != null) {
3156                        callback.error(R.string.no_conference_server_found, null);
3157                    }
3158                    return false;
3159                }
3160                final Jid jid = Jid.of(CryptoHelper.pronounceable(getRNG()), server, null);
3161                final Conversation conversation = findOrCreateConversation(account, jid, true, false, true);
3162                joinMuc(conversation, new OnConferenceJoined() {
3163                    @Override
3164                    public void onConferenceJoined(final Conversation conversation) {
3165                        final Bundle configuration = IqGenerator.defaultGroupChatConfiguration();
3166                        if (!TextUtils.isEmpty(name)) {
3167                            configuration.putString("muc#roomconfig_roomname", name);
3168                        }
3169                        pushConferenceConfiguration(conversation, configuration, new OnConfigurationPushed() {
3170                            @Override
3171                            public void onPushSucceeded() {
3172                                for (Jid invite : jids) {
3173                                    invite(conversation, invite);
3174                                }
3175                                for (String resource : account.getSelfContact().getPresences().toResourceArray()) {
3176                                    Jid other = account.getJid().withResource(resource);
3177                                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": sending direct invite to " + other);
3178                                    directInvite(conversation, other);
3179                                }
3180                                saveConversationAsBookmark(conversation, name);
3181                                if (callback != null) {
3182                                    callback.success(conversation);
3183                                }
3184                            }
3185
3186                            @Override
3187                            public void onPushFailed() {
3188                                archiveConversation(conversation);
3189                                if (callback != null) {
3190                                    callback.error(R.string.conference_creation_failed, conversation);
3191                                }
3192                            }
3193                        });
3194                    }
3195                });
3196                return true;
3197            } catch (IllegalArgumentException e) {
3198                if (callback != null) {
3199                    callback.error(R.string.conference_creation_failed, null);
3200                }
3201                return false;
3202            }
3203        } else {
3204            if (callback != null) {
3205                callback.error(R.string.not_connected_try_again, null);
3206            }
3207            return false;
3208        }
3209    }
3210
3211    public void fetchConferenceConfiguration(final Conversation conversation) {
3212        fetchConferenceConfiguration(conversation, null);
3213    }
3214
3215    public void fetchConferenceConfiguration(final Conversation conversation, final OnConferenceConfigurationFetched callback) {
3216        IqPacket request = mIqGenerator.queryDiscoInfo(conversation.getJid().asBareJid());
3217        sendIqPacket(conversation.getAccount(), request, new OnIqPacketReceived() {
3218            @Override
3219            public void onIqPacketReceived(Account account, IqPacket packet) {
3220                if (packet.getType() == IqPacket.TYPE.RESULT) {
3221                    final MucOptions mucOptions = conversation.getMucOptions();
3222                    final Bookmark bookmark = conversation.getBookmark();
3223                    final boolean sameBefore = StringUtils.equals(bookmark == null ? null : bookmark.getBookmarkName(), mucOptions.getName());
3224
3225                    if (mucOptions.updateConfiguration(new ServiceDiscoveryResult(packet))) {
3226                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": muc configuration changed for " + conversation.getJid().asBareJid());
3227                        updateConversation(conversation);
3228                    }
3229
3230                    if (bookmark != null && (sameBefore || bookmark.getBookmarkName() == null)) {
3231                        if (bookmark.setBookmarkName(StringUtils.nullOnEmpty(mucOptions.getName()))) {
3232                            createBookmark(account, bookmark);
3233                        }
3234                    }
3235
3236
3237                    if (callback != null) {
3238                        callback.onConferenceConfigurationFetched(conversation);
3239                    }
3240
3241
3242                    updateConversationUi();
3243                } else if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
3244                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received timeout waiting for conference configuration fetch");
3245                } else {
3246                    if (callback != null) {
3247                        callback.onFetchFailed(conversation, packet.getErrorCondition());
3248                    }
3249                }
3250            }
3251        });
3252    }
3253
3254    public void pushNodeConfiguration(Account account, final String node, final Bundle options, final OnConfigurationPushed callback) {
3255        pushNodeConfiguration(account, account.getJid().asBareJid(), node, options, callback);
3256    }
3257
3258    public void pushNodeConfiguration(Account account, final Jid jid, final String node, final Bundle options, final OnConfigurationPushed callback) {
3259        Log.d(Config.LOGTAG, "pushing node configuration");
3260        sendIqPacket(account, mIqGenerator.requestPubsubConfiguration(jid, node), new OnIqPacketReceived() {
3261            @Override
3262            public void onIqPacketReceived(Account account, IqPacket packet) {
3263                if (packet.getType() == IqPacket.TYPE.RESULT) {
3264                    Element pubsub = packet.findChild("pubsub", "http://jabber.org/protocol/pubsub#owner");
3265                    Element configuration = pubsub == null ? null : pubsub.findChild("configure");
3266                    Element x = configuration == null ? null : configuration.findChild("x", Namespace.DATA);
3267                    if (x != null) {
3268                        Data data = Data.parse(x);
3269                        data.submit(options);
3270                        sendIqPacket(account, mIqGenerator.publishPubsubConfiguration(jid, node, data), new OnIqPacketReceived() {
3271                            @Override
3272                            public void onIqPacketReceived(Account account, IqPacket packet) {
3273                                if (packet.getType() == IqPacket.TYPE.RESULT && callback != null) {
3274                                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": successfully changed node configuration for node " + node);
3275                                    callback.onPushSucceeded();
3276                                } else if (packet.getType() == IqPacket.TYPE.ERROR && callback != null) {
3277                                    callback.onPushFailed();
3278                                }
3279                            }
3280                        });
3281                    } else if (callback != null) {
3282                        callback.onPushFailed();
3283                    }
3284                } else if (packet.getType() == IqPacket.TYPE.ERROR && callback != null) {
3285                    callback.onPushFailed();
3286                }
3287            }
3288        });
3289    }
3290
3291    public void pushConferenceConfiguration(final Conversation conversation, final Bundle options, final OnConfigurationPushed callback) {
3292        if (options.getString("muc#roomconfig_whois", "moderators").equals("anyone")) {
3293            conversation.setAttribute("accept_non_anonymous", true);
3294            updateConversation(conversation);
3295        }
3296        if (options.containsKey("muc#roomconfig_moderatedroom")) {
3297            final boolean moderated = "1".equals(options.getString("muc#roomconfig_moderatedroom"));
3298            options.putString("members_by_default", moderated ? "0" : "1");
3299        }
3300        final IqPacket request = new IqPacket(IqPacket.TYPE.GET);
3301        request.setTo(conversation.getJid().asBareJid());
3302        request.query("http://jabber.org/protocol/muc#owner");
3303        sendIqPacket(conversation.getAccount(), request, new OnIqPacketReceived() {
3304            @Override
3305            public void onIqPacketReceived(Account account, IqPacket packet) {
3306                if (packet.getType() == IqPacket.TYPE.RESULT) {
3307                    final Data data = Data.parse(packet.query().findChild("x", Namespace.DATA));
3308                    data.submit(options);
3309                    final IqPacket set = new IqPacket(IqPacket.TYPE.SET);
3310                    set.setTo(conversation.getJid().asBareJid());
3311                    set.query("http://jabber.org/protocol/muc#owner").addChild(data);
3312                    sendIqPacket(account, set, new OnIqPacketReceived() {
3313                        @Override
3314                        public void onIqPacketReceived(Account account, IqPacket packet) {
3315                            if (callback != null) {
3316                                if (packet.getType() == IqPacket.TYPE.RESULT) {
3317                                    callback.onPushSucceeded();
3318                                } else {
3319                                    callback.onPushFailed();
3320                                }
3321                            }
3322                        }
3323                    });
3324                } else {
3325                    if (callback != null) {
3326                        callback.onPushFailed();
3327                    }
3328                }
3329            }
3330        });
3331    }
3332
3333    public void pushSubjectToConference(final Conversation conference, final String subject) {
3334        MessagePacket packet = this.getMessageGenerator().conferenceSubject(conference, StringUtils.nullOnEmpty(subject));
3335        this.sendMessagePacket(conference.getAccount(), packet);
3336    }
3337
3338    public void changeAffiliationInConference(final Conversation conference, Jid user, final MucOptions.Affiliation affiliation, final OnAffiliationChanged callback) {
3339        final Jid jid = user.asBareJid();
3340        IqPacket request = this.mIqGenerator.changeAffiliation(conference, jid, affiliation.toString());
3341        sendIqPacket(conference.getAccount(), request, new OnIqPacketReceived() {
3342            @Override
3343            public void onIqPacketReceived(Account account, IqPacket packet) {
3344                if (packet.getType() == IqPacket.TYPE.RESULT) {
3345                    conference.getMucOptions().changeAffiliation(jid, affiliation);
3346                    getAvatarService().clear(conference);
3347                    callback.onAffiliationChangedSuccessful(jid);
3348                } else {
3349                    callback.onAffiliationChangeFailed(jid, R.string.could_not_change_affiliation);
3350                }
3351            }
3352        });
3353    }
3354
3355    public void changeAffiliationsInConference(final Conversation conference, MucOptions.Affiliation before, MucOptions.Affiliation after) {
3356        List<Jid> jids = new ArrayList<>();
3357        for (MucOptions.User user : conference.getMucOptions().getUsers()) {
3358            if (user.getAffiliation() == before && user.getRealJid() != null) {
3359                jids.add(user.getRealJid());
3360            }
3361        }
3362        IqPacket request = this.mIqGenerator.changeAffiliation(conference, jids, after.toString());
3363        sendIqPacket(conference.getAccount(), request, mDefaultIqHandler);
3364    }
3365
3366    public void changeRoleInConference(final Conversation conference, final String nick, MucOptions.Role role) {
3367        IqPacket request = this.mIqGenerator.changeRole(conference, nick, role.toString());
3368        Log.d(Config.LOGTAG, request.toString());
3369        sendIqPacket(conference.getAccount(), request, (account, packet) -> {
3370            if (packet.getType() != IqPacket.TYPE.RESULT) {
3371                Log.d(Config.LOGTAG, account.getJid().asBareJid() + " unable to change role of " + nick);
3372            }
3373        });
3374    }
3375
3376    public void destroyRoom(final Conversation conversation, final OnRoomDestroy callback) {
3377        IqPacket request = new IqPacket(IqPacket.TYPE.SET);
3378        request.setTo(conversation.getJid().asBareJid());
3379        request.query("http://jabber.org/protocol/muc#owner").addChild("destroy");
3380        sendIqPacket(conversation.getAccount(), request, new OnIqPacketReceived() {
3381            @Override
3382            public void onIqPacketReceived(Account account, IqPacket packet) {
3383                if (packet.getType() == IqPacket.TYPE.RESULT) {
3384                    if (callback != null) {
3385                        callback.onRoomDestroySucceeded();
3386                    }
3387                } else if (packet.getType() == IqPacket.TYPE.ERROR) {
3388                    if (callback != null) {
3389                        callback.onRoomDestroyFailed();
3390                    }
3391                }
3392            }
3393        });
3394    }
3395
3396    private void disconnect(Account account, boolean force) {
3397        if ((account.getStatus() == Account.State.ONLINE)
3398                || (account.getStatus() == Account.State.DISABLED)) {
3399            final XmppConnection connection = account.getXmppConnection();
3400            if (!force) {
3401                List<Conversation> conversations = getConversations();
3402                for (Conversation conversation : conversations) {
3403                    if (conversation.getAccount() == account) {
3404                        if (conversation.getMode() == Conversation.MODE_MULTI) {
3405                            leaveMuc(conversation, true);
3406                        }
3407                    }
3408                }
3409                sendOfflinePresence(account);
3410            }
3411            connection.disconnect(force);
3412        }
3413    }
3414
3415    @Override
3416    public IBinder onBind(Intent intent) {
3417        return mBinder;
3418    }
3419
3420    public void updateMessage(Message message) {
3421        updateMessage(message, true);
3422    }
3423
3424    public void updateMessage(Message message, boolean includeBody) {
3425        databaseBackend.updateMessage(message, includeBody);
3426        updateConversationUi();
3427    }
3428
3429    public void createMessageAsync(final Message message) {
3430        mDatabaseWriterExecutor.execute(() -> databaseBackend.createMessage(message));
3431    }
3432
3433    public void updateMessage(Message message, String uuid) {
3434        if (!databaseBackend.updateMessage(message, uuid)) {
3435            Log.e(Config.LOGTAG, "error updated message in DB after edit");
3436        }
3437        updateConversationUi();
3438    }
3439
3440    protected void syncDirtyContacts(Account account) {
3441        for (Contact contact : account.getRoster().getContacts()) {
3442            if (contact.getOption(Contact.Options.DIRTY_PUSH)) {
3443                pushContactToServer(contact);
3444            }
3445            if (contact.getOption(Contact.Options.DIRTY_DELETE)) {
3446                deleteContactOnServer(contact);
3447            }
3448        }
3449    }
3450
3451    public void createContact(final Contact contact, final boolean autoGrant) {
3452        createContact(contact, autoGrant, null);
3453    }
3454
3455    public void createContact(final Contact contact, final boolean autoGrant, final String preAuth) {
3456        if (autoGrant) {
3457            contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
3458            contact.setOption(Contact.Options.ASKING);
3459        }
3460        pushContactToServer(contact, preAuth);
3461    }
3462
3463    public void pushContactToServer(final Contact contact) {
3464        pushContactToServer(contact, null);
3465    }
3466
3467    private void pushContactToServer(final Contact contact, final String preAuth) {
3468        contact.resetOption(Contact.Options.DIRTY_DELETE);
3469        contact.setOption(Contact.Options.DIRTY_PUSH);
3470        final Account account = contact.getAccount();
3471        if (account.getStatus() == Account.State.ONLINE) {
3472            final boolean ask = contact.getOption(Contact.Options.ASKING);
3473            final boolean sendUpdates = contact
3474                    .getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)
3475                    && contact.getOption(Contact.Options.PREEMPTIVE_GRANT);
3476            final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
3477            iq.query(Namespace.ROSTER).addChild(contact.asElement());
3478            account.getXmppConnection().sendIqPacket(iq, mDefaultIqHandler);
3479            if (sendUpdates) {
3480                sendPresencePacket(account, mPresenceGenerator.sendPresenceUpdatesTo(contact));
3481            }
3482            if (ask) {
3483                sendPresencePacket(account, mPresenceGenerator.requestPresenceUpdatesFrom(contact, preAuth));
3484            }
3485        } else {
3486            syncRoster(contact.getAccount());
3487        }
3488    }
3489
3490    public void publishMucAvatar(final Conversation conversation, final Uri image, final OnAvatarPublication callback) {
3491        new Thread(() -> {
3492            final Bitmap.CompressFormat format = Config.AVATAR_FORMAT;
3493            final int size = Config.AVATAR_SIZE;
3494            final Avatar avatar = getFileBackend().getPepAvatar(image, size, format);
3495            if (avatar != null) {
3496                if (!getFileBackend().save(avatar)) {
3497                    callback.onAvatarPublicationFailed(R.string.error_saving_avatar);
3498                    return;
3499                }
3500                avatar.owner = conversation.getJid().asBareJid();
3501                publishMucAvatar(conversation, avatar, callback);
3502            } else {
3503                callback.onAvatarPublicationFailed(R.string.error_publish_avatar_converting);
3504            }
3505        }).start();
3506    }
3507
3508    public void publishAvatar(final Account account, final Uri image, final OnAvatarPublication callback) {
3509        new Thread(() -> {
3510            final Bitmap.CompressFormat format = Config.AVATAR_FORMAT;
3511            final int size = Config.AVATAR_SIZE;
3512            final Avatar avatar = getFileBackend().getPepAvatar(image, size, format);
3513            if (avatar != null) {
3514                if (!getFileBackend().save(avatar)) {
3515                    Log.d(Config.LOGTAG, "unable to save vcard");
3516                    callback.onAvatarPublicationFailed(R.string.error_saving_avatar);
3517                    return;
3518                }
3519                publishAvatar(account, avatar, callback);
3520            } else {
3521                callback.onAvatarPublicationFailed(R.string.error_publish_avatar_converting);
3522            }
3523        }).start();
3524
3525    }
3526
3527    private void publishMucAvatar(Conversation conversation, Avatar avatar, OnAvatarPublication callback) {
3528        final IqPacket retrieve = mIqGenerator.retrieveVcardAvatar(avatar);
3529        sendIqPacket(conversation.getAccount(), retrieve, (account, response) -> {
3530            boolean itemNotFound = response.getType() == IqPacket.TYPE.ERROR && response.hasChild("error") && response.findChild("error").hasChild("item-not-found");
3531            if (response.getType() == IqPacket.TYPE.RESULT || itemNotFound) {
3532                Element vcard = response.findChild("vCard", "vcard-temp");
3533                if (vcard == null) {
3534                    vcard = new Element("vCard", "vcard-temp");
3535                }
3536                Element photo = vcard.findChild("PHOTO");
3537                if (photo == null) {
3538                    photo = vcard.addChild("PHOTO");
3539                }
3540                photo.clearChildren();
3541                photo.addChild("TYPE").setContent(avatar.type);
3542                photo.addChild("BINVAL").setContent(avatar.image);
3543                IqPacket publication = new IqPacket(IqPacket.TYPE.SET);
3544                publication.setTo(conversation.getJid().asBareJid());
3545                publication.addChild(vcard);
3546                sendIqPacket(account, publication, (a1, publicationResponse) -> {
3547                    if (publicationResponse.getType() == IqPacket.TYPE.RESULT) {
3548                        callback.onAvatarPublicationSucceeded();
3549                    } else {
3550                        Log.d(Config.LOGTAG, "failed to publish vcard " + publicationResponse.getErrorCondition());
3551                        callback.onAvatarPublicationFailed(R.string.error_publish_avatar_server_reject);
3552                    }
3553                });
3554            } else {
3555                Log.d(Config.LOGTAG, "failed to request vcard " + response.toString());
3556                callback.onAvatarPublicationFailed(R.string.error_publish_avatar_no_server_support);
3557            }
3558        });
3559    }
3560
3561    public void publishAvatar(Account account, final Avatar avatar, final OnAvatarPublication callback) {
3562        final Bundle options;
3563        if (account.getXmppConnection().getFeatures().pepPublishOptions()) {
3564            options = PublishOptions.openAccess();
3565        } else {
3566            options = null;
3567        }
3568        publishAvatar(account, avatar, options, true, callback);
3569    }
3570
3571    public void publishAvatar(Account account, final Avatar avatar, final Bundle options, final boolean retry, final OnAvatarPublication callback) {
3572        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": publishing avatar. options=" + options);
3573        IqPacket packet = this.mIqGenerator.publishAvatar(avatar, options);
3574        this.sendIqPacket(account, packet, new OnIqPacketReceived() {
3575
3576            @Override
3577            public void onIqPacketReceived(Account account, IqPacket result) {
3578                if (result.getType() == IqPacket.TYPE.RESULT) {
3579                    publishAvatarMetadata(account, avatar, options, true, callback);
3580                } else if (retry && PublishOptions.preconditionNotMet(result)) {
3581                    pushNodeConfiguration(account, "urn:xmpp:avatar:data", options, new OnConfigurationPushed() {
3582                        @Override
3583                        public void onPushSucceeded() {
3584                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": changed node configuration for avatar node");
3585                            publishAvatar(account, avatar, options, false, callback);
3586                        }
3587
3588                        @Override
3589                        public void onPushFailed() {
3590                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to change node configuration for avatar node");
3591                            publishAvatar(account, avatar, null, false, callback);
3592                        }
3593                    });
3594                } else {
3595                    Element error = result.findChild("error");
3596                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server rejected avatar " + (avatar.size / 1024) + "KiB " + (error != null ? error.toString() : ""));
3597                    if (callback != null) {
3598                        callback.onAvatarPublicationFailed(R.string.error_publish_avatar_server_reject);
3599                    }
3600                }
3601            }
3602        });
3603    }
3604
3605    public void publishAvatarMetadata(Account account, final Avatar avatar, final Bundle options, final boolean retry, final OnAvatarPublication callback) {
3606        final IqPacket packet = XmppConnectionService.this.mIqGenerator.publishAvatarMetadata(avatar, options);
3607        sendIqPacket(account, packet, new OnIqPacketReceived() {
3608            @Override
3609            public void onIqPacketReceived(Account account, IqPacket result) {
3610                if (result.getType() == IqPacket.TYPE.RESULT) {
3611                    if (account.setAvatar(avatar.getFilename())) {
3612                        getAvatarService().clear(account);
3613                        databaseBackend.updateAccount(account);
3614                        notifyAccountAvatarHasChanged(account);
3615                    }
3616                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": published avatar " + (avatar.size / 1024) + "KiB");
3617                    if (callback != null) {
3618                        callback.onAvatarPublicationSucceeded();
3619                    }
3620                } else if (retry && PublishOptions.preconditionNotMet(result)) {
3621                    pushNodeConfiguration(account, "urn:xmpp:avatar:metadata", options, new OnConfigurationPushed() {
3622                        @Override
3623                        public void onPushSucceeded() {
3624                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": changed node configuration for avatar meta data node");
3625                            publishAvatarMetadata(account, avatar, options, false, callback);
3626                        }
3627
3628                        @Override
3629                        public void onPushFailed() {
3630                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to change node configuration for avatar meta data node");
3631                            publishAvatarMetadata(account, avatar, null, false, callback);
3632                        }
3633                    });
3634                } else {
3635                    if (callback != null) {
3636                        callback.onAvatarPublicationFailed(R.string.error_publish_avatar_server_reject);
3637                    }
3638                }
3639            }
3640        });
3641    }
3642
3643    public void republishAvatarIfNeeded(Account account) {
3644        if (account.getAxolotlService().isPepBroken()) {
3645            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": skipping republication of avatar because pep is broken");
3646            return;
3647        }
3648        IqPacket packet = this.mIqGenerator.retrieveAvatarMetaData(null);
3649        this.sendIqPacket(account, packet, new OnIqPacketReceived() {
3650
3651            private Avatar parseAvatar(IqPacket packet) {
3652                Element pubsub = packet.findChild("pubsub", "http://jabber.org/protocol/pubsub");
3653                if (pubsub != null) {
3654                    Element items = pubsub.findChild("items");
3655                    if (items != null) {
3656                        return Avatar.parseMetadata(items);
3657                    }
3658                }
3659                return null;
3660            }
3661
3662            private boolean errorIsItemNotFound(IqPacket packet) {
3663                Element error = packet.findChild("error");
3664                return packet.getType() == IqPacket.TYPE.ERROR
3665                        && error != null
3666                        && error.hasChild("item-not-found");
3667            }
3668
3669            @Override
3670            public void onIqPacketReceived(Account account, IqPacket packet) {
3671                if (packet.getType() == IqPacket.TYPE.RESULT || errorIsItemNotFound(packet)) {
3672                    Avatar serverAvatar = parseAvatar(packet);
3673                    if (serverAvatar == null && account.getAvatar() != null) {
3674                        Avatar avatar = fileBackend.getStoredPepAvatar(account.getAvatar());
3675                        if (avatar != null) {
3676                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": avatar on server was null. republishing");
3677                            publishAvatar(account, fileBackend.getStoredPepAvatar(account.getAvatar()), null);
3678                        } else {
3679                            Log.e(Config.LOGTAG, account.getJid().asBareJid() + ": error rereading avatar");
3680                        }
3681                    }
3682                }
3683            }
3684        });
3685    }
3686
3687    public void fetchAvatar(Account account, Avatar avatar) {
3688        fetchAvatar(account, avatar, null);
3689    }
3690
3691    public void fetchAvatar(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
3692        final String KEY = generateFetchKey(account, avatar);
3693        synchronized (this.mInProgressAvatarFetches) {
3694            if (mInProgressAvatarFetches.add(KEY)) {
3695                switch (avatar.origin) {
3696                    case PEP:
3697                        this.mInProgressAvatarFetches.add(KEY);
3698                        fetchAvatarPep(account, avatar, callback);
3699                        break;
3700                    case VCARD:
3701                        this.mInProgressAvatarFetches.add(KEY);
3702                        fetchAvatarVcard(account, avatar, callback);
3703                        break;
3704                }
3705            } else if (avatar.origin == Avatar.Origin.PEP) {
3706                mOmittedPepAvatarFetches.add(KEY);
3707            } else {
3708                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": already fetching " + avatar.origin + " avatar for " + avatar.owner);
3709            }
3710        }
3711    }
3712
3713    private void fetchAvatarPep(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
3714        IqPacket packet = this.mIqGenerator.retrievePepAvatar(avatar);
3715        sendIqPacket(account, packet, (a, result) -> {
3716            synchronized (mInProgressAvatarFetches) {
3717                mInProgressAvatarFetches.remove(generateFetchKey(a, avatar));
3718            }
3719            final String ERROR = a.getJid().asBareJid() + ": fetching avatar for " + avatar.owner + " failed ";
3720            if (result.getType() == IqPacket.TYPE.RESULT) {
3721                avatar.image = mIqParser.avatarData(result);
3722                if (avatar.image != null) {
3723                    if (getFileBackend().save(avatar)) {
3724                        if (a.getJid().asBareJid().equals(avatar.owner)) {
3725                            if (a.setAvatar(avatar.getFilename())) {
3726                                databaseBackend.updateAccount(a);
3727                            }
3728                            getAvatarService().clear(a);
3729                            updateConversationUi();
3730                            updateAccountUi();
3731                        } else {
3732                            final Contact contact = a.getRoster().getContact(avatar.owner);
3733                            contact.setAvatar(avatar);
3734                            syncRoster(account);
3735                            getAvatarService().clear(contact);
3736                            updateConversationUi();
3737                            updateRosterUi();
3738                        }
3739                        if (callback != null) {
3740                            callback.success(avatar);
3741                        }
3742                        Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": successfully fetched pep avatar for " + avatar.owner);
3743                        return;
3744                    }
3745                } else {
3746
3747                    Log.d(Config.LOGTAG, ERROR + "(parsing error)");
3748                }
3749            } else {
3750                Element error = result.findChild("error");
3751                if (error == null) {
3752                    Log.d(Config.LOGTAG, ERROR + "(server error)");
3753                } else {
3754                    Log.d(Config.LOGTAG, ERROR + error.toString());
3755                }
3756            }
3757            if (callback != null) {
3758                callback.error(0, null);
3759            }
3760
3761        });
3762    }
3763
3764    private void fetchAvatarVcard(final Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
3765        IqPacket packet = this.mIqGenerator.retrieveVcardAvatar(avatar);
3766        this.sendIqPacket(account, packet, new OnIqPacketReceived() {
3767            @Override
3768            public void onIqPacketReceived(Account account, IqPacket packet) {
3769                final boolean previouslyOmittedPepFetch;
3770                synchronized (mInProgressAvatarFetches) {
3771                    final String KEY = generateFetchKey(account, avatar);
3772                    mInProgressAvatarFetches.remove(KEY);
3773                    previouslyOmittedPepFetch = mOmittedPepAvatarFetches.remove(KEY);
3774                }
3775                if (packet.getType() == IqPacket.TYPE.RESULT) {
3776                    Element vCard = packet.findChild("vCard", "vcard-temp");
3777                    Element photo = vCard != null ? vCard.findChild("PHOTO") : null;
3778                    String image = photo != null ? photo.findChildContent("BINVAL") : null;
3779                    if (image != null) {
3780                        avatar.image = image;
3781                        if (getFileBackend().save(avatar)) {
3782                            Log.d(Config.LOGTAG, account.getJid().asBareJid()
3783                                    + ": successfully fetched vCard avatar for " + avatar.owner + " omittedPep=" + previouslyOmittedPepFetch);
3784                            if (avatar.owner.isBareJid()) {
3785                                if (account.getJid().asBareJid().equals(avatar.owner) && account.getAvatar() == null) {
3786                                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": had no avatar. replacing with vcard");
3787                                    account.setAvatar(avatar.getFilename());
3788                                    databaseBackend.updateAccount(account);
3789                                    getAvatarService().clear(account);
3790                                    updateAccountUi();
3791                                } else {
3792                                    final Contact contact = account.getRoster().getContact(avatar.owner);
3793                                    contact.setAvatar(avatar, previouslyOmittedPepFetch);
3794                                    syncRoster(account);
3795                                    getAvatarService().clear(contact);
3796                                    updateRosterUi();
3797                                }
3798                                updateConversationUi();
3799                            } else {
3800                                Conversation conversation = find(account, avatar.owner.asBareJid());
3801                                if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
3802                                    MucOptions.User user = conversation.getMucOptions().findUserByFullJid(avatar.owner);
3803                                    if (user != null) {
3804                                        if (user.setAvatar(avatar)) {
3805                                            getAvatarService().clear(user);
3806                                            updateConversationUi();
3807                                            updateMucRosterUi();
3808                                        }
3809                                        if (user.getRealJid() != null) {
3810                                            Contact contact = account.getRoster().getContact(user.getRealJid());
3811                                            contact.setAvatar(avatar);
3812                                            syncRoster(account);
3813                                            getAvatarService().clear(contact);
3814                                            updateRosterUi();
3815                                        }
3816                                    }
3817                                }
3818                            }
3819                        }
3820                    }
3821                }
3822            }
3823        });
3824    }
3825
3826    public void checkForAvatar(Account account, final UiCallback<Avatar> callback) {
3827        IqPacket packet = this.mIqGenerator.retrieveAvatarMetaData(null);
3828        this.sendIqPacket(account, packet, new OnIqPacketReceived() {
3829
3830            @Override
3831            public void onIqPacketReceived(Account account, IqPacket packet) {
3832                if (packet.getType() == IqPacket.TYPE.RESULT) {
3833                    Element pubsub = packet.findChild("pubsub", "http://jabber.org/protocol/pubsub");
3834                    if (pubsub != null) {
3835                        Element items = pubsub.findChild("items");
3836                        if (items != null) {
3837                            Avatar avatar = Avatar.parseMetadata(items);
3838                            if (avatar != null) {
3839                                avatar.owner = account.getJid().asBareJid();
3840                                if (fileBackend.isAvatarCached(avatar)) {
3841                                    if (account.setAvatar(avatar.getFilename())) {
3842                                        databaseBackend.updateAccount(account);
3843                                    }
3844                                    getAvatarService().clear(account);
3845                                    callback.success(avatar);
3846                                } else {
3847                                    fetchAvatarPep(account, avatar, callback);
3848                                }
3849                                return;
3850                            }
3851                        }
3852                    }
3853                }
3854                callback.error(0, null);
3855            }
3856        });
3857    }
3858
3859    public void notifyAccountAvatarHasChanged(final Account account) {
3860        final XmppConnection connection = account.getXmppConnection();
3861        if (connection != null && connection.getFeatures().bookmarksConversion()) {
3862            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": avatar changed. resending presence to online group chats");
3863            for (Conversation conversation : conversations) {
3864                if (conversation.getAccount() == account && conversation.getMode() == Conversational.MODE_MULTI) {
3865                    final MucOptions mucOptions = conversation.getMucOptions();
3866                    if (mucOptions.online()) {
3867                        PresencePacket packet = mPresenceGenerator.selfPresence(account, Presence.Status.ONLINE, mucOptions.nonanonymous());
3868                        packet.setTo(mucOptions.getSelf().getFullJid());
3869                        connection.sendPresencePacket(packet);
3870                    }
3871                }
3872            }
3873        }
3874    }
3875
3876    public void deleteContactOnServer(Contact contact) {
3877        contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
3878        contact.resetOption(Contact.Options.DIRTY_PUSH);
3879        contact.setOption(Contact.Options.DIRTY_DELETE);
3880        Account account = contact.getAccount();
3881        if (account.getStatus() == Account.State.ONLINE) {
3882            IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
3883            Element item = iq.query(Namespace.ROSTER).addChild("item");
3884            item.setAttribute("jid", contact.getJid());
3885            item.setAttribute("subscription", "remove");
3886            account.getXmppConnection().sendIqPacket(iq, mDefaultIqHandler);
3887        }
3888    }
3889
3890    public void updateConversation(final Conversation conversation) {
3891        mDatabaseWriterExecutor.execute(() -> databaseBackend.updateConversation(conversation));
3892    }
3893
3894    private void reconnectAccount(final Account account, final boolean force, final boolean interactive) {
3895        synchronized (account) {
3896            XmppConnection connection = account.getXmppConnection();
3897            if (connection == null) {
3898                connection = createConnection(account);
3899                account.setXmppConnection(connection);
3900            }
3901            boolean hasInternet = hasInternetConnection();
3902            if (account.isEnabled() && hasInternet) {
3903                if (!force) {
3904                    disconnect(account, false);
3905                }
3906                Thread thread = new Thread(connection);
3907                connection.setInteractive(interactive);
3908                connection.prepareNewConnection();
3909                connection.interrupt();
3910                thread.start();
3911                scheduleWakeUpCall(Config.CONNECT_DISCO_TIMEOUT, account.getUuid().hashCode());
3912            } else {
3913                disconnect(account, force || account.getTrueStatus().isError() || !hasInternet);
3914                account.getRoster().clearPresences();
3915                connection.resetEverything();
3916                final AxolotlService axolotlService = account.getAxolotlService();
3917                if (axolotlService != null) {
3918                    axolotlService.resetBrokenness();
3919                }
3920                if (!hasInternet) {
3921                    account.setStatus(Account.State.NO_INTERNET);
3922                }
3923            }
3924        }
3925    }
3926
3927    public void reconnectAccountInBackground(final Account account) {
3928        new Thread(() -> reconnectAccount(account, false, true)).start();
3929    }
3930
3931    public void invite(Conversation conversation, Jid contact) {
3932        Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": inviting " + contact + " to " + conversation.getJid().asBareJid());
3933        MessagePacket packet = mMessageGenerator.invite(conversation, contact);
3934        sendMessagePacket(conversation.getAccount(), packet);
3935    }
3936
3937    public void directInvite(Conversation conversation, Jid jid) {
3938        MessagePacket packet = mMessageGenerator.directInvite(conversation, jid);
3939        sendMessagePacket(conversation.getAccount(), packet);
3940    }
3941
3942    public void resetSendingToWaiting(Account account) {
3943        for (Conversation conversation : getConversations()) {
3944            if (conversation.getAccount() == account) {
3945                conversation.findUnsentTextMessages(message -> markMessage(message, Message.STATUS_WAITING));
3946            }
3947        }
3948    }
3949
3950    public Message markMessage(final Account account, final Jid recipient, final String uuid, final int status) {
3951        return markMessage(account, recipient, uuid, status, null);
3952    }
3953
3954    public Message markMessage(final Account account, final Jid recipient, final String uuid, final int status, String errorMessage) {
3955        if (uuid == null) {
3956            return null;
3957        }
3958        for (Conversation conversation : getConversations()) {
3959            if (conversation.getJid().asBareJid().equals(recipient) && conversation.getAccount() == account) {
3960                final Message message = conversation.findSentMessageWithUuidOrRemoteId(uuid);
3961                if (message != null) {
3962                    markMessage(message, status, errorMessage);
3963                }
3964                return message;
3965            }
3966        }
3967        return null;
3968    }
3969
3970    public boolean markMessage(final Conversation conversation, final String uuid, final int status, final String serverMessageId) {
3971        return markMessage(conversation, uuid, status, serverMessageId, null);
3972    }
3973
3974    public boolean markMessage(final Conversation conversation, final String uuid, final int status, final String serverMessageId, final LocalizedContent body) {
3975        if (uuid == null) {
3976            return false;
3977        } else {
3978            final Message message = conversation.findSentMessageWithUuid(uuid);
3979            if (message != null) {
3980                if (message.getServerMsgId() == null) {
3981                    message.setServerMsgId(serverMessageId);
3982                }
3983                if (message.getEncryption() == Message.ENCRYPTION_NONE
3984                        && message.isTypeText()
3985                        && isBodyModified(message, body)) {
3986                    message.setBody(body.content);
3987                    if (body.count > 1) {
3988                        message.setBodyLanguage(body.language);
3989                    }
3990                    markMessage(message, status, null, true);
3991                } else {
3992                    markMessage(message, status);
3993                }
3994                return true;
3995            } else {
3996                return false;
3997            }
3998        }
3999    }
4000
4001    private static boolean isBodyModified(final Message message, final LocalizedContent body) {
4002        if (body == null || body.content == null) {
4003            return false;
4004        }
4005        return !body.content.equals(message.getBody());
4006    }
4007
4008    public void markMessage(Message message, int status) {
4009        markMessage(message, status, null);
4010    }
4011
4012
4013    public void markMessage(final Message message, final int status, final String errorMessage) {
4014        markMessage(message, status, errorMessage, false);
4015    }
4016
4017    public void markMessage(final Message message, final int status, final String errorMessage, final boolean includeBody) {
4018        final int oldStatus = message.getStatus();
4019        if (status == Message.STATUS_SEND_FAILED && (oldStatus == Message.STATUS_SEND_RECEIVED || oldStatus == Message.STATUS_SEND_DISPLAYED)) {
4020            return;
4021        }
4022        if (status == Message.STATUS_SEND_RECEIVED && oldStatus == Message.STATUS_SEND_DISPLAYED) {
4023            return;
4024        }
4025        message.setErrorMessage(errorMessage);
4026        message.setStatus(status);
4027        databaseBackend.updateMessage(message, includeBody);
4028        updateConversationUi();
4029        if (oldStatus != status && status == Message.STATUS_SEND_FAILED) {
4030            mNotificationService.pushFailedDelivery(message);
4031        }
4032    }
4033
4034    private SharedPreferences getPreferences() {
4035        return PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
4036    }
4037
4038    public long getAutomaticMessageDeletionDate() {
4039        final long timeout = getLongPreference(SettingsActivity.AUTOMATIC_MESSAGE_DELETION, R.integer.automatic_message_deletion);
4040        return timeout == 0 ? timeout : (System.currentTimeMillis() - (timeout * 1000));
4041    }
4042
4043    public long getLongPreference(String name, @IntegerRes int res) {
4044        long defaultValue = getResources().getInteger(res);
4045        try {
4046            return Long.parseLong(getPreferences().getString(name, String.valueOf(defaultValue)));
4047        } catch (NumberFormatException e) {
4048            return defaultValue;
4049        }
4050    }
4051
4052    public boolean getBooleanPreference(String name, @BoolRes int res) {
4053        return getPreferences().getBoolean(name, getResources().getBoolean(res));
4054    }
4055
4056    public boolean confirmMessages() {
4057        return getBooleanPreference("confirm_messages", R.bool.confirm_messages);
4058    }
4059
4060    public boolean allowMessageCorrection() {
4061        return getBooleanPreference("allow_message_correction", R.bool.allow_message_correction);
4062    }
4063
4064    public boolean sendChatStates() {
4065        return getBooleanPreference("chat_states", R.bool.chat_states);
4066    }
4067
4068    private boolean synchronizeWithBookmarks() {
4069        return getBooleanPreference("autojoin", R.bool.autojoin);
4070    }
4071
4072    public boolean useTorToConnect() {
4073        return QuickConversationsService.isConversations() && getBooleanPreference("use_tor", R.bool.use_tor);
4074    }
4075
4076    public boolean showExtendedConnectionOptions() {
4077        return QuickConversationsService.isConversations() && getBooleanPreference("show_connection_options", R.bool.show_connection_options);
4078    }
4079
4080    public boolean broadcastLastActivity() {
4081        return getBooleanPreference(SettingsActivity.BROADCAST_LAST_ACTIVITY, R.bool.last_activity);
4082    }
4083
4084    public int unreadCount() {
4085        int count = 0;
4086        for (Conversation conversation : getConversations()) {
4087            count += conversation.unreadCount();
4088        }
4089        return count;
4090    }
4091
4092
4093    private <T> List<T> threadSafeList(Set<T> set) {
4094        synchronized (LISTENER_LOCK) {
4095            return set.size() == 0 ? Collections.emptyList() : new ArrayList<>(set);
4096        }
4097    }
4098
4099    public void showErrorToastInUi(int resId) {
4100        for (OnShowErrorToast listener : threadSafeList(this.mOnShowErrorToasts)) {
4101            listener.onShowErrorToast(resId);
4102        }
4103    }
4104
4105    public void updateConversationUi() {
4106        for (OnConversationUpdate listener : threadSafeList(this.mOnConversationUpdates)) {
4107            listener.onConversationUpdate();
4108        }
4109    }
4110
4111    public void notifyJingleRtpConnectionUpdate(final Account account, final Jid with, final String sessionId, final RtpEndUserState state) {
4112        for (OnJingleRtpConnectionUpdate listener : threadSafeList(this.onJingleRtpConnectionUpdate)) {
4113            listener.onJingleRtpConnectionUpdate(account, with, sessionId, state);
4114        }
4115    }
4116
4117    public void notifyJingleRtpConnectionUpdate(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices) {
4118        for (OnJingleRtpConnectionUpdate listener : threadSafeList(this.onJingleRtpConnectionUpdate)) {
4119            listener.onAudioDeviceChanged(selectedAudioDevice, availableAudioDevices);
4120        }
4121    }
4122
4123    public void updateAccountUi() {
4124        for (OnAccountUpdate listener : threadSafeList(this.mOnAccountUpdates)) {
4125            listener.onAccountUpdate();
4126        }
4127    }
4128
4129    public void updateRosterUi() {
4130        for (OnRosterUpdate listener : threadSafeList(this.mOnRosterUpdates)) {
4131            listener.onRosterUpdate();
4132        }
4133    }
4134
4135    public boolean displayCaptchaRequest(Account account, String id, Data data, Bitmap captcha) {
4136        if (mOnCaptchaRequested.size() > 0) {
4137            DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
4138            Bitmap scaled = Bitmap.createScaledBitmap(captcha, (int) (captcha.getWidth() * metrics.scaledDensity),
4139                    (int) (captcha.getHeight() * metrics.scaledDensity), false);
4140            for (OnCaptchaRequested listener : threadSafeList(this.mOnCaptchaRequested)) {
4141                listener.onCaptchaRequested(account, id, data, scaled);
4142            }
4143            return true;
4144        }
4145        return false;
4146    }
4147
4148    public void updateBlocklistUi(final OnUpdateBlocklist.Status status) {
4149        for (OnUpdateBlocklist listener : threadSafeList(this.mOnUpdateBlocklist)) {
4150            listener.OnUpdateBlocklist(status);
4151        }
4152    }
4153
4154    public void updateMucRosterUi() {
4155        for (OnMucRosterUpdate listener : threadSafeList(this.mOnMucRosterUpdate)) {
4156            listener.onMucRosterUpdate();
4157        }
4158    }
4159
4160    public void keyStatusUpdated(AxolotlService.FetchStatus report) {
4161        for (OnKeyStatusUpdated listener : threadSafeList(this.mOnKeyStatusUpdated)) {
4162            listener.onKeyStatusUpdated(report);
4163        }
4164    }
4165
4166    public Account findAccountByJid(final Jid jid) {
4167        for (final Account account : this.accounts) {
4168            if (account.getJid().asBareJid().equals(jid.asBareJid())) {
4169                return account;
4170            }
4171        }
4172        return null;
4173    }
4174
4175    public Account findAccountByUuid(final String uuid) {
4176        for (Account account : this.accounts) {
4177            if (account.getUuid().equals(uuid)) {
4178                return account;
4179            }
4180        }
4181        return null;
4182    }
4183
4184    public Conversation findConversationByUuid(String uuid) {
4185        for (Conversation conversation : getConversations()) {
4186            if (conversation.getUuid().equals(uuid)) {
4187                return conversation;
4188            }
4189        }
4190        return null;
4191    }
4192
4193    public Conversation findUniqueConversationByJid(XmppUri xmppUri) {
4194        List<Conversation> findings = new ArrayList<>();
4195        for (Conversation c : getConversations()) {
4196            if (c.getAccount().isEnabled() && c.getJid().asBareJid().equals(xmppUri.getJid()) && ((c.getMode() == Conversational.MODE_MULTI) == xmppUri.isAction(XmppUri.ACTION_JOIN))) {
4197                findings.add(c);
4198            }
4199        }
4200        return findings.size() == 1 ? findings.get(0) : null;
4201    }
4202
4203    public boolean markRead(final Conversation conversation, boolean dismiss) {
4204        return markRead(conversation, null, dismiss).size() > 0;
4205    }
4206
4207    public void markRead(final Conversation conversation) {
4208        markRead(conversation, null, true);
4209    }
4210
4211    public List<Message> markRead(final Conversation conversation, String upToUuid, boolean dismiss) {
4212        if (dismiss) {
4213            mNotificationService.clear(conversation);
4214        }
4215        final List<Message> readMessages = conversation.markRead(upToUuid);
4216        if (readMessages.size() > 0) {
4217            Runnable runnable = () -> {
4218                for (Message message : readMessages) {
4219                    databaseBackend.updateMessage(message, false);
4220                }
4221            };
4222            mDatabaseWriterExecutor.execute(runnable);
4223            updateConversationUi();
4224            updateUnreadCountBadge();
4225            return readMessages;
4226        } else {
4227            return readMessages;
4228        }
4229    }
4230
4231    public synchronized void updateUnreadCountBadge() {
4232        int count = unreadCount();
4233        if (unreadCount != count) {
4234            Log.d(Config.LOGTAG, "update unread count to " + count);
4235            if (count > 0) {
4236                ShortcutBadger.applyCount(getApplicationContext(), count);
4237            } else {
4238                ShortcutBadger.removeCount(getApplicationContext());
4239            }
4240            unreadCount = count;
4241        }
4242    }
4243
4244    public void sendReadMarker(final Conversation conversation, String upToUuid) {
4245        final boolean isPrivateAndNonAnonymousMuc = conversation.getMode() == Conversation.MODE_MULTI && conversation.isPrivateAndNonAnonymous();
4246        final List<Message> readMessages = this.markRead(conversation, upToUuid, true);
4247        if (readMessages.size() > 0) {
4248            updateConversationUi();
4249        }
4250        final Message markable = Conversation.getLatestMarkableMessage(readMessages, isPrivateAndNonAnonymousMuc);
4251        if (confirmMessages()
4252                && markable != null
4253                && (markable.trusted() || isPrivateAndNonAnonymousMuc)
4254                && markable.getRemoteMsgId() != null) {
4255            Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": sending read marker to " + markable.getCounterpart().toString());
4256            final Account account = conversation.getAccount();
4257            final MessagePacket packet = mMessageGenerator.confirm(markable);
4258            this.sendMessagePacket(account, packet);
4259        }
4260    }
4261
4262    public SecureRandom getRNG() {
4263        return this.mRandom;
4264    }
4265
4266    public MemorizingTrustManager getMemorizingTrustManager() {
4267        return this.mMemorizingTrustManager;
4268    }
4269
4270    public void setMemorizingTrustManager(MemorizingTrustManager trustManager) {
4271        this.mMemorizingTrustManager = trustManager;
4272    }
4273
4274    public void updateMemorizingTrustmanager() {
4275        final MemorizingTrustManager tm;
4276        final boolean dontTrustSystemCAs = getBooleanPreference("dont_trust_system_cas", R.bool.dont_trust_system_cas);
4277        if (dontTrustSystemCAs) {
4278            tm = new MemorizingTrustManager(getApplicationContext(), null);
4279        } else {
4280            tm = new MemorizingTrustManager(getApplicationContext());
4281        }
4282        setMemorizingTrustManager(tm);
4283    }
4284
4285    public LruCache<String, Bitmap> getBitmapCache() {
4286        return this.mBitmapCache;
4287    }
4288
4289    public Collection<String> getKnownHosts() {
4290        final Set<String> hosts = new HashSet<>();
4291        for (final Account account : getAccounts()) {
4292            hosts.add(account.getServer());
4293            for (final Contact contact : account.getRoster().getContacts()) {
4294                if (contact.showInRoster()) {
4295                    final String server = contact.getServer();
4296                    if (server != null) {
4297                        hosts.add(server);
4298                    }
4299                }
4300            }
4301        }
4302        if (Config.QUICKSY_DOMAIN != null) {
4303            hosts.remove(Config.QUICKSY_DOMAIN.toEscapedString()); //we only want to show this when we type a e164 number
4304        }
4305        if (Config.DOMAIN_LOCK != null) {
4306            hosts.add(Config.DOMAIN_LOCK);
4307        }
4308        if (Config.MAGIC_CREATE_DOMAIN != null) {
4309            hosts.add(Config.MAGIC_CREATE_DOMAIN);
4310        }
4311        return hosts;
4312    }
4313
4314    public Collection<String> getKnownConferenceHosts() {
4315        final Set<String> mucServers = new HashSet<>();
4316        for (final Account account : accounts) {
4317            if (account.getXmppConnection() != null) {
4318                mucServers.addAll(account.getXmppConnection().getMucServers());
4319                for (Bookmark bookmark : account.getBookmarks()) {
4320                    final Jid jid = bookmark.getJid();
4321                    final String s = jid == null ? null : jid.getDomain().toEscapedString();
4322                    if (s != null) {
4323                        mucServers.add(s);
4324                    }
4325                }
4326            }
4327        }
4328        return mucServers;
4329    }
4330
4331    public void sendMessagePacket(Account account, MessagePacket packet) {
4332        final XmppConnection connection = account.getXmppConnection();
4333        if (connection != null) {
4334            connection.sendMessagePacket(packet);
4335        }
4336    }
4337
4338    public void sendPresencePacket(Account account, PresencePacket packet) {
4339        XmppConnection connection = account.getXmppConnection();
4340        if (connection != null) {
4341            connection.sendPresencePacket(packet);
4342        }
4343    }
4344
4345    public void sendCreateAccountWithCaptchaPacket(Account account, String id, Data data) {
4346        final XmppConnection connection = account.getXmppConnection();
4347        if (connection != null) {
4348            IqPacket request = mIqGenerator.generateCreateAccountWithCaptcha(account, id, data);
4349            connection.sendUnmodifiedIqPacket(request, connection.registrationResponseListener, true);
4350        }
4351    }
4352
4353    public void sendIqPacket(final Account account, final IqPacket packet, final OnIqPacketReceived callback) {
4354        final XmppConnection connection = account.getXmppConnection();
4355        if (connection != null) {
4356            connection.sendIqPacket(packet, callback);
4357        } else if (callback != null) {
4358            callback.onIqPacketReceived(account, new IqPacket(IqPacket.TYPE.TIMEOUT));
4359        }
4360    }
4361
4362    public void sendPresence(final Account account) {
4363        sendPresence(account, checkListeners() && broadcastLastActivity());
4364    }
4365
4366    private void sendPresence(final Account account, final boolean includeIdleTimestamp) {
4367        final Presence.Status status;
4368        if (manuallyChangePresence()) {
4369            status = account.getPresenceStatus();
4370        } else {
4371            status = getTargetPresence();
4372        }
4373        final PresencePacket packet = mPresenceGenerator.selfPresence(account, status);
4374        if (mLastActivity > 0 && includeIdleTimestamp) {
4375            long since = Math.min(mLastActivity, System.currentTimeMillis()); //don't send future dates
4376            packet.addChild("idle", Namespace.IDLE).setAttribute("since", AbstractGenerator.getTimestamp(since));
4377        }
4378        sendPresencePacket(account, packet);
4379    }
4380
4381    private void deactivateGracePeriod() {
4382        for (Account account : getAccounts()) {
4383            account.deactivateGracePeriod();
4384        }
4385    }
4386
4387    public void refreshAllPresences() {
4388        boolean includeIdleTimestamp = checkListeners() && broadcastLastActivity();
4389        for (Account account : getAccounts()) {
4390            if (account.isEnabled()) {
4391                sendPresence(account, includeIdleTimestamp);
4392            }
4393        }
4394    }
4395
4396    private void refreshAllFcmTokens() {
4397        for (Account account : getAccounts()) {
4398            if (account.isOnlineAndConnected() && mPushManagementService.available(account)) {
4399                mPushManagementService.registerPushTokenOnServer(account);
4400                //TODO renew mucs
4401            }
4402        }
4403    }
4404
4405    private void sendOfflinePresence(final Account account) {
4406        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": sending offline presence");
4407        sendPresencePacket(account, mPresenceGenerator.sendOfflinePresence(account));
4408    }
4409
4410    public MessageGenerator getMessageGenerator() {
4411        return this.mMessageGenerator;
4412    }
4413
4414    public PresenceGenerator getPresenceGenerator() {
4415        return this.mPresenceGenerator;
4416    }
4417
4418    public IqGenerator getIqGenerator() {
4419        return this.mIqGenerator;
4420    }
4421
4422    public IqParser getIqParser() {
4423        return this.mIqParser;
4424    }
4425
4426    public JingleConnectionManager getJingleConnectionManager() {
4427        return this.mJingleConnectionManager;
4428    }
4429
4430    public MessageArchiveService getMessageArchiveService() {
4431        return this.mMessageArchiveService;
4432    }
4433
4434    public QuickConversationsService getQuickConversationsService() {
4435        return this.mQuickConversationsService;
4436    }
4437
4438    public List<Contact> findContacts(Jid jid, String accountJid) {
4439        ArrayList<Contact> contacts = new ArrayList<>();
4440        for (Account account : getAccounts()) {
4441            if ((account.isEnabled() || accountJid != null)
4442                    && (accountJid == null || accountJid.equals(account.getJid().asBareJid().toString()))) {
4443                Contact contact = account.getRoster().getContactFromContactList(jid);
4444                if (contact != null) {
4445                    contacts.add(contact);
4446                }
4447            }
4448        }
4449        return contacts;
4450    }
4451
4452    public Conversation findFirstMuc(Jid jid) {
4453        for (Conversation conversation : getConversations()) {
4454            if (conversation.getAccount().isEnabled() && conversation.getJid().asBareJid().equals(jid.asBareJid()) && conversation.getMode() == Conversation.MODE_MULTI) {
4455                return conversation;
4456            }
4457        }
4458        return null;
4459    }
4460
4461    public NotificationService getNotificationService() {
4462        return this.mNotificationService;
4463    }
4464
4465    public HttpConnectionManager getHttpConnectionManager() {
4466        return this.mHttpConnectionManager;
4467    }
4468
4469    public void resendFailedMessages(final Message message) {
4470        final Collection<Message> messages = new ArrayList<>();
4471        Message current = message;
4472        while (current.getStatus() == Message.STATUS_SEND_FAILED) {
4473            messages.add(current);
4474            if (current.mergeable(current.next())) {
4475                current = current.next();
4476            } else {
4477                break;
4478            }
4479        }
4480        for (final Message msg : messages) {
4481            msg.setTime(System.currentTimeMillis());
4482            markMessage(msg, Message.STATUS_WAITING);
4483            this.resendMessage(msg, false);
4484        }
4485        if (message.getConversation() instanceof Conversation) {
4486            ((Conversation) message.getConversation()).sort();
4487        }
4488        updateConversationUi();
4489    }
4490
4491    public void clearConversationHistory(final Conversation conversation) {
4492        final long clearDate;
4493        final String reference;
4494        if (conversation.countMessages() > 0) {
4495            Message latestMessage = conversation.getLatestMessage();
4496            clearDate = latestMessage.getTimeSent() + 1000;
4497            reference = latestMessage.getServerMsgId();
4498        } else {
4499            clearDate = System.currentTimeMillis();
4500            reference = null;
4501        }
4502        conversation.clearMessages();
4503        conversation.setHasMessagesLeftOnServer(false); //avoid messages getting loaded through mam
4504        conversation.setLastClearHistory(clearDate, reference);
4505        Runnable runnable = () -> {
4506            databaseBackend.deleteMessagesInConversation(conversation);
4507            databaseBackend.updateConversation(conversation);
4508        };
4509        mDatabaseWriterExecutor.execute(runnable);
4510    }
4511
4512    public boolean sendBlockRequest(final Blockable blockable, boolean reportSpam) {
4513        if (blockable != null && blockable.getBlockedJid() != null) {
4514            final Jid jid = blockable.getBlockedJid();
4515            this.sendIqPacket(blockable.getAccount(), getIqGenerator().generateSetBlockRequest(jid, reportSpam), (a, response) -> {
4516                if (response.getType() == IqPacket.TYPE.RESULT) {
4517                    a.getBlocklist().add(jid);
4518                    updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
4519                }
4520            });
4521            if (blockable.getBlockedJid().isFullJid()) {
4522                return false;
4523            } else if (removeBlockedConversations(blockable.getAccount(), jid)) {
4524                updateConversationUi();
4525                return true;
4526            } else {
4527                return false;
4528            }
4529        } else {
4530            return false;
4531        }
4532    }
4533
4534    public boolean removeBlockedConversations(final Account account, final Jid blockedJid) {
4535        boolean removed = false;
4536        synchronized (this.conversations) {
4537            boolean domainJid = blockedJid.getLocal() == null;
4538            for (Conversation conversation : this.conversations) {
4539                boolean jidMatches = (domainJid && blockedJid.getDomain().equals(conversation.getJid().getDomain()))
4540                        || blockedJid.equals(conversation.getJid().asBareJid());
4541                if (conversation.getAccount() == account
4542                        && conversation.getMode() == Conversation.MODE_SINGLE
4543                        && jidMatches) {
4544                    this.conversations.remove(conversation);
4545                    markRead(conversation);
4546                    conversation.setStatus(Conversation.STATUS_ARCHIVED);
4547                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": archiving conversation " + conversation.getJid().asBareJid() + " because jid was blocked");
4548                    updateConversation(conversation);
4549                    removed = true;
4550                }
4551            }
4552        }
4553        return removed;
4554    }
4555
4556    public void sendUnblockRequest(final Blockable blockable) {
4557        if (blockable != null && blockable.getJid() != null) {
4558            final Jid jid = blockable.getBlockedJid();
4559            this.sendIqPacket(blockable.getAccount(), getIqGenerator().generateSetUnblockRequest(jid), new OnIqPacketReceived() {
4560                @Override
4561                public void onIqPacketReceived(final Account account, final IqPacket packet) {
4562                    if (packet.getType() == IqPacket.TYPE.RESULT) {
4563                        account.getBlocklist().remove(jid);
4564                        updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
4565                    }
4566                }
4567            });
4568        }
4569    }
4570
4571    public void publishDisplayName(Account account) {
4572        String displayName = account.getDisplayName();
4573        final IqPacket request;
4574        if (TextUtils.isEmpty(displayName)) {
4575            request = mIqGenerator.deleteNode(Namespace.NICK);
4576        } else {
4577            request = mIqGenerator.publishNick(displayName);
4578        }
4579        mAvatarService.clear(account);
4580        sendIqPacket(account, request, (account1, packet) -> {
4581            if (packet.getType() == IqPacket.TYPE.ERROR) {
4582                Log.d(Config.LOGTAG, account1.getJid().asBareJid() + ": unable to modify nick name " + packet.toString());
4583            }
4584        });
4585    }
4586
4587    public ServiceDiscoveryResult getCachedServiceDiscoveryResult(Pair<String, String> key) {
4588        ServiceDiscoveryResult result = discoCache.get(key);
4589        if (result != null) {
4590            return result;
4591        } else {
4592            result = databaseBackend.findDiscoveryResult(key.first, key.second);
4593            if (result != null) {
4594                discoCache.put(key, result);
4595            }
4596            return result;
4597        }
4598    }
4599
4600    public void fetchCaps(Account account, final Jid jid, final Presence presence) {
4601        final Pair<String, String> key = new Pair<>(presence.getHash(), presence.getVer());
4602        final ServiceDiscoveryResult disco = getCachedServiceDiscoveryResult(key);
4603        if (disco != null) {
4604            presence.setServiceDiscoveryResult(disco);
4605            final Contact contact = account.getRoster().getContact(jid);
4606            if (contact.refreshRtpCapability()) {
4607                syncRoster(account);
4608            }
4609        } else {
4610            final IqPacket request = new IqPacket(IqPacket.TYPE.GET);
4611            request.setTo(jid);
4612            final String node = presence.getNode();
4613            final String ver = presence.getVer();
4614            final Element query = request.query(Namespace.DISCO_INFO);
4615            if (node != null && ver != null) {
4616                query.setAttribute("node", node + "#" + ver);
4617            }
4618            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": making disco request for " + key.second + " to " + jid);
4619            sendIqPacket(account, request, (a, response) -> {
4620                if (response.getType() == IqPacket.TYPE.RESULT) {
4621                    final ServiceDiscoveryResult discoveryResult = new ServiceDiscoveryResult(response);
4622                    if (presence.getVer().equals(discoveryResult.getVer())) {
4623                        databaseBackend.insertDiscoveryResult(discoveryResult);
4624                        injectServiceDiscoveryResult(a.getRoster(), presence.getHash(), presence.getVer(), discoveryResult);
4625                    } else {
4626                        Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": mismatch in caps for contact " + jid + " " + presence.getVer() + " vs " + discoveryResult.getVer());
4627                    }
4628                } else {
4629                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to fetch caps from " + jid);
4630                }
4631            });
4632        }
4633    }
4634
4635    private void injectServiceDiscoveryResult(Roster roster, String hash, String ver, ServiceDiscoveryResult disco) {
4636        boolean rosterNeedsSync = false;
4637        for (final Contact contact : roster.getContacts()) {
4638            boolean serviceDiscoverySet = false;
4639            for (final Presence presence : contact.getPresences().getPresences()) {
4640                if (hash.equals(presence.getHash()) && ver.equals(presence.getVer())) {
4641                    presence.setServiceDiscoveryResult(disco);
4642                    serviceDiscoverySet = true;
4643                }
4644            }
4645            if (serviceDiscoverySet) {
4646                rosterNeedsSync |= contact.refreshRtpCapability();
4647            }
4648        }
4649        if (rosterNeedsSync) {
4650            syncRoster(roster.getAccount());
4651        }
4652    }
4653
4654    public void fetchMamPreferences(Account account, final OnMamPreferencesFetched callback) {
4655        final MessageArchiveService.Version version = MessageArchiveService.Version.get(account);
4656        IqPacket request = new IqPacket(IqPacket.TYPE.GET);
4657        request.addChild("prefs", version.namespace);
4658        sendIqPacket(account, request, (account1, packet) -> {
4659            Element prefs = packet.findChild("prefs", version.namespace);
4660            if (packet.getType() == IqPacket.TYPE.RESULT && prefs != null) {
4661                callback.onPreferencesFetched(prefs);
4662            } else {
4663                callback.onPreferencesFetchFailed();
4664            }
4665        });
4666    }
4667
4668    public PushManagementService getPushManagementService() {
4669        return mPushManagementService;
4670    }
4671
4672    public void changeStatus(Account account, PresenceTemplate template, String signature) {
4673        if (!template.getStatusMessage().isEmpty()) {
4674            databaseBackend.insertPresenceTemplate(template);
4675        }
4676        account.setPgpSignature(signature);
4677        account.setPresenceStatus(template.getStatus());
4678        account.setPresenceStatusMessage(template.getStatusMessage());
4679        databaseBackend.updateAccount(account);
4680        sendPresence(account);
4681    }
4682
4683    public List<PresenceTemplate> getPresenceTemplates(Account account) {
4684        List<PresenceTemplate> templates = databaseBackend.getPresenceTemplates();
4685        for (PresenceTemplate template : account.getSelfContact().getPresences().asTemplates()) {
4686            if (!templates.contains(template)) {
4687                templates.add(0, template);
4688            }
4689        }
4690        return templates;
4691    }
4692
4693    public void saveConversationAsBookmark(Conversation conversation, String name) {
4694        final Account account = conversation.getAccount();
4695        final Bookmark bookmark = new Bookmark(account, conversation.getJid().asBareJid());
4696        final String nick = conversation.getJid().getResource();
4697        if (nick != null && !nick.isEmpty() && !nick.equals(MucOptions.defaultNick(account))) {
4698            bookmark.setNick(nick);
4699        }
4700        if (!TextUtils.isEmpty(name)) {
4701            bookmark.setBookmarkName(name);
4702        }
4703        bookmark.setAutojoin(getPreferences().getBoolean("autojoin", getResources().getBoolean(R.bool.autojoin)));
4704        createBookmark(account, bookmark);
4705        bookmark.setConversation(conversation);
4706    }
4707
4708    public boolean verifyFingerprints(Contact contact, List<XmppUri.Fingerprint> fingerprints) {
4709        boolean performedVerification = false;
4710        final AxolotlService axolotlService = contact.getAccount().getAxolotlService();
4711        for (XmppUri.Fingerprint fp : fingerprints) {
4712            if (fp.type == XmppUri.FingerprintType.OMEMO) {
4713                String fingerprint = "05" + fp.fingerprint.replaceAll("\\s", "");
4714                FingerprintStatus fingerprintStatus = axolotlService.getFingerprintTrust(fingerprint);
4715                if (fingerprintStatus != null) {
4716                    if (!fingerprintStatus.isVerified()) {
4717                        performedVerification = true;
4718                        axolotlService.setFingerprintTrust(fingerprint, fingerprintStatus.toVerified());
4719                    }
4720                } else {
4721                    axolotlService.preVerifyFingerprint(contact, fingerprint);
4722                }
4723            }
4724        }
4725        return performedVerification;
4726    }
4727
4728    public boolean verifyFingerprints(Account account, List<XmppUri.Fingerprint> fingerprints) {
4729        final AxolotlService axolotlService = account.getAxolotlService();
4730        boolean verifiedSomething = false;
4731        for (XmppUri.Fingerprint fp : fingerprints) {
4732            if (fp.type == XmppUri.FingerprintType.OMEMO) {
4733                String fingerprint = "05" + fp.fingerprint.replaceAll("\\s", "");
4734                Log.d(Config.LOGTAG, "trying to verify own fp=" + fingerprint);
4735                FingerprintStatus fingerprintStatus = axolotlService.getFingerprintTrust(fingerprint);
4736                if (fingerprintStatus != null) {
4737                    if (!fingerprintStatus.isVerified()) {
4738                        axolotlService.setFingerprintTrust(fingerprint, fingerprintStatus.toVerified());
4739                        verifiedSomething = true;
4740                    }
4741                } else {
4742                    axolotlService.preVerifyFingerprint(account, fingerprint);
4743                    verifiedSomething = true;
4744                }
4745            }
4746        }
4747        return verifiedSomething;
4748    }
4749
4750    public boolean blindTrustBeforeVerification() {
4751        return getBooleanPreference(SettingsActivity.BLIND_TRUST_BEFORE_VERIFICATION, R.bool.btbv);
4752    }
4753
4754    public ShortcutService getShortcutService() {
4755        return mShortcutService;
4756    }
4757
4758    public void pushMamPreferences(Account account, Element prefs) {
4759        IqPacket set = new IqPacket(IqPacket.TYPE.SET);
4760        set.addChild(prefs);
4761        sendIqPacket(account, set, null);
4762    }
4763
4764    public void evictPreview(String uuid) {
4765        if (mBitmapCache.remove(uuid) != null) {
4766            Log.d(Config.LOGTAG, "deleted cached preview");
4767        }
4768    }
4769
4770    public interface OnMamPreferencesFetched {
4771        void onPreferencesFetched(Element prefs);
4772
4773        void onPreferencesFetchFailed();
4774    }
4775
4776    public interface OnAccountCreated {
4777        void onAccountCreated(Account account);
4778
4779        void informUser(int r);
4780    }
4781
4782    public interface OnMoreMessagesLoaded {
4783        void onMoreMessagesLoaded(int count, Conversation conversation);
4784
4785        void informUser(int r);
4786    }
4787
4788    public interface OnAccountPasswordChanged {
4789        void onPasswordChangeSucceeded();
4790
4791        void onPasswordChangeFailed();
4792    }
4793
4794    public interface OnRoomDestroy {
4795        void onRoomDestroySucceeded();
4796
4797        void onRoomDestroyFailed();
4798    }
4799
4800    public interface OnAffiliationChanged {
4801        void onAffiliationChangedSuccessful(Jid jid);
4802
4803        void onAffiliationChangeFailed(Jid jid, int resId);
4804    }
4805
4806    public interface OnConversationUpdate {
4807        void onConversationUpdate();
4808    }
4809
4810    public interface OnJingleRtpConnectionUpdate {
4811        void onJingleRtpConnectionUpdate(final Account account, final Jid with, final String sessionId, final RtpEndUserState state);
4812
4813        void onAudioDeviceChanged(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices);
4814    }
4815
4816    public interface OnAccountUpdate {
4817        void onAccountUpdate();
4818    }
4819
4820    public interface OnCaptchaRequested {
4821        void onCaptchaRequested(Account account, String id, Data data, Bitmap captcha);
4822    }
4823
4824    public interface OnRosterUpdate {
4825        void onRosterUpdate();
4826    }
4827
4828    public interface OnMucRosterUpdate {
4829        void onMucRosterUpdate();
4830    }
4831
4832    public interface OnConferenceConfigurationFetched {
4833        void onConferenceConfigurationFetched(Conversation conversation);
4834
4835        void onFetchFailed(Conversation conversation, String errorCondition);
4836    }
4837
4838    public interface OnConferenceJoined {
4839        void onConferenceJoined(Conversation conversation);
4840    }
4841
4842    public interface OnConfigurationPushed {
4843        void onPushSucceeded();
4844
4845        void onPushFailed();
4846    }
4847
4848    public interface OnShowErrorToast {
4849        void onShowErrorToast(int resId);
4850    }
4851
4852    public class XmppConnectionBinder extends Binder {
4853        public XmppConnectionService getService() {
4854            return XmppConnectionService.this;
4855        }
4856    }
4857
4858    private class InternalEventReceiver extends BroadcastReceiver {
4859
4860        @Override
4861        public void onReceive(Context context, Intent intent) {
4862            onStartCommand(intent, 0, 0);
4863        }
4864    }
4865
4866    public static class OngoingCall {
4867        private final AbstractJingleConnection.Id id;
4868        private final Set<Media> media;
4869
4870        public OngoingCall(AbstractJingleConnection.Id id, Set<Media> media) {
4871            this.id = id;
4872            this.media = media;
4873        }
4874
4875        @Override
4876        public boolean equals(Object o) {
4877            if (this == o) return true;
4878            if (o == null || getClass() != o.getClass()) return false;
4879            OngoingCall that = (OngoingCall) o;
4880            return Objects.equal(id, that.id);
4881        }
4882
4883        @Override
4884        public int hashCode() {
4885            return Objects.hashCode(id);
4886        }
4887    }
4888}