anotherim/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java

2590 lines
85 KiB
Java
Raw Normal View History

2014-02-28 17:46:01 +00:00
package eu.siacs.conversations.services;
2014-01-24 01:04:05 +00:00
import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.FileObserver;
import android.os.IBinder;
import android.os.Looper;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.util.Log;
import android.util.LruCache;
import net.java.otr4j.OtrException;
import net.java.otr4j.session.Session;
import net.java.otr4j.session.SessionID;
import net.java.otr4j.session.SessionImpl;
import net.java.otr4j.session.SessionStatus;
import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection;
import java.math.BigInteger;
import java.security.SecureRandom;
2014-07-10 17:42:37 +00:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
2014-02-01 14:07:20 +00:00
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
2014-07-12 10:41:37 +00:00
import java.util.concurrent.CopyOnWriteArrayList;
2014-02-13 22:40:08 +00:00
2014-07-22 15:27:44 +00:00
import de.duenndns.ssl.MemorizingTrustManager;
2014-08-31 14:28:21 +00:00
import eu.siacs.conversations.Config;
2014-08-04 23:36:17 +00:00
import eu.siacs.conversations.R;
2014-02-28 17:46:01 +00:00
import eu.siacs.conversations.crypto.PgpEngine;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Blockable;
2014-07-14 09:47:42 +00:00
import eu.siacs.conversations.entities.Bookmark;
2014-02-28 17:46:01 +00:00
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
2015-07-10 13:11:03 +00:00
import eu.siacs.conversations.entities.Transferable;
import eu.siacs.conversations.entities.TransferablePlaceholder;
2014-02-28 17:46:01 +00:00
import eu.siacs.conversations.entities.Message;
2014-03-03 04:01:02 +00:00
import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
import eu.siacs.conversations.generator.IqGenerator;
import eu.siacs.conversations.generator.MessageGenerator;
import eu.siacs.conversations.generator.PresenceGenerator;
2014-10-13 23:06:45 +00:00
import eu.siacs.conversations.http.HttpConnectionManager;
import eu.siacs.conversations.parser.IqParser;
2014-05-14 10:56:34 +00:00
import eu.siacs.conversations.parser.MessageParser;
import eu.siacs.conversations.parser.PresenceParser;
2014-02-28 17:46:01 +00:00
import eu.siacs.conversations.persistance.DatabaseBackend;
2014-04-05 19:06:10 +00:00
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.ui.UiCallback;
import eu.siacs.conversations.utils.CryptoHelper;
2014-03-09 12:21:28 +00:00
import eu.siacs.conversations.utils.ExceptionHelper;
2014-02-28 17:46:01 +00:00
import eu.siacs.conversations.utils.OnPhoneContactsLoadedListener;
import eu.siacs.conversations.utils.PRNGFixes;
2014-02-28 17:46:01 +00:00
import eu.siacs.conversations.utils.PhoneHelper;
import eu.siacs.conversations.utils.SerialSingleThreadExecutor;
import eu.siacs.conversations.utils.Xmlns;
2014-02-28 17:46:01 +00:00
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnBindListener;
import eu.siacs.conversations.xmpp.OnContactStatusChanged;
2014-02-28 17:46:01 +00:00
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
2014-08-26 14:52:42 +00:00
import eu.siacs.conversations.xmpp.OnMessageAcknowledged;
import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
import eu.siacs.conversations.xmpp.OnPresencePacketReceived;
2014-02-28 17:46:01 +00:00
import eu.siacs.conversations.xmpp.OnStatusChanged;
import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
2014-02-28 17:46:01 +00:00
import eu.siacs.conversations.xmpp.XmppConnection;
import eu.siacs.conversations.xmpp.chatstate.ChatState;
2014-11-20 17:33:04 +00:00
import eu.siacs.conversations.xmpp.forms.Data;
import eu.siacs.conversations.xmpp.forms.Field;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.jingle.JingleConnectionManager;
import eu.siacs.conversations.xmpp.jingle.OnJinglePacketReceived;
import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
import eu.siacs.conversations.xmpp.pep.Avatar;
2014-03-10 18:22:13 +00:00
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
import me.leolin.shortcutbadger.ShortcutBadger;
2014-01-24 01:04:05 +00:00
2014-12-13 12:52:57 +00:00
public class XmppConnectionService extends Service implements OnPhoneContactsLoadedListener {
2014-02-03 17:38:47 +00:00
public static final String ACTION_CLEAR_NOTIFICATION = "clear_notification";
public static final String ACTION_DISABLE_FOREGROUND = "disable_foreground";
private static final String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts";
2015-02-10 16:13:34 +00:00
public static final String ACTION_TRY_AGAIN = "try_again";
public static final String ACTION_DISABLE_ACCOUNT = "disable_account";
private ContentObserver contactObserver = new ContentObserver(null) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Intent intent = new Intent(getApplicationContext(),
XmppConnectionService.class);
intent.setAction(ACTION_MERGE_PHONE_CONTACTS);
startService(intent);
}
};
private final SerialSingleThreadExecutor mFileAddingExecutor = new SerialSingleThreadExecutor();
private final SerialSingleThreadExecutor mDatabaseExecutor = new SerialSingleThreadExecutor();
private final IBinder mBinder = new XmppConnectionBinder();
private final List<Conversation> conversations = new CopyOnWriteArrayList<>();
private final FileObserver fileObserver = new FileObserver(
FileBackend.getConversationsImageDirectory()) {
@Override
public void onEvent(int event, String path) {
if (event == FileObserver.DELETE) {
markFileDeleted(path.split("\\.")[0]);
}
}
};
private final OnJinglePacketReceived jingleListener = new OnJinglePacketReceived() {
@Override
public void onJinglePacketReceived(Account account, JinglePacket packet) {
mJingleConnectionManager.deliverPacket(account, packet);
}
};
private final OnBindListener mOnBindListener = new OnBindListener() {
@Override
public void onBind(final Account account) {
account.getRoster().clearPresences();
account.pendingConferenceJoins.clear();
account.pendingConferenceLeaves.clear();
fetchRosterFromServer(account);
fetchBookmarks(account);
sendPresence(account);
connectMultiModeConversations(account);
updateConversationUi();
}
};
private final OnMessageAcknowledged mOnMessageAcknowledgedListener = new OnMessageAcknowledged() {
@Override
public void onMessageAcknowledged(Account account, String uuid) {
for (final Conversation conversation : getConversations()) {
if (conversation.getAccount() == account) {
Message message = conversation.findUnsentMessageWithUuid(uuid);
if (message != null) {
markMessage(message, Message.STATUS_SEND);
if (conversation.setLastMessageTransmitted(System.currentTimeMillis())) {
databaseBackend.updateConversation(conversation);
}
}
}
}
}
};
private final IqGenerator mIqGenerator = new IqGenerator(this);
public DatabaseBackend databaseBackend;
public OnContactStatusChanged onContactStatusChanged = new OnContactStatusChanged() {
2014-08-26 14:52:42 +00:00
@Override
public void onContactStatusChanged(Contact contact, boolean online) {
Conversation conversation = find(getConversations(), contact);
if (conversation != null) {
if (online) {
conversation.endOtrIfNeeded();
if (contact.getPresences().size() == 1) {
sendUnsentMessages(conversation);
}
} else {
if (contact.getPresences().size() >= 1) {
if (conversation.hasValidOtrSession()) {
String otrResource = conversation.getOtrSession().getSessionID().getUserID();
if (!(Arrays.asList(contact.getPresences().asStringArray()).contains(otrResource))) {
conversation.endOtrIfNeeded();
}
}
} else {
conversation.endOtrIfNeeded();
}
}
}
}
};
private FileBackend fileBackend = new FileBackend(this);
2014-07-22 15:27:44 +00:00
private MemorizingTrustManager mMemorizingTrustManager;
2014-10-21 12:57:16 +00:00
private NotificationService mNotificationService = new NotificationService(
this);
private OnMessagePacketReceived mMessageParser = new MessageParser(this);
private OnPresencePacketReceived mPresenceParser = new PresenceParser(this);
private IqParser mIqParser = new IqParser(this);
private OnIqPacketReceived mDefaultIqHandler = new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() == IqPacket.TYPE.ERROR) {
Element error = packet.findChild("error");
String text = error != null ? error.findChildContent("text") : null;
if (text != null) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": received iq error - "+text);
}
}
}
};
private MessageGenerator mMessageGenerator = new MessageGenerator(this);
private PresenceGenerator mPresenceGenerator = new PresenceGenerator(this);
private List<Account> accounts;
2014-05-01 20:33:49 +00:00
private JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager(
this);
2014-10-15 12:41:27 +00:00
private HttpConnectionManager mHttpConnectionManager = new HttpConnectionManager(
this);
private AvatarService mAvatarService = new AvatarService(this);
private final List<String> mInProgressAvatarFetches = new ArrayList<>();
2014-12-05 00:54:16 +00:00
private MessageArchiveService mMessageArchiveService = new MessageArchiveService(this);
2014-07-12 11:42:17 +00:00
private OnConversationUpdate mOnConversationUpdate = null;
private int convChangedListenerCount = 0;
private OnShowErrorToast mOnShowErrorToast = null;
private int showErrorToastListenerCount = 0;
private int unreadCount = -1;
2014-07-12 11:42:17 +00:00
private OnAccountUpdate mOnAccountUpdate = null;
private OnStatusChanged statusListener = new OnStatusChanged() {
@Override
public void onStatusChanged(Account account) {
XmppConnection connection = account.getXmppConnection();
2014-07-12 11:42:17 +00:00
if (mOnAccountUpdate != null) {
2014-08-26 14:52:42 +00:00
mOnAccountUpdate.onAccountUpdate();
}
2014-11-15 16:09:02 +00:00
if (account.getStatus() == Account.State.ONLINE) {
2014-08-26 14:52:42 +00:00
for (Conversation conversation : account.pendingConferenceLeaves) {
2014-07-18 19:57:10 +00:00
leaveMuc(conversation);
}
2014-08-26 14:52:42 +00:00
for (Conversation conversation : account.pendingConferenceJoins) {
2014-07-18 19:57:10 +00:00
joinMuc(conversation);
}
2014-12-14 22:23:32 +00:00
mMessageArchiveService.executePendingQueries(account);
mJingleConnectionManager.cancelInTransmission();
2014-03-21 18:58:47 +00:00
List<Conversation> conversations = getConversations();
for (Conversation conversation : conversations) {
if (conversation.getAccount() == account) {
conversation.startOtrIfNeeded();
sendUnsentMessages(conversation);
}
}
if (connection != null && connection.getFeatures().csi()) {
if (checkListeners()) {
Log.d(Config.LOGTAG, account.getJid().toBareJid()
+ " sending csi//inactive");
connection.sendInactive();
} else {
Log.d(Config.LOGTAG, account.getJid().toBareJid()
2014-08-31 14:28:21 +00:00
+ " sending csi//active");
connection.sendActive();
}
}
2014-05-22 12:33:17 +00:00
syncDirtyContacts(account);
account.getAxolotlService().publishOwnDeviceIdIfNeeded();
account.getAxolotlService().publishBundlesIfNeeded();
scheduleWakeUpCall(Config.PING_MAX_INTERVAL, account.getUuid().hashCode());
2014-11-15 16:09:02 +00:00
} else if (account.getStatus() == Account.State.OFFLINE) {
2014-08-27 17:25:58 +00:00
resetSendingToWaiting(account);
if (!account.isOptionSet(Account.OPTION_DISABLED)) {
2014-03-11 14:44:22 +00:00
int timeToReconnect = mRandom.nextInt(50) + 10;
scheduleWakeUpCall(timeToReconnect,account.getUuid().hashCode());
}
2014-11-15 16:09:02 +00:00
} else if (account.getStatus() == Account.State.REGISTRATION_SUCCESSFUL) {
2014-03-13 16:29:22 +00:00
databaseBackend.updateAccount(account);
reconnectAccount(account, true);
2014-11-15 16:09:02 +00:00
} else if ((account.getStatus() != Account.State.CONNECTING)
&& (account.getStatus() != Account.State.NO_INTERNET)) {
2014-08-26 14:52:42 +00:00
if (connection != null) {
2014-08-22 11:22:34 +00:00
int next = connection.getTimeToNextAttempt();
Log.d(Config.LOGTAG, account.getJid().toBareJid()
2014-08-26 14:52:42 +00:00
+ ": error connecting account. try again in "
+ next + "s for the "
+ (connection.getAttempt() + 1) + " time");
scheduleWakeUpCall(next,account.getUuid().hashCode());
2014-08-22 11:22:34 +00:00
}
2014-11-15 16:09:02 +00:00
}
getNotificationService().updateErrorNotification();
}
};
2014-12-03 13:55:09 +00:00
private int accountChangedListenerCount = 0;
private OnRosterUpdate mOnRosterUpdate = null;
private OnUpdateBlocklist mOnUpdateBlocklist = null;
private int updateBlocklistListenerCount = 0;
2014-12-03 13:55:09 +00:00
private int rosterChangedListenerCount = 0;
private OnMucRosterUpdate mOnMucRosterUpdate = null;
2014-12-03 13:55:09 +00:00
private int mucRosterChangedListenerCount = 0;
private SecureRandom mRandom;
2014-02-27 23:22:56 +00:00
private OpenPgpServiceConnection pgpServiceConnection;
private PgpEngine mPgpEngine = null;
private WakeLock wakeLock;
2014-04-03 08:41:21 +00:00
private PowerManager pm;
2014-10-21 12:57:16 +00:00
private LruCache<String, Bitmap> mBitmapCache;
2014-12-13 12:52:57 +00:00
private Thread mPhoneContactMergerThread;
2014-02-27 23:22:56 +00:00
private boolean mRestoredFromDatabase = false;
2015-02-12 17:53:00 +00:00
public boolean areMessagesInitialized() {
return this.mRestoredFromDatabase;
2015-02-12 17:53:00 +00:00
}
2014-02-27 23:22:56 +00:00
public PgpEngine getPgpEngine() {
if (pgpServiceConnection.isBound()) {
if (this.mPgpEngine == null) {
this.mPgpEngine = new PgpEngine(new OpenPgpApi(
2014-11-15 16:09:02 +00:00
getApplicationContext(),
pgpServiceConnection.getService()), this);
2014-02-27 23:22:56 +00:00
}
return mPgpEngine;
} else {
return null;
}
}
2014-04-05 19:06:10 +00:00
public FileBackend getFileBackend() {
return this.fileBackend;
}
2014-05-01 20:33:49 +00:00
public AvatarService getAvatarService() {
return this.mAvatarService;
}
public void attachLocationToConversation(final Conversation conversation,
final Uri uri,
final UiCallback<Message> callback) {
int encryption = conversation.getNextEncryption(forceEncryption());
if (encryption == Message.ENCRYPTION_PGP) {
encryption = Message.ENCRYPTION_DECRYPTED;
}
Message message = new Message(conversation,uri.toString(),encryption);
if (conversation.getNextCounterpart() != null) {
message.setCounterpart(conversation.getNextCounterpart());
}
if (encryption == Message.ENCRYPTION_DECRYPTED) {
getPgpEngine().encrypt(message, callback);
} else {
callback.success(message);
}
}
public void attachFileToConversation(final Conversation conversation,
final Uri uri,
final UiCallback<Message> callback) {
final Message message;
if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) {
message = new Message(conversation, "",
Message.ENCRYPTION_DECRYPTED);
} else {
message = new Message(conversation, "",
conversation.getNextEncryption(forceEncryption()));
}
message.setCounterpart(conversation.getNextCounterpart());
message.setType(Message.TYPE_FILE);
2014-11-13 20:04:05 +00:00
String path = getFileBackend().getOriginalPath(uri);
if (path!=null) {
message.setRelativeFilePath(path);
getFileBackend().updateFileParams(message);
2014-11-14 02:27:18 +00:00
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
getPgpEngine().encrypt(message, callback);
} else {
callback.success(message);
}
} else {
mFileAddingExecutor.execute(new Runnable() {
@Override
public void run() {
try {
getFileBackend().copyFileToPrivateStorage(message, uri);
getFileBackend().updateFileParams(message);
2014-11-14 02:27:18 +00:00
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
getPgpEngine().encrypt(message, callback);
} else {
callback.success(message);
}
} catch (FileBackend.FileCopyException e) {
callback.error(e.getResId(), message);
}
}
});
2014-11-13 20:04:05 +00:00
}
}
public void attachImageToConversation(final Conversation conversation,
2014-11-15 16:09:02 +00:00
final Uri uri, final UiCallback<Message> callback) {
final Message message;
if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) {
2014-05-14 10:56:34 +00:00
message = new Message(conversation, "",
Message.ENCRYPTION_DECRYPTED);
} else {
message = new Message(conversation, "",
conversation.getNextEncryption(forceEncryption()));
}
message.setCounterpart(conversation.getNextCounterpart());
2014-05-06 19:34:30 +00:00
message.setType(Message.TYPE_IMAGE);
mFileAddingExecutor.execute(new Runnable() {
2014-05-01 20:33:49 +00:00
@Override
public void run() {
2014-05-14 16:32:58 +00:00
try {
getFileBackend().copyImageToPrivateStorage(message, uri);
if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) {
getPgpEngine().encrypt(message, callback);
} else {
callback.success(message);
}
} catch (final FileBackend.FileCopyException e) {
2014-06-11 19:53:25 +00:00
callback.error(e.getResId(), message);
}
}
});
2014-05-06 19:34:30 +00:00
}
2014-05-14 10:56:34 +00:00
public Conversation find(Bookmark bookmark) {
2014-08-26 14:52:42 +00:00
return find(bookmark.getAccount(), bookmark.getJid());
2014-07-14 09:47:42 +00:00
}
2014-08-26 14:52:42 +00:00
public Conversation find(final Account account, final Jid jid) {
2014-08-26 14:52:42 +00:00
return find(getConversations(), account, jid);
2014-03-03 04:01:02 +00:00
}
2014-02-03 17:38:47 +00:00
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final String action = intent == null ? null : intent.getAction();
if (action != null) {
2015-01-08 13:45:44 +00:00
switch (action) {
case ConnectivityManager.CONNECTIVITY_ACTION:
if (hasInternetConnection() && Config.RESET_ATTEMPT_COUNT_ON_NETWORK_CHANGE) {
resetAllAttemptCounts(true);
}
break;
2015-01-08 13:45:44 +00:00
case ACTION_MERGE_PHONE_CONTACTS:
if (mRestoredFromDatabase) {
PhoneHelper.loadPhoneContacts(getApplicationContext(),
new CopyOnWriteArrayList<Bundle>(),
this);
}
2015-01-08 13:45:44 +00:00
return START_STICKY;
case Intent.ACTION_SHUTDOWN:
logoutAndSave();
return START_NOT_STICKY;
case ACTION_CLEAR_NOTIFICATION:
mNotificationService.clear();
break;
case ACTION_DISABLE_FOREGROUND:
getPreferences().edit().putBoolean("keep_foreground_service",false).commit();
toggleForegroundService();
break;
2015-02-10 16:13:34 +00:00
case ACTION_TRY_AGAIN:
resetAllAttemptCounts(false);
2015-02-10 16:13:34 +00:00
break;
case ACTION_DISABLE_ACCOUNT:
try {
String jid = intent.getStringExtra("account");
Account account = jid == null ? null : findAccountByJid(Jid.fromString(jid));
if (account != null) {
account.setOption(Account.OPTION_DISABLED,true);
updateAccount(account);
}
} catch (final InvalidJidException ignored) {
break;
}
break;
2014-10-02 15:36:02 +00:00
}
}
this.wakeLock.acquire();
2014-02-03 17:38:47 +00:00
for (Account account : accounts) {
2014-03-11 14:44:22 +00:00
if (!account.isOptionSet(Account.OPTION_DISABLED)) {
if (!hasInternetConnection()) {
2014-11-15 16:09:02 +00:00
account.setStatus(Account.State.NO_INTERNET);
if (statusListener != null) {
2014-03-11 15:49:42 +00:00
statusListener.onStatusChanged(account);
}
} else {
2014-11-15 16:09:02 +00:00
if (account.getStatus() == Account.State.NO_INTERNET) {
account.setStatus(Account.State.OFFLINE);
if (statusListener != null) {
2014-03-11 15:49:42 +00:00
statusListener.onStatusChanged(account);
}
2014-03-11 14:44:22 +00:00
}
2014-11-15 16:09:02 +00:00
if (account.getStatus() == Account.State.ONLINE) {
long lastReceived = account.getXmppConnection().getLastPacketReceived();
long lastSent = account.getXmppConnection().getLastPingSent();
long pingInterval = "ui".equals(action) ? Config.PING_MIN_INTERVAL * 1000 : Config.PING_MAX_INTERVAL * 1000;
2015-01-06 18:46:52 +00:00
long msToNextPing = (Math.max(lastReceived,lastSent) + pingInterval) - SystemClock.elapsedRealtime();
long pingTimeoutIn = (lastSent + Config.PING_TIMEOUT * 1000) - SystemClock.elapsedRealtime();
if (lastSent > lastReceived) {
if (pingTimeoutIn < 0) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": ping timeout");
this.reconnectAccount(account, true);
} else {
int secs = (int) (pingTimeoutIn / 1000);
this.scheduleWakeUpCall(secs,account.getUuid().hashCode());
}
2015-01-06 18:46:52 +00:00
} else if (msToNextPing <= 0) {
2014-03-11 14:44:22 +00:00
account.getXmppConnection().sendPing();
Log.d(Config.LOGTAG, account.getJid().toBareJid()+" send ping");
this.scheduleWakeUpCall(Config.PING_TIMEOUT,account.getUuid().hashCode());
} else {
2015-01-06 18:46:52 +00:00
this.scheduleWakeUpCall((int) (msToNextPing / 1000), account.getUuid().hashCode());
2014-03-11 14:44:22 +00:00
}
2014-11-15 16:09:02 +00:00
} else if (account.getStatus() == Account.State.OFFLINE) {
reconnectAccount(account,true);
2015-01-25 19:52:53 +00:00
} else if (account.getStatus() == Account.State.CONNECTING) {
long timeout = Config.CONNECT_TIMEOUT - ((SystemClock.elapsedRealtime() - account.getXmppConnection().getLastConnect()) / 1000);
if (timeout < 0) {
Log.d(Config.LOGTAG, account.getJid() + ": time out during connect reconnecting");
reconnectAccount(account, true);
} else {
scheduleWakeUpCall((int) timeout,account.getUuid().hashCode());
}
2014-03-11 14:44:22 +00:00
} else {
2014-05-18 09:25:04 +00:00
if (account.getXmppConnection().getTimeToNextAttempt() <= 0) {
reconnectAccount(account, true);
}
}
2014-03-11 14:44:22 +00:00
}
2014-07-12 11:42:17 +00:00
if (mOnAccountUpdate != null) {
mOnAccountUpdate.onAccountUpdate();
}
2014-02-03 17:38:47 +00:00
}
}
/*PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
2014-11-15 16:09:02 +00:00
if (!pm.isScreenOn()) {
removeStaleListeners();
2014-11-15 16:09:02 +00:00
}*/
if (wakeLock.isHeld()) {
try {
wakeLock.release();
} catch (final RuntimeException ignored) {
}
}
2014-02-03 17:38:47 +00:00
return START_STICKY;
}
private void resetAllAttemptCounts(boolean reallyAll) {
Log.d(Config.LOGTAG,"resetting all attepmt counts");
for(Account account : accounts) {
if (account.hasErrorStatus() || reallyAll) {
final XmppConnection connection = account.getXmppConnection();
if (connection != null) {
connection.resetAttemptCount();
}
}
}
}
public boolean hasInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) getApplicationContext()
2014-11-15 16:09:02 +00:00
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnected();
}
2014-02-03 17:38:47 +00:00
@SuppressLint("TrulyRandom")
2014-02-03 17:38:47 +00:00
@Override
public void onCreate() {
2014-03-09 12:21:28 +00:00
ExceptionHelper.init(getApplicationContext());
PRNGFixes.apply();
this.mRandom = new SecureRandom();
updateMemorizingTrustmanager();
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
2014-10-21 12:57:16 +00:00
this.mBitmapCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(final String key, final Bitmap bitmap) {
2014-10-21 12:57:16 +00:00
return bitmap.getByteCount() / 1024;
}
};
this.databaseBackend = DatabaseBackend.getInstance(getApplicationContext());
2014-02-03 17:38:47 +00:00
this.accounts = databaseBackend.getAccounts();
2014-02-11 22:55:03 +00:00
restoreFromDatabase();
2014-05-01 20:33:49 +00:00
getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactObserver);
this.fileObserver.startWatching();
this.pgpServiceConnection = new OpenPgpServiceConnection(getApplicationContext(), "org.sufficientlysecure.keychain");
2014-02-27 23:22:56 +00:00
this.pgpServiceConnection.bindToService();
2014-03-11 14:44:22 +00:00
2014-04-03 08:41:21 +00:00
this.pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"XmppConnectionService");
toggleForegroundService();
updateUnreadCountBadge();
2014-02-03 17:38:47 +00:00
}
public void toggleForegroundService() {
if (getPreferences().getBoolean("keep_foreground_service",false)) {
startForeground(NotificationService.FOREGROUND_NOTIFICATION_ID, this.mNotificationService.createForegroundNotification());
} else {
stopForeground(true);
}
}
2014-05-22 07:36:00 +00:00
@Override
public void onTaskRemoved(final Intent rootIntent) {
super.onTaskRemoved(rootIntent);
if (!getPreferences().getBoolean("keep_foreground_service",false)) {
this.logoutAndSave();
}
}
2014-05-22 07:36:00 +00:00
private void logoutAndSave() {
for (final Account account : accounts) {
databaseBackend.writeRoster(account.getRoster());
2014-02-13 22:40:08 +00:00
if (account.getXmppConnection() != null) {
2014-05-21 20:22:36 +00:00
disconnect(account, false);
2014-02-13 22:40:08 +00:00
}
}
2014-05-21 20:22:36 +00:00
Context context = getApplicationContext();
2014-05-22 07:36:00 +00:00
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
2014-05-21 20:22:36 +00:00
Intent intent = new Intent(context, EventReceiver.class);
alarmManager.cancel(PendingIntent.getBroadcast(context, 0, intent, 0));
2014-08-31 14:28:21 +00:00
Log.d(Config.LOGTAG, "good bye");
2014-05-21 20:22:36 +00:00
stopSelf();
2014-02-13 22:40:08 +00:00
}
2014-03-11 14:44:22 +00:00
protected void scheduleWakeUpCall(int seconds, int requestCode) {
final long timeToWake = SystemClock.elapsedRealtime() + (seconds < 0 ? 1 : seconds + 1) * 1000;
Context context = getApplicationContext();
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
2014-03-11 14:44:22 +00:00
Intent intent = new Intent(context, EventReceiver.class);
intent.setAction("ping");
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, requestCode, intent, 0);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeToWake, alarmIntent);
}
2014-02-13 22:40:08 +00:00
public XmppConnection createConnection(final Account account) {
final SharedPreferences sharedPref = getPreferences();
account.setResource(sharedPref.getString("resource", "mobile")
.toLowerCase(Locale.getDefault()));
final XmppConnection connection = new XmppConnection(account, this);
connection.setOnMessagePacketReceivedListener(this.mMessageParser);
connection.setOnStatusChangedListener(this.statusListener);
connection.setOnPresencePacketReceivedListener(this.mPresenceParser);
2014-08-26 14:52:42 +00:00
connection.setOnUnregisteredIqPacketReceivedListener(this.mIqParser);
connection.setOnJinglePacketReceivedListener(this.jingleListener);
2014-07-18 19:57:10 +00:00
connection.setOnBindListener(this.mOnBindListener);
2014-12-05 00:54:16 +00:00
connection.setOnMessageAcknowledgeListener(this.mOnMessageAcknowledgedListener);
connection.addOnAdvancedStreamFeaturesAvailableListener(this.mMessageArchiveService);
return connection;
}
public void sendChatState(Conversation conversation) {
if (sendChatStates()) {
MessagePacket packet = mMessageGenerator.generateChatState(conversation);
sendMessagePacket(conversation.getAccount(), packet);
}
}
private void sendFileMessage(final Message message) {
Log.d(Config.LOGTAG, "send file message");
final Account account = message.getConversation().getAccount();
final XmppConnection connection = account.getXmppConnection();
if (connection != null && connection.getFeatures().httpUpload()) {
mHttpConnectionManager.createNewUploadConnection(message);
} else {
mJingleConnectionManager.createNewConnection(message);
}
}
public void sendMessage(final Message message) {
sendMessage(message, false);
}
private void sendMessage(final Message message, final boolean resend) {
final Account account = message.getConversation().getAccount();
final Conversation conversation = message.getConversation();
account.deactivateGracePeriod();
2014-04-11 07:13:56 +00:00
MessagePacket packet = null;
2014-06-16 12:06:34 +00:00
boolean saveInDb = true;
message.setStatus(Message.STATUS_WAITING);
if (!resend && message.getEncryption() != Message.ENCRYPTION_OTR) {
message.getConversation().endOtrIfNeeded();
message.getConversation().findUnsentMessagesWithEncryption(Message.ENCRYPTION_OTR,
new Conversation.OnMessageFound() {
@Override
public void onMessageFound(Message message) {
markMessage(message,Message.STATUS_SEND_FAILED);
}
});
}
if (account.isOnlineAndConnected()) {
switch (message.getEncryption()) {
case Message.ENCRYPTION_NONE:
if (message.needsUploading()) {
if (account.httpUploadAvailable() || message.fixCounterpart()) {
this.sendFileMessage(message);
} else {
break;
}
} else {
packet = mMessageGenerator.generateChat(message,resend);
}
break;
case Message.ENCRYPTION_PGP:
case Message.ENCRYPTION_DECRYPTED:
if (message.needsUploading()) {
if (account.httpUploadAvailable() || message.fixCounterpart()) {
this.sendFileMessage(message);
} else {
break;
}
} else {
packet = mMessageGenerator.generatePgpChat(message,resend);
}
break;
case Message.ENCRYPTION_OTR:
SessionImpl otrSession = conversation.getOtrSession();
if (otrSession != null && otrSession.getSessionStatus() == SessionStatus.ENCRYPTED) {
try {
message.setCounterpart(Jid.fromSessionID(otrSession.getSessionID()));
} catch (InvalidJidException e) {
break;
}
if (message.needsUploading()) {
mJingleConnectionManager.createNewConnection(message);
} else {
packet = mMessageGenerator.generateOtrChat(message,resend);
}
} else if (otrSession == null) {
if (message.fixCounterpart()) {
conversation.startOtrSession(message.getCounterpart().getResourcepart(), true);
} else {
break;
}
}
break;
2015-06-25 15:01:42 +00:00
case Message.ENCRYPTION_AXOLOTL:
message.setStatus(Message.STATUS_WAITING);
packet = account.getAxolotlService().fetchPacketFromCache(message);
if (packet == null && account.isOnlineAndConnected()) {
account.getAxolotlService().prepareMessage(message);
message.setAxolotlFingerprint(account.getAxolotlService().getOwnPublicKey().getFingerprint().replaceAll("\\s", ""));
}
2015-06-25 15:01:42 +00:00
break;
}
if (packet != null) {
if (account.getXmppConnection().getFeatures().sm() || conversation.getMode() == Conversation.MODE_MULTI) {
message.setStatus(Message.STATUS_UNSEND);
} else {
message.setStatus(Message.STATUS_SEND);
2014-02-11 22:55:03 +00:00
}
}
} else {
switch(message.getEncryption()) {
case Message.ENCRYPTION_DECRYPTED:
if (!message.needsUploading()) {
String pgpBody = message.getEncryptedBody();
String decryptedBody = message.getBody();
message.setBody(pgpBody);
message.setEncryption(Message.ENCRYPTION_PGP);
databaseBackend.createMessage(message);
saveInDb = false;
message.setBody(decryptedBody);
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
}
break;
case Message.ENCRYPTION_OTR:
if (!conversation.hasValidOtrSession() && message.getCounterpart() != null) {
conversation.startOtrSession(message.getCounterpart().getResourcepart(), false);
}
break;
case Message.ENCRYPTION_AXOLOTL:
message.setAxolotlFingerprint(account.getAxolotlService().getOwnPublicKey().getFingerprint().replaceAll("\\s", ""));
break;
2014-05-16 11:42:20 +00:00
}
2014-02-13 22:40:08 +00:00
}
if (resend) {
if (packet != null) {
if (account.getXmppConnection().getFeatures().sm() || conversation.getMode() == Conversation.MODE_MULTI) {
markMessage(message,Message.STATUS_UNSEND);
} else {
markMessage(message,Message.STATUS_SEND);
}
}
} else {
conversation.add(message);
if (saveInDb && (message.getEncryption() == Message.ENCRYPTION_NONE || saveEncryptedMessages())) {
databaseBackend.createMessage(message);
}
updateConversationUi();
2014-02-13 22:40:08 +00:00
}
if (packet != null) {
if (conversation.setOutgoingChatState(Config.DEFAULT_CHATSTATE)) {
if (this.sendChatStates()) {
packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
}
}
sendMessagePacket(account, packet);
2014-04-11 07:13:56 +00:00
}
2014-02-11 22:55:03 +00:00
}
private void sendUnsentMessages(final Conversation conversation) {
conversation.findWaitingMessages(new Conversation.OnMessageFound() {
@Override
public void onMessageFound(Message message) {
resendMessage(message);
2014-05-16 11:42:20 +00:00
}
});
2014-05-16 11:42:20 +00:00
}
2014-05-18 09:25:04 +00:00
public void resendMessage(final Message message) {
sendMessage(message, true);
2014-02-11 22:55:03 +00:00
}
public void fetchRosterFromServer(final Account account) {
2014-12-30 13:16:25 +00:00
final IqPacket iqPacket = new IqPacket(IqPacket.TYPE.GET);
2014-03-08 01:06:00 +00:00
if (!"".equals(account.getRosterVersion())) {
Log.d(Config.LOGTAG, account.getJid().toBareJid()
2014-08-31 14:28:21 +00:00
+ ": fetching roster version " + account.getRosterVersion());
2014-03-08 01:06:00 +00:00
} else {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": fetching roster");
2014-03-08 01:06:00 +00:00
}
iqPacket.query(Xmlns.ROSTER).setAttribute("ver",account.getRosterVersion());
sendIqPacket(account,iqPacket,mIqParser);
2014-02-10 02:34:00 +00:00
}
2014-08-26 14:52:42 +00:00
public void fetchBookmarks(final Account account) {
2014-12-30 13:16:25 +00:00
final IqPacket iqPacket = new IqPacket(IqPacket.TYPE.GET);
final Element query = iqPacket.query("jabber:iq:private");
2014-07-14 09:47:42 +00:00
query.addChild("storage", "storage:bookmarks");
final OnIqPacketReceived callback = new OnIqPacketReceived() {
2014-08-26 14:52:42 +00:00
2014-07-14 09:47:42 +00:00
@Override
public void onIqPacketReceived(final Account account, final IqPacket packet) {
final Element query = packet.query();
final List<Bookmark> bookmarks = new CopyOnWriteArrayList<>();
final Element storage = query.findChild("storage",
2014-08-26 14:52:42 +00:00
"storage:bookmarks");
if (storage != null) {
for (final Element item : storage.getChildren()) {
2014-07-14 09:47:42 +00:00
if (item.getName().equals("conference")) {
final Bookmark bookmark = Bookmark.parse(item, account);
2014-07-14 09:47:42 +00:00
bookmarks.add(bookmark);
Conversation conversation = find(bookmark);
2014-08-26 14:52:42 +00:00
if (conversation != null) {
conversation.setBookmark(bookmark);
2014-11-29 18:09:28 +00:00
} else if (bookmark.autojoin() && bookmark.getJid() != null) {
conversation = findOrCreateConversation(
account, bookmark.getJid(), true);
conversation.setBookmark(bookmark);
joinMuc(conversation);
2014-07-14 09:47:42 +00:00
}
}
}
}
account.setBookmarks(bookmarks);
}
};
sendIqPacket(account, iqPacket, callback);
}
2014-08-26 14:52:42 +00:00
2014-07-15 12:32:19 +00:00
public void pushBookmarks(Account account) {
2014-12-30 13:16:25 +00:00
IqPacket iqPacket = new IqPacket(IqPacket.TYPE.SET);
2014-07-15 12:32:19 +00:00
Element query = iqPacket.query("jabber:iq:private");
Element storage = query.addChild("storage", "storage:bookmarks");
2014-08-26 14:52:42 +00:00
for (Bookmark bookmark : account.getBookmarks()) {
storage.addChild(bookmark);
2014-07-15 12:32:19 +00:00
}
sendIqPacket(account, iqPacket, mDefaultIqHandler);
2014-07-15 12:32:19 +00:00
}
2014-02-10 02:34:00 +00:00
2014-12-13 12:52:57 +00:00
public void onPhoneContactsLoaded(final List<Bundle> phoneContacts) {
if (mPhoneContactMergerThread != null) {
mPhoneContactMergerThread.interrupt();
}
mPhoneContactMergerThread = new Thread(new Runnable() {
@Override
public void run() {
Log.d(Config.LOGTAG,"start merging phone contacts with roster");
for (Account account : accounts) {
2015-06-03 12:05:54 +00:00
List<Contact> withSystemAccounts = account.getRoster().getWithSystemAccounts();
2014-12-13 12:52:57 +00:00
for (Bundle phoneContact : phoneContacts) {
if (Thread.interrupted()) {
Log.d(Config.LOGTAG,"interrupted merging phone contacts");
return;
}
2014-12-13 12:52:57 +00:00
Jid jid;
try {
jid = Jid.fromString(phoneContact.getString("jid"));
} catch (final InvalidJidException e) {
2014-12-20 11:52:45 +00:00
continue;
2014-02-10 02:34:00 +00:00
}
2014-12-20 11:52:45 +00:00
final Contact contact = account.getRoster().getContact(jid);
2014-12-13 12:52:57 +00:00
String systemAccount = phoneContact.getInt("phoneid")
+ "#"
+ phoneContact.getString("lookup");
2014-12-13 12:52:57 +00:00
contact.setSystemAccount(systemAccount);
2015-06-03 12:05:54 +00:00
if (contact.setPhotoUri(phoneContact.getString("photouri"))) {
getAvatarService().clear(contact);
}
2014-12-13 12:52:57 +00:00
contact.setSystemName(phoneContact.getString("displayname"));
2015-06-03 12:05:54 +00:00
withSystemAccounts.remove(contact);
}
for(Contact contact : withSystemAccounts) {
contact.setSystemAccount(null);
contact.setSystemName(null);
if (contact.setPhotoUri(null)) {
getAvatarService().clear(contact);
}
2014-02-10 02:34:00 +00:00
}
2014-12-13 12:52:57 +00:00
}
Log.d(Config.LOGTAG,"finished merging phone contacts");
updateAccountUi();
}
});
mPhoneContactMergerThread.start();
2014-02-03 17:38:47 +00:00
}
private void restoreFromDatabase() {
synchronized (this.conversations) {
final Map<String, Account> accountLookupTable = new Hashtable<>();
2014-02-03 17:38:47 +00:00
for (Account account : this.accounts) {
accountLookupTable.put(account.getUuid(), account);
}
this.conversations.addAll(databaseBackend.getConversations(Conversation.STATUS_AVAILABLE));
for (Conversation conversation : this.conversations) {
Account account = accountLookupTable.get(conversation.getAccountUuid());
conversation.setAccount(account);
2014-02-03 17:38:47 +00:00
}
Runnable runnable =new Runnable() {
2015-02-12 17:53:00 +00:00
@Override
public void run() {
Log.d(Config.LOGTAG,"restoring roster");
for(Account account : accounts) {
databaseBackend.readRoster(account.getRoster());
account.initAccountServices(XmppConnectionService.this);
}
getBitmapCache().evictAll();
Looper.prepare();
PhoneHelper.loadPhoneContacts(getApplicationContext(),
new CopyOnWriteArrayList<Bundle>(),
XmppConnectionService.this);
Log.d(Config.LOGTAG,"restoring messages");
2015-02-12 17:53:00 +00:00
for (Conversation conversation : conversations) {
conversation.addAll(0, databaseBackend.getMessages(conversation, Config.PAGE_SIZE));
checkDeletedFiles(conversation);
}
mRestoredFromDatabase = true;
Log.d(Config.LOGTAG,"restored all messages");
2015-02-12 17:53:00 +00:00
updateConversationUi();
}
};
mDatabaseExecutor.execute(runnable);
2014-02-03 17:38:47 +00:00
}
}
public List<Conversation> getConversations() {
2014-07-12 10:41:37 +00:00
return this.conversations;
}
private void checkDeletedFiles(Conversation conversation) {
conversation.findMessagesWithFiles(new Conversation.OnMessageFound() {
@Override
public void onMessageFound(Message message) {
if (!getFileBackend().isFileAvailable(message)) {
2015-07-10 13:11:03 +00:00
message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_DELETED));
}
}
});
}
private void markFileDeleted(String uuid) {
for (Conversation conversation : getConversations()) {
Message message = conversation.findMessageWithFileAndUuid(uuid);
if (message != null) {
if (!getFileBackend().isFileAvailable(message)) {
2015-07-10 13:11:03 +00:00
message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_DELETED));
updateConversationUi();
}
return;
}
}
}
2014-09-05 11:29:20 +00:00
public void populateWithOrderedConversations(final List<Conversation> list) {
2014-09-05 11:29:20 +00:00
populateWithOrderedConversations(list, true);
2014-09-02 13:51:20 +00:00
}
public void populateWithOrderedConversations(final List<Conversation> list, boolean includeNoFileUpload) {
2014-07-12 10:41:37 +00:00
list.clear();
if (includeNoFileUpload) {
2014-09-02 13:51:20 +00:00
list.addAll(getConversations());
} else {
2014-09-05 11:29:20 +00:00
for (Conversation conversation : getConversations()) {
if (conversation.getMode() == Conversation.MODE_SINGLE
|| conversation.getAccount().httpUploadAvailable()) {
2014-09-02 13:51:20 +00:00
list.add(conversation);
}
}
}
2014-07-12 10:41:37 +00:00
Collections.sort(list, new Comparator<Conversation>() {
@Override
public int compare(Conversation lhs, Conversation rhs) {
Message left = lhs.getLatestMessage();
Message right = rhs.getLatestMessage();
if (left.getTimeSent() > right.getTimeSent()) {
return -1;
} else if (left.getTimeSent() < right.getTimeSent()) {
return 1;
} else {
return 0;
}
}
});
2014-02-03 17:38:47 +00:00
}
2014-12-17 08:32:51 +00:00
public void loadMoreMessages(final Conversation conversation, final long timestamp, final OnMoreMessagesLoaded callback) {
Log.d(Config.LOGTAG, "load more messages for " + conversation.getName() + " prior to " + MessageGenerator.getTimestamp(timestamp));
if (XmppConnectionService.this.getMessageArchiveService().queryInProgress(conversation,callback)) {
return;
}
Runnable runnable = new Runnable() {
2014-12-17 08:32:51 +00:00
@Override
public void run() {
final Account account = conversation.getAccount();
List<Message> messages = databaseBackend.getMessages(conversation, 50,timestamp);
if (messages.size() > 0) {
conversation.addAll(0, messages);
checkDeletedFiles(conversation);
2014-12-17 08:32:51 +00:00
callback.onMoreMessagesLoaded(messages.size(), conversation);
2015-01-02 23:47:22 +00:00
} else if (conversation.hasMessagesLeftOnServer()
&& account.isOnlineAndConnected()
&& account.getXmppConnection().getFeatures().mam()) {
2014-12-17 08:32:51 +00:00
MessageArchiveService.Query query = getMessageArchiveService().query(conversation,0,timestamp - 1);
if (query != null) {
query.setCallback(callback);
}
callback.informUser(R.string.fetching_history_from_server);
}
}
};
mDatabaseExecutor.execute(runnable);
}
2014-02-03 17:38:47 +00:00
public List<Account> getAccounts() {
return this.accounts;
}
public Conversation find(final Iterable<Conversation> haystack, final Contact contact) {
for (final Conversation conversation : haystack) {
if (conversation.getContact() == contact) {
return conversation;
}
}
return null;
}
2014-02-11 22:55:03 +00:00
public Conversation find(final Iterable<Conversation> haystack, final Account account, final Jid jid) {
if (jid == null) {
2014-11-29 18:09:28 +00:00
return null;
}
for (final Conversation conversation : haystack) {
2014-10-28 16:15:35 +00:00
if ((account == null || conversation.getAccount() == account)
&& (conversation.getJid().toBareJid().equals(jid.toBareJid()))) {
return conversation;
}
}
return null;
}
2014-08-26 14:52:42 +00:00
public Conversation findOrCreateConversation(final Account account, final Jid jid, final boolean muc) {
return this.findOrCreateConversation(account, jid, muc, null);
2014-12-13 11:25:52 +00:00
}
public Conversation findOrCreateConversation(final Account account, final Jid jid, final boolean muc, final MessageArchiveService.Query query) {
synchronized (this.conversations) {
Conversation conversation = find(account, jid);
if (conversation != null) {
return conversation;
}
conversation = databaseBackend.findConversation(account, jid);
if (conversation != null) {
conversation.setStatus(Conversation.STATUS_AVAILABLE);
conversation.setAccount(account);
if (muc) {
conversation.setMode(Conversation.MODE_MULTI);
conversation.setContactJid(jid);
} else {
conversation.setMode(Conversation.MODE_SINGLE);
conversation.setContactJid(jid.toBareJid());
}
conversation.setNextEncryption(-1);
conversation.addAll(0, databaseBackend.getMessages(conversation, Config.PAGE_SIZE));
this.databaseBackend.updateConversation(conversation);
} else {
String conversationName;
Contact contact = account.getRoster().getContact(jid);
if (contact != null) {
conversationName = contact.getDisplayName();
} else {
conversationName = jid.getLocalpart();
}
if (muc) {
conversation = new Conversation(conversationName, account, jid,
Conversation.MODE_MULTI);
} else {
conversation = new Conversation(conversationName, account, jid.toBareJid(),
Conversation.MODE_SINGLE);
}
this.databaseBackend.createConversation(conversation);
}
2015-01-23 23:22:51 +00:00
if (account.getXmppConnection() != null
&& account.getXmppConnection().getFeatures().mam()
&& !muc) {
if (query == null) {
this.mMessageArchiveService.query(conversation);
} else {
if (query.getConversation() == null) {
this.mMessageArchiveService.query(conversation, query.getStart());
}
2014-12-13 11:25:52 +00:00
}
}
checkDeletedFiles(conversation);
this.conversations.add(conversation);
updateConversationUi();
return conversation;
2014-02-03 17:38:47 +00:00
}
}
public void archiveConversation(Conversation conversation) {
getNotificationService().clear(conversation);
conversation.setStatus(Conversation.STATUS_ARCHIVED);
conversation.setNextEncryption(-1);
synchronized (this.conversations) {
if (conversation.getMode() == Conversation.MODE_MULTI) {
if (conversation.getAccount().getStatus() == Account.State.ONLINE) {
Bookmark bookmark = conversation.getBookmark();
if (bookmark != null && bookmark.autojoin()) {
bookmark.setAutojoin(false);
pushBookmarks(bookmark.getAccount());
}
2014-10-08 12:10:37 +00:00
}
leaveMuc(conversation);
} else {
conversation.endOtrIfNeeded();
2014-07-15 12:32:19 +00:00
}
this.databaseBackend.updateConversation(conversation);
this.conversations.remove(conversation);
updateConversationUi();
2014-02-13 22:40:08 +00:00
}
2014-02-03 17:38:47 +00:00
}
2014-05-01 20:33:49 +00:00
public void createAccount(final Account account) {
2015-05-26 02:36:32 +00:00
account.initAccountServices(this);
2014-01-28 18:21:54 +00:00
databaseBackend.createAccount(account);
this.accounts.add(account);
2015-03-05 14:46:33 +00:00
this.reconnectAccountInBackground(account);
2014-07-12 11:42:17 +00:00
updateAccountUi();
2014-01-28 18:21:54 +00:00
}
public void updateAccount(final Account account) {
this.statusListener.onStatusChanged(account);
2014-01-28 18:21:54 +00:00
databaseBackend.updateAccount(account);
reconnectAccount(account, false);
2014-07-12 11:42:17 +00:00
updateAccountUi();
getNotificationService().updateErrorNotification();
2014-01-28 18:21:54 +00:00
}
public void updateAccountPasswordOnServer(final Account account, final String newPassword, final OnAccountPasswordChanged callback) {
final IqPacket iq = getIqGenerator().generateSetPassword(account, newPassword);
sendIqPacket(account, iq, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(final Account account, final IqPacket packet) {
2014-12-30 13:16:25 +00:00
if (packet.getType() == IqPacket.TYPE.RESULT) {
account.setPassword(newPassword);
databaseBackend.updateAccount(account);
callback.onPasswordChangeSucceeded();
} else {
callback.onPasswordChangeFailed();
}
}
});
}
public void deleteAccount(final Account account) {
synchronized (this.conversations) {
for (final Conversation conversation : conversations) {
if (conversation.getAccount() == account) {
if (conversation.getMode() == Conversation.MODE_MULTI) {
leaveMuc(conversation);
} else if (conversation.getMode() == Conversation.MODE_SINGLE) {
conversation.endOtrIfNeeded();
}
conversations.remove(conversation);
2014-07-21 16:20:26 +00:00
}
}
if (account.getXmppConnection() != null) {
this.disconnect(account, true);
}
databaseBackend.deleteAccount(account);
this.accounts.remove(account);
updateAccountUi();
getNotificationService().updateErrorNotification();
2014-07-21 16:20:26 +00:00
}
2014-01-28 18:21:54 +00:00
}
2014-02-03 17:38:47 +00:00
2014-12-03 13:55:09 +00:00
public void setOnConversationListChangedListener(OnConversationUpdate listener) {
synchronized (this) {
2014-10-15 12:41:27 +00:00
if (checkListeners()) {
switchToForeground();
}
this.mOnConversationUpdate = listener;
this.mNotificationService.setIsInForeground(true);
2014-12-03 13:55:09 +00:00
if (this.convChangedListenerCount < 2) {
this.convChangedListenerCount++;
2014-11-15 16:09:02 +00:00
}
2014-12-03 13:55:09 +00:00
}
}
2014-02-03 17:38:47 +00:00
2014-02-01 14:07:20 +00:00
public void removeOnConversationListChangedListener() {
2014-12-03 13:55:09 +00:00
synchronized (this) {
2014-10-15 12:41:27 +00:00
this.convChangedListenerCount--;
if (this.convChangedListenerCount <= 0) {
this.convChangedListenerCount = 0;
this.mOnConversationUpdate = null;
this.mNotificationService.setIsInForeground(false);
if (checkListeners()) {
switchToBackground();
}
2014-08-26 15:43:44 +00:00
}
2014-03-29 19:29:03 +00:00
}
2014-02-01 14:07:20 +00:00
}
public void setOnShowErrorToastListener(OnShowErrorToast onShowErrorToast) {
synchronized (this) {
if (checkListeners()) {
switchToForeground();
}
this.mOnShowErrorToast = onShowErrorToast;
if (this.showErrorToastListenerCount < 2) {
this.showErrorToastListenerCount++;
}
}
this.mOnShowErrorToast = onShowErrorToast;
}
public void removeOnShowErrorToastListener() {
synchronized (this) {
this.showErrorToastListenerCount--;
if (this.showErrorToastListenerCount <= 0) {
this.showErrorToastListenerCount = 0;
this.mOnShowErrorToast = null;
if (checkListeners()) {
switchToBackground();
}
}
}
}
2014-07-12 11:42:17 +00:00
public void setOnAccountListChangedListener(OnAccountUpdate listener) {
2014-12-03 13:55:09 +00:00
synchronized (this) {
2014-10-15 12:41:27 +00:00
if (checkListeners()) {
switchToForeground();
}
this.mOnAccountUpdate = listener;
2014-12-03 13:55:09 +00:00
if (this.accountChangedListenerCount < 2) {
this.accountChangedListenerCount++;
}
2014-08-26 15:43:44 +00:00
}
}
public void removeOnAccountListChangedListener() {
2014-12-03 13:55:09 +00:00
synchronized (this) {
2014-10-15 12:41:27 +00:00
this.accountChangedListenerCount--;
if (this.accountChangedListenerCount <= 0) {
this.mOnAccountUpdate = null;
this.accountChangedListenerCount = 0;
if (checkListeners()) {
switchToBackground();
}
2014-08-26 15:43:44 +00:00
}
}
}
2014-08-26 14:52:42 +00:00
public void setOnRosterUpdateListener(final OnRosterUpdate listener) {
2014-12-03 13:55:09 +00:00
synchronized (this) {
2014-10-15 12:41:27 +00:00
if (checkListeners()) {
switchToForeground();
}
this.mOnRosterUpdate = listener;
2014-12-03 13:55:09 +00:00
if (this.rosterChangedListenerCount < 2) {
this.rosterChangedListenerCount++;
}
2014-08-26 15:43:44 +00:00
}
2014-07-18 13:35:31 +00:00
}
public void removeOnRosterUpdateListener() {
2014-12-03 13:55:09 +00:00
synchronized (this) {
2014-10-15 12:41:27 +00:00
this.rosterChangedListenerCount--;
if (this.rosterChangedListenerCount <= 0) {
this.rosterChangedListenerCount = 0;
this.mOnRosterUpdate = null;
if (checkListeners()) {
switchToBackground();
}
2014-09-10 15:59:57 +00:00
}
2014-08-26 15:43:44 +00:00
}
}
public void setOnUpdateBlocklistListener(final OnUpdateBlocklist listener) {
synchronized (this) {
if (checkListeners()) {
switchToForeground();
}
this.mOnUpdateBlocklist = listener;
if (this.updateBlocklistListenerCount < 2) {
this.updateBlocklistListenerCount++;
}
}
}
public void removeOnUpdateBlocklistListener() {
synchronized (this) {
this.updateBlocklistListenerCount--;
if (this.updateBlocklistListenerCount <= 0) {
this.updateBlocklistListenerCount = 0;
this.mOnUpdateBlocklist = null;
if (checkListeners()) {
switchToBackground();
}
}
}
}
public void setOnMucRosterUpdateListener(OnMucRosterUpdate listener) {
2014-12-03 13:55:09 +00:00
synchronized (this) {
if (checkListeners()) {
switchToForeground();
}
this.mOnMucRosterUpdate = listener;
2014-12-03 13:55:09 +00:00
if (this.mucRosterChangedListenerCount < 2) {
this.mucRosterChangedListenerCount++;
}
}
}
public void removeOnMucRosterUpdateListener() {
2014-12-03 13:55:09 +00:00
synchronized (this) {
this.mucRosterChangedListenerCount--;
if (this.mucRosterChangedListenerCount <= 0) {
this.mucRosterChangedListenerCount = 0;
this.mOnMucRosterUpdate = null;
if (checkListeners()) {
switchToBackground();
}
}
}
}
2014-08-26 15:43:44 +00:00
private boolean checkListeners() {
return (this.mOnAccountUpdate == null
&& this.mOnConversationUpdate == null
&& this.mOnRosterUpdate == null
&& this.mOnUpdateBlocklist == null
&& this.mOnShowErrorToast == null);
2014-08-26 15:43:44 +00:00
}
2014-08-26 15:43:44 +00:00
private void switchToForeground() {
for (Account account : getAccounts()) {
2014-11-15 16:09:02 +00:00
if (account.getStatus() == Account.State.ONLINE) {
2014-08-26 15:43:44 +00:00
XmppConnection connection = account.getXmppConnection();
if (connection != null && connection.getFeatures().csi()) {
connection.sendActive();
}
}
}
Log.d(Config.LOGTAG, "app switched into foreground");
2014-08-26 15:43:44 +00:00
}
2014-08-26 15:43:44 +00:00
private void switchToBackground() {
for (Account account : getAccounts()) {
2014-11-15 16:09:02 +00:00
if (account.getStatus() == Account.State.ONLINE) {
2014-08-26 15:43:44 +00:00
XmppConnection connection = account.getXmppConnection();
if (connection != null && connection.getFeatures().csi()) {
connection.sendInactive();
}
}
}
for(Conversation conversation : getConversations()) {
conversation.setIncomingChatState(ChatState.ACTIVE);
}
this.mNotificationService.setIsInForeground(false);
Log.d(Config.LOGTAG, "app switched into background");
2014-07-18 13:35:31 +00:00
}
2014-10-15 12:41:27 +00:00
private void connectMultiModeConversations(Account account) {
List<Conversation> conversations = getConversations();
for (Conversation conversation : conversations) {
if ((conversation.getMode() == Conversation.MODE_MULTI)
&& (conversation.getAccount() == account)) {
conversation.resetMucOptions();
joinMuc(conversation);
}
}
}
2014-02-13 22:40:08 +00:00
public void joinMuc(Conversation conversation) {
2014-05-22 13:36:41 +00:00
Account account = conversation.getAccount();
2014-07-18 19:57:10 +00:00
account.pendingConferenceJoins.remove(conversation);
account.pendingConferenceLeaves.remove(conversation);
2014-11-15 16:09:02 +00:00
if (account.getStatus() == Account.State.ONLINE) {
2014-12-02 23:05:41 +00:00
final String nick = conversation.getMucOptions().getProposedNick();
final Jid joinJid = conversation.getMucOptions().createJoinJid(nick);
if (joinJid == null) {
return; //safety net
}
2014-12-03 13:55:09 +00:00
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": joining conversation " + joinJid.toString());
2014-07-18 19:57:10 +00:00
PresencePacket packet = new PresencePacket();
2014-11-21 18:37:50 +00:00
packet.setFrom(conversation.getAccount().getJid());
packet.setTo(joinJid);
Element x = packet.addChild("x", "http://jabber.org/protocol/muc");
if (conversation.getMucOptions().getPassword() != null) {
x.addChild("password").setContent(conversation.getMucOptions().getPassword());
}
x.addChild("history").setAttribute("since", PresenceGenerator.getTimestamp(conversation.getLastMessageTransmitted()));
2014-07-18 19:57:10 +00:00
String sig = account.getPgpSignature();
if (sig != null) {
packet.addChild("status").setContent("online");
packet.addChild("x", "jabber:x:signed").setContent(sig);
}
sendPresencePacket(account, packet);
fetchConferenceConfiguration(conversation);
if (!joinJid.equals(conversation.getJid())) {
2014-07-21 20:57:41 +00:00
conversation.setContactJid(joinJid);
databaseBackend.updateConversation(conversation);
}
2015-01-23 23:22:51 +00:00
conversation.setHasMessagesLeftOnServer(false);
2014-07-18 19:57:10 +00:00
} else {
account.pendingConferenceJoins.add(conversation);
}
}
2014-03-11 14:44:22 +00:00
public void providePasswordForMuc(Conversation conversation, String password) {
if (conversation.getMode() == Conversation.MODE_MULTI) {
conversation.getMucOptions().setPassword(password);
if (conversation.getBookmark() != null) {
conversation.getBookmark().setAutojoin(true);
2014-09-07 12:06:23 +00:00
pushBookmarks(conversation.getAccount());
}
databaseBackend.updateConversation(conversation);
joinMuc(conversation);
}
}
2014-03-11 14:44:22 +00:00
public void renameInMuc(final Conversation conversation, final String nick, final UiCallback<Conversation> callback) {
2014-03-03 04:01:02 +00:00
final MucOptions options = conversation.getMucOptions();
final Jid joinJid = options.createJoinJid(nick);
2014-03-03 04:01:02 +00:00
if (options.online()) {
2014-06-03 09:04:17 +00:00
Account account = conversation.getAccount();
2014-03-03 04:01:02 +00:00
options.setOnRenameListener(new OnRenameListener() {
2014-03-11 14:44:22 +00:00
2014-03-03 04:01:02 +00:00
@Override
public void onSuccess() {
conversation.setContactJid(joinJid);
databaseBackend.updateConversation(conversation);
Bookmark bookmark = conversation.getBookmark();
if (bookmark != null) {
bookmark.setNick(nick);
pushBookmarks(bookmark.getAccount());
2014-03-04 03:09:15 +00:00
}
callback.success(conversation);
}
@Override
public void onFailure() {
2014-12-13 11:25:52 +00:00
callback.error(R.string.nick_in_use, conversation);
2014-03-03 04:01:02 +00:00
}
});
2014-03-03 04:01:02 +00:00
PresencePacket packet = new PresencePacket();
packet.setTo(joinJid);
packet.setFrom(conversation.getAccount().getJid());
2014-06-03 09:04:17 +00:00
String sig = account.getPgpSignature();
if (sig != null) {
packet.addChild("status").setContent("online");
packet.addChild("x", "jabber:x:signed").setContent(sig);
}
2014-08-26 14:52:42 +00:00
sendPresencePacket(account, packet);
2014-03-03 04:01:02 +00:00
} else {
conversation.setContactJid(joinJid);
2014-03-03 04:01:02 +00:00
databaseBackend.updateConversation(conversation);
2014-11-15 16:09:02 +00:00
if (conversation.getAccount().getStatus() == Account.State.ONLINE) {
2014-07-15 15:11:43 +00:00
Bookmark bookmark = conversation.getBookmark();
2014-08-26 14:52:42 +00:00
if (bookmark != null) {
2014-07-15 15:11:43 +00:00
bookmark.setNick(nick);
pushBookmarks(bookmark.getAccount());
}
2014-03-03 04:01:02 +00:00
joinMuc(conversation);
}
}
}
2014-02-13 22:40:08 +00:00
public void leaveMuc(Conversation conversation) {
2014-07-18 19:57:10 +00:00
Account account = conversation.getAccount();
account.pendingConferenceJoins.remove(conversation);
account.pendingConferenceLeaves.remove(conversation);
2014-11-15 16:09:02 +00:00
if (account.getStatus() == Account.State.ONLINE) {
2014-07-18 19:57:10 +00:00
PresencePacket packet = new PresencePacket();
packet.setTo(conversation.getJid());
packet.setFrom(conversation.getAccount().getJid());
2014-07-18 19:57:10 +00:00
packet.setAttribute("type", "unavailable");
2014-08-26 14:52:42 +00:00
sendPresencePacket(conversation.getAccount(), packet);
2014-07-18 19:57:10 +00:00
conversation.getMucOptions().setOffline();
conversation.deregisterWithBookmark();
Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid()
+ ": leaving muc " + conversation.getJid());
2014-07-18 19:57:10 +00:00
} else {
account.pendingConferenceLeaves.add(conversation);
}
2014-02-13 22:40:08 +00:00
}
private String findConferenceServer(final Account account) {
String server;
if (account.getXmppConnection() != null) {
server = account.getXmppConnection().getMucServer();
if (server != null) {
return server;
}
}
for (Account other : getAccounts()) {
if (other != account && other.getXmppConnection() != null) {
server = other.getXmppConnection().getMucServer();
if (server != null) {
return server;
}
}
}
return null;
}
public void createAdhocConference(final Account account, final Iterable<Jid> jids, final UiCallback<Conversation> callback) {
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": creating adhoc conference with " + jids.toString());
if (account.getStatus() == Account.State.ONLINE) {
try {
String server = findConferenceServer(account);
if (server == null) {
if (callback != null) {
callback.error(R.string.no_conference_server_found, null);
}
return;
}
String name = new BigInteger(75, getRNG()).toString(32);
Jid jid = Jid.fromParts(name, server, null);
final Conversation conversation = findOrCreateConversation(account, jid, true);
joinMuc(conversation);
Bundle options = new Bundle();
options.putString("muc#roomconfig_persistentroom", "1");
options.putString("muc#roomconfig_membersonly", "1");
options.putString("muc#roomconfig_publicroom", "0");
options.putString("muc#roomconfig_whois", "anyone");
pushConferenceConfiguration(conversation, options, new OnConferenceOptionsPushed() {
@Override
public void onPushSucceeded() {
for (Jid invite : jids) {
invite(conversation, invite);
}
if (account.countPresences() > 1) {
directInvite(conversation, account.getJid().toBareJid());
}
if (callback != null) {
callback.success(conversation);
}
}
@Override
public void onPushFailed() {
if (callback != null) {
callback.error(R.string.conference_creation_failed, conversation);
}
}
});
} catch (InvalidJidException e) {
if (callback != null) {
callback.error(R.string.conference_creation_failed, null);
}
}
} else {
if (callback != null) {
callback.error(R.string.not_connected_try_again, null);
}
}
}
public void fetchConferenceConfiguration(final Conversation conversation) {
IqPacket request = new IqPacket(IqPacket.TYPE.GET);
request.setTo(conversation.getJid().toBareJid());
request.query("http://jabber.org/protocol/disco#info");
sendIqPacket(conversation.getAccount(), request, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() != IqPacket.TYPE.ERROR) {
2015-01-08 13:45:44 +00:00
ArrayList<String> features = new ArrayList<>();
for (Element child : packet.query().getChildren()) {
if (child != null && child.getName().equals("feature")) {
String var = child.getAttribute("var");
if (var != null) {
features.add(var);
}
}
}
conversation.getMucOptions().updateFeatures(features);
2015-01-08 20:29:26 +00:00
updateConversationUi();
}
}
});
}
public void pushConferenceConfiguration(final Conversation conversation, final Bundle options, final OnConferenceOptionsPushed callback) {
2014-12-30 13:16:25 +00:00
IqPacket request = new IqPacket(IqPacket.TYPE.GET);
request.setTo(conversation.getJid().toBareJid());
request.query("http://jabber.org/protocol/muc#owner");
sendIqPacket(conversation.getAccount(), request, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
2014-12-30 13:16:25 +00:00
if (packet.getType() != IqPacket.TYPE.ERROR) {
Data data = Data.parse(packet.query().findChild("x", "jabber:x:data"));
for (Field field : data.getFields()) {
if (options.containsKey(field.getName())) {
field.setValue(options.getString(field.getName()));
}
}
data.submit();
2014-12-30 13:16:25 +00:00
IqPacket set = new IqPacket(IqPacket.TYPE.SET);
set.setTo(conversation.getJid().toBareJid());
set.query("http://jabber.org/protocol/muc#owner").addChild(data);
sendIqPacket(account, set, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
2014-12-30 13:16:25 +00:00
if (packet.getType() == IqPacket.TYPE.RESULT) {
if (callback != null) {
callback.onPushSucceeded();
}
} else {
if (callback != null) {
callback.onPushFailed();
}
}
}
});
} else {
if (callback != null) {
callback.onPushFailed();
}
}
}
});
}
public void pushSubjectToConference(final Conversation conference, final String subject) {
MessagePacket packet = this.getMessageGenerator().conferenceSubject(conference, subject);
this.sendMessagePacket(conference.getAccount(), packet);
final MucOptions mucOptions = conference.getMucOptions();
final MucOptions.User self = mucOptions.getSelf();
if (!mucOptions.persistent() && self.getAffiliation().ranks(MucOptions.Affiliation.OWNER)) {
Bundle options = new Bundle();
options.putString("muc#roomconfig_persistentroom", "1");
this.pushConferenceConfiguration(conference, options, null);
}
}
2015-01-07 14:03:29 +00:00
public void changeAffiliationInConference(final Conversation conference, Jid user, MucOptions.Affiliation affiliation, final OnAffiliationChanged callback) {
final Jid jid = user.toBareJid();
IqPacket request = this.mIqGenerator.changeAffiliation(conference, jid, affiliation.toString());
sendIqPacket(conference.getAccount(), request, new OnIqPacketReceived() {
2015-01-07 14:03:29 +00:00
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() == IqPacket.TYPE.RESULT) {
callback.onAffiliationChangedSuccessful(jid);
} else {
callback.onAffiliationChangeFailed(jid, R.string.could_not_change_affiliation);
2015-01-07 14:03:29 +00:00
}
}
});
}
public void changeAffiliationsInConference(final Conversation conference, MucOptions.Affiliation before, MucOptions.Affiliation after) {
List<Jid> jids = new ArrayList<>();
for (MucOptions.User user : conference.getMucOptions().getUsers()) {
if (user.getAffiliation() == before && user.getJid() != null) {
jids.add(user.getJid());
}
}
IqPacket request = this.mIqGenerator.changeAffiliation(conference, jids, after.toString());
sendIqPacket(conference.getAccount(), request, mDefaultIqHandler);
}
2015-01-08 00:23:53 +00:00
public void changeRoleInConference(final Conversation conference, final String nick, MucOptions.Role role, final OnRoleChanged callback) {
IqPacket request = this.mIqGenerator.changeRole(conference, nick, role.toString());
Log.d(Config.LOGTAG, request.toString());
2015-01-08 00:23:53 +00:00
sendIqPacket(conference.getAccount(), request, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
Log.d(Config.LOGTAG, packet.toString());
if (packet.getType() == IqPacket.TYPE.RESULT) {
callback.onRoleChangedSuccessful(nick);
} else {
callback.onRoleChangeFailed(nick, R.string.could_not_change_role);
}
}
});
}
public void disconnect(Account account, boolean force) {
2014-11-15 16:09:02 +00:00
if ((account.getStatus() == Account.State.ONLINE)
|| (account.getStatus() == Account.State.DISABLED)) {
if (!force) {
List<Conversation> conversations = getConversations();
for (Conversation conversation : conversations) {
if (conversation.getAccount() == account) {
if (conversation.getMode() == Conversation.MODE_MULTI) {
leaveMuc(conversation);
} else {
if (conversation.endOtrIfNeeded()) {
Log.d(Config.LOGTAG, account.getJid().toBareJid()
+ ": ended otr session with "
+ conversation.getJid());
}
}
}
}
sendOfflinePresence(account);
2014-02-13 22:40:08 +00:00
}
account.getXmppConnection().disconnect(force);
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
2014-02-16 15:32:15 +00:00
2014-02-27 23:22:56 +00:00
public void updateMessage(Message message) {
databaseBackend.updateMessage(message);
2014-10-14 16:16:03 +00:00
updateConversationUi();
2014-02-27 23:22:56 +00:00
}
2014-05-22 12:33:17 +00:00
protected void syncDirtyContacts(Account account) {
for (Contact contact : account.getRoster().getContacts()) {
2014-05-22 12:33:17 +00:00
if (contact.getOption(Contact.Options.DIRTY_PUSH)) {
pushContactToServer(contact);
}
if (contact.getOption(Contact.Options.DIRTY_DELETE)) {
deleteContactOnServer(contact);
}
}
}
2014-02-27 23:22:56 +00:00
public void createContact(Contact contact) {
SharedPreferences sharedPref = getPreferences();
boolean autoGrant = sharedPref.getBoolean("grant_new_contacts", true);
if (autoGrant) {
contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
contact.setOption(Contact.Options.ASKING);
}
pushContactToServer(contact);
2014-06-11 19:53:25 +00:00
}
public void onOtrSessionEstablished(Conversation conversation) {
final Account account = conversation.getAccount();
final Session otrSession = conversation.getOtrSession();
2014-08-31 14:28:21 +00:00
Log.d(Config.LOGTAG,
account.getJid().toBareJid() + " otr session established with "
+ conversation.getJid() + "/"
+ otrSession.getSessionID().getUserID());
conversation.findUnsentMessagesWithEncryption(Message.ENCRYPTION_OTR, new Conversation.OnMessageFound() {
@Override
public void onMessageFound(Message message) {
SessionID id = otrSession.getSessionID();
try {
message.setCounterpart(Jid.fromString(id.getAccountID() + "/" + id.getUserID()));
} catch (InvalidJidException e) {
return;
}
if (message.needsUploading()) {
mJingleConnectionManager.createNewConnection(message);
} else {
MessagePacket outPacket = mMessageGenerator.generateOtrChat(message, true);
if (outPacket != null) {
message.setStatus(Message.STATUS_SEND);
databaseBackend.updateMessage(message);
sendMessagePacket(account, outPacket);
}
}
updateConversationUi();
}
});
}
public boolean renewSymmetricKey(Conversation conversation) {
Account account = conversation.getAccount();
byte[] symmetricKey = new byte[32];
this.mRandom.nextBytes(symmetricKey);
Session otrSession = conversation.getOtrSession();
if (otrSession != null) {
MessagePacket packet = new MessagePacket();
packet.setType(MessagePacket.TYPE_CHAT);
packet.setFrom(account.getJid());
packet.addChild("private", "urn:xmpp:carbons:2");
packet.addChild("no-copy", "urn:xmpp:hints");
packet.setAttribute("to", otrSession.getSessionID().getAccountID() + "/"
+ otrSession.getSessionID().getUserID());
try {
packet.setBody(otrSession
.transformSending(CryptoHelper.FILETRANSFER
2015-01-20 17:01:39 +00:00
+ CryptoHelper.bytesToHex(symmetricKey))[0]);
2014-08-26 14:52:42 +00:00
sendMessagePacket(account, packet);
conversation.setSymmetricKey(symmetricKey);
return true;
} catch (OtrException e) {
return false;
}
}
return false;
}
2014-05-22 07:36:00 +00:00
public void pushContactToServer(final Contact contact) {
2014-05-22 12:33:17 +00:00
contact.resetOption(Contact.Options.DIRTY_DELETE);
contact.setOption(Contact.Options.DIRTY_PUSH);
final Account account = contact.getAccount();
2014-11-15 16:09:02 +00:00
if (account.getStatus() == Account.State.ONLINE) {
final boolean ask = contact.getOption(Contact.Options.ASKING);
final boolean sendUpdates = contact
.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)
&& contact.getOption(Contact.Options.PREEMPTIVE_GRANT);
2014-12-30 13:16:25 +00:00
final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
iq.query(Xmlns.ROSTER).addChild(contact.asElement());
account.getXmppConnection().sendIqPacket(iq, mDefaultIqHandler);
if (sendUpdates) {
2014-08-26 14:52:42 +00:00
sendPresencePacket(account,
mPresenceGenerator.sendPresenceUpdatesTo(contact));
2014-05-22 07:36:00 +00:00
}
if (ask) {
2014-08-26 14:52:42 +00:00
sendPresencePacket(account,
mPresenceGenerator.requestPresenceUpdatesFrom(contact));
}
2014-05-21 20:22:36 +00:00
}
}
2014-05-22 07:36:00 +00:00
public void publishAvatar(final Account account,
final Uri image,
final UiCallback<Avatar> callback) {
2014-08-31 14:28:21 +00:00
final Bitmap.CompressFormat format = Config.AVATAR_FORMAT;
final int size = Config.AVATAR_SIZE;
2014-08-26 14:52:42 +00:00
final Avatar avatar = getFileBackend()
.getPepAvatar(image, size, format);
2014-08-26 14:52:42 +00:00
if (avatar != null) {
2014-08-04 23:36:17 +00:00
avatar.height = size;
avatar.width = size;
if (format.equals(Bitmap.CompressFormat.WEBP)) {
avatar.type = "image/webp";
} else if (format.equals(Bitmap.CompressFormat.JPEG)) {
avatar.type = "image/jpeg";
} else if (format.equals(Bitmap.CompressFormat.PNG)) {
avatar.type = "image/png";
}
if (!getFileBackend().save(avatar)) {
callback.error(R.string.error_saving_avatar, avatar);
return;
}
final IqPacket packet = this.mIqGenerator.publishAvatar(avatar);
2014-08-04 23:36:17 +00:00
this.sendIqPacket(account, packet, new OnIqPacketReceived() {
2014-08-26 14:52:42 +00:00
2014-08-04 23:36:17 +00:00
@Override
public void onIqPacketReceived(Account account, IqPacket result) {
2014-12-30 13:16:25 +00:00
if (result.getType() == IqPacket.TYPE.RESULT) {
final IqPacket packet = XmppConnectionService.this.mIqGenerator
.publishAvatarMetadata(avatar);
2014-08-04 23:36:17 +00:00
sendIqPacket(account, packet, new OnIqPacketReceived() {
2014-08-26 14:52:42 +00:00
2014-08-04 23:36:17 +00:00
@Override
2014-08-26 14:52:42 +00:00
public void onIqPacketReceived(Account account,
IqPacket result) {
2014-12-30 13:16:25 +00:00
if (result.getType() == IqPacket.TYPE.RESULT) {
if (account.setAvatar(avatar.getFilename())) {
getAvatarService().clear(account);
databaseBackend.updateAccount(account);
}
2014-08-04 23:36:17 +00:00
callback.success(avatar);
} else {
2014-08-26 14:52:42 +00:00
callback.error(
R.string.error_publish_avatar_server_reject,
avatar);
2014-08-04 23:36:17 +00:00
}
}
});
} else {
2014-08-26 14:52:42 +00:00
callback.error(
R.string.error_publish_avatar_server_reject,
avatar);
2014-08-04 23:36:17 +00:00
}
}
});
} else {
2014-08-05 10:21:08 +00:00
callback.error(R.string.error_publish_avatar_converting, null);
}
}
2014-08-26 14:52:42 +00:00
public void fetchAvatar(Account account, Avatar avatar) {
fetchAvatar(account, avatar, null);
}
2014-08-26 14:52:42 +00:00
private static String generateFetchKey(Account account, final Avatar avatar) {
return account.getJid().toBareJid()+"_"+avatar.owner+"_"+avatar.sha1sum;
}
public void fetchAvatar(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
final String KEY = generateFetchKey(account, avatar);
synchronized(this.mInProgressAvatarFetches) {
if (this.mInProgressAvatarFetches.contains(KEY)) {
return;
} else {
switch (avatar.origin) {
case PEP:
this.mInProgressAvatarFetches.add(KEY);
fetchAvatarPep(account, avatar, callback);
break;
case VCARD:
this.mInProgressAvatarFetches.add(KEY);
fetchAvatarVcard(account, avatar, callback);
break;
}
}
}
}
private void fetchAvatarPep(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
IqPacket packet = this.mIqGenerator.retrievePepAvatar(avatar);
2014-08-05 20:58:46 +00:00
sendIqPacket(account, packet, new OnIqPacketReceived() {
2014-08-26 14:52:42 +00:00
2014-08-05 20:58:46 +00:00
@Override
public void onIqPacketReceived(Account account, IqPacket result) {
synchronized (mInProgressAvatarFetches) {
mInProgressAvatarFetches.remove(generateFetchKey(account, avatar));
}
final String ERROR = account.getJid().toBareJid()
+ ": fetching avatar for " + avatar.owner + " failed ";
2014-12-30 13:16:25 +00:00
if (result.getType() == IqPacket.TYPE.RESULT) {
2014-09-05 11:29:20 +00:00
avatar.image = mIqParser.avatarData(result);
if (avatar.image != null) {
if (getFileBackend().save(avatar)) {
if (account.getJid().toBareJid().equals(avatar.owner)) {
2014-09-05 11:29:20 +00:00
if (account.setAvatar(avatar.getFilename())) {
databaseBackend.updateAccount(account);
}
2014-10-21 12:57:16 +00:00
getAvatarService().clear(account);
2014-10-21 13:26:17 +00:00
updateConversationUi();
updateAccountUi();
2014-09-05 11:29:20 +00:00
} else {
Contact contact = account.getRoster()
.getContact(avatar.owner);
contact.setAvatar(avatar);
2014-10-21 12:57:16 +00:00
getAvatarService().clear(contact);
2014-10-21 13:26:17 +00:00
updateConversationUi();
updateRosterUi();
}
2014-09-05 11:29:20 +00:00
if (callback != null) {
callback.success(avatar);
}
Log.d(Config.LOGTAG, account.getJid().toBareJid()
+ ": succesfuly fetched pep avatar for " + avatar.owner);
2014-09-05 11:29:20 +00:00
return;
}
2014-09-05 11:29:20 +00:00
} else {
2014-09-05 11:29:20 +00:00
Log.d(Config.LOGTAG, ERROR + "(parsing error)");
}
} else {
Element error = result.findChild("error");
if (error == null) {
2014-09-05 11:29:20 +00:00
Log.d(Config.LOGTAG, ERROR + "(server error)");
} else {
Log.d(Config.LOGTAG, ERROR + error.toString());
}
}
2014-08-26 14:52:42 +00:00
if (callback != null) {
callback.error(0, null);
}
2014-09-05 11:29:20 +00:00
}
});
}
2014-08-26 14:52:42 +00:00
private void fetchAvatarVcard(final Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
IqPacket packet = this.mIqGenerator.retrieveVcardAvatar(avatar);
this.sendIqPacket(account, packet, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
synchronized (mInProgressAvatarFetches) {
mInProgressAvatarFetches.remove(generateFetchKey(account, avatar));
}
if (packet.getType() == IqPacket.TYPE.RESULT) {
Element vCard = packet.findChild("vCard", "vcard-temp");
Element photo = vCard != null ? vCard.findChild("PHOTO") : null;
String image = photo != null ? photo.findChildContent("BINVAL") : null;
if (image != null) {
avatar.image = image;
if (getFileBackend().save(avatar)) {
Log.d(Config.LOGTAG, account.getJid().toBareJid()
+ ": successfully fetched vCard avatar for " + avatar.owner);
Contact contact = account.getRoster()
.getContact(avatar.owner);
contact.setAvatar(avatar);
getAvatarService().clear(contact);
updateConversationUi();
updateRosterUi();
}
}
}
}
});
}
public void checkForAvatar(Account account, final UiCallback<Avatar> callback) {
IqPacket packet = this.mIqGenerator.retrieveAvatarMetaData(null);
this.sendIqPacket(account, packet, new OnIqPacketReceived() {
2014-08-26 14:52:42 +00:00
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
2014-12-30 13:16:25 +00:00
if (packet.getType() == IqPacket.TYPE.RESULT) {
2014-08-26 14:52:42 +00:00
Element pubsub = packet.findChild("pubsub",
"http://jabber.org/protocol/pubsub");
if (pubsub != null) {
Element items = pubsub.findChild("items");
2014-08-26 14:52:42 +00:00
if (items != null) {
Avatar avatar = Avatar.parseMetadata(items);
2014-08-26 14:52:42 +00:00
if (avatar != null) {
avatar.owner = account.getJid().toBareJid();
if (fileBackend.isAvatarCached(avatar)) {
if (account.setAvatar(avatar.getFilename())) {
databaseBackend.updateAccount(account);
}
2014-10-21 12:57:16 +00:00
getAvatarService().clear(account);
callback.success(avatar);
} else {
fetchAvatarPep(account, avatar, callback);
}
return;
}
}
}
2014-08-05 20:58:46 +00:00
}
callback.error(0, null);
2014-08-05 20:58:46 +00:00
}
});
}
2014-08-26 14:52:42 +00:00
public void deleteContactOnServer(Contact contact) {
contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
2014-05-22 12:33:17 +00:00
contact.resetOption(Contact.Options.DIRTY_PUSH);
2014-07-09 23:55:19 +00:00
contact.setOption(Contact.Options.DIRTY_DELETE);
Account account = contact.getAccount();
2014-11-15 16:09:02 +00:00
if (account.getStatus() == Account.State.ONLINE) {
2014-12-30 13:16:25 +00:00
IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
Element item = iq.query(Xmlns.ROSTER).addChild("item");
item.setAttribute("jid", contact.getJid().toString());
2014-05-22 07:36:00 +00:00
item.setAttribute("subscription", "remove");
account.getXmppConnection().sendIqPacket(iq, mDefaultIqHandler);
2014-05-22 07:36:00 +00:00
}
}
2014-02-21 20:35:23 +00:00
2014-03-03 04:01:02 +00:00
public void updateConversation(Conversation conversation) {
this.databaseBackend.updateConversation(conversation);
}
2014-03-05 14:41:14 +00:00
public void reconnectAccount(final Account account, final boolean force) {
2015-03-05 14:46:33 +00:00
synchronized (account) {
if (account.getXmppConnection() != null) {
disconnect(account, force);
}
if (!account.isOptionSet(Account.OPTION_DISABLED)) {
synchronized (this.mInProgressAvatarFetches) {
for(Iterator<String> iterator = this.mInProgressAvatarFetches.iterator(); iterator.hasNext();) {
final String KEY = iterator.next();
if (KEY.startsWith(account.getJid().toBareJid()+"_")) {
iterator.remove();
}
}
}
2015-03-05 14:46:33 +00:00
if (account.getXmppConnection() == null) {
account.setXmppConnection(createConnection(account));
}
Thread thread = new Thread(account.getXmppConnection());
thread.start();
scheduleWakeUpCall(Config.CONNECT_TIMEOUT, account.getUuid().hashCode());
} else {
account.getRoster().clearPresences();
account.setXmppConnection(null);
}
}
}
2014-03-11 14:44:22 +00:00
2015-03-05 14:46:33 +00:00
public void reconnectAccountInBackground(final Account account) {
new Thread(new Runnable() {
2014-03-10 18:22:13 +00:00
@Override
public void run() {
2015-03-05 14:46:33 +00:00
reconnectAccount(account,false);
}
}).start();
}
2014-03-14 21:40:56 +00:00
public void invite(Conversation conversation, Jid contact) {
Log.d(Config.LOGTAG,conversation.getAccount().getJid().toBareJid()+": inviting "+contact+" to "+conversation.getJid().toBareJid());
2014-07-21 14:04:53 +00:00
MessagePacket packet = mMessageGenerator.invite(conversation, contact);
2014-08-26 14:52:42 +00:00
sendMessagePacket(conversation.getAccount(), packet);
2014-03-15 03:59:18 +00:00
}
public void directInvite(Conversation conversation, Jid jid) {
MessagePacket packet = mMessageGenerator.directInvite(conversation, jid);
sendMessagePacket(conversation.getAccount(),packet);
}
2014-08-27 17:25:58 +00:00
public void resetSendingToWaiting(Account account) {
for (Conversation conversation : getConversations()) {
2014-08-27 17:25:58 +00:00
if (conversation.getAccount() == account) {
conversation.findUnsentTextMessages(new Conversation.OnMessageFound() {
@Override
public void onMessageFound(Message message) {
2014-08-27 17:25:58 +00:00
markMessage(message, Message.STATUS_WAITING);
}
});
2014-08-27 17:25:58 +00:00
}
}
2014-03-15 03:59:18 +00:00
}
2014-05-01 20:33:49 +00:00
public Message markMessage(final Account account, final Jid recipient, final String uuid, final int status) {
2014-10-02 16:54:21 +00:00
if (uuid == null) {
return null;
}
for (Conversation conversation : getConversations()) {
if (conversation.getJid().toBareJid().equals(recipient) && conversation.getAccount() == account) {
final Message message = conversation.findSentMessageWithUuid(uuid);
if (message != null) {
markMessage(message, status);
}
return message;
2014-05-16 20:46:15 +00:00
}
}
return null;
2014-05-16 20:46:15 +00:00
}
2014-05-18 09:25:04 +00:00
public boolean markMessage(Conversation conversation, String uuid,
int status) {
2014-10-02 16:54:21 +00:00
if (uuid == null) {
return false;
} else {
Message message = conversation.findSentMessageWithUuid(uuid);
if (message != null) {
markMessage(message, status);
return true;
} else {
return false;
2014-04-11 07:13:56 +00:00
}
}
}
2014-05-01 20:33:49 +00:00
2014-04-11 07:13:56 +00:00
public void markMessage(Message message, int status) {
if (status == Message.STATUS_SEND_FAILED
&& (message.getStatus() == Message.STATUS_SEND_RECEIVED || message
.getStatus() == Message.STATUS_SEND_DISPLAYED)) {
return;
}
2014-04-11 07:13:56 +00:00
message.setStatus(status);
databaseBackend.updateMessage(message);
2014-07-12 11:42:17 +00:00
updateConversationUi();
2014-04-11 07:13:56 +00:00
}
2014-05-01 20:33:49 +00:00
public SharedPreferences getPreferences() {
2014-05-01 20:33:49 +00:00
return PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
}
public boolean forceEncryption() {
return getPreferences().getBoolean("force_encryption", false);
}
public boolean confirmMessages() {
return getPreferences().getBoolean("confirm_messages", true);
}
public boolean sendChatStates() {
return getPreferences().getBoolean("chat_states", false);
}
public boolean saveEncryptedMessages() {
return !getPreferences().getBoolean("dont_save_encrypted", false);
}
2014-05-14 10:56:34 +00:00
public boolean indicateReceived() {
return getPreferences().getBoolean("indicate_received", false);
}
public int unreadCount() {
int count = 0;
for(Conversation conversation : getConversations()) {
count += conversation.unreadCount();
}
return count;
}
public void showErrorToastInUi(int resId) {
if (mOnShowErrorToast != null) {
mOnShowErrorToast.onShowErrorToast(resId);
}
}
2014-07-12 11:42:17 +00:00
public void updateConversationUi() {
if (mOnConversationUpdate != null) {
mOnConversationUpdate.onConversationUpdate();
}
}
2014-08-26 14:52:42 +00:00
2014-07-12 11:42:17 +00:00
public void updateAccountUi() {
if (mOnAccountUpdate != null) {
mOnAccountUpdate.onAccountUpdate();
}
}
2014-08-26 14:52:42 +00:00
2014-07-18 13:35:31 +00:00
public void updateRosterUi() {
if (mOnRosterUpdate != null) {
mOnRosterUpdate.onRosterUpdate();
}
}
2014-05-22 07:36:00 +00:00
public void updateBlocklistUi(final OnUpdateBlocklist.Status status) {
if (mOnUpdateBlocklist != null) {
mOnUpdateBlocklist.OnUpdateBlocklist(status);
}
}
public void updateMucRosterUi() {
if (mOnMucRosterUpdate != null) {
mOnMucRosterUpdate.onMucRosterUpdate();
}
}
public Account findAccountByJid(final Jid accountJid) {
for (Account account : this.accounts) {
if (account.getJid().toBareJid().equals(accountJid.toBareJid())) {
return account;
}
}
return null;
}
2014-08-26 14:52:42 +00:00
public Conversation findConversationByUuid(String uuid) {
for (Conversation conversation : getConversations()) {
if (conversation.getUuid().equals(uuid)) {
return conversation;
}
}
return null;
}
public void markRead(final Conversation conversation) {
2014-09-28 13:21:56 +00:00
mNotificationService.clear(conversation);
2014-10-07 14:02:52 +00:00
conversation.markRead();
updateUnreadCountBadge();
}
public synchronized void updateUnreadCountBadge() {
int count = unreadCount();
if (unreadCount != count) {
Log.d(Config.LOGTAG, "update unread count to " + count);
if (count > 0) {
ShortcutBadger.with(getApplicationContext()).count(count);
} else {
ShortcutBadger.with(getApplicationContext()).remove();
}
unreadCount = count;
}
}
public void sendReadMarker(final Conversation conversation) {
final Message markable = conversation.getLatestMarkableMessage();
this.markRead(conversation);
if (confirmMessages() && markable != null && markable.getRemoteMsgId() != null) {
Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + ": sending read marker to " + markable.getCounterpart().toString());
Account account = conversation.getAccount();
2014-11-19 16:40:42 +00:00
final Jid to = markable.getCounterpart();
MessagePacket packet = mMessageGenerator.confirm(account, to, markable.getRemoteMsgId());
this.sendMessagePacket(conversation.getAccount(), packet);
}
updateConversationUi();
}
2014-08-26 14:52:42 +00:00
public SecureRandom getRNG() {
return this.mRandom;
}
2014-08-26 14:52:42 +00:00
2014-07-22 15:27:44 +00:00
public MemorizingTrustManager getMemorizingTrustManager() {
return this.mMemorizingTrustManager;
}
public void setMemorizingTrustManager(MemorizingTrustManager trustManager) {
this.mMemorizingTrustManager = trustManager;
}
public void updateMemorizingTrustmanager() {
final MemorizingTrustManager tm;
final boolean dontTrustSystemCAs = getPreferences().getBoolean("dont_trust_system_cas", false);
if (dontTrustSystemCAs) {
tm = new MemorizingTrustManager(getApplicationContext(), null);
} else {
tm = new MemorizingTrustManager(getApplicationContext());
}
setMemorizingTrustManager(tm);
}
public PowerManager getPowerManager() {
return this.pm;
}
2014-10-21 12:57:16 +00:00
public LruCache<String, Bitmap> getBitmapCache() {
return this.mBitmapCache;
}
2014-06-30 08:46:46 +00:00
public void syncRosterToDisk(final Account account) {
Runnable runnable = new Runnable() {
2014-06-30 08:46:46 +00:00
@Override
public void run() {
databaseBackend.writeRoster(account.getRoster());
}
};
mDatabaseExecutor.execute(runnable);
2014-06-30 08:46:46 +00:00
}
2014-07-10 17:42:37 +00:00
public List<String> getKnownHosts() {
final List<String> hosts = new ArrayList<>();
for (final Account account : getAccounts()) {
if (!hosts.contains(account.getServer().toString())) {
hosts.add(account.getServer().toString());
2014-07-10 17:42:37 +00:00
}
for (final Contact contact : account.getRoster().getContacts()) {
2014-07-10 17:42:37 +00:00
if (contact.showInRoster()) {
final String server = contact.getServer().toString();
if (server != null && !hosts.contains(server)) {
2014-07-10 17:42:37 +00:00
hosts.add(server);
}
}
}
}
return hosts;
}
2014-07-11 17:48:41 +00:00
public List<String> getKnownConferenceHosts() {
final ArrayList<String> mucServers = new ArrayList<>();
for (final Account account : accounts) {
2014-07-11 17:48:41 +00:00
if (account.getXmppConnection() != null) {
final String server = account.getXmppConnection().getMucServer();
if (server != null && !mucServers.contains(server)) {
2014-07-11 17:48:41 +00:00
mucServers.add(server);
}
}
}
return mucServers;
}
2014-08-26 14:52:42 +00:00
public void sendMessagePacket(Account account, MessagePacket packet) {
2014-10-08 12:10:37 +00:00
XmppConnection connection = account.getXmppConnection();
if (connection != null) {
connection.sendMessagePacket(packet);
}
}
2014-08-26 14:52:42 +00:00
public void sendPresencePacket(Account account, PresencePacket packet) {
2014-10-08 12:10:37 +00:00
XmppConnection connection = account.getXmppConnection();
if (connection != null) {
connection.sendPresencePacket(packet);
}
}
2014-08-26 14:52:42 +00:00
public void sendIqPacket(final Account account, final IqPacket packet, final OnIqPacketReceived callback) {
final XmppConnection connection = account.getXmppConnection();
2014-10-08 12:10:37 +00:00
if (connection != null) {
connection.sendIqPacket(packet, callback);
}
2014-07-14 09:47:42 +00:00
}
2014-08-26 14:52:42 +00:00
public void sendPresence(final Account account) {
sendPresencePacket(account, mPresenceGenerator.sendPresence(account));
}
public void sendOfflinePresence(final Account account) {
sendPresencePacket(account, mPresenceGenerator.sendOfflinePresence(account));
}
public MessageGenerator getMessageGenerator() {
return this.mMessageGenerator;
}
2014-08-26 14:52:42 +00:00
public PresenceGenerator getPresenceGenerator() {
return this.mPresenceGenerator;
}
2014-08-26 14:52:42 +00:00
public IqGenerator getIqGenerator() {
2014-08-26 14:52:42 +00:00
return this.mIqGenerator;
}
2014-08-26 14:52:42 +00:00
public IqParser getIqParser() {
return this.mIqParser;
}
public JingleConnectionManager getJingleConnectionManager() {
return this.mJingleConnectionManager;
}
2014-08-26 14:52:42 +00:00
2014-12-05 00:54:16 +00:00
public MessageArchiveService getMessageArchiveService() {
return this.mMessageArchiveService;
}
public List<Contact> findContacts(Jid jid) {
ArrayList<Contact> contacts = new ArrayList<>();
for (Account account : getAccounts()) {
if (!account.isOptionSet(Account.OPTION_DISABLED)) {
2014-10-15 12:41:27 +00:00
Contact contact = account.getRoster().getContactFromRoster(jid);
if (contact != null) {
contacts.add(contact);
}
}
}
return contacts;
}
2014-09-29 16:28:13 +00:00
public NotificationService getNotificationService() {
return this.mNotificationService;
2014-09-28 13:21:56 +00:00
}
2014-10-13 23:06:45 +00:00
public HttpConnectionManager getHttpConnectionManager() {
return this.mHttpConnectionManager;
}
public void resendFailedMessages(final Message message) {
final Collection<Message> messages = new ArrayList<>();
Message current = message;
while (current.getStatus() == Message.STATUS_SEND_FAILED) {
messages.add(current);
if (current.mergeable(current.next())) {
current = current.next();
} else {
break;
}
}
for (final Message msg : messages) {
markMessage(msg, Message.STATUS_WAITING);
this.resendMessage(msg);
}
}
2014-12-15 16:14:27 +00:00
public void clearConversationHistory(final Conversation conversation) {
conversation.clearMessages();
conversation.setHasMessagesLeftOnServer(false); //avoid messages getting loaded through mam
2014-12-15 16:14:27 +00:00
new Thread(new Runnable() {
@Override
public void run() {
databaseBackend.deleteMessagesInConversation(conversation);
}
}).start();
}
public void sendBlockRequest(final Blockable blockable) {
if (blockable != null && blockable.getBlockedJid() != null) {
final Jid jid = blockable.getBlockedJid();
this.sendIqPacket(blockable.getAccount(), getIqGenerator().generateSetBlockRequest(jid), new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(final Account account, final IqPacket packet) {
2014-12-30 13:16:25 +00:00
if (packet.getType() == IqPacket.TYPE.RESULT) {
account.getBlocklist().add(jid);
updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
}
}
});
}
}
public void sendUnblockRequest(final Blockable blockable) {
if (blockable != null && blockable.getJid() != null) {
final Jid jid = blockable.getBlockedJid();
this.sendIqPacket(blockable.getAccount(), getIqGenerator().generateSetUnblockRequest(jid), new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(final Account account, final IqPacket packet) {
2014-12-30 13:16:25 +00:00
if (packet.getType() == IqPacket.TYPE.RESULT) {
account.getBlocklist().remove(jid);
updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
}
}
});
}
}
public interface OnMoreMessagesLoaded {
public void onMoreMessagesLoaded(int count, Conversation conversation);
public void informUser(int r);
}
public interface OnAccountPasswordChanged {
public void onPasswordChangeSucceeded();
public void onPasswordChangeFailed();
}
public interface OnAffiliationChanged {
public void onAffiliationChangedSuccessful(Jid jid);
public void onAffiliationChangeFailed(Jid jid, int resId);
}
public interface OnRoleChanged {
public void onRoleChangedSuccessful(String nick);
public void onRoleChangeFailed(String nick, int resid);
}
public interface OnConversationUpdate {
public void onConversationUpdate();
}
public interface OnAccountUpdate {
public void onAccountUpdate();
}
public interface OnRosterUpdate {
public void onRosterUpdate();
}
public interface OnMucRosterUpdate {
public void onMucRosterUpdate();
}
public interface OnConferenceOptionsPushed {
public void onPushSucceeded();
public void onPushFailed();
}
public interface OnShowErrorToast {
void onShowErrorToast(int resId);
}
public class XmppConnectionBinder extends Binder {
public XmppConnectionService getService() {
return XmppConnectionService.this;
}
}
2014-04-07 21:58:59 +00:00
}