From 6f6b8878deba974e20effd00b922203cc9dda818 Mon Sep 17 00:00:00 2001 From: Matt Garnes Date: Mon, 8 Dec 2014 17:47:07 -0800 Subject: Support sharing calendar events as .vcs files to MMS. - Export calendar events to a temporary .vcs file. Currently, the actual file format is still ical, this may need to be reformatted to actually contain only vCalendar content. - Share this specifically to the com.android.mms package when the share button is clicked. - Share the event as ical .ics to all other applications. Change-Id: If337360b17d4071d580087ecc97a2397d0563ff6 --- res/values/cm_strings.xml | 3 ++ src/com/android/calendar/AllInOneActivity.java | 16 +++---- src/com/android/calendar/EventInfoFragment.java | 22 ++++++++- .../android/calendar/icalendar/IcalendarUtils.java | 52 ++++++++++++++++++++++ 4 files changed, 84 insertions(+), 9 deletions(-) diff --git a/res/values/cm_strings.xml b/res/values/cm_strings.xml index ba9bb628..5652b3ef 100644 --- a/res/values/cm_strings.xml +++ b/res/values/cm_strings.xml @@ -47,4 +47,7 @@ "Go to" + + + Send event to: diff --git a/src/com/android/calendar/AllInOneActivity.java b/src/com/android/calendar/AllInOneActivity.java index 7bcb6772..3c869574 100644 --- a/src/com/android/calendar/AllInOneActivity.java +++ b/src/com/android/calendar/AllInOneActivity.java @@ -451,8 +451,8 @@ public class AllInOneActivity extends AbstractCalendarActivity implements EventH getLoaderManager().initLoader(0, null, this); } - // clean up cached ics files - in case onDestroy() didn't run the last time - cleanupCachedIcsFiles(); + // clean up cached ics and vcs files - in case onDestroy() didn't run the last time + cleanupCachedEventFiles(); } private long parseViewAction(final Intent intent) { @@ -644,22 +644,22 @@ public class AllInOneActivity extends AbstractCalendarActivity implements EventH CalendarController.removeInstance(this); - // clean up cached ics files - cleanupCachedIcsFiles(); + // clean up cached ics and vcs files + cleanupCachedEventFiles(); } /** - * Cleans up the temporarily generated ics files in the cache directory - * The files are of the format *.ics + * Cleans up the temporarily generated ics and vcs files in the cache directory + * The files are of the format *.ics and *.vcs */ - private void cleanupCachedIcsFiles() { + private void cleanupCachedEventFiles() { if (!isExternalStorageWritable()) return; File cacheDir = getExternalCacheDir(); File[] files = cacheDir.listFiles(); if (files == null) return; for (File file : files) { String filename = file.getName(); - if (filename.endsWith(".ics")) { + if (filename.endsWith(".ics") || filename.endsWith(".vcs")) { file.delete(); } } diff --git a/src/com/android/calendar/EventInfoFragment.java b/src/com/android/calendar/EventInfoFragment.java index 85be389d..90f73b02 100644 --- a/src/com/android/calendar/EventInfoFragment.java +++ b/src/com/android/calendar/EventInfoFragment.java @@ -1340,7 +1340,27 @@ public class EventInfoFragment extends DialogFragment implements OnCheckedChange // 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"); - startActivity(shareIntent); + + 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()); + // for now , we are duplicating ics file and using that as the vcs file + // 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); isShareSuccessful = true; } else { diff --git a/src/com/android/calendar/icalendar/IcalendarUtils.java b/src/com/android/calendar/icalendar/IcalendarUtils.java index ef96674b..e179926b 100644 --- a/src/com/android/calendar/icalendar/IcalendarUtils.java +++ b/src/com/android/calendar/icalendar/IcalendarUtils.java @@ -9,9 +9,12 @@ import android.util.Log; import com.android.calendar.CalendarEventModel; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; @@ -180,4 +183,53 @@ public class IcalendarUtils { // remove the local time zone's UTC offset return millis - TimeZone.getTimeZone(localTimeZone).getRawOffset(); } + + /** + * Copy the contents of a file into another + * + * @param src input / src file + * @param dst file to be copied into + */ + public static boolean copyFile(File src, File dst) { + boolean isSuccessful = false; + InputStream in = null; + OutputStream out = null; + try { + in = new FileInputStream(src); + out = new FileOutputStream(dst); + + byte[] buf = new byte[1024]; + + try { + for (int len; (len = in.read(buf)) > 0; ) { + out.write(buf, 0, len); + } + isSuccessful = true; + } catch (IOException e) { + // ignore + } + + } catch (FileNotFoundException fnf) { + // ignore + } finally { + + if (in != null) { + try { + in.close(); + } catch (IOException e) { + // ignore + } + } + + if (out != null) { + try { + out.close(); + } catch (IOException e) { + // ignore + } + } + } + + return isSuccessful; + } } -- cgit v1.2.3