IntervalTrigger.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Apache 2.0 License
  3. *
  4. * Copyright (c) Sebastian Katzer 2017
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apache License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://opensource.org/licenses/Apache-2.0/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. */
  21. package de.appplant.cordova.plugin.notification.trigger;
  22. import java.util.Calendar;
  23. import java.util.Date;
  24. /**
  25. * Trigger class for interval based notification. Trigger by a fixed interval
  26. * from now.
  27. */
  28. public class IntervalTrigger extends DateTrigger {
  29. // The number of ticks per interval
  30. private final int ticks;
  31. // The unit of the ticks
  32. final Unit unit;
  33. /**
  34. * Interval trigger based from now.
  35. *
  36. * @param ticks The number of ticks per interval.
  37. * @param unit The unit of the ticks.
  38. */
  39. public IntervalTrigger(int ticks, Unit unit) {
  40. this.ticks = ticks;
  41. this.unit = unit;
  42. }
  43. /**
  44. * Gets the next trigger date.
  45. *
  46. * @param base The date from where to calculate the trigger date.
  47. *
  48. * @return null if there's none next trigger date.
  49. */
  50. @Override
  51. public Date getNextTriggerDate(Date base) {
  52. Calendar cal = getCal(base);
  53. addInterval(cal);
  54. incOccurrence();
  55. return cal.getTime();
  56. }
  57. /**
  58. * Adds the amount of ticks to the calendar.
  59. *
  60. * @param cal The calendar to manipulate.
  61. *
  62. * @return The calendar instance.
  63. */
  64. Calendar addInterval(Calendar cal) {
  65. switch (unit) {
  66. case SECOND:
  67. cal.add(Calendar.SECOND, ticks);
  68. break;
  69. case MINUTE:
  70. cal.add(Calendar.MINUTE, ticks);
  71. break;
  72. case HOUR:
  73. cal.add(Calendar.HOUR_OF_DAY, ticks);
  74. break;
  75. case DAY:
  76. cal.add(Calendar.DAY_OF_YEAR, ticks);
  77. break;
  78. case WEEK:
  79. cal.add(Calendar.WEEK_OF_YEAR, ticks);
  80. break;
  81. case MONTH:
  82. cal.add(Calendar.MONTH, ticks);
  83. break;
  84. case QUARTER:
  85. cal.add(Calendar.MONTH, ticks * 3);
  86. break;
  87. case YEAR:
  88. cal.add(Calendar.YEAR, ticks);
  89. break;
  90. }
  91. return cal;
  92. }
  93. }