From cc1402442aaba9ab16b7669790c140fce85e24a0 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 4 May 2017 11:03:58 +0200 Subject: [PATCH] don't load signed prekeys on startup --- .../crypto/axolotl/AxolotlService.java | 2 +- .../crypto/axolotl/SQLiteAxolotlStore.java | 7 ++++--- .../persistance/DatabaseBackend.java | 19 +++++++++++++++++++ .../services/XmppConnectionService.java | 14 +++++++++++--- 4 files changed, 35 insertions(+), 7 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 f8fe76a2f..84df8596b 100644 --- a/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java +++ b/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java @@ -602,7 +602,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { // Validate signedPreKeyRecord + ID SignedPreKeyRecord signedPreKeyRecord; - int numSignedPreKeys = axolotlStore.loadSignedPreKeys().size(); + int numSignedPreKeys = axolotlStore.getSignedPreKeysCount(); try { signedPreKeyRecord = axolotlStore.loadSignedPreKey(bundle.getSignedPreKeyId()); if (flush diff --git a/src/main/java/eu/siacs/conversations/crypto/axolotl/SQLiteAxolotlStore.java b/src/main/java/eu/siacs/conversations/crypto/axolotl/SQLiteAxolotlStore.java index d55ebb52b..eb08832ee 100644 --- a/src/main/java/eu/siacs/conversations/crypto/axolotl/SQLiteAxolotlStore.java +++ b/src/main/java/eu/siacs/conversations/crypto/axolotl/SQLiteAxolotlStore.java @@ -79,9 +79,6 @@ public class SQLiteAxolotlStore implements AxolotlStore { this.mXmppConnectionService = service; this.localRegistrationId = loadRegistrationId(); this.currentPreKeyId = loadCurrentPreKeyId(); - for (SignedPreKeyRecord record : loadSignedPreKeys()) { - Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Got Axolotl signed prekey record:" + record.getId()); - } } public int getCurrentPreKeyId() { @@ -415,6 +412,10 @@ public class SQLiteAxolotlStore implements AxolotlStore { return mXmppConnectionService.databaseBackend.loadSignedPreKeys(account); } + public int getSignedPreKeysCount() { + return mXmppConnectionService.databaseBackend.getSignedPreKeysCount(account); + } + /** * Store a local SignedPreKeyRecord. * diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java index 3ee1acb80..6822ab42e 100644 --- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -1086,6 +1086,25 @@ public class DatabaseBackend extends SQLiteOpenHelper { return prekeys; } + public int getSignedPreKeysCount(Account account) { + String[] columns = {"count("+SQLiteAxolotlStore.KEY+")"}; + String[] selectionArgs = {account.getUuid()}; + SQLiteDatabase db = this.getReadableDatabase(); + Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME, + columns, + SQLiteAxolotlStore.ACCOUNT + "=?", + selectionArgs, + null, null, null); + final int count; + if (cursor.moveToFirst()) { + count = cursor.getInt(0); + } else { + count = 0; + } + cursor.close(); + return count; + } + public boolean containsSignedPreKey(Account account, int signedPreKeyId) { Cursor cursor = getCursorForPreKey(account, signedPreKeyId); int count = cursor.getCount(); diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 28ccfd7da..38f0150d7 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -991,7 +991,9 @@ public class XmppConnectionService extends Service { } }; + Log.d(Config.LOGTAG,"initializing database..."); this.databaseBackend = DatabaseBackend.getInstance(getApplicationContext()); + Log.d(Config.LOGTAG,"restoring accounts..."); this.accounts = databaseBackend.getAccounts(); if (Config.FREQUENT_RESTARTS_THRESHOLD != 0 @@ -1449,6 +1451,8 @@ public class XmppConnectionService extends Service { for (Account account : this.accounts) { accountLookupTable.put(account.getUuid(), account); } + Log.d(Config.LOGTAG,"restoring conversations..."); + final long startTimeConversationsRestore = SystemClock.elapsedRealtime(); this.conversations.addAll(databaseBackend.getConversations(Conversation.STATUS_AVAILABLE)); for(Iterator iterator = conversations.listIterator(); iterator.hasNext();) { Conversation conversation = iterator.next(); @@ -1460,6 +1464,8 @@ public class XmppConnectionService extends Service { iterator.remove(); } } + long diffConversationsRestore = SystemClock.elapsedRealtime() - startTimeConversationsRestore; + Log.d(Config.LOGTAG,"finished restoring conversations in "+diffConversationsRestore+"ms"); Runnable runnable = new Runnable() { @Override public void run() { @@ -1469,14 +1475,15 @@ public class XmppConnectionService extends Service { Log.d(Config.LOGTAG, "deleting messages that are older than "+AbstractGenerator.getTimestamp(deletionDate)); databaseBackend.expireOldMessages(deletionDate); } - Log.d(Config.LOGTAG, "restoring roster"); + Log.d(Config.LOGTAG,"restoring roster..."); for (Account account : accounts) { databaseBackend.readRoster(account.getRoster()); account.initAccountServices(XmppConnectionService.this); //roster needs to be loaded at this stage } getBitmapCache().evictAll(); loadPhoneContacts(); - Log.d(Config.LOGTAG, "restoring messages"); + Log.d(Config.LOGTAG, "restoring messages..."); + final long startMessageRestore = SystemClock.elapsedRealtime(); for (Conversation conversation : conversations) { conversation.addAll(0, databaseBackend.getMessages(conversation, Config.PAGE_SIZE)); checkDeletedFiles(conversation); @@ -1496,7 +1503,8 @@ public class XmppConnectionService extends Service { } mNotificationService.finishBacklog(false); mRestoredFromDatabase = true; - Log.d(Config.LOGTAG, "restored all messages"); + final long diffMessageRestore = SystemClock.elapsedRealtime() - startMessageRestore; + Log.d(Config.LOGTAG, "finished restoring messages in "+diffMessageRestore+"ms"); updateConversationUi(); } };