Add support for all day events and add location in the CalendarEventSpec

Further: fix the hashCode method to properly deal nulls fields.
master
Daniele Gobbetti 2017-04-19 17:44:02 +02:00
parent 546b68ad2d
commit 3ef5f5b811
3 changed files with 36 additions and 13 deletions

View File

@ -25,7 +25,9 @@ import android.widget.Toast;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Calendar;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List; import java.util.List;
@ -173,9 +175,19 @@ public class CalendarReceiver extends BroadcastReceiver {
CalendarEventSpec calendarEventSpec = new CalendarEventSpec(); CalendarEventSpec calendarEventSpec = new CalendarEventSpec();
calendarEventSpec.id = i; calendarEventSpec.id = i;
calendarEventSpec.title = calendarEvent.getTitle(); calendarEventSpec.title = calendarEvent.getTitle();
calendarEventSpec.allDay = calendarEvent.isAllDay();
calendarEventSpec.timestamp = calendarEvent.getBeginSeconds(); calendarEventSpec.timestamp = calendarEvent.getBeginSeconds();
//calendarEventSpec.durationInSeconds = calendarEvent.getDurationSeconds(); //FIXME: leads to problems right now //calendarEventSpec.durationInSeconds = calendarEvent.getDurationSeconds(); //FIXME: leads to problems right now
if (calendarEvent.isAllDay()) {
//force the all day events to begin at midnight and last a whole day
Calendar c = GregorianCalendar.getInstance();
c.setTimeInMillis(calendarEvent.getBegin());
c.set(Calendar.HOUR, 0);
calendarEventSpec.timestamp = (int) (c.getTimeInMillis() / 1000);
//calendarEventSpec.durationInSeconds = 24 * 60 *60; //TODO: commented because it is commented above
}
calendarEventSpec.description = calendarEvent.getDescription(); calendarEventSpec.description = calendarEvent.getDescription();
calendarEventSpec.location = calendarEvent.getLocation();
calendarEventSpec.type = CalendarEventSpec.TYPE_UNKNOWN; calendarEventSpec.type = CalendarEventSpec.TYPE_UNKNOWN;
if (syncState == EventState.NEEDS_UPDATE) { if (syncState == EventState.NEEDS_UPDATE) {
GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_UNKNOWN, i); GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_UNKNOWN, i);

View File

@ -27,4 +27,6 @@ public class CalendarEventSpec {
public int durationInSeconds; public int durationInSeconds;
public String title; public String title;
public String description; public String description;
public String location;
public boolean allDay;
} }

View File

@ -21,7 +21,6 @@ import android.content.ContentUris;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Instances; import android.provider.CalendarContract.Instances;
import java.util.ArrayList; import java.util.ArrayList;
@ -48,7 +47,8 @@ public class CalendarEvents {
Instances.TITLE, Instances.TITLE,
Instances.DESCRIPTION, Instances.DESCRIPTION,
Instances.EVENT_LOCATION, Instances.EVENT_LOCATION,
Instances.CALENDAR_DISPLAY_NAME Instances.CALENDAR_DISPLAY_NAME,
Instances.ALL_DAY
}; };
private static final int lookahead_days = 7; private static final int lookahead_days = 7;
@ -63,16 +63,16 @@ public class CalendarEvents {
private boolean fetchSystemEvents(Context mContext) { private boolean fetchSystemEvents(Context mContext) {
Calendar cal = GregorianCalendar.getInstance(); Calendar cal = GregorianCalendar.getInstance();
Long dtStart = cal.getTime().getTime(); Long dtStart = cal.getTimeInMillis();
cal.add(Calendar.DATE, lookahead_days); cal.add(Calendar.DATE, lookahead_days);
Long dtEnd = cal.getTime().getTime(); Long dtEnd = cal.getTimeInMillis();
Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI.buildUpon(); Uri.Builder eventsUriBuilder = Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(eventsUriBuilder, dtStart); ContentUris.appendId(eventsUriBuilder, dtStart);
ContentUris.appendId(eventsUriBuilder, dtEnd); ContentUris.appendId(eventsUriBuilder, dtEnd);
Uri eventsUri = eventsUriBuilder.build(); Uri eventsUri = eventsUriBuilder.build();
try (Cursor evtCursor = mContext.getContentResolver().query(eventsUri, EVENT_INSTANCE_PROJECTION, null, null, CalendarContract.Instances.BEGIN + " ASC")) { try (Cursor evtCursor = mContext.getContentResolver().query(eventsUri, EVENT_INSTANCE_PROJECTION, null, null, Instances.BEGIN + " ASC")) {
if (evtCursor == null || evtCursor.getCount() == 0) { if (evtCursor == null || evtCursor.getCount() == 0) {
return false; return false;
} }
@ -84,7 +84,8 @@ public class CalendarEvents {
evtCursor.getString(4), evtCursor.getString(4),
evtCursor.getString(5), evtCursor.getString(5),
evtCursor.getString(6), evtCursor.getString(6),
evtCursor.getString(7) evtCursor.getString(7),
!evtCursor.getString(8).equals("0")
); );
calendarEventList.add(calEvent); calendarEventList.add(calEvent);
} }
@ -100,8 +101,9 @@ public class CalendarEvents {
private String description; private String description;
private String location; private String location;
private String calName; private String calName;
private boolean allDay;
public CalendarEvent(long begin, long end, long id, String title, String description, String location, String calName) { public CalendarEvent(long begin, long end, long id, String title, String description, String location, String calName, boolean allDay) {
this.begin = begin; this.begin = begin;
this.end = end; this.end = end;
this.id = id; this.id = id;
@ -109,6 +111,7 @@ public class CalendarEvents {
this.description = description; this.description = description;
this.location = location; this.location = location;
this.calName = calName; this.calName = calName;
this.allDay = allDay;
} }
public long getBegin() { public long getBegin() {
@ -156,6 +159,10 @@ public class CalendarEvents {
return calName; return calName;
} }
public boolean isAllDay() {
return allDay;
}
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (other instanceof CalendarEvent) { if (other instanceof CalendarEvent) {
@ -166,7 +173,8 @@ public class CalendarEvents {
Objects.equals(this.getLocation(), e.getLocation()) && Objects.equals(this.getLocation(), e.getLocation()) &&
Objects.equals(this.getDescription(), e.getDescription()) && Objects.equals(this.getDescription(), e.getDescription()) &&
(this.getEnd() == e.getEnd()) && (this.getEnd() == e.getEnd()) &&
Objects.equals(this.getCalName(), e.getCalName()); Objects.equals(this.getCalName(), e.getCalName()) &&
(this.isAllDay() == e.isAllDay());
} else { } else {
return false; return false;
} }
@ -175,12 +183,13 @@ public class CalendarEvents {
@Override @Override
public int hashCode() { public int hashCode() {
int result = (int) id; int result = (int) id;
result = 31 * result + title.hashCode(); result = 31 * result + Objects.hash(title);
result = 31 * result + Long.valueOf(begin).hashCode(); result = 31 * result + Long.valueOf(begin).hashCode();
result = 31 * result + location.hashCode(); result = 31 * result + Objects.hash(location);
result = 31 * result + description.hashCode(); result = 31 * result + Objects.hash(description);
result = 31 * result + Long.valueOf(end).hashCode(); result = 31 * result + Long.valueOf(end).hashCode();
result = 31 * result + calName.hashCode(); result = 31 * result + Objects.hash(calName);
result = 31 * result + Boolean.valueOf(allDay).hashCode();
return result; return result;
} }
} }