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.jivesoftware.smack.util.XmlStringBuilder; 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 NAMESPACE = "jabber:iq:last"; 041 042 public long lastActivity = -1; 043 public String message; 044 045 public LastActivity() { 046 setType(IQ.Type.GET); 047 } 048 049 public LastActivity(String to) { 050 this(); 051 setTo(to); 052 } 053 054 @Override 055 public XmlStringBuilder getChildElementXML() { 056 XmlStringBuilder xml = new XmlStringBuilder(); 057 xml.halfOpenElement("query"); 058 xml.xmlnsAttribute(NAMESPACE); 059 if (lastActivity != -1) { 060 xml.attribute("seconds", Long.toString(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.closeEmptyElement(); 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 implements IQProvider { 105 106 public Provider() { 107 super(); 108 } 109 110 public IQ parseIQ(XmlPullParser parser) throws SmackException, XmlPullParserException { 111 if (parser.getEventType() != XmlPullParser.START_TAG) { 112 throw new SmackException("Parser not in proper position, or bad XML."); 113 } 114 115 LastActivity lastActivity = new LastActivity(); 116 String seconds = parser.getAttributeValue("", "seconds"); 117 if (seconds != null) { 118 try { 119 lastActivity.setLastActivity(Long.parseLong(seconds)); 120 } catch (NumberFormatException e) { 121 throw new SmackException("Could not parse last activity number", e); 122 } 123 } 124 try { 125 lastActivity.setMessage(parser.nextText()); 126 } catch (IOException e) { 127 throw new SmackException(e); 128 } 129 return lastActivity; 130 } 131 } 132}