diff options
author | Danesh M <daneshm90@gmail.com> | 2014-12-16 17:20:05 -0800 |
---|---|---|
committer | Danesh Mondegarian <daneshm90@gmail.com> | 2014-12-16 22:14:04 -0800 |
commit | ce5b4a44e51afdaf73c08f499f55bbb96985cc71 (patch) | |
tree | 7eccc5f35e58f6b2dbd1fa656d496c822e30cdab /src/com/android/calendar/icalendar/IcalendarUtils.java | |
parent | 19c77e769ce84908ac598ba9f3f2aba90d7a427b (diff) | |
download | android_packages_apps_Calendar-ce5b4a44e51afdaf73c08f499f55bbb96985cc71.tar.gz android_packages_apps_Calendar-ce5b4a44e51afdaf73c08f499f55bbb96985cc71.tar.bz2 android_packages_apps_Calendar-ce5b4a44e51afdaf73c08f499f55bbb96985cc71.zip |
Calendar : Add ability to import/export from sdcard
Change-Id: Ia2ae56b50bff764786cdd37b760eb6b94a63743f
Diffstat (limited to 'src/com/android/calendar/icalendar/IcalendarUtils.java')
-rw-r--r-- | src/com/android/calendar/icalendar/IcalendarUtils.java | 124 |
1 files changed, 114 insertions, 10 deletions
diff --git a/src/com/android/calendar/icalendar/IcalendarUtils.java b/src/com/android/calendar/icalendar/IcalendarUtils.java index e179926b..efbb4583 100644 --- a/src/com/android/calendar/icalendar/IcalendarUtils.java +++ b/src/com/android/calendar/icalendar/IcalendarUtils.java @@ -4,20 +4,17 @@ package com.android.calendar.icalendar; +import android.content.ContentResolver; +import android.content.Context; +import android.net.Uri; import android.provider.CalendarContract; -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.io.*; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Calendar; -import java.util.Date; +import java.util.Random; import java.util.TimeZone; /** @@ -25,7 +22,21 @@ import java.util.TimeZone; */ public class IcalendarUtils { - private static int sPermittedLineLength = 75; // Line length mandated by iCalendar format + public static int sPermittedLineLength = 75; // Line length mandated by iCalendar format + private static final Random tempFileRandom = new Random(); + + public static String uncleanseString(CharSequence sequence) { + if (sequence == null) return null; + String input = sequence.toString(); + + // reintroduce new lines with the literal '\n' + input = input.replaceAll("\\\\n", "\n"); + // reintroduce semicolons and commas + input = input.replaceAll("\\\\;", ";"); + input = input.replaceAll("\\\\\\,", ","); + + return input; + } /** * ensure the string conforms to the iCalendar encoding requirements @@ -47,6 +58,99 @@ public class IcalendarUtils { } /** + * Creates an empty temporary file in the given directory using the given + * prefix and suffix as part of the file name. If {@code suffix} is null, {@code .tmp} is used. + * + * <p>Note that this method does <i>not</i> call {@link #deleteOnExit}, but see the + * documentation for that method before you call it manually. + * + * @param prefix + * the prefix to the temp file name. + * @param suffix + * the suffix to the temp file name. + * @param directory + * the location to which the temp file is to be written, or + * {@code null} for the default location for temporary files, + * which is taken from the "java.io.tmpdir" system property. It + * may be necessary to set this property to an existing, writable + * directory for this method to work properly. + * @return the temporary file. + * @throws IllegalArgumentException + * if the length of {@code prefix} is less than 3. + * @throws IOException + * if an error occurs when writing the file. + */ + public static File createTempFile(String prefix, String suffix, File directory) + throws IOException { + // Force a prefix null check first + if (prefix.length() < 3) { + throw new IllegalArgumentException("prefix must be at least 3 characters"); + } + if (suffix == null) { + suffix = ".tmp"; + } + File tmpDirFile = directory; + if (tmpDirFile == null) { + String tmpDir = System.getProperty("java.io.tmpdir", "."); + tmpDirFile = new File(tmpDir); + } + File result; + do { + result = new File(tmpDirFile, + prefix + tempFileRandom.nextInt(Integer.MAX_VALUE) + suffix); + } while (!result.createNewFile()); + return result; + } + + public static VCalendar readCalendarFromFile(Context context, Uri uri) { + ArrayList<String> contents = getStringArrayFromFile(context, uri); + if (contents == null || contents.isEmpty()) { + return null; + } + VCalendar calendar = new VCalendar(); + calendar.populateFromString(contents); + return calendar; + } + + public static ArrayList<String> getStringArrayFromFile(Context context, Uri uri) { + String scheme = uri.getScheme(); + InputStream inputStream = null; + if(ContentResolver.SCHEME_CONTENT.equals(scheme)) { + try { + inputStream = context.getContentResolver().openInputStream(uri); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } else if (ContentResolver.SCHEME_FILE.equals(scheme)) { + File f = new File(uri.getPath()); + try { + inputStream = new FileInputStream(f); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + if (inputStream == null) { + return null; + } + + ArrayList<String> result = new ArrayList<String>(); + + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + String line; + while ((line = reader.readLine()) != null) { + result.add(line); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return result; + } + + /** * Stringify VCalendar object and write to file * @param calendar * @param file |