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