use of org.bouncycastle.asn1.x509.Time in project Etar-Calendar by Etar-Group.
the class EditEventHelper method constructDefaultStartTime.
/**
* When we aren't given an explicit start time, we default to the next
* upcoming half hour. So, for example, 5:01 -> 5:30, 5:30 -> 6:00, etc.
*
* @return a UTC time in milliseconds representing the next upcoming half
* hour
*/
protected long constructDefaultStartTime(long now) {
Time defaultStart = new Time();
defaultStart.set(now);
defaultStart.setSecond(0);
defaultStart.setMinute(30);
long defaultStartMillis = defaultStart.toMillis();
if (now < defaultStartMillis) {
return defaultStartMillis;
} else {
return defaultStartMillis + 30 * DateUtils.MINUTE_IN_MILLIS;
}
}
use of org.bouncycastle.asn1.x509.Time in project Etar-Calendar by Etar-Group.
the class EditEventHelper method updateRecurrenceRule.
/**
* Uses the recurrence selection and the model data to build an rrule and
* write it to the model.
*
* @param selection the type of rrule
* @param model The event to update
* @param weekStart the week start day, specified as java.util.Calendar
* constants
*/
static void updateRecurrenceRule(int selection, CalendarEventModel model, int weekStart) {
// Make sure we don't have any leftover data from the previous setting
EventRecurrence eventRecurrence = new EventRecurrence();
if (selection == DOES_NOT_REPEAT) {
model.mRrule = null;
return;
} else if (selection == REPEATS_CUSTOM) {
// Keep custom recurrence as before.
return;
} else if (selection == REPEATS_DAILY) {
eventRecurrence.freq = EventRecurrence.DAILY;
} else if (selection == REPEATS_EVERY_WEEKDAY) {
eventRecurrence.freq = EventRecurrence.WEEKLY;
int dayCount = 5;
int[] byday = new int[dayCount];
int[] bydayNum = new int[dayCount];
byday[0] = EventRecurrence.MO;
byday[1] = EventRecurrence.TU;
byday[2] = EventRecurrence.WE;
byday[3] = EventRecurrence.TH;
byday[4] = EventRecurrence.FR;
for (int day = 0; day < dayCount; day++) {
bydayNum[day] = 0;
}
eventRecurrence.byday = byday;
eventRecurrence.bydayNum = bydayNum;
eventRecurrence.bydayCount = dayCount;
} else if (selection == REPEATS_WEEKLY_ON_DAY) {
eventRecurrence.freq = EventRecurrence.WEEKLY;
int[] days = new int[1];
int dayCount = 1;
int[] dayNum = new int[dayCount];
Time startTime = new Time(model.mTimezone);
startTime.set(model.mStart);
days[0] = EventRecurrence.timeDay2Day(startTime.getWeekDay());
// not sure why this needs to be zero, but set it for now.
dayNum[0] = 0;
eventRecurrence.byday = days;
eventRecurrence.bydayNum = dayNum;
eventRecurrence.bydayCount = dayCount;
} else if (selection == REPEATS_MONTHLY_ON_DAY) {
eventRecurrence.freq = EventRecurrence.MONTHLY;
eventRecurrence.bydayCount = 0;
eventRecurrence.bymonthdayCount = 1;
int[] bymonthday = new int[1];
Time startTime = new Time(model.mTimezone);
startTime.set(model.mStart);
bymonthday[0] = startTime.getDay();
eventRecurrence.bymonthday = bymonthday;
} else if (selection == REPEATS_MONTHLY_ON_DAY_COUNT) {
eventRecurrence.freq = EventRecurrence.MONTHLY;
eventRecurrence.bydayCount = 1;
eventRecurrence.bymonthdayCount = 0;
int[] byday = new int[1];
int[] bydayNum = new int[1];
Time startTime = new Time(model.mTimezone);
startTime.set(model.mStart);
// Compute the week number (for example, the "2nd" Monday)
int dayCount = 1 + ((startTime.getDay() - 1) / 7);
if (dayCount == 5) {
dayCount = -1;
}
bydayNum[0] = dayCount;
byday[0] = EventRecurrence.timeDay2Day(startTime.getWeekDay());
eventRecurrence.byday = byday;
eventRecurrence.bydayNum = bydayNum;
} else if (selection == REPEATS_YEARLY) {
eventRecurrence.freq = EventRecurrence.YEARLY;
}
// Set the week start day.
eventRecurrence.wkst = EventRecurrence.calendarDay2Day(weekStart);
model.mRrule = eventRecurrence.toString();
}
use of org.bouncycastle.asn1.x509.Time in project Etar-Calendar by Etar-Group.
the class EditEventHelper method getContentValuesFromModel.
/**
* Goes through an event model and fills in content values for saving. This
* method will perform the initial collection of values from the model and
* put them into a set of ContentValues. It performs some basic work such as
* fixing the time on allDay events and choosing whether to use an rrule or
* dtend.
*
* @param model The complete model of the event you want to save
* @return values
*/
ContentValues getContentValuesFromModel(CalendarEventModel model) {
String title = model.mTitle;
boolean isAllDay = model.mAllDay;
String rrule = model.mRrule;
String timezone = model.mTimezone;
if (timezone == null) {
timezone = TimeZone.getDefault().getID();
}
Time startTime = new Time(timezone);
Time endTime = new Time(timezone);
startTime.set(model.mStart);
endTime.set(model.mEnd);
offsetStartTimeIfNecessary(startTime, endTime, rrule, model);
ContentValues values = new ContentValues();
long startMillis;
long endMillis;
long calendarId = model.mCalendarId;
if (isAllDay) {
// Reset start and end time, ensure at least 1 day duration, and set
// the timezone to UTC, as required for all-day events.
timezone = Time.TIMEZONE_UTC;
startTime.setHour(0);
startTime.setMinute(0);
startTime.setSecond(0);
startTime.setTimezone(timezone);
startMillis = startTime.normalize();
endTime.setHour(0);
endTime.setMinute(0);
endTime.setSecond(0);
endTime.setTimezone(timezone);
endMillis = endTime.normalize();
if (endMillis < startMillis + DateUtils.DAY_IN_MILLIS) {
// EditEventView#fillModelFromUI() should treat this case, but we want to ensure
// the condition anyway.
endMillis = startMillis + DateUtils.DAY_IN_MILLIS;
}
} else {
startMillis = startTime.toMillis();
endMillis = endTime.toMillis();
}
values.put(Events.CALENDAR_ID, calendarId);
values.put(Events.EVENT_TIMEZONE, timezone);
values.put(Events.TITLE, title);
values.put(Events.ALL_DAY, isAllDay ? 1 : 0);
values.put(Events.DTSTART, startMillis);
values.put(Events.RRULE, rrule);
if (!TextUtils.isEmpty(rrule)) {
addRecurrenceRule(values, model);
} else {
values.put(Events.DURATION, (String) null);
values.put(Events.DTEND, endMillis);
}
if (model.mDescription != null) {
values.put(Events.DESCRIPTION, model.mDescription.trim());
} else {
values.put(Events.DESCRIPTION, (String) null);
}
if (model.mLocation != null) {
values.put(Events.EVENT_LOCATION, model.mLocation.trim());
} else {
values.put(Events.EVENT_LOCATION, (String) null);
}
values.put(Events.AVAILABILITY, model.mAvailability);
values.put(Events.HAS_ATTENDEE_DATA, model.mHasAttendeeData ? 1 : 0);
int accessLevel = model.mAccessLevel;
values.put(Events.ACCESS_LEVEL, accessLevel);
values.put(Events.STATUS, model.mEventStatus);
if (model.isEventColorInitialized()) {
if (model.getEventColor() == model.getCalendarColor()) {
values.put(Events.EVENT_COLOR_KEY, NO_EVENT_COLOR);
} else {
values.put(Events.EVENT_COLOR_KEY, model.getEventColorKey());
}
}
return values;
}
use of org.bouncycastle.asn1.x509.Time in project Etar-Calendar by Etar-Group.
the class EditEventHelper method updatePastEvents.
/**
* Prepares an update to the original event so it stops where the new series
* begins. When we update 'this and all following' events we need to change
* the original event to end before a new series starts. This creates an
* update to the old event's rrule to do that.
*<p>
* If the event's recurrence rule has a COUNT, we also need to reduce the count in the
* RRULE for the exception event.
*
* @param ops The list of operations to add the update to
* @param originalModel The original event that we're updating
* @param endTimeMillis The time before which the event must end (i.e. the start time of the
* exception event instance).
* @return A replacement exception recurrence rule.
*/
public String updatePastEvents(ArrayList<ContentProviderOperation> ops, CalendarEventModel originalModel, long endTimeMillis) {
boolean origAllDay = originalModel.mAllDay;
String origRrule = originalModel.mRrule;
String newRrule = origRrule;
EventRecurrence origRecurrence = new EventRecurrence();
origRecurrence.parse(origRrule);
// Get the start time of the first instance in the original recurrence.
long startTimeMillis = originalModel.mStart;
Time dtstart = new Time();
dtstart.setTimezone(originalModel.mTimezone);
dtstart.set(startTimeMillis);
ContentValues updateValues = new ContentValues();
if (origRecurrence.count > 0) {
/*
* Generate the full set of instances for this recurrence, from the first to the
* one just before endTimeMillis. The list should never be empty, because this method
* should not be called for the first instance. All we're really interested in is
* the *number* of instances found.
*
* TODO: the model assumes RRULE and ignores RDATE, EXRULE, and EXDATE. For the
* current environment this is reasonable, but that may not hold in the future.
*
* TODO: if COUNT is 1, should we convert the event to non-recurring? e.g. we
* do an "edit this and all future events" on the 2nd instances.
*/
RecurrenceSet recurSet = new RecurrenceSet(originalModel.mRrule, null, null, null);
RecurrenceProcessor recurProc = new RecurrenceProcessor();
long[] recurrences;
try {
recurrences = recurProc.expand(dtstart, recurSet, startTimeMillis, endTimeMillis);
} catch (DateException de) {
throw new RuntimeException(de);
}
if (recurrences.length == 0) {
throw new RuntimeException("can't use this method on first instance");
}
EventRecurrence excepRecurrence = new EventRecurrence();
// TODO: add+use a copy constructor instead
excepRecurrence.parse(origRrule);
excepRecurrence.count -= recurrences.length;
newRrule = excepRecurrence.toString();
origRecurrence.count = recurrences.length;
} else {
// The "until" time must be in UTC time in order for Google calendar
// to display it properly. For all-day events, the "until" time string
// must include just the date field, and not the time field. The
// repeating events repeat up to and including the "until" time.
Time untilTime = new Time();
untilTime.setTimezone(Time.TIMEZONE_UTC);
// Subtract one second from the old begin time to get the new
// "until" time.
// subtract one second (1000 millis)
untilTime.set(endTimeMillis - 1000);
if (origAllDay) {
untilTime.setHour(0);
untilTime.setMinute(0);
untilTime.setSecond(0);
untilTime.setAllDay(true);
untilTime.normalize();
// This should no longer be necessary -- DTSTART should already be in the correct
// format for an all-day event.
dtstart.setHour(0);
dtstart.setMinute(0);
dtstart.setSecond(0);
dtstart.setAllDay(true);
dtstart.setTimezone(Time.TIMEZONE_UTC);
}
origRecurrence.until = untilTime.format2445();
}
updateValues.put(Events.RRULE, origRecurrence.toString());
updateValues.put(Events.DTSTART, dtstart.normalize());
ContentProviderOperation.Builder b = ContentProviderOperation.newUpdate(Uri.parse(originalModel.mUri)).withValues(updateValues);
ops.add(b.build());
return newRrule;
}
use of org.bouncycastle.asn1.x509.Time in project Etar-Calendar by Etar-Group.
the class EditEventHelper method checkTimeDependentFields.
// TODO think about how useful this is. Probably check if our event has
// changed early on and either update all or nothing. Should still do the if
// MODIFY_ALL bit.
void checkTimeDependentFields(CalendarEventModel originalModel, CalendarEventModel model, ContentValues values, int modifyWhich) {
long oldBegin = model.mOriginalStart;
long oldEnd = model.mOriginalEnd;
boolean oldAllDay = originalModel.mAllDay;
String oldRrule = originalModel.mRrule;
String oldTimezone = originalModel.mTimezone;
long newBegin = model.mStart;
long newEnd = model.mEnd;
boolean newAllDay = model.mAllDay;
String newRrule = model.mRrule;
String newTimezone = model.mTimezone;
// If none of the time-dependent fields changed, then remove them.
if (oldBegin == newBegin && oldEnd == newEnd && oldAllDay == newAllDay && TextUtils.equals(oldRrule, newRrule) && TextUtils.equals(oldTimezone, newTimezone)) {
values.remove(Events.DTSTART);
values.remove(Events.DTEND);
values.remove(Events.DURATION);
values.remove(Events.ALL_DAY);
values.remove(Events.RRULE);
values.remove(Events.EVENT_TIMEZONE);
return;
}
if (TextUtils.isEmpty(oldRrule) || TextUtils.isEmpty(newRrule)) {
return;
}
// then we leave the DTSTART field alone.
if (modifyWhich == MODIFY_ALL) {
long oldStartMillis = originalModel.mStart;
if (oldBegin != newBegin) {
// The user changed the start time of this event
long offset = newBegin - oldBegin;
oldStartMillis += offset;
}
if (newAllDay) {
Time time = new Time(Time.TIMEZONE_UTC);
time.set(oldStartMillis);
time.setHour(0);
time.setMinute(0);
time.setSecond(0);
oldStartMillis = time.toMillis();
}
values.put(Events.DTSTART, oldStartMillis);
}
}
Aggregations