From 7841448da398efe6a3a39449d5102f56b9af60a3 Mon Sep 17 00:00:00 2001 From: Tim Su Date: Wed, 7 Jul 2010 23:10:38 -0700 Subject: [PATCH] fixed up widget so now application starts up without crashing. --- astrid/AndroidManifest.xml | 2 +- .../com/todoroo/andlib/data/AbstractDatabase.java | 9 ++++++--- .../com/todoroo/astrid/reminders/ReminderService.java | 5 +++++ astrid/src/com/todoroo/astrid/dao/Database.java | 2 +- astrid/src/com/todoroo/astrid/widget/TasksWidget.java | 10 +++------- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/astrid/AndroidManifest.xml b/astrid/AndroidManifest.xml index 3e68894bc..46897df81 100644 --- a/astrid/AndroidManifest.xml +++ b/astrid/AndroidManifest.xml @@ -129,7 +129,7 @@ - + diff --git a/astrid/common-src/com/todoroo/andlib/data/AbstractDatabase.java b/astrid/common-src/com/todoroo/andlib/data/AbstractDatabase.java index f81d251d8..7990a7283 100644 --- a/astrid/common-src/com/todoroo/andlib/data/AbstractDatabase.java +++ b/astrid/common-src/com/todoroo/andlib/data/AbstractDatabase.java @@ -107,6 +107,9 @@ abstract public class AbstractDatabase { public synchronized final void openForWriting() { initializeHelper(); + if(database != null && !database.isReadOnly() && database.isOpen()) + return; + try { database = helper.getWritableDatabase(); } catch (SQLiteException writeException) { @@ -127,6 +130,8 @@ abstract public class AbstractDatabase { */ public synchronized final void openForReading() { initializeHelper(); + if(database != null && database.isOpen()) + return; database = helper.getReadableDatabase(); } @@ -153,9 +158,6 @@ abstract public class AbstractDatabase { * @return sql database. opens database if not yet open */ private synchronized final SQLiteDatabase getDatabase() { - // open database if requested - if(database == null) - openForWriting(); return database; } @@ -207,6 +209,7 @@ abstract public class AbstractDatabase { public DatabaseHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); + Log.e("AYAAA", "NEW DATABASE HLEPER", new Throwable()); } /** diff --git a/astrid/plugin-src/com/todoroo/astrid/reminders/ReminderService.java b/astrid/plugin-src/com/todoroo/astrid/reminders/ReminderService.java index 2d8c77bcb..889605503 100644 --- a/astrid/plugin-src/com/todoroo/astrid/reminders/ReminderService.java +++ b/astrid/plugin-src/com/todoroo/astrid/reminders/ReminderService.java @@ -21,6 +21,7 @@ import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.sql.Criterion; import com.todoroo.andlib.sql.Query; import com.todoroo.andlib.utility.DateUtilities; +import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.dao.TaskDao.TaskCriteria; import com.todoroo.astrid.model.Task; @@ -61,6 +62,9 @@ public final class ReminderService { @Autowired private TaskDao taskDao; + @Autowired + private Database database; + private AlarmScheduler scheduler = new ReminderAlarmScheduler(); public ReminderService() { @@ -295,6 +299,7 @@ public final class ReminderService { * @return todoroo cursor. PLEASE CLOSE THIS CURSOR! */ private TodorooCursor getTasksWithReminders(Property... properties) { + database.openForReading(); return taskDao.query(Query.select(properties).where(Criterion.and(TaskCriteria.isActive(), Task.REMINDER_FLAGS.gt(0)))); } diff --git a/astrid/src/com/todoroo/astrid/dao/Database.java b/astrid/src/com/todoroo/astrid/dao/Database.java index e8471e049..69a53ac08 100644 --- a/astrid/src/com/todoroo/astrid/dao/Database.java +++ b/astrid/src/com/todoroo/astrid/dao/Database.java @@ -17,7 +17,7 @@ import com.todoroo.astrid.model.Task; * */ @SuppressWarnings("nls") -public class Database extends AbstractDatabase { +public final class Database extends AbstractDatabase { // --- constants diff --git a/astrid/src/com/todoroo/astrid/widget/TasksWidget.java b/astrid/src/com/todoroo/astrid/widget/TasksWidget.java index e7609954e..907577338 100644 --- a/astrid/src/com/todoroo/astrid/widget/TasksWidget.java +++ b/astrid/src/com/todoroo/astrid/widget/TasksWidget.java @@ -58,13 +58,8 @@ public class TasksWidget extends AppWidgetProvider { @Override public void onStart(Intent intent, int startId) { - try { - Thread.sleep(3000); - } catch (InterruptedException e) { - return; - } - RemoteViews updateViews = buildUpdate(this); ContextManager.setContext(this); + RemoteViews updateViews = buildUpdate(this); ComponentName thisWidget = new ComponentName(this, TasksWidget.class); @@ -79,6 +74,8 @@ public class TasksWidget extends AppWidgetProvider { @SuppressWarnings("nls") public RemoteViews buildUpdate(Context context) { + DependencyInjectionService.getInstance().inject(this); + RemoteViews views = null; views = new RemoteViews(context.getPackageName(), @@ -97,7 +94,6 @@ public class TasksWidget extends AppWidgetProvider { try { Filter inboxFilter = CoreFilterExposer.buildInboxFilter(getResources()); inboxFilter.sqlQuery += "ORDER BY " + TaskService.defaultTaskOrder() + " LIMIT " + numberOfTasks; - DependencyInjectionService.getInstance().inject(this); database.openForReading(); cursor = taskService.fetchFiltered(inboxFilter, Task.TITLE, Task.DUE_DATE);