2015-12-16 14:45:01 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.model;
|
|
|
|
|
|
|
|
import android.content.ContentUris;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.provider.CalendarContract;
|
|
|
|
import android.provider.CalendarContract.Instances;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.GregorianCalendar;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class CalendarEvents {
|
|
|
|
|
|
|
|
// needed for pebble: time, duration, layout, reminders, actions
|
|
|
|
// layout: type, title, subtitle, body (max 512), tinyIcon, smallIcon, largeIcon
|
|
|
|
//further: primaryColor, secondaryColor, backgroundColor, headings, paragraphs, lastUpdated
|
|
|
|
// taken from: https://developer.getpebble.com/guides/timeline/pin-structure/
|
|
|
|
|
|
|
|
// needed for miband:
|
|
|
|
// time
|
|
|
|
|
2016-02-29 20:54:39 +01:00
|
|
|
private static final String[] EVENT_INSTANCE_PROJECTION = new String[]{
|
2015-12-16 14:45:01 +01:00
|
|
|
Instances._ID,
|
|
|
|
Instances.BEGIN,
|
|
|
|
Instances.END,
|
|
|
|
Instances.EVENT_ID,
|
|
|
|
Instances.TITLE,
|
|
|
|
Instances.DESCRIPTION,
|
|
|
|
Instances.EVENT_LOCATION,
|
|
|
|
Instances.CALENDAR_DISPLAY_NAME
|
|
|
|
};
|
|
|
|
|
|
|
|
private static final int lookahead_days = 7;
|
|
|
|
|
|
|
|
private List<CalendarEvent> calendarEventList = new ArrayList<CalendarEvent>();
|
|
|
|
|
|
|
|
public List<CalendarEvent> getCalendarEventList(Context mContext) {
|
|
|
|
fetchSystemEvents(mContext);
|
|
|
|
return calendarEventList;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean fetchSystemEvents(Context mContext) {
|
|
|
|
|
|
|
|
Calendar cal = GregorianCalendar.getInstance();
|
|
|
|
Long dtStart = cal.getTime().getTime();
|
|
|
|
cal.add(Calendar.DATE, lookahead_days);
|
|
|
|
Long dtEnd = cal.getTime().getTime();
|
|
|
|
|
|
|
|
Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI.buildUpon();
|
|
|
|
ContentUris.appendId(eventsUriBuilder, dtStart);
|
|
|
|
ContentUris.appendId(eventsUriBuilder, dtEnd);
|
|
|
|
Uri eventsUri = eventsUriBuilder.build();
|
|
|
|
|
2016-03-08 23:48:31 +01:00
|
|
|
try (Cursor evtCursor = mContext.getContentResolver().query(eventsUri, EVENT_INSTANCE_PROJECTION, null, null, CalendarContract.Instances.BEGIN + " ASC")) {
|
|
|
|
if (evtCursor == null || evtCursor.getCount() == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while (evtCursor.moveToNext()) {
|
2015-12-16 14:45:01 +01:00
|
|
|
CalendarEvent calEvent = new CalendarEvent(
|
|
|
|
evtCursor.getLong(1),
|
|
|
|
evtCursor.getLong(2),
|
|
|
|
evtCursor.getLong(3),
|
|
|
|
evtCursor.getString(4),
|
|
|
|
evtCursor.getString(5),
|
|
|
|
evtCursor.getString(6),
|
|
|
|
evtCursor.getString(7)
|
2016-02-29 20:54:39 +01:00
|
|
|
);
|
2015-12-16 14:45:01 +01:00
|
|
|
calendarEventList.add(calEvent);
|
2016-03-08 23:48:31 +01:00
|
|
|
}
|
2015-12-16 14:45:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class CalendarEvent {
|
|
|
|
private long begin;
|
|
|
|
private long end;
|
|
|
|
private long id;
|
|
|
|
private String title;
|
|
|
|
private String description;
|
|
|
|
private String location;
|
|
|
|
private String calName;
|
|
|
|
|
|
|
|
public CalendarEvent(long begin, long end, long id, String title, String description, String location, String calName) {
|
|
|
|
this.begin = begin;
|
|
|
|
this.end = end;
|
|
|
|
this.id = id;
|
|
|
|
this.title = title;
|
|
|
|
this.description = description;
|
|
|
|
this.location = location;
|
|
|
|
this.calName = calName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getBegin() {
|
|
|
|
return begin;
|
|
|
|
}
|
|
|
|
|
2015-12-19 20:26:43 +01:00
|
|
|
public int getBeginSeconds() {
|
2016-02-29 20:54:39 +01:00
|
|
|
return (int) (begin / 1000);
|
2015-12-19 20:26:43 +01:00
|
|
|
}
|
|
|
|
|
2015-12-16 14:45:01 +01:00
|
|
|
public long getEnd() {
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getDuration() {
|
|
|
|
return end - begin;
|
|
|
|
}
|
|
|
|
|
2015-12-19 20:26:43 +01:00
|
|
|
public int getDurationSeconds() {
|
2016-02-29 20:54:39 +01:00
|
|
|
return (int) ((getDuration()) / 1000);
|
2015-12-19 20:26:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public short getDurationMinutes() {
|
2016-02-29 20:54:39 +01:00
|
|
|
return (short) (getDurationSeconds() / 60);
|
2015-12-19 20:26:43 +01:00
|
|
|
}
|
|
|
|
|
2015-12-16 14:45:01 +01:00
|
|
|
|
|
|
|
public long getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getTitle() {
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getDescription() {
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getLocation() {
|
|
|
|
return location;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getCalName() {
|
|
|
|
return calName;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|