From 8b8573697665d5362513f06ee079ae94bf6366c0 Mon Sep 17 00:00:00 2001 From: Tim Su Date: Sun, 4 Jul 2010 01:15:39 -0700 Subject: [PATCH] added reminder preferences, fixed up various minor bugs with tests and real app --- astrid/AndroidManifest.xml | 7 ++ .../astrid/reminders/Notifications.java | 10 +- .../astrid/reminders/ReminderPreferences.java | 119 ++++++++++++++++++ .../astrid/reminders/ReminderService.java | 33 ++++- astrid/res/values/arrays.xml | 10 +- astrid/res/values/keys.xml | 38 +++--- astrid/res/values/strings-3.0.xml | 5 + astrid/res/values/strings-reminders.xml | 31 ++++- astrid/res/xml/preferences.xml | 28 +++-- astrid/res/xml/preferences_reminders.xml | 34 ++--- .../timsu/astrid/utilities/Preferences.java | 61 +++++---- .../astrid/activity/TaskEditActivity.java | 14 +-- .../astrid/activity/TaskListActivity.java | 13 +- .../todoroo/astrid/adapter/TaskAdapter.java | 8 +- astrid/src/com/todoroo/astrid/model/Task.java | 15 +-- .../todoroo/astrid/utility/Preferences.java | 44 +++++-- .../astrid/reminders/NotificationTests.java | 10 +- .../todoroo/astrid/test/DatabaseTestCase.java | 10 +- .../upgrade/Astrid2To3UpgradeTests.java | 6 +- 19 files changed, 373 insertions(+), 123 deletions(-) create mode 100644 astrid/plugin-src/com/todoroo/astrid/reminders/ReminderPreferences.java diff --git a/astrid/AndroidManifest.xml b/astrid/AndroidManifest.xml index e796fd622..fda239bef 100644 --- a/astrid/AndroidManifest.xml +++ b/astrid/AndroidManifest.xml @@ -188,6 +188,13 @@ + + + + + + diff --git a/astrid/plugin-src/com/todoroo/astrid/reminders/Notifications.java b/astrid/plugin-src/com/todoroo/astrid/reminders/Notifications.java index 59e5aed22..50fc25ad7 100644 --- a/astrid/plugin-src/com/todoroo/astrid/reminders/Notifications.java +++ b/astrid/plugin-src/com/todoroo/astrid/reminders/Notifications.java @@ -55,13 +55,14 @@ public class Notifications extends BroadcastReceiver { AstridDependencyInjector.initialize(); } + public Notifications() { + DependencyInjectionService.getInstance().inject(this); + } + @Override /** Alarm intent */ public void onReceive(Context context, Intent intent) { - DependencyInjectionService.getInstance().inject(this); ContextManager.setContext(context); - if(notificationManager == null) - notificationManager = new AndroidNotificationManager(context); long id = intent.getLongExtra(ID_KEY, 0); int type = intent.getIntExtra(TYPE_KEY, (byte) 0); @@ -102,6 +103,8 @@ public class Notifications extends BroadcastReceiver { */ public boolean showNotification(long id, int type, String reminder) { Context context = ContextManager.getContext(); + if(notificationManager == null) + notificationManager = new AndroidNotificationManager(context); Task task; try { @@ -232,6 +235,7 @@ public class Notifications extends BroadcastReceiver { if(Constants.DEBUG) Log.w("Astrid", "Logging notification: " + reminder); //$NON-NLS-1$ //$NON-NLS-2$ + notificationManager.notify((int)id, notification); return true; diff --git a/astrid/plugin-src/com/todoroo/astrid/reminders/ReminderPreferences.java b/astrid/plugin-src/com/todoroo/astrid/reminders/ReminderPreferences.java new file mode 100644 index 000000000..e16abe301 --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/reminders/ReminderPreferences.java @@ -0,0 +1,119 @@ +/** + * See the file "LICENSE" for the full license governing this code. + */ +package com.todoroo.astrid.reminders; + +import android.content.res.Resources; +import android.os.Bundle; +import android.preference.CheckBoxPreference; +import android.preference.EditTextPreference; +import android.preference.ListPreference; +import android.preference.Preference; +import android.preference.PreferenceActivity; +import android.preference.PreferenceGroup; +import android.preference.PreferenceScreen; +import android.preference.Preference.OnPreferenceChangeListener; + +import com.timsu.astrid.R; + +/** + * Displays the preference screen for users to edit their preferences + * + * @author Tim Su + * + */ +public class ReminderPreferences extends PreferenceActivity { + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + addPreferencesFromResource(R.xml.preferences_reminders); + + PreferenceScreen screen = getPreferenceScreen(); + initializePreference(screen); + + } + + private void initializePreference(Preference preference) { + if(preference instanceof PreferenceGroup) { + PreferenceGroup group = (PreferenceGroup)preference; + for(int i = 0; i < group.getPreferenceCount(); i++) { + initializePreference(group.getPreference(i)); + } + } else { + Object value = null; + if(preference instanceof ListPreference) + value = ((ListPreference)preference).getValue(); + else if(preference instanceof CheckBoxPreference) + value = ((CheckBoxPreference)preference).isChecked(); + else if(preference instanceof EditTextPreference) + value = ((EditTextPreference)preference).getText(); + + if(value != null) + updatePreferences(preference, value); + + preference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { + public boolean onPreferenceChange(Preference myPreference, Object newValue) { + updatePreferences(myPreference, newValue); + return true; + } + }); + } + } + + @Override + protected void onStart() { + super.onStart(); + } + + @Override + protected void onStop() { + super.onStop(); + } + + protected int valueToIndex(String value, String[] array) { + for(int i = 0; i < array.length; i++) + if(array[i].equals(value)) + return i; + return -1; + } + + /** + * + * @param resource if null, updates all resources + */ + protected void updatePreferences(Preference preference, Object value) { + Resources r = getResources(); + + if(r.getString(R.string.p_rmd_quietStart).equals(preference.getKey())) { + int index = valueToIndex((String)value, r.getStringArray(R.array.EPr_quiet_hours_start_values)); + if(index == -1) + preference.setSummary(r.getString(R.string.rmd_EPr_quiet_hours_desc_none)); + else { + String duration = r.getStringArray(R.array.EPr_quiet_hours_start)[index]; + preference.setSummary(r.getString(R.string.rmd_EPr_quiet_hours_start_desc, duration)); + } + } else if(r.getString(R.string.p_rmd_quietEnd).equals(preference.getKey())) { + int index = valueToIndex((String)value, r.getStringArray(R.array.EPr_quiet_hours_end_values)); + if(index == -1) + preference.setSummary(r.getString(R.string.rmd_EPr_quiet_hours_desc_none)); + else { + String duration = r.getStringArray(R.array.EPr_quiet_hours_end)[index]; + preference.setSummary(r.getString(R.string.rmd_EPr_quiet_hours_end_desc, duration)); + } + } else if(r.getString(R.string.p_rmd_ringtone).equals(preference.getKey())) { + if(value == null) + preference.setSummary(r.getString(R.string.rmd_EPr_ringtone_desc_default)); + else + preference.setSummary(r.getString(R.string.rmd_EPr_ringtone_desc_custom)); + } else if(r.getString(R.string.p_rmd_persistent).equals(preference.getKey())) { + if((Boolean)value) + preference.setSummary(r.getString(R.string.rmd_EPr_persistent_desc_true)); + else + preference.setSummary(r.getString(R.string.rmd_EPr_persistent_desc_false)); + } + + } + +} \ No newline at end of file diff --git a/astrid/plugin-src/com/todoroo/astrid/reminders/ReminderService.java b/astrid/plugin-src/com/todoroo/astrid/reminders/ReminderService.java index 57ab1842c..4fe6c93e2 100644 --- a/astrid/plugin-src/com/todoroo/astrid/reminders/ReminderService.java +++ b/astrid/plugin-src/com/todoroo/astrid/reminders/ReminderService.java @@ -7,6 +7,9 @@ import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; +import android.content.res.Resources; import android.util.Log; import com.timsu.astrid.R; @@ -62,6 +65,32 @@ public final class ReminderService { public ReminderService() { DependencyInjectionService.getInstance().inject(this); + setPreferenceDefaults(); + } + + // --- preference handling + + private static boolean preferencesInitialized = false; + + /** Set preference defaults, if unset. called at startup */ + public void setPreferenceDefaults() { + if(preferencesInitialized) + return; + + Context context = ContextManager.getContext(); + SharedPreferences prefs = Preferences.getPrefs(context); + Editor editor = prefs.edit(); + Resources r = context.getResources(); + + Preferences.setIfUnset(prefs, editor, r, R.string.p_rmd_quietStart, 22); + Preferences.setIfUnset(prefs, editor, r, R.string.p_rmd_quietEnd, 10); + Preferences.setIfUnset(prefs, editor, r, R.string.p_rmd_default_random, 7); + Preferences.setIfUnset(prefs, editor, r, R.string.p_rmd_time, 12); + Preferences.setIfUnset(prefs, editor, r, R.string.p_rmd_nagging, true); + Preferences.setIfUnset(prefs, editor, r, R.string.p_rmd_persistent, true); + + editor.commit(); + preferencesInitialized = true; } // --- reminder scheduling logic @@ -168,7 +197,7 @@ public final class ReminderService { else { // return notification time on this day Date date = new Date(dueDate); - date.setHours(Preferences.getIntegerFromString(R.string.p_reminder_time)); + date.setHours(Preferences.getIntegerFromString(R.string.p_rmd_time)); date.setMinutes(0); return date.getTime(); } @@ -207,7 +236,7 @@ public final class ReminderService { /** * Interface for testing */ - interface AlarmScheduler { + public interface AlarmScheduler { public void createAlarm(Task task, long time, int type); } diff --git a/astrid/res/values/arrays.xml b/astrid/res/values/arrays.xml index 24c97a09f..0e8246a35 100644 --- a/astrid/res/values/arrays.xml +++ b/astrid/res/values/arrays.xml @@ -51,6 +51,14 @@ + + + !!!! (Highest) + !!! + !! + ! (Lowest) + + No Urgency @@ -61,7 +69,7 @@ Next Month - + Don\'t hide Task is due diff --git a/astrid/res/values/keys.xml b/astrid/res/values/keys.xml index 9f3efd1ba..2bb01f99b 100644 --- a/astrid/res/values/keys.xml +++ b/astrid/res/values/keys.xml @@ -28,31 +28,31 @@ - notif_qstart + notif_qstart - notif_qend + notif_qend - notif_annoy + notif_annoy - notif_vibrate + notif_vibrate - notification_ringtone + notification_ringtone - notif_theme + notif_theme - nagging + nagging - - reminder_time + + reminder_time - - notif_default_reminder + + notif_default_reminder @@ -144,6 +144,14 @@ p_def_urg + + + 0 + 1 + 2 + 3 + + 0 @@ -156,10 +164,10 @@ - 0 - 1 - 2 - 3 + 0 + 1 + 2 + 3 diff --git a/astrid/res/values/strings-3.0.xml b/astrid/res/values/strings-3.0.xml index 526cfd91d..c532e9f08 100644 --- a/astrid/res/values/strings-3.0.xml +++ b/astrid/res/values/strings-3.0.xml @@ -407,4 +407,9 @@ If you don\'t want to see the new task right after you complete the old one, you Currently Set To: %s + + Default Hide Until + + Currently Set To: %s + diff --git a/astrid/res/values/strings-reminders.xml b/astrid/res/values/strings-reminders.xml index ad53e94c5..2e92b2332 100644 --- a/astrid/res/values/strings-reminders.xml +++ b/astrid/res/values/strings-reminders.xml @@ -5,8 +5,37 @@ - Reminder Settings + Reminder Settings + + Quiet Hours Start + + No notifications will appear after %s + + Please set both start and end times + + + Quiet Hours End + + Notifications will begin appearing starting at %s + + + Notification Ringtone + + Custom ringtone has been set + + Default ringtone will be used + + + Notification Persistence + + Notifications must be viewed individually to be cleared + + Notifications can be cleared with \"Clear All\" button + + + New Task Defaults + diff --git a/astrid/res/xml/preferences.xml b/astrid/res/xml/preferences.xml index 904e838db..e4934152f 100644 --- a/astrid/res/xml/preferences.xml +++ b/astrid/res/xml/preferences.xml @@ -2,12 +2,27 @@ + + + + + + - - + android:title="@string/rmd_EPr_alerts_header"> + android:title="@string/rmd_EPr_quiet_hours_start_title"/> + android:title="@string/rmd_EPr_quiet_hours_end_title"/> + android:key="@string/p_rmd_persistent" + android:title="@string/rmd_EPr_persistent_title"/> - diff --git a/astrid/src-legacy/com/timsu/astrid/utilities/Preferences.java b/astrid/src-legacy/com/timsu/astrid/utilities/Preferences.java index cb1ba33e8..af29d2b85 100644 --- a/astrid/src-legacy/com/timsu/astrid/utilities/Preferences.java +++ b/astrid/src-legacy/com/timsu/astrid/utilities/Preferences.java @@ -43,10 +43,10 @@ public class Preferences { Resources r = context.getResources(); Editor editor = prefs.edit(); - if(!prefs.contains(r.getString(R.string.p_notif_annoy))) { - editor.putBoolean(r.getString(R.string.p_notif_annoy), - DEFAULT_PERSISTENCE_MODE); - } +// if(!prefs.contains(r.getString(R.string.p_notif_annoy))) { +// editor.putBoolean(r.getString(R.string.p_notif_annoy), +// DEFAULT_PERSISTENCE_MODE); +// } if(!prefs.contains(r.getString(R.string.p_fontSize))) { editor.putString(r.getString(R.string.p_fontSize), "20"); } @@ -59,9 +59,9 @@ public class Preferences { if(!prefs.contains(r.getString(R.string.p_colorize))) { editor.putBoolean(r.getString(R.string.p_colorize), DEFAULT_COLORIZE); } - if(!prefs.contains(r.getString(R.string.p_notif_vibrate))) { - editor.putBoolean(r.getString(R.string.p_notif_vibrate), true); - } +// if(!prefs.contains(r.getString(R.string.p_notif_vibrate))) { +// editor.putBoolean(r.getString(R.string.p_notif_vibrate), true); +// } if (!prefs.contains(r.getString(R.string.p_backup))) { editor.putBoolean(r.getString(R.string.p_backup), true); } @@ -230,47 +230,53 @@ public class Preferences { /** returns hour at which quiet hours start, or null if not set */ public static Integer getQuietHourStart(Context context) { - return getIntegerValue(context, R.string.p_notif_quietStart); +// return getIntegerValue(context, R.string.p_notif_quietStart); + return 0; } /** returns hour at which quiet hours start, or null if not set */ public static Integer getQuietHourEnd(Context context) { - return getIntegerValue(context, R.string.p_notif_quietEnd); +// return getIntegerValue(context, R.string.p_notif_quietEnd); + return 0; } /** returns hour at which quiet hours start, or null if not set */ public static int getNotificationIconTheme(Context context) { - Integer index = getIntegerValue(context, R.string.p_notif_icon); - if(index == null) - index = 0; - return index; +// Integer index = getIntegerValue(context, R.string.p_notif_icon); +// if(index == null) +// index = 0; +// return index; + return 0; } /** Get notification ring tone, or null if not set */ public static Uri getNotificationRingtone(Context context) { Resources r = context.getResources(); - String value = getPrefs(context).getString(r.getString( - R.string.p_notification_ringtone), ""); - - try { - return Uri.parse(value); - } catch (RuntimeException e) { - return null; - } +// String value = getPrefs(context).getString(r.getString( +// R.string.p_notification_ringtone), ""); +// +// try { +// return Uri.parse(value); +// } catch (RuntimeException e) { +// return null; +// } + return null; } /** Get perstence mode setting */ public static boolean isPersistenceMode(Context context) { Resources r = context.getResources(); - return getPrefs(context).getBoolean(r.getString( - R.string.p_notif_annoy), DEFAULT_PERSISTENCE_MODE); +// return getPrefs(context).getBoolean(r.getString( +// R.string.p_notif_annoy), DEFAULT_PERSISTENCE_MODE); + return false; } /** Get vibration mode setting */ public static boolean shouldVibrate(Context context) { Resources r = context.getResources(); - return getPrefs(context).getBoolean(r.getString( - R.string.p_notif_vibrate), true); +// return getPrefs(context).getBoolean(r.getString( +// R.string.p_notif_vibrate), true); + return false; } /** Return # of days to remind by default */ @@ -283,8 +289,9 @@ public class Preferences { /** whether nags for postponing and other things should be shown */ public static boolean shouldShowNags(Context context) { - return getPrefs(context).getBoolean(context.getResources(). - getString(R.string.p_nagging), true); +// return getPrefs(context).getBoolean(context.getResources(). +// getString(R.string.p_nagging), true); + return false; } // --- appearance settings diff --git a/astrid/src/com/todoroo/astrid/activity/TaskEditActivity.java b/astrid/src/com/todoroo/astrid/activity/TaskEditActivity.java index 84caecc87..0c07a6c79 100644 --- a/astrid/src/com/todoroo/astrid/activity/TaskEditActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskEditActivity.java @@ -27,9 +27,9 @@ import java.util.List; import android.app.AlertDialog; import android.app.DatePickerDialog; -import android.app.DatePickerDialog.OnDateSetListener; import android.app.TabActivity; import android.app.TimePickerDialog; +import android.app.DatePickerDialog.OnDateSetListener; import android.app.TimePickerDialog.OnTimeSetListener; import android.content.ContentValues; import android.content.DialogInterface; @@ -45,7 +45,6 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView; -import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; @@ -61,21 +60,22 @@ import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; import android.widget.ToggleButton; +import android.widget.AdapterView.OnItemSelectedListener; import com.flurry.android.FlurryAgent; import com.timsu.astrid.R; import com.timsu.astrid.data.enums.RepeatInterval; -import com.timsu.astrid.data.task.AbstractTaskModel.RepeatInfo; import com.timsu.astrid.data.task.TaskModelForEdit; +import com.timsu.astrid.data.task.AbstractTaskModel.RepeatInfo; import com.timsu.astrid.utilities.AstridUtilities; import com.timsu.astrid.widget.NumberPicker; import com.timsu.astrid.widget.NumberPickerDialog; -import com.timsu.astrid.widget.NumberPickerDialog.OnNumberPickedListener; import com.timsu.astrid.widget.TimeDurationControlSet; +import com.timsu.astrid.widget.NumberPickerDialog.OnNumberPickedListener; import com.timsu.astrid.widget.TimeDurationControlSet.TimeDurationType; +import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.data.Property.IntegerProperty; import com.todoroo.andlib.data.Property.StringProperty; -import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.service.ExceptionService; @@ -938,7 +938,7 @@ public final class TaskEditActivity extends TabActivity { hideUntil.setAdapter(adapter); if(isNewTask()) { - hideUntil.setSelection(0); + hideUntil.setSelection(Preferences.getIntegerFromString(R.string.p_default_hideUntil_key)); } else if(date <= DUE_DATE_LESS_SEVEN){ hideUntil.setSelection((int)date); } else { @@ -980,7 +980,7 @@ public final class TaskEditActivity extends TabActivity { random = (CheckBox)findViewById(randomId); mode = (Spinner)findViewById(modeId); - periodic = Preferences.getIntegerFromString(R.string.p_default_reminder_random) + periodic = Preferences.getIntegerFromString(R.string.p_rmd_default_random) * DateUtilities.ONE_DAY; updatePeriodicString(); diff --git a/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java b/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java index 58a7d0459..23f33dc3f 100644 --- a/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java @@ -1,5 +1,6 @@ package com.todoroo.astrid.activity; +import java.util.Date; import java.util.List; import java.util.Map.Entry; @@ -28,6 +29,7 @@ import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; +import android.widget.Toast; import android.widget.AbsListView.OnScrollListener; import android.widget.AdapterView.AdapterContextMenuInfo; @@ -52,6 +54,7 @@ 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.reminders.ReminderService.AlarmScheduler; import com.todoroo.astrid.service.MetadataService; import com.todoroo.astrid.service.StartupService; import com.todoroo.astrid.service.TaskService; @@ -540,7 +543,15 @@ public class TaskListActivity extends ListActivity implements OnScrollListener { itemId = item.getGroupId(); Task task = new Task(); task.setId(itemId); - new ReminderService().scheduleAlarm(task); + ReminderService reminderService = new ReminderService(); + reminderService.setScheduler(new AlarmScheduler() { + @Override + public void createAlarm(Task theTask, long time, int type) { + Toast.makeText(TaskListActivity.this, "Scheduled Alarm: " + //$NON-NLS-1$ + new Date(time), Toast.LENGTH_LONG).show(); + } + }); + reminderService.scheduleAlarm(task); return true; } diff --git a/astrid/src/com/todoroo/astrid/adapter/TaskAdapter.java b/astrid/src/com/todoroo/astrid/adapter/TaskAdapter.java index ec9235ff0..1665b07a5 100644 --- a/astrid/src/com/todoroo/astrid/adapter/TaskAdapter.java +++ b/astrid/src/com/todoroo/astrid/adapter/TaskAdapter.java @@ -11,11 +11,11 @@ import android.content.res.Resources; import android.database.Cursor; import android.graphics.Paint; import android.view.ContextMenu; -import android.view.ContextMenu.ContextMenuInfo; import android.view.LayoutInflater; import android.view.View; -import android.view.View.OnCreateContextMenuListener; import android.view.ViewGroup; +import android.view.ContextMenu.ContextMenuInfo; +import android.view.View.OnCreateContextMenuListener; import android.widget.CheckBox; import android.widget.CursorAdapter; import android.widget.LinearLayout; @@ -34,6 +34,7 @@ import com.todoroo.astrid.api.AstridApiConstants; import com.todoroo.astrid.api.TaskDetail; import com.todoroo.astrid.model.Task; import com.todoroo.astrid.service.TaskService; +import com.todoroo.astrid.utility.Preferences; /** * Adapter for displaying a user's tasks as a list @@ -83,6 +84,7 @@ public class TaskAdapter extends CursorAdapter { private final int resource; private final LayoutInflater inflater; protected OnCompletedTaskListener onCompletedTaskListener = null; + private final int fontSize; /** * Constructor @@ -112,6 +114,7 @@ public class TaskAdapter extends CursorAdapter { completedItems = new HashMap(); detailCache = new HashMap>(); + fontSize = Preferences.getIntegerFromString(R.string.p_fontSize); IMPORTANCE_COLORS = Task.getImportanceColors(activity.getResources()); } @@ -129,6 +132,7 @@ public class TaskAdapter extends CursorAdapter { ViewHolder viewHolder = new ViewHolder(); viewHolder.task = new Task(); viewHolder.nameView = (TextView)view.findViewById(R.id.title); + viewHolder.nameView.setTextSize(fontSize); viewHolder.completeBox = (CheckBox)view.findViewById(R.id.completeBox); viewHolder.dueDate = (TextView)view.findViewById(R.id.dueDate); viewHolder.details = (LinearLayout)view.findViewById(R.id.details); diff --git a/astrid/src/com/todoroo/astrid/model/Task.java b/astrid/src/com/todoroo/astrid/model/Task.java index c7dd9c018..db150f003 100644 --- a/astrid/src/com/todoroo/astrid/model/Task.java +++ b/astrid/src/com/todoroo/astrid/model/Task.java @@ -21,6 +21,7 @@ import com.todoroo.andlib.data.Property.IntegerProperty; import com.todoroo.andlib.data.Property.LongProperty; import com.todoroo.andlib.data.Property.StringProperty; import com.todoroo.andlib.utility.DateUtilities; +import com.todoroo.astrid.utility.Preferences; /** * Data Model which represents a task users need to accomplish. @@ -184,10 +185,8 @@ public final class Task extends AbstractModel { * Call to load task default values from preferences. */ public static void refreshDefaultValues() { - /*defaultValues.put(URGENCY.name, - Preferences.getIntegerFromString(R.string.EPr_default_urgency_key)); defaultValues.put(IMPORTANCE.name, - Preferences.getIntegerFromString(R.string.EPr_default_importance_key));*/ + Preferences.getIntegerFromString(R.string.p_default_importance_key)); defaultValuesLoaded = true; } @@ -260,8 +259,10 @@ public final class Task extends AbstractModel { * @return true if hours, minutes, and seconds indicate end of day */ private static boolean isEndOfDay(Date date) { - return date.getHours() == 23 && date.getMinutes() == 59 && - date.getSeconds() == 59; + int hours = date.getHours(); + int minutes = date.getMinutes(); + int seconds = date.getSeconds(); + return hours == 23 && minutes == 59 && seconds == 59; } /** @@ -286,7 +287,7 @@ public final class Task extends AbstractModel { * Checks whether this due date has a due time or only a date */ public boolean hasDueTime() { - return isEndOfDay(new Date(getValue(DUE_DATE))); + return !isEndOfDay(new Date(getValue(DUE_DATE))); } /** @@ -298,7 +299,7 @@ public final class Task extends AbstractModel { public boolean getFlag(IntegerProperty property, int flag) { return (getValue(property) & flag) > 0; } - + /** * @return repeat data structure. Requires REPEAT */ diff --git a/astrid/src/com/todoroo/astrid/utility/Preferences.java b/astrid/src/com/todoroo/astrid/utility/Preferences.java index 235bc5125..0174492c2 100644 --- a/astrid/src/com/todoroo/astrid/utility/Preferences.java +++ b/astrid/src/com/todoroo/astrid/utility/Preferences.java @@ -8,7 +8,6 @@ import android.preference.PreferenceManager; import com.timsu.astrid.R; import com.todoroo.andlib.service.ContextManager; -import com.todoroo.astrid.model.Task; public class Preferences { @@ -21,22 +20,41 @@ public class Preferences { Editor editor = prefs.edit(); Resources r = context.getResources(); - if(getIntegerFromString(R.string.p_default_urgency_key) == null) { - editor.putString(r.getString(R.string.p_default_urgency_key), - Integer.toString(4)); - } - if(getIntegerFromString(R.string.p_default_importance_key) == null) { - editor.putString(r.getString(R.string.p_default_importance_key), - Integer.toString(Task.IMPORTANCE_SHOULD_DO)); - } - if(getIntegerFromString(R.string.p_reminder_time) == null) { - editor.putString(r.getString(R.string.p_reminder_time), - Integer.toString(12)); - } + setIfUnset(prefs, editor, r, R.string.p_default_urgency_key, 4); + setIfUnset(prefs, editor, r, R.string.p_default_importance_key, 2); + setIfUnset(prefs, editor, r, R.string.p_default_hideUntil_key, 0); editor.commit(); } + /** + * Helper to write to editor if key specified is null + * @param prefs + * @param editor + * @param r + * @param keyResource + * @param value + */ + public static void setIfUnset(SharedPreferences prefs, Editor editor, Resources r, int keyResource, int value) { + String key = r.getString(keyResource); + if(!prefs.contains(key)) + editor.putString(key, Integer.toString(value)); + } + + /** + * Helper to write to editor if key specified is null + * @param prefs + * @param editor + * @param r + * @param keyResource + * @param value + */ + public static void setIfUnset(SharedPreferences prefs, Editor editor, Resources r, int keyResource, boolean value) { + String key = r.getString(keyResource); + if(!prefs.contains(key)) + editor.putBoolean(key, value); + } + /* ====================================================================== * ========================================================= system prefs * ====================================================================== */ diff --git a/tests/src/com/todoroo/astrid/reminders/NotificationTests.java b/tests/src/com/todoroo/astrid/reminders/NotificationTests.java index 85ca7e50e..4ba0b8c8a 100644 --- a/tests/src/com/todoroo/astrid/reminders/NotificationTests.java +++ b/tests/src/com/todoroo/astrid/reminders/NotificationTests.java @@ -113,8 +113,8 @@ public class NotificationTests extends DatabaseTestCase { intent.putExtra(Notifications.ID_KEY, task.getId()); int hour = new Date().getHours(); - Preferences.setStringFromInteger(R.string.p_notif_quietStart, hour - 1); - Preferences.setStringFromInteger(R.string.p_notif_quietEnd, hour + 1); + Preferences.setStringFromInteger(R.string.p_rmd_quietStart, hour - 1); + Preferences.setStringFromInteger(R.string.p_rmd_quietEnd, hour + 1); // due date notification has vibrate Notifications.setNotificationManager(new TestNotificationManager() { @@ -142,8 +142,8 @@ public class NotificationTests extends DatabaseTestCase { new Notifications().onReceive(getContext(), intent); // wrapping works - Preferences.setStringFromInteger(R.string.p_notif_quietStart, hour + 2); - Preferences.setStringFromInteger(R.string.p_notif_quietEnd, hour + 1); + Preferences.setStringFromInteger(R.string.p_rmd_quietStart, hour + 2); + Preferences.setStringFromInteger(R.string.p_rmd_quietEnd, hour + 1); Notifications.setNotificationManager(new TestNotificationManager() { public void notify(int id, Notification notification) { @@ -157,7 +157,7 @@ public class NotificationTests extends DatabaseTestCase { // nonstop notification still sounds task.setValue(Task.REMINDER_FLAGS, Task.NOTIFY_NONSTOP); - task.save(); + taskDao.persist(task); Notifications.setNotificationManager(new TestNotificationManager() { public void notify(int id, Notification notification) { assertTrue(notification.sound != null || diff --git a/tests/src/com/todoroo/astrid/test/DatabaseTestCase.java b/tests/src/com/todoroo/astrid/test/DatabaseTestCase.java index bd2918758..727110cd8 100644 --- a/tests/src/com/todoroo/astrid/test/DatabaseTestCase.java +++ b/tests/src/com/todoroo/astrid/test/DatabaseTestCase.java @@ -19,20 +19,18 @@ public class DatabaseTestCase extends TodorooTestCase { public static Database database = new TestDatabase(); - public AlarmDatabase alarmsDatabase; - static { AstridDependencyInjector.initialize(); - - // initialize test dependency injector - TestDependencyInjector injector = TestDependencyInjector.initialize("db"); - injector.addInjectable("database", database); } @Override protected void setUp() throws Exception { super.setUp(); + // initialize test dependency injector + TestDependencyInjector injector = TestDependencyInjector.initialize("db"); + injector.addInjectable("database", database); + DependencyInjectionService.getInstance().inject(this); // empty out test databases diff --git a/tests/src/com/todoroo/astrid/upgrade/Astrid2To3UpgradeTests.java b/tests/src/com/todoroo/astrid/upgrade/Astrid2To3UpgradeTests.java index b461cc4fb..5397d2f88 100644 --- a/tests/src/com/todoroo/astrid/upgrade/Astrid2To3UpgradeTests.java +++ b/tests/src/com/todoroo/astrid/upgrade/Astrid2To3UpgradeTests.java @@ -34,7 +34,9 @@ public class Astrid2To3UpgradeTests extends DatabaseTestCase { private static final String TAGS_TEST = "tagstest"; private static final String TASKS_TEST = "taskstest"; - // --- setup and teardnwo + // --- setup and teardown + + private AlarmDatabase alarmsDatabase; @Autowired TaskDao taskDao; @@ -117,6 +119,7 @@ public class Astrid2To3UpgradeTests extends DatabaseTestCase { taskController.saveTask(griffey, false); TaskModelForEdit guti = new com.todoroo.astrid.legacy.data.task.TaskModelForEdit(); + Date createdDate = new Date(); guti.setName("franklin gutierrez"); guti.setPreferredDueDate(new Date(System.currentTimeMillis() + 5000000L)); guti.setImportance(Importance.LEVEL_1); @@ -125,7 +128,6 @@ public class Astrid2To3UpgradeTests extends DatabaseTestCase { guti.setElapsedSeconds(500); guti.setNotificationIntervalSeconds(200); taskController.saveTask(guti, false); - Date createdDate = new Date(); // assert created assertEquals(2, taskController.getAllTaskIdentifiers().size());