diff options
Diffstat (limited to 'src/main/java/eu/siacs/conversations/entities/Contact.java')
-rw-r--r-- | src/main/java/eu/siacs/conversations/entities/Contact.java | 244 |
1 files changed, 125 insertions, 119 deletions
diff --git a/src/main/java/eu/siacs/conversations/entities/Contact.java b/src/main/java/eu/siacs/conversations/entities/Contact.java index 6046abef..99a74907 100644 --- a/src/main/java/eu/siacs/conversations/entities/Contact.java +++ b/src/main/java/eu/siacs/conversations/entities/Contact.java @@ -8,9 +8,7 @@ import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.xml.Element; @@ -29,10 +27,10 @@ public class Contact implements ListItem { public static final String KEYS = "pgpkey"; public static final String ACCOUNT = "accountUuid"; public static final String AVATAR = "avatar"; - public static final String LAST_PRESENCE = "last_presence"; - public static final String LAST_TIME = "last_time"; + public static final String LAST_PRESENCE = "last_presence"; + public static final String LAST_TIME = "last_time"; public static final String GROUPS = "groups"; - + public Lastseen lastseen = new Lastseen(); protected String accountUuid; protected String systemName; protected String serverName; @@ -45,14 +43,11 @@ public class Contact implements ListItem { protected JSONObject keys = new JSONObject(); protected JSONArray groups = new JSONArray(); protected Presences presences = new Presences(); - protected Account account; - public Lastseen lastseen = new Lastseen(); - public Contact(final String account, final String systemName, final String serverName, - final Jid jid, final int subscription, final String photoUri, - final String systemAccount, final String keys, final String avatar, final Lastseen lastseen, final String groups) { + final Jid jid, final int subscription, final String photoUri, + final String systemAccount, final String keys, final String avatar, final Lastseen lastseen, final String groups) { this.accountUuid = account; this.systemName = systemName; this.serverName = serverName; @@ -78,18 +73,42 @@ public class Contact implements ListItem { this.jid = jid; } + public static Contact fromCursor(final Cursor cursor) { + final Lastseen lastseen = new Lastseen( + cursor.getString(cursor.getColumnIndex(LAST_PRESENCE)), + cursor.getLong(cursor.getColumnIndex(LAST_TIME))); + final Jid jid; + try { + jid = Jid.fromString(cursor.getString(cursor.getColumnIndex(JID))); + } catch (final InvalidJidException e) { + // TODO: Borked DB... handle this somehow? + return null; + } + return new Contact(cursor.getString(cursor.getColumnIndex(ACCOUNT)), + cursor.getString(cursor.getColumnIndex(SYSTEMNAME)), + cursor.getString(cursor.getColumnIndex(SERVERNAME)), + jid, + cursor.getInt(cursor.getColumnIndex(OPTIONS)), + cursor.getString(cursor.getColumnIndex(PHOTOURI)), + cursor.getString(cursor.getColumnIndex(SYSTEMACCOUNT)), + cursor.getString(cursor.getColumnIndex(KEYS)), + cursor.getString(cursor.getColumnIndex(AVATAR)), + lastseen, + cursor.getString(cursor.getColumnIndex(GROUPS))); + } + public String getDisplayName() { if (this.systemName != null) { - return this.systemName; - } else if (this.serverName != null) { - return this.serverName; + return this.systemName; + } else if (this.serverName != null) { + return this.serverName; } else if (this.presenceName != null) { - return this.presenceName; + return this.presenceName; } else if (jid.hasLocalpart()) { - return jid.getLocalpart(); + return jid.getLocalpart(); } else { - return jid.getDomainpart(); - } + return jid.getDomainpart(); + } } public String getProfilePhoto() { @@ -103,33 +122,43 @@ public class Contact implements ListItem { @Override public List<Tag> getTags() { ArrayList<Tag> tags = new ArrayList<Tag>(); - for(String group : getGroups()) { + for (String group : getGroups()) { tags.add(new Tag(group, UIHelper.getColorForName(group))); } int status = getMostAvailableStatus(); switch (getMostAvailableStatus()) { case Presences.CHAT: case Presences.ONLINE: - tags.add(new Tag("online",0xff259b24)); + tags.add(new Tag("online", 0xff259b24)); break; case Presences.AWAY: - tags.add(new Tag("away",0xffff9800)); + tags.add(new Tag("away", 0xffff9800)); break; case Presences.XA: - tags.add(new Tag("not available",0xffe51c23)); + tags.add(new Tag("not available", 0xffe51c23)); break; case Presences.DND: - tags.add(new Tag("dnd",0xffe51c23)); + tags.add(new Tag("dnd", 0xffe51c23)); break; } return tags; } public boolean match(String needle) { - return needle == null - || jid.toString().contains(needle.toLowerCase()) - || getDisplayName().toLowerCase() - .contains(needle.toLowerCase()); + if (needle == null) { + return true; + } + needle = needle.toLowerCase(); + return jid.toString().contains(needle) || getDisplayName().toLowerCase().contains(needle) || matchInTag(needle); + } + + private boolean matchInTag(String needle) { + for (Tag tag : getTags()) { + if (tag.getName().toLowerCase().contains(needle)) { + return true; + } + } + return false; } public ContentValues getContentValues() { @@ -145,40 +174,16 @@ public class Contact implements ListItem { values.put(AVATAR, avatar); values.put(LAST_PRESENCE, lastseen.presence); values.put(LAST_TIME, lastseen.time); - values.put(GROUPS,groups.toString()); + values.put(GROUPS, groups.toString()); return values; } - public static Contact fromCursor(final Cursor cursor) { - final Lastseen lastseen = new Lastseen( - cursor.getString(cursor.getColumnIndex(LAST_PRESENCE)), - cursor.getLong(cursor.getColumnIndex(LAST_TIME))); - final Jid jid; - try { - jid = Jid.fromString(cursor.getString(cursor.getColumnIndex(JID))); - } catch (final InvalidJidException e) { - // TODO: Borked DB... handle this somehow? - return null; - } - return new Contact(cursor.getString(cursor.getColumnIndex(ACCOUNT)), - cursor.getString(cursor.getColumnIndex(SYSTEMNAME)), - cursor.getString(cursor.getColumnIndex(SERVERNAME)), - jid, - cursor.getInt(cursor.getColumnIndex(OPTIONS)), - cursor.getString(cursor.getColumnIndex(PHOTOURI)), - cursor.getString(cursor.getColumnIndex(SYSTEMACCOUNT)), - cursor.getString(cursor.getColumnIndex(KEYS)), - cursor.getString(cursor.getColumnIndex(AVATAR)), - lastseen, - cursor.getString(cursor.getColumnIndex(GROUPS))); - } - public int getSubscription() { return this.subscription; } - public void setSystemAccount(String account) { - this.systemAccount = account; + public Account getAccount() { + return this.account; } public void setAccount(Account account) { @@ -186,14 +191,14 @@ public class Contact implements ListItem { this.accountUuid = account.getUuid(); } - public Account getAccount() { - return this.account; - } - public Presences getPresences() { return this.presences; } + public void setPresences(Presences pres) { + this.presences = pres; + } + public void updatePresence(String resource, int status) { this.presences.updatePresence(resource, status); } @@ -211,10 +216,6 @@ public class Contact implements ListItem { return this.presences.getMostAvailableStatus(); } - public void setPresences(Presences pres) { - this.presences = pres; - } - public void setPhotoUri(String uri) { this.photoUri = uri; } @@ -235,9 +236,13 @@ public class Contact implements ListItem { return systemAccount; } + public void setSystemAccount(String account) { + this.systemAccount = account; + } + public List<String> getGroups() { ArrayList<String> groups = new ArrayList<String>(); - for(int i = 0; i < this.groups.length(); ++i) { + for (int i = 0; i < this.groups.length(); ++i) { try { groups.add(this.groups.getString(i)); } catch (final JSONException ignored) { @@ -282,14 +287,6 @@ public class Contact implements ListItem { } } - public void setPgpKeyId(long keyId) { - try { - this.keys.put("pgp_keyid", keyId); - } catch (final JSONException ignored) { - - } - } - public long getPgpKeyId() { if (this.keys.has("pgp_keyid")) { try { @@ -302,6 +299,14 @@ public class Contact implements ListItem { } } + public void setPgpKeyId(long keyId) { + try { + this.keys.put("pgp_keyid", keyId); + } catch (final JSONException ignored) { + + } + } + public void setOption(int option) { this.subscription |= 1 << option; } @@ -325,26 +330,26 @@ public class Contact implements ListItem { String subscription = item.getAttribute("subscription"); if (subscription != null) { - switch (subscription) { - case "to": - this.resetOption(Options.FROM); - this.setOption(Options.TO); - break; - case "from": - this.resetOption(Options.TO); - this.setOption(Options.FROM); - this.resetOption(Options.PREEMPTIVE_GRANT); - break; - case "both": - this.setOption(Options.TO); - this.setOption(Options.FROM); - this.resetOption(Options.PREEMPTIVE_GRANT); - break; - case "none": - this.resetOption(Options.FROM); - this.resetOption(Options.TO); - break; - } + switch (subscription) { + case "to": + this.resetOption(Options.FROM); + this.setOption(Options.TO); + break; + case "from": + this.resetOption(Options.TO); + this.setOption(Options.FROM); + this.resetOption(Options.PREEMPTIVE_GRANT); + break; + case "both": + this.setOption(Options.TO); + this.setOption(Options.FROM); + this.resetOption(Options.PREEMPTIVE_GRANT); + break; + case "none": + this.resetOption(Options.FROM); + this.resetOption(Options.TO); + break; + } } // do NOT override asking if pending push request @@ -359,7 +364,7 @@ public class Contact implements ListItem { public void parseGroupsFromElement(Element item) { this.groups = new JSONArray(); - for(Element element : item.getChildren()) { + for (Element element : item.getChildren()) { if (element.getName().equals("group") && element.getContent() != null) { this.groups.put(element.getContent()); } @@ -372,37 +377,12 @@ public class Contact implements ListItem { if (this.serverName != null) { item.setAttribute("name", this.serverName); } - for(String group : getGroups()) { + for (String group : getGroups()) { item.addChild("group").setContent(group); } return item; } - public class Options { - public static final int TO = 0; - public static final int FROM = 1; - public static final int ASKING = 2; - public static final int PREEMPTIVE_GRANT = 3; - public static final int IN_ROSTER = 4; - public static final int PENDING_SUBSCRIPTION_REQUEST = 5; - public static final int DIRTY_PUSH = 6; - public static final int DIRTY_DELETE = 7; - } - - public static class Lastseen { - public long time; - public String presence; - - public Lastseen() { - time = 0; - presence = null; - } - public Lastseen(final String presence, final long time) { - this.time = time; - this.presence = presence; - } - } - @Override public int compareTo(final ListItem another) { return this.getDisplayName().compareToIgnoreCase( @@ -455,9 +435,35 @@ public class Contact implements ListItem { public String getShareableUri() { if (getOtrFingerprints().size() >= 1) { String otr = getOtrFingerprints().get(0); - return "xmpp:"+getJid().toBareJid().toString()+"?otr-fingerprint="+otr.replace(" ",""); + return "xmpp:" + getJid().toBareJid().toString() + "?otr-fingerprint=" + otr.replace(" ", ""); } else { - return "xmpp:"+getJid().toBareJid().toString(); + return "xmpp:" + getJid().toBareJid().toString(); } } + + public static class Lastseen { + public long time; + public String presence; + + public Lastseen() { + time = 0; + presence = null; + } + + public Lastseen(final String presence, final long time) { + this.time = time; + this.presence = presence; + } + } + + public class Options { + public static final int TO = 0; + public static final int FROM = 1; + public static final int ASKING = 2; + public static final int PREEMPTIVE_GRANT = 3; + public static final int IN_ROSTER = 4; + public static final int PENDING_SUBSCRIPTION_REQUEST = 5; + public static final int DIRTY_PUSH = 6; + public static final int DIRTY_DELETE = 7; + } } |