From 44d3b0a08b22ccf4069d51b54ed0446c160ac53f Mon Sep 17 00:00:00 2001 From: Derek DeMoro Date: Wed, 14 Sep 2005 14:31:39 +0000 Subject: [PATCH] Added Last Activity Support(JEP-012) to allow for retrieval of idle times. git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2820 b35dd754-fafc-0310-a699-88a17e54d16e --- build/resources/META-INF/smack.providers | 7 + .../smackx/packet/LastActivity.java | 155 ++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 source/org/jivesoftware/smackx/packet/LastActivity.java diff --git a/build/resources/META-INF/smack.providers b/build/resources/META-INF/smack.providers index 03f217adb..2af3b25de 100644 --- a/build/resources/META-INF/smack.providers +++ b/build/resources/META-INF/smack.providers @@ -128,4 +128,11 @@ org.jivesoftware.smack.packet.Bind$Provider + + + query + jabber:iq:last + org.jivesoftware.smackx.packet.LastActivity$Provider + + \ No newline at end of file diff --git a/source/org/jivesoftware/smackx/packet/LastActivity.java b/source/org/jivesoftware/smackx/packet/LastActivity.java new file mode 100644 index 000000000..119a5bf1a --- /dev/null +++ b/source/org/jivesoftware/smackx/packet/LastActivity.java @@ -0,0 +1,155 @@ +/** + * $RCSfile$ + * $Revision: 2407 $ + * $Date: 2004-11-02 15:37:00 -0800 (Tue, 02 Nov 2004) $ + * + * Copyright 2003-2004 Jive Software. + * + * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.jivesoftware.smackx.packet; + +import org.jivesoftware.smack.PacketCollector; +import org.jivesoftware.smack.SmackConfiguration; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smack.filter.PacketIDFilter; +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.provider.IQProvider; +import org.xmlpull.v1.XmlPullParser; + +/** + * 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 + * message the specified when doing so. + * To get the last activity of a user, simple send the LastActivity packet to them, as in the + * following code example: + *

+ *

+ * XMPPConnection con = new XMPPConnection("jabber.org");
+ * con.login("john", "doe");
+ * LastActivity activity = LastActivity.getLastActivity(con, "xray@jabber.org");
+ * 
+ * + * @author Derek DeMoro + */ +public class LastActivity extends IQ { + + public long lastActivity; + public String message; + + public LastActivity() { + setType(IQ.Type.GET); + } + + public String getChildElementXML() { + StringBuffer buf = new StringBuffer(); + buf.append(""); + return buf.toString(); + } + + + private void setLastActivity(long lastActivity) { + this.lastActivity = lastActivity; + } + + + private void setMessage(String message) { + this.message = message; + } + + /** + * Returns number of seconds that have passed since the user last logged out. + * If the user is offline, 0 will be returned. + * + * @return the number of seconds that have passed since the user last logged out. + */ + public long getLastActivity() { + return lastActivity; + } + + + /** + * Returns the status message of the last unavailable presence received from the user. + * + * @return the status message of the last unavailable presence received from the user + */ + public String getStatusMessage() { + return message; + } + + + /** + * The IQ Provider for LastActivity. + * + * @author Derek DeMoro + */ + public static class Provider implements IQProvider { + + public Provider() { + super(); + } + + public IQ parseIQ(XmlPullParser parser) throws Exception { + if (parser.getEventType() != XmlPullParser.START_TAG) { + throw new IllegalStateException("Parser not in proper position, or bad XML."); + } + + LastActivity lastActivity = new LastActivity(); + try { + String seconds = parser.getAttributeValue("", "seconds"); + String message = parser.nextText(); + if (seconds != null) { + lastActivity.setLastActivity(Long.parseLong(seconds)); + } + + if (message != null) { + lastActivity.setMessage(message); + } + } + catch (Exception e) { + e.printStackTrace(); + } + return lastActivity; + } + } + + /** + * Retrieve the last activity of a particular jid. + * @param con the current XMPPConnection. + * @param jid the JID of the user. + * @return the LastActivity packet of the jid. + * @throws XMPPException thrown if a server error has occured. + */ + public static LastActivity getLastActivity(XMPPConnection con, String jid) throws XMPPException { + LastActivity activity = new LastActivity(); + activity.setTo(jid); + + PacketCollector collector = con.createPacketCollector(new PacketIDFilter(activity.getPacketID())); + con.sendPacket(activity); + + LastActivity response = (LastActivity) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); + + // Cancel the collector. + collector.cancel(); + if (response == null) { + throw new XMPPException("No response from server on status set."); + } + if (response.getError() != null) { + throw new XMPPException(response.getError()); + } + return response; + } +}