moved most of the message/presence generation into seperate classes

This commit is contained in:
iNPUTmice 2014-07-12 03:44:23 +02:00
parent 789383e5cb
commit 99935dd630
9 changed files with 150 additions and 135 deletions

View file

@ -4,8 +4,6 @@ import java.security.interfaces.DSAPublicKey;
import java.util.ArrayList;
import java.util.List;
import eu.siacs.conversations.services.XmppConnectionService;
import net.java.otr4j.OtrException;
import net.java.otr4j.crypto.OtrCryptoEngineImpl;
import net.java.otr4j.crypto.OtrCryptoException;
@ -118,13 +116,10 @@ public class Conversation extends AbstractEntity {
}
}
public void markRead(XmppConnectionService service) {
markRead();
if (service.confirmMessages() && this.latestMarkableMessageId != null) {
service.sendConfirmMessage(getAccount(), getContactJid(),
this.latestMarkableMessageId);
public String popLatestMarkableMessageId() {
String id = this.latestMarkableMessageId;
this.latestMarkableMessageId = null;
}
return id;
}
public Message getLatestMessage() {

View file

@ -42,7 +42,7 @@ public class MessageGenerator {
delay.setAttribute("stamp", mDateFormat.format(date));
}
public MessagePacket generateOtrChat(Message message) throws OtrException {
public MessagePacket generateOtrChat(Message message) {
return generateOtrChat(message, false);
}
@ -106,4 +106,26 @@ public class MessageGenerator {
packet.setType(MessagePacket.TYPE_ERROR);
return packet;
}
public MessagePacket confirm(Account account, String to, String id) {
MessagePacket packet = new MessagePacket();
packet.setType(MessagePacket.TYPE_NORMAL);
packet.setTo(to);
packet.setFrom(account.getFullJid());
Element received = packet.addChild("displayed",
"urn:xmpp:chat-markers:0");
received.setAttribute("id", id);
return packet;
}
public MessagePacket conversationSubject(Conversation conversation,String subject) {
MessagePacket packet = new MessagePacket();
packet.setType(MessagePacket.TYPE_GROUPCHAT);
packet.setTo(conversation.getContactJid().split("/")[0]);
Element subjectChild = new Element("subject");
subjectChild.setContent(subject);
packet.addChild(subjectChild);
packet.setFrom(conversation.getAccount().getJid());
return packet;
}
}

View file

@ -0,0 +1,51 @@
package eu.siacs.conversations.generator;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
public class PresenceGenerator {
public PresencePacket requestPresenceUpdatesFrom(Contact contact) {
PresencePacket packet = new PresencePacket();
packet.setAttribute("type", "subscribe");
packet.setAttribute("to", contact.getJid());
packet.setAttribute("from", contact.getAccount().getJid());
return packet;
}
public PresencePacket stopPresenceUpdatesFrom(Contact contact) {
PresencePacket packet = new PresencePacket();
packet.setAttribute("type", "unsubscribe");
packet.setAttribute("to", contact.getJid());
packet.setAttribute("from", contact.getAccount().getJid());
return packet;
}
public PresencePacket stopPresenceUpdatesTo(Contact contact) {
PresencePacket packet = new PresencePacket();
packet.setAttribute("type", "unsubscribed");
packet.setAttribute("to", contact.getJid());
packet.setAttribute("from", contact.getAccount().getJid());
return packet;
}
public PresencePacket sendPresenceUpdatesTo(Contact contact) {
PresencePacket packet = new PresencePacket();
packet.setAttribute("type", "subscribed");
packet.setAttribute("to", contact.getJid());
packet.setAttribute("from", contact.getAccount().getJid());
return packet;
}
public PresencePacket sendPresence(Account account) {
PresencePacket packet = new PresencePacket();
packet.setAttribute("from", account.getFullJid());
String sig = account.getPgpSignature();
if (sig != null) {
packet.addChild("status").setContent("online");
packet.addChild("x", "jabber:x:signed").setContent(sig);
}
return packet;
}
}

View file

@ -5,6 +5,7 @@ import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.generator.PresenceGenerator;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnPresencePacketReceived;
@ -13,8 +14,11 @@ import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
public class PresenceParser extends AbstractParser implements
OnPresencePacketReceived {
private PresenceGenerator mPresenceGenerator;
public PresenceParser(XmppConnectionService service) {
super(service);
mPresenceGenerator = service.getPresenceGenerator();
}
public void parseConferencePresence(PresencePacket packet, Account account) {
@ -91,11 +95,10 @@ public class PresenceParser extends AbstractParser implements
.onContactStatusChanged(contact, false);
} else if (type.equals("subscribe")) {
if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
mXmppConnectionService.sendPresenceUpdatesTo(contact);
mXmppConnectionService.sendPresencePacket(account, mPresenceGenerator.stopPresenceUpdatesTo(contact));
if ((contact.getOption(Contact.Options.ASKING))
&& (!contact.getOption(Contact.Options.TO))) {
mXmppConnectionService
.requestPresenceUpdatesFrom(contact);
mXmppConnectionService.sendPresencePacket(account,mPresenceGenerator.requestPresenceUpdatesFrom(contact));
}
} else {
contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);

View file

@ -26,6 +26,7 @@ import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.generator.MessageGenerator;
import eu.siacs.conversations.generator.PresenceGenerator;
import eu.siacs.conversations.parser.MessageParser;
import eu.siacs.conversations.parser.PresenceParser;
import eu.siacs.conversations.persistance.DatabaseBackend;
@ -92,6 +93,7 @@ public class XmppConnectionService extends Service {
private MessageParser mMessageParser = new MessageParser(this);
private PresenceParser mPresenceParser = new PresenceParser(this);
private MessageGenerator mMessageGenerator = new MessageGenerator();
private PresenceGenerator mPresenceGenerator = new PresenceGenerator();
private List<Account> accounts;
private List<Conversation> conversations = null;
@ -542,7 +544,7 @@ public class XmppConnectionService extends Service {
account.getRoster().clearPresences();
account.clearPresences(); // self presences
fetchRosterFromServer(account);
sendPresence(account);
sendPresencePacket(account, mPresenceGenerator.sendPresence(account));
connectMultiModeConversations(account);
if (convChangedListener != null) {
convChangedListener.onConversationListChanged();
@ -591,14 +593,10 @@ public class XmppConnectionService extends Service {
&& conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
message.setPresence(conv.getOtrSession().getSessionID()
.getUserID());
try {
packet = mMessageGenerator.generateOtrChat(message);
send = true;
message.setStatus(Message.STATUS_SEND);
} catch (OtrException e) {
Log.e(LOGTAG, "error generating otr packet");
packet = null;
}
} else if (message.getPresence() == null) {
message.setStatus(Message.STATUS_WAITING);
}
@ -647,7 +645,7 @@ public class XmppConnectionService extends Service {
convChangedListener.onConversationListChanged();
}
if ((send) && (packet != null)) {
account.getXmppConnection().sendMessagePacket(packet);
sendMessagePacket(account, packet);
}
}
@ -715,7 +713,7 @@ public class XmppConnectionService extends Service {
}
}
if (packet != null) {
account.getXmppConnection().sendMessagePacket(packet);
sendMessagePacket(account,packet);
markMessage(message, Message.STATUS_SEND);
}
}
@ -996,7 +994,7 @@ public class XmppConnectionService extends Service {
mDateFormat.format(date));
}
packet.addChild(x);
account.getXmppConnection().sendPresencePacket(packet);
sendPresencePacket(account, packet);
}
private OnRenameListener renameListener = null;
@ -1035,8 +1033,7 @@ public class XmppConnectionService extends Service {
packet.addChild("status").setContent("online");
packet.addChild("x", "jabber:x:signed").setContent(sig);
}
account.getXmppConnection().sendPresencePacket(packet, null);
sendPresencePacket(account,packet);
} else {
String jid = conversation.getContactJid().split("/")[0] + "/"
+ nick;
@ -1055,8 +1052,7 @@ public class XmppConnectionService extends Service {
packet.setAttribute("from", conversation.getAccount().getFullJid());
packet.setAttribute("type", "unavailable");
Log.d(LOGTAG, "send leaving muc " + packet);
conversation.getAccount().getXmppConnection()
.sendPresencePacket(packet);
sendPresencePacket(conversation.getAccount(),packet);
conversation.getMucOptions().setOffline();
}
@ -1129,8 +1125,7 @@ public class XmppConnectionService extends Service {
if (outPacket != null) {
msg.setStatus(Message.STATUS_SEND);
databaseBackend.updateMessage(msg);
account.getXmppConnection()
.sendMessagePacket(outPacket);
sendMessagePacket(account,outPacket);
}
} else if (msg.getType() == Message.TYPE_IMAGE) {
mJingleConnectionManager.createNewConnection(msg);
@ -1157,7 +1152,7 @@ public class XmppConnectionService extends Service {
packet.setBody(otrSession
.transformSending(CryptoHelper.FILETRANSFER
+ CryptoHelper.bytesToHex(symmetricKey)));
account.getXmppConnection().sendMessagePacket(packet);
sendMessagePacket(account,packet);
conversation.setSymmetricKey(symmetricKey);
return true;
} catch (OtrException e) {
@ -1176,12 +1171,12 @@ public class XmppConnectionService extends Service {
iq.query("jabber:iq:roster").addChild(contact.asElement());
account.getXmppConnection().sendIqPacket(iq, null);
if (contact.getOption(Contact.Options.ASKING)) {
requestPresenceUpdatesFrom(contact);
sendPresencePacket(account, mPresenceGenerator.requestPresenceUpdatesFrom(contact));
}
if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)
&& contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
Log.d("xmppService", "contact had pending subscription");
sendPresenceUpdatesTo(contact);
sendPresencePacket(account, mPresenceGenerator.sendPresenceUpdatesTo(contact));
}
}
}
@ -1200,49 +1195,6 @@ public class XmppConnectionService extends Service {
}
}
public void requestPresenceUpdatesFrom(Contact contact) {
PresencePacket packet = new PresencePacket();
packet.setAttribute("type", "subscribe");
packet.setAttribute("to", contact.getJid());
packet.setAttribute("from", contact.getAccount().getJid());
contact.getAccount().getXmppConnection().sendPresencePacket(packet);
}
public void stopPresenceUpdatesFrom(Contact contact) {
PresencePacket packet = new PresencePacket();
packet.setAttribute("type", "unsubscribe");
packet.setAttribute("to", contact.getJid());
packet.setAttribute("from", contact.getAccount().getJid());
contact.getAccount().getXmppConnection().sendPresencePacket(packet);
}
public void stopPresenceUpdatesTo(Contact contact) {
PresencePacket packet = new PresencePacket();
packet.setAttribute("type", "unsubscribed");
packet.setAttribute("to", contact.getJid());
packet.setAttribute("from", contact.getAccount().getJid());
contact.getAccount().getXmppConnection().sendPresencePacket(packet);
}
public void sendPresenceUpdatesTo(Contact contact) {
PresencePacket packet = new PresencePacket();
packet.setAttribute("type", "subscribed");
packet.setAttribute("to", contact.getJid());
packet.setAttribute("from", contact.getAccount().getJid());
contact.getAccount().getXmppConnection().sendPresencePacket(packet);
}
public void sendPresence(Account account) {
PresencePacket packet = new PresencePacket();
packet.setAttribute("from", account.getFullJid());
String sig = account.getPgpSignature();
if (sig != null) {
packet.addChild("status").setContent("online");
packet.addChild("x", "jabber:x:signed").setContent(sig);
}
account.getXmppConnection().sendPresencePacket(packet);
}
public void updateConversation(Conversation conversation) {
this.databaseBackend.updateConversation(conversation);
}
@ -1271,21 +1223,6 @@ public class XmppConnectionService extends Service {
}).start();
}
public void sendConversationSubject(Conversation conversation,
String subject) {
MessagePacket packet = new MessagePacket();
packet.setType(MessagePacket.TYPE_GROUPCHAT);
packet.setTo(conversation.getContactJid().split("/")[0]);
Element subjectChild = new Element("subject");
subjectChild.setContent(subject);
packet.addChild(subjectChild);
packet.setFrom(conversation.getAccount().getJid());
Account account = conversation.getAccount();
if (account.getStatus() == Account.STATUS_ONLINE) {
account.getXmppConnection().sendMessagePacket(packet);
}
}
public void inviteToConference(Conversation conversation,
List<Contact> contacts) {
for (Contact contact : contacts) {
@ -1299,8 +1236,7 @@ public class XmppConnectionService extends Service {
x.addChild(invite);
packet.addChild(x);
Log.d(LOGTAG, packet.toString());
conversation.getAccount().getXmppConnection()
.sendMessagePacket(packet);
sendMessagePacket(conversation.getAccount(),packet);
}
}
@ -1363,18 +1299,13 @@ public class XmppConnectionService extends Service {
}
public void markRead(Conversation conversation) {
conversation.markRead(this);
conversation.markRead();
String id = conversation.popLatestMarkableMessageId();
if (confirmMessages() && id != null) {
Account account = conversation.getAccount();
String to = conversation.getContactJid();
this.sendMessagePacket(conversation.getAccount(), mMessageGenerator.confirm(account, to, id));
}
public void sendConfirmMessage(Account account, String to, String id) {
MessagePacket receivedPacket = new MessagePacket();
receivedPacket.setType(MessagePacket.TYPE_NORMAL);
receivedPacket.setTo(to);
receivedPacket.setFrom(account.getFullJid());
Element received = receivedPacket.addChild("displayed",
"urn:xmpp:chat-markers:0");
received.setAttribute("id", id);
account.getXmppConnection().sendMessagePacket(receivedPacket);
}
public SecureRandom getRNG() {
@ -1389,7 +1320,7 @@ public class XmppConnectionService extends Service {
if (account.getStatus() == Account.STATUS_ONLINE) {
MessagePacket error = this.mMessageGenerator
.generateNotAcceptable(packet);
account.getXmppConnection().sendMessagePacket(error);
sendMessagePacket(account,error);
}
}
@ -1434,4 +1365,20 @@ public class XmppConnectionService extends Service {
}
return mucServers;
}
public void sendMessagePacket(Account account, MessagePacket packet) {
account.getXmppConnection().sendMessagePacket(packet);
}
public void sendPresencePacket(Account account, PresencePacket packet) {
account.getXmppConnection().sendPresencePacket(packet);
}
public MessageGenerator getMessageGenerator() {
return this.mMessageGenerator;
}
public PresenceGenerator getPresenceGenerator() {
return this.mPresenceGenerator;
}
}

View file

@ -31,7 +31,9 @@ import eu.siacs.conversations.crypto.PgpEngine;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
public class ContactDetailsActivity extends XmppActivity {
public static final String ACTION_VIEW_CONTACT = "view_contact";
@ -293,6 +295,8 @@ public class ContactDetailsActivity extends XmppActivity {
@Override
protected void onStop() {
super.onStop();
XmppConnectionService xcs = activity.xmppConnectionService;
PresencePacket packet = null;
boolean updated = false;
if (contact!=null) {
boolean online = contact.getAccount().getStatus() == Account.STATUS_ONLINE;
@ -301,7 +305,7 @@ public class ContactDetailsActivity extends XmppActivity {
if (online) {
contact.resetOption(Contact.Options.FROM);
contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
activity.xmppConnectionService.stopPresenceUpdatesTo(contact);
packet = xcs.getPresenceGenerator().stopPresenceUpdatesTo(contact);
}
updated = true;
}
@ -317,7 +321,7 @@ public class ContactDetailsActivity extends XmppActivity {
if (send.isChecked()) {
if (online) {
if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
xmppConnectionService.sendPresenceUpdatesTo(contact);
packet = xcs.getPresenceGenerator().sendPresenceUpdatesTo(contact);
} else {
contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
}
@ -330,7 +334,7 @@ public class ContactDetailsActivity extends XmppActivity {
if (!receive.isChecked()) {
if (online) {
contact.resetOption(Contact.Options.TO);
activity.xmppConnectionService.stopPresenceUpdatesFrom(contact);
packet = xcs.getPresenceGenerator().stopPresenceUpdatesFrom(contact);
}
updated = true;
}
@ -339,8 +343,7 @@ public class ContactDetailsActivity extends XmppActivity {
if (!receive.isChecked()) {
if (online) {
contact.resetOption(Contact.Options.ASKING);
activity.xmppConnectionService
.stopPresenceUpdatesFrom(contact);
packet = xcs.getPresenceGenerator().stopPresenceUpdatesFrom(contact);
}
updated = true;
}
@ -348,8 +351,7 @@ public class ContactDetailsActivity extends XmppActivity {
if (receive.isChecked()) {
if (online) {
contact.setOption(Contact.Options.ASKING);
activity.xmppConnectionService
.requestPresenceUpdatesFrom(contact);
packet = xcs.getPresenceGenerator().requestPresenceUpdatesFrom(contact);
}
updated = true;
}
@ -357,6 +359,9 @@ public class ContactDetailsActivity extends XmppActivity {
}
if (updated) {
if (online) {
if (packet!=null) {
xcs.sendPresencePacket(contact.getAccount(), packet);
}
Toast.makeText(getApplicationContext(), getString(R.string.subscription_updated), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), getString(R.string.subscription_not_updated_offline), Toast.LENGTH_SHORT).show();

View file

@ -11,9 +11,9 @@ import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.entities.MucOptions.User;
import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
@ -62,7 +62,8 @@ public class MucDetailsActivity extends XmppActivity {
String subject = mSubject.getText().toString();
MucOptions options = conversation.getMucOptions();
if (!subject.equals(options.getSubject())) {
xmppConnectionService.sendConversationSubject(conversation,subject);
MessagePacket packet = xmppConnectionService.getMessageGenerator().conversationSubject(conversation, subject);
xmppConnectionService.sendMessagePacket(conversation.getAccount(), packet);
finish();
}
}

View file

@ -203,7 +203,9 @@ public abstract class XmppActivity extends Activity {
public void success(Account account) {
xmppConnectionService.databaseBackend
.updateAccount(account);
xmppConnectionService.sendPresence(account);
xmppConnectionService.sendPresencePacket(account,
xmppConnectionService.getPresenceGenerator()
.sendPresence(account));
if (conversation != null) {
conversation
.setNextEncryption(Message.ENCRYPTION_PGP);

View file

@ -18,7 +18,6 @@ import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
@ -793,20 +792,10 @@ public class XmppConnection implements Runnable {
this.sendPacket(packet, null);
}
public void sendMessagePacket(MessagePacket packet,
OnMessagePacketReceived callback) {
this.sendPacket(packet, callback);
}
public void sendPresencePacket(PresencePacket packet) {
this.sendPacket(packet, null);
}
public void sendPresencePacket(PresencePacket packet,
OnPresencePacketReceived callback) {
this.sendPacket(packet, callback);
}
private synchronized void sendPacket(final AbstractStanza packet,
PacketReceived callback) {
// TODO dont increment stanza count if packet = request packet or ack;