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