summaryrefslogtreecommitdiffstats
path: root/src/com/android/calendar/icalendar/VEvent.java
blob: 672aa18b1c9e1f07b82f1b7f4ab9856821847524 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
 * Copyright (C) 2014 The CyanogenMod Project
 */

package com.android.calendar.icalendar;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.UUID;

/**
 * Models the Event/VEvent component of the iCalendar format
 */
public class VEvent {

    // valid property identifiers for an event component
    // TODO: only a partial list of attributes has been implemented, implement the rest
    public static String CLASS = "CLASS";
    public static String CREATED = "CREATED";
    public static String LOCATION = "LOCATION";
    public static String ORGANIZER = "ORGANIZER";
    public static String PRIORITY = "PRIORITY";
    public static String SEQ = "SEQ";
    public static String STATUS = "STATUS";
    public static String UID = "UID";
    public static String URL = "URL";
    public static String DTSTART = "DTSTART";
    public static String DTEND = "DTEND";
    public static String DURATION = "DURATION";
    public static String DTSTAMP = "DTSTAMP";
    public static String SUMMARY = "SUMMARY";
    public static String DESCRIPTION = "DESCRIPTION";
    public static String ATTENDEE = "ATTENDEE";
    public static String CATEGORIES = "CATEGORIES";

    // stores the -arity of the attributes that this component can have
    private static HashMap<String, Integer> sPropertyList = new HashMap<String, Integer>();

    // initialize the approved list of mProperties for a calendar event
    static {
        sPropertyList.put(CLASS,1);
        sPropertyList.put(CREATED,1);
        sPropertyList.put(LOCATION,1);
        sPropertyList.put(ORGANIZER,1);
        sPropertyList.put(PRIORITY,1);
        sPropertyList.put(SEQ,1);
        sPropertyList.put(STATUS,1);
        sPropertyList.put(UID,1);
        sPropertyList.put(URL,1);
        sPropertyList.put(DTSTART,1);
        sPropertyList.put(DTEND,1);
        sPropertyList.put(DURATION, 1);
        sPropertyList.put(DTSTAMP,1);
        sPropertyList.put(SUMMARY,1);
        sPropertyList.put(DESCRIPTION,1);

        sPropertyList.put(ATTENDEE, Integer.MAX_VALUE);
        sPropertyList.put(CATEGORIES, Integer.MAX_VALUE);
        sPropertyList.put(CATEGORIES, Integer.MAX_VALUE);
    }

    // stores attributes and their corresponding values belonging to the Event component
    public HashMap<String, String> mProperties;

    public LinkedList<Attendee> mAttendees;
    public Organizer mOrganizer;

    /**
     * Constructor
     */
    public VEvent() {
        mProperties = new HashMap<String, String>();
        mAttendees = new LinkedList<Attendee>();

        // generate and add a unique identifier to this event - ical requisite
        addProperty(UID , UUID.randomUUID().toString() + "@cyanogenmod.com");
        addTimeStamp();
    }

    /**
     * For adding unary properties. For adding other property attributes , use the respective
     * component methods to create and add these special components.
     * @param property
     * @param value
     * @return
     */
    public boolean addProperty(String property, String value) {
        // only unary-properties for now
        if (sPropertyList.containsKey(property) && sPropertyList.get(property) == 1 &&
                value != null) {
            mProperties.put(property, IcalendarUtils.cleanseString(value));
            return true;
        }
        return false;
    }

    /**
     * returns the value of the requested event property or null if there isn't one
     */
    public String getProperty(String property) {
        return mProperties.get(property);
    }

    /**
     * Add attendees to the event
     * @param attendee
     */
    public void addAttendee(Attendee attendee) {
        if(attendee != null) mAttendees.add(attendee);
    }

    /**
     * Add an Organizer to the Event
     * @param organizer
     */
    public void addOrganizer(Organizer organizer) {
        if (organizer != null) mOrganizer = organizer;
    }

    /**
     * Add an start date-time to the event
     */
    public void addEventStart(long startMillis, String timeZone) {
        if (startMillis < 0) return;

        String formattedDateTime = IcalendarUtils.getICalFormattedDateTime(startMillis, timeZone);
        addProperty(DTSTART, formattedDateTime);
    }

    /**
     * Add an end date-time for event
     */
    public void addEventEnd(long endMillis, String timeZone) {
        if (endMillis < 0) return;

        String formattedDateTime = IcalendarUtils.getICalFormattedDateTime(endMillis, timeZone);
        addProperty(DTEND, formattedDateTime);
    }

    /**
     * Timestamps the events with the current date-time
     */
    private void addTimeStamp() {
        String formattedDateTime = IcalendarUtils.getICalFormattedDateTime(
                System.currentTimeMillis(), "UTC");
        addProperty(DTSTAMP, formattedDateTime);
    }

    /**
     * Returns the iCal representation of the Event component
     */
    public String getICalFormattedString() {
        StringBuilder sb = new StringBuilder();

        // Add Event properties
        sb.append("BEGIN:VEVENT\n");
        for (String property : mProperties.keySet() ) {
            sb.append(property + ":" + mProperties.get(property) + "\n");
        }

        // Enforce line length requirements
        sb = IcalendarUtils.enforceICalLineLength(sb);

        sb.append(mOrganizer.getICalFormattedString());

        // add event Attendees
        for (Attendee attendee : mAttendees) {
            sb.append(attendee.getICalFormattedString());
        }

        sb.append("END:VEVENT\n");

        return sb.toString();
    }

    public void populateFromEntries(ListIterator<String> iter) {
        while (iter.hasNext()) {
            String line = iter.next();
            if (line.contains("BEGIN:VEVENT")) {
                // Continue
            } else if (line.startsWith("END:EVENT")) {
                break;
            } else if (line.startsWith("ORGANIZER")) {
                String entry = parseTillNextAttribute(iter, line);
                mOrganizer = Organizer.populateFromICalString(entry);
            } else if (line.startsWith("ATTENDEE")) {
                // Go one previous, so VEvent, parses current line
                iter.previous();

                // Offload to Attendee for parsing
                Attendee attendee = new Attendee();
                attendee.populateFromEntries(iter);
                mAttendees.add(attendee);
            } else if (line.contains(":")) {
                String entry = parseTillNextAttribute(iter, line);
                int indexOfFirstColon = entry.indexOf(":");
                String key = entry.substring(0, indexOfFirstColon);
                String value = entry.substring(indexOfFirstColon + 1);
                mProperties.put(key, value);
            }
        }
    }

    public static String parseTillNextAttribute(ListIterator<String> iter, String currentLine) {
        StringBuilder parse = new StringBuilder();
        parse.append(currentLine);
        while (iter.hasNext()) {
            String line = iter.next();
            if (line.startsWith(" ")) {
                parse.append(line.replaceFirst(" ", ""));
            } else {
                iter.previous();
                break;
            }
        }
        return parse.toString();
    }

}