1 package ro
.ieval
.unical
;
2 import java
.text
.ParseException
;
3 import java
.text
.SimpleDateFormat
;
4 import java
.util
.Calendar
;
6 import java
.util
.GregorianCalendar
;
7 import java
.util
.TimeZone
;
9 import android
.app
.ActionBar
;
10 import android
.app
.Activity
;
11 import android
.app
.DatePickerDialog
.OnDateSetListener
;
12 import android
.app
.Fragment
;
13 import android
.app
.TimePickerDialog
.OnTimeSetListener
;
14 import android
.content
.ContentValues
;
15 import android
.content
.Context
;
16 import android
.content
.Intent
;
17 import android
.graphics
.Color
;
18 import android
.net
.Uri
;
19 import android
.os
.Bundle
;
20 import android
.provider
.CalendarContract
;
21 import android
.view
.Menu
;
22 import android
.view
.MenuInflater
;
23 import android
.view
.MenuItem
;
24 import android
.view
.View
;
25 import android
.widget
.*;
27 public final class AddEventActivity
extends Activity
implements OnDateSetListener
, OnTimeSetListener
{
29 private class OnCheckedChangeListener
implements android
.widget
.CompoundButton
.OnCheckedChangeListener
{
30 private final LinearLayout layout
;
31 public OnCheckedChangeListener(final LinearLayout layout
){
35 private class OnItemSelectedListener
implements android
.widget
.AdapterView
.OnItemSelectedListener
{
36 private final View repeatCount
;
37 private final View repeatUntil
;
39 public OnItemSelectedListener(final View repeatCount
, final View repeatUntil
) {
40 this.repeatCount
=repeatCount
;
41 this.repeatUntil
=repeatUntil
;
45 public void onItemSelected(final AdapterView
<?
> whatever
, final View ignored
, final int pos
, final long id
) {
48 repeatCount
.setVisibility(View
.GONE
);
49 repeatUntil
.setVisibility(View
.GONE
);
52 repeatCount
.setVisibility(View
.GONE
);
53 repeatUntil
.setVisibility(View
.VISIBLE
);
56 repeatCount
.setVisibility(View
.VISIBLE
);
57 repeatUntil
.setVisibility(View
.GONE
);
63 @Override public void onNothingSelected(final AdapterView
<?
> arg0
) { /* do nothing */ }
67 public void onCheckedChanged(final CompoundButton buttonView
, final boolean isChecked
) {
69 Utils
.setEnabledRecursively(layout
, true);
70 final Spinner repeatType
= (Spinner
) findViewById(R
.id
.repeat_type
);
71 repeatType
.setOnItemSelectedListener(new OnItemSelectedListener(findViewById(R
.id
.repeat_count
),findViewById(R
.id
.repeat_until
)));
73 Utils
.setEnabledRecursively(layout
, false);
78 private TextView dateTimeTextView
;
79 public static final String EXTRA_EVENT
= "event";
80 private static Event event
=new Event();
82 public static void editEvent(final Context c
, final Event e
) {
83 final Intent i
= new Intent(c
,AddEventActivity
.class);
84 i
.putExtra(AddEventActivity
.EXTRA_EVENT
,e
);
91 protected void onCreate(final Bundle savedInstanceState
) {
92 super.onCreate(savedInstanceState
);
93 setTitle(R
.string
.add_event_title
);
94 event
= getIntent().hasExtra(EXTRA_EVENT
) ?
(Event
) getIntent().getParcelableExtra(EXTRA_EVENT
) : new Event();
95 final ActionBar actionBar
= getActionBar();
96 setContentView(R
.layout
.add_event
);
98 if(getIntent().hasExtra(EXTRA_EVENT
)) {
99 final LinearLayout layout
= (LinearLayout
) findViewById(R
.id
.repeat_layout
);
100 final Switch repeatSwitch
= (Switch
) findViewById(R
.id
.repeat_switch
);
101 Utils
.setEnabledRecursively(layout
, false);
103 repeatSwitch
.setOnCheckedChangeListener(new OnCheckedChangeListener(layout
));
105 EditText title
= (EditText
) findViewById(R
.id
.eventtitle
);
106 title
.setText(event
.title
);
108 TextView tstart
= (TextView
) findViewById(R
.id
.time_start
);
109 TextView tend
= (TextView
) findViewById(R
.id
.time_end
);
110 SimpleDateFormat sf
=new SimpleDateFormat("HH:mm");
111 Date d
=new Date(event
.dtstart
);
112 tstart
.setText(sf
.format(d
).toString());
113 d
.setTime(event
.dtend
);
114 tend
.setText(sf
.format(d
).toString());
116 SimpleDateFormat sd
= new SimpleDateFormat("yyyy-MM-dd");
117 TextView dstart
= (TextView
)findViewById(R
.id
.date_start
);
118 TextView dend
= (TextView
)findViewById(R
.id
.date_end
);
119 d
.setTime(event
.dtstart
);
120 dstart
.setText(sd
.format(d
).toString());
121 d
.setTime(event
.dtend
);
122 dend
.setText(sd
.format(d
).toString());
123 EditText description
= (EditText
) findViewById(R
.id
.description
);
124 description
.setText(event
.description
);
129 public boolean onCreateOptionsMenu(final Menu menu
) {
130 final MenuInflater inflater
= getMenuInflater();
131 inflater
.inflate(R
.menu
.add_event
, menu
);
135 public void createCalendar() {
138 calUri
= CalendarContract
.Calendars
.CONTENT_URI
;
139 cv
= new ContentValues();
140 cv
.put(CalendarContract
.Calendars
.ACCOUNT_NAME
,"unical");
141 cv
.put(CalendarContract
.Calendars
.ACCOUNT_TYPE
,CalendarContract
.ACCOUNT_TYPE_LOCAL
);
142 cv
.put(CalendarContract
.Calendars
.NAME
,"unical");
143 cv
.put(CalendarContract
.Calendars
.CALENDAR_DISPLAY_NAME
, "Unical");
144 cv
.put(CalendarContract
.Calendars
.CALENDAR_COLOR
, Color
.BLACK
);
145 cv
.put(CalendarContract
.Calendars
.CALENDAR_ACCESS_LEVEL
, CalendarContract
.Calendars
.CAL_ACCESS_OWNER
);
146 cv
.put(CalendarContract
.Calendars
.OWNER_ACCOUNT
, "unical");
147 cv
.put(CalendarContract
.Calendars
.VISIBLE
, 1);
148 cv
.put(CalendarContract
.Calendars
.SYNC_EVENTS
, 1);
149 cv
.put(CalendarContract
.Calendars
.CALENDAR_TIME_ZONE
, TimeZone
.getDefault().getID());
150 calUri
= calUri
.buildUpon()
151 .appendQueryParameter(CalendarContract
.CALLER_IS_SYNCADAPTER
, "true")
152 .appendQueryParameter(CalendarContract
.Calendars
.ACCOUNT_NAME
, "unical")
153 .appendQueryParameter(CalendarContract
.Calendars
.ACCOUNT_TYPE
, CalendarContract
.ACCOUNT_TYPE_LOCAL
)
156 this.getContentResolver().delete(calUri
, CalendarContract
.Calendars
.ACCOUNT_NAME
+ " = 'unical'", new String
[0]);
157 this.getContentResolver().insert(calUri
, cv
);
158 Event
.calendarCreated
=true;
162 public boolean onOptionsItemSelected(final MenuItem item
) {
163 switch (item
.getItemId()) {
169 if(event
.title
!=null) event
.delete(this);
170 event
.title
=((EditText
)findViewById(R
.id
.eventtitle
)).getText().toString();
171 String start
=((TextView
)findViewById(R
.id
.date_start
)).getText().toString()+ " " +((TextView
)findViewById(R
.id
.time_start
)).getText().toString();
172 String end
=((TextView
)findViewById(R
.id
.date_end
)).getText().toString()+ " "+((TextView
)findViewById(R
.id
.time_end
)).getText().toString();
175 Date s
=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(start
);
176 Date e
=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(end
);
177 event
.dtstart
=s
.getTime();
178 event
.dtend
=s
.getTime();
179 } catch (ParseException e
) {
183 event
.description
=((EditText
) findViewById(R
.id
.description
)).getText().toString();
185 if(!Event
.calendarCreated
)createCalendar();
186 event
.calendarID
=Event
.getUnicalId(this);
189 Toast toast
= Toast
.makeText(getApplicationContext(),"Event saved!",Toast
.LENGTH_LONG
);
198 public void showDateTimePickerDialog(final View v
) {
199 dateTimeTextView
=(TextView
) v
;
200 if(v
.getId()==R
.id
.date_start
|| v
.getId()==R
.id
.date_end
|| v
.getId() == R
.id
.repeat_until
){
201 final String
[] parts
= dateTimeTextView
.getText().toString().split("-");
202 final DatePickerFragment fragment
= new DatePickerFragment();
203 final Bundle args
= new Bundle();
204 args
.putInt(DatePickerFragment
.ARGUMENT_YEAR
, Integer
.parseInt(parts
[0]));
205 args
.putInt(DatePickerFragment
.ARGUMENT_MONTH
, Integer
.parseInt(parts
[1]));
206 args
.putInt(DatePickerFragment
.ARGUMENT_DAY
, Integer
.parseInt(parts
[2]));
207 fragment
.setArguments(args
);
208 fragment
.show(getFragmentManager(), "datePicker");
210 final String
[] parts
= dateTimeTextView
.getText().toString().split(":");
211 final TimePickerFragment fragment
= new TimePickerFragment();
212 final Bundle args
= new Bundle();
213 args
.putInt(TimePickerFragment
.ARGUMENT_HOUR
, Integer
.parseInt(parts
[0]));
214 args
.putInt(TimePickerFragment
.ARGUMENT_MINUTE
, Integer
.parseInt(parts
[1]));
215 fragment
.setArguments(args
);
216 fragment
.show(getFragmentManager(), "timePicker");
221 public void onTimeSet(final TimePicker picker
, final int hour
, final int minute
) {
222 dateTimeTextView
.setText(String
.format("%02d:%02d",hour
,minute
));
223 if(dateTimeTextView
.getId() == R
.id
.time_start
)
224 ((TextView
)findViewById(R
.id
.time_end
)).setText(String
.format("%2d:%2d",hour
,minute
));
228 public void onDateSet(final DatePicker picker
, final int year
, final int monthOfYear
, final int dayOfMonth
) {
229 final Calendar calendar
=new GregorianCalendar(year
, monthOfYear
, dayOfMonth
);
230 dateTimeTextView
.setText(Utils
.formatDate(year
, monthOfYear
, dayOfMonth
));
231 //calendar.add(Calendar.DAY_OF_MONTH, 1);
232 if(dateTimeTextView
.getId() == R
.id
.date_start
)
233 ((TextView
)findViewById(R
.id
.date_end
)).setText(Utils
.formatDate(calendar
.get(Calendar
.YEAR
), calendar
.get(Calendar
.MONTH
), calendar
.get(Calendar
.DAY_OF_MONTH
)));