@Test public void test14_2() { mRecord.setRepeatType(Record.REPEAT_TYPE_DAYS_IN_WEEK); mRecord.setDayOfWeek(mTodayOfWeek, false); mRecord.setDayOfWeek(Calendar.FRIDAY, true); mRecord.setHour(12); mRecord.setMinute(15); long sendingInMillis = SendMessageHelper.getNextSendingInMillis(mRecord, mTodayTimeMillis); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, 2016); calendar.set(Calendar.MONTH, Calendar.MAY); calendar.set(Calendar.DAY_OF_MONTH, 13); calendar.set(Calendar.HOUR_OF_DAY, 12); calendar.set(Calendar.MINUTE, 15); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Assert.assertEquals(new Date(calendar.getTimeInMillis()), new Date(sendingInMillis)); }
static long getNextSendingInMillis(Record record, long currentTimeMillis) { int delta = Const.MILLIS_PER_MINUTE; int repeatType = record.getRepeatType(); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, record.getHour()); calendar.set(Calendar.MINUTE, record.getMinute()); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); if (repeatType == Record.REPEAT_TYPE_HOURS_IN_DAY || repeatType == Record.REPEAT_TYPE_DAYS_IN_WEEK) { long timeInMillis = calendar.getTimeInMillis(); int interval = (repeatType == Record.REPEAT_TYPE_HOURS_IN_DAY) ? (record.getPeriod() * Const.MILLIS_PER_HOUR) : Const.MILLIS_PER_DAY; timeInMillis -= delta; while (timeInMillis < currentTimeMillis) { timeInMillis += interval; } while (timeInMillis - interval > currentTimeMillis) { timeInMillis -= interval; } calendar.setTimeInMillis(timeInMillis + delta); if (repeatType == Record.REPEAT_TYPE_DAYS_IN_WEEK) { while (!record.isDayOfWeekEnabled(calendar.get(Calendar.DAY_OF_WEEK))) { calendar.setTimeInMillis(calendar.getTimeInMillis() + Const.MILLIS_PER_DAY); } } } else if (repeatType == Record.REPEAT_TYPE_DAY_IN_YEAR) { calendar.set(Calendar.MONTH, record.getMonth()); calendar.set(Calendar.DAY_OF_MONTH, record.getDayOfMonth()); calendar.setTimeInMillis(calendar.getTimeInMillis() - delta); while (calendar.getTimeInMillis() < currentTimeMillis) { calendar.roll(Calendar.YEAR, true); } Calendar prevYearCalendar = Calendar.getInstance(); prevYearCalendar.setTimeInMillis(calendar.getTimeInMillis()); prevYearCalendar.roll(Calendar.YEAR, false); while (prevYearCalendar.getTimeInMillis() > currentTimeMillis) { calendar.roll(Calendar.YEAR, false); prevYearCalendar.roll(Calendar.YEAR, false); } calendar.setTimeInMillis(calendar.getTimeInMillis() + delta); } return calendar.getTimeInMillis(); }
SendMessageHelper { static long getNextSendingInMillis(Record record, long currentTimeMillis) { int delta = Const.MILLIS_PER_MINUTE; int repeatType = record.getRepeatType(); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, record.getHour()); calendar.set(Calendar.MINUTE, record.getMinute()); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); if (repeatType == Record.REPEAT_TYPE_HOURS_IN_DAY || repeatType == Record.REPEAT_TYPE_DAYS_IN_WEEK) { long timeInMillis = calendar.getTimeInMillis(); int interval = (repeatType == Record.REPEAT_TYPE_HOURS_IN_DAY) ? (record.getPeriod() * Const.MILLIS_PER_HOUR) : Const.MILLIS_PER_DAY; timeInMillis -= delta; while (timeInMillis < currentTimeMillis) { timeInMillis += interval; } while (timeInMillis - interval > currentTimeMillis) { timeInMillis -= interval; } calendar.setTimeInMillis(timeInMillis + delta); if (repeatType == Record.REPEAT_TYPE_DAYS_IN_WEEK) { while (!record.isDayOfWeekEnabled(calendar.get(Calendar.DAY_OF_WEEK))) { calendar.setTimeInMillis(calendar.getTimeInMillis() + Const.MILLIS_PER_DAY); } } } else if (repeatType == Record.REPEAT_TYPE_DAY_IN_YEAR) { calendar.set(Calendar.MONTH, record.getMonth()); calendar.set(Calendar.DAY_OF_MONTH, record.getDayOfMonth()); calendar.setTimeInMillis(calendar.getTimeInMillis() - delta); while (calendar.getTimeInMillis() < currentTimeMillis) { calendar.roll(Calendar.YEAR, true); } Calendar prevYearCalendar = Calendar.getInstance(); prevYearCalendar.setTimeInMillis(calendar.getTimeInMillis()); prevYearCalendar.roll(Calendar.YEAR, false); while (prevYearCalendar.getTimeInMillis() > currentTimeMillis) { calendar.roll(Calendar.YEAR, false); prevYearCalendar.roll(Calendar.YEAR, false); } calendar.setTimeInMillis(calendar.getTimeInMillis() + delta); } return calendar.getTimeInMillis(); } }
SendMessageHelper { static long getNextSendingInMillis(Record record, long currentTimeMillis) { int delta = Const.MILLIS_PER_MINUTE; int repeatType = record.getRepeatType(); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, record.getHour()); calendar.set(Calendar.MINUTE, record.getMinute()); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); if (repeatType == Record.REPEAT_TYPE_HOURS_IN_DAY || repeatType == Record.REPEAT_TYPE_DAYS_IN_WEEK) { long timeInMillis = calendar.getTimeInMillis(); int interval = (repeatType == Record.REPEAT_TYPE_HOURS_IN_DAY) ? (record.getPeriod() * Const.MILLIS_PER_HOUR) : Const.MILLIS_PER_DAY; timeInMillis -= delta; while (timeInMillis < currentTimeMillis) { timeInMillis += interval; } while (timeInMillis - interval > currentTimeMillis) { timeInMillis -= interval; } calendar.setTimeInMillis(timeInMillis + delta); if (repeatType == Record.REPEAT_TYPE_DAYS_IN_WEEK) { while (!record.isDayOfWeekEnabled(calendar.get(Calendar.DAY_OF_WEEK))) { calendar.setTimeInMillis(calendar.getTimeInMillis() + Const.MILLIS_PER_DAY); } } } else if (repeatType == Record.REPEAT_TYPE_DAY_IN_YEAR) { calendar.set(Calendar.MONTH, record.getMonth()); calendar.set(Calendar.DAY_OF_MONTH, record.getDayOfMonth()); calendar.setTimeInMillis(calendar.getTimeInMillis() - delta); while (calendar.getTimeInMillis() < currentTimeMillis) { calendar.roll(Calendar.YEAR, true); } Calendar prevYearCalendar = Calendar.getInstance(); prevYearCalendar.setTimeInMillis(calendar.getTimeInMillis()); prevYearCalendar.roll(Calendar.YEAR, false); while (prevYearCalendar.getTimeInMillis() > currentTimeMillis) { calendar.roll(Calendar.YEAR, false); prevYearCalendar.roll(Calendar.YEAR, false); } calendar.setTimeInMillis(calendar.getTimeInMillis() + delta); } return calendar.getTimeInMillis(); } SendMessageHelper(); }
SendMessageHelper { static long getNextSendingInMillis(Record record, long currentTimeMillis) { int delta = Const.MILLIS_PER_MINUTE; int repeatType = record.getRepeatType(); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, record.getHour()); calendar.set(Calendar.MINUTE, record.getMinute()); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); if (repeatType == Record.REPEAT_TYPE_HOURS_IN_DAY || repeatType == Record.REPEAT_TYPE_DAYS_IN_WEEK) { long timeInMillis = calendar.getTimeInMillis(); int interval = (repeatType == Record.REPEAT_TYPE_HOURS_IN_DAY) ? (record.getPeriod() * Const.MILLIS_PER_HOUR) : Const.MILLIS_PER_DAY; timeInMillis -= delta; while (timeInMillis < currentTimeMillis) { timeInMillis += interval; } while (timeInMillis - interval > currentTimeMillis) { timeInMillis -= interval; } calendar.setTimeInMillis(timeInMillis + delta); if (repeatType == Record.REPEAT_TYPE_DAYS_IN_WEEK) { while (!record.isDayOfWeekEnabled(calendar.get(Calendar.DAY_OF_WEEK))) { calendar.setTimeInMillis(calendar.getTimeInMillis() + Const.MILLIS_PER_DAY); } } } else if (repeatType == Record.REPEAT_TYPE_DAY_IN_YEAR) { calendar.set(Calendar.MONTH, record.getMonth()); calendar.set(Calendar.DAY_OF_MONTH, record.getDayOfMonth()); calendar.setTimeInMillis(calendar.getTimeInMillis() - delta); while (calendar.getTimeInMillis() < currentTimeMillis) { calendar.roll(Calendar.YEAR, true); } Calendar prevYearCalendar = Calendar.getInstance(); prevYearCalendar.setTimeInMillis(calendar.getTimeInMillis()); prevYearCalendar.roll(Calendar.YEAR, false); while (prevYearCalendar.getTimeInMillis() > currentTimeMillis) { calendar.roll(Calendar.YEAR, false); prevYearCalendar.roll(Calendar.YEAR, false); } calendar.setTimeInMillis(calendar.getTimeInMillis() + delta); } return calendar.getTimeInMillis(); } SendMessageHelper(); void onRecordChanged(Record record); void onRecordRemoved(int recordId); }
SendMessageHelper { static long getNextSendingInMillis(Record record, long currentTimeMillis) { int delta = Const.MILLIS_PER_MINUTE; int repeatType = record.getRepeatType(); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, record.getHour()); calendar.set(Calendar.MINUTE, record.getMinute()); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); if (repeatType == Record.REPEAT_TYPE_HOURS_IN_DAY || repeatType == Record.REPEAT_TYPE_DAYS_IN_WEEK) { long timeInMillis = calendar.getTimeInMillis(); int interval = (repeatType == Record.REPEAT_TYPE_HOURS_IN_DAY) ? (record.getPeriod() * Const.MILLIS_PER_HOUR) : Const.MILLIS_PER_DAY; timeInMillis -= delta; while (timeInMillis < currentTimeMillis) { timeInMillis += interval; } while (timeInMillis - interval > currentTimeMillis) { timeInMillis -= interval; } calendar.setTimeInMillis(timeInMillis + delta); if (repeatType == Record.REPEAT_TYPE_DAYS_IN_WEEK) { while (!record.isDayOfWeekEnabled(calendar.get(Calendar.DAY_OF_WEEK))) { calendar.setTimeInMillis(calendar.getTimeInMillis() + Const.MILLIS_PER_DAY); } } } else if (repeatType == Record.REPEAT_TYPE_DAY_IN_YEAR) { calendar.set(Calendar.MONTH, record.getMonth()); calendar.set(Calendar.DAY_OF_MONTH, record.getDayOfMonth()); calendar.setTimeInMillis(calendar.getTimeInMillis() - delta); while (calendar.getTimeInMillis() < currentTimeMillis) { calendar.roll(Calendar.YEAR, true); } Calendar prevYearCalendar = Calendar.getInstance(); prevYearCalendar.setTimeInMillis(calendar.getTimeInMillis()); prevYearCalendar.roll(Calendar.YEAR, false); while (prevYearCalendar.getTimeInMillis() > currentTimeMillis) { calendar.roll(Calendar.YEAR, false); prevYearCalendar.roll(Calendar.YEAR, false); } calendar.setTimeInMillis(calendar.getTimeInMillis() + delta); } return calendar.getTimeInMillis(); } SendMessageHelper(); void onRecordChanged(Record record); void onRecordRemoved(int recordId);  }