blob: 24cfd0abbc572deda611741be09de57925fdb48e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
/*
* Copyright (c) 2010-2015, Isode Limited, London, England.
* All rights reserved.
*/
package com.isode.stroke.presence;
import com.isode.stroke.client.StanzaChannel;
import com.isode.stroke.elements.Presence;
import com.isode.stroke.jid.JID;
import com.isode.stroke.signals.Signal2;
import com.isode.stroke.signals.Signal3;
import com.isode.stroke.signals.SignalConnection;
import com.isode.stroke.signals.Slot1;
public class SubscriptionManager {
private StanzaChannel stanzaChannel;
private SignalConnection onPresenceReceivedConnection;
/**
* This signal is emitted when a presence subscription request is
* received.
*
* The third parameter of this signal is the original presence stanza
* received. This is useful when the subscriber adds extensions to
* the request.
*/
public final Signal3<JID, String, Presence> onPresenceSubscriptionRequest = new Signal3<JID, String, Presence>();
public final Signal2<JID, String> onPresenceSubscriptionRevoked = new Signal2<JID, String>();
public SubscriptionManager(StanzaChannel channel) {
stanzaChannel = channel;
onPresenceReceivedConnection = stanzaChannel.onPresenceReceived.connect(new Slot1<Presence>() {
@Override
public void call(Presence p1) {
handleIncomingPresence(p1);
}
});
}
void delete() {
onPresenceReceivedConnection.disconnect();
}
public void cancelSubscription(final JID jid) {
Presence stanza = new Presence();
stanza.setType(Presence.Type.Unsubscribed);
stanza.setTo(jid);
stanzaChannel.sendPresence(stanza);
}
public void confirmSubscription(final JID jid) {
Presence stanza = new Presence();
stanza.setType(Presence.Type.Subscribed);
stanza.setTo(jid);
stanzaChannel.sendPresence(stanza);
}
public void requestSubscription(final JID jid) {
Presence stanza = new Presence();
stanza.setType(Presence.Type.Subscribe);
stanza.setTo(jid);
stanzaChannel.sendPresence(stanza);
}
void handleIncomingPresence(Presence presence) {
JID bareJID = presence.getFrom().toBare();
if (Presence.Type.Subscribe.equals(presence.getType())) {
onPresenceSubscriptionRequest.emit(bareJID, presence.getStatus(), presence);
}
else if (Presence.Type.Unsubscribe.equals(presence.getType())) {
onPresenceSubscriptionRevoked.emit(bareJID, presence.getStatus());
}
}
}
|