diff --git a/astrid/AndroidManifest.xml b/astrid/AndroidManifest.xml index a0235fd51..da2e27d89 100644 --- a/astrid/AndroidManifest.xml +++ b/astrid/AndroidManifest.xml @@ -196,7 +196,7 @@ + android:taskAffinity="astrid.reminders" /> diff --git a/astrid/common-src/com/todoroo/andlib/data/AbstractDatabase.java b/astrid/common-src/com/todoroo/andlib/data/AbstractDatabase.java index e5d268a57..197356893 100644 --- a/astrid/common-src/com/todoroo/andlib/data/AbstractDatabase.java +++ b/astrid/common-src/com/todoroo/andlib/data/AbstractDatabase.java @@ -148,11 +148,12 @@ abstract public class AbstractDatabase { } /** - * @return sql database. throws error if database was not opened + * @return sql database. opens database if not yet open */ public final SQLiteDatabase getDatabase() { + // open database if requested if(database == null) - throw new IllegalStateException("Database was not opened!"); + openForWriting(); return database; } diff --git a/astrid/plugin-src/com/todoroo/astrid/reminders/Notifications.java b/astrid/plugin-src/com/todoroo/astrid/reminders/Notifications.java index 207c7e3cc..6676cfe13 100644 --- a/astrid/plugin-src/com/todoroo/astrid/reminders/Notifications.java +++ b/astrid/plugin-src/com/todoroo/astrid/reminders/Notifications.java @@ -20,6 +20,7 @@ import com.timsu.astrid.data.task.TaskIdentifier; import com.timsu.astrid.utilities.Constants; import com.timsu.astrid.utilities.Preferences; import com.todoroo.andlib.service.Autowired; +import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.service.ExceptionService; import com.todoroo.andlib.utility.DateUtilities; @@ -55,6 +56,7 @@ public class Notifications extends BroadcastReceiver { /** Alarm intent */ public void onReceive(Context context, Intent intent) { DependencyInjectionService.getInstance().inject(this); + ContextManager.setContext(context); long id = intent.getLongExtra(ID_KEY, 0); int type = intent.getIntExtra(TYPE_KEY, 0); @@ -68,7 +70,7 @@ public class Notifications extends BroadcastReceiver { else reminder = getRandomReminder(r.getStringArray(R.array.reminders)); - if(!showNotification(context, id, type, reminder)) { + if(!showNotification(id, type, reminder)) { NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); nm.cancel((int)id); @@ -95,8 +97,8 @@ public class Notifications extends BroadcastReceiver { * Schedule a new notification about the given task. Returns false if there was * some sort of error or the alarm should be disabled. */ - public boolean showNotification(Context context, long id, - int type, String reminder) { + public boolean showNotification(long id, int type, String reminder) { + Context context = ContextManager.getContext(); Task task; try { diff --git a/astrid/plugin-src/com/todoroo/astrid/reminders/ReminderService.java b/astrid/plugin-src/com/todoroo/astrid/reminders/ReminderService.java index 3b4570d5a..816ec673d 100644 --- a/astrid/plugin-src/com/todoroo/astrid/reminders/ReminderService.java +++ b/astrid/plugin-src/com/todoroo/astrid/reminders/ReminderService.java @@ -10,7 +10,6 @@ import android.content.Intent; import android.util.Log; import com.timsu.astrid.R; -import com.timsu.astrid.utilities.Constants; import com.todoroo.andlib.data.Property; import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.service.Autowired; @@ -22,6 +21,7 @@ import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.dao.TaskDao.TaskCriteria; import com.todoroo.astrid.model.Task; +import com.todoroo.astrid.utility.Constants; import com.todoroo.astrid.utility.Preferences; @@ -68,6 +68,7 @@ public final class ReminderService { * Schedules all alarms */ public void scheduleAllAlarms() { + TodorooCursor cursor = getTasksWithReminders(PROPERTIES); try { Task task = new Task(); @@ -240,7 +241,7 @@ public final class ReminderService { * @param properties * @return todoroo cursor. PLEASE CLOSE THIS CURSOR! */ - public TodorooCursor getTasksWithReminders(Property... properties) { + private TodorooCursor getTasksWithReminders(Property... properties) { return taskDao.query(Query.select(properties).where(Criterion.and(TaskCriteria.isActive(), Task.REMINDER_FLAGS.gt(0)))); } diff --git a/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java b/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java index 2027f6eef..d0188c9a4 100644 --- a/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java @@ -33,7 +33,6 @@ import android.widget.AdapterView.AdapterContextMenuInfo; import com.flurry.android.FlurryAgent; import com.timsu.astrid.R; -import com.timsu.astrid.activities.EditPreferences; import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; @@ -50,6 +49,8 @@ import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.filters.CoreFilterExposer; import com.todoroo.astrid.model.Metadata; import com.todoroo.astrid.model.Task; +import com.todoroo.astrid.reminders.Notifications; +import com.todoroo.astrid.reminders.ReminderService; import com.todoroo.astrid.service.MetadataService; import com.todoroo.astrid.service.StartupService; import com.todoroo.astrid.service.TaskService; @@ -81,6 +82,9 @@ public class TaskListActivity extends ListActivity implements OnScrollListener { private static final int CONTEXT_MENU_DELETE_TASK_ID = Menu.FIRST + 6; private static final int CONTEXT_MENU_ADDON_INTENT_ID = Menu.FIRST + 7; + /** menu code indicating the end of the context menu */ + private static final int CONTEXT_MENU_DEBUG = Menu.FIRST + 8; + // --- constants public static final String TOKEN_FILTER = "filter"; //$NON-NLS-1$ @@ -133,6 +137,8 @@ public class TaskListActivity extends ListActivity implements OnScrollListener { if(database == null) return; + if(Constants.DEBUG) + filter.title = "[D] " + filter.title; //$NON-NLS-1$ database.openForWriting(); setUpUiComponents(); @@ -428,6 +434,13 @@ public class TaskListActivity extends ListActivity implements OnScrollListener { menu.add(id, CONTEXT_MENU_DELETE_TASK_ID, Menu.NONE, R.string.TAd_contextDeleteTask); + if(Constants.DEBUG) { + menu.add(id, CONTEXT_MENU_DEBUG, Menu.NONE, + "schedule alarm"); //$NON-NLS-1$ + menu.add(id, CONTEXT_MENU_DEBUG + 1, Menu.NONE, + "make notification"); //$NON-NLS-1$ + } + if(contextMenuItemCache == null) return; @@ -496,7 +509,7 @@ public class TaskListActivity extends ListActivity implements OnScrollListener { return true; } - case CONTEXT_MENU_DELETE_TASK_ID: + case CONTEXT_MENU_DELETE_TASK_ID: { itemId = item.getGroupId(); Task task = new Task(); task.setId(itemId); @@ -504,6 +517,22 @@ public class TaskListActivity extends ListActivity implements OnScrollListener { return true; } + case CONTEXT_MENU_DEBUG: { + itemId = item.getGroupId(); + Task task = new Task(); + task.setId(itemId); + new ReminderService().scheduleAlarm(task); + return true; + } + + case CONTEXT_MENU_DEBUG + 1: { + itemId = item.getGroupId(); + new Notifications().showNotification(itemId, 0, "test reminder"); //$NON-NLS-1$ + return true; + } + + } + return false; } diff --git a/astrid/src/com/todoroo/astrid/dao/TaskDao.java b/astrid/src/com/todoroo/astrid/dao/TaskDao.java index d9cee3a4c..ee88237fc 100644 --- a/astrid/src/com/todoroo/astrid/dao/TaskDao.java +++ b/astrid/src/com/todoroo/astrid/dao/TaskDao.java @@ -35,7 +35,7 @@ public class TaskDao extends GenericDao { @Autowired Database database; - ReminderService reminderService = new ReminderService(); + ReminderService reminderService; public TaskDao() { super(Task.class); @@ -197,8 +197,11 @@ public class TaskDao extends GenericDao { private void afterSave(Task task, ContentValues values, boolean duringSync) { if(values.containsKey(Task.COMPLETION_DATE.name) && task.isCompleted()) afterComplete(task, values, duringSync); - else + else { + if(reminderService == null) + reminderService = new ReminderService(); reminderService.scheduleAlarm(task); + } if(duringSync) return; diff --git a/astrid/src/com/todoroo/astrid/model/Task.java b/astrid/src/com/todoroo/astrid/model/Task.java index 6c3714f84..e96eba94a 100644 --- a/astrid/src/com/todoroo/astrid/model/Task.java +++ b/astrid/src/com/todoroo/astrid/model/Task.java @@ -92,15 +92,15 @@ public final class Task extends AbstractModel { /** Flags for when to send reminders */ public static final IntegerProperty REMINDER_FLAGS = new IntegerProperty( - TABLE, "reminderFlags"); + TABLE, "notificationFlags"); /** Reminder period, in milliseconds. 0 means disabled */ public static final LongProperty REMINDER_PERIOD = new LongProperty( - TABLE, "reminderPeriod"); + TABLE, "notifications"); /** Unixtime the last reminder was triggered */ public static final LongProperty REMINDER_LAST = new LongProperty( - TABLE, "reminderLast"); + TABLE, "lastNotified"); public static final IntegerProperty REPEAT = new IntegerProperty( TABLE, "repeat"); diff --git a/astrid/src/com/todoroo/astrid/utility/Constants.java b/astrid/src/com/todoroo/astrid/utility/Constants.java index 70fc9fdc3..76dfdd434 100644 --- a/astrid/src/com/todoroo/astrid/utility/Constants.java +++ b/astrid/src/com/todoroo/astrid/utility/Constants.java @@ -28,4 +28,8 @@ public final class Constants { */ public static final long WIDGET_UPDATE_INTERVAL = 30 * 60 * 1000L; + /** + * Whether to turn on debugging logging and UI + */ + public static final boolean DEBUG = true; }