]> iEval git - unical.git/blob - src/ro/ieval/unical/Event.java
Now events are added to calendar. Edit also works now
[unical.git] / src / ro / ieval / unical / Event.java
1 package ro.ieval.unical;
2
3 import java.util.ArrayList;
4 import java.util.TimeZone;
5
6 import android.content.ContentResolver;
7 import android.content.ContentValues;
8 import android.content.Context;
9 import android.database.Cursor;
10 import android.graphics.Color;
11 import android.net.Uri;
12 import android.os.Parcel;
13 import android.os.Parcelable;
14 import android.provider.BaseColumns;
15 import android.provider.CalendarContract;
16 import android.provider.CalendarContract.Events;
17
18 final class Event implements Parcelable{
19 public static final Parcelable.Creator<Event> CREATOR = new Parcelable.Creator<Event>() {
20 @Override
21 public Event createFromParcel(final Parcel source) {
22 return new Event(source.readLong(), source.readString(), source.readString(), source.readString(), source.readString(), source.readInt(), source.readLong(), source.readLong(), source.readLong());
23 }
24
25 @Override
26 public Event[] newArray(final int size) {
27 return new Event[size];
28 }
29 };
30
31 private static final String[] PROJECTION = {
32 BaseColumns._ID,
33 Events.ORGANIZER,
34 Events.TITLE,
35 Events.EVENT_LOCATION,
36 Events.DESCRIPTION,
37 Events.EVENT_COLOR,
38 Events.DTSTART,
39 Events.DTEND,
40 Events.CALENDAR_ID,
41 };
42
43 /** Event ID */
44 public long _id;
45 /** Event owner */
46 public String organizer;
47 /** Event title */
48 public String title;
49 /** Event location */
50 public String eventLocation;
51 /** Event description */
52 public String description;
53 /** Event colour */
54 public int eventColour;
55 /** Event start time (UTC milliseconds since epoch) */
56 public long dtstart;
57 /** Event end time (UTC milliseconds since epoch) */
58 public long dtend;
59 /** Calendar ID */
60 public long calendarID;
61
62 public static Boolean calendarCreated=false;
63
64 public Event() {
65 dtstart = System.currentTimeMillis();
66 dtend = dtstart + 60*60*1000;
67 }
68
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) {
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;
78 this.calendarID=calendarID;
79 }
80
81 public static Event[] getEventsByCalendar(final Context context, final Calendar calendar, final String sort){
82 final ContentResolver cr=context.getContentResolver();
83 final Cursor cursor = cr.query(Events.CONTENT_URI,
84 PROJECTION,
85 Events.CALENDAR_ID+" = ?",
86 new String[]{Long.toString(calendar._id)},
87 sort);
88 cursor.moveToFirst();
89 final Event[] events = new Event[cursor.getCount()];
90 for(int i=0;i<events.length;i++){
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));
94 cursor.moveToNext();
95 }
96 cursor.close();
97 return events;
98 }
99
100 public static Event[] getAllEvents(final Context context) {
101 final ArrayList<Event> r = new ArrayList<Event>();
102 for(final Calendar calendar : Calendar.getAllCalendars(context)) {
103 if(calendar.name.equals("unical")) calendarCreated=true;
104 for(final Event event : getEventsByCalendar(context, calendar, null))
105 r.add(event);
106 }
107 return r.toArray(new Event[r.size()]);
108 }
109
110 public static long getUnicalId(final Context context) {
111 for(final Calendar calendar : Calendar.getAllCalendars(context))
112 if(calendar.name.equals("unical")) return calendar._id;
113
114 return 0;
115 }
116
117
118 public void insert(final Context context) {
119 ContentValues cv = new ContentValues();
120 cv.put("calendar_id",calendarID);
121 cv.put("title",title);
122 cv.put("description",description);
123 cv.put("dtstart",dtstart);
124 cv.put("dtend",dtend);
125 cv.put("eventTimezone", TimeZone.getDefault().getID());
126
127 Uri url = context.getContentResolver().insert(Events.CONTENT_URI, cv);
128 }
129
130 public void delete(final Context context){
131 final ContentResolver cr=context.getContentResolver();
132 cr.delete(Uri.withAppendedPath(Events.CONTENT_URI, Long.toString(_id)), null, null);
133 }
134
135 @Override
136 public int describeContents() {
137 return 0;
138 }
139
140 @Override
141 public void writeToParcel(final Parcel dest, final int flags) {
142 dest.writeLong(_id);
143 dest.writeString(organizer);
144 dest.writeString(title);
145 dest.writeString(eventLocation);
146 dest.writeString(description);
147 dest.writeInt(eventColour);
148 dest.writeLong(dtstart);
149 dest.writeLong(dtend);
150 dest.writeLong(calendarID);
151 }
152 }
This page took 0.04864 seconds and 4 git commands to generate.