From 1b1953ccc82e7c452c84b09e79e2d8dd434ec90c Mon Sep 17 00:00:00 2001 From: Petru Date: Thu, 15 Aug 2013 18:47:58 +0300 Subject: [PATCH] AddEvent - creates calendar if it doesn't exist --- src/ro/ieval/unical/AddEventActivity.java | 48 ++++++++++++++++++++++- src/ro/ieval/unical/Event.java | 9 ++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/ro/ieval/unical/AddEventActivity.java b/src/ro/ieval/unical/AddEventActivity.java index 5d1c1c1..4c761d6 100644 --- a/src/ro/ieval/unical/AddEventActivity.java +++ b/src/ro/ieval/unical/AddEventActivity.java @@ -1,4 +1,5 @@ package ro.ieval.unical; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; @@ -9,9 +10,13 @@ import android.app.Activity; import android.app.DatePickerDialog.OnDateSetListener; import android.app.Fragment; import android.app.TimePickerDialog.OnTimeSetListener; +import android.content.ContentValues; import android.content.Context; import android.content.Intent; +import android.graphics.Color; +import android.net.Uri; import android.os.Bundle; +import android.provider.CalendarContract; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -125,6 +130,30 @@ public final class AddEventActivity extends Activity implements OnDateSetListene return true; } + public void createCalendar() { + Uri calUri,result; + ContentValues cv; + calUri = CalendarContract.Calendars.CONTENT_URI; + cv = new ContentValues(); + cv.put(CalendarContract.Calendars.ACCOUNT_NAME,"unical"); + cv.put(CalendarContract.Calendars.ACCOUNT_TYPE,CalendarContract.ACCOUNT_TYPE_LOCAL); + cv.put(CalendarContract.Calendars.NAME,"unical"); + cv.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "Unical"); + cv.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.BLACK); + cv.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER); + cv.put(CalendarContract.Calendars.OWNER_ACCOUNT, "unical"); + cv.put(CalendarContract.Calendars.VISIBLE, 1); + cv.put(CalendarContract.Calendars.SYNC_EVENTS, 1); + 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().insert(calUri, cv); + Event.calendarCreated=true; + } + @Override public boolean onOptionsItemSelected(final MenuItem item) { switch (item.getItemId()) { @@ -132,7 +161,24 @@ public final class AddEventActivity extends Activity implements OnDateSetListene finish(); return true; case R.id.save: - System.out.println("MuieLaComisie\n"+event.title); + + Event ev=new Event(); + ev.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(); + } catch (ParseException e) { + e.printStackTrace(); + } + + ev.description=((EditText) findViewById(R.id.description)).getText().toString(); + + if(!Event.calendarCreated) createCalendar(); 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 65d35bf..f033533 100644 --- a/src/ro/ieval/unical/Event.java +++ b/src/ro/ieval/unical/Event.java @@ -3,12 +3,15 @@ package ro.ieval.unical; import java.util.ArrayList; import android.content.ContentResolver; +import android.content.ContentValues; import android.content.Context; import android.database.Cursor; +import android.graphics.Color; import android.net.Uri; import android.os.Parcel; import android.os.Parcelable; import android.provider.BaseColumns; +import android.provider.CalendarContract; import android.provider.CalendarContract.Events; final class Event implements Parcelable{ @@ -55,6 +58,8 @@ final class Event implements Parcelable{ /** Calendar ID */ public long calendarID; + public static Boolean calendarCreated=false; + public Event() { dtstart = System.currentTimeMillis(); dtend = dtstart + 60*60*1000; @@ -93,9 +98,11 @@ final class Event implements Parcelable{ public static Event[] getAllEvents(final Context context) { final ArrayList r = new ArrayList(); - for(final Calendar calendar : Calendar.getAllCalendars(context)) + for(final Calendar calendar : Calendar.getAllCalendars(context)) { + if(calendar.name.equals("unical")) calendarCreated=true; for(final Event event : getEventsByCalendar(context, calendar, null)) r.add(event); + } return r.toArray(new Event[r.size()]); } -- 2.39.2