aboutsummaryrefslogtreecommitdiff
path: root/src/org/linaro/connect/sched/ScheduleItem.java
blob: debf206e00fa18c060fb9105120707f2fadff280 (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
package org.linaro.connect.sched;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.linaro.connect.LinaroConnect;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.format.Time;
import android.util.Log;

public class ScheduleItem implements Serializable {

	private final HashMap<String, String> mFields;

	private ScheduleItem() {
		mFields = new HashMap<String, String>();
	}

	private void addField(String key, String val) {
		mFields.put(key, val);
	}

	public String getSummary() {
		return mFields.get("SUMMARY");
	}

	public String getDescription() {
		return mFields.get("DESCRIPTION");
	}

	public String getLocation() {
		return mFields.get("LOCATION");
	}

	public String getCategory() {
		return mFields.get("CATEGORIES");
	}

	public String getUrl() {
		return mFields.get("URL");
	}

	public Uri getUri() {
		return Uri.parse(getUrl());
	}

	private Date getDate(String field) {
		String s = mFields.get(field);
		Time t = new Time();
		t.parse(s);
		return new Date(t.toMillis(false));
	}

	public String getStartTime() {
		return getDate("DTSTART").toLocaleString();
	}

	@Override
	public String toString() {
		return getSummary();
	}

	private boolean matches(Filter f) {
		if( f == null )
			return true;

		boolean matches = true;

		if(matches && f.dayOfWeek != -1) {
			Date d = getDate("DTSTART");
			Calendar c = Calendar.getInstance();
			c.setTime(d);
			matches = (c.get(Calendar.DAY_OF_WEEK) == f.dayOfWeek);
		}

		if( matches && f.track != null )
			matches = f.track.equals(getCategory());

		return matches;
	}

	public Intent getIntent(Context ctx) {
		return null;
	}

	private static ScheduleItem parseLine(String line, ScheduleItem cur,
											List<ScheduleItem> items,
											Filter f) {
		if( "BEGIN:VEVENT".equals(line) ) {
			cur = new ScheduleItem();
		}
		else if( "END:VEVENT".equals(line) ) {
			if(cur != null && cur.matches(f))
				items.add(cur);
			cur = null;
		}
		else if( cur != null ) {
			String parts[] = line.split(":", 2);
			if( parts.length > 1 )
				cur.addField(parts[0], parts[1]);
		}
		return cur;
	}

	private static int getDayOfWeek(String day) {
		if( "monday".equalsIgnoreCase(day))
			return Calendar.MONDAY;
		else if( "tuesday".equalsIgnoreCase(day))
			return Calendar.TUESDAY;
		else if( "wednesday".equalsIgnoreCase(day))
			return Calendar.WEDNESDAY;
		else if( "thursday".equalsIgnoreCase(day))
			return Calendar.THURSDAY;
		else if( "friday".equalsIgnoreCase(day))
			return Calendar.FRIDAY;
		else if( "saturday".equalsIgnoreCase(day))
			return Calendar.SATURDAY;
		else if( "sunday".equalsIgnoreCase(day))
			return Calendar.SUNDAY;
		return -1;
	}

	public static class Filter {
		int dayOfWeek = -1;
		String track = null;

		public Filter(String day, String track) {
			dayOfWeek = getDayOfWeek(day);
			this.track = track;
		}
	}

	/**
	 * Takes an iCal input stream and returns corresponding objects. If a filter
	 * is provided it will narrow down results based on it
	 */
	public static ScheduleItem[] fromInputStream(InputStream is, Filter f) {
		ArrayList<ScheduleItem> items = new ArrayList<ScheduleItem>();

		if (is == null)
			return new ScheduleItem[0];

		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		try {
			String line;
			ScheduleItem cur = null;
			while( (line=br.readLine()) != null )
				cur = parseLine(line, cur, items, f);
		}
		catch(IOException e) {
			Log.e(LinaroConnect.TAG, "Error reading schedule input stream", e);
		}

		try {
			br.close();
		} catch (IOException e) {}

		return items.toArray(new ScheduleItem[items.size()]);
	}
}