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    @SuppressWarnings("JavaUtilDate")
041    public IdleElement() {
042        this(new Date());
043    }
044
045    /**
046     * Create a new IdleElement.
047     * @param since date of last user interaction
048     */
049    public IdleElement(Date since) {
050        this.since = Objects.requireNonNull(since);
051    }
052
053    /**
054     * Return the value of last user interaction.
055     * @return date of last interaction
056     */
057    public Date getSince() {
058        return since;
059    }
060
061    /**
062     * Add an Idle element with current date to the presence.
063     * @param presence presence
064     */
065    public static void addToPresence(Presence presence) {
066        presence.addExtension(new IdleElement());
067    }
068
069    /**
070     * Return the IdleElement from a presence.
071     * Returns null, if no IdleElement found.
072     *
073     * @param presence presence
074     * @return idleElement from presence or null
075     */
076    public static IdleElement fromPresence(Presence presence) {
077        return presence.getExtension(IdleElement.class);
078    }
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    public String getNamespace() {
085        return NAMESPACE;
086    }
087
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    public String getElementName() {
093        return ELEMENT;
094    }
095
096    /**
097     * {@inheritDoc}
098     */
099    @Override
100    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
101        return new XmlStringBuilder(this)
102                .attribute(ATTR_SINCE, since)
103                .closeEmptyElement();
104    }
105}