LastActivity.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software, 2014 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.iqlast.packet;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.SmackException;
  20. import org.jivesoftware.smack.packet.IQ;
  21. import org.jivesoftware.smack.provider.IQProvider;
  22. import org.jxmpp.jid.Jid;
  23. import org.xmlpull.v1.XmlPullParser;
  24. import org.xmlpull.v1.XmlPullParserException;

  25. /**
  26.  * A last activity IQ for retrieving information about the last activity associated with a Jabber ID.
  27.  * LastActivity (XEP-0012) allows for retrieval of how long a particular user has been idle and the
  28.  * message the specified when doing so. Use {@link org.jivesoftware.smackx.iqlast.LastActivityManager}
  29.  * to get the last activity of a user.
  30.  *
  31.  * @author Derek DeMoro
  32.  * @author Florian Schmaus
  33.  */
  34. public class LastActivity extends IQ {

  35.     public static final String ELEMENT = QUERY_ELEMENT;
  36.     public static final String NAMESPACE = "jabber:iq:last";

  37.     public long lastActivity = -1;
  38.     public String message;

  39.     public LastActivity() {
  40.         super(ELEMENT, NAMESPACE);
  41.         setType(IQ.Type.get);
  42.     }

  43.     public LastActivity(Jid to) {
  44.         this();
  45.         setTo(to);
  46.     }

  47.     @Override
  48.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  49.         xml.optLongAttribute("seconds", lastActivity);

  50.         // We don't support adding the optional message attribute, because it is usually only added
  51.         // by XMPP servers and not by client entities.
  52.         xml.setEmptyElement();
  53.         return xml;
  54.     }


  55.     public void setLastActivity(long lastActivity) {
  56.         this.lastActivity = lastActivity;
  57.     }


  58.     private void setMessage(String message) {
  59.         this.message = message;
  60.     }

  61.     /**
  62.      * Returns number of seconds that have passed since the user last logged out.
  63.      * If the user is offline, 0 will be returned.
  64.      *
  65.      * @return the number of seconds that have passed since the user last logged out.
  66.      */
  67.     public long getIdleTime() {
  68.         return lastActivity;
  69.     }


  70.     /**
  71.      * Returns the status message of the last unavailable presence received from the user.
  72.      *
  73.      * @return the status message of the last unavailable presence received from the user
  74.      */
  75.     public String getStatusMessage() {
  76.         return message;
  77.     }


  78.     /**
  79.      * The IQ Provider for LastActivity.
  80.      *
  81.      * @author Derek DeMoro
  82.      */
  83.     public static class Provider extends IQProvider<LastActivity> {

  84.         @Override
  85.         public LastActivity parse(XmlPullParser parser, int initialDepth) throws SmackException, XmlPullParserException {
  86.             LastActivity lastActivity = new LastActivity();
  87.             String seconds = parser.getAttributeValue("", "seconds");
  88.             if (seconds != null) {
  89.                 try {
  90.                     lastActivity.setLastActivity(Long.parseLong(seconds));
  91.                 } catch (NumberFormatException e) {
  92.                     throw new SmackException("Could not parse last activity number", e);
  93.                 }
  94.             }
  95.             try {
  96.                 lastActivity.setMessage(parser.nextText());
  97.             } catch (IOException e) {
  98.                 throw new SmackException(e);
  99.             }
  100.             return lastActivity;
  101.         }
  102.     }
  103. }