From 008f747b956d85055d3b6a2b213eeec90b67729c Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 12 Sep 2016 22:48:51 +0200 Subject: don't use display version of jids --- .../messenger/persistance/DatabaseBackend.java | 174 +++++++++++---------- .../java/de/pixart/messenger/xmpp/jid/Jid.java | 24 ++- 2 files changed, 101 insertions(+), 97 deletions(-) (limited to 'src') diff --git a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java index 99ff1a5fb..b1ed26eba 100644 --- a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java +++ b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java @@ -11,7 +11,6 @@ import android.util.Base64; import android.util.Log; import android.util.Pair; -import org.json.JSONException; import org.whispersystems.libaxolotl.AxolotlAddress; import org.whispersystems.libaxolotl.IdentityKey; import org.whispersystems.libaxolotl.IdentityKeyPair; @@ -32,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 de.pixart.messenger.Config; import de.pixart.messenger.crypto.axolotl.AxolotlService; @@ -51,8 +51,8 @@ public class DatabaseBackend extends SQLiteOpenHelper { private static DatabaseBackend instance = null; - public static final String DATABASE_NAME = "history"; - public static final int DATABASE_VERSION = 27; + private static final String DATABASE_NAME = "history"; + private static final int DATABASE_VERSION = 28; private static String CREATE_CONTATCS_STATEMENT = "create table " + Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, " @@ -250,86 +250,7 @@ public class DatabaseBackend extends SQLiteOpenHelper { db.execSQL("update " + Account.TABLENAME + " set " + Account.ROSTERVERSION + " = NULL"); } if (oldVersion < 14 && newVersion >= 14) { - // migrate db to new, canonicalized JID domainpart representation - - // Conversation table - Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME, new String[0]); - while (cursor.moveToNext()) { - String newJid; - try { - newJid = Jid.fromString( - cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID)) - ).toString(); - } catch (InvalidJidException ignored) { - Log.e(Config.LOGTAG, "Failed to migrate Conversation CONTACTJID " - + cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID)) - + ": " + ignored + ". Skipping..."); - continue; - } - - String updateArgs[] = { - newJid, - cursor.getString(cursor.getColumnIndex(Conversation.UUID)), - }; - db.execSQL("update " + Conversation.TABLENAME - + " set " + Conversation.CONTACTJID + " = ? " - + " where " + Conversation.UUID + " = ?", updateArgs); - } - cursor.close(); - - // Contact table - cursor = db.rawQuery("select * from " + Contact.TABLENAME, new String[0]); - while (cursor.moveToNext()) { - String newJid; - try { - newJid = Jid.fromString( - cursor.getString(cursor.getColumnIndex(Contact.JID)) - ).toString(); - } catch (InvalidJidException ignored) { - Log.e(Config.LOGTAG, "Failed to migrate Contact JID " - + cursor.getString(cursor.getColumnIndex(Contact.JID)) - + ": " + ignored + ". Skipping..."); - continue; - } - - String updateArgs[] = { - newJid, - cursor.getString(cursor.getColumnIndex(Contact.ACCOUNT)), - cursor.getString(cursor.getColumnIndex(Contact.JID)), - }; - db.execSQL("update " + Contact.TABLENAME - + " set " + Contact.JID + " = ? " - + " where " + Contact.ACCOUNT + " = ? " - + " AND " + Contact.JID + " = ?", updateArgs); - } - cursor.close(); - - // Account table - cursor = db.rawQuery("select * from " + Account.TABLENAME, new String[0]); - while (cursor.moveToNext()) { - String newServer; - try { - newServer = Jid.fromParts( - cursor.getString(cursor.getColumnIndex(Account.USERNAME)), - cursor.getString(cursor.getColumnIndex(Account.SERVER)), - "mobile" - ).getDomainpart(); - } catch (InvalidJidException ignored) { - Log.e(Config.LOGTAG, "Failed to migrate Account SERVER " - + cursor.getString(cursor.getColumnIndex(Account.SERVER)) - + ": " + ignored + ". Skipping..."); - continue; - } - - String updateArgs[] = { - newServer, - cursor.getString(cursor.getColumnIndex(Account.UUID)), - }; - db.execSQL("update " + Account.TABLENAME - + " set " + Account.SERVER + " = ? " - + " where " + Account.UUID + " = ?", updateArgs); - } - cursor.close(); + canonicalizeJids(db); } if (oldVersion < 15 && newVersion >= 15) { recreateAxolotlDb(db); @@ -406,6 +327,93 @@ public class DatabaseBackend extends SQLiteOpenHelper { if (oldVersion < 27 && newVersion >= 27) { db.execSQL("DELETE FROM "+ServiceDiscoveryResult.TABLENAME); } + + if (oldVersion < 28 && newVersion >= 28) { + canonicalizeJids(db); + } + } + + private void canonicalizeJids(SQLiteDatabase db) { + // migrate db to new, canonicalized JID domainpart representation + + // Conversation table + Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME, new String[0]); + while (cursor.moveToNext()) { + String newJid; + try { + newJid = Jid.fromString( + cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID)) + ).toString(); + } catch (InvalidJidException ignored) { + Log.e(Config.LOGTAG, "Failed to migrate Conversation CONTACTJID " + + cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID)) + + ": " + ignored + ". Skipping..."); + continue; + } + + String updateArgs[] = { + newJid, + cursor.getString(cursor.getColumnIndex(Conversation.UUID)), + }; + db.execSQL("update " + Conversation.TABLENAME + + " set " + Conversation.CONTACTJID + " = ? " + + " where " + Conversation.UUID + " = ?", updateArgs); + } + cursor.close(); + + // Contact table + cursor = db.rawQuery("select * from " + Contact.TABLENAME, new String[0]); + while (cursor.moveToNext()) { + String newJid; + try { + newJid = Jid.fromString( + cursor.getString(cursor.getColumnIndex(Contact.JID)) + ).toString(); + } catch (InvalidJidException ignored) { + Log.e(Config.LOGTAG, "Failed to migrate Contact JID " + + cursor.getString(cursor.getColumnIndex(Contact.JID)) + + ": " + ignored + ". Skipping..."); + continue; + } + + String updateArgs[] = { + newJid, + cursor.getString(cursor.getColumnIndex(Contact.ACCOUNT)), + cursor.getString(cursor.getColumnIndex(Contact.JID)), + }; + db.execSQL("update " + Contact.TABLENAME + + " set " + Contact.JID + " = ? " + + " where " + Contact.ACCOUNT + " = ? " + + " AND " + Contact.JID + " = ?", updateArgs); + } + cursor.close(); + + // Account table + cursor = db.rawQuery("select * from " + Account.TABLENAME, new String[0]); + while (cursor.moveToNext()) { + String newServer; + try { + newServer = Jid.fromParts( + cursor.getString(cursor.getColumnIndex(Account.USERNAME)), + cursor.getString(cursor.getColumnIndex(Account.SERVER)), + "mobile" + ).getDomainpart(); + } catch (InvalidJidException ignored) { + Log.e(Config.LOGTAG, "Failed to migrate Account SERVER " + + cursor.getString(cursor.getColumnIndex(Account.SERVER)) + + ": " + ignored + ". Skipping..."); + continue; + } + + String updateArgs[] = { + newServer, + cursor.getString(cursor.getColumnIndex(Account.UUID)), + }; + db.execSQL("update " + Account.TABLENAME + + " set " + Account.SERVER + " = ? " + + " where " + Account.UUID + " = ?", updateArgs); + } + cursor.close(); } public static synchronized DatabaseBackend getInstance(Context context) { diff --git a/src/main/java/de/pixart/messenger/xmpp/jid/Jid.java b/src/main/java/de/pixart/messenger/xmpp/jid/Jid.java index 2f5451364..ffbd754ee 100644 --- a/src/main/java/de/pixart/messenger/xmpp/jid/Jid.java +++ b/src/main/java/de/pixart/messenger/xmpp/jid/Jid.java @@ -21,10 +21,6 @@ public final class Jid { private final String domainpart; private final String resourcepart; - // It's much more efficient to store the ful JID as well as the parts instead of figuring them - // all out every time (since some characters are displayed but aren't used for comparisons). - private final String displayjid; - public String getLocalpart() { return localpart; } @@ -73,7 +69,6 @@ public final class Jid { Jid fromCache = Jid.cache.get(jid); if (fromCache != null) { - displayjid = fromCache.displayjid; localpart = fromCache.localpart; domainpart = fromCache.domainpart; resourcepart = fromCache.resourcepart; @@ -94,8 +89,6 @@ public final class Jid { throw new InvalidJidException(InvalidJidException.INVALID_CHARACTER); } - String finaljid; - final int domainpartStart; final int atLoc = jid.indexOf("@"); final int slashLoc = jid.indexOf("/"); @@ -103,7 +96,6 @@ public final class Jid { // or there are one or more "@" signs but they're all in the resourcepart (eg. "example.net/@/rp@"): if (atCount == 0 || (atCount > 0 && slashLoc != -1 && atLoc > slashLoc)) { localpart = ""; - finaljid = ""; domainpartStart = 0; } else { final String lp = jid.substring(0, atLoc); @@ -116,7 +108,6 @@ public final class Jid { throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH); } domainpartStart = atLoc + 1; - finaljid = lp + "@"; } final String dp; @@ -135,7 +126,6 @@ public final class Jid { } catch (final StringprepException e) { throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e); } - finaljid = finaljid + dp + "/" + rp; } else { resourcepart = ""; try{ @@ -143,7 +133,6 @@ public final class Jid { } catch (final StringprepException e) { throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e); } - finaljid = finaljid + dp; } // Remove trailing "." before storing the domain part. @@ -167,8 +156,6 @@ public final class Jid { } Jid.cache.put(jid, this); - - this.displayjid = finaljid; } public Jid toBareJid() { @@ -191,7 +178,16 @@ public final class Jid { @Override public String toString() { - return displayjid; + String out; + if (hasLocalpart()) { + out = localpart + '@' + domainpart; + } else { + out = domainpart; + } + if (!resourcepart.isEmpty()) { + out += '/'+resourcepart; + } + return out; } @Override -- cgit v1.2.3