From 8adf4a8a64f925610a1156963da33d3fb70a0de1 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Thu, 14 Jun 2018 15:23:20 +0200 Subject: [PATCH] move notification of key changes into DAOs --- .../AutocryptPeerDataAccessObject.java | 11 ++- .../provider/DatabaseNotifyManager.java | 42 +++++++++++ .../keychain/provider/KeyRepository.java | 16 ++--- .../provider/KeyWritableRepository.java | 44 +++++++----- .../keychain/provider/KeychainProvider.java | 70 +++---------------- .../provider/LastUpdateInteractor.java | 10 ++- .../remote/KeychainExternalProvider.java | 16 ++++- ...tusLoader.java => KeyserverStatusDao.java} | 0 .../keychain/util/DatabaseUtil.java | 29 ++++++++ .../keychain/util/DatabaseUtils.java | 34 --------- .../keychain/provider/InteropTest.java | 3 +- 11 files changed, 146 insertions(+), 129 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/DatabaseNotifyManager.java rename OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/keyview/loader/{KeyserverStatusLoader.java => KeyserverStatusDao.java} (100%) delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/DatabaseUtils.java diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/AutocryptPeerDataAccessObject.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/AutocryptPeerDataAccessObject.java index 32c8c3ee1..d7d4707dd 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/AutocryptPeerDataAccessObject.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/AutocryptPeerDataAccessObject.java @@ -72,10 +72,11 @@ public class AutocryptPeerDataAccessObject { private final SimpleContentResolverInterface queryInterface; private final String packageName; - + private final DatabaseNotifyManager databaseNotifyManager; public AutocryptPeerDataAccessObject(Context context, String packageName) { this.packageName = packageName; + this.databaseNotifyManager = DatabaseNotifyManager.create(context); final ContentResolver contentResolver = context.getContentResolver(); queryInterface = new SimpleContentResolverInterface() { @@ -102,9 +103,11 @@ public class AutocryptPeerDataAccessObject { }; } - public AutocryptPeerDataAccessObject(SimpleContentResolverInterface queryInterface, String packageName) { + public AutocryptPeerDataAccessObject(SimpleContentResolverInterface queryInterface, String packageName, + DatabaseNotifyManager databaseNotifyManager) { this.queryInterface = queryInterface; this.packageName = packageName; + this.databaseNotifyManager = databaseNotifyManager; } public Long getMasterKeyIdForAutocryptPeer(String autocryptId) { @@ -190,6 +193,7 @@ public class AutocryptPeerDataAccessObject { cv.put(ApiAutocryptPeer.IS_MUTUAL, isMutual ? 1 : 0); queryInterface .update(ApiAutocryptPeer.buildByPackageNameAndAutocryptId(packageName, autocryptId), cv, null, null); + databaseNotifyManager.notifyAutocryptUpdate(autocryptId, masterKeyId); } public void updateKeyGossipFromAutocrypt(String autocryptId, Date effectiveDate, long masterKeyId) { @@ -211,10 +215,13 @@ public class AutocryptPeerDataAccessObject { cv.put(ApiAutocryptPeer.GOSSIP_ORIGIN, origin); queryInterface .update(ApiAutocryptPeer.buildByPackageNameAndAutocryptId(packageName, autocryptId), cv, null, null); + databaseNotifyManager.notifyAutocryptUpdate(autocryptId, masterKeyId); } public void delete(String autocryptId) { + Long masterKeyId = getMasterKeyIdForAutocryptPeer(autocryptId); queryInterface.delete(ApiAutocryptPeer.buildByPackageNameAndAutocryptId(packageName, autocryptId), null, null); + databaseNotifyManager.notifyAutocryptDelete(autocryptId, masterKeyId); } public List determineAutocryptRecommendations(String... autocryptIds) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/DatabaseNotifyManager.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/DatabaseNotifyManager.java new file mode 100644 index 000000000..b283dd576 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/DatabaseNotifyManager.java @@ -0,0 +1,42 @@ +package org.sufficientlysecure.keychain.provider; + + +import android.content.ContentResolver; +import android.content.Context; +import android.net.Uri; + +import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; + + +public class DatabaseNotifyManager { + private ContentResolver contentResolver; + + public static DatabaseNotifyManager create(Context context) { + ContentResolver contentResolver = context.getContentResolver(); + return new DatabaseNotifyManager(contentResolver); + } + + private DatabaseNotifyManager(ContentResolver contentResolver) { + this.contentResolver = contentResolver; + } + + public void notifyKeyChange(long masterKeyId) { + Uri uri = KeyRings.buildGenericKeyRingUri(masterKeyId); + contentResolver.notifyChange(uri, null); + } + + public void notifyAutocryptDelete(String autocryptId, Long masterKeyId) { + Uri uri = KeyRings.buildGenericKeyRingUri(masterKeyId); + contentResolver.notifyChange(uri, null); + } + + public void notifyAutocryptUpdate(String autocryptId, long masterKeyId) { + Uri uri = KeyRings.buildGenericKeyRingUri(masterKeyId); + contentResolver.notifyChange(uri, null); + } + + public void notifyKeyserverStatusChange(long masterKeyId) { + Uri uri = KeyRings.buildGenericKeyRingUri(masterKeyId); + contentResolver.notifyChange(uri, null); + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyRepository.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyRepository.java index ac71b52b2..2f1847a17 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyRepository.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyRepository.java @@ -53,7 +53,7 @@ public class KeyRepository { public static final int FIELD_TYPE_STRING = 4; public static final int FIELD_TYPE_BLOB = 5; - final ContentResolver mContentResolver; + final ContentResolver contentResolver; final LocalPublicKeyStorage mLocalPublicKeyStorage; OperationLog mLog; int mIndent; @@ -71,7 +71,7 @@ public class KeyRepository { KeyRepository(ContentResolver contentResolver, LocalPublicKeyStorage localPublicKeyStorage, OperationLog log, int indent) { - mContentResolver = contentResolver; + this.contentResolver = contentResolver; mLocalPublicKeyStorage = localPublicKeyStorage; mIndent = indent; mLog = log; @@ -121,7 +121,7 @@ public class KeyRepository { private HashMap getGenericData(Uri uri, String[] proj, int[] types, String selection) throws NotFoundException { - Cursor cursor = mContentResolver.query(uri, proj, selection, null, null); + Cursor cursor = contentResolver.query(uri, proj, selection, null, null); try { HashMap result = new HashMap<>(proj.length); @@ -184,7 +184,7 @@ public class KeyRepository { } public CanonicalizedPublicKeyRing getCanonicalizedPublicKeyRing(Uri queryUri) throws NotFoundException { - Cursor cursor = mContentResolver.query(queryUri, + Cursor cursor = contentResolver.query(queryUri, new String[] { KeyRings.MASTER_KEY_ID, KeyRings.VERIFIED }, null, null, null); try { if (cursor != null && cursor.moveToFirst()) { @@ -208,7 +208,7 @@ public class KeyRepository { } public CanonicalizedSecretKeyRing getCanonicalizedSecretKeyRing(Uri queryUri) throws NotFoundException { - Cursor cursor = mContentResolver.query(queryUri, + Cursor cursor = contentResolver.query(queryUri, new String[] { KeyRings.MASTER_KEY_ID, KeyRings.VERIFIED, KeyRings.HAS_ANY_SECRET }, null, null, null); try { if (cursor != null && cursor.moveToFirst()) { @@ -232,7 +232,7 @@ public class KeyRepository { } public ArrayList getConfirmedUserIds(long masterKeyId) throws NotFoundException { - Cursor cursor = mContentResolver.query(UserPackets.buildUserIdsUri(masterKeyId), + Cursor cursor = contentResolver.query(UserPackets.buildUserIdsUri(masterKeyId), new String[]{UserPackets.USER_ID}, UserPackets.VERIFIED + " = " + Certs.VERIFIED_SECRET, null, null ); if (cursor == null) { @@ -276,12 +276,12 @@ public class KeyRepository { } public ContentResolver getContentResolver() { - return mContentResolver; + return contentResolver; } @Nullable Long getLastUpdateTime(long masterKeyId) { - Cursor lastUpdatedCursor = mContentResolver.query( + Cursor lastUpdatedCursor = contentResolver.query( UpdatedKeys.CONTENT_URI, new String[] { UpdatedKeys.LAST_UPDATED }, UpdatedKeys.MASTER_KEY_ID + " = ?", diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyWritableRepository.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyWritableRepository.java index 9aff7d75a..373332d26 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyWritableRepository.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyWritableRepository.java @@ -83,32 +83,38 @@ import timber.log.Timber; public class KeyWritableRepository extends KeyRepository { private static final int MAX_CACHED_KEY_SIZE = 1024 * 50; - private final Context mContext; + private final Context context; private final LastUpdateInteractor lastUpdateInteractor; + private DatabaseNotifyManager databaseNotifyManager; public static KeyWritableRepository create(Context context) { LocalPublicKeyStorage localPublicKeyStorage = LocalPublicKeyStorage.getInstance(context); LastUpdateInteractor lastUpdateInteractor = LastUpdateInteractor.create(context); + DatabaseNotifyManager databaseNotifyManager = DatabaseNotifyManager.create(context); - return new KeyWritableRepository(context, localPublicKeyStorage, lastUpdateInteractor); + return new KeyWritableRepository(context, localPublicKeyStorage, lastUpdateInteractor, + databaseNotifyManager); } @VisibleForTesting KeyWritableRepository(Context context, LocalPublicKeyStorage localPublicKeyStorage, - LastUpdateInteractor lastUpdateInteractor) { - this(context, localPublicKeyStorage, lastUpdateInteractor, new OperationLog(), 0); + LastUpdateInteractor lastUpdateInteractor, DatabaseNotifyManager databaseNotifyManager) { + this(context, localPublicKeyStorage, lastUpdateInteractor, new OperationLog(), 0, + databaseNotifyManager); } private KeyWritableRepository(Context context, LocalPublicKeyStorage localPublicKeyStorage, - LastUpdateInteractor lastUpdateInteractor, OperationLog log, int indent) { + LastUpdateInteractor lastUpdateInteractor, OperationLog log, int indent, + DatabaseNotifyManager databaseNotifyManager) { super(context.getContentResolver(), localPublicKeyStorage, log, indent); - mContext = context; + this.context = context; + this.databaseNotifyManager = databaseNotifyManager; this.lastUpdateInteractor = lastUpdateInteractor; } private LongSparseArray getTrustedMasterKeys() { - Cursor cursor = mContentResolver.query(KeyRings.buildUnifiedKeyRingsUri(), new String[] { + Cursor cursor = contentResolver.query(KeyRings.buildUnifiedKeyRingsUri(), new String[] { KeyRings.MASTER_KEY_ID, // we pick from cache only information that is not easily available from keyrings KeyRings.HAS_ANY_SECRET, KeyRings.VERIFIED @@ -530,7 +536,7 @@ public class KeyWritableRepository extends KeyRepository { try { // delete old version of this keyRing (from database only!), which also deletes all keys and userIds on cascade - int deleted = mContentResolver.delete( + int deleted = contentResolver.delete( KeyRingData.buildPublicKeyRingUri(masterKeyId), null, null); if (deleted > 0) { log(LogType.MSG_IP_DELETE_OLD_OK); @@ -540,7 +546,8 @@ public class KeyWritableRepository extends KeyRepository { } log(LogType.MSG_IP_APPLY_BATCH); - mContentResolver.applyBatch(KeychainContract.CONTENT_AUTHORITY, operations); + contentResolver.applyBatch(KeychainContract.CONTENT_AUTHORITY, operations); + databaseNotifyManager.notifyKeyChange(masterKeyId); log(LogType.MSG_IP_SUCCESS); return result; @@ -603,7 +610,7 @@ public class KeyWritableRepository extends KeyRepository { // insert new version of this keyRing Uri uri = KeyRingData.buildSecretKeyRingUri(masterKeyId); - return mContentResolver.insert(uri, values); + return contentResolver.insert(uri, values); } public boolean deleteKeyRing(long masterKeyId) { @@ -613,8 +620,11 @@ public class KeyWritableRepository extends KeyRepository { Timber.e(e, "Could not delete file!"); return false; } - mContentResolver.delete(ApiAutocryptPeer.buildByMasterKeyId(masterKeyId),null, null); - int deletedRows = mContentResolver.delete(KeyRingData.buildPublicKeyRingUri(masterKeyId), null, null); + contentResolver.delete(ApiAutocryptPeer.buildByMasterKeyId(masterKeyId),null, null); + int deletedRows = contentResolver.delete(KeyRingData.buildPublicKeyRingUri(masterKeyId), null, null); + + databaseNotifyManager.notifyKeyChange(masterKeyId); + return deletedRows > 0; } @@ -690,7 +700,7 @@ public class KeyWritableRepository extends KeyRepository { // first, mark all keys as not available ContentValues values = new ContentValues(); values.put(Keys.HAS_SECRET, SecretKeyType.GNU_DUMMY.getNum()); - mContentResolver.update(uri, values, null, null); + contentResolver.update(uri, values, null, null); // then, mark exactly the keys we have available log(LogType.MSG_IS_IMPORTING_SUBKEYS); @@ -699,7 +709,7 @@ public class KeyWritableRepository extends KeyRepository { long id = sub.getKeyId(); SecretKeyType mode = sub.getSecretKeyTypeSuperExpensive(); values.put(Keys.HAS_SECRET, mode.getNum()); - int upd = mContentResolver.update(uri, values, Keys.KEY_ID + " = ?", + int upd = contentResolver.update(uri, values, Keys.KEY_ID + " = ?", new String[]{Long.toString(id)}); if (upd == 1) { switch (mode) { @@ -1030,11 +1040,11 @@ public class KeyWritableRepository extends KeyRepository { log.add(LogType.MSG_TRUST, 0); Cursor cursor; - Preferences preferences = Preferences.getPreferences(mContext); + Preferences preferences = Preferences.getPreferences(context); boolean isTrustDbInitialized = preferences.isKeySignaturesTableInitialized(); if (!isTrustDbInitialized) { log.add(LogType.MSG_TRUST_INITIALIZE, 1); - cursor = mContentResolver.query(KeyRings.buildUnifiedKeyRingsUri(), + cursor = contentResolver.query(KeyRings.buildUnifiedKeyRingsUri(), new String[] { KeyRings.MASTER_KEY_ID }, null, null, null); } else { String[] signerMasterKeyIdStrings = new String[signerMasterKeyIds.size()]; @@ -1044,7 +1054,7 @@ public class KeyWritableRepository extends KeyRepository { signerMasterKeyIdStrings[i++] = Long.toString(masterKeyId); } - cursor = mContentResolver.query(KeyRings.buildUnifiedKeyRingsFilterBySigner(), + cursor = contentResolver.query(KeyRings.buildUnifiedKeyRingsFilterBySigner(), new String[] { KeyRings.MASTER_KEY_ID }, null, signerMasterKeyIdStrings, null); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainProvider.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainProvider.java index a3d160151..d581de0f5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainProvider.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainProvider.java @@ -50,6 +50,7 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.UpdatedKeys; import org.sufficientlysecure.keychain.provider.KeychainContract.UserPackets; import org.sufficientlysecure.keychain.provider.KeychainContract.UserPacketsColumns; import org.sufficientlysecure.keychain.provider.KeychainDatabase.Tables; +import org.sufficientlysecure.keychain.util.DatabaseUtil; import timber.log.Timber; import static android.database.DatabaseUtils.dumpCursorToString; @@ -101,7 +102,7 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe String authority = KeychainContract.CONTENT_AUTHORITY; - /** + /* * list key_rings * *
@@ -124,7 +125,7 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
                         + "/" + KeychainContract.PATH_USER_IDS,
                 KEY_RINGS_USER_IDS);
 
-        /**
+        /*
          * find by criteria other than master key id
          *
          * key_rings/find/email/_
@@ -144,7 +145,7 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
                         + KeychainContract.PATH_FILTER + "/" + KeychainContract.PATH_BY_SIGNER,
                 KEY_RINGS_FILTER_BY_SIGNER);
 
-        /**
+        /*
          * list key_ring specifics
          *
          * 
@@ -189,7 +190,7 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
                         + KeychainContract.PATH_CERTS + "/*/*",
                 KEY_RING_CERTS_SPECIFIC);
 
-        /**
+        /*
          * API apps
          *
          * 
@@ -205,7 +206,7 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
         matcher.addURI(authority, KeychainContract.BASE_API_APPS + "/*/"
                 + KeychainContract.PATH_ALLOWED_KEYS, API_ALLOWED_KEYS);
 
-        /**
+        /*
          * Trust Identity access
          *
          * 
@@ -221,7 +222,7 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
                 KeychainContract.PATH_BY_PACKAGE_NAME + "/*/*", AUTOCRYPT_PEERS_BY_PACKAGE_NAME_AND_TRUST_ID);
 
 
-        /**
+        /*
          * to access table containing last updated dates of keys
          */
         matcher.addURI(authority, KeychainContract.BASE_UPDATED_KEYS, UPDATED_KEYS);
@@ -835,7 +836,7 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
 
         SQLiteDatabase db = getDb().getReadableDatabase();
 
-        Cursor cursor = qb.query(db, projection, selection, selectionArgs, groupBy, having, orderBy);
+        Cursor cursor = qb.query(db, projection, selection, selectionArgs, groupBy, null, orderBy);
         if (cursor != null) {
             // Tell the cursor what uri to watch, so it knows when its source data changes
             cursor.setNotificationUri(getContext().getContentResolver(), uri);
@@ -849,27 +850,7 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
 
         if (Constants.DEBUG && Constants.DEBUG_EXPLAIN_QUERIES) {
             String rawQuery = qb.buildQuery(projection, selection, groupBy, having, orderBy, null);
-            Cursor explainCursor = db.rawQuery("EXPLAIN QUERY PLAN " + rawQuery, selectionArgs);
-
-            // this is a debugging feature, we can be a little careless
-            explainCursor.moveToFirst();
-
-            StringBuilder line = new StringBuilder();
-            for (int i = 0; i < explainCursor.getColumnCount(); i++) {
-                line.append(explainCursor.getColumnName(i)).append(", ");
-            }
-            Timber.d(line.toString());
-
-            while (!explainCursor.isAfterLast()) {
-                line = new StringBuilder();
-                for (int i = 0; i < explainCursor.getColumnCount(); i++) {
-                    line.append(explainCursor.getString(i)).append(", ");
-                }
-                Timber.d(line.toString());
-                explainCursor.moveToNext();
-            }
-
-            explainCursor.close();
+            DatabaseUtil.explainQuery(db, rawQuery);
         }
 
         return cursor;
@@ -966,9 +947,6 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
                 rowUri = uri;
             }
 
-            // notify of changes in db
-            getContext().getContentResolver().notifyChange(uri, null);
-
         } catch (SQLiteConstraintException e) {
             Timber.d(e, "Constraint exception on insert! Entry already existing?");
         }
@@ -1003,7 +981,6 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
                 }
                 // corresponding keys and userIds are deleted by ON DELETE CASCADE
                 count = db.delete(Tables.KEY_RINGS_PUBLIC, selection, selectionArgs);
-                contentResolver.notifyChange(KeyRings.buildGenericKeyRingUri(uri.getPathSegments().get(1)), null);
                 break;
             }
             case KEY_RING_SECRET: {
@@ -1013,7 +990,6 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
                     selection += " AND (" + additionalSelection + ")";
                 }
                 count = db.delete(Tables.KEY_RINGS_SECRET, selection, selectionArgs);
-                contentResolver.notifyChange(KeyRings.buildGenericKeyRingUri(uri.getPathSegments().get(1)), null);
                 break;
             }
 
@@ -1024,20 +1000,7 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
                 String selection = ApiAutocryptPeer.PACKAGE_NAME + " = ? AND " + ApiAutocryptPeer.IDENTIFIER + " = ?";
                 selectionArgs = new String[] { packageName, autocryptPeer };
 
-                Cursor cursor = db.query(Tables.API_AUTOCRYPT_PEERS, new String[] { ApiAutocryptPeer.MASTER_KEY_ID },
-                        selection, selectionArgs, null, null, null);
-                Long masterKeyId = null;
-                if (cursor != null && cursor.moveToNext() && !cursor.isNull(0)) {
-                    masterKeyId = cursor.getLong(0);
-                }
-
                 count = db.delete(Tables.API_AUTOCRYPT_PEERS, selection, selectionArgs);
-
-                if (masterKeyId != null) {
-                    contentResolver.notifyChange(KeyRings.buildGenericKeyRingUri(masterKeyId), null);
-                }
-                contentResolver.notifyChange(
-                        ApiAutocryptPeer.buildByPackageNameAndAutocryptId(packageName, autocryptPeer), null);
                 break;
             }
 
@@ -1047,7 +1010,6 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
                     selection += " AND (" + additionalSelection + ")";
                 }
                 count = db.delete(Tables.API_AUTOCRYPT_PEERS, selection, selectionArgs);
-                contentResolver.notifyChange(KeyRings.buildGenericKeyRingUri(uri.getLastPathSegment()), null);
                 break;
 
             case API_APPS_BY_PACKAGE_NAME: {
@@ -1076,7 +1038,6 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
         Timber.v("update(uri=" + uri + ", values=" + values.toString() + ")");
 
         final SQLiteDatabase db = getDb().getWritableDatabase();
-        ContentResolver contentResolver = getContext().getContentResolver();
 
         int count = 0;
         try {
@@ -1127,25 +1088,12 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
                         db.insertOrThrow(Tables.API_AUTOCRYPT_PEERS, null, values);
                     }
 
-                    Long masterKeyId = values.getAsLong(ApiAutocryptPeer.MASTER_KEY_ID);
-                    if (masterKeyId != null) {
-                        contentResolver.notifyChange(KeyRings.buildGenericKeyRingUri(masterKeyId), null);
-                    }
-                    Long gossipMasterKeyId = values.getAsLong(ApiAutocryptPeer.GOSSIP_MASTER_KEY_ID);
-                    if (gossipMasterKeyId != null && !gossipMasterKeyId.equals(masterKeyId)) {
-                        contentResolver.notifyChange(KeyRings.buildGenericKeyRingUri(gossipMasterKeyId), null);
-                    }
-
                     break;
                 }
                 default: {
                     throw new UnsupportedOperationException("Unknown uri: " + uri);
                 }
             }
-
-            // notify of changes in db
-            contentResolver.notifyChange(uri, null);
-
         } catch (SQLiteConstraintException e) {
             Timber.d(e, "Constraint exception on update! Entry already existing?");
         }
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/LastUpdateInteractor.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/LastUpdateInteractor.java
index 83f58ae2c..89520cb1a 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/LastUpdateInteractor.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/LastUpdateInteractor.java
@@ -15,14 +15,16 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.UpdatedKeys;
 
 public class LastUpdateInteractor {
     private final ContentResolver contentResolver;
+    private final DatabaseNotifyManager databaseNotifyManager;
 
 
     public static LastUpdateInteractor create(Context context) {
-        return new LastUpdateInteractor(context.getContentResolver());
+        return new LastUpdateInteractor(context.getContentResolver(), DatabaseNotifyManager.create(context));
     }
 
-    private LastUpdateInteractor(ContentResolver contentResolver) {
+    private LastUpdateInteractor(ContentResolver contentResolver, DatabaseNotifyManager databaseNotifyManager) {
         this.contentResolver = contentResolver;
+        this.databaseNotifyManager = databaseNotifyManager;
     }
 
     @Nullable
@@ -69,6 +71,8 @@ public class LastUpdateInteractor {
 
         // this will actually update/replace, doing the right thing™ for seenOnKeyservers value
         // see `KeychainProvider.insert()`
-        return contentResolver.insert(UpdatedKeys.CONTENT_URI, values);
+        Uri insert = contentResolver.insert(UpdatedKeys.CONTENT_URI, values);
+        databaseNotifyManager.notifyKeyserverStatusChange(masterKeyId);
+        return insert;
     }
 }
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/KeychainExternalProvider.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/KeychainExternalProvider.java
index 328c1ca8d..2253e3e4d 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/KeychainExternalProvider.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/KeychainExternalProvider.java
@@ -25,6 +25,7 @@ import java.util.List;
 
 import android.content.ContentProvider;
 import android.content.ContentValues;
+import android.content.Context;
 import android.content.UriMatcher;
 import android.database.Cursor;
 import android.database.DatabaseUtils;
@@ -40,6 +41,7 @@ import org.sufficientlysecure.keychain.provider.ApiDataAccessObject;
 import org.sufficientlysecure.keychain.provider.AutocryptPeerDataAccessObject;
 import org.sufficientlysecure.keychain.provider.AutocryptPeerDataAccessObject.AutocryptRecommendationResult;
 import org.sufficientlysecure.keychain.provider.AutocryptPeerDataAccessObject.AutocryptState;
+import org.sufficientlysecure.keychain.provider.DatabaseNotifyManager;
 import org.sufficientlysecure.keychain.provider.KeychainContract.ApiApps;
 import org.sufficientlysecure.keychain.provider.KeychainContract.Certs;
 import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
@@ -72,6 +74,7 @@ public class KeychainExternalProvider extends ContentProvider implements SimpleC
     private UriMatcher uriMatcher;
     private ApiPermissionHelper apiPermissionHelper;
     private KeychainProvider internalKeychainProvider;
+    private DatabaseNotifyManager databaseNotifyManager;
 
 
     /**
@@ -103,9 +106,15 @@ public class KeychainExternalProvider extends ContentProvider implements SimpleC
     public boolean onCreate() {
         uriMatcher = buildUriMatcher();
 
+        Context context = getContext();
+        if (context == null) {
+            throw new NullPointerException("Context can't be null during onCreate!");
+        }
+
         internalKeychainProvider = new KeychainProvider();
-        internalKeychainProvider.attachInfo(getContext(), null);
-        apiPermissionHelper = new ApiPermissionHelper(getContext(), new ApiDataAccessObject(internalKeychainProvider));
+        internalKeychainProvider.attachInfo(context, null);
+        apiPermissionHelper = new ApiPermissionHelper(context, new ApiDataAccessObject(internalKeychainProvider));
+        databaseNotifyManager = DatabaseNotifyManager.create(context);
         return true;
     }
 
@@ -270,7 +279,8 @@ public class KeychainExternalProvider extends ContentProvider implements SimpleC
                 }
                 if (!isWildcardSelector && queriesAutocryptResult) {
                     AutocryptPeerDataAccessObject autocryptPeerDao =
-                            new AutocryptPeerDataAccessObject(internalKeychainProvider, callingPackageName);
+                            new AutocryptPeerDataAccessObject(internalKeychainProvider, callingPackageName,
+                                    databaseNotifyManager);
                     fillTempTableWithAutocryptRecommendations(db, autocryptPeerDao, selectionArgs);
                 }
 
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/keyview/loader/KeyserverStatusLoader.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/keyview/loader/KeyserverStatusDao.java
similarity index 100%
rename from OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/keyview/loader/KeyserverStatusLoader.java
rename to OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/keyview/loader/KeyserverStatusDao.java
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/DatabaseUtil.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/DatabaseUtil.java
index 2f9e79ce8..dd21accd6 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/DatabaseUtil.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/DatabaseUtil.java
@@ -17,8 +17,13 @@
 
 package org.sufficientlysecure.keychain.util;
 
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
 import android.text.TextUtils;
 
+import timber.log.Timber;
+
+
 /**
  * Shamelessly copied from android.database.DatabaseUtils
  */
@@ -50,4 +55,28 @@ public class DatabaseUtil {
         System.arraycopy(newValues, 0, result, originalValues.length, newValues.length);
         return result;
     }
+
+    public static void explainQuery(SQLiteDatabase db, String sql) {
+        Cursor explainCursor = db.rawQuery("EXPLAIN QUERY PLAN " + sql, new String[0]);
+
+        // this is a debugging feature, we can be a little careless
+        explainCursor.moveToFirst();
+
+        StringBuilder line = new StringBuilder();
+        for (int i = 0; i < explainCursor.getColumnCount(); i++) {
+            line.append(explainCursor.getColumnName(i)).append(", ");
+        }
+        Timber.d(line.toString());
+
+        while (!explainCursor.isAfterLast()) {
+            line = new StringBuilder();
+            for (int i = 0; i < explainCursor.getColumnCount(); i++) {
+                line.append(explainCursor.getString(i)).append(", ");
+            }
+            Timber.d(line.toString());
+            explainCursor.moveToNext();
+        }
+
+        explainCursor.close();
+    }
 }
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/DatabaseUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/DatabaseUtils.java
deleted file mode 100644
index 69e4f05fe..000000000
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/DatabaseUtils.java
+++ /dev/null
@@ -1,34 +0,0 @@
-package org.sufficientlysecure.keychain.util;
-
-
-import android.database.Cursor;
-import android.database.sqlite.SQLiteDatabase;
-
-import timber.log.Timber;
-
-
-public class DatabaseUtils {
-    public static void explainQuery(SQLiteDatabase db, String sql) {
-        Cursor explainCursor = db.rawQuery("EXPLAIN QUERY PLAN " + sql, new String[0]);
-
-        // this is a debugging feature, we can be a little careless
-        explainCursor.moveToFirst();
-
-        StringBuilder line = new StringBuilder();
-        for (int i = 0; i < explainCursor.getColumnCount(); i++) {
-            line.append(explainCursor.getColumnName(i)).append(", ");
-        }
-        Timber.d(line.toString());
-
-        while (!explainCursor.isAfterLast()) {
-            line = new StringBuilder();
-            for (int i = 0; i < explainCursor.getColumnCount(); i++) {
-                line.append(explainCursor.getString(i)).append(", ");
-            }
-            Timber.d(line.toString());
-            explainCursor.moveToNext();
-        }
-
-        explainCursor.close();
-    }
-}
diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/provider/InteropTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/provider/InteropTest.java
index 457fc2adb..6cdc1428d 100644
--- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/provider/InteropTest.java
+++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/provider/InteropTest.java
@@ -244,7 +244,8 @@ public class InteropTest {
 
         KeyWritableRepository helper = new KeyWritableRepository(RuntimeEnvironment.application,
                 LocalPublicKeyStorage.getInstance(RuntimeEnvironment.application),
-                LastUpdateInteractor.create(RuntimeEnvironment.application)) {
+                LastUpdateInteractor.create(RuntimeEnvironment.application),
+                DatabaseNotifyManager.create(RuntimeEnvironment.application)) {
 
             @Override
             public CachedPublicKeyRing getCachedPublicKeyRing(Uri queryUri) throws PgpKeyNotFoundException {