mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-25 15:52:06 +01:00
Use switch-case for Presence.getType()
This commit is contained in:
parent
469548f53a
commit
7874daa59b
2 changed files with 31 additions and 23 deletions
|
@ -849,12 +849,15 @@ public class Roster {
|
||||||
Presence presence = (Presence) packet;
|
Presence presence = (Presence) packet;
|
||||||
String from = presence.getFrom();
|
String from = presence.getFrom();
|
||||||
String key = getPresenceMapKey(from);
|
String key = getPresenceMapKey(from);
|
||||||
|
Map<String, Presence> userPresences;
|
||||||
|
RosterEntry entry;
|
||||||
|
Presence response = null;
|
||||||
|
|
||||||
// If an "available" presence, add it to the presence map. Each presence
|
// If an "available" presence, add it to the presence map. Each presence
|
||||||
// map will hold for a particular user a map with the presence
|
// map will hold for a particular user a map with the presence
|
||||||
// packets saved for each resource.
|
// packets saved for each resource.
|
||||||
if (presence.getType() == Presence.Type.available) {
|
switch (presence.getType()) {
|
||||||
Map<String, Presence> userPresences;
|
case available:
|
||||||
// Get the user presence map
|
// Get the user presence map
|
||||||
if (presenceMap.get(key) == null) {
|
if (presenceMap.get(key) == null) {
|
||||||
userPresences = new ConcurrentHashMap<String, Presence>();
|
userPresences = new ConcurrentHashMap<String, Presence>();
|
||||||
|
@ -869,17 +872,16 @@ public class Roster {
|
||||||
// Add the new presence, using the resources as a key.
|
// Add the new presence, using the resources as a key.
|
||||||
userPresences.put(XmppStringUtils.parseResource(from), presence);
|
userPresences.put(XmppStringUtils.parseResource(from), presence);
|
||||||
// If the user is in the roster, fire an event.
|
// If the user is in the roster, fire an event.
|
||||||
RosterEntry entry = entries.get(key);
|
entry = entries.get(key);
|
||||||
if (entry != null) {
|
if (entry != null) {
|
||||||
fireRosterPresenceEvent(presence);
|
fireRosterPresenceEvent(presence);
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
// If an "unavailable" packet.
|
// If an "unavailable" packet.
|
||||||
else if (presence.getType() == Presence.Type.unavailable) {
|
case unavailable:
|
||||||
// If no resource, this is likely an offline presence as part of
|
// If no resource, this is likely an offline presence as part of
|
||||||
// a roster presence flood. In that case, we store it.
|
// a roster presence flood. In that case, we store it.
|
||||||
if ("".equals(XmppStringUtils.parseResource(from))) {
|
if ("".equals(XmppStringUtils.parseResource(from))) {
|
||||||
Map<String, Presence> userPresences;
|
|
||||||
// Get the user presence map
|
// Get the user presence map
|
||||||
if (presenceMap.get(key) == null) {
|
if (presenceMap.get(key) == null) {
|
||||||
userPresences = new ConcurrentHashMap<String, Presence>();
|
userPresences = new ConcurrentHashMap<String, Presence>();
|
||||||
|
@ -892,19 +894,18 @@ public class Roster {
|
||||||
}
|
}
|
||||||
// Otherwise, this is a normal offline presence.
|
// Otherwise, this is a normal offline presence.
|
||||||
else if (presenceMap.get(key) != null) {
|
else if (presenceMap.get(key) != null) {
|
||||||
Map<String, Presence> userPresences = presenceMap.get(key);
|
userPresences = presenceMap.get(key);
|
||||||
// Store the offline presence, as it may include extra information
|
// Store the offline presence, as it may include extra information
|
||||||
// such as the user being on vacation.
|
// such as the user being on vacation.
|
||||||
userPresences.put(XmppStringUtils.parseResource(from), presence);
|
userPresences.put(XmppStringUtils.parseResource(from), presence);
|
||||||
}
|
}
|
||||||
// If the user is in the roster, fire an event.
|
// If the user is in the roster, fire an event.
|
||||||
RosterEntry entry = entries.get(key);
|
entry = entries.get(key);
|
||||||
if (entry != null) {
|
if (entry != null) {
|
||||||
fireRosterPresenceEvent(presence);
|
fireRosterPresenceEvent(presence);
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
else if (presence.getType() == Presence.Type.subscribe) {
|
case subscribe:
|
||||||
Presence response = null;
|
|
||||||
switch (subscriptionMode) {
|
switch (subscriptionMode) {
|
||||||
case accept_all:
|
case accept_all:
|
||||||
// Accept all subscription requests.
|
// Accept all subscription requests.
|
||||||
|
@ -923,24 +924,24 @@ public class Roster {
|
||||||
response.setTo(presence.getFrom());
|
response.setTo(presence.getFrom());
|
||||||
connection.sendPacket(response);
|
connection.sendPacket(response);
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
else if (presence.getType() == Presence.Type.unsubscribe) {
|
case unsubscribe:
|
||||||
if (subscriptionMode != SubscriptionMode.manual) {
|
if (subscriptionMode != SubscriptionMode.manual) {
|
||||||
// Acknowledge and accept unsubscription notification so that the
|
// Acknowledge and accept unsubscription notification so that the
|
||||||
// server will stop sending notifications saying that the contact
|
// server will stop sending notifications saying that the contact
|
||||||
// has unsubscribed to our presence.
|
// has unsubscribed to our presence.
|
||||||
Presence response = new Presence(Presence.Type.unsubscribed);
|
response = new Presence(Presence.Type.unsubscribed);
|
||||||
response.setTo(presence.getFrom());
|
response.setTo(presence.getFrom());
|
||||||
connection.sendPacket(response);
|
connection.sendPacket(response);
|
||||||
}
|
}
|
||||||
// Otherwise, in manual mode so ignore.
|
// Otherwise, in manual mode so ignore.
|
||||||
}
|
break;
|
||||||
// Error presence packets from a bare JID mean we invalidate all existing
|
// Error presence packets from a bare JID mean we invalidate all existing
|
||||||
// presence info for the user.
|
// presence info for the user.
|
||||||
else if (presence.getType() == Presence.Type.error &&
|
case error:
|
||||||
"".equals(XmppStringUtils.parseResource(from)))
|
if (!"".equals(XmppStringUtils.parseResource(from))) {
|
||||||
{
|
break;
|
||||||
Map<String, Presence> userPresences;
|
}
|
||||||
if (!presenceMap.containsKey(key)) {
|
if (!presenceMap.containsKey(key)) {
|
||||||
userPresences = new ConcurrentHashMap<String, Presence>();
|
userPresences = new ConcurrentHashMap<String, Presence>();
|
||||||
presenceMap.put(key, userPresences);
|
presenceMap.put(key, userPresences);
|
||||||
|
@ -953,10 +954,13 @@ public class Roster {
|
||||||
// Set the new presence using the empty resource as a key.
|
// Set the new presence using the empty resource as a key.
|
||||||
userPresences.put("", presence);
|
userPresences.put("", presence);
|
||||||
// If the user is in the roster, fire an event.
|
// If the user is in the roster, fire an event.
|
||||||
RosterEntry entry = entries.get(key);
|
entry = entries.get(key);
|
||||||
if (entry != null) {
|
if (entry != null) {
|
||||||
fireRosterPresenceEvent(presence);
|
fireRosterPresenceEvent(presence);
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -159,7 +159,8 @@ public class MultiUserChat {
|
||||||
String from = presence.getFrom();
|
String from = presence.getFrom();
|
||||||
String myRoomJID = MultiUserChat.this.room + "/" + nickname;
|
String myRoomJID = MultiUserChat.this.room + "/" + nickname;
|
||||||
boolean isUserStatusModification = presence.getFrom().equals(myRoomJID);
|
boolean isUserStatusModification = presence.getFrom().equals(myRoomJID);
|
||||||
if (presence.getType() == Presence.Type.available) {
|
switch (presence.getType()) {
|
||||||
|
case available:
|
||||||
Presence oldPresence = occupantsMap.put(from, presence);
|
Presence oldPresence = occupantsMap.put(from, presence);
|
||||||
if (oldPresence != null) {
|
if (oldPresence != null) {
|
||||||
// Get the previous occupant's affiliation & role
|
// Get the previous occupant's affiliation & role
|
||||||
|
@ -187,8 +188,8 @@ public class MultiUserChat {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
else if (presence.getType() == Presence.Type.unavailable) {
|
case unavailable:
|
||||||
occupantsMap.remove(from);
|
occupantsMap.remove(from);
|
||||||
MUCUser mucUser = MUCUser.from(packet);
|
MUCUser mucUser = MUCUser.from(packet);
|
||||||
if (mucUser != null && mucUser.getStatus() != null) {
|
if (mucUser != null && mucUser.getStatus() != null) {
|
||||||
|
@ -206,6 +207,9 @@ public class MultiUserChat {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
for (PresenceListener listener : presenceListeners) {
|
for (PresenceListener listener : presenceListeners) {
|
||||||
listener.processPresence(presence);
|
listener.processPresence(presence);
|
||||||
|
|
Loading…
Reference in a new issue