]> iEval git - unical.git/commitdiff
AddEvent - creates calendar if it doesn't exist
authorPetru <l3asketballplayer@yahoo.com>
Thu, 15 Aug 2013 15:47:58 +0000 (18:47 +0300)
committerPetru <l3asketballplayer@yahoo.com>
Thu, 15 Aug 2013 15:47:58 +0000 (18:47 +0300)
src/ro/ieval/unical/AddEventActivity.java
src/ro/ieval/unical/Event.java

index 5d1c1c133f4b88344b2fe4b85bfabf9a250d1fc7..4c761d69c2f4730e69d605520ddcf56738cf4e1b 100644 (file)
@@ -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();
index 65d35bfc795971b5ce48d69cbb17e30ffa878195..f033533ecbb619b9e39e1668ec5a75da0d669f7c 100644 (file)
@@ -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<Event> r = new ArrayList<Event>();
-               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()]);
        }
 
This page took 0.029009 seconds and 4 git commands to generate.