From fccce229c6a75e3a0b4845199323a00cadf21ac7 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Sun, 10 Jan 2016 14:56:55 -0500 Subject: Factor out a representation of XEP-0030 results And the parser from Element to this representation. --- .../entities/ServiceDiscoveryResult.java | 84 ++++++++++++++++++++++ .../siacs/conversations/xmpp/XmppConnection.java | 77 ++++++++------------ 2 files changed, 114 insertions(+), 47 deletions(-) create mode 100644 src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java diff --git a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java new file mode 100644 index 00000000..96346440 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java @@ -0,0 +1,84 @@ +package eu.siacs.conversations.entities; + +import java.util.List; +import java.util.ArrayList; + +import eu.siacs.conversations.xml.Element; +import eu.siacs.conversations.xmpp.stanzas.IqPacket; + +public class ServiceDiscoveryResult { + public static class Identity { + protected final String category; + protected final String type; + protected final String name; + + public Identity(final String category, final String type, final String name) { + this.category = category; + this.type = type; + this.name = name; + } + + public Identity(final Element el) { + this.category = el.getAttribute("category"); + this.type = el.getAttribute("type"); + this.name = el.getAttribute("name"); + } + + public String getCategory() { + return this.category; + } + + public String getType() { + return this.type; + } + + public String getName() { + return this.name; + } + } + + protected final List identities; + protected final List features; + + public ServiceDiscoveryResult(final List identities, final List features) { + this.identities = identities; + this.features = features; + } + + public ServiceDiscoveryResult(final IqPacket packet) { + this.identities = new ArrayList<>(); + this.features = new ArrayList<>(); + + final List elements = packet.query().getChildren(); + + for (final Element element : elements) { + if (element.getName().equals("identity")) { + Identity id = new Identity(element); + if (id.getType() != null && id.getCategory() != null) { + identities.add(id); + } + } else if (element.getName().equals("feature")) { + features.add(element.getAttribute("var")); + } + } + } + + public List getIdentities() { + return this.identities; + } + + public List getFeatures() { + return this.features; + } + + public boolean hasIdentity(String category, String type) { + for(Identity id : this.getIdentities()) { + if((category == null || id.getCategory().equals(category)) && + (type == null || id.getType().equals(type))) { + return true; + } + } + + return false; + } +} diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index a3dd0644..556688fd 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -58,6 +58,7 @@ import eu.siacs.conversations.crypto.sasl.SaslMechanism; import eu.siacs.conversations.crypto.sasl.ScramSha1; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Message; +import eu.siacs.conversations.entities.ServiceDiscoveryResult; import eu.siacs.conversations.generator.IqGenerator; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.utils.CryptoHelper; @@ -101,7 +102,7 @@ public class XmppConnection implements Runnable { private boolean needsBinding = true; private boolean shouldAuthenticate = true; private Element streamFeatures; - private final HashMap disco = new HashMap<>(); + private final HashMap disco = new HashMap<>(); private String streamId = null; private int smVersion = 3; @@ -1031,39 +1032,26 @@ public class XmppConnection implements Runnable { if (packet.getType() == IqPacket.TYPE.RESULT) { boolean advancedStreamFeaturesLoaded; synchronized (XmppConnection.this.disco) { - final List elements = packet.query().getChildren(); - final Info info = new Info(); - for (final Element element : elements) { - if (element.getName().equals("identity")) { - String type = element.getAttribute("type"); - String category = element.getAttribute("category"); - String name = element.getAttribute("name"); - if (type != null && category != null) { - info.identities.add(new Pair<>(category, type)); - if (mServerIdentity == Identity.UNKNOWN - && type.equals("im") - && category.equals("server")) { - if (name != null && jid.equals(account.getServer())) { - switch (name) { - case "Prosody": - mServerIdentity = Identity.PROSODY; - break; - case "ejabberd": - mServerIdentity = Identity.EJABBERD; - break; - case "Slack-XMPP": - mServerIdentity = Identity.SLACK; - break; - } - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server name: " + name); - } + ServiceDiscoveryResult result = new ServiceDiscoveryResult(packet); + for (final ServiceDiscoveryResult.Identity id : result.getIdentities()) { + if (mServerIdentity == Identity.UNKNOWN && id.getType().equals("im") && + id.getCategory().equals("server") && id.getName() != null && + jid.equals(account.getServer())) { + switch (id.getName()) { + case "Prosody": + mServerIdentity = Identity.PROSODY; + break; + case "ejabberd": + mServerIdentity = Identity.EJABBERD; + break; + case "Slack-XMPP": + mServerIdentity = Identity.SLACK; + break; } + Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server name: " + id.getName()); } - } else if (element.getName().equals("feature")) { - info.features.add(element.getAttribute("var")); - } } - disco.put(jid, info); + disco.put(jid, result); advancedStreamFeaturesLoaded = disco.containsKey(account.getServer()) && disco.containsKey(account.getJid().toBareJid()); } @@ -1316,8 +1304,8 @@ public class XmppConnection implements Runnable { public List findDiscoItemsByFeature(final String feature) { synchronized (this.disco) { final List items = new ArrayList<>(); - for (final Entry cursor : this.disco.entrySet()) { - if (cursor.getValue().features.contains(feature)) { + for (final Entry cursor : this.disco.entrySet()) { + if (cursor.getValue().getFeatures().contains(feature)) { items.add(cursor.getKey()); } } @@ -1344,11 +1332,11 @@ public class XmppConnection implements Runnable { public String getMucServer() { synchronized (this.disco) { - for (final Entry cursor : disco.entrySet()) { - final Info value = cursor.getValue(); - if (value.features.contains("http://jabber.org/protocol/muc") - && !value.features.contains("jabber:iq:gateway") - && !value.identities.contains(new Pair<>("conference", "irc"))) { + for (final Entry cursor : disco.entrySet()) { + final ServiceDiscoveryResult value = cursor.getValue(); + if (value.getFeatures().contains("http://jabber.org/protocol/muc") + && !value.getFeatures().contains("jabber:iq:gateway") + && !value.hasIdentity("conference", "irc")) { return cursor.getKey().toString(); } } @@ -1411,11 +1399,6 @@ public class XmppConnection implements Runnable { return mServerIdentity; } - private class Info { - public final ArrayList features = new ArrayList<>(); - public final ArrayList> identities = new ArrayList<>(); - } - private class UnauthorizedException extends IOException { } @@ -1450,7 +1433,7 @@ public class XmppConnection implements Runnable { private boolean hasDiscoFeature(final Jid server, final String feature) { synchronized (XmppConnection.this.disco) { return connection.disco.containsKey(server) && - connection.disco.get(server).features.contains(feature); + connection.disco.get(server).getFeatures().contains(feature); } } @@ -1478,12 +1461,12 @@ public class XmppConnection implements Runnable { public boolean pep() { synchronized (XmppConnection.this.disco) { final Pair needle = new Pair<>("pubsub", "pep"); - Info info = disco.get(account.getServer()); - if (info != null && info.identities.contains(needle)) { + ServiceDiscoveryResult info = disco.get(account.getServer()); + if (info != null && info.hasIdentity("pubsub", "pep")) { return true; } else { info = disco.get(account.getJid().toBareJid()); - return info != null && info.identities.contains(needle); + return info != null && info.hasIdentity("pubsub", "pep"); } } } -- cgit v1.2.3 From 1e335d527b888963aa953542cfae866ade0d5867 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Sun, 10 Jan 2016 16:25:26 -0500 Subject: Generate capHash from any discovery result --- .../entities/ServiceDiscoveryResult.java | 84 ++++++++++++++++++++-- 1 file changed, 80 insertions(+), 4 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java index 96346440..853e75aa 100644 --- a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java +++ b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java @@ -1,26 +1,41 @@ package eu.siacs.conversations.entities; -import java.util.List; +import android.content.ContentValues; +import android.util.Base64; +import java.io.UnsupportedEncodingException; +import java.lang.Comparable; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xmpp.stanzas.IqPacket; public class ServiceDiscoveryResult { - public static class Identity { + + protected static String blankNull(String s) { + return s == null ? "" : s; + } + + public static class Identity implements Comparable { protected final String category; protected final String type; + protected final String lang; protected final String name; - public Identity(final String category, final String type, final String name) { + public Identity(final String category, final String type, final String lang, final String name) { this.category = category; this.type = type; + this.lang = lang; this.name = name; } public Identity(final Element el) { this.category = el.getAttribute("category"); this.type = el.getAttribute("type"); + this.lang = el.getAttribute("xml:lang"); this.name = el.getAttribute("name"); } @@ -32,9 +47,29 @@ public class ServiceDiscoveryResult { return this.type; } + public String getLang() { + return this.lang; + } + public String getName() { return this.name; } + + public int compareTo(Object other) { + Identity o = (Identity)other; + int r = blankNull(this.getCategory()).compareTo(blankNull(o.getCategory())); + if(r == 0) { + r = blankNull(this.getType()).compareTo(blankNull(o.getType())); + } + if(r == 0) { + r = blankNull(this.getLang()).compareTo(blankNull(o.getLang())); + } + if(r == 0) { + r = blankNull(this.getName()).compareTo(blankNull(o.getName())); + } + + return r; + } } protected final List identities; @@ -58,7 +93,9 @@ public class ServiceDiscoveryResult { identities.add(id); } } else if (element.getName().equals("feature")) { - features.add(element.getAttribute("var")); + if (element.getAttribute("var") != null) { + features.add(element.getAttribute("var")); + } } } } @@ -81,4 +118,43 @@ public class ServiceDiscoveryResult { return false; } + + public byte[] getCapHash() { + StringBuilder s = new StringBuilder(); + + List identities = this.getIdentities(); + Collections.sort(identities); + + for(Identity id : identities) { + s.append( + blankNull(id.getCategory()) + "/" + + blankNull(id.getType()) + "/" + + blankNull(id.getLang()) + "/" + + blankNull(id.getName()) + "<" + ); + } + + List features = this.getFeatures(); + Collections.sort(features); + + for (String feature : features) { + s.append(feature + "<"); + } + + // TODO: data forms? + + MessageDigest md; + try { + md = MessageDigest.getInstance("SHA-1"); + } catch (NoSuchAlgorithmException e) { + return null; + } + + try { + return md.digest(s.toString().getBytes("UTF-8")); + } catch(UnsupportedEncodingException e) { + return null; + } + } + } -- cgit v1.2.3 From 56f8fff935baa4cec804807cfed3728d11c086ed Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Sun, 10 Jan 2016 16:43:48 -0500 Subject: Implement toJSON on ServiceDiscoveryResult --- .../entities/ServiceDiscoveryResult.java | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java index 853e75aa..ac4e8c47 100644 --- a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java +++ b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java @@ -9,6 +9,9 @@ import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xmpp.stanzas.IqPacket; @@ -70,6 +73,19 @@ public class ServiceDiscoveryResult { return r; } + + public JSONObject toJSON() { + try { + JSONObject o = new JSONObject(); + o.put("category", this.getCategory()); + o.put("type", this.getType()); + o.put("lang", this.getLang()); + o.put("name", this.getName()); + return o; + } catch(JSONException e) { + return null; + } + } } protected final List identities; @@ -157,4 +173,22 @@ public class ServiceDiscoveryResult { } } + public JSONObject toJSON() { + try { + JSONObject o = new JSONObject(); + + JSONArray ids = new JSONArray(); + for(Identity id : this.getIdentities()) { + ids.put(id.toJSON()); + } + o.put("identites", ids); + + o.put("features", new JSONArray(this.getFeatures())); + + return o; + } catch(JSONException e) { + return null; + } + } + } -- cgit v1.2.3 From ad36a4ba89f880a5522ffd5179dcaa98985e4164 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 12 Jan 2016 21:53:38 -0500 Subject: Persisitence and loading for ServiceDiscoveryResult --- .../entities/ServiceDiscoveryResult.java | 68 ++++++++++++++++++---- .../conversations/persistance/DatabaseBackend.java | 41 ++++++++++++- 2 files changed, 98 insertions(+), 11 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java index ac4e8c47..9d77efab 100644 --- a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java +++ b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java @@ -1,6 +1,7 @@ package eu.siacs.conversations.entities; import android.content.ContentValues; +import android.database.Cursor; import android.util.Base64; import java.io.UnsupportedEncodingException; import java.lang.Comparable; @@ -17,6 +18,10 @@ import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xmpp.stanzas.IqPacket; public class ServiceDiscoveryResult { + public static final String TABLENAME = "discovery_results"; + public static final String HASH = "hash"; + public static final String VER = "ver"; + public static final String RESULT = "result"; protected static String blankNull(String s) { return s == null ? "" : s; @@ -36,10 +41,21 @@ public class ServiceDiscoveryResult { } public Identity(final Element el) { - this.category = el.getAttribute("category"); - this.type = el.getAttribute("type"); - this.lang = el.getAttribute("xml:lang"); - this.name = el.getAttribute("name"); + this( + el.getAttribute("category"), + el.getAttribute("type"), + el.getAttribute("xml:lang"), + el.getAttribute("name") + ); + } + + public Identity(final JSONObject o) { + this( + o.optString("category", null), + o.optString("type", null), + o.optString("lang", null), + o.optString("name", null) + ); } public String getCategory() { @@ -88,17 +104,15 @@ public class ServiceDiscoveryResult { } } + protected final String hash; + protected final byte[] ver; protected final List identities; protected final List features; - public ServiceDiscoveryResult(final List identities, final List features) { - this.identities = identities; - this.features = features; - } - public ServiceDiscoveryResult(final IqPacket packet) { this.identities = new ArrayList<>(); this.features = new ArrayList<>(); + this.hash = "sha-1"; // We only support sha-1 for now final List elements = packet.query().getChildren(); @@ -114,6 +128,33 @@ public class ServiceDiscoveryResult { } } } + + this.ver = this.mkCapHash(); + } + + public ServiceDiscoveryResult(String hash, byte[] ver, JSONObject o) throws JSONException { + this.identities = new ArrayList<>(); + this.features = new ArrayList<>(); + this.hash = hash; + this.ver = ver; + + JSONArray identities = o.optJSONArray("identities"); + for(int i = 0; i < identities.length(); i++) { + this.identities.add(new Identity(identities.getJSONObject(i))); + } + + JSONArray features = o.optJSONArray("features"); + for(int i = 0; i < features.length(); i++) { + this.features.add(features.getString(i)); + } + } + + public ServiceDiscoveryResult(Cursor cursor) throws JSONException { + this( + cursor.getString(cursor.getColumnIndex(HASH)), + Base64.decode(cursor.getString(cursor.getColumnIndex(VER)), Base64.DEFAULT), + new JSONObject(cursor.getString(cursor.getColumnIndex(RESULT))) + ); } public List getIdentities() { @@ -135,7 +176,7 @@ public class ServiceDiscoveryResult { return false; } - public byte[] getCapHash() { + protected byte[] mkCapHash() { StringBuilder s = new StringBuilder(); List identities = this.getIdentities(); @@ -191,4 +232,11 @@ public class ServiceDiscoveryResult { } } + public ContentValues getContentValues() { + final ContentValues values = new ContentValues(); + values.put(HASH, this.hash); + values.put(VER, new String(Base64.encode(this.ver, Base64.DEFAULT)).trim()); + values.put(RESULT, this.toJSON().toString()); + return values; + } } diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java index 77c16f25..2f28a30f 100644 --- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -31,6 +31,7 @@ import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; +import org.json.JSONException; import eu.siacs.conversations.Config; import eu.siacs.conversations.crypto.axolotl.AxolotlService; @@ -41,6 +42,7 @@ import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Roster; +import eu.siacs.conversations.entities.ServiceDiscoveryResult; import eu.siacs.conversations.xmpp.jid.InvalidJidException; import eu.siacs.conversations.xmpp.jid.Jid; @@ -49,7 +51,7 @@ public class DatabaseBackend extends SQLiteOpenHelper { private static DatabaseBackend instance = null; private static final String DATABASE_NAME = "history"; - private static final int DATABASE_VERSION = 22; + private static final int DATABASE_VERSION = 23; private static String CREATE_CONTATCS_STATEMENT = "create table " + Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, " @@ -63,6 +65,14 @@ public class DatabaseBackend extends SQLiteOpenHelper { + ") ON DELETE CASCADE, UNIQUE(" + Contact.ACCOUNT + ", " + Contact.JID + ") ON CONFLICT REPLACE);"; + private static String CREATE_DISCOVERY_RESULTS_STATEMENT = "create table " + + ServiceDiscoveryResult.TABLENAME + "(" + + ServiceDiscoveryResult.HASH + " TEXT, " + + ServiceDiscoveryResult.VER + " TEXT, " + + ServiceDiscoveryResult.RESULT + " TEXT, " + + "UNIQUE(" + ServiceDiscoveryResult.HASH + ", " + + ServiceDiscoveryResult.VER + ") ON CONFLICT REPLACE);"; + private static String CREATE_PREKEYS_STATEMENT = "CREATE TABLE " + SQLiteAxolotlStore.PREKEY_TABLENAME + "(" + SQLiteAxolotlStore.ACCOUNT + " TEXT, " @@ -158,6 +168,7 @@ public class DatabaseBackend extends SQLiteOpenHelper { + ") ON DELETE CASCADE);"); db.execSQL(CREATE_CONTATCS_STATEMENT); + db.execSQL(CREATE_DISCOVERY_RESULTS_STATEMENT); db.execSQL(CREATE_SESSIONS_STATEMENT); db.execSQL(CREATE_PREKEYS_STATEMENT); db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT); @@ -355,6 +366,10 @@ public class DatabaseBackend extends SQLiteOpenHelper { if (oldVersion < 22 && newVersion >= 22) { db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.CERTIFICATE); } + + if (oldVersion < 23 && newVersion >= 23) { + db.execSQL(CREATE_DISCOVERY_RESULTS_STATEMENT); + } } public static synchronized DatabaseBackend getInstance(Context context) { @@ -379,6 +394,30 @@ public class DatabaseBackend extends SQLiteOpenHelper { db.insert(Account.TABLENAME, null, account.getContentValues()); } + public void insertDiscoveryResult(ServiceDiscoveryResult result) { + SQLiteDatabase db = this.getWritableDatabase(); + db.insert(ServiceDiscoveryResult.TABLENAME, null, result.getContentValues()); + } + + public ServiceDiscoveryResult findDiscoveryResult(final String hash, final String ver) { + SQLiteDatabase db = this.getReadableDatabase(); + String[] selectionArgs = {hash, ver}; + Cursor cursor = db.query(ServiceDiscoveryResult.TABLENAME, null, + ServiceDiscoveryResult.HASH + "=? AND " + ServiceDiscoveryResult.VER + "=?", + selectionArgs, null, null, null); + if (cursor.getCount() == 0) + return null; + cursor.moveToFirst(); + + ServiceDiscoveryResult result = null; + try { + result = new ServiceDiscoveryResult(cursor); + } catch (JSONException e) { /* result is still null */ } + + cursor.close(); + return result; + } + public CopyOnWriteArrayList getConversations(int status) { CopyOnWriteArrayList list = new CopyOnWriteArrayList<>(); SQLiteDatabase db = this.getReadableDatabase(); -- cgit v1.2.3 From bf5b2f73f5a61f0a0179c9d4431579f87eecb001 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Sun, 17 Jan 2016 16:28:38 -0500 Subject: Use a Presence class for presence information Only has status for now, but doing it so I can add disco to it --- .../eu/siacs/conversations/entities/Contact.java | 23 +++++--- .../eu/siacs/conversations/entities/Presence.java | 49 +++++++++++++++ .../eu/siacs/conversations/entities/Presences.java | 46 +++------------ .../conversations/generator/PresenceGenerator.java | 19 ++---- .../siacs/conversations/parser/PresenceParser.java | 3 +- .../services/XmppConnectionService.java | 9 +-- .../conversations/ui/ConversationFragment.java | 69 +++++++++++----------- .../ui/StartConversationActivity.java | 5 +- 8 files changed, 121 insertions(+), 102 deletions(-) create mode 100644 src/main/java/eu/siacs/conversations/entities/Presence.java diff --git a/src/main/java/eu/siacs/conversations/entities/Contact.java b/src/main/java/eu/siacs/conversations/entities/Contact.java index c2d8b278..ed773b6e 100644 --- a/src/main/java/eu/siacs/conversations/entities/Contact.java +++ b/src/main/java/eu/siacs/conversations/entities/Contact.java @@ -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) { diff --git a/src/main/java/eu/siacs/conversations/entities/Presence.java b/src/main/java/eu/siacs/conversations/entities/Presence.java new file mode 100644 index 00000000..adba74d1 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/entities/Presence.java @@ -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; + } +} diff --git a/src/main/java/eu/siacs/conversations/entities/Presences.java b/src/main/java/eu/siacs/conversations/entities/Presences.java index 4729a11b..813eda7a 100644 --- a/src/main/java/eu/siacs/conversations/entities/Presences.java +++ b/src/main/java/eu/siacs/conversations/entities/Presences.java @@ -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 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 presences = new Hashtable<>(); - - public Hashtable getPresences() { + public Hashtable 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> it = presences.entrySet().iterator(); - while (it.hasNext()) { - Entry 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()); } } diff --git a/src/main/java/eu/siacs/conversations/generator/PresenceGenerator.java b/src/main/java/eu/siacs/conversations/generator/PresenceGenerator.java index fdfde88c..093a8963 100644 --- a/src/main/java/eu/siacs/conversations/generator/PresenceGenerator.java +++ b/src/main/java/eu/siacs/conversations/generator/PresenceGenerator.java @@ -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(); diff --git a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java index d27182c1..46c8a35d 100644 --- a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java +++ b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java @@ -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) { diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index a295b2ce..9ad8ea1c 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -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; } } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 0af3a921..352d00ca 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -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)); diff --git a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java index 00a501f3..63859337 100644 --- a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java @@ -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); } } -- cgit v1.2.3 From 000f59d614d07ac573c1159ea964ebfee6ecbef5 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Sun, 17 Jan 2016 16:46:32 -0500 Subject: Fetch cached caps result on new presence --- src/main/java/eu/siacs/conversations/entities/Presence.java | 5 ++++- .../java/eu/siacs/conversations/parser/PresenceParser.java | 12 ++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Presence.java b/src/main/java/eu/siacs/conversations/entities/Presence.java index adba74d1..d31bb69d 100644 --- a/src/main/java/eu/siacs/conversations/entities/Presence.java +++ b/src/main/java/eu/siacs/conversations/entities/Presence.java @@ -22,8 +22,11 @@ public class Presence implements Comparable { } protected final Status status; + protected final ServiceDiscoveryResult disco; + + public Presence(Element show, ServiceDiscoveryResult disco) { + this.disco = disco; - public Presence(Element show) { if ((show == null) || (show.getContent() == null)) { this.status = Status.ONLINE; } else if (show.getContent().equals("away")) { diff --git a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java index 46c8a35d..88631a91 100644 --- a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java +++ b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java @@ -9,8 +9,8 @@ import eu.siacs.conversations.entities.Contact; 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.entities.ServiceDiscoveryResult; import eu.siacs.conversations.generator.PresenceGenerator; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.xml.Element; @@ -177,7 +177,15 @@ public class PresenceParser extends AbstractParser implements } } int sizeBefore = contact.getPresences().size(); - contact.updatePresence(presence, new Presence(packet.findChild("show"))); + + ServiceDiscoveryResult disco = null; + Element caps = packet.findChild("c", "http://jabber.org/protocol/caps"); + if (caps != null) { + disco = mXmppConnectionService.databaseBackend. + findDiscoveryResult(caps.getAttribute("hash"), caps.getAttribute("ver")); + } + contact.updatePresence(presence, new Presence(packet.findChild("show"), disco)); + PgpEngine pgp = mXmppConnectionService.getPgpEngine(); Element x = packet.findChild("x", "jabber:x:signed"); if (pgp != null && x != null) { -- cgit v1.2.3 From ae84ff2f0c01be832075d16556db2136c1b5ac55 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Sun, 17 Jan 2016 17:11:00 -0500 Subject: Do disco for caps hashes we have never seen Then cache it --- .../siacs/conversations/parser/PresenceParser.java | 25 ++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java index 88631a91..da7a8ce6 100644 --- a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java +++ b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java @@ -14,9 +14,11 @@ import eu.siacs.conversations.entities.ServiceDiscoveryResult; import eu.siacs.conversations.generator.PresenceGenerator; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.xml.Element; +import eu.siacs.conversations.xmpp.OnIqPacketReceived; import eu.siacs.conversations.xmpp.OnPresencePacketReceived; import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.pep.Avatar; +import eu.siacs.conversations.xmpp.stanzas.IqPacket; import eu.siacs.conversations.xmpp.stanzas.PresencePacket; public class PresenceParser extends AbstractParser implements @@ -161,7 +163,7 @@ public class PresenceParser extends AbstractParser implements final String type = packet.getAttribute("type"); final Contact contact = account.getRoster().getContact(from); if (type == null) { - String presence = from.isBareJid() ? "" : from.getResourcepart(); + final String presence = from.isBareJid() ? "" : from.getResourcepart(); contact.setPresenceName(packet.findChildContent("nick", "http://jabber.org/protocol/nick")); Avatar avatar = Avatar.parsePresence(packet.findChild("x", "vcard-temp:x:update")); if (avatar != null && !contact.isSelf()) { @@ -180,11 +182,30 @@ public class PresenceParser extends AbstractParser implements ServiceDiscoveryResult disco = null; Element caps = packet.findChild("c", "http://jabber.org/protocol/caps"); + if (caps != null) { disco = mXmppConnectionService.databaseBackend. findDiscoveryResult(caps.getAttribute("hash"), caps.getAttribute("ver")); } - contact.updatePresence(presence, new Presence(packet.findChild("show"), disco)); + + if (disco != null || caps == null) { + contact.updatePresence(presence, new Presence(packet.findChild("show"), disco)); + } else { + IqPacket request = new IqPacket(IqPacket.TYPE.GET); + request.setTo(from); + request.query("http://jabber.org/protocol/disco#info"); + + mXmppConnectionService.sendIqPacket(account, request, new OnIqPacketReceived() { + @Override + public void onIqPacketReceived(Account account, IqPacket discoPacket) { + if (discoPacket.getType() == IqPacket.TYPE.RESULT) { + ServiceDiscoveryResult disco = new ServiceDiscoveryResult(discoPacket); + contact.updatePresence(presence, new Presence(packet.findChild("show"), disco)); + mXmppConnectionService.databaseBackend.insertDiscoveryResult(disco); + } + } + }); + } PgpEngine pgp = mXmppConnectionService.getPgpEngine(); Element x = packet.findChild("x", "jabber:x:signed"); -- cgit v1.2.3 From f30df7a535bef4e79cd15cbdeffd4f5549b5c3fc Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 2 Feb 2016 11:21:07 +0100 Subject: catch a few NPE when parsing invalid pep nodes --- src/main/java/eu/siacs/conversations/parser/IqParser.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/parser/IqParser.java b/src/main/java/eu/siacs/conversations/parser/IqParser.java index 1761e0df..09bbabeb 100644 --- a/src/main/java/eu/siacs/conversations/parser/IqParser.java +++ b/src/main/java/eu/siacs/conversations/parser/IqParser.java @@ -142,7 +142,7 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived { } try { publicKey = Curve.decodePoint(Base64.decode(signedPreKeyPublic.getContent(),Base64.DEFAULT), 0); - } catch (InvalidKeyException | IllegalArgumentException e) { + } catch (InvalidKeyException | IllegalArgumentException | NullPointerException e) { Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"Invalid signedPreKeyPublic in PEP: " + e.getMessage()); } return publicKey; @@ -169,7 +169,7 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived { } try { identityKey = new IdentityKey(Base64.decode(identityKeyElement.getContent(), Base64.DEFAULT), 0); - } catch (InvalidKeyException | IllegalArgumentException e) { + } catch (InvalidKeyException | IllegalArgumentException | NullPointerException e) { Log.e(Config.LOGTAG,AxolotlService.LOGPREFIX+" : "+"Invalid identityKey in PEP: "+e.getMessage()); } return identityKey; @@ -200,7 +200,7 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived { try { ECPublicKey preKeyPublic = Curve.decodePoint(Base64.decode(preKeyPublicElement.getContent(), Base64.DEFAULT), 0); preKeyRecords.put(preKeyId, preKeyPublic); - } catch (InvalidKeyException | IllegalArgumentException e) { + } catch (InvalidKeyException | IllegalArgumentException | NullPointerException e) { Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"Invalid preKeyPublic (ID="+preKeyId+") in PEP: "+ e.getMessage()+", skipping..."); continue; } -- cgit v1.2.3 From ba9ba8ffe2ba3ef84ad1ed2adc57ab91e6f8fa73 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 2 Feb 2016 11:21:29 +0100 Subject: avoid npe when accessing the pgp connection service --- .../java/eu/siacs/conversations/services/XmppConnectionService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 99183dff..94af846e 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -334,7 +334,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } public PgpEngine getPgpEngine() { - if (pgpServiceConnection.isBound()) { + if (pgpServiceConnection != null && pgpServiceConnection.isBound()) { if (this.mPgpEngine == null) { this.mPgpEngine = new PgpEngine(new OpenPgpApi( getApplicationContext(), -- cgit v1.2.3 From fab0a45955e04fa3210da651ac74ea6541abadab Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 2 Feb 2016 13:43:20 +0100 Subject: re-read common name from certificates on startup --- .../crypto/axolotl/AxolotlService.java | 25 ++++++++++++++++++++++ .../eu/siacs/conversations/entities/Contact.java | 9 ++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java b/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java index 43a90010..949975aa 100644 --- a/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java +++ b/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java @@ -1,5 +1,6 @@ package eu.siacs.conversations.crypto.axolotl; +import android.os.Bundle; import android.security.KeyChain; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -39,6 +40,7 @@ import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.parser.IqParser; import eu.siacs.conversations.services.XmppConnectionService; +import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.SerialSingleThreadExecutor; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded; @@ -160,6 +162,20 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { AxolotlAddress axolotlAddress = new AxolotlAddress(bareJid, deviceId); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building session for remote address: " + axolotlAddress.toString()); IdentityKey identityKey = store.loadSession(axolotlAddress).getSessionState().getRemoteIdentityKey(); + if(Config.X509_VERIFICATION) { + X509Certificate certificate = store.getFingerprintCertificate(identityKey.getFingerprint().replaceAll("\\s", "")); + if (certificate != null) { + Bundle information = CryptoHelper.extractCertificateInformation(certificate); + try { + final String cn = information.getString("subject_cn"); + final Jid jid = Jid.fromString(bareJid); + Log.d(Config.LOGTAG,"setting common name for "+jid+" to "+cn); + account.getRoster().getContact(jid).setCommonName(cn); + } catch (final InvalidJidException ignored) { + //ignored + } + } + } this.put(axolotlAddress, new XmppAxolotlSession(account, store, axolotlAddress, identityKey)); } } @@ -619,6 +635,15 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { setFingerprintTrust(fingerprint, XmppAxolotlSession.Trust.TRUSTED_X509); axolotlStore.setFingerprintCertificate(fingerprint, verification.first[0]); fetchStatusMap.put(address, FetchStatus.SUCCESS_VERIFIED); + Bundle information = CryptoHelper.extractCertificateInformation(verification.first[0]); + try { + final String cn = information.getString("subject_cn"); + final Jid jid = Jid.fromString(address.getName()); + Log.d(Config.LOGTAG,"setting common name for "+jid+" to "+cn); + account.getRoster().getContact(jid).setCommonName(cn); + } catch (final InvalidJidException ignored) { + //ignored + } finishBuildingSessionsFromPEP(address); return; } catch (Exception e) { diff --git a/src/main/java/eu/siacs/conversations/entities/Contact.java b/src/main/java/eu/siacs/conversations/entities/Contact.java index c2d8b278..b338656b 100644 --- a/src/main/java/eu/siacs/conversations/entities/Contact.java +++ b/src/main/java/eu/siacs/conversations/entities/Contact.java @@ -38,6 +38,7 @@ public class Contact implements ListItem, Blockable { protected String systemName; protected String serverName; protected String presenceName; + protected String commonName; protected Jid jid; protected int subscription = 0; protected String systemAccount; @@ -105,8 +106,8 @@ public class Contact implements ListItem, Blockable { } public String getDisplayName() { - if (this.presenceName != null && Config.X509_VERIFICATION) { - return this.presenceName; + if (this.commonName != null && Config.X509_VERIFICATION) { + return this.commonName; } else if (this.systemName != null) { return this.systemName; } else if (this.serverName != null) { @@ -510,6 +511,10 @@ public class Contact implements ListItem, Blockable { return account.getJid().toBareJid().equals(getJid().toBareJid()); } + public void setCommonName(String cn) { + this.commonName = cn; + } + public static class Lastseen { public long time; public String presence; -- cgit v1.2.3 From 58c6f9bfb283f5c54695b9662b5b4b08e5d4357f Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 2 Feb 2016 15:39:46 +0100 Subject: get rid of broken totalMessageCount for mam queries --- .../java/eu/siacs/conversations/parser/MessageParser.java | 6 ++---- .../conversations/services/MessageArchiveService.java | 14 ++------------ 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/parser/MessageParser.java b/src/main/java/eu/siacs/conversations/parser/MessageParser.java index d8eae969..60948918 100644 --- a/src/main/java/eu/siacs/conversations/parser/MessageParser.java +++ b/src/main/java/eu/siacs/conversations/parser/MessageParser.java @@ -271,7 +271,7 @@ public class MessageParser extends AbstractParser implements packet = f.first; isForwarded = true; serverMsgId = result.getAttribute("id"); - query.incrementTotalCount(); + query.incrementMessageCount(); } else if (query != null) { Log.d(Config.LOGTAG,account.getJid().toBareJid()+": received mam result from invalid sender"); return; @@ -411,9 +411,7 @@ public class MessageParser extends AbstractParser implements } } - if (query != null) { - query.incrementMessageCount(); - } else { + if (query == null) { mXmppConnectionService.updateConversationUi(); } diff --git a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java index 4403f99c..6fcb4612 100644 --- a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java +++ b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java @@ -201,10 +201,10 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { Element last = set == null ? null : set.findChild("last"); Element first = set == null ? null : set.findChild("first"); Element relevant = query.getPagingOrder() == PagingOrder.NORMAL ? last : first; - boolean abort = (query.getStart() == 0 && query.getTotalCount() >= Config.PAGE_SIZE) || query.getTotalCount() >= Config.MAM_MAX_MESSAGES; + boolean abort = (query.getStart() == 0 && query.getMessageCount() >= Config.PAGE_SIZE) || query.getMessageCount() >= Config.MAM_MAX_MESSAGES; if (complete || relevant == null || abort) { this.finalizeQuery(query); - Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid().toString()+": finished mam after "+query.getTotalCount()+" messages"); + Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid().toString()+": finished mam after "+query.getMessageCount()+" messages"); if (query.getWith() == null && query.getMessageCount() > 0) { mXmppConnectionService.getNotificationService().finishBacklog(true); } @@ -246,7 +246,6 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { } public class Query { - private int totalCount = 0; private int messageCount = 0; private long start; private long end; @@ -279,7 +278,6 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { Query query = new Query(this.account,this.start,this.end); query.reference = reference; query.conversation = conversation; - query.totalCount = totalCount; query.callback = callback; return query; } @@ -345,18 +343,10 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { return this.account; } - public void incrementTotalCount() { - this.totalCount++; - } - public void incrementMessageCount() { this.messageCount++; } - public int getTotalCount() { - return this.totalCount; - } - public int getMessageCount() { return this.messageCount; } -- cgit v1.2.3 From 01bad127084cdfc623c57be18e123ef8ff6655b0 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 2 Feb 2016 18:15:57 +0100 Subject: fixed 'unencrypted' not showing up for conferences when encryption is forced --- src/main/java/eu/siacs/conversations/ui/ConversationActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java index b4857067..2461c39d 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java @@ -854,7 +854,7 @@ public class ConversationActivity extends XmppActivity MenuItem pgp = popup.getMenu().findItem(R.id.encryption_choice_pgp); MenuItem axolotl = popup.getMenu().findItem(R.id.encryption_choice_axolotl); pgp.setVisible(!Config.HIDE_PGP_IN_UI && !Config.X509_VERIFICATION); - none.setVisible(!Config.FORCE_E2E_ENCRYPTION); + none.setVisible(!Config.FORCE_E2E_ENCRYPTION || conversation.getMode() == Conversation.MODE_MULTI); otr.setVisible(!Config.X509_VERIFICATION); if (conversation.getMode() == Conversation.MODE_MULTI) { otr.setVisible(false); -- cgit v1.2.3 From f0798216d568bca30051ba5392263da31e78eb98 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 3 Feb 2016 10:40:02 +0100 Subject: refactored disco cache. avoid making duplicate call. check hash --- src/main/java/eu/siacs/conversations/Config.java | 2 + .../eu/siacs/conversations/entities/Account.java | 3 ++ .../eu/siacs/conversations/entities/Presence.java | 42 +++++++++++---- .../entities/ServiceDiscoveryResult.java | 20 +++++--- .../siacs/conversations/parser/PresenceParser.java | 33 +++--------- .../services/XmppConnectionService.java | 59 +++++++++++++++++++++- 6 files changed, 116 insertions(+), 43 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/Config.java b/src/main/java/eu/siacs/conversations/Config.java index 9ee221cc..3d32a2cb 100644 --- a/src/main/java/eu/siacs/conversations/Config.java +++ b/src/main/java/eu/siacs/conversations/Config.java @@ -62,6 +62,8 @@ public final class Config { public static final boolean IGNORE_ID_REWRITE_IN_MUC = true; + public static final boolean REQUEST_DISCO = true; + public static final long MILLISECONDS_IN_DAY = 24 * 60 * 60 * 1000; public static final long MAM_MAX_CATCHUP = MILLISECONDS_IN_DAY / 2; public static final int MAM_MAX_MESSAGES = 500; diff --git a/src/main/java/eu/siacs/conversations/entities/Account.java b/src/main/java/eu/siacs/conversations/entities/Account.java index 4abfc801..356b34e5 100644 --- a/src/main/java/eu/siacs/conversations/entities/Account.java +++ b/src/main/java/eu/siacs/conversations/entities/Account.java @@ -3,6 +3,7 @@ package eu.siacs.conversations.entities; import android.content.ContentValues; import android.database.Cursor; import android.os.SystemClock; +import android.util.Pair; import eu.siacs.conversations.crypto.PgpDecryptionService; import net.java.otr4j.crypto.OtrCryptoEngineImpl; @@ -14,6 +15,7 @@ import org.json.JSONObject; import java.security.PublicKey; import java.security.interfaces.DSAPublicKey; import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArraySet; @@ -48,6 +50,7 @@ public class Account extends AbstractEntity { public static final int OPTION_DISABLED = 1; public static final int OPTION_REGISTER = 2; public static final int OPTION_USECOMPRESSION = 3; + public final HashSet> inProgressDiscoFetches = new HashSet<>(); public boolean httpUploadAvailable() { return xmppConnection != null && xmppConnection.getFeatures().httpUpload(); diff --git a/src/main/java/eu/siacs/conversations/entities/Presence.java b/src/main/java/eu/siacs/conversations/entities/Presence.java index d31bb69d..69cde832 100644 --- a/src/main/java/eu/siacs/conversations/entities/Presence.java +++ b/src/main/java/eu/siacs/conversations/entities/Presence.java @@ -22,23 +22,31 @@ public class Presence implements Comparable { } protected final Status status; - protected final ServiceDiscoveryResult disco; + protected ServiceDiscoveryResult disco; + protected final String ver; + protected final String hash; - public Presence(Element show, ServiceDiscoveryResult disco) { - this.disco = disco; + private Presence(Status status, String ver, String hash) { + this.status = status; + this.ver = ver; + this.hash = hash; + } + public static Presence parse(Element show, Element caps) { + final String hash = caps == null ? null : caps.getAttribute("hash"); + final String ver = caps == null ? null : caps.getAttribute("ver"); if ((show == null) || (show.getContent() == null)) { - this.status = Status.ONLINE; + return new Presence(Status.ONLINE, ver, hash); } else if (show.getContent().equals("away")) { - this.status = Status.AWAY; + return new Presence(Status.AWAY, ver, hash); } else if (show.getContent().equals("xa")) { - this.status = Status.XA; + return new Presence(Status.XA, ver, hash); } else if (show.getContent().equals("chat")) { - this.status = Status.CHAT; + return new Presence(Status.CHAT, ver, hash); } else if (show.getContent().equals("dnd")) { - this.status = Status.DND; + return new Presence(Status.DND, ver, hash); } else { - this.status = Status.OFFLINE; + return new Presence(Status.OFFLINE, ver, hash); } } @@ -49,4 +57,20 @@ public class Presence implements Comparable { public Status getStatus() { return this.status; } + + public boolean hasCaps() { + return ver != null && hash != null; + } + + public String getVer() { + return this.ver; + } + + public String getHash() { + return this.hash; + } + + public void setServiceDiscoveryResult(ServiceDiscoveryResult disco) { + this.disco = disco; + } } diff --git a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java index 9d77efab..c50640e1 100644 --- a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java +++ b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java @@ -128,7 +128,6 @@ public class ServiceDiscoveryResult { } } } - this.ver = this.mkCapHash(); } @@ -139,16 +138,23 @@ public class ServiceDiscoveryResult { this.ver = ver; JSONArray identities = o.optJSONArray("identities"); - for(int i = 0; i < identities.length(); i++) { - this.identities.add(new Identity(identities.getJSONObject(i))); + if (identities != null) { + for (int i = 0; i < identities.length(); i++) { + this.identities.add(new Identity(identities.getJSONObject(i))); + } } - JSONArray features = o.optJSONArray("features"); - for(int i = 0; i < features.length(); i++) { - this.features.add(features.getString(i)); + if (features != null) { + for (int i = 0; i < features.length(); i++) { + this.features.add(features.getString(i)); + } } } + public String getVer() { + return new String(Base64.encode(this.ver, Base64.DEFAULT)).trim(); + } + public ServiceDiscoveryResult(Cursor cursor) throws JSONException { this( cursor.getString(cursor.getColumnIndex(HASH)), @@ -235,7 +241,7 @@ public class ServiceDiscoveryResult { public ContentValues getContentValues() { final ContentValues values = new ContentValues(); values.put(HASH, this.hash); - values.put(VER, new String(Base64.encode(this.ver, Base64.DEFAULT)).trim()); + values.put(VER, getVer()); values.put(RESULT, this.toJSON().toString()); return values; } diff --git a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java index 5a25b283..2e706ff7 100644 --- a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java +++ b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java @@ -170,7 +170,7 @@ public class PresenceParser extends AbstractParser implements final String type = packet.getAttribute("type"); final Contact contact = account.getRoster().getContact(from); if (type == null) { - final String presence = from.isBareJid() ? "" : from.getResourcepart(); + final String resource = from.isBareJid() ? "" : from.getResourcepart(); contact.setPresenceName(packet.findChildContent("nick", "http://jabber.org/protocol/nick")); Avatar avatar = Avatar.parsePresence(packet.findChild("x", "vcard-temp:x:update")); if (avatar != null && !contact.isSelf()) { @@ -187,31 +187,12 @@ public class PresenceParser extends AbstractParser implements } int sizeBefore = contact.getPresences().size(); - ServiceDiscoveryResult disco = null; - Element caps = packet.findChild("c", "http://jabber.org/protocol/caps"); - - if (caps != null) { - disco = mXmppConnectionService.databaseBackend. - findDiscoveryResult(caps.getAttribute("hash"), caps.getAttribute("ver")); - } - - if (disco != null || caps == null) { - contact.updatePresence(presence, new Presence(packet.findChild("show"), disco)); - } else { - IqPacket request = new IqPacket(IqPacket.TYPE.GET); - request.setTo(from); - request.query("http://jabber.org/protocol/disco#info"); - - mXmppConnectionService.sendIqPacket(account, request, new OnIqPacketReceived() { - @Override - public void onIqPacketReceived(Account account, IqPacket discoPacket) { - if (discoPacket.getType() == IqPacket.TYPE.RESULT) { - ServiceDiscoveryResult disco = new ServiceDiscoveryResult(discoPacket); - contact.updatePresence(presence, new Presence(packet.findChild("show"), disco)); - mXmppConnectionService.databaseBackend.insertDiscoveryResult(disco); - } - } - }); + final Element show = packet.findChild("show"); + final Element caps = packet.findChild("c", "http://jabber.org/protocol/caps"); + final Presence presence = Presence.parse(show, caps); + contact.updatePresence(resource, presence); + if (presence.hasCaps() && Config.REQUEST_DISCO) { + mXmppConnectionService.fetchCaps(account, from, presence); } PgpEngine pgp = mXmppConnectionService.getPgpEngine(); diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 43ca5a54..6735b9cc 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -74,6 +74,8 @@ 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.Roster; +import eu.siacs.conversations.entities.ServiceDiscoveryResult; import eu.siacs.conversations.entities.Transferable; import eu.siacs.conversations.entities.TransferablePlaceholder; import eu.siacs.conversations.generator.IqGenerator; @@ -245,6 +247,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa private OnKeyStatusUpdated mOnKeyStatusUpdated = null; private int keyStatusUpdatedListenerCount = 0; private SecureRandom mRandom; + private LruCache,ServiceDiscoveryResult> discoCache = new LruCache<>(20); private final OnBindListener mOnBindListener = new OnBindListener() { @Override @@ -2956,13 +2959,67 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa @Override public void onIqPacketReceived(Account account, IqPacket packet) { if (packet.getType() == IqPacket.TYPE.ERROR) { - Log.d(Config.LOGTAG,account.getJid().toBareJid()+": could not publish nick"); + Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not publish nick"); } } }); } } + private ServiceDiscoveryResult getCachedServiceDiscoveryResult(Pair key) { + ServiceDiscoveryResult result = discoCache.get(key); + if (result != null) { + return result; + } else { + result = databaseBackend.findDiscoveryResult(key.first, key.second); + if (result != null) { + discoCache.put(key, result); + } + return result; + } + } + + public void fetchCaps(Account account, final Jid jid, final Presence presence) { + final Pair key = new Pair<>(presence.getHash(), presence.getVer()); + ServiceDiscoveryResult disco = getCachedServiceDiscoveryResult(key); + if (disco != null) { + presence.setServiceDiscoveryResult(disco); + } else { + if (!account.inProgressDiscoFetches.contains(key)) { + account.inProgressDiscoFetches.add(key); + IqPacket request = new IqPacket(IqPacket.TYPE.GET); + request.setTo(jid); + request.query("http://jabber.org/protocol/disco#info"); + Log.d(Config.LOGTAG,account.getJid().toBareJid()+": making disco request for "+key.second+" to "+jid); + sendIqPacket(account, request, new OnIqPacketReceived() { + @Override + public void onIqPacketReceived(Account account, IqPacket discoPacket) { + if (discoPacket.getType() == IqPacket.TYPE.RESULT) { + ServiceDiscoveryResult disco = new ServiceDiscoveryResult(discoPacket); + if (presence.getVer().equals(disco.getVer())) { + databaseBackend.insertDiscoveryResult(disco); + injectServiceDiscorveryResult(account.getRoster(), presence.getHash(), presence.getVer(), disco); + } else { + Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": mismatch in caps for contact " + jid+" "+presence.getVer()+" vs "+disco.getVer()); + } + } + account.inProgressDiscoFetches.remove(key); + } + }); + } + } + } + + private void injectServiceDiscorveryResult(Roster roster, String hash, String ver, ServiceDiscoveryResult disco) { + for(Contact contact : roster.getContacts()) { + for(Presence presence : contact.getPresences().getPresences().values()) { + if (hash.equals(presence.getHash()) && ver.equals(presence.getVer())) { + presence.setServiceDiscoveryResult(disco); + } + } + } + } + public interface OnAccountCreated { void onAccountCreated(Account account); -- cgit v1.2.3 From 1274b0ef3998fdd3753ae9f930029c657e905ddf Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 3 Feb 2016 10:40:44 +0100 Subject: Revert "get rid of broken totalMessageCount for mam queries" This reverts commit 58c6f9bfb283f5c54695b9662b5b4b08e5d4357f. --- .../java/eu/siacs/conversations/parser/MessageParser.java | 6 ++++-- .../conversations/services/MessageArchiveService.java | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/parser/MessageParser.java b/src/main/java/eu/siacs/conversations/parser/MessageParser.java index 60948918..d8eae969 100644 --- a/src/main/java/eu/siacs/conversations/parser/MessageParser.java +++ b/src/main/java/eu/siacs/conversations/parser/MessageParser.java @@ -271,7 +271,7 @@ public class MessageParser extends AbstractParser implements packet = f.first; isForwarded = true; serverMsgId = result.getAttribute("id"); - query.incrementMessageCount(); + query.incrementTotalCount(); } else if (query != null) { Log.d(Config.LOGTAG,account.getJid().toBareJid()+": received mam result from invalid sender"); return; @@ -411,7 +411,9 @@ public class MessageParser extends AbstractParser implements } } - if (query == null) { + if (query != null) { + query.incrementMessageCount(); + } else { mXmppConnectionService.updateConversationUi(); } diff --git a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java index 6fcb4612..4403f99c 100644 --- a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java +++ b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java @@ -201,10 +201,10 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { Element last = set == null ? null : set.findChild("last"); Element first = set == null ? null : set.findChild("first"); Element relevant = query.getPagingOrder() == PagingOrder.NORMAL ? last : first; - boolean abort = (query.getStart() == 0 && query.getMessageCount() >= Config.PAGE_SIZE) || query.getMessageCount() >= Config.MAM_MAX_MESSAGES; + boolean abort = (query.getStart() == 0 && query.getTotalCount() >= Config.PAGE_SIZE) || query.getTotalCount() >= Config.MAM_MAX_MESSAGES; if (complete || relevant == null || abort) { this.finalizeQuery(query); - Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid().toString()+": finished mam after "+query.getMessageCount()+" messages"); + Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid().toString()+": finished mam after "+query.getTotalCount()+" messages"); if (query.getWith() == null && query.getMessageCount() > 0) { mXmppConnectionService.getNotificationService().finishBacklog(true); } @@ -246,6 +246,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { } public class Query { + private int totalCount = 0; private int messageCount = 0; private long start; private long end; @@ -278,6 +279,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { Query query = new Query(this.account,this.start,this.end); query.reference = reference; query.conversation = conversation; + query.totalCount = totalCount; query.callback = callback; return query; } @@ -343,10 +345,18 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { return this.account; } + public void incrementTotalCount() { + this.totalCount++; + } + public void incrementMessageCount() { this.messageCount++; } + public int getTotalCount() { + return this.totalCount; + } + public int getMessageCount() { return this.messageCount; } -- cgit v1.2.3 From 0911669b07b062b30d41da0623ff0e7288ad2ddd Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 3 Feb 2016 16:04:21 +0100 Subject: count all messages in a query --- src/main/java/eu/siacs/conversations/parser/MessageParser.java | 6 ++---- .../java/eu/siacs/conversations/services/MessageArchiveService.java | 5 +---- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/parser/MessageParser.java b/src/main/java/eu/siacs/conversations/parser/MessageParser.java index d8eae969..60948918 100644 --- a/src/main/java/eu/siacs/conversations/parser/MessageParser.java +++ b/src/main/java/eu/siacs/conversations/parser/MessageParser.java @@ -271,7 +271,7 @@ public class MessageParser extends AbstractParser implements packet = f.first; isForwarded = true; serverMsgId = result.getAttribute("id"); - query.incrementTotalCount(); + query.incrementMessageCount(); } else if (query != null) { Log.d(Config.LOGTAG,account.getJid().toBareJid()+": received mam result from invalid sender"); return; @@ -411,9 +411,7 @@ public class MessageParser extends AbstractParser implements } } - if (query != null) { - query.incrementMessageCount(); - } else { + if (query == null) { mXmppConnectionService.updateConversationUi(); } diff --git a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java index 4403f99c..0d8e49d6 100644 --- a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java +++ b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java @@ -345,12 +345,9 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { return this.account; } - public void incrementTotalCount() { - this.totalCount++; - } - public void incrementMessageCount() { this.messageCount++; + this.totalCount++; } public int getTotalCount() { -- cgit v1.2.3 From 1d572c61d0a55d5ac7a96ccaacd0f3243253757e Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 3 Feb 2016 17:19:05 +0100 Subject: cache server caps --- .../entities/ServiceDiscoveryResult.java | 19 ++++++++++++++++++- .../eu/siacs/conversations/xmpp/XmppConnection.java | 17 ++++++++++++++++- .../java/eu/siacs/conversations/xmpp/forms/Data.java | 3 ++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java index c50640e1..42f2d840 100644 --- a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java +++ b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java @@ -9,12 +9,14 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import eu.siacs.conversations.xml.Element; +import eu.siacs.conversations.xmpp.forms.Data; import eu.siacs.conversations.xmpp.stanzas.IqPacket; public class ServiceDiscoveryResult { @@ -108,10 +110,12 @@ public class ServiceDiscoveryResult { protected final byte[] ver; protected final List identities; protected final List features; + protected final List forms; public ServiceDiscoveryResult(final IqPacket packet) { this.identities = new ArrayList<>(); this.features = new ArrayList<>(); + this.forms = new ArrayList<>(); this.hash = "sha-1"; // We only support sha-1 for now final List elements = packet.query().getChildren(); @@ -126,6 +130,8 @@ public class ServiceDiscoveryResult { if (element.getAttribute("var") != null) { features.add(element.getAttribute("var")); } + } else if (element.getName().equals("x") && "jabber:x:data".equals(element.getAttribute("xmlns"))) { + forms.add(Data.parse(element)); } } this.ver = this.mkCapHash(); @@ -134,6 +140,7 @@ public class ServiceDiscoveryResult { public ServiceDiscoveryResult(String hash, byte[] ver, JSONObject o) throws JSONException { this.identities = new ArrayList<>(); this.features = new ArrayList<>(); + this.forms = new ArrayList<>(); this.hash = hash; this.ver = ver; @@ -204,7 +211,17 @@ public class ServiceDiscoveryResult { s.append(feature + "<"); } - // TODO: data forms? + Collections.sort(forms, new Comparator() { + @Override + public int compare(Data lhs, Data rhs) { + return lhs.getFormType().compareTo(rhs.getFormType()); + } + }); + + for(Data form : forms) { + s.append(form.getFormType()+"<"); + //TODO append fields and values + } MessageDigest md; try { diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index 635d2b9b..c911f654 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -1022,7 +1022,19 @@ public class XmppConnection implements Runnable { Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": starting service discovery"); mXmppConnectionService.scheduleWakeUpCall(Config.CONNECT_DISCO_TIMEOUT, account.getUuid().hashCode()); sendServiceDiscoveryItems(account.getServer()); - sendServiceDiscoveryInfo(account.getServer()); + Element caps = streamFeatures.findChild("c"); + final String hash = caps == null ? null : caps.getAttribute("hash"); + final String ver = caps == null ? null : caps.getAttribute("ver"); + ServiceDiscoveryResult discoveryResult = null; + if (hash != null && ver != null) { + discoveryResult = mXmppConnectionService.databaseBackend.findDiscoveryResult(hash, ver); + } + if (discoveryResult == null) { + sendServiceDiscoveryInfo(account.getServer()); + } else { + Log.d(Config.LOGTAG,account.getJid().toBareJid()+": server caps came from cache"); + disco.put(account.getServer(), discoveryResult); + } sendServiceDiscoveryInfo(account.getJid().toBareJid()); this.lastSessionStarted = SystemClock.elapsedRealtime(); } @@ -1060,6 +1072,9 @@ public class XmppConnection implements Runnable { Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server name: " + id.getName()); } } + if (jid.equals(account.getServer())) { + mXmppConnectionService.databaseBackend.insertDiscoveryResult(result); + } disco.put(jid, result); advancedStreamFeaturesLoaded = disco.containsKey(account.getServer()) && disco.containsKey(account.getJid().toBareJid()); diff --git a/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java b/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java index 0053a399..50a41892 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java +++ b/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java @@ -80,7 +80,8 @@ public class Data extends Element { } public String getFormType() { - return this.getAttribute("FORM_TYPE"); + Field typeFiled = this.getFieldByName("FORM_TYPE"); + return typeFiled == null ? "" : typeFiled.getValue(); } public String getTitle() { -- cgit v1.2.3 From 7dd9545ea3777972b1d80e59dfc5166dd53cceb0 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 3 Feb 2016 18:17:16 +0100 Subject: use TLSv1.2 as SSL context on supported plattforms --- .../eu/siacs/conversations/http/HttpConnectionManager.java | 3 ++- .../java/eu/siacs/conversations/utils/SSLSocketHelper.java | 11 +++++++++++ src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java b/src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java index 910c43f3..a8b31a7a 100644 --- a/src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java +++ b/src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java @@ -23,6 +23,7 @@ import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.services.AbstractConnectionManager; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.utils.CryptoHelper; +import eu.siacs.conversations.utils.SSLSocketHelper; public class HttpConnectionManager extends AbstractConnectionManager { @@ -76,7 +77,7 @@ public class HttpConnectionManager extends AbstractConnectionManager { new StrictHostnameVerifier()); } try { - final SSLContext sc = SSLContext.getInstance("TLS"); + final SSLContext sc = SSLSocketHelper.getSSLContext(); sc.init(null, new X509TrustManager[]{trustManager}, mXmppConnectionService.getRNG()); diff --git a/src/main/java/eu/siacs/conversations/utils/SSLSocketHelper.java b/src/main/java/eu/siacs/conversations/utils/SSLSocketHelper.java index 49e9a81a..3a8c1c0a 100644 --- a/src/main/java/eu/siacs/conversations/utils/SSLSocketHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/SSLSocketHelper.java @@ -1,11 +1,14 @@ package eu.siacs.conversations.utils; +import android.os.Build; + import java.lang.reflect.Method; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import java.util.Collection; import java.util.LinkedList; +import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; @@ -59,4 +62,12 @@ public class SSLSocketHelper { // ignore any error, we just can't set the alpn protocol... } } + + public static SSLContext getSSLContext() throws NoSuchAlgorithmException { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + return SSLContext.getInstance("TLSv1.2"); + } else { + return SSLContext.getInstance("TLS"); + } + } } diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index c911f654..8b7eae39 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -408,7 +408,7 @@ public class XmppConnection implements Runnable { } private TlsFactoryVerifier getTlsFactoryVerifier() throws NoSuchAlgorithmException, KeyManagementException, IOException { - final SSLContext sc = SSLContext.getInstance("TLS"); + final SSLContext sc = SSLSocketHelper.getSSLContext(); MemorizingTrustManager trustManager = this.mXmppConnectionService.getMemorizingTrustManager(); KeyManager[] keyManager; if (account.getPrivateKeyAlias() != null && account.getPassword().isEmpty()) { -- cgit v1.2.3 From 17791a703ec3f9a29e8e47d48c9aaed6cf49cc30 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 4 Feb 2016 10:27:38 +0100 Subject: removed unecessary logging when muc tiles update --- src/main/java/eu/siacs/conversations/parser/PresenceParser.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java index 2e706ff7..45c31e2b 100644 --- a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java +++ b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java @@ -42,7 +42,6 @@ public class PresenceParser extends AbstractParser implements processConferencePresence(packet, mucOptions); final List tileUserAfter = mucOptions.getUsers(5); if (!tileUserAfter.equals(tileUserBefore)) { - Log.d(Config.LOGTAG,account.getJid().toBareJid()+": update tiles for "+conversation.getName()); mXmppConnectionService.getAvatarService().clear(mucOptions); } if (before != mucOptions.online() || (mucOptions.online() && count != mucOptions.getUserCount())) { -- cgit v1.2.3 From f88b8c703eca10f6a0b96e01e5ba912b02488759 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 4 Feb 2016 11:55:42 +0100 Subject: add more fault tolerant checks for messages left on server --- .../eu/siacs/conversations/entities/Conversation.java | 9 +++++++++ .../conversations/services/MessageArchiveService.java | 17 +++++++++++------ .../conversations/services/XmppConnectionService.java | 2 +- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java index 22607fc6..f2c08b9a 100644 --- a/src/main/java/eu/siacs/conversations/entities/Conversation.java +++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java @@ -81,6 +81,7 @@ public class Conversation extends AbstractEntity implements Blockable { private ChatState mOutgoingChatState = Config.DEFAULT_CHATSTATE; private ChatState mIncomingChatState = Config.DEFAULT_CHATSTATE; private String mLastReceivedOtrMessageId = null; + private String mFirstMamReference = null; public boolean hasMessagesLeftOnServer() { return messagesLeftOnServer; @@ -277,6 +278,14 @@ public class Conversation extends AbstractEntity implements Blockable { } } + public void setFirstMamReference(String reference) { + this.mFirstMamReference = reference; + } + + public String getFirstMamReference() { + return this.mFirstMamReference; + } + public interface OnMessageFound { void onMessageFound(final Message message); } diff --git a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java index 0d8e49d6..a225760f 100644 --- a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java +++ b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java @@ -102,6 +102,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { return null; } final Query query = new Query(conversation, start, end,PagingOrder.REVERSE); + query.reference = conversation.getFirstMamReference(); this.queries.add(query); this.execute(query); return query; @@ -141,7 +142,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { } } else if (packet.getType() != IqPacket.TYPE.RESULT) { Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": error executing mam: " + packet.toString()); - finalizeQuery(query); + finalizeQuery(query, true); } } }); @@ -152,14 +153,14 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { } } - private void finalizeQuery(Query query) { + private void finalizeQuery(Query query, boolean done) { synchronized (this.queries) { this.queries.remove(query); } final Conversation conversation = query.getConversation(); if (conversation != null) { conversation.sort(); - conversation.setHasMessagesLeftOnServer(query.getMessageCount() > 0); + conversation.setHasMessagesLeftOnServer(!done); } else { for(Conversation tmp : this.mXmppConnectionService.getConversations()) { if (tmp.getAccount() == query.getAccount()) { @@ -202,9 +203,13 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { Element first = set == null ? null : set.findChild("first"); Element relevant = query.getPagingOrder() == PagingOrder.NORMAL ? last : first; boolean abort = (query.getStart() == 0 && query.getTotalCount() >= Config.PAGE_SIZE) || query.getTotalCount() >= Config.MAM_MAX_MESSAGES; + if (query.getConversation() != null) { + query.getConversation().setFirstMamReference(first == null ? null : first.getContent()); + } if (complete || relevant == null || abort) { - this.finalizeQuery(query); - Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid().toString()+": finished mam after "+query.getTotalCount()+" messages"); + final boolean done = (complete || query.getMessageCount() == 0) && query.getStart() == 0; + this.finalizeQuery(query, done); + Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid()+": finished mam after "+query.getTotalCount()+" messages. messages left="+Boolean.toString(!done)); if (query.getWith() == null && query.getMessageCount() > 0) { mXmppConnectionService.getNotificationService().finishBacklog(true); } @@ -216,7 +221,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { nextQuery = query.prev(first == null ? null : first.getContent()); } this.execute(nextQuery); - this.finalizeQuery(query); + this.finalizeQuery(query, false); synchronized (this.queries) { this.queries.remove(query); this.queries.add(nextQuery); diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 6735b9cc..73f17e3b 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -1230,7 +1230,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa && account.isOnlineAndConnected()) { if ((conversation.getMode() == Conversation.MODE_SINGLE && account.getXmppConnection().getFeatures().mam()) || (conversation.getMode() == Conversation.MODE_MULTI && conversation.getMucOptions().mamSupport())) { - MessageArchiveService.Query query = getMessageArchiveService().query(conversation, 0, timestamp - 1); + MessageArchiveService.Query query = getMessageArchiveService().query(conversation, 0, timestamp); if (query != null) { query.setCallback(callback); } -- cgit v1.2.3 From 4fdb0d92fe854126ad5201d56752716b9ab581b2 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 4 Feb 2016 14:39:16 +0100 Subject: prevent previoulsly cleared messages from reloading. fixes #1110 --- .../siacs/conversations/entities/Conversation.java | 12 +++++++ .../eu/siacs/conversations/entities/Message.java | 8 +++++ .../services/MessageArchiveService.java | 17 ++++----- .../services/XmppConnectionService.java | 4 +++ .../conversations/ui/ConversationActivity.java | 6 ++++ .../conversations/ui/ConversationFragment.java | 10 ++++-- .../conversations/ui/adapter/MessageAdapter.java | 42 ++++++++++++++++++---- src/main/res/layout/message_status.xml | 9 +++++ src/main/res/values/strings.xml | 1 + 9 files changed, 92 insertions(+), 17 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java index f2c08b9a..cb472b87 100644 --- a/src/main/java/eu/siacs/conversations/entities/Conversation.java +++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java @@ -286,6 +286,14 @@ public class Conversation extends AbstractEntity implements Blockable { return this.mFirstMamReference; } + public void setLastClearHistory(long time) { + setAttribute("last_clear_history",String.valueOf(time)); + } + + public long getLastClearHistory() { + return getLongAttribute("last_clear_history", 0); + } + public interface OnMessageFound { void onMessageFound(final Message message); } @@ -720,6 +728,10 @@ public class Conversation extends AbstractEntity implements Blockable { } public long getLastMessageTransmitted() { + long last_clear = getLastClearHistory(); + if (last_clear != 0) { + return last_clear; + } synchronized (this.messages) { for(int i = this.messages.size() - 1; i >= 0; --i) { Message message = this.messages.get(i); diff --git a/src/main/java/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java index f3d891e8..f37ae427 100644 --- a/src/main/java/eu/siacs/conversations/entities/Message.java +++ b/src/main/java/eu/siacs/conversations/entities/Message.java @@ -178,6 +178,14 @@ public class Message extends AbstractEntity { return message; } + public static Message createLoadMoreMessage(Conversation conversation) { + final Message message = new Message(); + message.setType(Message.TYPE_STATUS); + message.setConversation(conversation); + message.setBody("LOAD_MORE"); + return message; + } + @Override public ContentValues getContentValues() { ContentValues values = new ContentValues(); diff --git a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java index a225760f..165c7c2a 100644 --- a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java +++ b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java @@ -24,13 +24,13 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { private final XmppConnectionService mXmppConnectionService; - private final HashSet queries = new HashSet(); - private final ArrayList pendingQueries = new ArrayList(); + private final HashSet queries = new HashSet<>(); + private final ArrayList pendingQueries = new ArrayList<>(); public enum PagingOrder { NORMAL, REVERSE - }; + } public MessageArchiveService(final XmppConnectionService service) { this.mXmppConnectionService = service; @@ -137,7 +137,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { synchronized (MessageArchiveService.this.queries) { MessageArchiveService.this.queries.remove(query); if (query.hasCallback()) { - query.callback(); + query.callback(false); } } } else if (packet.getType() != IqPacket.TYPE.RESULT) { @@ -169,7 +169,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { } } if (query.hasCallback()) { - query.callback(); + query.callback(done); } else { this.mXmppConnectionService.updateConversationUi(); } @@ -329,10 +329,10 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { this.callback = callback; } - public void callback() { + public void callback(boolean done) { if (this.callback != null) { this.callback.onMoreMessagesLoaded(messageCount,conversation); - if (messageCount == 0) { + if (done) { this.callback.informUser(R.string.no_more_history_on_server); } } @@ -375,7 +375,8 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { public String toString() { StringBuilder builder = new StringBuilder(); if (this.muc()) { - builder.append("to="+this.getWith().toString()); + builder.append("to="); + builder.append(this.getWith().toString()); } else { builder.append("with="); if (this.getWith() == null) { diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 73f17e3b..0973036c 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -1215,6 +1215,9 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa public void loadMoreMessages(final Conversation conversation, final long timestamp, final OnMoreMessagesLoaded callback) { if (XmppConnectionService.this.getMessageArchiveService().queryInProgress(conversation, callback)) { return; + } else if (timestamp == 0) { + callback.onMoreMessagesLoaded(0, conversation); + return; } Log.d(Config.LOGTAG, "load more messages for " + conversation.getName() + " prior to " + MessageGenerator.getTimestamp(timestamp)); Runnable runnable = new Runnable() { @@ -2911,6 +2914,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa public void clearConversationHistory(final Conversation conversation) { conversation.clearMessages(); conversation.setHasMessagesLeftOnServer(false); //avoid messages getting loaded through mam + conversation.setLastClearHistory(System.currentTimeMillis()); Runnable runnable = new Runnable() { @Override public void run() { diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java index 2461c39d..97774650 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java @@ -1602,4 +1602,10 @@ public class ConversationActivity extends XmppActivity public boolean highlightSelectedConversations() { return !isConversationsOverviewHideable() || this.conversationWasSelectedByKeyboard; } + + public void setMessagesLoaded() { + if (mConversationFragment != null) { + mConversationFragment.setMessagesLoaded(); + } + } } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index af6f5d6f..42e442ed 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -40,12 +40,9 @@ import net.java.otr4j.session.SessionStatus; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.NoSuchElementException; -import java.util.concurrent.ConcurrentLinkedQueue; import eu.siacs.conversations.Config; import eu.siacs.conversations.R; -import eu.siacs.conversations.crypto.PgpEngine; import eu.siacs.conversations.crypto.axolotl.AxolotlService; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Contact; @@ -318,6 +315,10 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa private ConversationActivity activity; private Message selectedMessage; + public void setMessagesLoaded() { + this.messagesLoaded = true; + } + private void sendMessage() { final String body = mEditMessage.getText().toString(); if (body.length() == 0 || this.conversation == null) { @@ -1008,6 +1009,9 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa protected void updateStatusMessages() { synchronized (this.messageList) { + if (conversation.getLastClearHistory() != 0) { + this.messageList.add(0, Message.createLoadMoreMessage(conversation)); + } if (conversation.getMode() == Conversation.MODE_SINGLE) { ChatState state = conversation.getIncomingChatState(); if (state == ChatState.COMPOSING) { diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index a9234e1a..fc2bd2ab 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -428,6 +428,19 @@ public class MessageAdapter extends ArrayAdapter { viewHolder.image.setOnLongClickListener(openContextMenu); } + private void loadMoreMessages(Conversation conversation) { + conversation.setLastClearHistory(0); + conversation.setHasMessagesLeftOnServer(true); + conversation.setFirstMamReference(null); + long timestamp = conversation.getLastMessageTransmitted(); + if (timestamp == 0) { + timestamp = System.currentTimeMillis(); + } + activity.setMessagesLoaded(); + activity.xmppConnectionService.getMessageArchiveService().query(conversation, 0, timestamp); + Toast.makeText(activity, R.string.fetching_history_from_server,Toast.LENGTH_LONG).show(); + } + @Override public View getView(int position, View view, ViewGroup parent) { final Message message = getItem(position); @@ -484,6 +497,7 @@ public class MessageAdapter extends ArrayAdapter { view = activity.getLayoutInflater().inflate(R.layout.message_status, parent, false); viewHolder.contact_picture = (ImageView) view.findViewById(R.id.message_photo); viewHolder.status_message = (TextView) view.findViewById(R.id.status_message); + viewHolder.load_more_messages = (Button) view.findViewById(R.id.load_more_messages); break; default: viewHolder = null; @@ -500,16 +514,31 @@ public class MessageAdapter extends ArrayAdapter { boolean darkBackground = (type == RECEIVED && (!isInValidSession || !mUseWhiteBackground)); if (type == STATUS) { - if (conversation.getMode() == Conversation.MODE_SINGLE) { - viewHolder.contact_picture.setImageBitmap(activity - .avatarService().get(conversation.getContact(), - activity.getPixel(32))); - viewHolder.contact_picture.setAlpha(0.5f); + if ("LOAD_MORE".equals(message.getBody())) { + viewHolder.status_message.setVisibility(View.GONE); + viewHolder.contact_picture.setVisibility(View.GONE); + viewHolder.load_more_messages.setVisibility(View.VISIBLE); + viewHolder.load_more_messages.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + loadMoreMessages(message.getConversation()); + } + }); + } else { + viewHolder.status_message.setVisibility(View.VISIBLE); + viewHolder.contact_picture.setVisibility(View.VISIBLE); + viewHolder.load_more_messages.setVisibility(View.GONE); + if (conversation.getMode() == Conversation.MODE_SINGLE) { + viewHolder.contact_picture.setImageBitmap(activity + .avatarService().get(conversation.getContact(), + activity.getPixel(32))); + viewHolder.contact_picture.setAlpha(0.5f); + } viewHolder.status_message.setText(message.getBody()); } return view; } else { - loadAvatar(message,viewHolder.contact_picture); + loadAvatar(message, viewHolder.contact_picture); } viewHolder.contact_picture @@ -671,6 +700,7 @@ public class MessageAdapter extends ArrayAdapter { protected ImageView contact_picture; protected TextView status_message; protected TextView encryption; + public Button load_more_messages; } class BitmapWorkerTask extends AsyncTask { diff --git a/src/main/res/layout/message_status.xml b/src/main/res/layout/message_status.xml index ad2579fa..93a80d84 100644 --- a/src/main/res/layout/message_status.xml +++ b/src/main/res/layout/message_status.xml @@ -9,6 +9,15 @@ android:paddingRight="8dp" android:paddingTop="5dp"> +