001/**
002 *
003 * Copyright © 2018 Paul Schaub
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.last_interaction.element;
018
019import java.util.Date;
020
021import javax.xml.namespace.QName;
022
023import org.jivesoftware.smack.packet.ExtensionElement;
024import org.jivesoftware.smack.packet.Presence;
025import org.jivesoftware.smack.util.Objects;
026import org.jivesoftware.smack.util.XmlStringBuilder;
027
028public class IdleElement implements ExtensionElement {
029
030    public static final String NAMESPACE = "urn:xmpp:idle:1";
031    public static final String ELEMENT = "idle";
032    public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
033    public static final String ATTR_SINCE = "since";
034
035    private final Date since;
036
037    /**
038     * Create a new IdleElement with the current date as date of last user interaction.
039     */
040    public IdleElement() {
041        this(new Date());
042    }
043
044    /**
045     * Create a new IdleElement.
046     * @param since date of last user interaction
047     */
048    public IdleElement(Date since) {
049        this.since = Objects.requireNonNull(since);
050    }
051
052    /**
053     * Return the value of last user interaction.
054     * @return date of last interaction
055     */
056    public Date getSince() {
057        return since;
058    }
059
060    /**
061     * Add an Idle element with current date to the presence.
062     * @param presence presence
063     */
064    public static void addToPresence(Presence presence) {
065        presence.addExtension(new IdleElement());
066    }
067
068    /**
069     * Return the IdleElement from a presence.
070     * Returns null, if no IdleElement found.
071     *
072     * @param presence presence
073     * @return idleElement from presence or null
074     */
075    public static IdleElement fromPresence(Presence presence) {
076        return presence.getExtension(IdleElement.class);
077    }
078
079    /**
080     * {@inheritDoc}
081     */
082    @Override
083    public String getNamespace() {
084        return NAMESPACE;
085    }
086
087    /**
088     * {@inheritDoc}
089     */
090    @Override
091    public String getElementName() {
092        return ELEMENT;
093    }
094
095    /**
096     * {@inheritDoc}
097     */
098    @Override
099    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
100        return new XmlStringBuilder(this)
101                .attribute(ATTR_SINCE, since)
102                .closeEmptyElement();
103    }
104}