use of com.android.calendar.icalendar.Organizer in project Etar-Calendar by Etar-Group.
the class EventInfoFragment method shareEvent.
/**
* Generates an .ics formatted file with the event info and launches intent chooser to
* share said file
*/
private void shareEvent(ShareType type) {
// Create the respective ICalendar objects from the event info
VCalendar calendar = new VCalendar();
calendar.addProperty(VCalendar.VERSION, "2.0");
calendar.addProperty(VCalendar.PRODID, VCalendar.PRODUCT_IDENTIFIER);
calendar.addProperty(VCalendar.CALSCALE, "GREGORIAN");
calendar.addProperty(VCalendar.METHOD, "REQUEST");
VEvent event = new VEvent();
mEventCursor.moveToFirst();
// Add event start and end datetime
if (!mAllDay) {
String eventTimeZone = mEventCursor.getString(EVENT_INDEX_EVENT_TIMEZONE);
event.addEventStart(mStartMillis, eventTimeZone);
event.addEventEnd(mEndMillis, eventTimeZone);
} else {
// All-day events' start and end time are stored as UTC.
// Treat the event start and end time as being in the local time zone and convert them
// to the corresponding UTC datetime. If the UTC time is used as is, the ical recipients
// will report the wrong start and end time (+/- 1 day) for the event as they will
// convert the UTC time to their respective local time-zones
String localTimeZone = Utils.getTimeZone(mActivity, mTZUpdater);
long eventStart = IcalendarUtils.convertTimeToUtc(mStartMillis, localTimeZone);
long eventEnd = IcalendarUtils.convertTimeToUtc(mEndMillis, localTimeZone);
event.addEventStart(eventStart, "UTC");
event.addEventEnd(eventEnd, "UTC");
}
event.addProperty(VEvent.LOCATION, mEventCursor.getString(EVENT_INDEX_EVENT_LOCATION));
event.addProperty(VEvent.DESCRIPTION, mEventCursor.getString(EVENT_INDEX_DESCRIPTION));
event.addProperty(VEvent.SUMMARY, mEventCursor.getString(EVENT_INDEX_TITLE));
event.addOrganizer(new Organizer(mEventOrganizerDisplayName, mEventOrganizerEmail));
// Add Attendees to event
for (Attendee attendee : mAcceptedAttendees) {
IcalendarUtils.addAttendeeToEvent(attendee, event);
}
for (Attendee attendee : mDeclinedAttendees) {
IcalendarUtils.addAttendeeToEvent(attendee, event);
}
for (Attendee attendee : mTentativeAttendees) {
IcalendarUtils.addAttendeeToEvent(attendee, event);
}
for (Attendee attendee : mNoResponseAttendees) {
IcalendarUtils.addAttendeeToEvent(attendee, event);
}
// Compose all of the ICalendar objects
calendar.addEvent(event);
// Create and share ics file
boolean isShareSuccessful = false;
try {
// Event title serves as the file name prefix
String filePrefix = event.getProperty(VEvent.SUMMARY);
if (filePrefix == null || filePrefix.length() < 3) {
// Default to a generic filename if event title doesn't qualify
// Prefix length constraint is imposed by File#createTempFile
filePrefix = "invite";
}
filePrefix = filePrefix.replaceAll("\\W+", " ");
if (!filePrefix.endsWith(" ")) {
filePrefix += " ";
}
File dir;
if (type == ShareType.SDCARD) {
dir = EXPORT_SDCARD_DIRECTORY;
if (!dir.exists()) {
dir.mkdir();
}
} else {
dir = mActivity.getExternalCacheDir();
}
File inviteFile = IcalendarUtils.createTempFile(filePrefix, ".ics", dir);
if (IcalendarUtils.writeCalendarToFile(calendar, inviteFile)) {
if (type == ShareType.INTENT) {
// Set world-readable
inviteFile.setReadable(true, false);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(inviteFile));
// The ics file is sent as an extra, the receiving application decides whether
// to parse the file to extract calendar events or treat it as a regular file
shareIntent.setType("application/octet-stream");
Intent chooserIntent = Intent.createChooser(shareIntent, getResources().getString(R.string.cal_share_intent_title));
// The MMS app only responds to "text/x-vcalendar" so we create a chooser intent
// that includes the targeted mms intent + any that respond to the above general
// purpose "application/octet-stream" intent.
File vcsInviteFile = File.createTempFile(filePrefix, ".vcs", mActivity.getExternalCacheDir());
// TODO: revisit above
if (IcalendarUtils.copyFile(inviteFile, vcsInviteFile)) {
Intent mmsShareIntent = new Intent();
mmsShareIntent.setAction(Intent.ACTION_SEND);
mmsShareIntent.setPackage("com.android.mms");
mmsShareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(vcsInviteFile));
mmsShareIntent.setType("text/x-vcalendar");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { mmsShareIntent });
}
startActivity(chooserIntent);
} else {
String msg = getString(R.string.cal_export_succ_msg);
Toast.makeText(mActivity, String.format(msg, inviteFile), Toast.LENGTH_SHORT).show();
}
isShareSuccessful = true;
} else {
// Error writing event info to file
isShareSuccessful = false;
}
} catch (IOException e) {
e.printStackTrace();
isShareSuccessful = false;
}
if (!isShareSuccessful) {
Log.e(TAG, "Couldn't generate ics file");
Toast.makeText(mActivity, R.string.error_generating_ics, Toast.LENGTH_SHORT).show();
}
}
Aggregations