1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-09-10 18:59:41 +02:00

SMACK-345: Improved detection of last activity. Properly synchronized access to lastMessageSent and registerd last activity namespace with ServiceDiscoveryManager. Improved LastActivity Provider

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@13475 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Florian Schmaus 2013-02-14 00:00:54 +00:00 committed by flow
parent ddabe436b4
commit 4060a97b65
2 changed files with 128 additions and 63 deletions

View file

@ -20,6 +20,8 @@
package org.jivesoftware.smackx.packet;
import java.io.IOException;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.Connection;
@ -29,10 +31,11 @@ import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.StringUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
/**
* A last activity IQ for retrieving information about the last activity associated with a Jabber ID.
* LastActivity (JEP-012) allows for retrieval of how long a particular user has been idle and the
* LastActivity (XEP-0012) allows for retrieval of how long a particular user has been idle and the
* message the specified when doing so. Use {@link org.jivesoftware.smackx.LastActivityManager}
* to get the last activity of a user.
*
@ -40,6 +43,8 @@ import org.xmlpull.v1.XmlPullParser;
*/
public class LastActivity extends IQ {
public static final String NAMESPACE = "jabber:iq:last";
public long lastActivity = -1;
public String message;
@ -49,12 +54,11 @@ public class LastActivity extends IQ {
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append("<query xmlns=\"jabber:iq:last\"");
if (lastActivity != -1) {
buf.append(" seconds=\"").append(lastActivity).append("\"");
}
buf.append("></query>");
buf.append("<query xmlns=\"" + NAMESPACE + "\"");
if (lastActivity != -1) {
buf.append(" seconds=\"").append(lastActivity).append("\"");
}
buf.append("></query>");
return buf.toString();
}
@ -100,26 +104,29 @@ public class LastActivity extends IQ {
super();
}
public IQ parseIQ(XmlPullParser parser) throws Exception {
public IQ parseIQ(XmlPullParser parser) throws XMPPException, XmlPullParserException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException("Parser not in proper position, or bad XML.");
throw new XMPPException("Parser not in proper position, or bad XML.");
}
LastActivity lastActivity = new LastActivity();
String seconds = parser.getAttributeValue("", "seconds");
String message = null;
try {
String seconds = parser.getAttributeValue("", "seconds");
String message = parser.nextText();
if (seconds != null) {
long xmlSeconds = new Double(seconds).longValue();
lastActivity.setLastActivity((int)xmlSeconds);
}
if (message != null) {
lastActivity.setMessage(message);
message = parser.nextText();
} catch (IOException e1) {
// Ignore
}
if (seconds != null) {
try {
lastActivity.setLastActivity(Long.parseLong(seconds));
} catch (NumberFormatException e) {
// Ignore
}
}
catch (Exception e) {
e.printStackTrace();
if (message != null) {
lastActivity.setMessage(message);
}
return lastActivity;
}