private Calendar getTriggerTimeForDay(int trigId, TimeTrigDesc trigDesc, int dayOffset) { Calendar target = Calendar.getInstance(); target.add(Calendar.DAY_OF_YEAR, dayOffset); String dayStr = TimeTrigDesc.getDayOfWeekString(target.get(Calendar.DAY_OF_WEEK)); if (!trigDesc.doesRepeatOnDay(dayStr)) { return null; } if (dayOffset == 0) { return getTriggerTimeForToday(trigId, trigDesc); } target.set(Calendar.SECOND, 0); if (!trigDesc.isRandomized()) { target.set(Calendar.HOUR_OF_DAY, trigDesc.getTriggerTime().getHour()); target.set(Calendar.MINUTE, trigDesc.getTriggerTime().getMinute()); } else { target.set(Calendar.HOUR_OF_DAY, trigDesc.getRangeStart().getHour()); target.set(Calendar.MINUTE, trigDesc.getRangeStart().getMinute()); int diff = trigDesc.getRangeStart().differenceInMinutes(trigDesc.getRangeEnd()); Random rand = new Random(); // Generate a random number (both ranges inclusive) target.add(Calendar.MINUTE, rand.nextInt(diff + 1)); } return target; }
private Calendar getTriggerTimeForToday(int trigId, TimeTrigDesc trigDesc) { TimeTrigger timeTrig = new TimeTrigger(); if (timeTrig.hasTriggeredToday(this, trigId)) { return null; } Calendar now = Calendar.getInstance(); Calendar target = Calendar.getInstance(); target.set(Calendar.SECOND, 0); if (!trigDesc.isRandomized()) { target.set(Calendar.HOUR_OF_DAY, trigDesc.getTriggerTime().getHour()); target.set(Calendar.MINUTE, trigDesc.getTriggerTime().getMinute()); if (now.before(target)) { return target; } } else { // if randomized, check if there is any more time left in the interval SimpleTime tCurr = new SimpleTime(); SimpleTime tStart = trigDesc.getRangeStart(); SimpleTime tEnd = trigDesc.getRangeEnd(); if (tCurr.isBefore(tEnd)) { int diff; if (tCurr.isAfter(tStart)) { diff = tCurr.differenceInMinutes(tEnd); target.set(Calendar.HOUR_OF_DAY, tCurr.getHour()); target.set(Calendar.MINUTE, tCurr.getMinute()); } else { diff = tStart.differenceInMinutes(tEnd); target.set(Calendar.HOUR_OF_DAY, tStart.getHour()); target.set(Calendar.MINUTE, tStart.getMinute()); } Random rand = new Random(); // Generate a random number (both ranges inclusive) target.add(Calendar.MINUTE, rand.nextInt(diff + 1)); return target; } } return null; }