cordova-plugin-local-notifications-continued 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/.github/FUNDING.yml +1 -0
  2. package/.github/ISSUE_TEMPLATE/report_issue.yml +126 -0
  3. package/CHANGELOG.md +358 -0
  4. package/LICENSE +202 -0
  5. package/README.md +1531 -0
  6. package/images/android-actions-with-input-clicked.png +0 -0
  7. package/images/android-actions-with-input-not-clicked.png +0 -0
  8. package/images/android-actions.png +0 -0
  9. package/images/android-alarms-and-reminders-in-app-settings.png +0 -0
  10. package/images/android-alarms-and-reminders-setting.png +0 -0
  11. package/images/android-app-hibernation-notification.png +0 -0
  12. package/images/android-app-hibernation-settings-android-12.png +0 -0
  13. package/images/android-app-hibernation-settings-android-13-14.png +0 -0
  14. package/images/android-app-hibernation-settings-android-15.png +0 -0
  15. package/images/android-attachments-image-folded.png +0 -0
  16. package/images/android-attachments-image-unfolded.png +0 -0
  17. package/images/android-chat.png +0 -0
  18. package/images/android-icon.svg +19 -0
  19. package/images/android-inbox.png +0 -0
  20. package/images/android-notification-example.png +0 -0
  21. package/images/android-progress.png +0 -0
  22. package/images/android-request-permission.png +0 -0
  23. package/images/android-stack.png +0 -0
  24. package/images/apple-icon.svg +1 -0
  25. package/images/cordova-app-notified-logo.png +0 -0
  26. package/images/ios-actions-with-input.png +0 -0
  27. package/images/ios-actions.png +0 -0
  28. package/images/ios-attachments-image-folded.png +0 -0
  29. package/images/ios-attachments-image-unfolded.png +0 -0
  30. package/images/ios-notification.png +0 -0
  31. package/images/ios-request-permission.png +0 -0
  32. package/images/logo.png +0 -0
  33. package/package.json +53 -0
  34. package/plugin.xml +266 -0
  35. package/src/android/ClickActivity.java +72 -0
  36. package/src/android/LocalNotification.java +779 -0
  37. package/src/android/Manager.java +327 -0
  38. package/src/android/Notification.java +844 -0
  39. package/src/android/Options.java +956 -0
  40. package/src/android/OptionsTrigger.java +104 -0
  41. package/src/android/action/Action.java +214 -0
  42. package/src/android/action/ActionGroup.java +135 -0
  43. package/src/android/build/localnotification.gradle +29 -0
  44. package/src/android/receiver/ClearReceiver.java +61 -0
  45. package/src/android/receiver/RestoreReceiver.java +70 -0
  46. package/src/android/receiver/TriggerReceiver.java +70 -0
  47. package/src/android/trigger/TriggerHandler.java +195 -0
  48. package/src/android/trigger/TriggerHandlerAt.java +63 -0
  49. package/src/android/trigger/TriggerHandlerEvery.java +257 -0
  50. package/src/android/trigger/TriggerHandlerIn.java +69 -0
  51. package/src/android/util/AssetUtil.java +385 -0
  52. package/src/android/util/CallbackContextUtil.java +88 -0
  53. package/src/android/util/PluginFileProvider.java +34 -0
  54. package/src/android/xml/shared_files_provider_paths.xml +38 -0
  55. package/src/ios/APPLocalNotification.h +56 -0
  56. package/src/ios/APPLocalNotification.m +650 -0
  57. package/src/ios/APPNotificationCategory.h +28 -0
  58. package/src/ios/APPNotificationCategory.m +107 -0
  59. package/src/ios/APPNotificationContent.h +32 -0
  60. package/src/ios/APPNotificationContent.m +122 -0
  61. package/src/ios/APPNotificationOptions.h +42 -0
  62. package/src/ios/APPNotificationOptions.m +689 -0
  63. package/src/ios/UNNotificationRequest+APPLocalNotification.h +32 -0
  64. package/src/ios/UNNotificationRequest+APPLocalNotification.m +100 -0
  65. package/src/ios/UNUserNotificationCenter+APPLocalNotification.h +60 -0
  66. package/src/ios/UNUserNotificationCenter+APPLocalNotification.m +344 -0
  67. package/www/local-notification.js +1104 -0
@@ -0,0 +1,195 @@
1
+ /*
2
+ * Apache 2.0 License
3
+ *
4
+ * Copyright (c) Sebastian Katzer 2017
5
+ * Copyright (c) Manuel Beck 2024
6
+ *
7
+ * This file contains Original Code and/or Modifications of Original Code
8
+ * as defined in and that are subject to the Apache License
9
+ * Version 2.0 (the 'License'). You may not use this file except in
10
+ * compliance with the License. Please obtain a copy of the License at
11
+ * http://opensource.org/licenses/Apache-2.0/ and read it before using this
12
+ * file.
13
+ *
14
+ * The Original Code and all software distributed under the License are
15
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19
+ * Please see the License for the specific language governing rights and
20
+ * limitations under the License.
21
+ */
22
+
23
+ package de.appplant.cordova.plugin.localnotification.trigger;
24
+
25
+ import android.util.Log;
26
+ import java.util.Calendar;
27
+ import java.util.Date;
28
+
29
+ import org.json.JSONObject;
30
+
31
+ import de.appplant.cordova.plugin.localnotification.Options;
32
+ import de.appplant.cordova.plugin.localnotification.OptionsTrigger;
33
+
34
+ public abstract class TriggerHandler {
35
+
36
+ public static final String TAG = "TriggerHandler";
37
+
38
+ Options options;
39
+
40
+ /** Helper for trigger property */
41
+ OptionsTrigger optionsTrigger;
42
+
43
+ int occurrence = 0;
44
+
45
+ /**
46
+ * The base date from where to calculate the next trigger
47
+ */
48
+ Date baseDate;
49
+
50
+ /**
51
+ * trigger date calculated by {@link #getNextTriggerDate()}
52
+ */
53
+ Date triggerDate;
54
+
55
+ /**
56
+ * @param options Notification options
57
+ */
58
+ public TriggerHandler(Options options) {
59
+ this.options = options;
60
+ this.optionsTrigger = options.getOptionsTrigger();
61
+ // Set the base date from where to calculate the next trigger
62
+ // This can be set by config or is set to the current date
63
+ this.baseDate = new Date();
64
+ }
65
+
66
+ public abstract boolean isLastOccurrence();
67
+
68
+ /**
69
+ * Calculates the next trigger. Can return null if there's no next trigger.
70
+ * @param baseCalendar The base calendar from where to calculate the next trigger.
71
+ */
72
+ public abstract Date calculateNextTrigger(Calendar baseCalendar);
73
+
74
+ /**
75
+ * Gets the next trigger date.
76
+ * @param base The date from where to calculate the trigger date.
77
+ * @return null if there's none next trigger date.
78
+ */
79
+ public Date getNextTriggerDate() {
80
+ // Use last trigger date as base date for calculating the next trigger
81
+ if (triggerDate != null) baseDate = triggerDate;
82
+
83
+ // Clear the last trigger date, so it's reflecting the current status of this date trigger
84
+ triggerDate = null;
85
+
86
+ Log.d(TAG, "Get next trigger date" +
87
+ ", baseDate=" + baseDate +
88
+ ", triggerOptions=" + optionsTrigger.toString() +
89
+ ", occurrence=" + occurrence);
90
+
91
+ // All occurrences have been run through
92
+ if (isLastOccurrence()) return null;
93
+
94
+ Date nextTriggerDate = calculateNextTrigger(dateToCalendar(baseDate));
95
+
96
+ Log.d(TAG, "Next trigger date: " + nextTriggerDate + ", notificationId=" + options.getId());
97
+
98
+ if (nextTriggerDate == null) return null;
99
+
100
+ // Count occurrence
101
+ occurrence++;
102
+
103
+ // Remember trigger date
104
+ triggerDate = nextTriggerDate;
105
+
106
+ return nextTriggerDate;
107
+ }
108
+
109
+ /**
110
+ * Restores the state of the trigger, when the notification is loaded from the SharedPreferences
111
+ * @param occurrence
112
+ * @param baseDate
113
+ * @param triggerDate
114
+ */
115
+ public void restoreState(int occurrence, Date baseDate, Date triggerDate) {
116
+ this.occurrence = occurrence;
117
+ this.baseDate = baseDate;
118
+ this.triggerDate = triggerDate;
119
+ }
120
+
121
+ public Date getTriggerDate() {
122
+ return triggerDate;
123
+ }
124
+
125
+ public Date getBaseDate() {
126
+ return baseDate;
127
+ }
128
+
129
+ /**
130
+ * The value of the occurrence.
131
+ */
132
+ public int getOccurrence() {
133
+ return occurrence;
134
+ }
135
+
136
+ /**
137
+ * Converts a {@link Date} to {@link Calendar}.
138
+ */
139
+ Calendar dateToCalendar(Date date) {
140
+ Calendar calendar = Calendar.getInstance();
141
+ calendar.setTime(date);
142
+ return calendar;
143
+ }
144
+
145
+ /**
146
+ * Checks if the trigger date is within the trigger before option, if present
147
+ */
148
+ public boolean isWithinTriggerbefore(Calendar calendar) {
149
+ // Return true, if there is no trigger before option, otherwise compare against it
150
+ return !optionsTrigger.has("before") || calendar.getTimeInMillis() < optionsTrigger.getBefore();
151
+ }
152
+
153
+ /**
154
+ * Adds the amount of triggerUnit to the calendar.
155
+ * @param calendar The calendar to manipulate.
156
+ */
157
+ public void addInterval(Calendar calendar, String triggerUnit, int amount) {
158
+ switch (triggerUnit) {
159
+ case "second":
160
+ calendar.add(Calendar.SECOND, amount);
161
+ break;
162
+
163
+ case "minute":
164
+ calendar.add(Calendar.MINUTE, amount);
165
+ break;
166
+
167
+ case "hour":
168
+ calendar.add(Calendar.HOUR_OF_DAY, amount);
169
+ break;
170
+
171
+ case "day":
172
+ calendar.add(Calendar.DAY_OF_YEAR, amount);
173
+ break;
174
+
175
+ case "week":
176
+ calendar.add(Calendar.WEEK_OF_YEAR, amount);
177
+ break;
178
+
179
+ case "month":
180
+ calendar.add(Calendar.MONTH, amount);
181
+ break;
182
+
183
+ case "quarter":
184
+ calendar.add(Calendar.MONTH, amount * 3);
185
+ break;
186
+
187
+ case "year":
188
+ calendar.add(Calendar.YEAR, amount);
189
+ break;
190
+
191
+ default:
192
+ throw new IllegalArgumentException("Unknown trigger unit: " + triggerUnit);
193
+ }
194
+ }
195
+ }
@@ -0,0 +1,63 @@
1
+ /*
2
+ * Apache 2.0 License
3
+ *
4
+ * Copyright (c) Sebastian Katzer 2017
5
+ * Copyright (c) Manuel Beck 2024
6
+ *
7
+ * This file contains Original Code and/or Modifications of Original Code
8
+ * as defined in and that are subject to the Apache License
9
+ * Version 2.0 (the 'License'). You may not use this file except in
10
+ * compliance with the License. Please obtain a copy of the License at
11
+ * http://opensource.org/licenses/Apache-2.0/ and read it before using this
12
+ * file.
13
+ *
14
+ * The Original Code and all software distributed under the License are
15
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19
+ * Please see the License for the specific language governing rights and
20
+ * limitations under the License.
21
+ */
22
+
23
+ package de.appplant.cordova.plugin.localnotification.trigger;
24
+
25
+ import android.util.Log;
26
+
27
+ import java.util.Calendar;
28
+ import java.util.Date;
29
+
30
+ import de.appplant.cordova.plugin.localnotification.Options;
31
+ import de.appplant.cordova.plugin.localnotification.OptionsTrigger;
32
+
33
+ /**
34
+ * Handles trigger.at
35
+ *
36
+ * Example:
37
+ * trigger: { at: new Date(2017, 10, 27, 15) }
38
+ */
39
+ public class TriggerHandlerAt extends TriggerHandler {
40
+
41
+ public static final String TAG = "TriggerHandlerAt";
42
+
43
+ public TriggerHandlerAt(Options options) {
44
+ super(options);
45
+ }
46
+
47
+ public boolean isLastOccurrence() {
48
+ // Can only be scheduled one time
49
+ return occurrence == 1;
50
+ }
51
+
52
+ /**
53
+ * Calculates the next trigger.
54
+ * @param baseCalendar The base calendar from where to calculate the next trigger.
55
+ */
56
+ public Date calculateNextTrigger(Calendar baseCalendar) {
57
+ // All occurrences are done
58
+ if (isLastOccurrence()) return null;
59
+
60
+ // trigger: { at: new Date(2017, 10, 27, 15) }
61
+ return new Date(optionsTrigger.getAt());
62
+ }
63
+ }
@@ -0,0 +1,257 @@
1
+ /*
2
+ * Apache 2.0 License
3
+ *
4
+ * Copyright (c) Sebastian Katzer 2017
5
+ * Copyright (c) Manuel Beck 2024
6
+ *
7
+ * This file contains Original Code and/or Modifications of Original Code
8
+ * as defined in and that are subject to the Apache License
9
+ * Version 2.0 (the 'License'). You may not use this file except in
10
+ * compliance with the License. Please obtain a copy of the License at
11
+ * http://opensource.org/licenses/Apache-2.0/ and read it before using this
12
+ * file.
13
+ *
14
+ * The Original Code and all software distributed under the License are
15
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19
+ * Please see the License for the specific language governing rights and
20
+ * limitations under the License.
21
+ */
22
+
23
+ package de.appplant.cordova.plugin.localnotification.trigger;
24
+
25
+ import android.util.Log;
26
+ import org.json.JSONObject;
27
+ import java.util.Calendar;
28
+ import java.util.Date;
29
+
30
+ import de.appplant.cordova.plugin.localnotification.Options;
31
+ import de.appplant.cordova.plugin.localnotification.OptionsTrigger;
32
+
33
+ public class TriggerHandlerEvery extends TriggerHandler {
34
+
35
+ public static final String TAG = "TriggerHandlerEvery";
36
+
37
+ /** Is null if trigger.every is a JSONObject */
38
+ private String triggerEveryString;
39
+
40
+ /** Is null if trigger.every is a String */
41
+ private JSONObject triggerEveryJSONObject;
42
+
43
+ /**
44
+ * Example:
45
+ * trigger: { every: 'day', count: 5 }
46
+ * trigger every: { minute: 10, hour: 9, day: 27, month: 10 }
47
+ */
48
+ public TriggerHandlerEvery(Options options) {
49
+ super(options);
50
+ // trigger.every can be String or JSONObject, only one of them
51
+ // will be set
52
+ this.triggerEveryString = optionsTrigger.getEveryAsString();
53
+ this.triggerEveryJSONObject = optionsTrigger.getEveryAsJSONObject();
54
+
55
+ // Change base date if firstAt or after is set
56
+ if (optionsTrigger.has("firstAt")) {
57
+ this.baseDate = new Date(optionsTrigger.getFirstAt());
58
+
59
+ } else if (optionsTrigger.has("after")) {
60
+ this.baseDate = new Date(optionsTrigger.getAfter());
61
+ }
62
+ }
63
+
64
+ public boolean isLastOccurrence() {
65
+ // Check if trigger.count is exceeded if it is set
66
+ return optionsTrigger.has("count") && occurrence >= optionsTrigger.getCount();
67
+ }
68
+
69
+ /**
70
+ * Calculates the next trigger.
71
+ * @param baseCalendar The base calendar from where to calculate the next trigger.
72
+ */
73
+ public Date calculateNextTrigger(Calendar baseCalendar) {
74
+ // All occurrences are done
75
+ if (isLastOccurrence()) return null;
76
+
77
+ Calendar nextCalendar = (Calendar) baseCalendar.clone();
78
+
79
+ // trigger: { every: 'day', count: 5 }
80
+ if (triggerEveryString != null) {
81
+ try {
82
+ addInterval(nextCalendar, triggerEveryString, 1);
83
+ } catch (IllegalArgumentException exception) {
84
+ Log.e(TAG, "Error calculating next trigger, trigger unit is wrong: " + exception.getMessage());
85
+ return null;
86
+ }
87
+
88
+ // trigger every: { minute: 10, hour: 9, day: 27, month: 10 }
89
+ } else if (triggerEveryJSONObject != null) {
90
+ // Set calendar to trigger.every values like minute: 20, hour: 9, etc.
91
+ // Returns the next higher Calendar field for calculating the next trigger
92
+ // If 0 is returned the options are empty or wrong
93
+ int nextTriggerCalendarFieldToIncrease = setEveryValues(nextCalendar);
94
+
95
+ // Nothing should be increased, options are empty or wrong
96
+ if (nextTriggerCalendarFieldToIncrease == 0) return null;
97
+
98
+ // If the next trigger is in the past or equal, increase the calendar
99
+ // The trigger could be set by trigger.every to the past.
100
+ // For e.g., if the current time is 9:30 and every: {minute: 10} is set, the trigger
101
+ // would be set to 9:10. To get a next trigger, the hour have to be increased by 1 to 10:10.
102
+ if (nextCalendar.compareTo(baseCalendar) <= 0) {
103
+ nextCalendar.add(nextTriggerCalendarFieldToIncrease, 1);
104
+ // Correct trigger after incrementing it
105
+ // Example: If weekday was set to monday and a year was added,
106
+ // the weekday could be changed to another day, set to monday again
107
+ setEveryValues(nextCalendar);
108
+ }
109
+ }
110
+
111
+ // Check if the trigger is within the before option
112
+ if (!isWithinTriggerbefore(nextCalendar)) return null;
113
+
114
+ return nextCalendar.getTime();
115
+ }
116
+
117
+ /**
118
+ * Set trigger.every values like { month: 10, day: 27, ...} in the given calendar.
119
+ * @param calendar
120
+ * @return The next higher {@link Calendar} field that has to be increase from the highest trigger.every option
121
+ * For e.g. returns {@link Calendar.HOUR} when maximum minute is set,
122
+ * or {@link Calendar.DAY_OF_YEAR} when maximum hour is set.
123
+ * Returns 0 if no or wrong trigger.every values are set.
124
+ */
125
+ private int setEveryValues(Calendar calendar) {
126
+ int nextTriggerCalendarFieldToIncrease = 0;
127
+
128
+ // Set second to 0
129
+ calendar.set(Calendar.SECOND, 0);
130
+
131
+ // Set minute from options in next calendar
132
+ if (triggerEveryJSONObject.has("minute")) {
133
+ calendar.set(Calendar.MINUTE, triggerEveryJSONObject.optInt("minute"));
134
+ // One hour has to be added for the next trigger
135
+ nextTriggerCalendarFieldToIncrease = Calendar.HOUR;
136
+ }
137
+
138
+ // Set hour from options in next calendar
139
+ if (triggerEveryJSONObject.has("hour")) {
140
+ calendar.set(Calendar.HOUR_OF_DAY, triggerEveryJSONObject.optInt("hour"));
141
+ resetTimeIfNotSetByTrigger(calendar);
142
+ // One day has to be added for the next trigger
143
+ nextTriggerCalendarFieldToIncrease = Calendar.DAY_OF_YEAR;
144
+ }
145
+
146
+ // Set day from options in next calendar
147
+ if (triggerEveryJSONObject.has("day")) {
148
+ calendar.set(Calendar.DAY_OF_MONTH, triggerEveryJSONObject.optInt("day"));
149
+ resetTimeIfNotSetByTrigger(calendar);
150
+ // One month has to be added for the next trigger
151
+ nextTriggerCalendarFieldToIncrease = Calendar.MONTH;
152
+ }
153
+
154
+ // Set weekday (day of week) from options in next calendar (1 = Monday, 7 = Sunday)
155
+ if (triggerEveryJSONObject.has("weekday")) {
156
+ // Calendar.MONDAY is 2, so we have to add 1 to the weekday
157
+ calendar.set(Calendar.DAY_OF_WEEK, 1 + triggerEveryJSONObject.optInt("weekday"));
158
+
159
+ resetTimeIfNotSetByTrigger(calendar);
160
+
161
+ // One week has to be added for the next trigger
162
+ nextTriggerCalendarFieldToIncrease = Calendar.WEEK_OF_YEAR;
163
+ }
164
+
165
+ // Set weekOfMonth from options in next calendar
166
+ if (triggerEveryJSONObject.has("weekOfMonth")) {
167
+
168
+ int weekOfMonth = triggerEveryJSONObject.optInt("weekOfMonth");
169
+ calendar.set(Calendar.WEEK_OF_MONTH, weekOfMonth);
170
+
171
+ // Reset hour/minute
172
+ resetTimeIfNotSetByTrigger(calendar);
173
+ resetWeekdayIfNotSetByTrigger(calendar);
174
+
175
+ // If the week of month is the first week, set day of month to 1
176
+ if (weekOfMonth == 1) {
177
+ calendar.set(Calendar.DAY_OF_MONTH, 1);
178
+ // Correct weekday if it is set, but prevent jumping to the last month
179
+ setWeekdayIfInFuture(calendar);
180
+ }
181
+
182
+ // One month has to be added for the next trigger
183
+ nextTriggerCalendarFieldToIncrease = Calendar.MONTH;
184
+ }
185
+
186
+ // Set week of year from options in next calendar
187
+ if (triggerEveryJSONObject.has("week")) {
188
+ int weekOfYear = triggerEveryJSONObject.optInt("week");
189
+ calendar.set(Calendar.WEEK_OF_YEAR, weekOfYear);
190
+
191
+ resetTimeIfNotSetByTrigger(calendar);
192
+ resetWeekdayIfNotSetByTrigger(calendar);
193
+
194
+ // If the week of year is the first week, set day of year to 1
195
+ if (weekOfYear == 1) {
196
+ calendar.set(Calendar.DAY_OF_YEAR, 1);
197
+ // Correct weekday if it is set, but prevent jumping to the last year
198
+ setWeekdayIfInFuture(calendar);
199
+ }
200
+
201
+ nextTriggerCalendarFieldToIncrease = Calendar.YEAR;
202
+ }
203
+
204
+ // Set month from options in next calendar
205
+ if (triggerEveryJSONObject.has("month")) {
206
+ // The first month is 0 for Calendar
207
+ calendar.set(Calendar.MONTH, triggerEveryJSONObject.optInt("month") - 1);
208
+
209
+ resetTimeIfNotSetByTrigger(calendar);
210
+ resetDayIfNotSetByTrigger(calendar);
211
+
212
+ // One year has to be added for the next trigger
213
+ nextTriggerCalendarFieldToIncrease = Calendar.YEAR;
214
+ }
215
+
216
+ return nextTriggerCalendarFieldToIncrease;
217
+ }
218
+
219
+ /**
220
+ * Set minute/hour to 0 if not set by trigger
221
+ **/
222
+ private void resetTimeIfNotSetByTrigger(Calendar calendar) {
223
+ // Reset minute if not set
224
+ if (!triggerEveryJSONObject.has("minute")) calendar.set(Calendar.MINUTE, 0);
225
+ // Reset hour if not set
226
+ if (!triggerEveryJSONObject.has("hour")) calendar.set(Calendar.HOUR_OF_DAY, 0);
227
+ }
228
+
229
+ /**
230
+ * Set day to 1 if not set by trigger
231
+ * @param calendar
232
+ */
233
+ private void resetDayIfNotSetByTrigger(Calendar calendar) {
234
+ if (triggerEveryJSONObject.has("day") || triggerEveryJSONObject.has("weekday")) return;
235
+ calendar.set(Calendar.DAY_OF_MONTH, 1);
236
+ }
237
+
238
+ private void resetWeekdayIfNotSetByTrigger(Calendar calendar) {
239
+ if (triggerEveryJSONObject.has("weekday")) return;
240
+ calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
241
+ }
242
+
243
+ /**
244
+ * Sets a configured weekday only if it is in the future. This should prevent the calendar
245
+ * jump to the last month or year, if the date was set to the first of a month or year.
246
+ */
247
+ private void setWeekdayIfInFuture(Calendar calendar) {
248
+ // Not set by options
249
+ if (!triggerEveryJSONObject.has("weekday")) return;
250
+
251
+ // Only set if in future. For weekday 1 is Monday and for Calendar it is 2,
252
+ // so we have to consider it
253
+ if (calendar.get(Calendar.DAY_OF_WEEK) < 1 + triggerEveryJSONObject.optInt("weekday")) {
254
+ calendar.set(Calendar.DAY_OF_WEEK, 1 + triggerEveryJSONObject.optInt("weekday"));
255
+ }
256
+ }
257
+ }
@@ -0,0 +1,69 @@
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
+
22
+ package de.appplant.cordova.plugin.localnotification.trigger;
23
+
24
+ import android.util.Log;
25
+
26
+ import java.util.Calendar;
27
+ import java.util.Date;
28
+
29
+ import de.appplant.cordova.plugin.localnotification.Options;
30
+ import de.appplant.cordova.plugin.localnotification.OptionsTrigger;
31
+
32
+ /**
33
+ * Handles trigger.in
34
+ *
35
+ * Example:
36
+ * trigger: { in: 1, unit: 'hour' }
37
+ */
38
+ public class TriggerHandlerIn extends TriggerHandler {
39
+
40
+ public static final String TAG = "TriggerHandlerIn";
41
+
42
+ public TriggerHandlerIn(Options options) {
43
+ super(options);
44
+ }
45
+
46
+ public boolean isLastOccurrence() {
47
+ // trigger.in can only be scheduled one time
48
+ return occurrence == 1;
49
+ }
50
+
51
+ /**
52
+ * Calculates the next trigger.
53
+ * @param baseCalendar The base calendar from where to calculate the next trigger.
54
+ */
55
+ public Date calculateNextTrigger(Calendar baseCalendar) {
56
+ // All occurrences are done
57
+ if (isLastOccurrence()) return null;
58
+
59
+ // trigger: { in: 1, unit: 'hour' }
60
+ // Catch wrong trigger units
61
+ try {
62
+ addInterval(baseCalendar, optionsTrigger.getUnit(), optionsTrigger.getIn());
63
+ return baseCalendar.getTime();
64
+ } catch (IllegalArgumentException exception) {
65
+ Log.e(TAG, "Error calculating trigger, trigger unit is wrong: " + exception.getMessage());
66
+ return null;
67
+ }
68
+ }
69
+ }