PresenceGenerator.java

 1package eu.siacs.conversations.generator;
 2
 3import android.text.TextUtils;
 4
 5import eu.siacs.conversations.entities.Account;
 6import eu.siacs.conversations.entities.Contact;
 7import eu.siacs.conversations.entities.MucOptions;
 8import eu.siacs.conversations.entities.Presence;
 9import eu.siacs.conversations.services.XmppConnectionService;
10import eu.siacs.conversations.xml.Element;
11import eu.siacs.conversations.xml.Namespace;
12import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
13
14public class PresenceGenerator extends AbstractGenerator {
15
16	public PresenceGenerator(XmppConnectionService service) {
17		super(service);
18	}
19
20	private PresencePacket subscription(String type, Contact contact) {
21		PresencePacket packet = new PresencePacket();
22		packet.setAttribute("type", type);
23		packet.setTo(contact.getJid());
24		packet.setFrom(contact.getAccount().getJid().asBareJid());
25		return packet;
26	}
27
28	public PresencePacket requestPresenceUpdatesFrom(Contact contact) {
29		PresencePacket packet = subscription("subscribe", contact);
30		String displayName = contact.getAccount().getDisplayName();
31		if (!TextUtils.isEmpty(displayName)) {
32			packet.addChild("nick",Namespace.NICK).setContent(displayName);
33		}
34		return packet;
35	}
36
37	public PresencePacket stopPresenceUpdatesFrom(Contact contact) {
38		return subscription("unsubscribe", contact);
39	}
40
41	public PresencePacket stopPresenceUpdatesTo(Contact contact) {
42		return subscription("unsubscribed", contact);
43	}
44
45	public PresencePacket sendPresenceUpdatesTo(Contact contact) {
46		return subscription("subscribed", contact);
47	}
48
49	public PresencePacket selfPresence(Account account, Presence.Status status) {
50		return selfPresence(account, status, true);
51	}
52
53	public PresencePacket selfPresence(Account account, Presence.Status status, boolean includePgpAnnouncement) {
54		PresencePacket packet = new PresencePacket();
55		if(status.toShowString() != null) {
56			packet.addChild("show").setContent(status.toShowString());
57		}
58		packet.setFrom(account.getJid());
59		final String sig = account.getPgpSignature();
60		if (includePgpAnnouncement && sig != null && mXmppConnectionService.getPgpEngine() != null) {
61			packet.addChild("x", "jabber:x:signed").setContent(sig);
62		}
63		final String capHash = getCapHash(account);
64		if (capHash != null) {
65			Element cap = packet.addChild("c",
66					"http://jabber.org/protocol/caps");
67			cap.setAttribute("hash", "sha-1");
68			cap.setAttribute("node", "http://conversations.im");
69			cap.setAttribute("ver", capHash);
70		}
71		return packet;
72	}
73
74	public PresencePacket leave(final MucOptions mucOptions) {
75		PresencePacket presencePacket = new PresencePacket();
76		presencePacket.setTo(mucOptions.getSelf().getFullJid());
77		presencePacket.setFrom(mucOptions.getAccount().getJid());
78		presencePacket.setAttribute("type", "unavailable");
79		return presencePacket;
80	}
81
82	public PresencePacket sendOfflinePresence(Account account) {
83		PresencePacket packet = new PresencePacket();
84		packet.setFrom(account.getJid());
85		packet.setAttribute("type","unavailable");
86		return packet;
87	}
88}