Request.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2014-2015 by appPlant UG. All rights reserved.
  3. *
  4. * @APPPLANT_LICENSE_HEADER_START@
  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. * @APPPLANT_LICENSE_HEADER_END@
  22. */
  23. package de.appplant.cordova.plugin.notification;
  24. import org.json.JSONObject;
  25. import java.util.Arrays;
  26. import java.util.Calendar;
  27. import java.util.Date;
  28. import java.util.List;
  29. import de.appplant.cordova.plugin.notification.trigger.MatchTrigger;
  30. import de.appplant.cordova.plugin.notification.trigger.DateTrigger;
  31. import de.appplant.cordova.plugin.notification.trigger.IntervalTrigger;
  32. import static de.appplant.cordova.plugin.notification.trigger.IntervalTrigger.Unit;
  33. public class Request {
  34. // Key name for bundled extras
  35. static final String EXTRA = "NOTIFICATION_OCCURRENCE_EXTRA";
  36. // The options spec
  37. private final Options options;
  38. // The right trigger for the options
  39. private final DateTrigger trigger;
  40. // How often the trigger shall occur
  41. private final int count;
  42. // The trigger spec
  43. private final JSONObject spec;
  44. // The current trigger date
  45. private Date triggerDate;
  46. /**
  47. * Constructor
  48. *
  49. * @param options The options spec.
  50. */
  51. public Request(Options options) {
  52. this.options = options;
  53. this.spec = options.getTrigger();
  54. this.count = spec.optInt("count", 1);
  55. this.trigger = buildTrigger();
  56. this.triggerDate = trigger.getNextTriggerDate(getBaseDate());
  57. }
  58. /**
  59. * Gets the options spec.
  60. */
  61. public Options getOptions() {
  62. return options;
  63. }
  64. /**
  65. * The identifier for the request.
  66. *
  67. * @return The notification ID as the string
  68. */
  69. public String getIdentifier() {
  70. return options.getId().toString() + "-" + getOccurrence();
  71. }
  72. /**
  73. * The value of the internal occurrence counter.
  74. */
  75. int getOccurrence() {
  76. return trigger.getOccurrence();
  77. }
  78. /**
  79. * If there's one more trigger date to calculate.
  80. */
  81. private boolean hasNext() {
  82. return triggerDate != null && getOccurrence() < count;
  83. }
  84. /**
  85. * Moves the internal occurrence counter by one.
  86. */
  87. boolean moveNext() {
  88. if (hasNext()) {
  89. triggerDate = getNextTriggerDate();
  90. } else {
  91. triggerDate = null;
  92. }
  93. return this.triggerDate != null;
  94. }
  95. /**
  96. * Gets the current trigger date.
  97. *
  98. * @return null if there's no trigger date.
  99. */
  100. Date getTriggerDate() {
  101. Calendar now = Calendar.getInstance();
  102. if (triggerDate == null)
  103. return null;
  104. if ((now.getTimeInMillis() - triggerDate.getTime()) > 60000) {
  105. return null;
  106. }
  107. return triggerDate;
  108. }
  109. /**
  110. * Gets the next trigger date based on the current trigger date.
  111. */
  112. private Date getNextTriggerDate() {
  113. return trigger.getNextTriggerDate(triggerDate);
  114. }
  115. /**
  116. * Build the trigger specified in options.
  117. */
  118. private DateTrigger buildTrigger() {
  119. Object every = spec.opt("every");
  120. if (every instanceof JSONObject) {
  121. return new MatchTrigger(getDateMatchingComponents());
  122. }
  123. Unit unit = getUnit();
  124. int ticks = getTicks();
  125. return new IntervalTrigger(ticks, unit);
  126. }
  127. /**
  128. * Gets the unit value.
  129. */
  130. private Unit getUnit() {
  131. Object every = spec.opt("every");
  132. String unit = "SECOND";
  133. if (spec.has("unit")) {
  134. unit = spec.optString("unit", "second");
  135. } else
  136. if (every instanceof String) {
  137. unit = spec.optString("every", "second");
  138. }
  139. return Unit.valueOf(unit.toUpperCase());
  140. }
  141. /**
  142. * Gets the tick value.
  143. */
  144. private int getTicks() {
  145. Object every = spec.opt("every");
  146. int ticks = 0;
  147. if (spec.has("at")) {
  148. ticks = 0;
  149. } else
  150. if (spec.has("in")) {
  151. ticks = spec.optInt("in", 0);
  152. } else
  153. if (every instanceof String) {
  154. ticks = 1;
  155. } else
  156. if (!(every instanceof JSONObject)) {
  157. ticks = spec.optInt("every", 0);
  158. }
  159. return ticks;
  160. }
  161. /**
  162. * Gets an array of all date parts to construct a datetime instance.
  163. *
  164. * @return [min, hour, day, month, year]
  165. */
  166. private List<Integer> getDateMatchingComponents() {
  167. JSONObject every = spec.optJSONObject("every");
  168. return Arrays.asList(
  169. (Integer) every.opt("minute"),
  170. (Integer) every.opt("hour"),
  171. (Integer) every.opt("day"),
  172. (Integer) every.opt("month"),
  173. (Integer) every.opt("year")
  174. );
  175. }
  176. /**
  177. * Gets the base date from where to calculate the next trigger date.
  178. */
  179. private Date getBaseDate() {
  180. if (spec.has("at")) {
  181. return new Date(spec.optLong("at", 0) * 1000);
  182. } else {
  183. return new Date();
  184. }
  185. }
  186. }