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)));
}
switch (getMostAvailableStatus()) {
case Presences.CHAT:
case Presences.ONLINE:
case CHAT:
case ONLINE:
tags.add(new Tag("online", 0xff259b24));
break;
case Presences.AWAY:
case AWAY:
tags.add(new Tag("away", 0xffff9800));
break;
case Presences.XA:
case XA:
tags.add(new Tag("not available", 0xfff44336));
break;
case Presences.DND:
case DND:
tags.add(new Tag("dnd", 0xfff44336));
break;
}
@ -225,8 +225,8 @@ public class Contact implements ListItem, Blockable {
this.presences = pres;
}
public void updatePresence(final String resource, final int status) {
this.presences.updatePresence(resource, status);
public void updatePresence(final String resource, final Presence presence) {
this.presences.updatePresence(resource, presence);
}
public void removePresence(final String resource) {
@ -238,8 +238,13 @@ public class Contact implements ListItem, Blockable {
this.resetOption(Options.PENDING_SUBSCRIPTION_REQUEST);
}
public int getMostAvailableStatus() {
return this.presences.getMostAvailableStatus();
public Presence.Status getMostAvailableStatus() {
Presence p = this.presences.getMostAvailablePresence();
if (p == null) {
return Presence.Status.OFFLINE;
}
return p.getStatus();
}
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;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;
import eu.siacs.conversations.xml.Element;
public class Presences {
private final Hashtable<String, Presence> presences = new Hashtable<>();
public static final int CHAT = -1;
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() {
public Hashtable<String, Presence> getPresences() {
return this.presences;
}
public void updatePresence(String resource, int status) {
public void updatePresence(String resource, Presence presence) {
synchronized (this.presences) {
this.presences.put(resource, status);
this.presences.put(resource, presence);
}
}
@ -39,32 +31,10 @@ public class Presences {
}
}
public int getMostAvailableStatus() {
int status = OFFLINE;
public Presence getMostAvailablePresence() {
synchronized (this.presences) {
Iterator<Entry<String, Integer>> it = presences.entrySet().iterator();
while (it.hasNext()) {
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;
if (presences.size() < 1) { return null; }
return Collections.min(presences.values());
}
}

View file

@ -2,7 +2,7 @@ package eu.siacs.conversations.generator;
import eu.siacs.conversations.entities.Account;
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.xml.Element;
import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
@ -37,21 +37,10 @@ public class PresenceGenerator extends AbstractGenerator {
return subscription("subscribed", contact);
}
public PresencePacket selfPresence(Account account, int presence) {
public PresencePacket selfPresence(Account account, Presence.Status status) {
PresencePacket packet = new PresencePacket();
switch(presence) {
case Presences.AWAY:
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;
if(status.toShowString() != null) {
packet.addChild("show").setContent(status.toShowString());
}
packet.setFrom(account.getJid());
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.MucOptions;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.entities.Presence;
import eu.siacs.conversations.generator.PresenceGenerator;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xml.Element;
@ -176,7 +177,7 @@ public class PresenceParser extends AbstractParser implements
}
}
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();
Element x = packet.findChild("x", "jabber:x:signed");
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.MucOptions;
import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
import eu.siacs.conversations.entities.Presence;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.entities.Transferable;
import eu.siacs.conversations.entities.TransferablePlaceholder;
@ -596,13 +597,13 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
return getPreferences().getString("picture_compression", "auto");
}
private int getTargetPresence() {
private Presence.Status getTargetPresence() {
if (xaOnSilentMode() && isPhoneSilenced()) {
return Presences.XA;
return Presence.Status.XA;
} else if (awayWhenScreenOff() && !isInteractive()) {
return Presences.AWAY;
return Presence.Status.AWAY;
} 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.Message;
import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.entities.Presence;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.entities.Transferable;
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}
private int getSendButtonImageResource(SendButtonAction action, int status) {
private int getSendButtonImageResource(SendButtonAction action, Presence.Status status) {
switch (action) {
case TEXT:
switch (status) {
case Presences.CHAT:
case Presences.ONLINE:
case CHAT:
case ONLINE:
return R.drawable.ic_send_text_online;
case Presences.AWAY:
case AWAY:
return R.drawable.ic_send_text_away;
case Presences.XA:
case Presences.DND:
case XA:
case DND:
return R.drawable.ic_send_text_dnd;
default:
return R.drawable.ic_send_text_offline;
}
case TAKE_PHOTO:
switch (status) {
case Presences.CHAT:
case Presences.ONLINE:
case CHAT:
case ONLINE:
return R.drawable.ic_send_photo_online;
case Presences.AWAY:
case AWAY:
return R.drawable.ic_send_photo_away;
case Presences.XA:
case Presences.DND:
case XA:
case DND:
return R.drawable.ic_send_photo_dnd;
default:
return R.drawable.ic_send_photo_offline;
}
case RECORD_VOICE:
switch (status) {
case Presences.CHAT:
case Presences.ONLINE:
case CHAT:
case ONLINE:
return R.drawable.ic_send_voice_online;
case Presences.AWAY:
case AWAY:
return R.drawable.ic_send_voice_away;
case Presences.XA:
case Presences.DND:
case XA:
case DND:
return R.drawable.ic_send_voice_dnd;
default:
return R.drawable.ic_send_voice_offline;
}
case SEND_LOCATION:
switch (status) {
case Presences.CHAT:
case Presences.ONLINE:
case CHAT:
case ONLINE:
return R.drawable.ic_send_location_online;
case Presences.AWAY:
case AWAY:
return R.drawable.ic_send_location_away;
case Presences.XA:
case Presences.DND:
case XA:
case DND:
return R.drawable.ic_send_location_dnd;
default:
return R.drawable.ic_send_location_offline;
}
case CANCEL:
switch (status) {
case Presences.CHAT:
case Presences.ONLINE:
case CHAT:
case ONLINE:
return R.drawable.ic_send_cancel_online;
case Presences.AWAY:
case AWAY:
return R.drawable.ic_send_cancel_away;
case Presences.XA:
case Presences.DND:
case XA:
case DND:
return R.drawable.ic_send_cancel_dnd;
default:
return R.drawable.ic_send_cancel_offline;
}
case CHOOSE_PICTURE:
switch (status) {
case Presences.CHAT:
case Presences.ONLINE:
case CHAT:
case ONLINE:
return R.drawable.ic_send_picture_online;
case Presences.AWAY:
case AWAY:
return R.drawable.ic_send_picture_away;
case Presences.XA:
case Presences.DND:
case XA:
case DND:
return R.drawable.ic_send_picture_dnd;
default:
return R.drawable.ic_send_picture_offline;
@ -946,7 +947,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
public void updateSendButton() {
final Conversation c = this.conversation;
final SendButtonAction action;
final int status;
final Presence.Status status;
final boolean empty = this.mEditMessage == null || this.mEditMessage.getText().length() == 0;
final boolean conference = c.getMode() == Conversation.MODE_MULTI;
if (conference && !c.getAccount().httpUploadAvailable()) {
@ -993,10 +994,10 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
if (c.getMode() == Conversation.MODE_SINGLE) {
status = c.getContact().getMostAvailableStatus();
} else {
status = c.getMucOptions().online() ? Presences.ONLINE : Presences.OFFLINE;
status = c.getMucOptions().online() ? Presence.Status.ONLINE : Presence.Status.OFFLINE;
}
} else {
status = Presences.OFFLINE;
status = Presence.Status.OFFLINE;
}
this.mSendButton.setTag(action);
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.Conversation;
import eu.siacs.conversations.entities.ListItem;
import eu.siacs.conversations.entities.Presence;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
@ -725,9 +726,11 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
for (Account account : xmppConnectionService.getAccounts()) {
if (account.getStatus() != Account.State.DISABLED) {
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)
&& (!this.mHideOfflineContacts
|| contact.getPresences().getMostAvailableStatus() < Presences.OFFLINE)) {
|| s.compareTo(Presence.Status.OFFLINE) < 0)) {
this.contacts.add(contact);
}
}