IdleElement.java

  1. /**
  2.  *
  3.  * Copyright © 2018 Paul Schaub
  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.last_interaction.element;

  18. import java.util.Date;

  19. import javax.xml.namespace.QName;

  20. import org.jivesoftware.smack.packet.ExtensionElement;
  21. import org.jivesoftware.smack.packet.Presence;
  22. import org.jivesoftware.smack.util.Objects;
  23. import org.jivesoftware.smack.util.XmlStringBuilder;

  24. public class IdleElement implements ExtensionElement {

  25.     public static final String NAMESPACE = "urn:xmpp:idle:1";
  26.     public static final String ELEMENT = "idle";
  27.     public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
  28.     public static final String ATTR_SINCE = "since";

  29.     private final Date since;

  30.     /**
  31.      * Create a new IdleElement with the current date as date of last user interaction.
  32.      */
  33.     public IdleElement() {
  34.         this(new Date());
  35.     }

  36.     /**
  37.      * Create a new IdleElement.
  38.      * @param since date of last user interaction
  39.      */
  40.     public IdleElement(Date since) {
  41.         this.since = Objects.requireNonNull(since);
  42.     }

  43.     /**
  44.      * Return the value of last user interaction.
  45.      * @return date of last interaction
  46.      */
  47.     public Date getSince() {
  48.         return since;
  49.     }

  50.     /**
  51.      * Add an Idle element with current date to the presence.
  52.      * @param presence presence
  53.      */
  54.     public static void addToPresence(Presence presence) {
  55.         presence.addExtension(new IdleElement());
  56.     }

  57.     /**
  58.      * Return the IdleElement from a presence.
  59.      * Returns null, if no IdleElement found.
  60.      *
  61.      * @param presence presence
  62.      * @return idleElement from presence or null
  63.      */
  64.     public static IdleElement fromPresence(Presence presence) {
  65.         return presence.getExtension(IdleElement.class);
  66.     }

  67.     /**
  68.      * {@inheritDoc}
  69.      */
  70.     @Override
  71.     public String getNamespace() {
  72.         return NAMESPACE;
  73.     }

  74.     /**
  75.      * {@inheritDoc}
  76.      */
  77.     @Override
  78.     public String getElementName() {
  79.         return ELEMENT;
  80.     }

  81.     /**
  82.      * {@inheritDoc}
  83.      */
  84.     @Override
  85.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  86.         return new XmlStringBuilder(this)
  87.                 .attribute(ATTR_SINCE, since)
  88.                 .closeEmptyElement();
  89.     }
  90. }