Use a Presence class for presence information

Only has status for now, but doing it so I can add disco to it
This commit is contained in:
Stephen Paul Weber 2016-01-17 16:28:38 -05:00
parent ad36a4ba89
commit bf5b2f73f5
8 changed files with 121 additions and 102 deletions

View File

@ -135,17 +135,17 @@ public class Contact implements ListItem, Blockable {
tags.add(new Tag(group, UIHelper.getColorForName(group))); tags.add(new Tag(group, UIHelper.getColorForName(group)));
} }
switch (getMostAvailableStatus()) { switch (getMostAvailableStatus()) {
case Presences.CHAT: case CHAT:
case Presences.ONLINE: case ONLINE:
tags.add(new Tag("online", 0xff259b24)); tags.add(new Tag("online", 0xff259b24));
break; break;
case Presences.AWAY: case AWAY:
tags.add(new Tag("away", 0xffff9800)); tags.add(new Tag("away", 0xffff9800));
break; break;
case Presences.XA: case XA:
tags.add(new Tag("not available", 0xfff44336)); tags.add(new Tag("not available", 0xfff44336));
break; break;
case Presences.DND: case DND:
tags.add(new Tag("dnd", 0xfff44336)); tags.add(new Tag("dnd", 0xfff44336));
break; break;
} }
@ -225,8 +225,8 @@ public class Contact implements ListItem, Blockable {
this.presences = pres; this.presences = pres;
} }
public void updatePresence(final String resource, final int status) { public void updatePresence(final String resource, final Presence presence) {
this.presences.updatePresence(resource, status); this.presences.updatePresence(resource, presence);
} }
public void removePresence(final String resource) { public void removePresence(final String resource) {
@ -238,8 +238,13 @@ public class Contact implements ListItem, Blockable {
this.resetOption(Options.PENDING_SUBSCRIPTION_REQUEST); this.resetOption(Options.PENDING_SUBSCRIPTION_REQUEST);
} }
public int getMostAvailableStatus() { public Presence.Status getMostAvailableStatus() {
return this.presences.getMostAvailableStatus(); Presence p = this.presences.getMostAvailablePresence();
if (p == null) {
return Presence.Status.OFFLINE;
}
return p.getStatus();
} }
public boolean setPhotoUri(String uri) { public boolean setPhotoUri(String uri) {

View File

@ -0,0 +1,49 @@
package eu.siacs.conversations.entities;
import java.lang.Comparable;
import eu.siacs.conversations.xml.Element;
public class Presence implements Comparable {
public enum Status {
CHAT, ONLINE, AWAY, XA, DND, OFFLINE;
public String toShowString() {
switch(this) {
case CHAT: return "chat";
case AWAY: return "away";
case XA: return "xa";
case DND: return "dnd";
}
return null;
}
}
protected final Status status;
public Presence(Element show) {
if ((show == null) || (show.getContent() == null)) {
this.status = Status.ONLINE;
} else if (show.getContent().equals("away")) {
this.status = Status.AWAY;
} else if (show.getContent().equals("xa")) {
this.status = Status.XA;
} else if (show.getContent().equals("chat")) {
this.status = Status.CHAT;
} else if (show.getContent().equals("dnd")) {
this.status = Status.DND;
} else {
this.status = Status.OFFLINE;
}
}
public int compareTo(Object other) {
return this.status.compareTo(((Presence)other).status);
}
public Status getStatus() {
return this.status;
}
}

View File

@ -1,29 +1,21 @@
package eu.siacs.conversations.entities; package eu.siacs.conversations.entities;
import java.util.Collections;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map.Entry;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
public class Presences { public class Presences {
private final Hashtable<String, Presence> presences = new Hashtable<>();
public static final int CHAT = -1; public Hashtable<String, Presence> getPresences() {
public static final int ONLINE = 0;
public static final int AWAY = 1;
public static final int XA = 2;
public static final int DND = 3;
public static final int OFFLINE = 4;
private final Hashtable<String, Integer> presences = new Hashtable<>();
public Hashtable<String, Integer> getPresences() {
return this.presences; return this.presences;
} }
public void updatePresence(String resource, int status) { public void updatePresence(String resource, Presence presence) {
synchronized (this.presences) { synchronized (this.presences) {
this.presences.put(resource, status); this.presences.put(resource, presence);
} }
} }
@ -39,32 +31,10 @@ public class Presences {
} }
} }
public int getMostAvailableStatus() { public Presence getMostAvailablePresence() {
int status = OFFLINE;
synchronized (this.presences) { synchronized (this.presences) {
Iterator<Entry<String, Integer>> it = presences.entrySet().iterator(); if (presences.size() < 1) { return null; }
while (it.hasNext()) { return Collections.min(presences.values());
Entry<String, Integer> entry = it.next();
if (entry.getValue() < status)
status = entry.getValue();
}
}
return status;
}
public static int parseShow(Element show) {
if ((show == null) || (show.getContent() == null)) {
return Presences.ONLINE;
} else if (show.getContent().equals("away")) {
return Presences.AWAY;
} else if (show.getContent().equals("xa")) {
return Presences.XA;
} else if (show.getContent().equals("chat")) {
return Presences.CHAT;
} else if (show.getContent().equals("dnd")) {
return Presences.DND;
} else {
return Presences.OFFLINE;
} }
} }

View File

@ -2,7 +2,7 @@ package eu.siacs.conversations.generator;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Presences; import eu.siacs.conversations.entities.Presence;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.stanzas.PresencePacket; import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
@ -37,21 +37,10 @@ public class PresenceGenerator extends AbstractGenerator {
return subscription("subscribed", contact); return subscription("subscribed", contact);
} }
public PresencePacket selfPresence(Account account, int presence) { public PresencePacket selfPresence(Account account, Presence.Status status) {
PresencePacket packet = new PresencePacket(); PresencePacket packet = new PresencePacket();
switch(presence) { if(status.toShowString() != null) {
case Presences.AWAY: packet.addChild("show").setContent(status.toShowString());
packet.addChild("show").setContent("away");
break;
case Presences.XA:
packet.addChild("show").setContent("xa");
break;
case Presences.CHAT:
packet.addChild("show").setContent("chat");
break;
case Presences.DND:
packet.addChild("show").setContent("dnd");
break;
} }
packet.setFrom(account.getJid()); packet.setFrom(account.getJid());
String sig = account.getPgpSignature(); String sig = account.getPgpSignature();

View File

@ -10,6 +10,7 @@ import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.MucOptions; import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.entities.Presences; import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.entities.Presence;
import eu.siacs.conversations.generator.PresenceGenerator; import eu.siacs.conversations.generator.PresenceGenerator;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
@ -176,7 +177,7 @@ public class PresenceParser extends AbstractParser implements
} }
} }
int sizeBefore = contact.getPresences().size(); int sizeBefore = contact.getPresences().size();
contact.updatePresence(presence, Presences.parseShow(packet.findChild("show"))); contact.updatePresence(presence, new Presence(packet.findChild("show")));
PgpEngine pgp = mXmppConnectionService.getPgpEngine(); PgpEngine pgp = mXmppConnectionService.getPgpEngine();
Element x = packet.findChild("x", "jabber:x:signed"); Element x = packet.findChild("x", "jabber:x:signed");
if (pgp != null && x != null) { if (pgp != null && x != null) {

View File

@ -72,6 +72,7 @@ import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.MucOptions; import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.entities.MucOptions.OnRenameListener; import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
import eu.siacs.conversations.entities.Presence;
import eu.siacs.conversations.entities.Presences; import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.entities.Transferable; import eu.siacs.conversations.entities.Transferable;
import eu.siacs.conversations.entities.TransferablePlaceholder; import eu.siacs.conversations.entities.TransferablePlaceholder;
@ -596,13 +597,13 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
return getPreferences().getString("picture_compression", "auto"); return getPreferences().getString("picture_compression", "auto");
} }
private int getTargetPresence() { private Presence.Status getTargetPresence() {
if (xaOnSilentMode() && isPhoneSilenced()) { if (xaOnSilentMode() && isPhoneSilenced()) {
return Presences.XA; return Presence.Status.XA;
} else if (awayWhenScreenOff() && !isInteractive()) { } else if (awayWhenScreenOff() && !isInteractive()) {
return Presences.AWAY; return Presence.Status.AWAY;
} else { } else {
return Presences.ONLINE; return Presence.Status.ONLINE;
} }
} }

View File

@ -53,6 +53,7 @@ import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.DownloadableFile; import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.MucOptions; import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.entities.Presence;
import eu.siacs.conversations.entities.Presences; import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.entities.Transferable; import eu.siacs.conversations.entities.Transferable;
import eu.siacs.conversations.entities.TransferablePlaceholder; import eu.siacs.conversations.entities.TransferablePlaceholder;
@ -859,82 +860,82 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
enum SendButtonAction {TEXT, TAKE_PHOTO, SEND_LOCATION, RECORD_VOICE, CANCEL, CHOOSE_PICTURE} enum SendButtonAction {TEXT, TAKE_PHOTO, SEND_LOCATION, RECORD_VOICE, CANCEL, CHOOSE_PICTURE}
private int getSendButtonImageResource(SendButtonAction action, int status) { private int getSendButtonImageResource(SendButtonAction action, Presence.Status status) {
switch (action) { switch (action) {
case TEXT: case TEXT:
switch (status) { switch (status) {
case Presences.CHAT: case CHAT:
case Presences.ONLINE: case ONLINE:
return R.drawable.ic_send_text_online; return R.drawable.ic_send_text_online;
case Presences.AWAY: case AWAY:
return R.drawable.ic_send_text_away; return R.drawable.ic_send_text_away;
case Presences.XA: case XA:
case Presences.DND: case DND:
return R.drawable.ic_send_text_dnd; return R.drawable.ic_send_text_dnd;
default: default:
return R.drawable.ic_send_text_offline; return R.drawable.ic_send_text_offline;
} }
case TAKE_PHOTO: case TAKE_PHOTO:
switch (status) { switch (status) {
case Presences.CHAT: case CHAT:
case Presences.ONLINE: case ONLINE:
return R.drawable.ic_send_photo_online; return R.drawable.ic_send_photo_online;
case Presences.AWAY: case AWAY:
return R.drawable.ic_send_photo_away; return R.drawable.ic_send_photo_away;
case Presences.XA: case XA:
case Presences.DND: case DND:
return R.drawable.ic_send_photo_dnd; return R.drawable.ic_send_photo_dnd;
default: default:
return R.drawable.ic_send_photo_offline; return R.drawable.ic_send_photo_offline;
} }
case RECORD_VOICE: case RECORD_VOICE:
switch (status) { switch (status) {
case Presences.CHAT: case CHAT:
case Presences.ONLINE: case ONLINE:
return R.drawable.ic_send_voice_online; return R.drawable.ic_send_voice_online;
case Presences.AWAY: case AWAY:
return R.drawable.ic_send_voice_away; return R.drawable.ic_send_voice_away;
case Presences.XA: case XA:
case Presences.DND: case DND:
return R.drawable.ic_send_voice_dnd; return R.drawable.ic_send_voice_dnd;
default: default:
return R.drawable.ic_send_voice_offline; return R.drawable.ic_send_voice_offline;
} }
case SEND_LOCATION: case SEND_LOCATION:
switch (status) { switch (status) {
case Presences.CHAT: case CHAT:
case Presences.ONLINE: case ONLINE:
return R.drawable.ic_send_location_online; return R.drawable.ic_send_location_online;
case Presences.AWAY: case AWAY:
return R.drawable.ic_send_location_away; return R.drawable.ic_send_location_away;
case Presences.XA: case XA:
case Presences.DND: case DND:
return R.drawable.ic_send_location_dnd; return R.drawable.ic_send_location_dnd;
default: default:
return R.drawable.ic_send_location_offline; return R.drawable.ic_send_location_offline;
} }
case CANCEL: case CANCEL:
switch (status) { switch (status) {
case Presences.CHAT: case CHAT:
case Presences.ONLINE: case ONLINE:
return R.drawable.ic_send_cancel_online; return R.drawable.ic_send_cancel_online;
case Presences.AWAY: case AWAY:
return R.drawable.ic_send_cancel_away; return R.drawable.ic_send_cancel_away;
case Presences.XA: case XA:
case Presences.DND: case DND:
return R.drawable.ic_send_cancel_dnd; return R.drawable.ic_send_cancel_dnd;
default: default:
return R.drawable.ic_send_cancel_offline; return R.drawable.ic_send_cancel_offline;
} }
case CHOOSE_PICTURE: case CHOOSE_PICTURE:
switch (status) { switch (status) {
case Presences.CHAT: case CHAT:
case Presences.ONLINE: case ONLINE:
return R.drawable.ic_send_picture_online; return R.drawable.ic_send_picture_online;
case Presences.AWAY: case AWAY:
return R.drawable.ic_send_picture_away; return R.drawable.ic_send_picture_away;
case Presences.XA: case XA:
case Presences.DND: case DND:
return R.drawable.ic_send_picture_dnd; return R.drawable.ic_send_picture_dnd;
default: default:
return R.drawable.ic_send_picture_offline; return R.drawable.ic_send_picture_offline;
@ -946,7 +947,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
public void updateSendButton() { public void updateSendButton() {
final Conversation c = this.conversation; final Conversation c = this.conversation;
final SendButtonAction action; final SendButtonAction action;
final int status; final Presence.Status status;
final boolean empty = this.mEditMessage == null || this.mEditMessage.getText().length() == 0; final boolean empty = this.mEditMessage == null || this.mEditMessage.getText().length() == 0;
final boolean conference = c.getMode() == Conversation.MODE_MULTI; final boolean conference = c.getMode() == Conversation.MODE_MULTI;
if (conference && !c.getAccount().httpUploadAvailable()) { if (conference && !c.getAccount().httpUploadAvailable()) {
@ -993,10 +994,10 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
if (c.getMode() == Conversation.MODE_SINGLE) { if (c.getMode() == Conversation.MODE_SINGLE) {
status = c.getContact().getMostAvailableStatus(); status = c.getContact().getMostAvailableStatus();
} else { } else {
status = c.getMucOptions().online() ? Presences.ONLINE : Presences.OFFLINE; status = c.getMucOptions().online() ? Presence.Status.ONLINE : Presence.Status.OFFLINE;
} }
} else { } else {
status = Presences.OFFLINE; status = Presence.Status.OFFLINE;
} }
this.mSendButton.setTag(action); this.mSendButton.setTag(action);
this.mSendButton.setImageResource(getSendButtonImageResource(action, status)); this.mSendButton.setImageResource(getSendButtonImageResource(action, status));

View File

@ -63,6 +63,7 @@ import eu.siacs.conversations.entities.Bookmark;
import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.ListItem; import eu.siacs.conversations.entities.ListItem;
import eu.siacs.conversations.entities.Presence;
import eu.siacs.conversations.entities.Presences; import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate; import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
import eu.siacs.conversations.ui.adapter.KnownHostsAdapter; import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
@ -725,9 +726,11 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
for (Account account : xmppConnectionService.getAccounts()) { for (Account account : xmppConnectionService.getAccounts()) {
if (account.getStatus() != Account.State.DISABLED) { if (account.getStatus() != Account.State.DISABLED) {
for (Contact contact : account.getRoster().getContacts()) { for (Contact contact : account.getRoster().getContacts()) {
Presence p = contact.getPresences().getMostAvailablePresence();
Presence.Status s = p == null ? Presence.Status.OFFLINE : p.getStatus();
if (contact.showInRoster() && contact.match(needle) if (contact.showInRoster() && contact.match(needle)
&& (!this.mHideOfflineContacts && (!this.mHideOfflineContacts
|| contact.getPresences().getMostAvailableStatus() < Presences.OFFLINE)) { || s.compareTo(Presence.Status.OFFLINE) < 0)) {
this.contacts.add(contact); this.contacts.add(contact);
} }
} }