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 public LastActivity() { 049 super(ELEMENT, NAMESPACE); 050 setType(IQ.Type.get); 051 } 052 053 public LastActivity(Jid to) { 054 this(); 055 setTo(to); 056 } 057 058 @Override 059 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { 060 xml.optLongAttribute("seconds", lastActivity); 061 062 // We don't support adding the optional message attribute, because it is usually only added 063 // by XMPP servers and not by client entities. 064 xml.setEmptyElement(); 065 return xml; 066 } 067 068 069 public void setLastActivity(long lastActivity) { 070 this.lastActivity = lastActivity; 071 } 072 073 074 private void setMessage(String message) { 075 this.message = message; 076 } 077 078 /** 079 * Returns number of seconds that have passed since the user last logged out. 080 * If the user is offline, 0 will be returned. 081 * 082 * @return the number of seconds that have passed since the user last logged out. 083 */ 084 public long getIdleTime() { 085 return lastActivity; 086 } 087 088 089 /** 090 * Returns the status message of the last unavailable presence received from the user. 091 * 092 * @return the status message of the last unavailable presence received from the user 093 */ 094 public String getStatusMessage() { 095 return message; 096 } 097 098 099 /** 100 * The IQ Provider for LastActivity. 101 * 102 * @author Derek DeMoro 103 */ 104 public static class Provider extends IqProvider<LastActivity> { 105 106 @Override 107 public LastActivity parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException { 108 LastActivity lastActivity = new LastActivity(); 109 String seconds = parser.getAttributeValue("", "seconds"); 110 if (seconds != null) { 111 try { 112 lastActivity.setLastActivity(Long.parseLong(seconds)); 113 } catch (NumberFormatException e) { 114 // TODO: Should be SmackParseException (or a SmackParseNumberException subclass of). 115 throw new IOException("Could not parse last activity number", e); 116 } 117 } 118 lastActivity.setMessage(parser.nextText()); 119 return lastActivity; 120 } 121 } 122}