clean up LocalStore

This commit is contained in:
Vincent Breitmoser 2017-08-22 16:08:59 +02:00
parent dae1ca201b
commit b768d2fb6f
4 changed files with 89 additions and 91 deletions

View file

@ -103,7 +103,7 @@ public class LocalFolder extends Folder<LocalMessage> 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<LocalMessage> 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<LocalMessage> implements Serializable {
}
try {
this.localStore.database.execute(false, new DbCallback<Void>() {
this.localStore.getDatabase().execute(false, new DbCallback<Void>() {
@Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException {
Cursor cursor = null;
@ -231,7 +231,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
@Override
public boolean exists() throws MessagingException {
return this.localStore.database.execute(false, new DbCallback<Boolean>() {
return this.localStore.getDatabase().execute(false, new DbCallback<Boolean>() {
@Override
public Boolean doDbWork(final SQLiteDatabase db) throws WrappedException {
Cursor cursor = null;
@ -285,7 +285,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
@Override
public int getMessageCount() throws MessagingException {
try {
return this.localStore.database.execute(false, new DbCallback<Integer>() {
return this.localStore.getDatabase().execute(false, new DbCallback<Integer>() {
@Override
public Integer doDbWork(final SQLiteDatabase db) throws WrappedException {
try {
@ -318,7 +318,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
}
try {
return this.localStore.database.execute(false, new DbCallback<Integer>() {
return this.localStore.getDatabase().execute(false, new DbCallback<Integer>() {
@Override
public Integer doDbWork(final SQLiteDatabase db) throws WrappedException {
int unreadMessageCount = 0;
@ -349,7 +349,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
}
try {
return this.localStore.database.execute(false, new DbCallback<Integer>() {
return this.localStore.getDatabase().execute(false, new DbCallback<Integer>() {
@Override
public Integer doDbWork(final SQLiteDatabase db) throws WrappedException {
int flaggedMessageCount = 0;
@ -449,7 +449,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
private void updateFolderColumn(final String column, final Object value) throws MessagingException {
try {
this.localStore.database.execute(false, new DbCallback<Void>() {
this.localStore.getDatabase().execute(false, new DbCallback<Void>() {
@Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException {
try {
@ -545,7 +545,7 @@ public class LocalFolder extends Folder<LocalMessage> 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<LocalMessage> implements Serializable {
public void fetch(final List<LocalMessage> messages, final FetchProfile fp, final MessageRetrievalListener<LocalMessage> listener)
throws MessagingException {
try {
this.localStore.database.execute(false, new DbCallback<Void>() {
this.localStore.getDatabase().execute(false, new DbCallback<Void>() {
@Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException {
try {
@ -806,7 +806,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
public String getMessageUidById(final long id) throws MessagingException {
try {
return this.localStore.database.execute(false, new DbCallback<String>() {
return this.localStore.getDatabase().execute(false, new DbCallback<String>() {
@Override
public String doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try {
@ -837,7 +837,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
@Override
public LocalMessage getMessage(final String uid) throws MessagingException {
try {
return this.localStore.database.execute(false, new DbCallback<LocalMessage>() {
return this.localStore.getDatabase().execute(false, new DbCallback<LocalMessage>() {
@Override
public LocalMessage doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try {
@ -875,7 +875,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
public Map<String,Long> getAllMessagesAndEffectiveDates() throws MessagingException {
try {
return localStore.database.execute(false, new DbCallback<Map<String, Long>>() {
return localStore.getDatabase().execute(false, new DbCallback<Map<String, Long>>() {
@Override
public Map<String, Long> doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
Cursor cursor = null;
@ -917,7 +917,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
public List<LocalMessage> getMessages(final MessageRetrievalListener<LocalMessage> listener,
final boolean includeDeleted) throws MessagingException {
try {
return localStore.database.execute(false, new DbCallback<List<LocalMessage>>() {
return localStore.getDatabase().execute(false, new DbCallback<List<LocalMessage>>() {
@Override
public List<LocalMessage> doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try {
@ -943,7 +943,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
public List<String> getAllMessageUids() throws MessagingException {
try {
return localStore.database.execute(false, new DbCallback<List<String>>() {
return localStore.getDatabase().execute(false, new DbCallback<List<String>>() {
@Override
public List<String> doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
Cursor cursor = null;
@ -1032,7 +1032,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
final Map<String, String> uidMap = new HashMap<>();
try {
this.localStore.database.execute(false, new DbCallback<Void>() {
this.localStore.getDatabase().execute(false, new DbCallback<Void>() {
@Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try {
@ -1157,7 +1157,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
* @return The local version of the message. Never <code>null</code>.
*/
public LocalMessage storeSmallMessage(final Message message, final Runnable runnable) throws MessagingException {
return this.localStore.database.execute(true, new DbCallback<LocalMessage>() {
return this.localStore.getDatabase().execute(true, new DbCallback<LocalMessage>() {
@Override
public LocalMessage doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try {
@ -1193,7 +1193,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
public void destroyMessages(final List<? extends Message> messages) {
try {
this.localStore.database.execute(true, new DbCallback<Void>() {
this.localStore.getDatabase().execute(true, new DbCallback<Void>() {
@Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
for (Message message : messages) {
@ -1261,7 +1261,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
open(OPEN_MODE_RW);
try {
final Map<String, String> uidMap = new HashMap<>();
this.localStore.database.execute(true, new DbCallback<Void>() {
this.localStore.getDatabase().execute(true, new DbCallback<Void>() {
@Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try {
@ -1646,7 +1646,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
public void addPartToMessage(final LocalMessage message, final Part part) throws MessagingException {
open(OPEN_MODE_RW);
localStore.database.execute(false, new DbCallback<Void>() {
localStore.getDatabase().execute(false, new DbCallback<Void>() {
@Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
long messagePartId;
@ -1685,7 +1685,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
open(OPEN_MODE_RW);
final ContentValues cv = new ContentValues();
cv.put("uid", message.getUid());
this.localStore.database.execute(false, new DbCallback<Void>() {
this.localStore.getDatabase().execute(false, new DbCallback<Void>() {
@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<LocalMessage> implements Serializable {
// Use one transaction to set all flags
try {
this.localStore.database.execute(true, new DbCallback<Void>() {
this.localStore.getDatabase().execute(true, new DbCallback<Void>() {
@Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException,
UnavailableStorageException {
@ -1764,7 +1764,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
open(OPEN_MODE_RO);
try {
this.localStore.database.execute(false, new DbCallback<Void>() {
this.localStore.getDatabase().execute(false, new DbCallback<Void>() {
@Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException {
try {
@ -1807,7 +1807,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
@Override
public void delete(final boolean recurse) throws MessagingException {
try {
this.localStore.database.execute(false, new DbCallback<Void>() {
this.localStore.getDatabase().execute(false, new DbCallback<Void>() {
@Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try {
@ -1850,7 +1850,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
private void destroyMessage(final long messageId, final long messagePartId, final String messageIdHeader)
throws MessagingException {
try {
localStore.database.execute(true, new DbCallback<Void>() {
localStore.getDatabase().execute(true, new DbCallback<Void>() {
@Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException,
UnavailableStorageException {
@ -1995,7 +1995,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
}
private void deleteMessageParts(final long rootMessagePartId) throws MessagingException {
localStore.database.execute(false, new DbCallback<Void>() {
localStore.getDatabase().execute(false, new DbCallback<Void>() {
@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<LocalMessage> implements Serializable {
}
private void deleteMessageDataFromDisk(final long rootMessagePartId) throws MessagingException {
localStore.database.execute(false, new DbCallback<Void>() {
localStore.getDatabase().execute(false, new DbCallback<Void>() {
@Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
deleteMessagePartsFromDisk(db, rootMessagePartId);
@ -2063,7 +2063,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
* framework to examine send date in lieu of internal date.</p>
*/
public void updateLastUid() throws MessagingException {
Integer lastUid = this.localStore.database.execute(false, new DbCallback<Integer>() {
Integer lastUid = this.localStore.getDatabase().execute(false, new DbCallback<Integer>() {
@Override
public Integer doDbWork(final SQLiteDatabase db) {
Cursor cursor = null;
@ -2089,7 +2089,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
}
public Long getOldestMessageDate() throws MessagingException {
return this.localStore.database.execute(false, new DbCallback<Long>() {
return this.localStore.getDatabase().execute(false, new DbCallback<Long>() {
@Override
public Long doDbWork(final SQLiteDatabase db) {
Cursor cursor = null;
@ -2218,7 +2218,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
throws MessagingException {
try {
return this.localStore.database.execute(false, new DbCallback<List<Message>>() {
return this.localStore.getDatabase().execute(false, new DbCallback<List<Message>>() {
@Override
public List<Message> doDbWork(final SQLiteDatabase db) throws WrappedException {
try {

View file

@ -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<Void>() {
this.localStore.getDatabase().execute(true, new DbCallback<Void>() {
@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<Void>() {
localStore.getDatabase().execute(true, new DbCallback<Void>() {
@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<Void>() {
localStore.getDatabase().execute(true, new DbCallback<Void>() {
@Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, MessagingException {
ContentValues cv = new ContentValues();

View file

@ -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<Integer>() {
@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<Integer>() {
@Override
public Integer doDbWork(final SQLiteDatabase db) {
@ -568,7 +548,7 @@ public class LocalStore extends Store implements Serializable {
StringBuilder query = new StringBuilder();
List<String> 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<String> 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<Long> 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<Long> 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<String, List<String>> getFoldersAndUids(final List<Long> 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");
}
}
}
}

View file

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