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