| 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 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 | |
| 118 | public static Event[] getAllEvents(final Context context) { |
| 119 | final ArrayList<Event> r = new ArrayList<Event>(); |
| 120 | for(final Calendar calendar : Calendar.getAllCalendars(context)) { |
| 121 | if(calendar.name.equals("unical")) calendarCreated=true; |
| 122 | for(final Event event : getEventsByCalendar(context, calendar, null)) |
| 123 | r.add(event); |
| 124 | } |
| 125 | return r.toArray(new Event[r.size()]); |
| 126 | } |
| 127 | |
| 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 | |
| 136 | public void insert(final Context context) { |
| 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); |
| 146 | } |
| 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 | } |
| 157 | |
| 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 | } |
| 162 | |
| 163 | @Override |
| 164 | public int describeContents() { |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public void writeToParcel(final Parcel dest, final int flags) { |
| 170 | dest.writeLong(_id); |
| 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); |
| 178 | dest.writeLong(calendarID); |
| 179 | } |
| 180 | } |