From 57c11d42d5da20d04785813aaabe41d1f8494525 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 10 Aug 2017 14:17:47 +0200 Subject: [PATCH] create a new axolotl service when the account jid changes --- .../crypto/axolotl/AxolotlService.java | 13 +++++++++++++ .../eu/siacs/conversations/entities/Account.java | 11 ++++++++++- .../conversations/persistance/DatabaseBackend.java | 2 +- 3 files changed, 24 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 6c89d9d5f..5197c419a 100644 --- a/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java +++ b/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java @@ -263,6 +263,9 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { } public AxolotlService(Account account, XmppConnectionService connectionService) { + if (account == null || connectionService == null) { + throw new IllegalArgumentException("account and service cannot be null"); + } if (Security.getProvider("BC") == null) { Security.addProvider(new BouncyCastleProvider()); } @@ -362,6 +365,16 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { publishBundlesIfNeeded(true, wipeOther); } + public void destroy() { + Log.d(Config.LOGTAG,account.getJid().toBareJid()+": destroying old axolotl service. no longer in use"); + mXmppConnectionService.databaseBackend.wipeAxolotlDb(account); + } + + public AxolotlService makeNew() { + Log.d(Config.LOGTAG,account.getJid().toBareJid()+": make new axolotl service"); + return new AxolotlService(this.account,this.mXmppConnectionService); + } + public int getOwnDeviceId() { return axolotlStore.getLocalRegistrationId(); } diff --git a/src/main/java/eu/siacs/conversations/entities/Account.java b/src/main/java/eu/siacs/conversations/entities/Account.java index 08975db66..d25deab40 100644 --- a/src/main/java/eu/siacs/conversations/entities/Account.java +++ b/src/main/java/eu/siacs/conversations/entities/Account.java @@ -312,8 +312,17 @@ public class Account extends AbstractEntity { public boolean setJid(final Jid next) { final Jid prev = this.jid != null ? this.jid.toBareJid() : null; + final boolean changed = prev == null || (next != null && !prev.equals(next.toBareJid())); + if (changed) { + final AxolotlService oldAxolotlService = this.axolotlService; + if (oldAxolotlService != null) { + oldAxolotlService.destroy(); + this.jid = next; + this.axolotlService = oldAxolotlService.makeNew(); + } + } this.jid = next; - return prev == null || (next != null && !prev.equals(next.toBareJid())); + return changed; } public Jid getServer() { diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java index d9e431034..9d5eda22b 100644 --- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -1389,7 +1389,7 @@ public class DatabaseBackend extends SQLiteOpenHelper { } - public void recreateAxolotlDb(SQLiteDatabase db) { + private void recreateAxolotlDb(SQLiteDatabase db) { Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + ">>> (RE)CREATING AXOLOTL DATABASE <<<"); db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.SESSION_TABLENAME); db.execSQL(CREATE_SESSIONS_STATEMENT);