001/**
002 *
003 * Copyright 2003-2007 Jive Software, 2014-2019 Florian Schmaus
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 */
017
018package org.jivesoftware.smackx.iqlast.packet;
019
020import java.io.IOException;
021
022import org.jivesoftware.smack.packet.IQ;
023import org.jivesoftware.smack.packet.IqData;
024import org.jivesoftware.smack.packet.XmlEnvironment;
025import org.jivesoftware.smack.provider.IqProvider;
026import org.jivesoftware.smack.xml.XmlPullParser;
027import org.jivesoftware.smack.xml.XmlPullParserException;
028
029import org.jxmpp.jid.Jid;
030
031/**
032 * A last activity IQ for retrieving information about the last activity associated with a Jabber ID.
033 * LastActivity (XEP-0012) allows for retrieval of how long a particular user has been idle and the
034 * message the specified when doing so. Use {@link org.jivesoftware.smackx.iqlast.LastActivityManager}
035 * to get the last activity of a user.
036 *
037 * @author Derek DeMoro
038 * @author Florian Schmaus
039 */
040public class LastActivity extends IQ {
041
042    public static final String ELEMENT = QUERY_ELEMENT;
043    public static final String NAMESPACE = "jabber:iq:last";
044
045    public long lastActivity = -1;
046    public String message;
047
048    @SuppressWarnings("this-escape")
049    public LastActivity() {
050        super(ELEMENT, NAMESPACE);
051        setType(IQ.Type.get);
052    }
053
054    @SuppressWarnings("this-escape")
055    public LastActivity(Jid to) {
056        this();
057        setTo(to);
058    }
059
060    @Override
061    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
062        xml.optLongAttribute("seconds", lastActivity);
063
064        // We don't support adding the optional message attribute, because it is usually only added
065        // by XMPP servers and not by client entities.
066        xml.setEmptyElement();
067        return xml;
068    }
069
070
071    public void setLastActivity(long lastActivity) {
072        this.lastActivity = lastActivity;
073    }
074
075
076    private void setMessage(String message) {
077        this.message = message;
078    }
079
080    /**
081     * Returns number of seconds that have passed since the user last logged out.
082     * If the user is offline, 0 will be returned.
083     *
084     * @return the number of seconds that have passed since the user last logged out.
085     */
086    public long getIdleTime() {
087        return lastActivity;
088    }
089
090
091    /**
092     * Returns the status message of the last unavailable presence received from the user.
093     *
094     * @return the status message of the last unavailable presence received from the user
095     */
096    public String getStatusMessage() {
097        return message;
098    }
099
100
101    /**
102     * The IQ Provider for LastActivity.
103     *
104     * @author Derek DeMoro
105     */
106    public static class Provider extends IqProvider<LastActivity> {
107
108        @Override
109        public LastActivity parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
110            LastActivity lastActivity = new LastActivity();
111            String seconds = parser.getAttributeValue("", "seconds");
112            if (seconds != null) {
113                try {
114                    lastActivity.setLastActivity(Long.parseLong(seconds));
115                } catch (NumberFormatException e) {
116                    // TODO: Should be SmackParseException (or a SmackParseNumberException subclass of).
117                    throw new IOException("Could not parse last activity number", e);
118                }
119            }
120            lastActivity.setMessage(parser.nextText());
121            return lastActivity;
122        }
123    }
124}