diff --git a/src/main/java/com/todoroo/andlib/data/DatabaseDao.java b/src/main/java/com/todoroo/andlib/data/DatabaseDao.java index 718ef0a81..c38e24815 100644 --- a/src/main/java/com/todoroo/andlib/data/DatabaseDao.java +++ b/src/main/java/com/todoroo/andlib/data/DatabaseDao.java @@ -36,8 +36,10 @@ public class DatabaseDao { private AbstractDatabase database; - public DatabaseDao(Class modelClass) { + public DatabaseDao(AbstractDatabase database, Class modelClass) { this.modelClass = modelClass; + this.database = database; + table = database.getTable(this.modelClass); try { modelClass.getConstructor(); // check for default constructor } catch (NoSuchMethodException e) { @@ -50,26 +52,13 @@ public class DatabaseDao { return table; } - /** - * Sets database accessed by this DAO. Used for dependency-injected - * initialization by child classes and unit tests - */ - public void setDatabase(AbstractDatabase database) { - if(database == this.database) { - return; - } - this.database = database; - table = database.getTable(modelClass); - } - // --- listeners public interface ModelUpdateListener { public void onModelUpdated(MTYPE model); } - private final ArrayList> listeners = - new ArrayList<>(); + private final ArrayList> listeners = new ArrayList<>(); public void addListener(ModelUpdateListener listener) { listeners.add(listener); diff --git a/src/main/java/com/todoroo/astrid/dao/MetadataDao.java b/src/main/java/com/todoroo/astrid/dao/MetadataDao.java index 05b8a7dc5..af8ae870e 100644 --- a/src/main/java/com/todoroo/astrid/dao/MetadataDao.java +++ b/src/main/java/com/todoroo/astrid/dao/MetadataDao.java @@ -7,6 +7,7 @@ package com.todoroo.astrid.dao; import com.todoroo.andlib.data.Callback; import com.todoroo.andlib.data.DatabaseDao; +import com.todoroo.andlib.data.Property; import com.todoroo.andlib.sql.Criterion; import com.todoroo.andlib.sql.Join; import com.todoroo.andlib.sql.Query; @@ -14,6 +15,8 @@ import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.data.Task; +import java.util.List; + import javax.inject.Inject; import javax.inject.Singleton; @@ -24,12 +27,53 @@ import javax.inject.Singleton; * */ @Singleton -public class MetadataDao extends DatabaseDao { +public class MetadataDao { + + private final DatabaseDao dao; @Inject public MetadataDao(Database database) { - super(Metadata.class); - setDatabase(database); + dao = new DatabaseDao<>(database, Metadata.class); + } + + public void query(Callback callback, Query query) { + query(query, callback); + } + + public void query(Query query, Callback callback) { + dao.query(query, callback); + } + + public Metadata getFirst(Query query) { + return dao.getFirst(query); + } + + public int update(Criterion where, Metadata template) { + return dao.update(where, template); + } + + public void createNew(Metadata metadata) { + dao.createNew(metadata); + } + + public List toList(Query where) { + return dao.toList(where); + } + + public int deleteWhere(Criterion criterion) { + return dao.deleteWhere(criterion); + } + + public boolean delete(long id) { + return dao.delete(id); + } + + public void saveExisting(Metadata metadata) { + dao.saveExisting(metadata); + } + + public Metadata fetch(long id, Property... properties) { + return dao.fetch(id, properties); } // --- SQL clause generators @@ -55,7 +99,6 @@ public class MetadataDao extends DatabaseDao { } } - @Override public boolean persist(Metadata item) { if(!item.containsNonNullValue(Metadata.TASK)) { throw new IllegalArgumentException("metadata needs to be attached to a task: " + item.getMergedValues()); //$NON-NLS-1$ @@ -64,23 +107,23 @@ public class MetadataDao extends DatabaseDao { item.setCreationDate(DateUtilities.now()); } - return super.persist(item); + return dao.persist(item); } /** * Clean up metadata. Typically called on startup */ public void removeDanglingMetadata() { - deleteWhere(Metadata.ID.in(Query.select(Metadata.ID).from(Metadata.TABLE).join(Join.left(Task.TABLE, + dao.deleteWhere(Metadata.ID.in(Query.select(Metadata.ID).from(Metadata.TABLE).join(Join.left(Task.TABLE, Metadata.TASK.eq(Task.ID))).where(Task.TITLE.isNull()))); } public void byTask(long taskId, Callback callback) { - query(callback, Query.select(Metadata.PROPERTIES).where(Metadata.TASK.eq(taskId))); + dao.query(callback, Query.select(Metadata.PROPERTIES).where(Metadata.TASK.eq(taskId))); } public void byTaskAndKey(long taskId, String key, Callback callback) { - query(callback, Query.select(Metadata.PROPERTIES).where( + dao.query(callback, Query.select(Metadata.PROPERTIES).where( Criterion.and(Metadata.TASK.eq(taskId), Metadata.KEY.eq(key)))); } } diff --git a/src/main/java/com/todoroo/astrid/dao/RemoteModelDao.java b/src/main/java/com/todoroo/astrid/dao/RemoteModelDao.java index 12cf53fe1..286e85316 100644 --- a/src/main/java/com/todoroo/astrid/dao/RemoteModelDao.java +++ b/src/main/java/com/todoroo/astrid/dao/RemoteModelDao.java @@ -1,5 +1,6 @@ package com.todoroo.astrid.dao; +import com.todoroo.andlib.data.AbstractDatabase; import com.todoroo.andlib.data.DatabaseDao; import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.helper.UUIDHelper; @@ -15,8 +16,8 @@ import com.todoroo.astrid.helper.UUIDHelper; */ public class RemoteModelDao extends DatabaseDao { - public RemoteModelDao(Class modelClass) { - super(modelClass); + public RemoteModelDao(AbstractDatabase database, Class modelClass) { + super(database, modelClass); } @Override diff --git a/src/main/java/com/todoroo/astrid/dao/StoreObjectDao.java b/src/main/java/com/todoroo/astrid/dao/StoreObjectDao.java index 2d04920bc..09f543c52 100644 --- a/src/main/java/com/todoroo/astrid/dao/StoreObjectDao.java +++ b/src/main/java/com/todoroo/astrid/dao/StoreObjectDao.java @@ -17,32 +17,31 @@ import com.todoroo.astrid.gtasks.GtasksList; import java.util.List; import javax.inject.Inject; -import javax.inject.Singleton; import static com.google.common.collect.Iterables.transform; import static com.google.common.collect.Lists.newArrayList; import static com.todoroo.andlib.sql.Criterion.and; import static com.todoroo.andlib.sql.Query.select; -@Singleton -public class StoreObjectDao extends DatabaseDao { +public class StoreObjectDao { + + private final DatabaseDao dao; private static Criterion isSavedFilter = StoreObject.TYPE.eq(SavedFilter.TYPE); @Inject public StoreObjectDao(Database database) { - super(StoreObject.class); - setDatabase(database); + dao = new DatabaseDao<>(database, StoreObject.class); } public void getSavedFilters(Callback callback) { - query(callback, select(StoreObject.PROPERTIES) + dao.query(callback, select(StoreObject.PROPERTIES) .where(isSavedFilter) .orderBy(Order.asc(SavedFilter.NAME))); } public GtasksList getGtasksList(long id) { - StoreObject result = fetch(id, StoreObject.PROPERTIES); + StoreObject result = dao.fetch(id, StoreObject.PROPERTIES); if (!result.getType().equals(GtasksList.TYPE)) { throw new RuntimeException("Not a google task list"); } @@ -58,18 +57,30 @@ public class StoreObjectDao extends DatabaseDao { })); } + public void persist(StoreObject storeObject) { + dao.persist(storeObject); + } + public void persist(GtasksList list) { persist(list.getStoreObject()); } public List getByType(String type) { - return toList(select(StoreObject.PROPERTIES) + return dao.toList(select(StoreObject.PROPERTIES) .where(StoreObject.TYPE.eq(type))); } public StoreObject getSavedFilterByName(String title) { - return getFirst(select(StoreObject.ID) + return dao.getFirst(select(StoreObject.ID) .where(and(isSavedFilter, SavedFilter.NAME.eq(title)))); } + + public void delete(long id) { + dao.delete(id); + } + + public void createNew(StoreObject storeObject) { + dao.createNew(storeObject); + } } diff --git a/src/main/java/com/todoroo/astrid/dao/TagDataDao.java b/src/main/java/com/todoroo/astrid/dao/TagDataDao.java index a9af3b010..787b6760a 100644 --- a/src/main/java/com/todoroo/astrid/dao/TagDataDao.java +++ b/src/main/java/com/todoroo/astrid/dao/TagDataDao.java @@ -6,6 +6,7 @@ package com.todoroo.astrid.dao; import com.todoroo.andlib.data.Callback; +import com.todoroo.andlib.data.DatabaseDao; import com.todoroo.andlib.data.Property; import com.todoroo.andlib.sql.Criterion; import com.todoroo.andlib.sql.Functions; @@ -23,41 +24,70 @@ import javax.inject.Singleton; * @author Tim Su */ @Singleton -public class TagDataDao extends RemoteModelDao { +public class TagDataDao { + + private final RemoteModelDao dao; @Inject public TagDataDao(Database database) { - super(TagData.class); - setDatabase(database); + dao = new RemoteModelDao<>(database, TagData.class); } /** * Fetch a model object by UUID */ public TagData fetch(String uuid, Property... properties) { - return getFirst(Query.select(properties).where(RemoteModel.UUID_PROPERTY.eq(uuid))); + return dao.getFirst(Query.select(properties).where(RemoteModel.UUID_PROPERTY.eq(uuid))); + } + + public TagData fetch(long id, Property... properties) { + return dao.fetch(id, properties); } public TagData getTagByName(String name, Property... properties) { - return getFirst(Query.select(properties).where(TagData.NAME.eqCaseInsensitive(name))); + return dao.getFirst(Query.select(properties).where(TagData.NAME.eqCaseInsensitive(name))); } public void allTags(Callback callback) { // TODO: does this need to be ordered? - query(callback, Query.select(TagData.PROPERTIES) + dao.query(callback, Query.select(TagData.PROPERTIES) .where(TagData.DELETION_DATE.eq(0)) .orderBy(Order.asc(TagData.ID))); } public TagData getByUuid(String uuid, Property... properties) { - return getFirst(Query.select(properties).where(TagData.UUID.eq(uuid))); + return dao.getFirst(Query.select(properties).where(TagData.UUID.eq(uuid))); } public void tagDataOrderedByName(Callback callback) { - query(callback, Query.select(TagData.PROPERTIES).where(Criterion.and( + dao.query(callback, Query.select(TagData.PROPERTIES).where(Criterion.and( TagData.DELETION_DATE.eq(0), TagData.NAME.isNotNull()) ).orderBy(Order.asc(Functions.upper(TagData.NAME)))); } + + public void persist(TagData tagData) { + dao.persist(tagData); + } + + public void update(Criterion where, TagData template) { + dao.update(where, template); + } + + public void saveExisting(TagData tagData) { + dao.saveExisting(tagData); + } + + public void addListener(DatabaseDao.ModelUpdateListener modelUpdateListener) { + dao.addListener(modelUpdateListener); + } + + public void delete(long id) { + dao.delete(id); + } + + public void createNew(TagData tag) { + dao.createNew(tag); + } } diff --git a/src/main/java/com/todoroo/astrid/dao/TaskAttachmentDao.java b/src/main/java/com/todoroo/astrid/dao/TaskAttachmentDao.java index f92529632..5250d9760 100644 --- a/src/main/java/com/todoroo/astrid/dao/TaskAttachmentDao.java +++ b/src/main/java/com/todoroo/astrid/dao/TaskAttachmentDao.java @@ -12,23 +12,22 @@ import com.todoroo.andlib.sql.Query; import com.todoroo.astrid.data.TaskAttachment; import javax.inject.Inject; -import javax.inject.Singleton; -@Singleton -public class TaskAttachmentDao extends RemoteModelDao { +public class TaskAttachmentDao { + + private final RemoteModelDao dao; @Inject public TaskAttachmentDao(Database database) { - super(TaskAttachment.class); - setDatabase(database); + dao = new RemoteModelDao<>(database, TaskAttachment.class); } public boolean taskHasAttachments(String taskUuid) { - return count(byUuid(taskUuid, TaskAttachment.TASK_UUID).limit(1)) > 0; + return dao.count(byUuid(taskUuid, TaskAttachment.TASK_UUID).limit(1)) > 0; } public void getAttachments(String taskUuid, Callback callback) { - query(callback, byUuid(taskUuid, TaskAttachment.PROPERTIES)); + dao.query(callback, byUuid(taskUuid, TaskAttachment.PROPERTIES)); } private static Query byUuid(String taskUuid, Property... properties) { @@ -37,5 +36,17 @@ public class TaskAttachmentDao extends RemoteModelDao { TaskAttachment.DELETED_AT.eq(0)) ); } + + public void createNew(TaskAttachment attachment) { + dao.createNew(attachment); + } + + public void delete(long id) { + dao.delete(id); + } + + public void saveExisting(TaskAttachment m) { + dao.saveExisting(m); + } } diff --git a/src/main/java/com/todoroo/astrid/dao/TaskDao.java b/src/main/java/com/todoroo/astrid/dao/TaskDao.java index e6f0768af..41db3e0ed 100644 --- a/src/main/java/com/todoroo/astrid/dao/TaskDao.java +++ b/src/main/java/com/todoroo/astrid/dao/TaskDao.java @@ -9,6 +9,7 @@ import android.content.ContentValues; import android.database.sqlite.SQLiteConstraintException; import com.todoroo.andlib.data.AbstractModel; +import com.todoroo.andlib.data.DatabaseDao; import com.todoroo.andlib.data.Property; import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.sql.Criterion; @@ -28,6 +29,9 @@ import org.tasks.R; import org.tasks.notifications.NotificationManager; import org.tasks.preferences.Preferences; +import java.io.ByteArrayOutputStream; +import java.util.List; + import javax.inject.Inject; import javax.inject.Singleton; @@ -38,10 +42,12 @@ import javax.inject.Singleton; * */ @Singleton -public class TaskDao extends RemoteModelDao { +public class TaskDao { private static final Logger log = LoggerFactory.getLogger(TaskDao.class); + private final RemoteModelDao dao; + private final MetadataDao metadataDao; private final Broadcaster broadcaster; private final ReminderService reminderService; @@ -52,8 +58,7 @@ public class TaskDao extends RemoteModelDao { public TaskDao(Database database, MetadataDao metadataDao, Broadcaster broadcaster, ReminderService reminderService, NotificationManager notificationManager, Preferences preferences) { - super(Task.class); - setDatabase(database); + dao = new RemoteModelDao<>(database, Task.class); this.preferences = preferences; this.metadataDao = metadataDao; this.broadcaster = broadcaster; @@ -61,6 +66,42 @@ public class TaskDao extends RemoteModelDao { this.notificationManager = notificationManager; } + public TodorooCursor query(Query query) { + return dao.query(query); + } + + public Task fetch(long id, Property... properties) { + return dao.fetch(id, properties); + } + + public int count(Query query) { + return dao.count(query); + } + + public TodorooCursor rawQuery(String selection, String[] selectionArgs, Property.LongProperty id) { + return dao.rawQuery(selection, selectionArgs, id); + } + + public int update(Criterion where, Task template) { + return dao.update(where, template); + } + + public int deleteWhere(Criterion criterion) { + return dao.deleteWhere(criterion); + } + + public void addListener(DatabaseDao.ModelUpdateListener modelUpdateListener) { + dao.addListener(modelUpdateListener); + } + + public List toList(Query query) { + return dao.toList(query); + } + + public void persist(Task task) { + dao.persist(task); + } + // --- SQL clause generators /** @@ -103,7 +144,7 @@ public class TaskDao extends RemoteModelDao { } public String uuidFromLocalId(long localId) { - TodorooCursor cursor = query(Query.select(RemoteModel.UUID_PROPERTY).where(AbstractModel.ID_PROPERTY.eq(localId))); + TodorooCursor cursor = dao.query(Query.select(RemoteModel.UUID_PROPERTY).where(AbstractModel.ID_PROPERTY.eq(localId))); try { if (cursor.getCount() == 0) { return RemoteModel.NO_UUID; @@ -122,9 +163,8 @@ public class TaskDao extends RemoteModelDao { * * @return true if delete was successful */ - @Override public boolean delete(long id) { - boolean result = super.delete(id); + boolean result = dao.delete(id); if(!result) { return false; } @@ -158,7 +198,7 @@ public class TaskDao extends RemoteModelDao { } public void handleSQLiteConstraintException(Task task) { - TodorooCursor cursor = query(Query.select(Task.ID).where( + TodorooCursor cursor = dao.query(Query.select(Task.ID).where( Task.UUID.eq(task.getUUID()))); if (cursor.getCount() > 0) { cursor.moveToFirst(); @@ -167,7 +207,6 @@ public class TaskDao extends RemoteModelDao { } } - @Override public boolean createNew(Task item) { if(!item.containsValue(Task.CREATION_DATE)) { item.setCreationDate(DateUtilities.now()); @@ -189,7 +228,7 @@ public class TaskDao extends RemoteModelDao { setDefaultReminders(preferences, item); ContentValues values = item.getSetValues(); - boolean result = super.createNew(item); + boolean result = dao.createNew(item); if(result) { afterSave(item, values); } @@ -222,7 +261,6 @@ public class TaskDao extends RemoteModelDao { } } - @Override public boolean saveExisting(Task item) { ContentValues values = item.getSetValues(); if(values == null || values.size() == 0) { @@ -233,7 +271,7 @@ public class TaskDao extends RemoteModelDao { item.setModificationDate(DateUtilities.now()); } } - boolean result = super.saveExisting(item); + boolean result = dao.saveExisting(item); if(result) { afterSave(item, values); } @@ -259,7 +297,7 @@ public class TaskDao extends RemoteModelDao { } catch (SQLiteConstraintException e) { log.error(e.getMessage(), e); String uuid = item.getUUID(); - TodorooCursor tasksWithUUID = query(Query.select( + TodorooCursor tasksWithUUID = dao.query(Query.select( SQL_CONSTRAINT_MERGE_PROPERTIES).where( Task.UUID.eq(uuid))); try { @@ -270,7 +308,7 @@ public class TaskDao extends RemoteModelDao { continue; } - compareAndMergeAfterConflict(curr, fetch(item.getId(), + compareAndMergeAfterConflict(curr, dao.fetch(item.getId(), tasksWithUUID.getProperties())); return; } diff --git a/src/main/java/com/todoroo/astrid/dao/TaskListMetadataDao.java b/src/main/java/com/todoroo/astrid/dao/TaskListMetadataDao.java index bbe811ee4..4956ad055 100644 --- a/src/main/java/com/todoroo/astrid/dao/TaskListMetadataDao.java +++ b/src/main/java/com/todoroo/astrid/dao/TaskListMetadataDao.java @@ -19,18 +19,26 @@ import javax.inject.Singleton; * * @author Tim Su */ -@Singleton -public class TaskListMetadataDao extends RemoteModelDao { +public class TaskListMetadataDao { + + private final RemoteModelDao dao; @Inject public TaskListMetadataDao(Database database) { - super(TaskListMetadata.class); - setDatabase(database); + dao = new RemoteModelDao<>(database, TaskListMetadata.class); } public TaskListMetadata fetchByTagId(String tagUuid, Property... properties) { - return getFirst(Query.select(properties).where(Criterion.or(TaskListMetadata.TAG_UUID.eq(tagUuid), + return dao.getFirst(Query.select(properties).where(Criterion.or(TaskListMetadata.TAG_UUID.eq(tagUuid), TaskListMetadata.FILTER.eq(tagUuid)))); } + + public void createNew(TaskListMetadata taskListMetadata) { + dao.createNew(taskListMetadata); + } + + public void saveExisting(TaskListMetadata list) { + dao.saveExisting(list); + } } diff --git a/src/main/java/com/todoroo/astrid/dao/UserActivityDao.java b/src/main/java/com/todoroo/astrid/dao/UserActivityDao.java index 480d63ae6..6b7827987 100644 --- a/src/main/java/com/todoroo/astrid/dao/UserActivityDao.java +++ b/src/main/java/com/todoroo/astrid/dao/UserActivityDao.java @@ -10,36 +10,33 @@ import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.astrid.data.UserActivity; import javax.inject.Inject; -import javax.inject.Singleton; -@Singleton -public class UserActivityDao extends RemoteModelDao { +public class UserActivityDao { + + private final RemoteModelDao dao; @Inject public UserActivityDao(Database database) { - super(UserActivity.class); - setDatabase(database); + dao = new RemoteModelDao<>(database, UserActivity.class); } - @Override public boolean createNew(UserActivity item) { if (!item.containsValue(UserActivity.CREATED_AT)) { item.setCreatedAt(DateUtilities.now()); } - return super.createNew(item); + return dao.createNew(item); } - @Override public boolean saveExisting(UserActivity item) { ContentValues values = item.getSetValues(); if (values == null || values.size() == 0) { return false; } - return super.saveExisting(item); + return dao.saveExisting(item); } public void getCommentsForTask(String taskUuid, Callback callback) { - query(callback, Query.select(UserActivity.PROPERTIES).where( + dao.query(callback, Query.select(UserActivity.PROPERTIES).where( Criterion.and(UserActivity.ACTION.eq(UserActivity.ACTION_TASK_COMMENT), UserActivity.TARGET_ID.eq(taskUuid), UserActivity.DELETED_AT.eq(0)) diff --git a/src/main/java/com/todoroo/astrid/gtasks/sync/GtasksSyncService.java b/src/main/java/com/todoroo/astrid/gtasks/sync/GtasksSyncService.java index 0d96a731a..d666c5024 100644 --- a/src/main/java/com/todoroo/astrid/gtasks/sync/GtasksSyncService.java +++ b/src/main/java/com/todoroo/astrid/gtasks/sync/GtasksSyncService.java @@ -50,6 +50,7 @@ public class GtasksSyncService { private final GtasksPreferenceService gtasksPreferenceService; private final GtasksTokenValidator gtasksTokenValidator; private final GtasksMetadata gtasksMetadataFactory; + private final LinkedBlockingQueue operationQueue = new LinkedBlockingQueue<>(); @Inject public GtasksSyncService(MetadataDao metadataDao, GtasksMetadataService gtasksMetadataService, @@ -63,8 +64,6 @@ public class GtasksSyncService { this.gtasksMetadataFactory = gtasksMetadataFactory; } - private final LinkedBlockingQueue operationQueue = new LinkedBlockingQueue<>(); - private abstract class SyncOnSaveOperation { abstract public void op(GtasksInvoker invoker) throws IOException; } @@ -121,16 +120,14 @@ public class GtasksSyncService { if(model.checkAndClearTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC)) { return; } - if (gtasksPreferenceService.isOngoing() && !model.checkTransitory(TaskService.TRANS_REPEAT_COMPLETE)) //Don't try and sync changes that occur during a normal sync - { + if (gtasksPreferenceService.isOngoing() && !model.checkTransitory(TaskService.TRANS_REPEAT_COMPLETE)) { //Don't try and sync changes that occur during a normal sync return; } final ContentValues setValues = model.getSetValues(); if(setValues == null || !checkForToken()) { return; } - if (!checkValuesForProperties(setValues, TASK_PROPERTIES)) //None of the properties we sync were updated - { + if (!checkValuesForProperties(setValues, TASK_PROPERTIES)) { //None of the properties we sync were updated return; } diff --git a/src/main/java/com/todoroo/astrid/provider/Astrid3ContentProvider.java b/src/main/java/com/todoroo/astrid/provider/Astrid3ContentProvider.java index 745a5da31..fb28b5325 100644 --- a/src/main/java/com/todoroo/astrid/provider/Astrid3ContentProvider.java +++ b/src/main/java/com/todoroo/astrid/provider/Astrid3ContentProvider.java @@ -16,15 +16,12 @@ import android.database.sqlite.SQLiteQueryBuilder; import android.net.Uri; import android.text.TextUtils; -import com.todoroo.andlib.data.AbstractDatabase; import com.todoroo.andlib.data.AbstractModel; import com.todoroo.andlib.data.DatabaseDao; import com.todoroo.astrid.api.AstridApiConstants; import com.todoroo.astrid.dao.Database; -import com.todoroo.astrid.dao.MetadataDao; -import com.todoroo.astrid.dao.StoreObjectDao; +import com.todoroo.astrid.dao.RemoteModelDao; import com.todoroo.astrid.dao.TaskDao; -import com.todoroo.astrid.dao.UserActivityDao; import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.data.StoreObject; import com.todoroo.astrid.data.Task; @@ -80,16 +77,13 @@ public class Astrid3ContentProvider extends InjectingContentProvider { private static final UriMatcher uriMatcher; - private static AbstractDatabase databaseOverride; + private static Database databaseOverride; // --- instance variables private boolean open; @Inject Lazy database; @Inject Lazy taskDao; - @Inject Lazy metadataDao; - @Inject Lazy storeObjectDao; - @Inject Lazy userActivityDao; static { uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); @@ -150,41 +144,37 @@ public class Astrid3ContentProvider extends InjectingContentProvider { } private UriHelper generateHelper(Uri uri, boolean populateModel) { - AbstractDatabase db = getDatabase(); + final Database db = getDatabase(); if(uri.toString().startsWith(Task.CONTENT_URI.toString())) { UriHelper helper = new UriHelper<>(); helper.model = populateModel ? new Task() : null; - helper.dao = taskDao.get(); - helper.dao.setDatabase(db); + helper.dao = new RemoteModelDao<>(db, Task.class); return helper; } else if(uri.toString().startsWith(Metadata.CONTENT_URI.toString())) { UriHelper helper = new UriHelper<>(); helper.model = populateModel ? new Metadata() : null; - helper.dao = metadataDao.get(); - helper.dao.setDatabase(db); + helper.dao = new DatabaseDao<>(db, Metadata.class); return helper; } else if(uri.toString().startsWith(StoreObject.CONTENT_URI.toString())) { UriHelper helper = new UriHelper<>(); helper.model = populateModel ? new StoreObject() : null; - helper.dao = storeObjectDao.get(); - helper.dao.setDatabase(db); + helper.dao = new DatabaseDao<>(db, StoreObject.class); return helper; } else if(uri.toString().startsWith(UserActivity.CONTENT_URI.toString())) { UriHelper helper = new UriHelper<>(); helper.model = populateModel ? new UserActivity() : null; - helper.dao = userActivityDao.get(); - helper.dao.setDatabase(db); + helper.dao = new RemoteModelDao<>(db, UserActivity.class); return helper; } throw new UnsupportedOperationException("Unknown URI " + uri); } - public static void setDatabaseOverride(AbstractDatabase override) { + public static void setDatabaseOverride(Database override) { databaseOverride = override; } - private AbstractDatabase getDatabase() { + private Database getDatabase() { if (!open) { database.get().openForWriting(); open = true;