mirror of https://github.com/iSoron/uhabits.git
parent
3938ae6fa8
commit
d6dacfd24b
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits;
|
||||
|
||||
import android.os.*;
|
||||
import android.support.test.runner.*;
|
||||
import android.test.suitebuilder.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.ui.*;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.*;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@MediumTest
|
||||
public class HabitLoggerTest extends BaseAndroidTest
|
||||
{
|
||||
@Test
|
||||
public void testLogReminderScheduled() throws IOException
|
||||
{
|
||||
if (!isLogcatAvailable()) return;
|
||||
|
||||
long time = 1422277200000L; // 13:00 jan 26, 2015 (UTC)
|
||||
Habit habit = fixtures.createEmptyHabit();
|
||||
habit.setName("Write journal");
|
||||
|
||||
logger.logReminderScheduled(habit, time);
|
||||
|
||||
String expectedMsg = "Setting alarm (2015-01-26 130000): Wri\n";
|
||||
assertLogcatContains(expectedMsg);
|
||||
}
|
||||
|
||||
protected void assertLogcatContains(String expectedMsg) throws IOException
|
||||
{
|
||||
BaseSystem system = new BaseSystem(targetContext);
|
||||
String logcat = system.getLogcat();
|
||||
assertThat(logcat, containsString(expectedMsg));
|
||||
}
|
||||
|
||||
protected boolean isLogcatAvailable()
|
||||
{
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits;
|
||||
|
||||
import android.support.annotation.*;
|
||||
import android.util.*;
|
||||
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
public class HabitLogger
|
||||
{
|
||||
public void logReminderScheduled(@NonNull Habit habit,
|
||||
@NonNull Long reminderTime)
|
||||
{
|
||||
int min = Math.min(3, habit.getName().length());
|
||||
String name = habit.getName().substring(0, min);
|
||||
|
||||
DateFormat df = DateUtils.getBackupDateFormat();
|
||||
String time = df.format(new Date(reminderTime));
|
||||
|
||||
Log.i("ReminderHelper",
|
||||
String.format("Setting alarm (%s): %s", time, name));
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.intents;
|
||||
|
||||
import android.app.*;
|
||||
import android.content.*;
|
||||
import android.os.*;
|
||||
import android.support.annotation.*;
|
||||
|
||||
import static android.app.AlarmManager.*;
|
||||
import static android.content.Context.*;
|
||||
|
||||
public class IntentScheduler
|
||||
{
|
||||
private final AlarmManager manager;
|
||||
|
||||
public IntentScheduler(@NonNull Context context)
|
||||
{
|
||||
manager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
|
||||
}
|
||||
|
||||
public void schedule(@NonNull Long timestamp, PendingIntent intent)
|
||||
{
|
||||
if (Build.VERSION.SDK_INT >= 23)
|
||||
{
|
||||
manager.setExactAndAllowWhileIdle(RTC_WAKEUP, timestamp, intent);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 19)
|
||||
{
|
||||
manager.setExact(RTC_WAKEUP, timestamp, intent);
|
||||
return;
|
||||
}
|
||||
|
||||
manager.set(RTC_WAKEUP, timestamp, intent);
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.utils;
|
||||
|
||||
import android.app.*;
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.intents.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
import static org.isoron.uhabits.utils.DateUtils.*;
|
||||
|
||||
public class ReminderScheduler
|
||||
{
|
||||
private final IntentFactory intentFactory;
|
||||
|
||||
private final IntentScheduler intentScheduler;
|
||||
|
||||
@Inject
|
||||
HabitLogger logger;
|
||||
|
||||
public ReminderScheduler(IntentFactory intentFactory,
|
||||
IntentScheduler intentScheduler)
|
||||
{
|
||||
this.intentFactory = intentFactory;
|
||||
this.intentScheduler = intentScheduler;
|
||||
HabitsApplication.getComponent().inject(this);
|
||||
}
|
||||
|
||||
public void schedule(@NonNull Habit habit, @Nullable Long reminderTime)
|
||||
{
|
||||
if (!habit.hasReminder()) return;
|
||||
Reminder reminder = habit.getReminder();
|
||||
if (reminderTime == null) reminderTime = getReminderTime(reminder);
|
||||
long timestamp = getStartOfDay(toLocalTime(reminderTime));
|
||||
|
||||
PendingIntent intent = intentFactory.buildShowReminder(habit,
|
||||
reminderTime, timestamp);
|
||||
intentScheduler.schedule(reminderTime, intent);
|
||||
logger.logReminderScheduled(habit, reminderTime);
|
||||
}
|
||||
|
||||
public void schedule(@NonNull HabitList habits)
|
||||
{
|
||||
HabitList reminderHabits = habits.getFiltered(HabitMatcher.WITH_ALARM);
|
||||
for (Habit habit : reminderHabits)
|
||||
schedule(habit, null);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Long getReminderTime(@NonNull Reminder reminder)
|
||||
{
|
||||
Calendar calendar = DateUtils.getStartOfTodayCalendar();
|
||||
calendar.set(Calendar.HOUR_OF_DAY, reminder.getHour());
|
||||
calendar.set(Calendar.MINUTE, reminder.getMinute());
|
||||
calendar.set(Calendar.SECOND, 0);
|
||||
Long time = calendar.getTimeInMillis();
|
||||
|
||||
if (DateUtils.getLocalTime() > time)
|
||||
time += AlarmManager.INTERVAL_DAY;
|
||||
|
||||
return time;
|
||||
}
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.utils;
|
||||
|
||||
import android.app.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.intents.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.junit.*;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@SuppressWarnings("JavaDoc")
|
||||
public class ReminderSchedulerTest extends BaseUnitTest
|
||||
{
|
||||
private IntentFactory intentFactory;
|
||||
|
||||
private IntentScheduler intentScheduler;
|
||||
|
||||
private ReminderScheduler scheduler;
|
||||
|
||||
private Habit habit;
|
||||
|
||||
private PendingIntent intent;
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void setUp()
|
||||
{
|
||||
super.setUp();
|
||||
intentFactory = mock(IntentFactory.class);
|
||||
intentScheduler = mock(IntentScheduler.class);
|
||||
intent = mock(PendingIntent.class);
|
||||
|
||||
scheduler = new ReminderScheduler(intentFactory, intentScheduler);
|
||||
habit = fixtures.createEmptyHabit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSchedule_atSpecificTime()
|
||||
{
|
||||
long atTime = 1422617400000L; // 11:30 jan 30, 2015 (UTC)
|
||||
long expectedCheckmarkTime = 1422576000000L; // 00:00 jan 27, 2015 (UTC)
|
||||
|
||||
habit.setReminder(new Reminder(8, 30, DateUtils.ALL_WEEK_DAYS));
|
||||
scheduleAndVerify(atTime, expectedCheckmarkTime, atTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSchedule_laterToday()
|
||||
{
|
||||
long now = 1422253800000L; // 06:30 jan 26, 2015 (UTC)
|
||||
DateUtils.setFixedLocalTime(now);
|
||||
|
||||
long expectedCheckmarkTime = 1422230400000L; // 00:00 jan 26, 2015 (UTC)
|
||||
long expectedReminderTime = 1422261000000L; // 08:30 jan 26, 2015 (UTC)
|
||||
|
||||
habit.setReminder(new Reminder(8, 30, DateUtils.ALL_WEEK_DAYS));
|
||||
|
||||
scheduleAndVerify(null, expectedCheckmarkTime, expectedReminderTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSchedule_list()
|
||||
{
|
||||
long now = 1422277200000L; // 13:00 jan 26, 2015 (UTC)
|
||||
DateUtils.setFixedLocalTime(now);
|
||||
|
||||
fixtures.purgeHabits();
|
||||
|
||||
Habit h1 = fixtures.createEmptyHabit();
|
||||
h1.setReminder(new Reminder(8, 30, DateUtils.ALL_WEEK_DAYS));
|
||||
|
||||
Habit h2 = fixtures.createEmptyHabit();
|
||||
h2.setReminder(new Reminder(18, 30, DateUtils.ALL_WEEK_DAYS));
|
||||
|
||||
fixtures.createEmptyHabit();
|
||||
|
||||
scheduler.schedule(habitList);
|
||||
|
||||
verify(intentScheduler).schedule(1422347400000L, null);
|
||||
verify(intentScheduler).schedule(1422297000000L, null);
|
||||
verifyNoMoreInteractions(intentScheduler);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSchedule_tomorrow()
|
||||
{
|
||||
long now = 1453813200000L; // 13:00 jan 26, 2016 (UTC)
|
||||
DateUtils.setFixedLocalTime(now);
|
||||
|
||||
long expectedCheckmarkTime = 1453852800000L; // 00:00 jan 27, 2016 (UTC)
|
||||
long expectedReminderTime = 1453883400000L; // 08:30 jan 27, 2016 (UTC)
|
||||
|
||||
habit.setReminder(new Reminder(8, 30, DateUtils.ALL_WEEK_DAYS));
|
||||
scheduleAndVerify(null, expectedCheckmarkTime, expectedReminderTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSchedule_withoutReminder()
|
||||
{
|
||||
scheduler.schedule(habit, null);
|
||||
verifyZeroInteractions(intentScheduler);
|
||||
}
|
||||
|
||||
private void scheduleAndVerify(Long atTime,
|
||||
long expectedCheckmarkTime,
|
||||
long expectedReminderTime)
|
||||
{
|
||||
when(intentFactory.buildShowReminder(habit, expectedReminderTime,
|
||||
expectedCheckmarkTime)).thenReturn(intent);
|
||||
|
||||
scheduler.schedule(habit, atTime);
|
||||
|
||||
verify(logger).logReminderScheduled(habit, expectedReminderTime);
|
||||
|
||||
verify(intentFactory).buildShowReminder(habit, expectedReminderTime,
|
||||
expectedCheckmarkTime);
|
||||
verify(intentScheduler).schedule(expectedReminderTime, intent);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue