Skip to content

Commit 702bab9

Browse files
Merge pull request #15870 from nextcloud/processVEvent
Fixing ProcessVEvent
2 parents 6e8f5ec + 33a35a1 commit 702bab9

1 file changed

Lines changed: 26 additions & 42 deletions

File tree

app/src/main/java/third_parties/sufficientlysecure/ProcessVEvent.java

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
import net.fortuna.ical4j.model.Calendar;
3030
import net.fortuna.ical4j.model.DateTime;
31-
import net.fortuna.ical4j.model.Dur;
3231
import net.fortuna.ical4j.model.Parameter;
3332
import net.fortuna.ical4j.model.Property;
3433
import net.fortuna.ical4j.model.component.CalendarComponent;
@@ -48,6 +47,7 @@
4847
import java.time.temporal.TemporalUnit;
4948
import java.util.ArrayList;
5049
import java.util.List;
50+
import java.util.Objects;
5151
import java.util.UUID;
5252

5353
import javax.inject.Inject;
@@ -56,8 +56,6 @@
5656
@SuppressLint("NewApi")
5757
public class ProcessVEvent {
5858
private static final String TAG = "ICS_ProcessVEvent";
59-
60-
private static final Duration ONE_DAY = createDuration("P1D");
6159
private static final Duration ZERO_SECONDS = createDuration("PT0S");
6260

6361
private static final String[] EVENT_QUERY_COLUMNS = new String[]{Events.CALENDAR_ID, Events._ID};
@@ -89,7 +87,7 @@ public Options(Context context) {
8987
}
9088

9189
public List<Integer> getReminders(List<Integer> eventReminders) {
92-
if (eventReminders.size() > 0 && getImportReminders()) {
90+
if (!eventReminders.isEmpty() && getImportReminders()) {
9391
return eventReminders;
9492
}
9593
return mDefaultReminders;
@@ -224,7 +222,7 @@ public void run() throws Exception {
224222
continue;
225223
}
226224

227-
if (Events.UID_2445 != null && !c.containsKey(Events.UID_2445)) {
225+
if (!c.containsKey(Events.UID_2445)) {
228226
// Create a UID for this event to use. We create it here so if
229227
// exported multiple times it will always have the same id.
230228
c.put(Events.UID_2445, generateUid()); // TODO use
@@ -242,7 +240,7 @@ public void run() throws Exception {
242240
continue;
243241
}
244242

245-
final long id = Long.parseLong(uri.getLastPathSegment());
243+
final long id = Long.parseLong(Objects.requireNonNull(uri.getLastPathSegment()));
246244

247245
for (int time : options.getReminders(reminders)) {
248246
cAlarm.put(Reminders.EVENT_ID, id);
@@ -327,7 +325,7 @@ private ContentValues convertToDB(VEvent e, Options options,
327325
c.put(Events.ORGANIZER, mailTo.getTo());
328326
c.put(Events.GUESTS_CAN_MODIFY, 1); // Ensure we can edit if not the organiser
329327
} catch (ParseException ignored) {
330-
Log_OC.e(TAG, "Failed to parse Organiser URI " + uri.toString());
328+
Log_OC.e(TAG, "Failed to parse Organiser URI " + uri);
331329
}
332330
}
333331

@@ -372,24 +370,22 @@ private ContentValues convertToDB(VEvent e, Options options,
372370
}
373371

374372
// Work out availability. This is confusing as FREEBUSY and TRANSP overlap.
375-
if (Events.AVAILABILITY != null) {
376-
int availability = Events.AVAILABILITY_BUSY;
377-
if (hasProperty(e, Property.TRANSP)) {
378-
if (e.getTransparency() == Transp.TRANSPARENT) {
379-
availability = Events.AVAILABILITY_FREE;
380-
}
373+
int availability = Events.AVAILABILITY_BUSY;
374+
if (hasProperty(e, Property.TRANSP)) {
375+
if (e.getTransparency() == Transp.TRANSPARENT) {
376+
availability = Events.AVAILABILITY_FREE;
377+
}
381378

382-
} else if (hasProperty(e, Property.FREEBUSY)) {
383-
FreeBusy fb = (FreeBusy) e.getProperty(Property.FREEBUSY);
384-
FbType fbType = (FbType) fb.getParameter(Parameter.FBTYPE);
385-
if (fbType != null && fbType == FbType.FREE) {
386-
availability = Events.AVAILABILITY_FREE;
387-
} else if (fbType != null && fbType == FbType.BUSY_TENTATIVE) {
388-
availability = Events.AVAILABILITY_TENTATIVE;
389-
}
379+
} else if (hasProperty(e, Property.FREEBUSY)) {
380+
FreeBusy fb = e.getProperty(Property.FREEBUSY);
381+
FbType fbType = fb.getParameter(Parameter.FBTYPE);
382+
if (fbType != null && fbType == FbType.FREE) {
383+
availability = Events.AVAILABILITY_FREE;
384+
} else if (fbType != null && fbType == FbType.BUSY_TENTATIVE) {
385+
availability = Events.AVAILABILITY_TENTATIVE;
390386
}
391-
c.put(Events.AVAILABILITY, availability);
392387
}
388+
c.put(Events.AVAILABILITY, availability);
393389

394390
copyProperty(c, Events.RRULE, e, Property.RRULE);
395391
copyProperty(c, Events.RDATE, e, Property.RDATE);
@@ -402,8 +398,7 @@ private ContentValues convertToDB(VEvent e, Options options,
402398
c.remove(Events.UID_2445);
403399
}
404400

405-
for (Object alarm : e.getAlarms()) {
406-
VAlarm a = (VAlarm) alarm;
401+
for (VAlarm a : e.getAlarms()) {
407402

408403
if (a.getAction() != Action.AUDIO && a.getAction() != Action.DISPLAY) {
409404
continue; // Ignore email and procedure alarms
@@ -419,7 +414,7 @@ private ContentValues convertToDB(VEvent e, Options options,
419414
if (t.getDateTime() != null)
420415
alarmMs = t.getDateTime().getTime(); // Absolute
421416
else if (t.getDuration() != null) {
422-
Related rel = (Related) t.getParameter(Parameter.RELATED);
417+
Related rel = t.getParameter(Parameter.RELATED);
423418
if (rel != null && rel == Related.END)
424419
alarmStartMs = e.getEndDate().getDate().getTime();
425420
alarmMs = alarmStartMs + durationToMs(t.getDuration());
@@ -433,7 +428,7 @@ else if (t.getDuration() != null) {
433428
}
434429
}
435430

436-
if (options.getReminders(reminders).size() > 0) {
431+
if (!options.getReminders(reminders).isEmpty()) {
437432
c.put(Events.HAS_ALARM, 1);
438433
}
439434

@@ -456,16 +451,6 @@ private static long durationToMs(TemporalAmount d) {
456451
return ms;
457452
}
458453

459-
private static long durationToMs(Dur d) {
460-
long ms = 0;
461-
ms += d.getSeconds() * DateUtils.SECOND_IN_MILLIS;
462-
ms += d.getMinutes() * DateUtils.MINUTE_IN_MILLIS;
463-
ms += d.getHours() * DateUtils.HOUR_IN_MILLIS;
464-
ms += d.getDays() * DateUtils.DAY_IN_MILLIS;
465-
ms += d.getWeeks() * DateUtils.WEEK_IN_MILLIS;
466-
return ms;
467-
}
468-
469454
private boolean hasProperty(VEvent e, String name) {
470455
return e.getProperty(name) != null;
471456
}
@@ -523,7 +508,7 @@ private Cursor query(ContentResolver resolver, Options options, ContentValues c)
523508
StringBuilder b = new StringBuilder();
524509
List<String> argsList = new ArrayList<>();
525510

526-
if (options.getKeepUids() && Events.UID_2445 != null && c.containsKey(Events.UID_2445)) {
511+
if (options.getKeepUids() && c.containsKey(Events.UID_2445)) {
527512
// Use our UID to query, either globally or per-calendar unique
528513
if (!options.getGlobalUids()) {
529514
b.append(Events.CALENDAR_ID).append("=? AND ");
@@ -557,7 +542,7 @@ private Cursor query(ContentResolver resolver, Options options, ContentValues c)
557542
return queryEvents(resolver, b, argsList);
558543
}
559544

560-
private void checkTestValue(VEvent e, ContentValues c, String keyValue, String testName) {
545+
private void checkTestValue(ContentValues c, String keyValue, String testName) {
561546
String[] parts = keyValue.split("=");
562547
String key = parts[0];
563548
String expected = parts.length > 1 ? parts[1] : "";
@@ -597,11 +582,10 @@ private void processEventTests(VEvent e, ContentValues c, List<Integer> reminder
597582
}
598583
c.put("reminders", reminderValues.toString());
599584

600-
for (Object o : e.getProperties()) {
601-
Property p = (Property) o;
585+
for (Property p : e.getProperties()) {
602586
switch (p.getName()) {
603587
case "X-TEST-VALUE":
604-
checkTestValue(e, c, p.getValue(), testName.getValue());
588+
checkTestValue(c, p.getValue(), testName.getValue());
605589
break;
606590
case "X-TEST-MIN-VERSION":
607591
final int ver = Integer.parseInt(p.getValue());
@@ -619,7 +603,7 @@ private String generateUid() {
619603
// Generated UIDs take the form <ms>-<uuid>@nextcloud.com.
620604
if (mUidTail == null) {
621605
String uidPid = preferences.getUidPid();
622-
if (uidPid.length() == 0) {
606+
if (uidPid.isEmpty()) {
623607
uidPid = UUID.randomUUID().toString().replace("-", "");
624608
preferences.setUidPid(uidPid);
625609
}

0 commit comments

Comments
 (0)