From b768d2fb6fe1a8df2855050dc2d77a8c43366c67 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 22 Aug 2017 16:08:59 +0200 Subject: [PATCH] clean up LocalStore --- .../com/fsck/k9/mailstore/LocalFolder.java | 60 +++++----- .../com/fsck/k9/mailstore/LocalMessage.java | 6 +- .../com/fsck/k9/mailstore/LocalStore.java | 112 +++++++++--------- .../mailstore/StoreSchemaDefinitionTest.java | 2 +- 4 files changed, 89 insertions(+), 91 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java index feb515e80..2086c228d 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java @@ -103,7 +103,7 @@ public class LocalFolder extends Folder implements Serializable { super(); this.localStore = localStore; this.folderName = name; - attachmentInfoExtractor = localStore.attachmentInfoExtractor; + attachmentInfoExtractor = localStore.getAttachmentInfoExtractor(); if (getAccount().getInboxFolderName().equals(getName())) { syncClass = FolderClass.FIRST_CLASS; @@ -116,7 +116,7 @@ public class LocalFolder extends Folder implements Serializable { super(); this.localStore = localStore; this.folderId = id; - attachmentInfoExtractor = localStore.attachmentInfoExtractor; + attachmentInfoExtractor = localStore.getAttachmentInfoExtractor(); } public long getId() { @@ -152,7 +152,7 @@ public class LocalFolder extends Folder implements Serializable { } try { - this.localStore.database.execute(false, new DbCallback() { + this.localStore.getDatabase().execute(false, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException { Cursor cursor = null; @@ -231,7 +231,7 @@ public class LocalFolder extends Folder implements Serializable { @Override public boolean exists() throws MessagingException { - return this.localStore.database.execute(false, new DbCallback() { + return this.localStore.getDatabase().execute(false, new DbCallback() { @Override public Boolean doDbWork(final SQLiteDatabase db) throws WrappedException { Cursor cursor = null; @@ -285,7 +285,7 @@ public class LocalFolder extends Folder implements Serializable { @Override public int getMessageCount() throws MessagingException { try { - return this.localStore.database.execute(false, new DbCallback() { + return this.localStore.getDatabase().execute(false, new DbCallback() { @Override public Integer doDbWork(final SQLiteDatabase db) throws WrappedException { try { @@ -318,7 +318,7 @@ public class LocalFolder extends Folder implements Serializable { } try { - return this.localStore.database.execute(false, new DbCallback() { + return this.localStore.getDatabase().execute(false, new DbCallback() { @Override public Integer doDbWork(final SQLiteDatabase db) throws WrappedException { int unreadMessageCount = 0; @@ -349,7 +349,7 @@ public class LocalFolder extends Folder implements Serializable { } try { - return this.localStore.database.execute(false, new DbCallback() { + return this.localStore.getDatabase().execute(false, new DbCallback() { @Override public Integer doDbWork(final SQLiteDatabase db) throws WrappedException { int flaggedMessageCount = 0; @@ -449,7 +449,7 @@ public class LocalFolder extends Folder implements Serializable { private void updateFolderColumn(final String column, final Object value) throws MessagingException { try { - this.localStore.database.execute(false, new DbCallback() { + this.localStore.getDatabase().execute(false, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException { try { @@ -545,7 +545,7 @@ public class LocalFolder extends Folder implements Serializable { private String getPrefId(String name) { if (prefId == null) { - prefId = this.localStore.uUid + "." + name; + prefId = this.localStore.getUUid() + "." + name; } return prefId; @@ -662,7 +662,7 @@ public class LocalFolder extends Folder implements Serializable { public void fetch(final List messages, final FetchProfile fp, final MessageRetrievalListener listener) throws MessagingException { try { - this.localStore.database.execute(false, new DbCallback() { + this.localStore.getDatabase().execute(false, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException { try { @@ -806,7 +806,7 @@ public class LocalFolder extends Folder implements Serializable { public String getMessageUidById(final long id) throws MessagingException { try { - return this.localStore.database.execute(false, new DbCallback() { + return this.localStore.getDatabase().execute(false, new DbCallback() { @Override public String doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { try { @@ -837,7 +837,7 @@ public class LocalFolder extends Folder implements Serializable { @Override public LocalMessage getMessage(final String uid) throws MessagingException { try { - return this.localStore.database.execute(false, new DbCallback() { + return this.localStore.getDatabase().execute(false, new DbCallback() { @Override public LocalMessage doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { try { @@ -875,7 +875,7 @@ public class LocalFolder extends Folder implements Serializable { public Map getAllMessagesAndEffectiveDates() throws MessagingException { try { - return localStore.database.execute(false, new DbCallback>() { + return localStore.getDatabase().execute(false, new DbCallback>() { @Override public Map doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { Cursor cursor = null; @@ -917,7 +917,7 @@ public class LocalFolder extends Folder implements Serializable { public List getMessages(final MessageRetrievalListener listener, final boolean includeDeleted) throws MessagingException { try { - return localStore.database.execute(false, new DbCallback>() { + return localStore.getDatabase().execute(false, new DbCallback>() { @Override public List doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { try { @@ -943,7 +943,7 @@ public class LocalFolder extends Folder implements Serializable { public List getAllMessageUids() throws MessagingException { try { - return localStore.database.execute(false, new DbCallback>() { + return localStore.getDatabase().execute(false, new DbCallback>() { @Override public List doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { Cursor cursor = null; @@ -1032,7 +1032,7 @@ public class LocalFolder extends Folder implements Serializable { final Map uidMap = new HashMap<>(); try { - this.localStore.database.execute(false, new DbCallback() { + this.localStore.getDatabase().execute(false, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { try { @@ -1157,7 +1157,7 @@ public class LocalFolder extends Folder implements Serializable { * @return The local version of the message. Never null. */ public LocalMessage storeSmallMessage(final Message message, final Runnable runnable) throws MessagingException { - return this.localStore.database.execute(true, new DbCallback() { + return this.localStore.getDatabase().execute(true, new DbCallback() { @Override public LocalMessage doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { try { @@ -1193,7 +1193,7 @@ public class LocalFolder extends Folder implements Serializable { public void destroyMessages(final List messages) { try { - this.localStore.database.execute(true, new DbCallback() { + this.localStore.getDatabase().execute(true, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { for (Message message : messages) { @@ -1261,7 +1261,7 @@ public class LocalFolder extends Folder implements Serializable { open(OPEN_MODE_RW); try { final Map uidMap = new HashMap<>(); - this.localStore.database.execute(true, new DbCallback() { + this.localStore.getDatabase().execute(true, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { try { @@ -1646,7 +1646,7 @@ public class LocalFolder extends Folder implements Serializable { public void addPartToMessage(final LocalMessage message, final Part part) throws MessagingException { open(OPEN_MODE_RW); - localStore.database.execute(false, new DbCallback() { + localStore.getDatabase().execute(false, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { long messagePartId; @@ -1685,7 +1685,7 @@ public class LocalFolder extends Folder implements Serializable { open(OPEN_MODE_RW); final ContentValues cv = new ContentValues(); cv.put("uid", message.getUid()); - this.localStore.database.execute(false, new DbCallback() { + this.localStore.getDatabase().execute(false, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { db.update("messages", cv, "id = ?", new String[] @@ -1705,7 +1705,7 @@ public class LocalFolder extends Folder implements Serializable { // Use one transaction to set all flags try { - this.localStore.database.execute(true, new DbCallback() { + this.localStore.getDatabase().execute(true, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { @@ -1764,7 +1764,7 @@ public class LocalFolder extends Folder implements Serializable { open(OPEN_MODE_RO); try { - this.localStore.database.execute(false, new DbCallback() { + this.localStore.getDatabase().execute(false, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException { try { @@ -1807,7 +1807,7 @@ public class LocalFolder extends Folder implements Serializable { @Override public void delete(final boolean recurse) throws MessagingException { try { - this.localStore.database.execute(false, new DbCallback() { + this.localStore.getDatabase().execute(false, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { try { @@ -1850,7 +1850,7 @@ public class LocalFolder extends Folder implements Serializable { private void destroyMessage(final long messageId, final long messagePartId, final String messageIdHeader) throws MessagingException { try { - localStore.database.execute(true, new DbCallback() { + localStore.getDatabase().execute(true, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { @@ -1995,7 +1995,7 @@ public class LocalFolder extends Folder implements Serializable { } private void deleteMessageParts(final long rootMessagePartId) throws MessagingException { - localStore.database.execute(false, new DbCallback() { + localStore.getDatabase().execute(false, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { db.delete("message_parts", "root = ?", new String[] { Long.toString(rootMessagePartId) }); @@ -2005,7 +2005,7 @@ public class LocalFolder extends Folder implements Serializable { } private void deleteMessageDataFromDisk(final long rootMessagePartId) throws MessagingException { - localStore.database.execute(false, new DbCallback() { + localStore.getDatabase().execute(false, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { deleteMessagePartsFromDisk(db, rootMessagePartId); @@ -2063,7 +2063,7 @@ public class LocalFolder extends Folder implements Serializable { * framework to examine send date in lieu of internal date.

*/ public void updateLastUid() throws MessagingException { - Integer lastUid = this.localStore.database.execute(false, new DbCallback() { + Integer lastUid = this.localStore.getDatabase().execute(false, new DbCallback() { @Override public Integer doDbWork(final SQLiteDatabase db) { Cursor cursor = null; @@ -2089,7 +2089,7 @@ public class LocalFolder extends Folder implements Serializable { } public Long getOldestMessageDate() throws MessagingException { - return this.localStore.database.execute(false, new DbCallback() { + return this.localStore.getDatabase().execute(false, new DbCallback() { @Override public Long doDbWork(final SQLiteDatabase db) { Cursor cursor = null; @@ -2218,7 +2218,7 @@ public class LocalFolder extends Folder implements Serializable { throws MessagingException { try { - return this.localStore.database.execute(false, new DbCallback>() { + return this.localStore.getDatabase().execute(false, new DbCallback>() { @Override public List doDbWork(final SQLiteDatabase db) throws WrappedException { try { diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessage.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessage.java index cbf69fbca..be7ae4ce6 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessage.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessage.java @@ -257,7 +257,7 @@ public class LocalMessage extends MimeMessage { public void setFlag(final Flag flag, final boolean set) throws MessagingException { try { - this.localStore.database.execute(true, new DbCallback() { + this.localStore.getDatabase().execute(true, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { try { @@ -297,7 +297,7 @@ public class LocalMessage extends MimeMessage { */ private void delete() throws MessagingException { try { - localStore.database.execute(true, new DbCallback() { + localStore.getDatabase().execute(true, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { ContentValues cv = new ContentValues(); @@ -339,7 +339,7 @@ public class LocalMessage extends MimeMessage { } try { - localStore.database.execute(true, new DbCallback() { + localStore.getDatabase().execute(true, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException, MessagingException { ContentValues cv = new ContentValues(); diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalStore.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalStore.java index c1aa977e9..dc8302082 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalStore.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalStore.java @@ -155,41 +155,17 @@ public class LocalStore extends Store implements Serializable { public static final int DB_VERSION = 60; - - public static String getColumnNameForFlag(Flag flag) { - switch (flag) { - case SEEN: { - return MessageColumns.READ; - } - case FLAGGED: { - return MessageColumns.FLAGGED; - } - case ANSWERED: { - return MessageColumns.ANSWERED; - } - case FORWARDED: { - return MessageColumns.FORWARDED; - } - default: { - throw new IllegalArgumentException("Flag must be a special column flag"); - } - } - } - - - protected String uUid = null; - - final Context context; - - LockableDatabase database; - - private ContentResolver mContentResolver; - private final Account mAccount; + private final Context context; + private final ContentResolver contentResolver; private final MessagePreviewCreator messagePreviewCreator; private final MessageFulltextCreator messageFulltextCreator; private final AttachmentCounter attachmentCounter; private final PendingCommandSerializer pendingCommandSerializer; - final AttachmentInfoExtractor attachmentInfoExtractor; + private final AttachmentInfoExtractor attachmentInfoExtractor; + + private final Account account; + private final String uUid; + private final LockableDatabase database; /** * local://localhost/path/to/database/uuid.db @@ -197,13 +173,8 @@ public class LocalStore extends Store implements Serializable { * @throws UnavailableStorageException if not {@link StorageProvider#isReady(Context)} */ private LocalStore(final Account account, final Context context) throws MessagingException { - mAccount = account; - database = new LockableDatabase(context, account.getUuid(), new StoreSchemaDefinition(this)); - this.context = context; - mContentResolver = context.getContentResolver(); - database.setStorageProviderId(account.getLocalStorageProviderId()); - uUid = account.getUuid(); + this.contentResolver = context.getContentResolver(); messagePreviewCreator = MessagePreviewCreator.newInstance(); messageFulltextCreator = MessageFulltextCreator.newInstance(); @@ -211,6 +182,11 @@ public class LocalStore extends Store implements Serializable { pendingCommandSerializer = PendingCommandSerializer.getInstance(); attachmentInfoExtractor = AttachmentInfoExtractor.getInstance(); + this.account = account; + this.uUid = account.getUuid(); + + database = new LockableDatabase(context, account.getUuid(), new StoreSchemaDefinition(this)); + database.setStorageProviderId(account.getLocalStorageProviderId()); database.open(); } @@ -266,8 +242,12 @@ public class LocalStore extends Store implements Serializable { return context; } - protected Account getAccount() { - return mAccount; + Account getAccount() { + return account; + } + + String getUUid() { + return uUid; } protected Storage getStorage() { @@ -358,7 +338,7 @@ public class LocalStore extends Store implements Serializable { } } - public int getMessageCount() throws MessagingException { + private int getMessageCount() throws MessagingException { return database.execute(false, new DbCallback() { @Override public Integer doDbWork(final SQLiteDatabase db) { @@ -374,7 +354,7 @@ public class LocalStore extends Store implements Serializable { }); } - public int getFolderCount() throws MessagingException { + private int getFolderCount() throws MessagingException { return database.execute(false, new DbCallback() { @Override public Integer doDbWork(final SQLiteDatabase db) { @@ -568,7 +548,7 @@ public class LocalStore extends Store implements Serializable { StringBuilder query = new StringBuilder(); List queryArgs = new ArrayList<>(); - SqlQueryBuilder.buildWhereClause(mAccount, search.getConditions(), query, queryArgs); + SqlQueryBuilder.buildWhereClause(account, search.getConditions(), query, queryArgs); // Avoid "ambiguous column name" error by prefixing "id" with the message table name String where = SqlQueryBuilder.addPrefixToSelection(new String[] { "id" }, @@ -914,10 +894,10 @@ public class LocalStore extends Store implements Serializable { // When created, special folders should always be displayed // inbox should be integrated // and the inbox and drafts folders should be syncced by default - if (mAccount.isSpecialFolder(name)) { + if (account.isSpecialFolder(name)) { prefHolder.inTopGroup = true; prefHolder.displayClass = LocalFolder.FolderClass.FIRST_CLASS; - if (name.equalsIgnoreCase(mAccount.getInboxFolderName())) { + if (name.equalsIgnoreCase(account.getInboxFolderName())) { prefHolder.integrate = true; prefHolder.notifyClass = LocalFolder.FolderClass.FIRST_CLASS; prefHolder.pushClass = LocalFolder.FolderClass.FIRST_CLASS; @@ -925,8 +905,8 @@ public class LocalStore extends Store implements Serializable { prefHolder.pushClass = LocalFolder.FolderClass.INHERITED; } - if (name.equalsIgnoreCase(mAccount.getInboxFolderName()) || - name.equalsIgnoreCase(mAccount.getDraftsFolderName())) { + if (name.equalsIgnoreCase(account.getInboxFolderName()) || + name.equalsIgnoreCase(account.getDraftsFolderName())) { prefHolder.syncClass = LocalFolder.FolderClass.FIRST_CLASS; } else { prefHolder.syncClass = LocalFolder.FolderClass.NO_CLASS; @@ -978,7 +958,7 @@ public class LocalStore extends Store implements Serializable { return database; } - public MessagePreviewCreator getMessagePreviewCreator() { + MessagePreviewCreator getMessagePreviewCreator() { return messagePreviewCreator; } @@ -986,13 +966,17 @@ public class LocalStore extends Store implements Serializable { return messageFulltextCreator; } - public AttachmentCounter getAttachmentCounter() { + AttachmentCounter getAttachmentCounter() { return attachmentCounter; } + AttachmentInfoExtractor getAttachmentInfoExtractor() { + return attachmentInfoExtractor; + } + void notifyChange() { Uri uri = Uri.withAppendedPath(EmailProvider.CONTENT_URI, "account/" + uUid + "/messages"); - mContentResolver.notifyChange(uri, null); + contentResolver.notifyChange(uri, null); } /** @@ -1008,10 +992,8 @@ public class LocalStore extends Store implements Serializable { * Supplies the argument set and the code to query/update the database. * @param batchSize * The maximum size of the selection set in each SQL statement. - * - * @throws MessagingException */ - public void doBatchSetSelection(final BatchSetSelection selectionCallback, final int batchSize) + private void doBatchSetSelection(final BatchSetSelection selectionCallback, final int batchSize) throws MessagingException { final List selectionArgs = new ArrayList<>(); @@ -1063,7 +1045,7 @@ public class LocalStore extends Store implements Serializable { /** * Defines the behavior of {@link LocalStore#doBatchSetSelection(BatchSetSelection, int)}. */ - public interface BatchSetSelection { + interface BatchSetSelection { /** * @return The size of the argument list. */ @@ -1089,7 +1071,6 @@ public class LocalStore extends Store implements Serializable { * {@code " IN (?,?,?)"} (starts with a space). * @param selectionArgs * The current subset of the argument list. - * @throws UnavailableStorageException */ void doDbWork(SQLiteDatabase db, String selectionSet, String[] selectionArgs) throws UnavailableStorageException; @@ -1116,7 +1097,6 @@ public class LocalStore extends Store implements Serializable { * @param newState * {@code true}, if the flag should be set. {@code false}, otherwise. * - * @throws MessagingException */ public void setFlag(final List messageIds, final Flag flag, final boolean newState) throws MessagingException { @@ -1165,7 +1145,6 @@ public class LocalStore extends Store implements Serializable { * @param newState * {@code true}, if the flag should be set. {@code false}, otherwise. * - * @throws MessagingException */ public void setFlagForThreads(final List threadRootIds, Flag flag, final boolean newState) throws MessagingException { @@ -1217,7 +1196,6 @@ public class LocalStore extends Store implements Serializable { * * @return The list of UIDs for the messages grouped by folder name. * - * @throws MessagingException */ public Map> getFoldersAndUids(final List messageIds, final boolean threadedList) throws MessagingException { @@ -1289,4 +1267,24 @@ public class LocalStore extends Store implements Serializable { return folderMap; } + + public static String getColumnNameForFlag(Flag flag) { + switch (flag) { + case SEEN: { + return MessageColumns.READ; + } + case FLAGGED: { + return MessageColumns.FLAGGED; + } + case ANSWERED: { + return MessageColumns.ANSWERED; + } + case FORWARDED: { + return MessageColumns.FORWARDED; + } + default: { + throw new IllegalArgumentException("Flag must be a special column flag"); + } + } + } } diff --git a/k9mail/src/test/java/com/fsck/k9/mailstore/StoreSchemaDefinitionTest.java b/k9mail/src/test/java/com/fsck/k9/mailstore/StoreSchemaDefinitionTest.java index 384809f21..1fd519671 100644 --- a/k9mail/src/test/java/com/fsck/k9/mailstore/StoreSchemaDefinitionTest.java +++ b/k9mail/src/test/java/com/fsck/k9/mailstore/StoreSchemaDefinitionTest.java @@ -336,7 +336,7 @@ public class StoreSchemaDefinitionTest { LockableDatabase lockableDatabase = createLockableDatabase(); LocalStore localStore = mock(LocalStore.class); - localStore.database = lockableDatabase; + when(localStore.getDatabase()).thenReturn(lockableDatabase); when(localStore.getContext()).thenReturn(context); when(localStore.getAccount()).thenReturn(account);