diff --git a/astrid/src/main/java/com/todoroo/astrid/provider/Astrid2TaskProvider.java b/astrid/src/main/java/com/todoroo/astrid/provider/Astrid2TaskProvider.java index ba486cfc5..1a6b54be7 100644 --- a/astrid/src/main/java/com/todoroo/astrid/provider/Astrid2TaskProvider.java +++ b/astrid/src/main/java/com/todoroo/astrid/provider/Astrid2TaskProvider.java @@ -97,14 +97,6 @@ public class Astrid2TaskProvider extends ContentProvider { AstridDependencyInjector.initialize(); } - public Astrid2TaskProvider() { - try { - DependencyInjectionService.getInstance().inject(this); - } catch (Exception e) { - // can't do anything about this - } - } - @Override public int delete(Uri uri, String selection, String[] selectionArgs) { if (LOGD) { @@ -185,7 +177,7 @@ public class Astrid2TaskProvider extends ContentProvider { MatrixCursor ret = new MatrixCursor(TASK_FIELD_LIST); - TodorooCursor cursor = taskService.query(Query.select(Task.ID, Task.TITLE, + TodorooCursor cursor = getTaskService().query(Query.select(Task.ID, Task.TITLE, Task.IMPORTANCE, Task.DUE_DATE).where(Criterion.and(TaskCriteria.isActive(), TaskCriteria.isVisible())). orderBy(SortHelper.defaultTaskOrder()).limit(MAX_NUMBER_OF_TASKS)); @@ -278,7 +270,7 @@ public class Astrid2TaskProvider extends ContentProvider { replace(ID, Task.ID.name). replace(IMPORTANCE, Task.IMPORTANCE.name); - return taskService.updateBySelection(criteria, selectionArgs, task); + return getTaskService().updateBySelection(criteria, selectionArgs, task); case URI_TAGS: throw new UnsupportedOperationException("tags updating: not yet"); @@ -304,4 +296,10 @@ public class Astrid2TaskProvider extends ContentProvider { } } + private TaskService getTaskService() { + if (taskService == null) { + DependencyInjectionService.getInstance().inject(this); + } + return taskService; + } } diff --git a/astrid/src/main/java/com/todoroo/astrid/provider/Astrid3ContentProvider.java b/astrid/src/main/java/com/todoroo/astrid/provider/Astrid3ContentProvider.java index 9e5faa657..7ec40c49d 100644 --- a/astrid/src/main/java/com/todoroo/astrid/provider/Astrid3ContentProvider.java +++ b/astrid/src/main/java/com/todoroo/astrid/provider/Astrid3ContentProvider.java @@ -34,9 +34,6 @@ import com.todoroo.astrid.data.Task; import com.todoroo.astrid.data.UserActivity; import com.todoroo.astrid.service.AstridDependencyInjector; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.HashSet; import java.util.Map.Entry; import java.util.Set; @@ -70,8 +67,6 @@ import java.util.Set; */ public class Astrid3ContentProvider extends ContentProvider { - private static final Logger log = LoggerFactory.getLogger(Astrid3ContentProvider.class); - static { AstridDependencyInjector.initialize(); } @@ -91,7 +86,6 @@ public class Astrid3ContentProvider extends ContentProvider { // --- instance variables - @Autowired private Database database; @@ -109,13 +103,7 @@ public class Astrid3ContentProvider extends ContentProvider { @Override public boolean onCreate() { - try { - database.openForWriting(); - return database.getDatabase() != null; - } catch (Exception e) { - log.error(e.getMessage(), e); - return false; - } + return true; } static { @@ -135,8 +123,6 @@ public class Astrid3ContentProvider extends ContentProvider { } public Astrid3ContentProvider() { - DependencyInjectionService.getInstance().inject(this); - setReadPermission(AstridApiConstants.PERMISSION_READ); setWritePermission(AstridApiConstants.PERMISSION_WRITE); } @@ -179,29 +165,30 @@ public class Astrid3ContentProvider extends ContentProvider { } private UriHelper generateHelper(Uri uri, boolean populateModel) { + AbstractDatabase db = getDatabase(); if(uri.toString().startsWith(Task.CONTENT_URI.toString())) { UriHelper helper = new UriHelper<>(); helper.model = populateModel ? new Task() : null; helper.dao = taskDao; - helper.dao.setDatabase(getDatabase()); + helper.dao.setDatabase(db); return helper; } else if(uri.toString().startsWith(Metadata.CONTENT_URI.toString())) { UriHelper helper = new UriHelper<>(); helper.model = populateModel ? new Metadata() : null; helper.dao = metadataDao; - helper.dao.setDatabase(getDatabase()); + helper.dao.setDatabase(db); return helper; } else if(uri.toString().startsWith(StoreObject.CONTENT_URI.toString())) { UriHelper helper = new UriHelper<>(); helper.model = populateModel ? new StoreObject() : null; helper.dao = storeObjectDao; - helper.dao.setDatabase(getDatabase()); + helper.dao.setDatabase(db); return helper; } else if(uri.toString().startsWith(UserActivity.CONTENT_URI.toString())) { UriHelper helper = new UriHelper<>(); helper.model = populateModel ? new UserActivity() : null; helper.dao = userActivityDao; - helper.dao.setDatabase(getDatabase()); + helper.dao.setDatabase(db); return helper; } @@ -213,6 +200,10 @@ public class Astrid3ContentProvider extends ContentProvider { } private AbstractDatabase getDatabase() { + if (database == null) { + DependencyInjectionService.getInstance().inject(this); + database.openForWriting(); + } if(databaseOverride != null) { return databaseOverride; } @@ -422,5 +413,4 @@ public class Astrid3ContentProvider extends ContentProvider { ContentResolver cr = ContextManager.getContext().getContentResolver(); cr.notifyChange(Task.CONTENT_URI, null); } - } diff --git a/astrid/src/main/java/com/todoroo/astrid/provider/SqlContentProvider.java b/astrid/src/main/java/com/todoroo/astrid/provider/SqlContentProvider.java index f0164db92..4d8fbe21d 100644 --- a/astrid/src/main/java/com/todoroo/astrid/provider/SqlContentProvider.java +++ b/astrid/src/main/java/com/todoroo/astrid/provider/SqlContentProvider.java @@ -11,15 +11,13 @@ import android.content.UriMatcher; import android.database.Cursor; import android.net.Uri; +import com.todoroo.andlib.data.AbstractDatabase; import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.astrid.api.AstridApiConstants; import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.service.AstridDependencyInjector; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * Non-public-API SQL content provider. * @@ -32,8 +30,6 @@ import org.slf4j.LoggerFactory; */ public class SqlContentProvider extends ContentProvider { - private static final Logger log = LoggerFactory.getLogger(SqlContentProvider.class); - // --- instance variables private static UriMatcher uriMatcher; @@ -52,19 +48,10 @@ public class SqlContentProvider extends ContentProvider { @Override public boolean onCreate() { - try { - database.openForWriting(); - return database.getDatabase() != null; - } catch (Exception e) { - log.error(e.getMessage(), e); - return false; - } + return true; } - public SqlContentProvider() { - DependencyInjectionService.getInstance().inject(this); - setReadPermission(AstridApiConstants.PERMISSION_READ); setWritePermission(AstridApiConstants.PERMISSION_WRITE); } @@ -129,7 +116,15 @@ public class SqlContentProvider extends ContentProvider { public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { - return database.rawQuery(selection); + return getDatabase().rawQuery(selection); } + private AbstractDatabase getDatabase() { + if (database == null) { + DependencyInjectionService.getInstance().inject(this); + database.openForWriting(); + } + + return database; + } }