Time preference handles minutes

Closes #52
pull/120/head
Alex Baker 11 years ago
parent b2a7a9fd08
commit 62c280e532

@ -129,6 +129,10 @@ public class Preferences {
/**
* Sets string preference
*/
public static void setString(int resourceId, String value) {
setString(ContextManager.getContext().getString(resourceId), value);
}
public static void setString(String key, String newValue) {
Context context = ContextManager.getContext();
Editor editor = getPrefs(context).edit();
@ -193,6 +197,14 @@ public class Preferences {
/** Gets a int preference
* @return default if value is unset otherwise the value
*/
public static int getInt(int resourceId) {
return getInt(resourceId, 0);
}
public static int getInt(int resourceId, int defValue) {
return getInt(ContextManager.getContext().getString(resourceId), defValue);
}
public static int getInt(String key, int defValue) {
Context context = ContextManager.getContext();
return getPrefs(context).getInt(key, defValue);
@ -201,6 +213,10 @@ public class Preferences {
/**
* Sets int preference
*/
public static void setInt(int resourceId, int value) {
setInt(ContextManager.getContext().getString(resourceId), value);
}
public static void setInt(String key, int value) {
Context context = ContextManager.getContext();
Editor editor = getPrefs(context).edit();

@ -14,7 +14,6 @@ package com.todoroo.andlib.utility;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.DialogPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
@ -80,7 +79,7 @@ abstract public class TodorooPreferenceActivity extends PreferenceActivity {
value = PreferenceManager.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), null);
} else if(preference instanceof TimePreference) {
value = ((TimePreference) preference).getLastHour();
value = ((TimePreference) preference).getMillisOfDay();
}
updatePreferences(preference, value);

@ -8,6 +8,8 @@ import android.util.AttributeSet;
import android.view.View;
import android.widget.TimePicker;
import org.joda.time.DateTime;
/**
* Preference dialog that displays a TimePicker and persists the selected value.
*
@ -22,7 +24,7 @@ import android.widget.TimePicker;
public class TimePreference extends DialogPreference {
/** The last hour digit picked by the user in String format */
private String lastHour = "0";
private int millisOfDay;
private TimePicker picker = null;
public TimePreference(Context context, AttributeSet attrs) {
@ -37,9 +39,7 @@ public class TimePreference extends DialogPreference {
public View onCreateDialogView() {
picker = new TimePicker(getContext());
picker.setCurrentHour(Integer.parseInt(getLastHour()));
picker.setCurrentMinute(0);
picker.setIs24HourView(DateFormat.is24HourFormat(getContext()));
refreshPicker();
return picker;
}
@ -48,9 +48,15 @@ public class TimePreference extends DialogPreference {
public void onBindDialogView(View v) {
super.onBindDialogView(v);
picker.setCurrentHour(Integer.parseInt(getLastHour()));
picker.setCurrentMinute(0);
refreshPicker();
}
private void refreshPicker() {
DateTime dateTime = DateTime.now().withMillisOfDay(millisOfDay);
picker.setCurrentHour(dateTime.getHourOfDay());
picker.setCurrentMinute(dateTime.getMinuteOfHour());
picker.setIs24HourView(DateFormat.is24HourFormat(getContext()));
}
@Override
@ -58,33 +64,37 @@ public class TimePreference extends DialogPreference {
super.onDialogClosed(positiveResult);
/** When the dialog is closed update the lastHour variable and store the value in preferences */
if (positiveResult) {
lastHour = String.valueOf(picker.getCurrentHour());
if (callChangeListener(lastHour)) {
persistString(lastHour);
millisOfDay = new DateTime()
.withMillisOfDay(0)
.withHourOfDay(picker.getCurrentHour())
.withMinuteOfHour(picker.getCurrentMinute())
.getMillisOfDay();
if (callChangeListener(millisOfDay)) {
persistInt(millisOfDay);
}
}
}
@Override
public Object onGetDefaultValue(TypedArray array, int index) {
return (array.getString(index));
return (array.getInt(index, 0));
}
/** When called for the first time initialize the value of the last hour to either the saved one
* or to the default one. If a default one is not provided use "0" */
@Override
public void onSetInitialValue(boolean restoreValue, Object defaultValue) {
String defString = (defaultValue == null) ? "0" : defaultValue.toString();
int def = (defaultValue == null) ? 0 : (int) defaultValue;
if (restoreValue) {
lastHour = getPersistedString(defString);
millisOfDay = getPersistedInt(def);
} else {
lastHour = defString;
millisOfDay = def;
}
}
public String getLastHour() {
return lastHour;
public int getMillisOfDay() {
return millisOfDay;
}
}

@ -42,13 +42,14 @@ import com.todoroo.astrid.utility.Constants;
import com.todoroo.astrid.utility.Flags;
import com.todoroo.astrid.voice.VoiceOutputService;
import org.joda.time.DateTime;
import org.tasks.R;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static org.tasks.date.DateTimeUtils.newDate;
import static org.tasks.date.DateTimeUtils.currentTimeMillis;
public class Notifications extends BroadcastReceiver {
@ -470,17 +471,16 @@ public class Notifications extends BroadcastReceiver {
*/
public static boolean isQuietHours() {
boolean quietHoursEnabled = Preferences.getBoolean(R.string.p_rmd_enable_quiet, false);
int quietHoursStart = Preferences.getIntegerFromString(R.string.p_rmd_quietStart, -1);
int quietHoursEnd = Preferences.getIntegerFromString(R.string.p_rmd_quietEnd, -1);
if(quietHoursEnabled) {
int hour = newDate().getHours();
long quietHoursStart = new DateTime().withMillisOfDay(Preferences.getInt(R.string.p_rmd_quietStart)).getMillis();
long quietHoursEnd = new DateTime().withMillisOfDay(Preferences.getInt(R.string.p_rmd_quietEnd)).getMillis();
long now = currentTimeMillis();
if(quietHoursStart <= quietHoursEnd) {
if(hour >= quietHoursStart && hour < quietHoursEnd) {
if(now >= quietHoursStart && now < quietHoursEnd) {
return true;
}
} else { // wrap across 24/hour boundary
if(hour >= quietHoursStart || hour < quietHoursEnd) {
if(now >= quietHoursStart || now < quietHoursEnd) {
return true;
}
}

@ -8,12 +8,14 @@ package com.todoroo.astrid.reminders;
import android.content.res.Resources;
import android.preference.Preference;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.andlib.utility.TodorooPreferenceActivity;
import org.joda.time.DateTime;
import org.tasks.R;
import java.text.DateFormat;
/**
* Displays the preference screen for users to edit their preferences
*
@ -38,25 +40,24 @@ public class ReminderPreferences extends TodorooPreferenceActivity {
preference.setSummary("");
}
} else if(r.getString(R.string.p_rmd_quietStart).equals(preference.getKey())) {
int index = Integer.parseInt((String) value);
if(index <= 0) {
preference.setSummary(r.getString(R.string.rmd_EPr_quiet_hours_desc_none));
} else {
String setting = String.valueOf(index);
if(Preferences.getBoolean(R.string.p_rmd_enable_quiet, false)) {
int millisOfDay = (int) value;
String setting = DateFormat.getTimeInstance(DateFormat.SHORT).format(new DateTime().withMillisOfDay(millisOfDay).toDate());
preference.setSummary(r.getString(R.string.rmd_EPr_quiet_hours_start_desc, setting));
} else {
preference.setSummary(r.getString(R.string.rmd_EPr_quiet_hours_desc_none));
}
} else if(r.getString(R.string.p_rmd_quietEnd).equals(preference.getKey())) {
int index = Integer.parseInt((String) value);
int quietHoursStart = Preferences.getIntegerFromString(R.string.p_rmd_quietStart, -1);
if(index == -1 || quietHoursStart == -1) {
preference.setSummary(r.getString(R.string.rmd_EPr_quiet_hours_desc_none));
} else {
String setting = String.valueOf(index);
if(Preferences.getBoolean(R.string.p_rmd_enable_quiet, false)) {
int millisOfDay = (int) value;
String setting = DateFormat.getTimeInstance(DateFormat.SHORT).format(new DateTime().withMillisOfDay(millisOfDay).toDate());
preference.setSummary(r.getString(R.string.rmd_EPr_quiet_hours_end_desc, setting));
} else {
preference.setSummary(r.getString(R.string.rmd_EPr_quiet_hours_desc_none));
}
} else if(r.getString(R.string.p_rmd_time).equals(preference.getKey())) {
String setting = (String) value;
int millisOfDay = (int) value;
String setting = DateFormat.getTimeInstance(DateFormat.SHORT).format(new DateTime().withMillisOfDay(millisOfDay).toDate());
preference.setSummary(r.getString(R.string.rmd_EPr_rmd_time_desc, setting));
} else if(r.getString(R.string.p_rmd_ringtone).equals(preference.getKey())) {
if(value == null || "content://settings/system/notification_sound".equals(value)) //$NON-NLS-1$

@ -28,11 +28,13 @@ import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.utility.Constants;
import org.joda.time.DateTime;
import org.tasks.R;
import java.util.Date;
import java.util.Random;
import static org.tasks.date.DateTimeUtils.currentTimeMillis;
import static org.tasks.date.DateTimeUtils.newDate;
@ -105,6 +107,8 @@ public final class ReminderService {
private static boolean preferencesInitialized = false;
private static final int MILLIS_PER_HOUR = 60 * 60 * 1000;
/** Set preference defaults, if unset. called at startup */
public void setPreferenceDefaults() {
if(preferencesInitialized) {
@ -117,10 +121,10 @@ public final class ReminderService {
Resources r = context.getResources();
Preferences.setIfUnset(prefs, editor, r, R.string.p_rmd_enable_quiet, false);
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_quietStart, 22 * MILLIS_PER_HOUR);
Preferences.setIfUnset(prefs, editor, r, R.string.p_rmd_quietEnd, 10 * MILLIS_PER_HOUR);
Preferences.setIfUnset(prefs, editor, r, R.string.p_rmd_default_random_hours, 0);
Preferences.setIfUnset(prefs, editor, r, R.string.p_rmd_time, 18);
Preferences.setIfUnset(prefs, editor, r, R.string.p_rmd_time, 18 * MILLIS_PER_HOUR);
Preferences.setIfUnset(prefs, editor, r, R.string.p_rmd_persistent, true);
editor.commit();
@ -317,27 +321,18 @@ public final class ReminderService {
dueDateAlarm = dueDate;
} else if (DateUtilities.now() > lastReminder + DateUtilities.ONE_DAY) {
// return notification time on this day
Date date = newDate(dueDate);
date.setHours(Preferences.getIntegerFromString(R.string.p_rmd_time, 18));
date.setMinutes(0);
date.setSeconds(0);
Date date = new DateTime(dueDate).withMillisOfDay(Preferences.getInt(R.string.p_rmd_time, 18 * MILLIS_PER_HOUR)).toDate();
dueDateAlarm = date.getTime();
if (dueDate > getNowValue() && dueDateAlarm < getNowValue()) {
// this only happens for tasks due today, cause dueDateAlarm wouldn't be in the past otherwise
// if the default reminder is in the past, then reschedule it
// on this day before start of quiet hours or after quiet hours
// randomly placed in this interval
int quietHoursStart = Preferences.getIntegerFromString(R.string.p_rmd_quietStart, -1);
Date quietHoursStartDate = newDate();
quietHoursStartDate.setHours(quietHoursStart);
quietHoursStartDate.setMinutes(0);
quietHoursStartDate.setSeconds(0);
int quietHoursEnd = Preferences.getIntegerFromString(R.string.p_rmd_quietEnd, -1);
Date quietHoursEndDate = newDate();
quietHoursEndDate.setHours(quietHoursEnd);
quietHoursEndDate.setMinutes(0);
quietHoursEndDate.setSeconds(0);
long quietHoursStart = new DateTime().withMillisOfDay(Preferences.getInt(R.string.p_rmd_quietStart, 22 * MILLIS_PER_HOUR)).getMillis();
Date quietHoursStartDate = newDate(quietHoursStart);
long quietHoursEnd = new DateTime().withMillisOfDay(Preferences.getInt(R.string.p_rmd_quietEnd, 10 * MILLIS_PER_HOUR)).getMillis();
Date quietHoursEndDate = newDate(quietHoursEnd);
boolean quietHoursEnabled = Preferences.getBoolean(R.string.p_rmd_enable_quiet, false);
@ -347,14 +342,14 @@ public final class ReminderService {
int periodDivFactor = 4;
if(quietHoursEnabled) {
int hour = newDate().getHours();
long now = currentTimeMillis();
if(quietHoursStart <= quietHoursEnd) {
if(hour >= quietHoursStart && hour < quietHoursEnd) {
if(now >= quietHoursStart && now < quietHoursEnd) {
// its quiet now, quietHoursEnd is 23 max,
// so put the default reminder to the end of the quiethours
date.setHours(quietHoursEnd);
date = quietHoursEndDate;
dueDateAlarm = date.getTime();
} else if (hour < quietHoursStart) {
} else if (now < quietHoursStart) {
// quietHours didnt start yet
millisToQuiet = quietHoursStartDate.getTime() - getNowValue();
long millisAfterQuiet = dueDate - quietHoursEndDate.getTime();
@ -370,11 +365,11 @@ public final class ReminderService {
dueDateAlarm = getNowValue() + (millisToEndOfDay / periodDivFactor);
}
} else { // wrap across 24/hour boundary
if(hour >= quietHoursStart) {
if(now >= quietHoursStart) {
// do nothing for the end of day, dont let it even vibrate
dueDateAlarm = NO_ALARM;
} else if (hour < quietHoursEnd) {
date.setHours(quietHoursEnd);
} else if (now < quietHoursEnd) {
date = quietHoursEndDate;
dueDateAlarm = date.getTime();
} else {
// quietHours didnt start yet

@ -11,6 +11,7 @@ import com.todoroo.astrid.activity.BeastModePreferences;
import com.todoroo.astrid.core.SortHelper;
import com.todoroo.astrid.data.Task;
import org.joda.time.DateTime;
import org.tasks.R;
public class AstridDefaultPreferenceSpec extends AstridPreferenceSpec {
@ -95,39 +96,24 @@ public class AstridDefaultPreferenceSpec extends AstridPreferenceSpec {
}
static void migrateToNewQuietHours() {
Context context = ContextManager.getContext();
Resources r = context.getResources();
/** START Migration to new Quiet Hours settings */
boolean hasMigrated = Preferences.getBoolean(R.string.p_rmd_hasMigrated, false);
if(!hasMigrated) {
// for each preference load old stored value
int quietHoursStart = Preferences.getIntegerFromString(R.string.p_rmd_quietStart_old, -1);
Preferences.setBoolean(R.string.p_rmd_enable_quiet, quietHoursStart > 0);
int quietHoursEnd = Preferences.getIntegerFromString(R.string.p_rmd_quietEnd_old, -1);
int defReminderTime = Preferences.getIntegerFromString(R.string.p_rmd_time_old, -1);
// if a previous quietHoursStart preference exists and it's not disabled (so it's not 0 or -1)
if (quietHoursStart > 0) {
quietHoursStart = (quietHoursStart + 19) % 24;
if (quietHoursEnd >= 0) {
quietHoursEnd = (quietHoursEnd + 9) % 24;
}
Preferences.setStringFromInteger(R.string.p_rmd_quietStart, quietHoursStart);
Preferences.setStringFromInteger(R.string.p_rmd_quietEnd, quietHoursEnd);
}
// if a previous defReminderTime preference exists
if (defReminderTime >= 0 && defReminderTime < 24) {
// convert to hours from index. 9 is the initial 9AM in the reminder array
// so you have to return 9 hours to get to 0 (and modulo the result to reverse negative results)
defReminderTime = (defReminderTime + 9) % 24;
// save changed preferences in the new preference keys
Preferences.setStringFromInteger(R.string.p_rmd_time, defReminderTime);
}
boolean quietHoursEnabled = Preferences.getIntegerFromString(R.string.p_rmd_quietStart_old, -1) >= 0;
Preferences.setBoolean(R.string.p_rmd_enable_quiet, quietHoursEnabled);
// set migration to completed
if (quietHoursEnabled) {
setTime(R.string.p_rmd_quietStart_old, R.string.p_rmd_quietStart, 22);
setTime(R.string.p_rmd_quietEnd_old, R.string.p_rmd_quietEnd, 10);
}
setTime(R.string.p_rmd_time_old, R.string.p_rmd_time, 18);
Preferences.setBoolean(R.string.p_rmd_hasMigrated, true);
}
/** END Migration to new Quiet Hours settings */
}
private static void setTime(int oldResourceId, int newResourceId, int defValue) {
int hour = Preferences.getIntegerFromString(oldResourceId, defValue);
int millisOfDay = new DateTime().withMillisOfDay(0).withHourOfDay(hour).getMillisOfDay();
Preferences.setInt(newResourceId, millisOfDay);
}
}

@ -105,92 +105,7 @@
<item>730</item>
<item>1460</item>
</string-array>
<string-array name="EPr_quiet_hours_start_values">
<!-- quiet_hours_start_values: 24-hour representation of quiet_hour_start -->
<item></item>
<item>20</item>
<item>21</item>
<item>22</item>
<item>23</item>
<item>00</item>
<item>01</item>
<item>02</item>
<item>03</item>
<item>04</item>
<item>05</item>
<item>06</item>
<item>07</item>
<item>08</item>
<item>09</item>
<item>10</item>
<item>11</item>
<item>12</item>
<item>13</item>
<item>14</item>
<item>15</item>
<item>16</item>
<item>17</item>
<item>18</item>
<item>19</item>
</string-array>
<string-array name="EPr_quiet_hours_end_values">
<!-- quiet_hours_end_values: 24-hour representation of quiet_hours_end -->
<item>09</item>
<item>10</item>
<item>11</item>
<item>12</item>
<item>13</item>
<item>14</item>
<item>15</item>
<item>16</item>
<item>17</item>
<item>18</item>
<item>19</item>
<item>20</item>
<item>21</item>
<item>22</item>
<item>23</item>
<item>00</item>
<item>01</item>
<item>02</item>
<item>03</item>
<item>04</item>
<item>05</item>
<item>06</item>
<item>07</item>
<item>08</item>
</string-array>
<string-array name="EPr_rmd_time_values">
<!-- rmd_time_values: 24-hour representation of rmd_time_values -->
<item>09</item>
<item>10</item>
<item>11</item>
<item>12</item>
<item>13</item>
<item>14</item>
<item>15</item>
<item>16</item>
<item>17</item>
<item>18</item>
<item>19</item>
<item>20</item>
<item>21</item>
<item>22</item>
<item>23</item>
<item>00</item>
<item>01</item>
<item>02</item>
<item>03</item>
<item>04</item>
<item>05</item>
<item>06</item>
<item>07</item>
<item>08</item>
</string-array>
<!-- ======================================================= APPEARANCE == -->
<string name="p_fontSize">font_size</string>

@ -1,5 +1,7 @@
package com.todoroo.astrid.reminders;
import android.annotation.SuppressLint;
import com.todoroo.andlib.utility.Preferences;
import org.joda.time.DateTime;
@ -10,6 +12,8 @@ import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.tasks.R;
import java.util.concurrent.TimeUnit;
import static com.todoroo.astrid.reminders.Notifications.isQuietHours;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@ -20,6 +24,9 @@ import static org.tasks.TestUtilities.clearPreferences;
@RunWith(RobolectricTestRunner.class)
public class NotificationsTest {
@SuppressLint("NewApi")
private static final int MILLIS_PER_HOUR = (int) TimeUnit.HOURS.toMillis(1);
private static final DateTime now =
new DateTime(2014, 1, 23, 18, 8, 31, 540);
@ -85,10 +92,10 @@ public class NotificationsTest {
}
private void setQuietHoursStart(int hour) {
Preferences.setStringFromInteger(R.string.p_rmd_quietStart, hour);
Preferences.setInt(R.string.p_rmd_quietStart, hour * MILLIS_PER_HOUR);
}
private void setQuietHoursEnd(int hour) {
Preferences.setStringFromInteger(R.string.p_rmd_quietEnd, hour);
Preferences.setInt(R.string.p_rmd_quietEnd, hour * MILLIS_PER_HOUR);
}
}

@ -1,5 +1,7 @@
package com.todoroo.astrid.reminders;
import android.annotation.SuppressLint;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.test.TodorooRobolectricTestCase;
import com.todoroo.andlib.utility.Preferences;
@ -14,6 +16,8 @@ import org.robolectric.RobolectricTestRunner;
import org.tasks.Freeze;
import org.tasks.R;
import java.util.concurrent.TimeUnit;
import static com.todoroo.astrid.reminders.ReminderService.NO_ALARM;
import static org.junit.Assert.assertEquals;
import static org.tasks.Freeze.freezeAt;
@ -23,6 +27,9 @@ import static org.tasks.date.DateTimeUtils.newDate;
@RunWith(RobolectricTestRunner.class)
public class NotifyAtDeadlineTest extends TodorooRobolectricTestCase {
@SuppressLint("NewApi")
private static final int MILLIS_PER_HOUR = (int) TimeUnit.HOURS.toMillis(1);
@Autowired
private TaskDao taskDao;
@ -38,6 +45,7 @@ public class NotifyAtDeadlineTest extends TodorooRobolectricTestCase {
super.before();
freezeAt(new DateTime(2014, 1, 24, 17, 23, 37));
service = new ReminderService();
service.setPreferenceDefaults();
}
@After
@ -83,9 +91,9 @@ public class NotifyAtDeadlineTest extends TodorooRobolectricTestCase {
@Test
public void notifyIfLastNotificationWasMoreThanOneDayAgo() {
final DateTime dueDate = new DateTime(2014, 1, 23, 0, 0, 0, 0);
final DateTime dueDate = new DateTime(2014, 1, 24, 0, 0, 0, 0);
Task task = new Task() {{
setDueDate(dueDate.getMillis());
setDueDate(Task.URGENCY_SPECIFIC_DAY, dueDate.getMillis());
setReminderFlags(Task.NOTIFY_AT_DEADLINE);
setReminderLast(new DateTime(2014, 1, 23, 17, 23, 36).getMillis());
}};
@ -98,7 +106,7 @@ public class NotifyAtDeadlineTest extends TodorooRobolectricTestCase {
public void duringQuietHoursSetNotificationAtEnd() {
setQuietHours(0, 10);
Freeze.freezeAt(new DateTime(2014, 1, 27, 9, 13, 37, 501));
Preferences.setStringFromInteger(R.string.p_rmd_time, 8);
Preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
Task task = new Task() {{
setDueDate(Task.URGENCY_SPECIFIC_DAY, newDate(2014, 1, 27).getTime());
setReminderFlags(Task.NOTIFY_AT_DEADLINE);
@ -112,7 +120,7 @@ public class NotifyAtDeadlineTest extends TodorooRobolectricTestCase {
public void afterQuietHoursSetNotificationOnePeriodCloserToDueDate() {
setQuietHours(0, 10);
Freeze.freezeAt(new DateTime(2014, 1, 27, 11, 13, 37, 501));
Preferences.setStringFromInteger(R.string.p_rmd_time, 8);
Preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
Task task = new Task() {{
setDueDate(Task.URGENCY_SPECIFIC_DAY, newDate(2014, 1, 27).getTime());
setReminderFlags(Task.NOTIFY_AT_DEADLINE);
@ -126,13 +134,13 @@ public class NotifyAtDeadlineTest extends TodorooRobolectricTestCase {
public void beforeQuietStartDueDateMoreThanOnePeriodAfterEnd() {
setQuietHours(2, 11);
Freeze.freezeAt(new DateTime(2014, 1, 27, 1, 53, 37, 509));
Preferences.setStringFromInteger(R.string.p_rmd_time, 1);
Preferences.setInt(R.string.p_rmd_time, MILLIS_PER_HOUR);
Task task = new Task() {{
setDueDate(Task.URGENCY_SPECIFIC_DAY, newDate(2014, 1, 27).getTime());
setReminderFlags(Task.NOTIFY_AT_DEADLINE);
}};
assertEquals(
new DateTime(2014, 1, 27, 11, 0, 0, 509).getMillis(),
new DateTime(2014, 1, 27, 11, 0, 0, 0).getMillis(),
service.calculateNextDueDateReminder(task));
}
@ -140,9 +148,9 @@ public class NotifyAtDeadlineTest extends TodorooRobolectricTestCase {
public void beforeQuietStartDueDateLessThanOnePeriodAfterEnd() {
setQuietHours(3, 11);
Freeze.freezeAt(new DateTime(2014, 1, 27, 1, 53, 37, 509));
Preferences.setStringFromInteger(R.string.p_rmd_time, 1);
Preferences.setInt(R.string.p_rmd_time, MILLIS_PER_HOUR);
assertEquals(
new DateTime(2014, 1, 27, 2, 10, 13, 259).getMillis(),
new DateTime(2014, 1, 27, 2, 10, 13, 131).getMillis(),
service.calculateNextDueDateReminder(dueAtNoon));
}
@ -150,7 +158,7 @@ public class NotifyAtDeadlineTest extends TodorooRobolectricTestCase {
public void noAlarmAfterQuietHoursStartWithWrap() {
setQuietHours(10, 1);
Freeze.freezeAt(new DateTime(2014, 1, 27, 10, 0, 0, 0));
Preferences.setStringFromInteger(R.string.p_rmd_time, 8);
Preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
assertEquals(
NO_ALARM,
service.calculateNextDueDateReminder(dueAtNoon));
@ -160,7 +168,7 @@ public class NotifyAtDeadlineTest extends TodorooRobolectricTestCase {
public void setToQuietAlarmEndWithWrap() {
setQuietHours(22, 11);
Freeze.freezeAt(new DateTime(2014, 1, 27, 10, 59, 59, 999));
Preferences.setStringFromInteger(R.string.p_rmd_time, 8);
Preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
assertEquals(
new DateTime(2014, 1, 27, 11, 0, 0, 0).getMillis(),
service.calculateNextDueDateReminder(dueAtNoon));
@ -170,7 +178,7 @@ public class NotifyAtDeadlineTest extends TodorooRobolectricTestCase {
public void setReminderOnePeriodFromNowBeforeQuietHourStartWithWrap() {
setQuietHours(22, 11);
Freeze.freezeAt(new DateTime(2014, 1, 27, 11, 0, 0, 0));
Preferences.setStringFromInteger(R.string.p_rmd_time, 8);
Preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
assertEquals(
// wtf? this is after due date
new DateTime(2014, 1, 27, 13, 45, 0, 0).getMillis(),
@ -180,7 +188,7 @@ public class NotifyAtDeadlineTest extends TodorooRobolectricTestCase {
@Test
public void setReminderOnePeriodFromNowNoQuietHours() {
Freeze.freezeAt(new DateTime(2014, 1, 27, 11, 0, 0, 0));
Preferences.setStringFromInteger(R.string.p_rmd_time, 8);
Preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
assertEquals(
new DateTime(2014, 1, 27, 11, 15, 0, 0).getMillis(),
service.calculateNextDueDateReminder(dueAtNoon));
@ -188,7 +196,7 @@ public class NotifyAtDeadlineTest extends TodorooRobolectricTestCase {
private void setQuietHours(int start, int end) {
Preferences.setBoolean(R.string.p_rmd_enable_quiet, true);
Preferences.setStringFromInteger(R.string.p_rmd_quietStart, start);
Preferences.setStringFromInteger(R.string.p_rmd_quietEnd, end);
Preferences.setInt(R.string.p_rmd_quietStart, start * MILLIS_PER_HOUR);
Preferences.setInt(R.string.p_rmd_quietEnd, end * MILLIS_PER_HOUR);
}
}

@ -1,5 +1,9 @@
package com.todoroo.astrid.utility;
import android.annotation.SuppressLint;
import com.todoroo.andlib.utility.Preferences;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -7,8 +11,10 @@ import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.tasks.R;
import java.util.concurrent.TimeUnit;
import static com.todoroo.andlib.utility.Preferences.getBoolean;
import static com.todoroo.andlib.utility.Preferences.getStringValue;
import static com.todoroo.andlib.utility.Preferences.getInt;
import static com.todoroo.andlib.utility.Preferences.setStringFromInteger;
import static com.todoroo.astrid.utility.AstridDefaultPreferenceSpec.migrateToNewQuietHours;
import static org.junit.Assert.assertEquals;
@ -19,6 +25,9 @@ import static org.tasks.TestUtilities.clearPreferences;
@RunWith(RobolectricTestRunner.class)
public class AstridDefaultPreferenceSpecTest {
@SuppressLint("NewApi")
private static final int MILLIS_PER_HOUR = (int) TimeUnit.HOURS.toMillis(1);
@Before
public void before() {
clearPreferences();
@ -42,7 +51,7 @@ public class AstridDefaultPreferenceSpecTest {
@Test
public void quietHoursDisabledAfterMigration() {
setOldQuietHoursStart(0);
Preferences.setString(R.string.p_rmd_quietStart_old, "");
migrateToNewQuietHours();
@ -55,7 +64,7 @@ public class AstridDefaultPreferenceSpecTest {
migrateToNewQuietHours();
assertEquals("9", newReminderTime());
assertEquals(0, newReminderTime());
}
@Test
@ -64,7 +73,7 @@ public class AstridDefaultPreferenceSpecTest {
migrateToNewQuietHours();
assertEquals("8", newReminderTime());
assertEquals(23 * MILLIS_PER_HOUR, newReminderTime());
}
@Test
@ -73,16 +82,16 @@ public class AstridDefaultPreferenceSpecTest {
migrateToNewQuietHours();
assertEquals("20", newQuietHoursStartTime());
assertEquals(MILLIS_PER_HOUR, newQuietHoursStartTime());
}
@Test
public void migrateFromEndOfQuietHoursStartArray() {
setOldQuietHoursStart(24);
setOldQuietHoursStart(23);
migrateToNewQuietHours();
assertEquals("19", newQuietHoursStartTime());
assertEquals(23 * MILLIS_PER_HOUR, newQuietHoursStartTime());
}
@Test
@ -92,7 +101,7 @@ public class AstridDefaultPreferenceSpecTest {
migrateToNewQuietHours();
assertEquals("9", newQuietHoursEndTime());
assertEquals(0, newQuietHoursEndTime());
}
@Test
@ -102,7 +111,7 @@ public class AstridDefaultPreferenceSpecTest {
migrateToNewQuietHours();
assertEquals("8", newQuietHoursEndTime());
assertEquals(23 * MILLIS_PER_HOUR, newQuietHoursEndTime());
}
private boolean quietHoursEnabled() {
@ -125,15 +134,15 @@ public class AstridDefaultPreferenceSpecTest {
setStringFromInteger(R.string.p_rmd_time_old, index);
}
private String newQuietHoursStartTime() {
return getStringValue(R.string.p_rmd_quietStart);
private int newQuietHoursStartTime() {
return getInt(R.string.p_rmd_quietStart);
}
private String newQuietHoursEndTime() {
return getStringValue(R.string.p_rmd_quietEnd);
private int newQuietHoursEndTime() {
return getInt(R.string.p_rmd_quietEnd);
}
private String newReminderTime() {
return getStringValue(R.string.p_rmd_time);
private int newReminderTime() {
return getInt(R.string.p_rmd_time);
}
}

Loading…
Cancel
Save