-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathICalObject.kt
More file actions
1527 lines (1331 loc) · 64.9 KB
/
ICalObject.kt
File metadata and controls
1527 lines (1331 loc) · 64.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) Techbee e.U.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.techbee.jtx.database
import android.content.ContentValues
import android.content.Context
import android.net.Uri
import android.os.Parcelable
import android.provider.BaseColumns
import android.util.Log
import androidx.annotation.ColorInt
import androidx.annotation.StringRes
import androidx.annotation.VisibleForTesting
import androidx.core.net.toUri
import androidx.core.util.PatternsCompat
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.PrimaryKey
import at.bitfire.ical4android.util.TimeApiExtensions.toLocalDate
import at.techbee.jtx.JtxContract
import at.techbee.jtx.R
import at.techbee.jtx.ui.settings.DropdownSettingOption
import at.techbee.jtx.ui.settings.SettingsStateHolder
import at.techbee.jtx.util.DateTimeUtils
import at.techbee.jtx.util.DateTimeUtils.requireTzId
import at.techbee.jtx.util.UiUtil.asDayOfWeek
import kotlinx.parcelize.Parcelize
import net.fortuna.ical4j.model.Date
import net.fortuna.ical4j.model.DateList
import net.fortuna.ical4j.model.DateTime
import net.fortuna.ical4j.model.Period
import net.fortuna.ical4j.model.PeriodList
import net.fortuna.ical4j.model.Property
import net.fortuna.ical4j.model.Recur
import net.fortuna.ical4j.model.Recur.Frequency
import net.fortuna.ical4j.model.TimeZoneRegistryFactory
import net.fortuna.ical4j.model.component.VJournal
import net.fortuna.ical4j.model.component.VToDo
import net.fortuna.ical4j.model.parameter.Value
import net.fortuna.ical4j.model.property.DtStart
import net.fortuna.ical4j.model.property.ExDate
import net.fortuna.ical4j.model.property.RDate
import net.fortuna.ical4j.model.property.RRule
import net.fortuna.ical4j.model.property.RecurrenceId
import java.io.UnsupportedEncodingException
import java.net.URLDecoder
import java.text.ParseException
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.TextStyle
import java.time.temporal.ChronoUnit
import java.util.Locale
import java.util.TimeZone
import java.util.UUID
import kotlin.math.absoluteValue
import kotlin.time.Duration
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
/** The name of the the table for IcalObjects.
* This is a general purpose table containing general columns
* for Journals, Notes and Todos */
const val TABLE_NAME_ICALOBJECT = "icalobject"
/** The name of the ID column.
* This is the unique identifier of an ICalObject
* Type: [Long]*/
const val COLUMN_ID = BaseColumns._ID
/** The column for the module.
* This is an internal differentiation for JOURNAL, NOTE and TODOs
* provided in the enum [Module]
* Type: [String]
*/
const val COLUMN_MODULE = "module"
/* The names of all the other columns */
/** The column for the component based on the values
* provided in the enum [Component]
* Type: [String]
*/
const val COLUMN_COMPONENT = "component"
/**
* Purpose: This column/property defines a short summary or subject for the calendar component.
* See [https://tools.ietf.org/html/rfc5545#section-3.8.1.12]
* Type: [String]
*/
const val COLUMN_SUMMARY = "summary"
/**
* Purpose: This column/property provides a more complete description of the calendar component than that provided by the "SUMMARY" property.
* See [https://tools.ietf.org/html/rfc5545#section-3.8.1.5]
* Type: [String]
*/
const val COLUMN_DESCRIPTION = "description"
/**
* Purpose: This column/property specifies when the calendar component begins.
* The corresponding timezone is stored in [COLUMN_DTSTART_TIMEZONE].
* See [https://tools.ietf.org/html/rfc5545#section-3.8.2.4]
* Type: [Long]
*/
const val COLUMN_DTSTART = "dtstart"
/**
* Purpose: This column/property specifies the timezone of when the calendar component begins.
* The corresponding datetime is stored in [COLUMN_DTSTART].
* See [https://tools.ietf.org/html/rfc5545#section-3.8.2.4]
* Type: [String]
*/
const val COLUMN_DTSTART_TIMEZONE = "dtstarttimezone"
/**
* Purpose: This column/property specifies when the calendar component ends.
* The corresponding timezone is stored in [COLUMN_DTEND_TIMEZONE].
* See [https://tools.ietf.org/html/rfc5545#section-3.8.2.4]
* Type: [Long]
*/
const val COLUMN_DTEND = "dtend"
/**
* Purpose: This column/property specifies the timezone of when the calendar component ends.
* The corresponding datetime is stored in [COLUMN_DTEND].
* See [https://tools.ietf.org/html/rfc5545#section-3.8.2.2]
* Type: [String]
*/
const val COLUMN_DTEND_TIMEZONE = "dtendtimezone"
/**
* Purpose: This property defines the overall status or confirmation for the calendar component.
* The possible values of a status are defined in [Status]
* See [https://tools.ietf.org/html/rfc5545#section-3.8.1.11]
* Type: [String]
*/
const val COLUMN_STATUS = "status"
/**
* Purpose: This property defines the access classification for a calendar component.
* The possible values of a status are defined in the enum [Classification].
* See [https://tools.ietf.org/html/rfc5545#section-3.8.1.11]
* Type: [String]
*/
const val COLUMN_CLASSIFICATION = "classification"
/**
* Purpose: This property defines a Uniform Resource Locator (URL) associated with the iCalendar object.
* See [https://tools.ietf.org/html/rfc5545#section-3.8.4.6]
* Type: [String]
*/
const val COLUMN_URL = "url"
/**
* Purpose: This property is used to represent contact information or alternately a reference
* to contact information associated with the calendar component.
* See [https://tools.ietf.org/html/rfc5545#section-3.8.4.2]
* Type: [String]
*/
const val COLUMN_CONTACT = "contact"
/**
* Purpose: This property specifies information related to the global position for the activity specified by a calendar component.
* This property is split in the fields [COLUMN_GEO_LAT] for the latitude
* and [COLUMN_GEO_LONG] for the longitude coordinates.
* See [https://tools.ietf.org/html/rfc5545#section-3.8.1.6]
* Type: [Double]
*/
const val COLUMN_GEO_LAT = "geolat"
/**
* Purpose: This property specifies information related to the global position for the activity specified by a calendar component.
* This property is split in the fields [COLUMN_GEO_LAT] for the latitude
* and [COLUMN_GEO_LONG] for the longitude coordinates.
* See [https://tools.ietf.org/html/rfc5545#section-3.8.1.6]
* Type: [Double]
*/
const val COLUMN_GEO_LONG = "geolong"
/**
* Purpose: This property defines the intended venue for the activity defined by a calendar component.
* See [https://tools.ietf.org/html/rfc5545#section-3.8.1.7]
* Type: [String]
*/
const val COLUMN_LOCATION = "location"
/**
* Purpose: This property defines the alternative representation of the intended venue for the activity defined by a calendar component.
* See [https://tools.ietf.org/html/rfc5545#section-3.8.1.7]
* Type: [String]
*/
const val COLUMN_LOCATION_ALTREP = "locationaltrep"
/**
* Purpose: This property is used by an assignee or delegatee of a to-do to convey the percent completion of a to-do to the "Organizer".
* See [https://tools.ietf.org/html/rfc5545#section-3.8.1.8]
* Type: [Int]
*/
const val COLUMN_PERCENT = "percent"
/**
* Purpose: This property defines the relative priority for a calendar component.
* See [https://tools.ietf.org/html/rfc5545#section-3.8.1.9]
* Type: [Int]
*/
const val COLUMN_PRIORITY = "priority"
/**
* Purpose: This property defines the date and time that a to-do is expected to be completed.
* The corresponding timezone is stored in [COLUMN_DUE_TIMEZONE].
* See [https://tools.ietf.org/html/rfc5545#section-3.8.2.3]
* Type: [Long]
*/
const val COLUMN_DUE = "due"
/**
* Purpose: This column/property specifies the timezone of when a to-do is expected to be completed.
* The corresponding datetime is stored in [COLUMN_DUE].
* See [https://tools.ietf.org/html/rfc5545#section-3.8.2.2]
* Type: [String]
*/
const val COLUMN_DUE_TIMEZONE = "duetimezone"
/**
* Purpose: This property defines the date and time that a to-do was actually completed.
* The corresponding timezone is stored in [COLUMN_COMPLETED_TIMEZONE].
* See [https://tools.ietf.org/html/rfc5545#section-3.8.2.1]
* Type: [Long]
*/
const val COLUMN_COMPLETED = "completed"
/**
* Purpose: This column/property specifies the timezone of when a to-do was actually completed.
* The corresponding datetime is stored in [COLUMN_DUE].
* See [https://tools.ietf.org/html/rfc5545#section-3.8.2.1]
* Type: [String]
*/
const val COLUMN_COMPLETED_TIMEZONE = "completedtimezone"
/**
* Purpose: This property specifies a positive duration of time.
* See [https://tools.ietf.org/html/rfc5545#section-3.8.2.5]
* Type: [String]
*/
const val COLUMN_DURATION = "duration"
/**
* Purpose: This property defines the persistent, globally unique identifier for the calendar component.
* See [https://tools.ietf.org/html/rfc5545#section-3.8.4.7]
* Type: [String]
*/
const val COLUMN_UID = "uid"
/**
* Purpose: This property specifies the date and time that the calendar information
* was created by the calendar user agent in the calendar store.
* See [https://tools.ietf.org/html/rfc5545#section-3.8.7.1]
* Type: [Long]
*/
const val COLUMN_CREATED = "created"
/**
* Purpose: In the case of an iCalendar object that specifies a
* "METHOD" property, this property specifies the date and time that
* the instance of the iCalendar object was created. In the case of
* an iCalendar object that doesn't specify a "METHOD" property, this
* property specifies the date and time that the information
* associated with the calendar component was last revised in the
* calendar store.
* See [https://tools.ietf.org/html/rfc5545#section-3.8.7.2]
* Type: [Long]
*/
const val COLUMN_DTSTAMP = "dtstamp"
/**
* Purpose: This property specifies the date and time that the information associated
* with the calendar component was last revised in the calendar store.
* See [https://tools.ietf.org/html/rfc5545#section-3.8.7.3]
* Type: [Long]
*/
const val COLUMN_LAST_MODIFIED = "lastmodified"
/**
* Purpose: This property defines the revision sequence number of the calendar component within a sequence of revisions.
* See [https://tools.ietf.org/html/rfc5545#section-3.8.7.4]
* Type: [Int]
*/
const val COLUMN_SEQUENCE = "sequence"
/**
* Purpose: This property defines a rule or repeating pattern for recurring events,
* to-dos, journal entries, or time zone definitions.
* Type: [String]
*/
const val COLUMN_RRULE = "rrule"
/**
* Purpose: This property defines the list of DATE-TIME values for
* recurring events, to-dos, journal entries, or time zone definitions.
* Type: [String], contains a list of comma-separated date values as Long
*/
const val COLUMN_RDATE = "rdate"
/**
* Purpose: This property defines the list of DATE-TIME exceptions for
* recurring events, to-dos, journal entries, or time zone definitions.
* Type: [String], contains a list of comma-separated date values as Long
*/
const val COLUMN_EXDATE = "exdate"
/**
* Purpose: This property is used in conjunction with the "UID" and
* "SEQUENCE" properties to identify a specific instance of a
* recurring "VEVENT", "VTODO", or "VJOURNAL" calendar component.
* The property value is the original value of the "DTSTART" property
* of the recurrence instance.
*/
const val COLUMN_RECURID = "recurid"
/**
* Purpose: This property is used in conjunction with the "UID" and
* "SEQUENCE" properties to identify a specific instance of a
* recurring "VEVENT", "VTODO", or "VJOURNAL" calendar component.
* The property value is the original value of the "DTSTART" property
* of the recurrence instance.
*/
const val COLUMN_RECURID_TIMEZONE = "recuridtimezone"
/**
* Purpose: This property is used to return status code information
related to the processing of an associated iCalendar object. The
value type for this property is TEXT.
*/
const val COLUMN_RSTATUS = "rstatus"
/**
* Purpose: This property specifies a color used for displaying the calendar, event, t0d0, or journal data.
* See [https://tools.ietf.org/html/rfc7986#section-5.9]
* Type: [String]
*/
const val COLUMN_COLOR = "color"
/**
* Purpose: This column is the foreign key to the [TABLE_NAME_COLLECTION].
* Type: [Long]
*/
const val COLUMN_ICALOBJECT_COLLECTIONID = "collectionId"
/**
* Purpose: This column defines if a local collection was changed that is supposed to be synchronised.
* Type: [Boolean]
*/
const val COLUMN_DIRTY = "dirty"
/**
* Purpose: This column defines if a collection that is supposed to be synchonized was locally marked as deleted.
* Type: [Boolean]
*/
const val COLUMN_DELETED = "deleted"
/**
* Purpose: filename of the synched entry (*.ics), only relevant for synched entries through sync-adapter
* Type: [String]
*/
const val COLUMN_FILENAME = "filename"
/**
* Purpose: eTag for SyncAdapter, only relevant for synched entries through sync-adapter
* Type: [String]
*/
const val COLUMN_ETAG = "etag"
/**
* Purpose: scheduleTag for SyncAdapter, only relevant for synched entries through sync-adapter
* Type: [String]
*/
const val COLUMN_SCHEDULETAG = "scheduletag"
/**
* Purpose: flags for SyncAdapter, only relevant for synched entries through sync-adapter
* Type: [Int]
*/
const val COLUMN_FLAGS = "flags"
/**
* Purpose: defines if the subtasks for this entry are expanded
* Type: [Boolean?]
*/
const val COLUMN_SUBTASKS_EXPANDED = "subtasksExpanded"
/**
* Purpose: defines if the subnotes for this entry are expanded
* Type: [Boolean?]
*/
const val COLUMN_SUBNOTES_EXPANDED = "subnotesExpanded"
/**
* Purpose: defines if the attachments for this entry are expanded
* Type: [Boolean?]
*/
const val COLUMN_ATTACHMENTS_EXPANDED = "attachmentsExpanded"
/**
* Purpose: defines if the order index especially for subtasks and subnotes
* Type: [Int?]
*/
const val COLUMN_SORT_INDEX = "sortIndex"
/**
* Purpose: defines if the parents for this entry are expanded
* Type: [Boolean?]
*/
const val COLUMN_PARENTS_EXPANDED = "parentsExpanded"
/**
* Purpose: Defines an extended status to the RFC-status for more flexibility
* This is put into an extended property in the iCalendar-file
* Type: [String]
*/
const val COLUMN_EXTENDED_STATUS = "xstatus"
/**
* Purpose: Defines the radius for a geofence in meters
* This is put into an extended property in the iCalendar-file
* Type: [String]
*/
const val COLUMN_GEOFENCE_RADIUS = "geofenceRadius"
/**
* Purpose: Defines the radius for a geofence in meters
* This is put into an extended property in the iCalendar-file
* Type: [String]
*/
const val COLUMN_IS_ALARM_NOTIFICATION_ACTIVE = "isAlarmNotificationActive"
@Parcelize
@Entity(
tableName = TABLE_NAME_ICALOBJECT,
foreignKeys = [ForeignKey(
entity = ICalCollection::class,
parentColumns = arrayOf(COLUMN_COLLECTION_ID),
childColumns = arrayOf(COLUMN_ICALOBJECT_COLLECTIONID),
onDelete = ForeignKey.CASCADE
)]
)
data class ICalObject(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(index = true, name = COLUMN_ID)
var id: Long = 0L,
@ColumnInfo(name = COLUMN_MODULE) var module: String = Module.NOTE.name,
@ColumnInfo(name = COLUMN_COMPONENT) var component: String = Component.VJOURNAL.name,
@ColumnInfo(name = COLUMN_SUMMARY) var summary: String? = null,
@ColumnInfo(name = COLUMN_DESCRIPTION) var description: String? = null,
@ColumnInfo(name = COLUMN_DTSTART) var dtstart: Long? = null,
@ColumnInfo(name = COLUMN_DTSTART_TIMEZONE) var dtstartTimezone: String? = null,
@ColumnInfo(name = COLUMN_DTEND) var dtend: Long? = null,
@ColumnInfo(name = COLUMN_DTEND_TIMEZONE) var dtendTimezone: String? = null,
@ColumnInfo(name = COLUMN_STATUS) var status: String? = null,
@ColumnInfo(name = COLUMN_EXTENDED_STATUS) var xstatus: String? = null,
@ColumnInfo(name = COLUMN_CLASSIFICATION) var classification: String? = null,
@ColumnInfo(name = COLUMN_URL) var url: String? = null,
@ColumnInfo(name = COLUMN_CONTACT) var contact: String? = null,
@ColumnInfo(name = COLUMN_GEO_LAT) var geoLat: Double? = null,
@ColumnInfo(name = COLUMN_GEO_LONG) var geoLong: Double? = null,
@ColumnInfo(name = COLUMN_LOCATION) var location: String? = null,
@ColumnInfo(name = COLUMN_LOCATION_ALTREP) var locationAltrep: String? = null,
@ColumnInfo(name = COLUMN_GEOFENCE_RADIUS) var geofenceRadius: Int? = null,
@ColumnInfo(name = COLUMN_PERCENT) var percent: Int? = null, // VTODO only!
@ColumnInfo(name = COLUMN_PRIORITY) var priority: Int? = null, // VTODO and VEVENT
@ColumnInfo(name = COLUMN_DUE) var due: Long? = null, // VTODO only!
@ColumnInfo(name = COLUMN_DUE_TIMEZONE) var dueTimezone: String? = null, //VTODO only!
@ColumnInfo(name = COLUMN_COMPLETED) var completed: Long? = null, // VTODO only!
@ColumnInfo(name = COLUMN_COMPLETED_TIMEZONE) var completedTimezone: String? = null, //VTODO only!
@ColumnInfo(name = COLUMN_DURATION) var duration: String? = null, //VTODO only!
@ColumnInfo(name = COLUMN_UID) var uid: String = generateNewUID(), //unique identifier, see https://tools.ietf.org/html/rfc5545#section-3.8.4.7
/*
The following properties specify change management information in calendar components.
https://tools.ietf.org/html/rfc5545#section-3.8.7
*/
@ColumnInfo(name = COLUMN_CREATED) var created: Long = System.currentTimeMillis(), // see https://tools.ietf.org/html/rfc5545#section-3.8.7.1
@ColumnInfo(name = COLUMN_DTSTAMP) var dtstamp: Long = System.currentTimeMillis(), // see https://tools.ietf.org/html/rfc5545#section-3.8.7.2
@ColumnInfo(name = COLUMN_LAST_MODIFIED) var lastModified: Long = System.currentTimeMillis(), // see https://tools.ietf.org/html/rfc5545#section-3.8.7.3
@ColumnInfo(name = COLUMN_SEQUENCE) var sequence: Long = 0, // increase on every change (+1), see https://tools.ietf.org/html/rfc5545#section-3.8.7.4
@ColumnInfo(name = COLUMN_RRULE) var rrule: String? = null, //only for recurring events, see https://tools.ietf.org/html/rfc5545#section-3.8.5.3
@ColumnInfo(name = COLUMN_EXDATE) var exdate: String? = null, //only for recurring events, see https://tools.ietf.org/html/rfc5545#section-3.8.5.1
@ColumnInfo(name = COLUMN_RDATE) var rdate: String? = null, //only for recurring events, see https://tools.ietf.org/html/rfc5545#section-3.8.5.2
@ColumnInfo(name = COLUMN_RECURID) var recurid: String? = null, //only for recurring events, see https://tools.ietf.org/html/rfc5545#section-3.8.5
@ColumnInfo(name = COLUMN_RECURID_TIMEZONE) var recuridTimezone: String? = null,
@ColumnInfo(name = COLUMN_RSTATUS) var rstatus: String? = null,
@ColumnInfo(name = COLUMN_COLOR)
@ColorInt
var color: Int? = null,
@ColumnInfo(index = true, name = COLUMN_ICALOBJECT_COLLECTIONID) var collectionId: Long = 1L,
@ColumnInfo(name = COLUMN_DIRTY) var dirty: Boolean = false,
@ColumnInfo(name = COLUMN_DELETED) var deleted: Boolean = false,
@ColumnInfo(name = COLUMN_FILENAME) var fileName: String? = null,
@ColumnInfo(name = COLUMN_ETAG) var eTag: String? = null,
@ColumnInfo(name = COLUMN_SCHEDULETAG) var scheduleTag: String? = null,
@ColumnInfo(name = COLUMN_FLAGS) var flags: Int? = null,
@ColumnInfo(name = COLUMN_SUBTASKS_EXPANDED) var isSubtasksExpanded: Boolean? = null,
@ColumnInfo(name = COLUMN_SUBNOTES_EXPANDED) var isSubnotesExpanded: Boolean? = null,
@ColumnInfo(name = COLUMN_PARENTS_EXPANDED) var isParentsExpanded: Boolean? = null,
@ColumnInfo(name = COLUMN_ATTACHMENTS_EXPANDED) var isAttachmentsExpanded: Boolean? = null,
@ColumnInfo(name = COLUMN_SORT_INDEX) var sortIndex: Int? = null,
@ColumnInfo(name = COLUMN_IS_ALARM_NOTIFICATION_ACTIVE, defaultValue = "0") var isAlarmNotificationActive: Boolean = false
) : Parcelable {
companion object {
private const val WINDOW_SECONDLY = 1 / 24 / 60
private const val WINDOW_MINUTELY = 1 / 24
private const val WINDOW_HOURLY = 1
private const val WINDOW_DAILY = 14
private const val WINDOW_WEEKLY = 30
private const val WINDOW_MONTHLY = 365
private const val WINDOW_YEARLY = 730
private const val WINDOW_DEFAULT = 30
const val TZ_ALLDAY = "ALLDAY"
fun createJournal() = createJournal(null)
fun createJournal(summary: String?): ICalObject = ICalObject(
component = Component.VJOURNAL.name,
module = Module.JOURNAL.name,
dtstart = DateTimeUtils.getTodayAsLong(),
dtstartTimezone = TZ_ALLDAY,
status = Status.FINAL.status,
summary = summary,
dirty = true
)
fun createNote() = createNote(null)
fun createNote(summary: String?) = ICalObject(
component = Component.VJOURNAL.name,
module = Module.NOTE.name,
status = Status.FINAL.status,
summary = summary,
dirty = true
)
fun createTodo() = createTask(null)
fun createTask(summary: String?) = ICalObject(
component = Component.VTODO.name,
module = Module.TODO.name,
summary = summary,
status = null,
percent = null,
priority = null,
dueTimezone = TZ_ALLDAY,
dtstartTimezone = TZ_ALLDAY,
completedTimezone = TZ_ALLDAY,
dirty = true
)
fun generateNewUID() = UUID.randomUUID().toString()
/**
* Create a new [ICalObject] from the specified [ContentValues].
*
* @param values A [ICalObject] that at least contain [.COLUMN_NAME].
* @return A newly created [ICalObject] instance.
*/
fun fromContentValues(values: ContentValues?): ICalObject? {
if (values == null)
return null
if (values.getAsLong(COLUMN_ICALOBJECT_COLLECTIONID) == null)
throw IllegalArgumentException("CollectionId cannot be null.")
return ICalObject().applyContentValues(values)
}
fun fromText(
module: Module,
collectionId: Long,
text: String?,
defaultJournalDateSettingOption: DropdownSettingOption,
defaultStartDateSettingOption: DropdownSettingOption,
defaultStartTime: LocalTime?,
defaultStartTimezone: String?,
defaultDueDateSettingOption: DropdownSettingOption,
defaultDueTime: LocalTime?,
defaultDueTimezone: String?,
defaultClassification: Classification
): ICalObject {
val iCalObject = when(module) {
Module.JOURNAL -> createJournal()
Module.NOTE -> createNote()
Module.TODO -> createTodo()
}
if(module == Module.JOURNAL) {
iCalObject.setDefaultJournalDateFromSettings(defaultJournalDateSettingOption)
}
if(module == Module.TODO) {
iCalObject.setDefaultStartDateFromSettings(defaultStartDateSettingOption, defaultStartTime, defaultStartTimezone)
iCalObject.setDefaultDueDateFromSettings(defaultDueDateSettingOption, defaultDueTime, defaultDueTimezone)
}
iCalObject.classification = defaultClassification.classification
iCalObject.parseSummaryAndDescription(text)
iCalObject.parseURL(text)
iCalObject.parseLatLng(text)
iCalObject.collectionId = collectionId
return iCalObject
}
fun getRecurId(dtstart: Long?, dtstartTimezone: String?): String? {
if (dtstart == null)
return null
return when {
dtstartTimezone == TZ_ALLDAY -> DtStart(Date(dtstart)).value
dtstartTimezone.isNullOrEmpty() -> DtStart(DateTime(dtstart)).value
else -> {
val timezone = TimeZoneRegistryFactory.getInstance().createRegistry()
.getTimeZone(dtstartTimezone)
val withTimezone = DtStart(DateTime(dtstart))
withTimezone.timeZone = timezone
withTimezone.value + withTimezone.parameters
}
}
}
/**
* This function checks if the given timezone is a timezone that can be processed by the app
* @param [tz] the timezone that needs to be validated
* @return If the string is null or TZ_ALLDAY, then the input parameter is just returned.
* Else the timezone is checked if it can be transformed into a Java Timezone and the
* Id of the timezone is returned. This would be the same as the input or GMT if the
* Timezone is unknown.
*/
@VisibleForTesting
fun getValidTimezoneOrNull(tz: String?): String? {
if(tz == null || tz == TZ_ALLDAY)
return tz
return TimeZone.getTimeZone(tz).id
}
fun getMapLink(geoLat: Double?, geoLong: Double?, location: String?, mapsProvider: DropdownSettingOption): Uri? {
return if(geoLat != null || geoLong != null) {
try {
if (mapsProvider == DropdownSettingOption.MAP_GOOGLE_MAPS)
"https://www.google.com/maps/search/?api=1&query=$geoLat%2C$geoLong".toUri()
else
"https://www.openstreetmap.org/#map=15/$geoLat/$geoLong".toUri()
} catch (_: java.lang.IllegalArgumentException) { null }
} else if (!location.isNullOrEmpty()) {
if (mapsProvider == DropdownSettingOption.MAP_GOOGLE_MAPS)
"https://www.google.com/maps/search/$location/".toUri()
else
"https://www.openstreetmap.org/search?query=$location".toUri()
} else null
}
/**
* @param geoLat Latitude as Double
* @param geoLong Longitude as Double
* @return A textual representation of the Latitude and Longitude e.g. (1.23400, 5.67700)
*/
fun getLatLongString(geoLat: Double?, geoLong: Double?): String? {
return if(geoLat != null && geoLong != null) {
"(" + "%.5f".format(Locale.ENGLISH, geoLat) + ", " + "%.5f".format(Locale.ENGLISH, geoLong) + ")"
} else {
null
}
}
/**
* @return true if the current entry is overdue and not completed,
* null if no due date is set and not completed, false otherwise
*/
fun isOverdue(status: String?, percent: Int?, due: Long?, dueTimezone: String?): Boolean? {
if(percent == 100 || status == Status.COMPLETED.status)
return false
if(due == null)
return null
val localNow = ZonedDateTime.now()
val localDue = DateTimeUtils.getZonedDateTimeInLocalTZ(due, dueTimezone)
return ChronoUnit.MINUTES.between(localNow, localDue) < 0L
}
fun getDtstartTextInfo(module: Module, dtstart: Long?, dtstartTimezone: String?, daysOnly: Boolean = false, shortStyle: Boolean = false, context: Context): String {
if(dtstart == null && module == Module.TODO)
return context.getString(R.string.list_start_without)
else if(dtstart == null)
return context.getString(R.string.list_date_without)
val settingsStateHolder = SettingsStateHolder(context)
val timezone2show =
if(dtstartTimezone == TZ_ALLDAY || dtstartTimezone == null || settingsStateHolder.settingDisplayTimezone.value == DropdownSettingOption.DISPLAY_TIMEZONE_ORIGINAL)
dtstartTimezone
else
null
val localNow = ZonedDateTime.now()
val localTomorrow = localNow.plusDays(1)
val localStart = DateTimeUtils.getZonedDateTimeInLocalTZ(dtstart, dtstartTimezone) ?: return ""
var finalString = ""
fun getTimeAndTimezone(): String {
var timeAndTimezone = ""
if(timezone2show != TZ_ALLDAY && !daysOnly)
timeAndTimezone += " ${DateTimeUtils.convertLongToShortTimeString(dtstart, timezone2show)}"
if(timezone2show != null && timezone2show != TZ_ALLDAY && !daysOnly)
timeAndTimezone += " ${requireTzId(timezone2show).id}"
return timeAndTimezone
}
if(module == Module.TODO && !shortStyle) {
when {
localStart.year == localNow.year && localStart.month == localNow.month && localStart.dayOfMonth == localNow.dayOfMonth && (daysOnly || timezone2show == TZ_ALLDAY) -> finalString += context.getString(R.string.list_start_today)
ChronoUnit.MINUTES.between(localNow, localStart) < 0L -> finalString += context.getString(R.string.list_start_past)
ChronoUnit.HOURS.between(localNow, localStart) < 1L -> finalString += context.getString(R.string.list_start_shortly)
localStart.year == localNow.year && localStart.month == localNow.month && localStart.dayOfMonth == localNow.dayOfMonth -> finalString += context.getString(R.string.list_start_inXhours, ChronoUnit.HOURS.between(localNow, localStart))
localStart.year == localTomorrow.year && localStart.month == localTomorrow.month && localStart.dayOfMonth == localTomorrow.dayOfMonth -> {
finalString += context.getString(R.string.list_start_tomorrow)
finalString += getTimeAndTimezone()
}
ChronoUnit.DAYS.between(localNow, localStart) < 6 -> {
finalString += context.getString(R.string.list_start_on_weekday, localStart.dayOfWeek.getDisplayName(TextStyle.FULL, Locale.getDefault()))
finalString += getTimeAndTimezone()
}
else -> {
finalString += DateTimeUtils.convertLongToMediumDateString(dtstart, timezone2show)
finalString += getTimeAndTimezone()
}
}
} else {
when {
localStart.year == localNow.year && localStart.month == localNow.month && localStart.dayOfMonth == localNow.dayOfMonth -> {
finalString += context.getString(R.string.list_date_today)
finalString += getTimeAndTimezone()
}
localStart.year == localTomorrow.year && localStart.month == localTomorrow.month && localStart.dayOfMonth == localTomorrow.dayOfMonth -> {
finalString += context.getString(R.string.list_date_tomorrow)
finalString += getTimeAndTimezone()
}
else -> {
finalString += DateTimeUtils.convertLongToMediumDateString(dtstart, timezone2show)
finalString += getTimeAndTimezone()
}
}
}
return finalString
}
fun getDueTextInfo(status: String?, due: Long?, dueTimezone: String?, percent: Int?, daysOnly: Boolean = false, context: Context): String {
if(percent == 100 || status == Status.COMPLETED.status)
return context.getString(R.string.completed)
if(due == null)
return context.getString(R.string.list_due_without)
val settingsStateHolder = SettingsStateHolder(context)
val timezone2show =
if(dueTimezone == TZ_ALLDAY || dueTimezone == null || settingsStateHolder.settingDisplayTimezone.value == DropdownSettingOption.DISPLAY_TIMEZONE_ORIGINAL)
dueTimezone
else
null
val localNow = ZonedDateTime.now()
val localTomorrow = localNow.plusDays(1)
val localDue = DateTimeUtils.getZonedDateTimeInLocalTZ(due, dueTimezone) ?: return ""
var finalString = ""
fun getTimeAndTimezone(): String {
var timeAndTimezone = ""
if(timezone2show != TZ_ALLDAY && !daysOnly)
timeAndTimezone += " ${DateTimeUtils.convertLongToShortTimeString(due, timezone2show)}"
if(timezone2show != null && timezone2show != TZ_ALLDAY && !daysOnly)
timeAndTimezone += " ${requireTzId(timezone2show).id}"
return timeAndTimezone
}
when {
localDue.year == localNow.year && localDue.month == localNow.month && localDue.dayOfMonth == localNow.dayOfMonth && (daysOnly || timezone2show == TZ_ALLDAY) -> finalString += context.getString(R.string.list_due_today)
ChronoUnit.MINUTES.between(localNow, localDue) < 0L -> finalString += context.getString(R.string.list_due_overdue)
ChronoUnit.HOURS.between(localNow, localDue) < 1L -> finalString += context.getString(R.string.list_due_shortly)
localDue.year == localNow.year && localDue.month == localNow.month && localDue.dayOfMonth == localNow.dayOfMonth -> finalString += context.getString(R.string.list_due_inXhours, ChronoUnit.HOURS.between(localNow, localDue))
localDue.year == localTomorrow.year && localDue.month == localTomorrow.month && localDue.dayOfMonth == localTomorrow.dayOfMonth -> {
finalString += context.getString(R.string.list_due_tomorrow)
finalString += getTimeAndTimezone()
}
ChronoUnit.DAYS.between(localNow, localDue) < 6 -> {
finalString += context.getString(R.string.list_due_on_weekday, localDue.dayOfWeek.getDisplayName(TextStyle.FULL, Locale.getDefault()))
finalString += getTimeAndTimezone()
}
else -> {
finalString += DateTimeUtils.convertLongToMediumDateString(due, timezone2show)
finalString += getTimeAndTimezone()
}
}
return finalString
}
fun getAsRecurId(datetime: Long, recuridTimezone: String?) = when {
recuridTimezone == JtxContract.JtxICalObject.TZ_ALLDAY -> Date(datetime).toString()
recuridTimezone == TimeZone.getTimeZone("UTC").id -> DateTime(datetime).apply { this.isUtc = true }.toString()
recuridTimezone.isNullOrEmpty() -> DateTime(datetime).apply { this.isUtc = false }.toString()
else -> DateTime(datetime).apply { this.timeZone = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(recuridTimezone) }.toString()
}
}
fun applyContentValues(values: ContentValues): ICalObject {
values.getAsString(COLUMN_COMPONENT)?.let { component -> this.component = component }
values.getAsString(COLUMN_SUMMARY)?.let { summary -> this.summary = summary }
values.getAsString(COLUMN_DESCRIPTION)
?.let { description -> this.description = description }
values.getAsLong(COLUMN_DTSTART)?.let { dtstart ->
this.dtstart = dtstart
}
values.getAsString(COLUMN_DTSTART_TIMEZONE)?.let { dtstartTimezone ->
this.dtstartTimezone = getValidTimezoneOrNull(dtstartTimezone)
}
values.getAsLong(COLUMN_DTEND)?.let { dtend -> this.dtend = dtend }
values.getAsString(COLUMN_DTEND_TIMEZONE)?.let { dtendTimezone ->
this.dtendTimezone = getValidTimezoneOrNull(dtendTimezone)
}
values.getAsString(COLUMN_STATUS)?.let { status -> this.status = status }
values.getAsString(COLUMN_EXTENDED_STATUS)?.let { xstatus -> this.xstatus = xstatus }
values.getAsString(COLUMN_CLASSIFICATION)
?.let { classification -> this.classification = classification }
values.getAsString(COLUMN_URL)?.let { url -> this.url = url }
values.getAsString(COLUMN_CONTACT)?.let { contact -> this.contact = contact }
values.getAsDouble(COLUMN_GEO_LAT)?.let { geoLat -> this.geoLat = geoLat }
values.getAsDouble(COLUMN_GEO_LONG)?.let { geoLong -> this.geoLong = geoLong }
values.getAsString(COLUMN_LOCATION)?.let { location -> this.location = location }
values.getAsString(COLUMN_LOCATION_ALTREP)?.let { locationAltrep -> this.locationAltrep = locationAltrep }
values.getAsInteger(COLUMN_GEOFENCE_RADIUS)?.let { geofenceRadius -> this.geofenceRadius = geofenceRadius }
values.getAsInteger(COLUMN_PERCENT)?.let { percent ->
if(percent in 1..100)
this.percent = percent
else
this.percent = null }
values.getAsInteger(COLUMN_PRIORITY)?.let { priority -> this.priority = priority }
values.getAsLong(COLUMN_DUE)?.let { due -> this.due = due }
values.getAsString(COLUMN_DUE_TIMEZONE)
?.let { dueTimezone -> this.dueTimezone = getValidTimezoneOrNull(dueTimezone) }
values.getAsLong(COLUMN_COMPLETED)?.let { completed -> this.completed = completed }
values.getAsString(COLUMN_COMPLETED_TIMEZONE)
?.let { completedTimezone -> this.completedTimezone = getValidTimezoneOrNull(completedTimezone) }
values.getAsString(COLUMN_DURATION)?.let { duration -> this.duration = duration }
values.getAsString(COLUMN_RRULE)?.let { rrule -> this.rrule = rrule }
values.getAsString(COLUMN_RDATE)?.let { rdate -> this.rdate = rdate }
values.getAsString(COLUMN_EXDATE)?.let { exdate -> this.exdate = exdate }
values.getAsString(COLUMN_RECURID)?.let { recurid -> this.recurid = recurid }
values.getAsString(COLUMN_RECURID_TIMEZONE)?.let { recuridTimezone -> this.recuridTimezone = recuridTimezone }
values.getAsString(COLUMN_RSTATUS)?.let { rstatus -> this.rstatus = rstatus }
values.getAsString(COLUMN_UID)?.let { uid -> this.uid = uid }
values.getAsLong(COLUMN_CREATED)?.let { created -> this.created = created }
values.getAsLong(COLUMN_DTSTAMP)?.let { dtstamp -> this.dtstamp = dtstamp }
values.getAsLong(COLUMN_LAST_MODIFIED)
?.let { lastModified -> this.lastModified = lastModified }
values.getAsLong(COLUMN_SEQUENCE)?.let { sequence -> this.sequence = sequence }
values.getAsInteger(COLUMN_COLOR)?.let { color -> this.color = color }
//values.getAsString(COLUMN_OTHER)?.let { other -> this.other = other }
values.getAsLong(COLUMN_ICALOBJECT_COLLECTIONID)
?.let { collectionId -> this.collectionId = collectionId }
values.getAsString(COLUMN_DIRTY)?.let { dirty -> this.dirty = dirty == "1" || dirty == "true" }
values.getAsString(COLUMN_DELETED)?.let { deleted -> this.deleted = deleted == "1" || deleted == "true" }
values.getAsString(COLUMN_FILENAME)?.let { fileName -> this.fileName = fileName }
values.getAsString(COLUMN_ETAG)?.let { eTag -> this.eTag = eTag }
values.getAsString(COLUMN_SCHEDULETAG)
?.let { scheduleTag -> this.scheduleTag = scheduleTag }
values.getAsInteger(COLUMN_FLAGS)?.let { flags -> this.flags = flags }
if (this.component == Component.VJOURNAL.name && this.dtstart != null)
this.module = Module.JOURNAL.name
else if (this.component == Component.VJOURNAL.name && this.dtstart == null)
this.module = Module.NOTE.name
else if (this.component == Component.VTODO.name)
this.module = Module.TODO.name
else
throw IllegalArgumentException("Unsupported component: ${this.component}. Supported components: ${Component.entries.toTypedArray()}.")
if(recurid != null && sequence <= 0)
sequence = 1 // mark changed instances with a sequence if missing!
return this
}
fun setUpdatedProgress(newPercent: Int?, keepInSync: Boolean) {
percent = if(newPercent == 0) null else newPercent
if(keepInSync) {
status = when (newPercent) {
100 -> Status.COMPLETED.status
in 1..99 -> Status.IN_PROCESS.status
else -> Status.NEEDS_ACTION.status
}
if (completed == null && percent == 100) {
completedTimezone = dueTimezone ?: dtstartTimezone
completed = if (completedTimezone == TZ_ALLDAY)
DateTimeUtils.getTodayAsLong()
else
System.currentTimeMillis()
} else if (completed != null && percent != 100) {
completed = null
completedTimezone = null
}
}
makeDirty()
return
}
/**
* Sets the dirty flag, updates sequence and lastModified value, makes recurring entry an exception
*/
fun makeDirty() {
lastModified = System.currentTimeMillis()
sequence += 1
dirty = true
}
/**
* @return a Recur Object based on the given rrule or null
*/
fun getRecur(): Recur? {
if(this.rrule.isNullOrEmpty())
return null
return try {
Recur(this.rrule)
} catch (e: ParseException) {
Log.w("getRrule", "Illegal representation of UNTIL\n$e")
null
} catch (e: IllegalArgumentException) {
Log.w("getRrule", "Unrecognized rrule\n$e")
null
}
}
fun getInstancesFromRrule(): List<Long> {
if (rrule.isNullOrEmpty() || dtstart == null)
return emptyList()
try {
val calComponent = when (component) {
JtxContract.JtxICalObject.Component.VTODO.name -> VToDo(true /* generates DTSTAMP */)
JtxContract.JtxICalObject.Component.VJOURNAL.name -> VJournal(true /* generates DTSTAMP */)
else -> return emptyList()
}
val props = calComponent.properties
dtstart?.let {