Created
July 11, 2017 10:18
-
-
Save abhinav272/2b735122e1459ac715627d962449182f to your computer and use it in GitHub Desktop.
ICalParser for parsing ics files. Read ical files(.ics) and provide Content Values for adding it to Android Calendar.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.ContentValues; | |
import android.net.Uri; | |
import android.provider.CalendarContract; | |
import net.fortuna.ical4j.data.CalendarBuilder; | |
import net.fortuna.ical4j.data.ParserException; | |
import net.fortuna.ical4j.model.Calendar; | |
import net.fortuna.ical4j.model.Component; | |
import net.fortuna.ical4j.model.Property; | |
import net.fortuna.ical4j.model.component.Daylight; | |
import net.fortuna.ical4j.model.component.Observance; | |
import net.fortuna.ical4j.model.component.Standard; | |
import net.fortuna.ical4j.model.component.VEvent; | |
import net.fortuna.ical4j.model.component.VTimeZone; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.TimeZone; | |
public class ICALParser { | |
private static final String TAG = "ICALParser"; | |
private Calendar calendar; | |
private VEvent event; | |
private Daylight daylight; | |
private VTimeZone vTimeZone; | |
private Standard standard; | |
public ICALParser(InputStream inputStream) throws IOException, ParserException { | |
CalendarBuilder builder = new CalendarBuilder(); | |
calendar = builder.build(inputStream); | |
event = findFirstEvent(calendar); | |
vTimeZone = findFirstTimeZone(calendar); | |
daylight = findFirstDayLight(vTimeZone); | |
standard = findFirstStandard(vTimeZone); | |
} | |
public ContentValues buildContentValues() { | |
ContentValues values = new ContentValues(); | |
TimeZone timeZone = null; | |
values.put(CalendarContract.Events.HAS_ALARM,1); | |
values.put(CalendarContract.Events.CALENDAR_ID, 1); | |
for (Object o : vTimeZone.getProperties()) { | |
Property property = (Property) o; | |
switch (property.getName()){ | |
case "TZID": | |
values.put(CalendarContract.Events.EVENT_TIMEZONE, DateUtils.getTimeZone(property.getValue()).getID()); | |
values.put(CalendarContract.Events.EVENT_END_TIMEZONE, DateUtils.getTimeZone(property.getValue()).getID()); | |
timeZone = DateUtils.getTimeZone(property.getValue()); | |
break; | |
} | |
} | |
for (Object o : event.getProperties()) { | |
Property property = (Property) o; | |
switch (property.getName()){ | |
case "DTEND": | |
values.put(CalendarContract.Events.DTEND, DateUtils.parseDateForContentValues(property.getValue(), timeZone).getTime()); | |
break; | |
case "DTSTART": | |
values.put(CalendarContract.Events.DTSTART, DateUtils.parseDateForContentValues(property.getValue(), timeZone).getTime()); | |
break; | |
case "DESCRIPTION": | |
values.put(CalendarContract.Events.DESCRIPTION, property.getValue()); | |
break; | |
case "LOCATION": | |
values.put(CalendarContract.Events.EVENT_LOCATION, property.getValue()); | |
break; | |
case "SUMMARY": | |
values.put(CalendarContract.Events.TITLE, property.getValue()); | |
break; | |
} | |
} | |
return values; | |
} | |
public void setReminder(long eventId) { | |
String reminderUriString = "content://com.android.calendar/reminders"; | |
ContentValues reminderValues = new ContentValues(); | |
reminderValues.put(CalendarContract.Reminders.EVENT_ID, eventId); | |
reminderValues.put(CalendarContract.Reminders.MINUTES, Constants.CalendarEvents.REMINDER_MINUTES); | |
reminderValues.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT); | |
Equinox.getInstance().getApplicationContext().getContentResolver().insert(Uri.parse(reminderUriString), reminderValues); | |
} | |
private VEvent findFirstEvent(Calendar calendar){ | |
for (Object o : calendar.getComponents()) { | |
Component c = (Component) o; | |
VEvent e = c instanceof VEvent ? ((VEvent) c) : null; | |
if (e != null) { | |
return e; | |
} | |
} | |
return null; | |
} | |
private Standard findFirstStandard(VTimeZone vTimeZone) { | |
for (Object o : vTimeZone.getObservances()) { | |
Observance c = (Observance) o; | |
Standard s = c instanceof Standard ? ((Standard) c) : null; | |
if (s != null) { | |
return s; | |
} | |
} | |
return null; | |
} | |
private VTimeZone findFirstTimeZone(Calendar calendar) { | |
for (Object o : calendar.getComponents()) { | |
Component c = (Component) o; | |
VTimeZone vt = c instanceof VTimeZone ? ((VTimeZone) c) : null; | |
if (vt != null) { | |
return vt; | |
} | |
} | |
return null; | |
} | |
private Daylight findFirstDayLight(VTimeZone vTimeZone) { | |
for (Object o : vTimeZone.getObservances()) { | |
Observance c = (Observance) o; | |
Daylight d = c instanceof Daylight ? ((Daylight) c) : null; | |
if (d != null) { | |
return d; | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment