]>
Commit | Line | Data |
---|---|---|
bdd8f09c | 1 | package ro.ieval.unical; |
7be52756 | 2 | |
92086ce7 | 3 | import java.util.ArrayList; |
684382d3 | 4 | import java.util.TimeZone; |
92086ce7 | 5 | |
39f0cac1 | 6 | import android.content.ContentResolver; |
1b1953cc | 7 | import android.content.ContentValues; |
39f0cac1 | 8 | import android.content.Context; |
7be52756 | 9 | import android.database.Cursor; |
1b1953cc | 10 | import android.graphics.Color; |
4b8bd2f8 | 11 | import android.net.Uri; |
39f0cac1 MG |
12 | import android.os.Parcel; |
13 | import android.os.Parcelable; | |
8a817995 | 14 | import android.provider.BaseColumns; |
1b1953cc | 15 | import android.provider.CalendarContract; |
39f0cac1 | 16 | import android.provider.CalendarContract.Events; |
7be52756 | 17 | |
39f0cac1 MG |
18 | final class Event implements Parcelable{ |
19 | public static final Parcelable.Creator<Event> CREATOR = new Parcelable.Creator<Event>() { | |
20 | @Override | |
5cf4714f | 21 | public Event createFromParcel(final Parcel source) { |
b7fd90e9 | 22 | return new Event(source.readLong(), source.readString(), source.readString(), source.readString(), source.readString(), source.readInt(), source.readLong(), source.readLong(), source.readLong()); |
39f0cac1 MG |
23 | } |
24 | ||
25 | @Override | |
5cf4714f | 26 | public Event[] newArray(final int size) { |
39f0cac1 MG |
27 | return new Event[size]; |
28 | } | |
29 | }; | |
30 | ||
31 | private static final String[] PROJECTION = { | |
8a817995 | 32 | BaseColumns._ID, |
39f0cac1 MG |
33 | Events.ORGANIZER, |
34 | Events.TITLE, | |
35 | Events.EVENT_LOCATION, | |
36 | Events.DESCRIPTION, | |
37 | Events.EVENT_COLOR, | |
38 | Events.DTSTART, | |
39 | Events.DTEND, | |
b7fd90e9 | 40 | Events.CALENDAR_ID, |
39f0cac1 MG |
41 | }; |
42 | ||
43 | /** Event ID */ | |
5835912b | 44 | public long _id; |
39f0cac1 | 45 | /** Event owner */ |
5835912b | 46 | public String organizer; |
39f0cac1 | 47 | /** Event title */ |
5835912b | 48 | public String title; |
39f0cac1 | 49 | /** Event location */ |
5835912b | 50 | public String eventLocation; |
39f0cac1 | 51 | /** Event description */ |
5835912b | 52 | public String description; |
39f0cac1 | 53 | /** Event colour */ |
5835912b | 54 | public int eventColour; |
070d3ab2 | 55 | /** Event start time (UTC milliseconds since epoch) */ |
5835912b | 56 | public long dtstart; |
39f0cac1 | 57 | /** Event end time (UTC milliseconds since epoch) */ |
5835912b | 58 | public long dtend; |
b7fd90e9 | 59 | /** Calendar ID */ |
5835912b | 60 | public long calendarID; |
39f0cac1 | 61 | |
1b1953cc P |
62 | public static Boolean calendarCreated=false; |
63 | ||
e4fb15ad MG |
64 | public Event() { |
65 | dtstart = System.currentTimeMillis(); | |
66 | dtend = dtstart + 60*60*1000; | |
67 | } | |
c32befcc | 68 | |
412b2d69 | 69 | public Event(final long _id, final String organizer, final String title, final String eventLocation, final String description, final int eventColor, final long dtstart, final long dtend, final long calendarID) { |
39f0cac1 MG |
70 | this._id=_id; |
71 | this.organizer=organizer; | |
72 | this.title=title; | |
73 | this.eventLocation=eventLocation; | |
74 | this.description=description; | |
75 | this.eventColour=eventColor; | |
76 | this.dtstart=dtstart; | |
77 | this.dtend=dtend; | |
b7fd90e9 | 78 | this.calendarID=calendarID; |
39f0cac1 MG |
79 | } |
80 | ||
810f5287 | 81 | public static Event[] getEventsByCalendar(final Context context, final Calendar calendar, final String sort){ |
39f0cac1 MG |
82 | final ContentResolver cr=context.getContentResolver(); |
83 | final Cursor cursor = cr.query(Events.CONTENT_URI, | |
84 | PROJECTION, | |
85 | Events.CALENDAR_ID+" = ?", | |
810f5287 | 86 | new String[]{Long.toString(calendar._id)}, |
fc891ea8 | 87 | sort); |
39f0cac1 MG |
88 | cursor.moveToFirst(); |
89 | final Event[] events = new Event[cursor.getCount()]; | |
16e2550c | 90 | for(int i=0;i<events.length;i++){ |
810f5287 MG |
91 | int colour=cursor.getInt(5); |
92 | colour=colour == 0 ? calendar.colour : colour; | |
93 | events[i]=new Event(cursor.getLong(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), colour, cursor.getLong(6), cursor.getLong(7), cursor.getLong(8)); | |
16e2550c MG |
94 | cursor.moveToNext(); |
95 | } | |
8a817995 | 96 | cursor.close(); |
39f0cac1 MG |
97 | return events; |
98 | } | |
070d3ab2 | 99 | |
86ad3786 MG |
100 | public static Event getEventById(final Context context, final long id){ |
101 | final ContentResolver cr=context.getContentResolver(); | |
102 | final Cursor cursor = cr.query(Events.CONTENT_URI, | |
103 | PROJECTION, | |
104 | BaseColumns._ID+" = ?", | |
105 | new String[]{Long.toString(id)}, | |
106 | null); | |
107 | final Event event; | |
108 | if(cursor.moveToFirst()){ | |
109 | int colour=cursor.getInt(5); | |
110 | colour=colour == 0 ? Calendar.getCalendarById(context, cursor.getLong(8)).colour : colour; | |
111 | event = new Event(cursor.getLong(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), colour, cursor.getLong(6), cursor.getLong(7), cursor.getLong(8)); | |
112 | } else | |
113 | event = null; | |
114 | cursor.close(); | |
115 | return event; | |
116 | } | |
117 | ||
92086ce7 | 118 | public static Event[] getAllEvents(final Context context) { |
c1da3e5d | 119 | final ArrayList<Event> r = new ArrayList<Event>(); |
1b1953cc P |
120 | for(final Calendar calendar : Calendar.getAllCalendars(context)) { |
121 | if(calendar.name.equals("unical")) calendarCreated=true; | |
810f5287 | 122 | for(final Event event : getEventsByCalendar(context, calendar, null)) |
c1da3e5d | 123 | r.add(event); |
1b1953cc | 124 | } |
92086ce7 PT |
125 | return r.toArray(new Event[r.size()]); |
126 | } | |
39f0cac1 | 127 | |
684382d3 P |
128 | public static long getUnicalId(final Context context) { |
129 | for(final Calendar calendar : Calendar.getAllCalendars(context)) | |
130 | if(calendar.name.equals("unical")) return calendar._id; | |
131 | ||
132 | return 0; | |
133 | } | |
134 | ||
135 | ||
ad349897 | 136 | public void insert(final Context context) { |
684382d3 P |
137 | ContentValues cv = new ContentValues(); |
138 | cv.put("calendar_id",calendarID); | |
139 | cv.put("title",title); | |
140 | cv.put("description",description); | |
141 | cv.put("dtstart",dtstart); | |
142 | cv.put("dtend",dtend); | |
143 | cv.put("eventTimezone", TimeZone.getDefault().getID()); | |
144 | ||
145 | Uri url = context.getContentResolver().insert(Events.CONTENT_URI, cv); | |
ad349897 | 146 | } |
a9aebc18 P |
147 | public void edit(final Context context) { |
148 | ContentValues cv = new ContentValues(); | |
149 | cv.put("calendar_id",calendarID); | |
150 | cv.put("title",title); | |
151 | cv.put("description",description); | |
152 | cv.put("dtstart",dtstart); | |
153 | cv.put("dtend",dtend); | |
154 | cv.put("eventTimezone", TimeZone.getDefault().getID()); | |
155 | context.getContentResolver().update(Events.CONTENT_URI,cv,"_id = '" + String.valueOf(_id) + "'",new String[0]); | |
156 | } | |
ad349897 | 157 | |
4b8bd2f8 MG |
158 | public void delete(final Context context){ |
159 | final ContentResolver cr=context.getContentResolver(); | |
160 | cr.delete(Uri.withAppendedPath(Events.CONTENT_URI, Long.toString(_id)), null, null); | |
161 | } | |
070d3ab2 | 162 | |
39f0cac1 MG |
163 | @Override |
164 | public int describeContents() { | |
165 | return 0; | |
166 | } | |
167 | ||
168 | @Override | |
5cf4714f | 169 | public void writeToParcel(final Parcel dest, final int flags) { |
d022ee49 | 170 | dest.writeLong(_id); |
39f0cac1 MG |
171 | dest.writeString(organizer); |
172 | dest.writeString(title); | |
173 | dest.writeString(eventLocation); | |
174 | dest.writeString(description); | |
175 | dest.writeInt(eventColour); | |
176 | dest.writeLong(dtstart); | |
177 | dest.writeLong(dtend); | |
b7fd90e9 | 178 | dest.writeLong(calendarID); |
bdd8f09c PT |
179 | } |
180 | } |