diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/AbstractDao.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/AbstractDao.java new file mode 100644 index 000000000..89e084989 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/AbstractDao.java @@ -0,0 +1,27 @@ +package org.sufficientlysecure.keychain.provider; + + +import android.arch.persistence.db.SupportSQLiteDatabase; + + +class AbstractDao { + private final KeychainDatabase db; + private final DatabaseNotifyManager databaseNotifyManager; + + AbstractDao(KeychainDatabase db, DatabaseNotifyManager databaseNotifyManager) { + this.db = db; + this.databaseNotifyManager = databaseNotifyManager; + } + + SupportSQLiteDatabase getReadableDb() { + return db.getReadableDatabase(); + } + + SupportSQLiteDatabase getWritableDb() { + return db.getWritableDatabase(); + } + + DatabaseNotifyManager getDatabaseNotifyManager() { + return databaseNotifyManager; + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ApiDataAccessObject.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ApiAppDao.java similarity index 77% rename from OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ApiDataAccessObject.java rename to OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ApiAppDao.java index 22f1b9cc8..10129b3a5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ApiDataAccessObject.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ApiAppDao.java @@ -23,7 +23,6 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import android.arch.persistence.db.SupportSQLiteDatabase; import android.content.Context; import android.database.Cursor; @@ -36,16 +35,20 @@ import org.sufficientlysecure.keychain.model.ApiAllowedKey; import org.sufficientlysecure.keychain.model.ApiApp; -public class ApiDataAccessObject { - private final SupportSQLiteDatabase db; - - public ApiDataAccessObject(Context context) { +public class ApiAppDao extends AbstractDao { + public static ApiAppDao getInstance(Context context) { KeychainDatabase keychainDatabase = new KeychainDatabase(context); - db = keychainDatabase.getWritableDatabase(); + DatabaseNotifyManager databaseNotifyManager = DatabaseNotifyManager.create(context); + + return new ApiAppDao(keychainDatabase, databaseNotifyManager); + } + + private ApiAppDao(KeychainDatabase keychainDatabase, DatabaseNotifyManager databaseNotifyManager) { + super(keychainDatabase, databaseNotifyManager); } public ApiApp getApiApp(String packageName) { - try (Cursor cursor = db.query(ApiApp.FACTORY.selectByPackageName(packageName))) { + try (Cursor cursor = getReadableDb().query(ApiApp.FACTORY.selectByPackageName(packageName))) { if (cursor.moveToFirst()) { return ApiApp.FACTORY.selectByPackageNameMapper().map(cursor); } @@ -54,7 +57,7 @@ public class ApiDataAccessObject { } public byte[] getApiAppCertificate(String packageName) { - try (Cursor cursor = db.query(ApiApp.FACTORY.getCertificate(packageName))) { + try (Cursor cursor = getReadableDb().query(ApiApp.FACTORY.getCertificate(packageName))) { if (cursor.moveToFirst()) { return ApiApp.FACTORY.getCertificateMapper().map(cursor); } @@ -63,13 +66,13 @@ public class ApiDataAccessObject { } public void insertApiApp(ApiApp apiApp) { - InsertApiApp statement = new ApiAppsModel.InsertApiApp(db); + InsertApiApp statement = new ApiAppsModel.InsertApiApp(getWritableDb()); statement.bind(apiApp.package_name(), apiApp.package_signature()); statement.execute(); } public void deleteApiApp(String packageName) { - DeleteByPackageName deleteByPackageName = new DeleteByPackageName(db); + DeleteByPackageName deleteByPackageName = new DeleteByPackageName(getWritableDb()); deleteByPackageName.bind(packageName); deleteByPackageName.executeUpdateDelete(); } @@ -77,7 +80,7 @@ public class ApiDataAccessObject { public HashSet getAllowedKeyIdsForApp(String packageName) { SqlDelightQuery allowedKeys = ApiAllowedKey.FACTORY.getAllowedKeys(packageName); HashSet keyIds = new HashSet<>(); - try (Cursor cursor = db.query(allowedKeys)) { + try (Cursor cursor = getReadableDb().query(allowedKeys)) { while (cursor.moveToNext()) { long allowedKeyId = ApiAllowedKey.FACTORY.getAllowedKeysMapper().map(cursor); keyIds.add(allowedKeyId); @@ -87,11 +90,11 @@ public class ApiDataAccessObject { } public void saveAllowedKeyIdsForApp(String packageName, Set allowedKeyIds) { - ApiAllowedKey.DeleteByPackageName deleteByPackageName = new ApiAllowedKey.DeleteByPackageName(db); + ApiAllowedKey.DeleteByPackageName deleteByPackageName = new ApiAllowedKey.DeleteByPackageName(getWritableDb()); deleteByPackageName.bind(packageName); deleteByPackageName.executeUpdateDelete(); - InsertAllowedKey statement = new InsertAllowedKey(db); + InsertAllowedKey statement = new InsertAllowedKey(getWritableDb()); for (Long keyId : allowedKeyIds) { statement.bind(packageName, keyId); statement.execute(); @@ -99,7 +102,7 @@ public class ApiDataAccessObject { } public void addAllowedKeyIdForApp(String packageName, long allowedKeyId) { - InsertAllowedKey statement = new InsertAllowedKey(db); + InsertAllowedKey statement = new InsertAllowedKey(getWritableDb()); statement.bind(packageName, allowedKeyId); statement.execute(); } @@ -108,7 +111,7 @@ public class ApiDataAccessObject { SqlDelightQuery query = ApiApp.FACTORY.selectAll(); ArrayList result = new ArrayList<>(); - try (Cursor cursor = db.query(query)) { + try (Cursor cursor = getReadableDb().query(query)) { while (cursor.moveToNext()) { ApiApp apiApp = ApiApp.FACTORY.selectAllMapper().map(cursor); result.add(apiApp); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/AutocryptPeerDao.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/AutocryptPeerDao.java index bdf79f6a7..3e9f6e2dc 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/AutocryptPeerDao.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/AutocryptPeerDao.java @@ -22,7 +22,6 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; -import android.arch.persistence.db.SupportSQLiteDatabase; import android.content.Context; import android.database.Cursor; import android.support.annotation.Nullable; @@ -39,25 +38,21 @@ import org.sufficientlysecure.keychain.model.AutocryptPeer.AutocryptKeyStatus; import org.sufficientlysecure.keychain.model.AutocryptPeer.GossipOrigin; -public class AutocryptPeerDao { - private final SupportSQLiteDatabase db; - private final DatabaseNotifyManager databaseNotifyManager; - +public class AutocryptPeerDao extends AbstractDao { public static AutocryptPeerDao getInstance(Context context) { KeychainDatabase keychainDatabase = new KeychainDatabase(context); DatabaseNotifyManager databaseNotifyManager = DatabaseNotifyManager.create(context); - return new AutocryptPeerDao(keychainDatabase.getWritableDatabase(), databaseNotifyManager); + return new AutocryptPeerDao(keychainDatabase, databaseNotifyManager); } - private AutocryptPeerDao(SupportSQLiteDatabase writableDatabase, DatabaseNotifyManager databaseNotifyManager) { - this.db = writableDatabase; - this.databaseNotifyManager = databaseNotifyManager; + private AutocryptPeerDao(KeychainDatabase database, DatabaseNotifyManager databaseNotifyManager) { + super(database, databaseNotifyManager); } public Long getMasterKeyIdForAutocryptPeer(String autocryptId) { SqlDelightQuery query = AutocryptPeer.FACTORY.selectMasterKeyIdByIdentifier(autocryptId); - try (Cursor cursor = db.query(query)) { + try (Cursor cursor = getReadableDb().query(query)) { if (cursor.moveToFirst()) { return AutocryptPeer.FACTORY.selectMasterKeyIdByIdentifierMapper().map(cursor); } @@ -74,10 +69,10 @@ public class AutocryptPeerDao { return null; } - public List getAutocryptPeers(String packageName, String... autocryptId) { + private List getAutocryptPeers(String packageName, String... autocryptId) { ArrayList result = new ArrayList<>(autocryptId.length); SqlDelightQuery query = AutocryptPeer.FACTORY.selectByIdentifiers(packageName, autocryptId); - try (Cursor cursor = db.query(query)) { + try (Cursor cursor = getReadableDb().query(query)) { if (cursor.moveToNext()) { AutocryptPeer autocryptPeer = AutocryptPeer.PEER_MAPPER.map(cursor); result.add(autocryptPeer); @@ -89,7 +84,7 @@ public class AutocryptPeerDao { public List getAutocryptKeyStatus(String packageName, String[] autocryptIds) { ArrayList result = new ArrayList<>(autocryptIds.length); SqlDelightQuery query = AutocryptPeer.FACTORY.selectAutocryptKeyStatus(packageName, autocryptIds, System.currentTimeMillis()); - try (Cursor cursor = db.query(query)) { + try (Cursor cursor = getReadableDb().query(query)) { if (cursor.moveToNext()) { AutocryptKeyStatus autocryptPeer = AutocryptPeer.KEY_STATUS_MAPPER.map(cursor); result.add(autocryptPeer); @@ -99,12 +94,12 @@ public class AutocryptPeerDao { } public void insertOrUpdateLastSeen(String packageName, String autocryptId, Date date) { - UpdateLastSeen updateStatement = new UpdateLastSeen(db, AutocryptPeer.FACTORY); + UpdateLastSeen updateStatement = new UpdateLastSeen(getWritableDb(), AutocryptPeer.FACTORY); updateStatement.bind(packageName, autocryptId, date); int updated = updateStatement.executeUpdateDelete(); if (updated == 0) { - InsertPeer insertStatement = new InsertPeer(db, AutocryptPeer.FACTORY); + InsertPeer insertStatement = new InsertPeer(getWritableDb(), AutocryptPeer.FACTORY); insertStatement.bind(packageName, autocryptId, date); insertStatement.executeInsert(); } @@ -112,30 +107,30 @@ public class AutocryptPeerDao { public void updateKey(String packageName, String autocryptId, Date effectiveDate, long masterKeyId, boolean isMutual) { - UpdateKey updateStatement = new UpdateKey(db, AutocryptPeer.FACTORY); + UpdateKey updateStatement = new UpdateKey(getWritableDb(), AutocryptPeer.FACTORY); updateStatement.bind(packageName, autocryptId, effectiveDate, masterKeyId, isMutual); int rowsUpdated = updateStatement.executeUpdateDelete(); if (rowsUpdated == 0) { throw new IllegalStateException("No rows updated! Was this peer inserted before the update?"); } - databaseNotifyManager.notifyAutocryptUpdate(autocryptId, masterKeyId); + getDatabaseNotifyManager().notifyAutocryptUpdate(autocryptId, masterKeyId); } public void updateKeyGossip(String packageName, String autocryptId, Date effectiveDate, long masterKeyId, GossipOrigin origin) { - UpdateGossipKey updateStatement = new UpdateGossipKey(db, AutocryptPeer.FACTORY); + UpdateGossipKey updateStatement = new UpdateGossipKey(getWritableDb(), AutocryptPeer.FACTORY); updateStatement.bind(packageName, autocryptId, effectiveDate, masterKeyId, origin); int rowsUpdated = updateStatement.executeUpdateDelete(); if (rowsUpdated == 0) { throw new IllegalStateException("No rows updated! Was this peer inserted before the update?"); } - databaseNotifyManager.notifyAutocryptUpdate(autocryptId, masterKeyId); + getDatabaseNotifyManager().notifyAutocryptUpdate(autocryptId, masterKeyId); } public List getAutocryptPeersForKey(long masterKeyId) { ArrayList result = new ArrayList<>(); SqlDelightQuery query = AutocryptPeer.FACTORY.selectByMasterKeyId(masterKeyId); - try (Cursor cursor = db.query(query)) { + try (Cursor cursor = getReadableDb().query(query)) { if (cursor.moveToNext()) { AutocryptPeer autocryptPeer = AutocryptPeer.PEER_MAPPER.map(cursor); result.add(autocryptPeer); @@ -146,16 +141,16 @@ public class AutocryptPeerDao { public void deleteByIdentifier(String packageName, String autocryptId) { Long masterKeyId = getMasterKeyIdForAutocryptPeer(autocryptId); - DeleteByIdentifier deleteStatement = new DeleteByIdentifier(db); + DeleteByIdentifier deleteStatement = new DeleteByIdentifier(getReadableDb()); deleteStatement.bind(packageName, autocryptId); deleteStatement.execute(); if (masterKeyId != null) { - databaseNotifyManager.notifyAutocryptDelete(autocryptId, masterKeyId); + getDatabaseNotifyManager().notifyAutocryptDelete(autocryptId, masterKeyId); } } public void deleteByMasterKeyId(long masterKeyId) { - DeleteByMasterKeyId deleteStatement = new DeleteByMasterKeyId(db); + DeleteByMasterKeyId deleteStatement = new DeleteByMasterKeyId(getReadableDb()); deleteStatement.bind(masterKeyId); deleteStatement.execute(); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyMetadataDao.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyMetadataDao.java index f98258c8b..94f458aee 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyMetadataDao.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyMetadataDao.java @@ -6,7 +6,6 @@ import java.util.Date; import java.util.List; import java.util.concurrent.TimeUnit; -import android.arch.persistence.db.SupportSQLiteDatabase; import android.content.Context; import android.database.Cursor; @@ -15,26 +14,21 @@ import org.sufficientlysecure.keychain.KeyMetadataModel.ReplaceKeyMetadata; import org.sufficientlysecure.keychain.model.KeyMetadata; -public class KeyMetadataDao { - private final SupportSQLiteDatabase db; - private DatabaseNotifyManager databaseNotifyManager; - - +public class KeyMetadataDao extends AbstractDao { public static KeyMetadataDao create(Context context) { - SupportSQLiteDatabase supportSQLiteDatabase = new KeychainDatabase(context).getWritableDatabase(); + KeychainDatabase database = new KeychainDatabase(context); DatabaseNotifyManager databaseNotifyManager = DatabaseNotifyManager.create(context); - return new KeyMetadataDao(supportSQLiteDatabase, databaseNotifyManager); + return new KeyMetadataDao(database, databaseNotifyManager); } - private KeyMetadataDao(SupportSQLiteDatabase supportSQLiteDatabase, DatabaseNotifyManager databaseNotifyManager) { - this.db = supportSQLiteDatabase; - this.databaseNotifyManager = databaseNotifyManager; + private KeyMetadataDao(KeychainDatabase database, DatabaseNotifyManager databaseNotifyManager) { + super(database, databaseNotifyManager); } public KeyMetadata getKeyMetadata(long masterKeyId) { SqlDelightQuery query = KeyMetadata.FACTORY.selectByMasterKeyId(masterKeyId); - try (Cursor cursor = db.query(query)) { + try (Cursor cursor = getReadableDb().query(query)) { if (cursor.moveToFirst()) { return KeyMetadata.FACTORY.selectByMasterKeyIdMapper().map(cursor); } @@ -43,22 +37,22 @@ public class KeyMetadataDao { } public void resetAllLastUpdatedTimes() { - new KeyMetadata.DeleteAllLastUpdatedTimes(db).execute(); + new KeyMetadata.DeleteAllLastUpdatedTimes(getWritableDb()).execute(); } public void renewKeyLastUpdatedTime(long masterKeyId, boolean seenOnKeyservers) { - ReplaceKeyMetadata replaceStatement = new ReplaceKeyMetadata(db, KeyMetadata.FACTORY); + ReplaceKeyMetadata replaceStatement = new ReplaceKeyMetadata(getWritableDb(), KeyMetadata.FACTORY); replaceStatement.bind(masterKeyId, new Date(), seenOnKeyservers); replaceStatement.executeInsert(); - databaseNotifyManager.notifyKeyMetadataChange(masterKeyId); + getDatabaseNotifyManager().notifyKeyMetadataChange(masterKeyId); } public List getFingerprintsForKeysOlderThan(long olderThan, TimeUnit timeUnit) { SqlDelightQuery query = KeyMetadata.FACTORY.selectFingerprintsForKeysOlderThan(new Date(timeUnit.toMillis(olderThan))); List fingerprintList = new ArrayList<>(); - try (Cursor cursor = db.query(query)) { + try (Cursor cursor = getReadableDb().query(query)) { while (cursor.moveToNext()) { byte[] fingerprint = KeyMetadata.FACTORY.selectFingerprintsForKeysOlderThanMapper().map(cursor); fingerprintList.add(fingerprint); 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 5db3f9cd0..c8ca026f4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyRepository.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyRepository.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; -import android.arch.persistence.db.SupportSQLiteDatabase; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; @@ -44,7 +43,7 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.UserPackets; import timber.log.Timber; -public class KeyRepository { +public class KeyRepository extends AbstractDao { // If we ever switch to api level 11, we can ditch this whole mess! public static final int FIELD_TYPE_NULL = 1; // this is called integer to stay coherent with the constants in Cursor (api level 11) @@ -56,7 +55,6 @@ public class KeyRepository { final ContentResolver contentResolver; final LocalPublicKeyStorage mLocalPublicKeyStorage; final LocalSecretKeyStorage localSecretKeyStorage; - final SupportSQLiteDatabase db; OperationLog mLog; int mIndent; @@ -65,23 +63,26 @@ public class KeyRepository { ContentResolver contentResolver = context.getContentResolver(); LocalPublicKeyStorage localPublicKeyStorage = LocalPublicKeyStorage.getInstance(context); LocalSecretKeyStorage localSecretKeyStorage = LocalSecretKeyStorage.getInstance(context); - SupportSQLiteDatabase db = new KeychainDatabase(context).getWritableDatabase(); + KeychainDatabase database = new KeychainDatabase(context); + DatabaseNotifyManager databaseNotifyManager = DatabaseNotifyManager.create(context); - return new KeyRepository(contentResolver, db, localPublicKeyStorage, localSecretKeyStorage); + return new KeyRepository(contentResolver, database, databaseNotifyManager, localPublicKeyStorage, localSecretKeyStorage); } - private KeyRepository(ContentResolver contentResolver, SupportSQLiteDatabase db, + private KeyRepository(ContentResolver contentResolver, KeychainDatabase database, + DatabaseNotifyManager databaseNotifyManager, LocalPublicKeyStorage localPublicKeyStorage, LocalSecretKeyStorage localSecretKeyStorage) { - this(contentResolver, db, localPublicKeyStorage, localSecretKeyStorage, new OperationLog(), 0); + this(contentResolver, database, databaseNotifyManager, localPublicKeyStorage, localSecretKeyStorage, new OperationLog(), 0); } - KeyRepository(ContentResolver contentResolver, SupportSQLiteDatabase db, + KeyRepository(ContentResolver contentResolver, KeychainDatabase database, + DatabaseNotifyManager databaseNotifyManager, LocalPublicKeyStorage localPublicKeyStorage, LocalSecretKeyStorage localSecretKeyStorage, OperationLog log, int indent) { + super(database, databaseNotifyManager); this.contentResolver = contentResolver; - this.db = db; mLocalPublicKeyStorage = localPublicKeyStorage; this.localSecretKeyStorage = localSecretKeyStorage; mIndent = indent; @@ -288,7 +289,7 @@ public class KeyRepository { public final byte[] loadPublicKeyRingData(long masterKeyId) throws NotFoundException { SqlDelightQuery query = KeyRingPublic.FACTORY.selectByMasterKeyId(masterKeyId); - try (Cursor cursor = db.query(query)) { + try (Cursor cursor = getReadableDb().query(query)) { if (cursor.moveToFirst()) { KeyRingPublic keyRingPublic = KeyRingPublic.MAPPER.map(cursor); byte[] keyRingData = keyRingPublic.key_ring_data(); 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 a32fdacb0..7cabe3de5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyWritableRepository.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyWritableRepository.java @@ -25,7 +25,6 @@ import java.util.Collections; import java.util.Date; import java.util.List; -import android.arch.persistence.db.SupportSQLiteDatabase; import android.content.ContentProviderOperation; import android.content.ContentValues; import android.content.Context; @@ -92,25 +91,26 @@ public class KeyWritableRepository extends KeyRepository { LocalSecretKeyStorage localSecretKeyStorage = LocalSecretKeyStorage.getInstance(context); DatabaseNotifyManager databaseNotifyManager = DatabaseNotifyManager.create(context); AutocryptPeerDao autocryptPeerDao = AutocryptPeerDao.getInstance(context); - SupportSQLiteDatabase db = new KeychainDatabase(context).getWritableDatabase(); - return new KeyWritableRepository(context, db, + KeychainDatabase database = new KeychainDatabase(context); + + return new KeyWritableRepository(context, database, localPublicKeyStorage, localSecretKeyStorage, databaseNotifyManager, autocryptPeerDao); } @VisibleForTesting KeyWritableRepository(Context context, - SupportSQLiteDatabase db, LocalPublicKeyStorage localPublicKeyStorage, + KeychainDatabase database, LocalPublicKeyStorage localPublicKeyStorage, LocalSecretKeyStorage localSecretKeyStorage, DatabaseNotifyManager databaseNotifyManager, AutocryptPeerDao autocryptPeerDao) { - this(context, db, localPublicKeyStorage, localSecretKeyStorage, databaseNotifyManager, new OperationLog(), 0, + this(context, database, localPublicKeyStorage, localSecretKeyStorage, databaseNotifyManager, new OperationLog(), 0, autocryptPeerDao); } - private KeyWritableRepository(Context context, SupportSQLiteDatabase db, + private KeyWritableRepository(Context context, KeychainDatabase database, LocalPublicKeyStorage localPublicKeyStorage, LocalSecretKeyStorage localSecretKeyStorage, DatabaseNotifyManager databaseNotifyManager, OperationLog log, int indent, AutocryptPeerDao autocryptPeerDao) { - super(context.getContentResolver(), db, localPublicKeyStorage, localSecretKeyStorage, log, indent); + super(context.getContentResolver(), database, databaseNotifyManager, localPublicKeyStorage, localSecretKeyStorage, log, indent); this.context = context; this.databaseNotifyManager = databaseNotifyManager; @@ -535,9 +535,11 @@ 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 = contentResolver.delete( - KeyRingData.buildPublicKeyRingUri(masterKeyId), null, null); - if (deleted > 0) { + DeleteByMasterKeyId deleteStatement = new DeleteByMasterKeyId(getWritableDb()); + deleteStatement.bind(masterKeyId); + int deletedRows = deleteStatement.executeUpdateDelete(); + + if (deletedRows > 0) { log(LogType.MSG_IP_DELETE_OLD_OK); result |= SaveKeyringResult.UPDATED; } else { @@ -595,7 +597,7 @@ public class KeyWritableRepository extends KeyRepository { } autocryptPeerDao.deleteByMasterKeyId(masterKeyId); - DeleteByMasterKeyId deleteStatement = new DeleteByMasterKeyId(db); + DeleteByMasterKeyId deleteStatement = new DeleteByMasterKeyId(getWritableDb()); deleteStatement.bind(masterKeyId); int deletedRows = deleteStatement.executeUpdateDelete(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/SimpleContentResolverInterface.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/SimpleContentResolverInterface.java index 483dd7419..5d18431de 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/SimpleContentResolverInterface.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/SimpleContentResolverInterface.java @@ -26,7 +26,7 @@ import android.net.Uri; * from {#android.content.ContentResolver}. It is used to allow substitution * of a ContentResolver in DAOs. * - * @see ApiDataAccessObject + * @see ApiAppDao */ public interface SimpleContentResolverInterface { Cursor query(Uri contentUri, String[] projection, String selection, String[] selectionArgs, String sortOrder); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ApiPermissionHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ApiPermissionHelper.java index fa6d04bbd..365bd92d0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ApiPermissionHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ApiPermissionHelper.java @@ -35,7 +35,7 @@ import android.os.Binder; import org.openintents.openpgp.OpenPgpError; import org.openintents.openpgp.util.OpenPgpApi; import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.provider.ApiDataAccessObject; +import org.sufficientlysecure.keychain.provider.ApiAppDao; import timber.log.Timber; @@ -45,13 +45,13 @@ import timber.log.Timber; public class ApiPermissionHelper { private final Context mContext; - private final ApiDataAccessObject mApiDao; + private final ApiAppDao mApiAppDao; private PackageManager mPackageManager; - public ApiPermissionHelper(Context context, ApiDataAccessObject apiDao) { + public ApiPermissionHelper(Context context, ApiAppDao apiAppDao) { mContext = context; mPackageManager = context.getPackageManager(); - mApiDao = apiDao; + mApiAppDao = apiAppDao; } public static class WrongPackageCertificateException extends Exception { @@ -206,7 +206,7 @@ public class ApiPermissionHelper { public boolean isPackageAllowed(String packageName) throws WrongPackageCertificateException { Timber.d("isPackageAllowed packageName: " + packageName); - byte[] storedPackageCert = mApiDao.getApiAppCertificate(packageName); + byte[] storedPackageCert = mApiAppDao.getApiAppCertificate(packageName); boolean isKnownPackage = storedPackageCert != null; if (!isKnownPackage) { 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 2d146fe77..29550b3a7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/KeychainExternalProvider.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/KeychainExternalProvider.java @@ -38,7 +38,7 @@ import android.text.TextUtils; import org.sufficientlysecure.keychain.BuildConfig; import org.sufficientlysecure.keychain.Constants; -import org.sufficientlysecure.keychain.provider.ApiDataAccessObject; +import org.sufficientlysecure.keychain.provider.ApiAppDao; import org.sufficientlysecure.keychain.provider.KeychainContract.Certs; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.KeychainContract.Keys; @@ -102,7 +102,7 @@ public class KeychainExternalProvider extends ContentProvider implements SimpleC throw new NullPointerException("Context can't be null during onCreate!"); } - apiPermissionHelper = new ApiPermissionHelper(context, new ApiDataAccessObject(getContext())); + apiPermissionHelper = new ApiPermissionHelper(context, ApiAppDao.getInstance(getContext())); return true; } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java index 2a7fe0874..3591df50f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java @@ -67,7 +67,7 @@ import org.sufficientlysecure.keychain.pgp.PgpSignEncryptOperation; import org.sufficientlysecure.keychain.pgp.Progressable; import org.sufficientlysecure.keychain.pgp.SecurityProblem; import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; -import org.sufficientlysecure.keychain.provider.ApiDataAccessObject; +import org.sufficientlysecure.keychain.provider.ApiAppDao; import org.sufficientlysecure.keychain.provider.AutocryptPeerDao; import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing; import org.sufficientlysecure.keychain.provider.KeyRepository; @@ -97,7 +97,7 @@ public class OpenPgpService extends Service { private ApiPermissionHelper mApiPermissionHelper; private KeyRepository mKeyRepository; - private ApiDataAccessObject mApiDao; + private ApiAppDao mApiAppDao; private OpenPgpServiceKeyIdExtractor mKeyIdExtractor; private ApiPendingIntentFactory mApiPendingIntentFactory; @@ -105,8 +105,8 @@ public class OpenPgpService extends Service { public void onCreate() { super.onCreate(); mKeyRepository = KeyRepository.create(this); - mApiDao = new ApiDataAccessObject(this); - mApiPermissionHelper = new ApiPermissionHelper(this, mApiDao); + mApiAppDao = ApiAppDao.getInstance(this); + mApiPermissionHelper = new ApiPermissionHelper(this, mApiAppDao); mApiPendingIntentFactory = new ApiPendingIntentFactory(getBaseContext()); mKeyIdExtractor = OpenPgpServiceKeyIdExtractor.getInstance(getContentResolver(), mApiPendingIntentFactory); } @@ -915,7 +915,7 @@ public class OpenPgpService extends Service { private HashSet getAllowedKeyIds() { String currentPkg = mApiPermissionHelper.getCurrentCallingPackage(); - return mApiDao.getAllowedKeyIdsForApp(currentPkg); + return mApiAppDao.getAllowedKeyIdsForApp(currentPkg); } /** diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/PackageUninstallReceiver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/PackageUninstallReceiver.java index 85c6b2dad..faa2f805e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/PackageUninstallReceiver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/PackageUninstallReceiver.java @@ -23,7 +23,7 @@ import android.content.Context; import android.content.Intent; import android.net.Uri; -import org.sufficientlysecure.keychain.provider.ApiDataAccessObject; +import org.sufficientlysecure.keychain.provider.ApiAppDao; public class PackageUninstallReceiver extends BroadcastReceiver { @@ -36,8 +36,8 @@ public class PackageUninstallReceiver extends BroadcastReceiver { } String packageName = uri.getEncodedSchemeSpecificPart(); - ApiDataAccessObject apiDao = new ApiDataAccessObject(context); - apiDao.deleteApiApp(packageName); + ApiAppDao apiAppDao = ApiAppDao.getInstance(context); + apiAppDao.deleteApiApp(packageName); } } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/SshAuthenticationService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/SshAuthenticationService.java index aead4985c..4851d0815 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/SshAuthenticationService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/SshAuthenticationService.java @@ -45,7 +45,7 @@ import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKey; import org.sufficientlysecure.keychain.pgp.SshPublicKey; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; -import org.sufficientlysecure.keychain.provider.ApiDataAccessObject; +import org.sufficientlysecure.keychain.provider.ApiAppDao; import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing; import org.sufficientlysecure.keychain.provider.KeyRepository; import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; @@ -63,7 +63,7 @@ public class SshAuthenticationService extends Service { private ApiPermissionHelper mApiPermissionHelper; private KeyRepository mKeyRepository; - private ApiDataAccessObject mApiDao; + private ApiAppDao mApiAppDao; private ApiPendingIntentFactory mApiPendingIntentFactory; private static final List SUPPORTED_VERSIONS = Collections.unmodifiableList(Collections.singletonList(1)); @@ -74,9 +74,9 @@ public class SshAuthenticationService extends Service { @Override public void onCreate() { super.onCreate(); - mApiPermissionHelper = new ApiPermissionHelper(this, new ApiDataAccessObject(this)); + mApiPermissionHelper = new ApiPermissionHelper(this, ApiAppDao.getInstance(this)); mKeyRepository = KeyRepository.create(this); - mApiDao = new ApiDataAccessObject(this); + mApiAppDao = ApiAppDao.getInstance(this); mApiPendingIntentFactory = new ApiPendingIntentFactory(getBaseContext()); } @@ -394,7 +394,7 @@ public class SshAuthenticationService extends Service { private HashSet getAllowedKeyIds() { String currentPkg = mApiPermissionHelper.getCurrentCallingPackage(); - return mApiDao.getAllowedKeyIdsForApp(currentPkg); + return mApiAppDao.getAllowedKeyIdsForApp(currentPkg); } /** diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsActivity.java index 79c54147e..cd8448f83 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsActivity.java @@ -37,7 +37,7 @@ import org.bouncycastle.util.encoders.Hex; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.model.ApiApp; import org.sufficientlysecure.keychain.operations.results.OperationResult; -import org.sufficientlysecure.keychain.provider.ApiDataAccessObject; +import org.sufficientlysecure.keychain.provider.ApiAppDao; import org.sufficientlysecure.keychain.ui.base.BaseActivity; import org.sufficientlysecure.keychain.ui.dialog.AdvancedAppSettingsDialogFragment; import timber.log.Timber; @@ -54,7 +54,7 @@ public class AppSettingsActivity extends BaseActivity { // model ApiApp mApiApp; - private ApiDataAccessObject apiDataAccessObject; + private ApiAppDao apiAppDao; @Override protected void onCreate(Bundle savedInstanceState) { @@ -76,7 +76,7 @@ public class AppSettingsActivity extends BaseActivity { return; } - apiDataAccessObject = new ApiDataAccessObject(this); + apiAppDao = ApiAppDao.getInstance(this); loadData(savedInstanceState); } @@ -170,7 +170,7 @@ public class AppSettingsActivity extends BaseActivity { } private void loadData(Bundle savedInstanceState) { - mApiApp = apiDataAccessObject.getApiApp(packageName); + mApiApp = apiAppDao.getApiApp(packageName); // get application name and icon from package manager String appName; @@ -211,7 +211,7 @@ public class AppSettingsActivity extends BaseActivity { } private void revokeAccess() { - apiDataAccessObject.deleteApiApp(packageName); + apiAppDao.deleteApiApp(packageName); finish(); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsAllowedKeysListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsAllowedKeysListFragment.java index 5c8b99926..ee9dbbf6c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsAllowedKeysListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppSettingsAllowedKeysListFragment.java @@ -33,7 +33,7 @@ import android.widget.ListView; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.ListFragmentWorkaround; -import org.sufficientlysecure.keychain.provider.ApiDataAccessObject; +import org.sufficientlysecure.keychain.provider.ApiAppDao; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.ui.adapter.KeyAdapter; import org.sufficientlysecure.keychain.ui.adapter.KeySelectableAdapter; @@ -44,7 +44,7 @@ public class AppSettingsAllowedKeysListFragment extends ListFragmentWorkaround i private static final String ARG_PACKAGE_NAME = "package_name"; private KeySelectableAdapter mAdapter; - private ApiDataAccessObject mApiDao; + private ApiAppDao mApiAppDao; private String packageName; @@ -66,7 +66,7 @@ public class AppSettingsAllowedKeysListFragment extends ListFragmentWorkaround i public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - mApiDao = new ApiDataAccessObject(getActivity()); + mApiAppDao = ApiAppDao.getInstance(getActivity()); } @Override @@ -104,7 +104,7 @@ public class AppSettingsAllowedKeysListFragment extends ListFragmentWorkaround i // application this would come from a resource. setEmptyText(getString(R.string.list_empty)); - Set checked = mApiDao.getAllowedKeyIdsForApp(packageName); + Set checked = mApiAppDao.getAllowedKeyIdsForApp(packageName); mAdapter = new KeySelectableAdapter(getActivity(), null, 0, checked); setListAdapter(mAdapter); getListView().setOnItemClickListener(mAdapter); @@ -137,7 +137,7 @@ public class AppSettingsAllowedKeysListFragment extends ListFragmentWorkaround i } */ public void saveAllowedKeys() { - mApiDao.saveAllowedKeyIdsForApp(packageName, getSelectedMasterKeyIds()); + mApiAppDao.saveAllowedKeyIdsForApp(packageName, getSelectedMasterKeyIds()); } @Override diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppsListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppsListFragment.java index e55b1e19f..7f0a24c5f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppsListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AppsListFragment.java @@ -41,7 +41,7 @@ import android.widget.TextView; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.model.ApiApp; -import org.sufficientlysecure.keychain.provider.ApiDataAccessObject; +import org.sufficientlysecure.keychain.provider.ApiAppDao; import org.sufficientlysecure.keychain.remote.ui.AppsListFragment.ApiAppAdapter; import org.sufficientlysecure.keychain.ui.base.RecyclerFragment; import org.sufficientlysecure.keychain.ui.keyview.loader.AsyncTaskLiveData; @@ -169,14 +169,14 @@ public class AppsListFragment extends RecyclerFragment { } public static class ApiAppsLiveData extends AsyncTaskLiveData> { - private final ApiDataAccessObject apiDao; + private final ApiAppDao apiAppDao; private final PackageManager packageManager; ApiAppsLiveData(Context context) { super(context, null); packageManager = getContext().getPackageManager(); - apiDao = new ApiDataAccessObject(context); + apiAppDao = ApiAppDao.getInstance(context); } @Override @@ -191,7 +191,7 @@ public class AppsListFragment extends RecyclerFragment { } private void loadRegisteredApps(ArrayList result) { - List registeredApiApps = apiDao.getAllApiApps(); + List registeredApiApps = apiAppDao.getAllApiApps(); for (ApiApp apiApp : registeredApiApps) { ListedApp listedApp; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteRegisterPresenter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteRegisterPresenter.java index 66b4f2965..22d7dba0a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteRegisterPresenter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteRegisterPresenter.java @@ -27,12 +27,12 @@ import android.graphics.drawable.Drawable; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.model.ApiApp; -import org.sufficientlysecure.keychain.provider.ApiDataAccessObject; +import org.sufficientlysecure.keychain.provider.ApiAppDao; import timber.log.Timber; class RemoteRegisterPresenter { - private final ApiDataAccessObject apiDao; + private final ApiAppDao apiAppDao; private final PackageManager packageManager; private final Context context; @@ -45,7 +45,7 @@ class RemoteRegisterPresenter { RemoteRegisterPresenter(Context context) { this.context = context; - apiDao = new ApiDataAccessObject(context); + apiAppDao = ApiAppDao.getInstance(context); packageManager = context.getPackageManager(); } @@ -76,7 +76,7 @@ class RemoteRegisterPresenter { } void onClickAllow() { - apiDao.insertApiApp(apiApp); + apiAppDao.insertApiApp(apiApp); view.finishWithResult(resultData); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RequestKeyPermissionPresenter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RequestKeyPermissionPresenter.java index a3c4e1bad..1a8e6e2c9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RequestKeyPermissionPresenter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RequestKeyPermissionPresenter.java @@ -29,7 +29,7 @@ import org.openintents.openpgp.util.OpenPgpUtils.UserId; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType; import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; -import org.sufficientlysecure.keychain.provider.ApiDataAccessObject; +import org.sufficientlysecure.keychain.provider.ApiAppDao; import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing; import org.sufficientlysecure.keychain.provider.KeyRepository; import org.sufficientlysecure.keychain.provider.KeyRepository.NotFoundException; @@ -42,7 +42,7 @@ import timber.log.Timber; class RequestKeyPermissionPresenter { private final Context context; private final PackageManager packageManager; - private final ApiDataAccessObject apiDataAccessObject; + private final ApiAppDao apiAppDao; private final ApiPermissionHelper apiPermissionHelper; private RequestKeyPermissionMvpView view; @@ -54,19 +54,19 @@ class RequestKeyPermissionPresenter { static RequestKeyPermissionPresenter createRequestKeyPermissionPresenter(Context context) { PackageManager packageManager = context.getPackageManager(); - ApiDataAccessObject apiDataAccessObject = new ApiDataAccessObject(context); - ApiPermissionHelper apiPermissionHelper = new ApiPermissionHelper(context, apiDataAccessObject); + ApiAppDao apiAppDao = ApiAppDao.getInstance(context); + ApiPermissionHelper apiPermissionHelper = new ApiPermissionHelper(context, apiAppDao); KeyRepository keyRepository = KeyRepository.create(context); - return new RequestKeyPermissionPresenter(context, apiDataAccessObject, apiPermissionHelper, packageManager, + return new RequestKeyPermissionPresenter(context, apiAppDao, apiPermissionHelper, packageManager, keyRepository); } - private RequestKeyPermissionPresenter(Context context, ApiDataAccessObject apiDataAccessObject, + private RequestKeyPermissionPresenter(Context context, ApiAppDao apiAppDao, ApiPermissionHelper apiPermissionHelper, PackageManager packageManager, KeyRepository keyRepository) { this.context = context; - this.apiDataAccessObject = apiDataAccessObject; + this.apiAppDao = apiAppDao; this.apiPermissionHelper = apiPermissionHelper; this.packageManager = packageManager; this.keyRepository = keyRepository; @@ -160,7 +160,7 @@ class RequestKeyPermissionPresenter { } void onClickAllow() { - apiDataAccessObject.addAllowedKeyIdForApp(packageName, masterKeyId); + apiAppDao.addAllowedKeyIdForApp(packageName, masterKeyId); view.finish(); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/SelectSignKeyIdListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/SelectSignKeyIdListFragment.java index ff5940a05..3f8c4b905 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/SelectSignKeyIdListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/SelectSignKeyIdListFragment.java @@ -32,7 +32,7 @@ import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpUtils; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.KeyRing; -import org.sufficientlysecure.keychain.provider.ApiDataAccessObject; +import org.sufficientlysecure.keychain.provider.ApiAppDao; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.remote.ui.adapter.SelectSignKeyAdapter; import org.sufficientlysecure.keychain.ui.CreateKeyActivity; @@ -49,7 +49,7 @@ public class SelectSignKeyIdListFragment extends RecyclerFragment