2006-09-05 00:03:48 +02:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
2013-02-07 20:10:22 +01:00
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
2006-09-05 00:03:48 +02:00
|
|
|
*
|
|
|
|
* Copyright 2003-2006 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;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.*;
|
|
|
|
import org.jivesoftware.smack.filter.AndFilter;
|
|
|
|
import org.jivesoftware.smack.filter.IQTypeFilter;
|
|
|
|
import org.jivesoftware.smack.filter.PacketIDFilter;
|
|
|
|
import org.jivesoftware.smack.filter.PacketTypeFilter;
|
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
2013-02-14 01:00:54 +01:00
|
|
|
import org.jivesoftware.smack.packet.Message;
|
2006-09-05 00:03:48 +02:00
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
2013-02-14 01:00:54 +01:00
|
|
|
import org.jivesoftware.smack.packet.Presence;
|
|
|
|
import org.jivesoftware.smackx.packet.DiscoverInfo;
|
2006-09-05 00:03:48 +02:00
|
|
|
import org.jivesoftware.smackx.packet.LastActivity;
|
|
|
|
|
|
|
|
/**
|
2013-02-14 01:00:54 +01:00
|
|
|
* A last activity manager for handling information about the last activity
|
|
|
|
* associated with a Jabber ID. A manager handles incoming LastActivity requests
|
|
|
|
* of existing Connections. It also allows to request last activity information
|
|
|
|
* of other users.
|
|
|
|
* <p>
|
|
|
|
*
|
|
|
|
* LastActivity (XEP-0012) based on the sending JID's type allows for retrieval
|
|
|
|
* of:
|
2006-09-05 00:03:48 +02:00
|
|
|
* <ol>
|
|
|
|
* <li>How long a particular user has been idle
|
2013-02-14 01:00:54 +01:00
|
|
|
* <li>How long a particular user has been logged-out and the message the
|
|
|
|
* specified when doing so.
|
2006-09-05 00:03:48 +02:00
|
|
|
* <li>How long a host has been up.
|
|
|
|
* </ol>
|
|
|
|
* <p/>
|
2013-02-14 01:00:54 +01:00
|
|
|
*
|
|
|
|
* For example to get the idle time of a user logged in a resource, simple send
|
|
|
|
* the LastActivity packet to them, as in the following code:
|
|
|
|
* <p>
|
|
|
|
*
|
2006-09-05 00:03:48 +02:00
|
|
|
* <pre>
|
2013-02-14 01:00:54 +01:00
|
|
|
* Connection con = new XMPPConnection("jabber.org");
|
|
|
|
* con.login("john", "doe");
|
|
|
|
* LastActivity activity = LastActivity.getLastActivity(con, "xray@jabber.org/Smack");
|
2006-09-05 00:03:48 +02:00
|
|
|
* </pre>
|
2013-02-14 01:00:54 +01:00
|
|
|
*
|
|
|
|
* To get the lapsed time since the last user logout is the same as above but
|
|
|
|
* with out the resource:
|
|
|
|
*
|
2006-09-05 00:03:48 +02:00
|
|
|
* <pre>
|
2013-02-14 01:00:54 +01:00
|
|
|
* LastActivity activity = LastActivity.getLastActivity(con, "xray@jabber.org");
|
2006-09-05 00:03:48 +02:00
|
|
|
* </pre>
|
2013-02-14 01:00:54 +01:00
|
|
|
*
|
|
|
|
* To get the uptime of a host, you simple send the LastActivity packet to it,
|
|
|
|
* as in the following code example:
|
|
|
|
* <p>
|
|
|
|
*
|
2006-09-05 00:03:48 +02:00
|
|
|
* <pre>
|
2013-02-14 01:00:54 +01:00
|
|
|
* LastActivity activity = LastActivity.getLastActivity(con, "jabber.org");
|
2006-09-05 00:03:48 +02:00
|
|
|
* </pre>
|
2013-02-14 01:00:54 +01:00
|
|
|
*
|
2006-09-05 00:03:48 +02:00
|
|
|
* @author Gabriel Guardincerri
|
2013-02-14 01:00:54 +01:00
|
|
|
* @see <a href="http://xmpp.org/extensions/xep-0012.html">XEP-0012: Last
|
|
|
|
* Activity</a>
|
2006-09-05 00:03:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
public class LastActivityManager {
|
|
|
|
|
|
|
|
private long lastMessageSent;
|
|
|
|
|
2010-02-09 12:55:56 +01:00
|
|
|
private Connection connection;
|
2006-09-05 00:03:48 +02:00
|
|
|
|
|
|
|
// Enable the LastActivity support on every established connection
|
|
|
|
static {
|
2010-02-09 12:55:56 +01:00
|
|
|
Connection.addConnectionCreationListener(new ConnectionCreationListener() {
|
|
|
|
public void connectionCreated(Connection connection) {
|
2006-09-05 00:03:48 +02:00
|
|
|
new LastActivityManager(connection);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a last activity manager to response last activity requests.
|
2013-02-14 01:00:54 +01:00
|
|
|
*
|
|
|
|
* @param connection
|
|
|
|
* The Connection that the last activity requests will use.
|
2006-09-05 00:03:48 +02:00
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
private LastActivityManager(Connection connection) {
|
2006-09-05 00:03:48 +02:00
|
|
|
this.connection = connection;
|
|
|
|
|
|
|
|
// Listen to all the sent messages to reset the idle time on each one
|
2010-02-09 12:55:56 +01:00
|
|
|
connection.addPacketSendingListener(new PacketListener() {
|
2006-09-05 00:03:48 +02:00
|
|
|
public void processPacket(Packet packet) {
|
2013-02-14 01:00:54 +01:00
|
|
|
Presence presence = (Presence) packet;
|
|
|
|
Presence.Mode mode = presence.getMode();
|
|
|
|
if (mode == null) return;
|
|
|
|
switch (mode) {
|
|
|
|
case available:
|
|
|
|
case chat:
|
|
|
|
// We assume that only a switch to available and chat indicates user activity
|
|
|
|
// since other mode changes could be also a result of some sort of automatism
|
|
|
|
resetIdleTime();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, new PacketTypeFilter(Presence.class));
|
|
|
|
|
|
|
|
connection.addPacketListener(new PacketListener() {
|
|
|
|
@Override
|
|
|
|
public void processPacket(Packet packet) {
|
|
|
|
Message message = (Message) packet;
|
|
|
|
// if it's not an error message, reset the idle time
|
|
|
|
if (message.getType() == Message.Type.error) return;
|
2006-09-05 00:03:48 +02:00
|
|
|
resetIdleTime();
|
|
|
|
}
|
2013-02-14 01:00:54 +01:00
|
|
|
}, new PacketTypeFilter(Message.class));
|
2006-09-05 00:03:48 +02:00
|
|
|
|
|
|
|
// Register a listener for a last activity query
|
|
|
|
connection.addPacketListener(new PacketListener() {
|
|
|
|
|
|
|
|
public void processPacket(Packet packet) {
|
|
|
|
LastActivity message = new LastActivity();
|
|
|
|
message.setType(IQ.Type.RESULT);
|
|
|
|
message.setTo(packet.getFrom());
|
|
|
|
message.setFrom(packet.getTo());
|
|
|
|
message.setPacketID(packet.getPacketID());
|
|
|
|
message.setLastActivity(getIdleTime());
|
|
|
|
|
|
|
|
LastActivityManager.this.connection.sendPacket(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
}, new AndFilter(new IQTypeFilter(IQ.Type.GET), new PacketTypeFilter(LastActivity.class)));
|
2013-02-14 01:00:54 +01:00
|
|
|
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(LastActivity.NAMESPACE);
|
|
|
|
resetIdleTime();
|
2006-09-05 00:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the idle time to 0, this should be invoked when a new message is
|
|
|
|
* sent.
|
|
|
|
*/
|
|
|
|
private void resetIdleTime() {
|
2013-02-14 01:00:54 +01:00
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
synchronized (this) {
|
|
|
|
lastMessageSent = now;
|
|
|
|
}
|
2006-09-05 00:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The idle time is the lapsed time between the last message sent and now.
|
2013-02-14 01:00:54 +01:00
|
|
|
*
|
2006-09-05 00:03:48 +02:00
|
|
|
* @return the lapsed time between the last message sent and now.
|
|
|
|
*/
|
|
|
|
private long getIdleTime() {
|
2013-02-14 01:00:54 +01:00
|
|
|
long lms;
|
2006-09-05 00:03:48 +02:00
|
|
|
long now = System.currentTimeMillis();
|
2013-02-14 01:00:54 +01:00
|
|
|
synchronized (this) {
|
|
|
|
lms = lastMessageSent;
|
|
|
|
}
|
|
|
|
return ((now - lms) / 1000);
|
2006-09-05 00:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the last activity of a particular jid. If the jid is a full JID
|
|
|
|
* (i.e., a JID of the form of 'user@host/resource') then the last activity
|
|
|
|
* is the idle time of that connected resource. On the other hand, when the
|
|
|
|
* jid is a bare JID (e.g. 'user@host') then the last activity is the lapsed
|
2013-02-14 01:00:54 +01:00
|
|
|
* time since the last logout or 0 if the user is currently logged in.
|
|
|
|
* Moreover, when the jid is a server or component (e.g., a JID of the form
|
|
|
|
* 'host') the last activity is the uptime.
|
|
|
|
*
|
|
|
|
* @param con
|
|
|
|
* the current Connection.
|
|
|
|
* @param jid
|
|
|
|
* the JID of the user.
|
2006-09-05 00:03:48 +02:00
|
|
|
* @return the LastActivity packet of the jid.
|
2013-02-14 01:00:54 +01:00
|
|
|
* @throws XMPPException
|
|
|
|
* thrown if a server error has occured.
|
2006-09-05 00:03:48 +02:00
|
|
|
*/
|
2013-02-14 01:00:54 +01:00
|
|
|
public static LastActivity getLastActivity(Connection con, String jid) throws XMPPException {
|
2006-09-05 00:03:48 +02:00
|
|
|
LastActivity activity = new LastActivity();
|
|
|
|
activity.setTo(jid);
|
|
|
|
|
2013-02-14 01:00:54 +01:00
|
|
|
PacketCollector collector = con.createPacketCollector(new PacketIDFilter(activity.getPacketID()));
|
2006-09-05 00:03:48 +02:00
|
|
|
con.sendPacket(activity);
|
|
|
|
|
2013-02-14 01:00:54 +01:00
|
|
|
LastActivity response = (LastActivity) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
|
2006-09-05 00:03:48 +02:00
|
|
|
|
|
|
|
// 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;
|
2013-02-14 01:00:54 +01:00
|
|
|
}
|
2006-09-05 00:03:48 +02:00
|
|
|
|
2013-02-14 01:00:54 +01:00
|
|
|
/**
|
|
|
|
* Returns true if Last Activity (XEP-0012) is supported by a given JID
|
|
|
|
*
|
|
|
|
* @param connection the connection to be used
|
|
|
|
* @param jid a JID to be tested for Last Activity support
|
|
|
|
* @return true if Last Activity is supported, otherwise false
|
|
|
|
*/
|
|
|
|
public static boolean isLastActivitySupported(Connection connection, String jid) {
|
|
|
|
try {
|
|
|
|
DiscoverInfo result =
|
|
|
|
ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(jid);
|
|
|
|
return result.containsFeature(LastActivity.NAMESPACE);
|
|
|
|
}
|
|
|
|
catch (XMPPException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2006-09-05 00:03:48 +02:00
|
|
|
}
|