create a new axolotl service when the account jid changes

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java | 13 
src/main/java/eu/siacs/conversations/entities/Account.java              | 11 
src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java   |  2 
3 files changed, 24 insertions(+), 2 deletions(-)

Detailed changes

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();
 	}

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() {

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);