mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-23 04:22:05 +01:00
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
This commit is contained in:
parent
d61e2e5c59
commit
44d3b0a08b
2 changed files with 162 additions and 0 deletions
|
@ -128,4 +128,11 @@
|
|||
<className>org.jivesoftware.smack.packet.Bind$Provider</className>
|
||||
</iqProvider>
|
||||
|
||||
<!-- Last Activity -->
|
||||
<iqProvider>
|
||||
<elementName>query</elementName>
|
||||
<namespace>jabber:iq:last</namespace>
|
||||
<className>org.jivesoftware.smackx.packet.LastActivity$Provider</className>
|
||||
</iqProvider>
|
||||
|
||||
</smackProviders>
|
155
source/org/jivesoftware/smackx/packet/LastActivity.java
Normal file
155
source/org/jivesoftware/smackx/packet/LastActivity.java
Normal file
|
@ -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:
|
||||
* <p/>
|
||||
* <pre>
|
||||
* XMPPConnection con = new XMPPConnection("jabber.org");
|
||||
* con.login("john", "doe");
|
||||
* LastActivity activity = LastActivity.getLastActivity(con, "xray@jabber.org");
|
||||
* </pre>
|
||||
*
|
||||
* @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("<query xmlns=\"jabber:iq:last\"></query>");
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue