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