From 684382d3fff6c49c3373bf8fd0f44d7cb2ffe218 Mon Sep 17 00:00:00 2001 From: Petru Date: Thu, 15 Aug 2013 20:26:36 +0300 Subject: [PATCH] Now events are added to calendar. Edit also works now --- res/layout/add_event.xml | 10 ---------- src/ro/ieval/unical/AddEventActivity.java | 20 +++++++++++++------- src/ro/ieval/unical/Event.java | 21 ++++++++++++++++++--- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/res/layout/add_event.xml b/res/layout/add_event.xml index 8ac6f0c..c14561c 100644 --- a/res/layout/add_event.xml +++ b/res/layout/add_event.xml @@ -179,16 +179,6 @@ android:ems="8" android:visibility="gone" /> - - - - \ No newline at end of file diff --git a/src/ro/ieval/unical/AddEventActivity.java b/src/ro/ieval/unical/AddEventActivity.java index 4c761d6..8d00641 100644 --- a/src/ro/ieval/unical/AddEventActivity.java +++ b/src/ro/ieval/unical/AddEventActivity.java @@ -4,6 +4,7 @@ import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; +import java.util.TimeZone; import android.app.ActionBar; import android.app.Activity; @@ -76,11 +77,12 @@ public final class AddEventActivity extends Activity implements OnDateSetListene private TextView dateTimeTextView; public static final String EXTRA_EVENT = "event"; - private Event event; + private static Event event=new Event(); public static void editEvent(final Context c, final Event e) { final Intent i = new Intent(c,AddEventActivity.class); i.putExtra(AddEventActivity.EXTRA_EVENT,e); + event=e; c.startActivity(i); } @@ -144,12 +146,14 @@ public final class AddEventActivity extends Activity implements OnDateSetListene cv.put(CalendarContract.Calendars.OWNER_ACCOUNT, "unical"); cv.put(CalendarContract.Calendars.VISIBLE, 1); cv.put(CalendarContract.Calendars.SYNC_EVENTS, 1); + cv.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, TimeZone.getDefault().getID()); calUri = calUri.buildUpon() .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true") .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, "unical") .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL) .build(); + this.getContentResolver().delete(calUri, CalendarContract.Calendars.ACCOUNT_NAME + " = 'unical'", new String[0]); this.getContentResolver().insert(calUri, cv); Event.calendarCreated=true; } @@ -162,23 +166,25 @@ public final class AddEventActivity extends Activity implements OnDateSetListene return true; case R.id.save: - Event ev=new Event(); - ev.title=((EditText)findViewById(R.id.eventtitle)).getText().toString(); + if(event.title!=null) event.delete(this); + event.title=((EditText)findViewById(R.id.eventtitle)).getText().toString(); String start=((TextView)findViewById(R.id.date_start)).getText().toString()+ " " +((TextView)findViewById(R.id.time_start)).getText().toString(); String end=((TextView)findViewById(R.id.date_end)).getText().toString()+ " "+((TextView)findViewById(R.id.time_end)).getText().toString(); try { Date s=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(start); Date e=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(end); - ev.dtstart=s.getTime(); - ev.dtend=s.getTime(); + event.dtstart=s.getTime(); + event.dtend=s.getTime(); } catch (ParseException e) { e.printStackTrace(); } - ev.description=((EditText) findViewById(R.id.description)).getText().toString(); + event.description=((EditText) findViewById(R.id.description)).getText().toString(); - if(!Event.calendarCreated) createCalendar(); + if(!Event.calendarCreated)createCalendar(); + event.calendarID=Event.getUnicalId(this); + event.insert(this); Toast toast = Toast.makeText(getApplicationContext(),"Event saved!",Toast.LENGTH_LONG); toast.show(); diff --git a/src/ro/ieval/unical/Event.java b/src/ro/ieval/unical/Event.java index f033533..a7fce60 100644 --- a/src/ro/ieval/unical/Event.java +++ b/src/ro/ieval/unical/Event.java @@ -1,6 +1,7 @@ package ro.ieval.unical; import java.util.ArrayList; +import java.util.TimeZone; import android.content.ContentResolver; import android.content.ContentValues; @@ -106,10 +107,24 @@ final class Event implements Parcelable{ return r.toArray(new Event[r.size()]); } + public static long getUnicalId(final Context context) { + for(final Calendar calendar : Calendar.getAllCalendars(context)) + if(calendar.name.equals("unical")) return calendar._id; + + return 0; + } + + public void insert(final Context context) { - final ContentResolver cr=context.getContentResolver(); - //cr.insert(Uri.withAppendedPath(Events.CONTENT_URI)); - //cr.notifyChange(); + ContentValues cv = new ContentValues(); + cv.put("calendar_id",calendarID); + cv.put("title",title); + cv.put("description",description); + cv.put("dtstart",dtstart); + cv.put("dtend",dtend); + cv.put("eventTimezone", TimeZone.getDefault().getID()); + + Uri url = context.getContentResolver().insert(Events.CONTENT_URI, cv); } public void delete(final Context context){ -- 2.39.2