2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2004-11-03 00:53:30 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack.packet;
|
|
|
|
|
2014-06-10 18:35:24 +02:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2015-02-03 18:58:17 +01:00
|
|
|
import org.jivesoftware.smack.util.Objects;
|
2014-03-21 09:51:52 +01:00
|
|
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
2004-05-23 00:53:21 +02:00
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
2003-01-17 08:12:57 +01:00
|
|
|
* Represents XMPP presence packets. Every presence packet has a type, which is one of
|
|
|
|
* the following values:
|
|
|
|
* <ul>
|
2006-07-17 10:39:08 +02:00
|
|
|
* <li>{@link Presence.Type#available available} -- (Default) indicates the user is available to
|
2003-01-17 08:12:57 +01:00
|
|
|
* receive messages.
|
2006-07-17 10:39:08 +02:00
|
|
|
* <li>{@link Presence.Type#unavailable unavailable} -- the user is unavailable to receive messages.
|
|
|
|
* <li>{@link Presence.Type#subscribe subscribe} -- request subscription to recipient's presence.
|
|
|
|
* <li>{@link Presence.Type#subscribed subscribed} -- grant subscription to sender's presence.
|
|
|
|
* <li>{@link Presence.Type#unsubscribe unsubscribe} -- request removal of subscription to
|
|
|
|
* sender's presence.
|
|
|
|
* <li>{@link Presence.Type#unsubscribed unsubscribed} -- grant removal of subscription to
|
|
|
|
* sender's presence.
|
|
|
|
* <li>{@link Presence.Type#error error} -- the presence packet contains an error message.
|
2003-01-17 08:12:57 +01:00
|
|
|
* </ul><p>
|
|
|
|
*
|
|
|
|
* A number of attributes are optional:
|
2003-01-13 17:58:47 +01:00
|
|
|
* <ul>
|
|
|
|
* <li>Status -- free-form text describing a user's presence (i.e., gone to lunch).
|
|
|
|
* <li>Priority -- non-negative numerical priority of a sender's resource. The
|
|
|
|
* highest resource priority is the default recipient of packets not addressed
|
|
|
|
* to a particular resource.
|
2006-07-17 10:39:08 +02:00
|
|
|
* <li>Mode -- one of five presence modes: {@link Mode#available available} (the default),
|
|
|
|
* {@link Mode#chat chat}, {@link Mode#away away}, {@link Mode#xa xa} (extended away), and
|
|
|
|
* {@link Mode#dnd dnd} (do not disturb).
|
2003-01-17 08:12:57 +01:00
|
|
|
* </ul><p>
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2014-05-12 18:38:28 +02:00
|
|
|
* Presence packets are used for two purposes. First, to notify the server of
|
|
|
|
* the user's current presence status. Second, they are used to subscribe and
|
2003-01-16 16:41:42 +01:00
|
|
|
* unsubscribe users from the roster.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* @author Matt Tucker
|
|
|
|
*/
|
2015-02-05 11:17:27 +01:00
|
|
|
public final class Presence extends Stanza {
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2014-09-03 17:50:12 +02:00
|
|
|
public static final String ELEMENT = "presence";
|
|
|
|
|
2006-07-17 10:39:08 +02:00
|
|
|
private Type type = Type.available;
|
2003-01-13 17:58:47 +01:00
|
|
|
private String status = null;
|
2007-04-10 20:44:08 +02:00
|
|
|
private int priority = Integer.MIN_VALUE;
|
2007-02-12 02:11:09 +01:00
|
|
|
private Mode mode = null;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new presence update. Status, priority, and mode are left un-set.
|
|
|
|
*
|
2003-01-16 16:41:42 +01:00
|
|
|
* @param type the type.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2003-01-16 16:41:42 +01:00
|
|
|
public Presence(Type type) {
|
2007-04-26 08:22:55 +02:00
|
|
|
setType(type);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new presence update with a specified status, priority, and mode.
|
|
|
|
*
|
2003-01-16 16:41:42 +01:00
|
|
|
* @param type the type.
|
2003-01-13 17:58:47 +01:00
|
|
|
* @param status a text message describing the presence update.
|
|
|
|
* @param priority the priority of this presence update.
|
|
|
|
* @param mode the mode type for this presence update.
|
|
|
|
*/
|
2003-01-16 16:41:42 +01:00
|
|
|
public Presence(Type type, String status, int priority, Mode mode) {
|
2007-04-26 08:22:55 +02:00
|
|
|
setType(type);
|
|
|
|
setStatus(status);
|
|
|
|
setPriority(priority);
|
|
|
|
setMode(mode);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2007-02-17 20:43:44 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the {@link Type presence type} is available (online) and
|
|
|
|
* false if the user is unavailable (offline), or if this is a presence packet
|
|
|
|
* involved in a subscription operation. This is a convenience method
|
2008-11-19 21:54:05 +01:00
|
|
|
* equivalent to <tt>getType() == Presence.Type.available</tt>. Note that even
|
|
|
|
* when the user is available, their presence mode may be {@link Mode#away away},
|
|
|
|
* {@link Mode#xa extended away} or {@link Mode#dnd do not disturb}. Use
|
|
|
|
* {@link #isAway()} to determine if the user is away.
|
2007-02-17 20:43:44 +01:00
|
|
|
*
|
|
|
|
* @return true if the presence type is available.
|
|
|
|
*/
|
|
|
|
public boolean isAvailable() {
|
|
|
|
return type == Type.available;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the presence type is {@link Type#available available} and the presence
|
|
|
|
* mode is {@link Mode#away away}, {@link Mode#xa extended away}, or
|
|
|
|
* {@link Mode#dnd do not disturb}. False will be returned when the type or mode
|
|
|
|
* is any other value, including when the presence type is unavailable (offline).
|
|
|
|
* This is a convenience method equivalent to
|
|
|
|
* <tt>type == Type.available && (mode == Mode.away || mode == Mode.xa || mode == Mode.dnd)</tt>.
|
|
|
|
*
|
|
|
|
* @return true if the presence type is available and the presence mode is away, xa, or dnd.
|
|
|
|
*/
|
|
|
|
public boolean isAway() {
|
|
|
|
return type == Type.available && (mode == Mode.away || mode == Mode.xa || mode == Mode.dnd);
|
|
|
|
}
|
|
|
|
|
2003-01-17 08:12:57 +01:00
|
|
|
/**
|
|
|
|
* Returns the type of this presence packet.
|
|
|
|
*
|
|
|
|
* @return the type of the presence packet.
|
|
|
|
*/
|
2003-01-16 16:41:42 +01:00
|
|
|
public Type getType() {
|
|
|
|
return type;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2003-01-17 08:12:57 +01:00
|
|
|
/**
|
|
|
|
* Sets the type of the presence packet.
|
|
|
|
*
|
|
|
|
* @param type the type of the presence packet.
|
|
|
|
*/
|
2003-01-16 16:41:42 +01:00
|
|
|
public void setType(Type type) {
|
2015-02-03 18:58:17 +01:00
|
|
|
this.type = Objects.requireNonNull(type, "Type cannot be null");
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the status message of the presence update, or <tt>null</tt> if there
|
2003-04-18 06:38:27 +02:00
|
|
|
* is not a status. The status is free-form text describing a user's presence
|
2003-01-13 17:58:47 +01:00
|
|
|
* (i.e., "gone to lunch").
|
|
|
|
*
|
|
|
|
* @return the status message.
|
|
|
|
*/
|
|
|
|
public String getStatus() {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the status message of the presence update. The status is free-form text
|
2003-04-18 06:38:27 +02:00
|
|
|
* describing a user's presence (i.e., "gone to lunch").
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* @param status the status message.
|
|
|
|
*/
|
|
|
|
public void setStatus(String status) {
|
|
|
|
this.status = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-04-10 20:44:08 +02:00
|
|
|
* Returns the priority of the presence, or Integer.MIN_VALUE if no priority has been set.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* @return the priority.
|
|
|
|
*/
|
|
|
|
public int getPriority() {
|
|
|
|
return priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-11-05 18:44:05 +01:00
|
|
|
* Sets the priority of the presence. The valid range is -128 through 128.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* @param priority the priority of the presence.
|
2004-11-05 18:44:05 +01:00
|
|
|
* @throws IllegalArgumentException if the priority is outside the valid range.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
public void setPriority(int priority) {
|
2004-11-05 18:44:05 +01:00
|
|
|
if (priority < -128 || priority > 128) {
|
|
|
|
throw new IllegalArgumentException("Priority value " + priority +
|
|
|
|
" is not valid. Valid range is -128 through 128.");
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
this.priority = priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-03 18:58:45 +01:00
|
|
|
* Returns the mode of the presence update.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* @return the mode.
|
|
|
|
*/
|
|
|
|
public Mode getMode() {
|
2015-02-03 18:58:45 +01:00
|
|
|
if (mode == null) {
|
|
|
|
return Mode.available;
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-02-12 02:14:30 +01:00
|
|
|
* Sets the mode of the presence update. A null presence mode value is interpreted
|
|
|
|
* to be the same thing as {@link Presence.Mode#available}.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* @param mode the mode.
|
|
|
|
*/
|
|
|
|
public void setMode(Mode mode) {
|
|
|
|
this.mode = mode;
|
|
|
|
}
|
|
|
|
|
2014-03-21 09:51:52 +01:00
|
|
|
@Override
|
|
|
|
public XmlStringBuilder toXML() {
|
|
|
|
XmlStringBuilder buf = new XmlStringBuilder();
|
2014-09-03 17:50:12 +02:00
|
|
|
buf.halfOpenElement(ELEMENT);
|
2014-03-21 09:51:52 +01:00
|
|
|
addCommonAttributes(buf);
|
2006-07-17 10:39:08 +02:00
|
|
|
if (type != Type.available) {
|
2014-03-21 09:51:52 +01:00
|
|
|
buf.attribute("type", type);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2014-08-20 21:25:14 +02:00
|
|
|
buf.rightAngleBracket();
|
2014-03-21 09:51:52 +01:00
|
|
|
|
|
|
|
buf.optElement("status", status);
|
2007-04-10 20:44:08 +02:00
|
|
|
if (priority != Integer.MIN_VALUE) {
|
2014-03-21 09:51:52 +01:00
|
|
|
buf.element("priority", Integer.toString(priority));
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2006-07-17 10:39:08 +02:00
|
|
|
if (mode != null && mode != Mode.available) {
|
2014-03-21 09:51:52 +01:00
|
|
|
buf.element("show", mode);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2014-03-21 09:51:52 +01:00
|
|
|
buf.append(getExtensionsXML());
|
2003-05-27 17:07:13 +02:00
|
|
|
|
2005-03-28 18:54:49 +02:00
|
|
|
// Add the error sub-packet, if there is one.
|
2014-11-07 21:12:01 +01:00
|
|
|
appendErrorIfExists(buf);
|
|
|
|
|
2014-09-03 17:50:12 +02:00
|
|
|
buf.closeElement(ELEMENT);
|
2005-03-28 18:54:49 +02:00
|
|
|
|
2014-03-21 09:51:52 +01:00
|
|
|
return buf;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2003-01-16 16:41:42 +01:00
|
|
|
/**
|
2014-05-12 18:38:28 +02:00
|
|
|
* An enum to represent the presence type. Note that presence type is often confused
|
|
|
|
* with presence mode. Generally, if a user is signed in to a server, they have a presence
|
2006-07-17 10:39:08 +02:00
|
|
|
* type of {@link #available available}, even if the mode is {@link Mode#away away},
|
|
|
|
* {@link Mode#dnd dnd}, etc. The presence type is only {@link #unavailable unavailable} when
|
|
|
|
* the user is signing out of the server.
|
2003-01-16 16:41:42 +01:00
|
|
|
*/
|
2006-07-17 10:39:08 +02:00
|
|
|
public enum Type {
|
2003-01-16 16:41:42 +01:00
|
|
|
|
2006-07-17 10:39:08 +02:00
|
|
|
/**
|
|
|
|
* The user is available to receive messages (default).
|
|
|
|
*/
|
|
|
|
available,
|
2003-01-16 16:41:42 +01:00
|
|
|
|
2006-07-17 10:39:08 +02:00
|
|
|
/**
|
|
|
|
* The user is unavailable to receive messages.
|
|
|
|
*/
|
|
|
|
unavailable,
|
2003-01-16 16:41:42 +01:00
|
|
|
|
2006-07-17 10:39:08 +02:00
|
|
|
/**
|
|
|
|
* Request subscription to recipient's presence.
|
|
|
|
*/
|
|
|
|
subscribe,
|
2003-01-16 16:41:42 +01:00
|
|
|
|
2006-07-17 10:39:08 +02:00
|
|
|
/**
|
|
|
|
* Grant subscription to sender's presence.
|
|
|
|
*/
|
|
|
|
subscribed,
|
2003-01-16 16:41:42 +01:00
|
|
|
|
|
|
|
/**
|
2006-07-17 10:39:08 +02:00
|
|
|
* Request removal of subscription to sender's presence.
|
2003-01-16 16:41:42 +01:00
|
|
|
*/
|
2006-07-17 10:39:08 +02:00
|
|
|
unsubscribe,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grant removal of subscription to sender's presence.
|
|
|
|
*/
|
|
|
|
unsubscribed,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The presence packet contains an error message.
|
|
|
|
*/
|
2014-09-03 17:32:41 +02:00
|
|
|
error,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A presence probe as defined in section 4.3 of RFC 6121
|
|
|
|
*/
|
|
|
|
probe,
|
|
|
|
;
|
2014-06-10 18:35:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a String into the corresponding types. Valid String values that can be converted
|
|
|
|
* to types are: "available", "unavailable", "subscribe", "subscribed", "unsubscribe",
|
|
|
|
* "unsubscribed" and "error".
|
|
|
|
*
|
|
|
|
* @param string the String value to covert.
|
|
|
|
* @return the corresponding Type.
|
|
|
|
* @throws IllegalArgumentException when not able to parse the string parameter
|
|
|
|
* @throws NullPointerException if the string is null
|
|
|
|
*/
|
|
|
|
public static Type fromString(String string) {
|
|
|
|
return Type.valueOf(string.toLowerCase(Locale.US));
|
|
|
|
}
|
2003-01-16 16:41:42 +01:00
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
2006-07-17 10:39:08 +02:00
|
|
|
* An enum to represent the presence mode.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2006-07-17 10:39:08 +02:00
|
|
|
public enum Mode {
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2006-07-17 10:39:08 +02:00
|
|
|
/**
|
2007-02-12 01:56:47 +01:00
|
|
|
* Free to chat.
|
2006-07-17 10:39:08 +02:00
|
|
|
*/
|
2007-02-12 01:56:47 +01:00
|
|
|
chat,
|
2003-01-16 16:41:42 +01:00
|
|
|
|
2006-07-17 10:39:08 +02:00
|
|
|
/**
|
2007-02-12 01:56:47 +01:00
|
|
|
* Available (the default).
|
2006-07-17 10:39:08 +02:00
|
|
|
*/
|
2007-02-12 01:56:47 +01:00
|
|
|
available,
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2006-07-17 10:39:08 +02:00
|
|
|
/**
|
|
|
|
* Away.
|
|
|
|
*/
|
|
|
|
away,
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2006-07-17 10:39:08 +02:00
|
|
|
/**
|
|
|
|
* Away for an extended period of time.
|
|
|
|
*/
|
|
|
|
xa,
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
/**
|
2006-07-17 10:39:08 +02:00
|
|
|
* Do not disturb.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2014-06-10 18:35:24 +02:00
|
|
|
dnd;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a String into the corresponding types. Valid String values that can be converted
|
|
|
|
* to types are: "chat", "available", "away", "xa", and "dnd".
|
|
|
|
*
|
|
|
|
* @param string the String value to covert.
|
|
|
|
* @return the corresponding Type.
|
|
|
|
* @throws IllegalArgumentException when not able to parse the string parameter
|
|
|
|
* @throws NullPointerException if the string is null
|
|
|
|
*/
|
|
|
|
public static Mode fromString(String string) {
|
|
|
|
return Mode.valueOf(string.toLowerCase(Locale.US));
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|