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