diff options
Diffstat (limited to '')
5 files changed, 57 insertions, 13 deletions
diff --git a/src/de/gultsch/chat/xmpp/MessagePacket.java b/src/de/gultsch/chat/xmpp/MessagePacket.java index de6e0234..313fb355 100644 --- a/src/de/gultsch/chat/xmpp/MessagePacket.java +++ b/src/de/gultsch/chat/xmpp/MessagePacket.java @@ -10,4 +10,12 @@ public class MessagePacket extends Element { public MessagePacket() { super("message"); } + + public String getFrom() { + return getAttribute("from"); + } + + public String getBody() { + return this.findChild("body").getContent(); + } } diff --git a/src/de/gultsch/chat/xmpp/OnIqPacketReceived.java b/src/de/gultsch/chat/xmpp/OnIqPacketReceived.java index 5e69b0ce..0a2e0361 100644 --- a/src/de/gultsch/chat/xmpp/OnIqPacketReceived.java +++ b/src/de/gultsch/chat/xmpp/OnIqPacketReceived.java @@ -1,5 +1,7 @@ package de.gultsch.chat.xmpp; +import de.gultsch.chat.entities.Account; + public interface OnIqPacketReceived { - public void onIqPacketReceived(IqPacket packet); + public void onIqPacketReceived(Account account, IqPacket packet); } diff --git a/src/de/gultsch/chat/xmpp/OnMessagePacketReceived.java b/src/de/gultsch/chat/xmpp/OnMessagePacketReceived.java index 9f4e6317..30f6c29b 100644 --- a/src/de/gultsch/chat/xmpp/OnMessagePacketReceived.java +++ b/src/de/gultsch/chat/xmpp/OnMessagePacketReceived.java @@ -1,5 +1,7 @@ package de.gultsch.chat.xmpp; +import de.gultsch.chat.entities.Account; + public interface OnMessagePacketReceived { - public void onMessagePacketReceived(MessagePacket packet); + public void onMessagePacketReceived(Account account, MessagePacket packet); } diff --git a/src/de/gultsch/chat/xmpp/OnPresencePacketReceived.java b/src/de/gultsch/chat/xmpp/OnPresencePacketReceived.java index d48c430d..4c976779 100644 --- a/src/de/gultsch/chat/xmpp/OnPresencePacketReceived.java +++ b/src/de/gultsch/chat/xmpp/OnPresencePacketReceived.java @@ -1,5 +1,7 @@ package de.gultsch.chat.xmpp; +import de.gultsch.chat.entities.Account; + public interface OnPresencePacketReceived { - public void onPresencePacketReceived(PresencePacket packet); + public void onPresencePacketReceived(Account account, PresencePacket packet); } diff --git a/src/de/gultsch/chat/xmpp/XmppConnection.java b/src/de/gultsch/chat/xmpp/XmppConnection.java index a167bd87..441e4b2d 100644 --- a/src/de/gultsch/chat/xmpp/XmppConnection.java +++ b/src/de/gultsch/chat/xmpp/XmppConnection.java @@ -49,6 +49,9 @@ public class XmppConnection implements Runnable { private static final int PACKET_PRESENCE = 2; private Hashtable<String, OnIqPacketReceived> iqPacketCallbacks = new Hashtable<String, OnIqPacketReceived>(); + private OnPresencePacketReceived presenceListener = null; + private OnIqPacketReceived unregisteredIqListener = null; + private OnMessagePacketReceived messageListener = null; public XmppConnection(Account account, PowerManager pm) { this.account = account; @@ -115,11 +118,11 @@ public class XmppConnection implements Runnable { sendStartStream(); processStream(tagReader.readTag()); } else if (nextTag.isStart("iq")) { - Log.d(LOGTAG,processIq(nextTag).toString()); + processIq(nextTag); } else if (nextTag.isStart("message")) { - Log.d(LOGTAG,processMessage(nextTag).toString()); + processMessage(nextTag); } else if (nextTag.isStart("presence")) { - Log.d(LOGTAG,processPresence(nextTag).toString()); + processPresence(nextTag); } else { Log.d(LOGTAG, "found unexpected tag: " + nextTag.getName() + " as child of " + currentTag.getName()); @@ -158,18 +161,26 @@ public class XmppConnection implements Runnable { private IqPacket processIq(Tag currentTag) throws XmlPullParserException, IOException { IqPacket packet = (IqPacket) processPacket(currentTag,PACKET_IQ); if (iqPacketCallbacks.containsKey(packet.getId())) { - iqPacketCallbacks.get(packet.getId()).onIqPacketReceived(packet); + iqPacketCallbacks.get(packet.getId()).onIqPacketReceived(account,packet); iqPacketCallbacks.remove(packet.getId()); + } else if (this.unregisteredIqListener != null) { + this.unregisteredIqListener.onIqPacketReceived(account,packet); } return packet; } - private MessagePacket processMessage(Tag currentTag) throws XmlPullParserException, IOException { - return (MessagePacket) processPacket(currentTag, PACKET_MESSAGE); + private void processMessage(Tag currentTag) throws XmlPullParserException, IOException { + MessagePacket packet = (MessagePacket) processPacket(currentTag, PACKET_MESSAGE); + if (this.messageListener != null) { + this.messageListener.onMessagePacketReceived(account,packet); + } } - private PresencePacket processPresence(Tag currentTag) throws XmlPullParserException, IOException { - return (PresencePacket) processPacket(currentTag, PACKET_PRESENCE); + private void processPresence(Tag currentTag) throws XmlPullParserException, IOException { + PresencePacket packet = (PresencePacket) processPacket(currentTag, PACKET_PRESENCE); + if (this.presenceListener != null) { + this.presenceListener.onPresencePacketReceived(account,packet); + } } private void sendStartTLS() throws XmlPullParserException, IOException { @@ -248,7 +259,7 @@ public class XmppConnection implements Runnable { iq.addChild(bind); this.sendIqPacket(iq, new OnIqPacketReceived() { @Override - public void onIqPacketReceived(IqPacket packet) { + public void onIqPacketReceived(Account account, IqPacket packet) { Log.d(LOGTAG,"answer for our bind was: "+packet.toString()); } }); @@ -277,10 +288,29 @@ public class XmppConnection implements Runnable { String id = nextRandomId(); packet.setAttribute("id",id); tagWriter.writeElement(packet); - tagWriter.flush(); if (callback != null) { iqPacketCallbacks.put(id, callback); } Log.d(LOGTAG,"sending: "+packet.toString()); } + + public void sendMessagePacket(MessagePacket packet) throws IOException { + tagWriter.writeElement(packet); + } + + public void sendPresencePacket(PresencePacket packet) throws IOException { + tagWriter.writeElement(packet); + } + + public void setOnMessagePacketReceivedListener(OnMessagePacketReceived listener) { + this.messageListener = listener; + } + + public void setOnUnregisteredIqPacketReceivedListener(OnIqPacketReceived listener) { + this.unregisteredIqListener = listener; + } + + public void setOnPresencePacketReceivedListener(OnPresencePacketReceived listener) { + this.presenceListener = listener; + } } |