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
;
8 import android
.app
.ActionBar
;
9 import android
.app
.Activity
;
10 import android
.app
.DatePickerDialog
.OnDateSetListener
;
11 import android
.app
.Fragment
;
12 import android
.app
.TimePickerDialog
.OnTimeSetListener
;
13 import android
.content
.ContentValues
;
14 import android
.content
.Context
;
15 import android
.content
.Intent
;
16 import android
.graphics
.Color
;
17 import android
.net
.Uri
;
18 import android
.os
.Bundle
;
19 import android
.provider
.CalendarContract
;
20 import android
.view
.Menu
;
21 import android
.view
.MenuInflater
;
22 import android
.view
.MenuItem
;
23 import android
.view
.View
;
24 import android
.widget
.*;
26 public final class AddEventActivity
extends Activity
implements OnDateSetListener
, OnTimeSetListener
{
28 private class OnCheckedChangeListener
implements android
.widget
.CompoundButton
.OnCheckedChangeListener
{
29 private final LinearLayout layout
;
30 public OnCheckedChangeListener(final LinearLayout layout
){
34 private class OnItemSelectedListener
implements android
.widget
.AdapterView
.OnItemSelectedListener
{
35 private final View repeatCount
;
36 private final View repeatUntil
;
38 public OnItemSelectedListener(final View repeatCount
, final View repeatUntil
) {
39 this.repeatCount
=repeatCount
;
40 this.repeatUntil
=repeatUntil
;
44 public void onItemSelected(final AdapterView
<?
> whatever
, final View ignored
, final int pos
, final long id
) {
47 repeatCount
.setVisibility(View
.GONE
);
48 repeatUntil
.setVisibility(View
.GONE
);
51 repeatCount
.setVisibility(View
.GONE
);
52 repeatUntil
.setVisibility(View
.VISIBLE
);
55 repeatCount
.setVisibility(View
.VISIBLE
);
56 repeatUntil
.setVisibility(View
.GONE
);
62 @Override public void onNothingSelected(final AdapterView
<?
> arg0
) { /* do nothing */ }
66 public void onCheckedChanged(final CompoundButton buttonView
, final boolean isChecked
) {
68 Utils
.setEnabledRecursively(layout
, true);
69 final Spinner repeatType
= (Spinner
) findViewById(R
.id
.repeat_type
);
70 repeatType
.setOnItemSelectedListener(new OnItemSelectedListener(findViewById(R
.id
.repeat_count
),findViewById(R
.id
.repeat_until
)));
72 Utils
.setEnabledRecursively(layout
, false);
77 private TextView dateTimeTextView
;
78 public static final String EXTRA_EVENT
= "event";
81 public static void editEvent(final Context c
, final Event e
) {
82 final Intent i
= new Intent(c
,AddEventActivity
.class);
83 i
.putExtra(AddEventActivity
.EXTRA_EVENT
,e
);
89 protected void onCreate(final Bundle savedInstanceState
) {
90 super.onCreate(savedInstanceState
);
91 setTitle(R
.string
.add_event_title
);
92 event
= getIntent().hasExtra(EXTRA_EVENT
) ?
(Event
) getIntent().getParcelableExtra(EXTRA_EVENT
) : new Event();
93 final ActionBar actionBar
= getActionBar();
94 setContentView(R
.layout
.add_event
);
96 if(getIntent().hasExtra(EXTRA_EVENT
)) {
97 final LinearLayout layout
= (LinearLayout
) findViewById(R
.id
.repeat_layout
);
98 final Switch repeatSwitch
= (Switch
) findViewById(R
.id
.repeat_switch
);
99 Utils
.setEnabledRecursively(layout
, false);
101 repeatSwitch
.setOnCheckedChangeListener(new OnCheckedChangeListener(layout
));
103 EditText title
= (EditText
) findViewById(R
.id
.eventtitle
);
104 title
.setText(event
.title
);
106 TextView tstart
= (TextView
) findViewById(R
.id
.time_start
);
107 TextView tend
= (TextView
) findViewById(R
.id
.time_end
);
108 SimpleDateFormat sf
=new SimpleDateFormat("HH:mm");
109 Date d
=new Date(event
.dtstart
);
110 tstart
.setText(sf
.format(d
).toString());
111 d
.setTime(event
.dtend
);
112 tend
.setText(sf
.format(d
).toString());
114 SimpleDateFormat sd
= new SimpleDateFormat("yyyy-MM-dd");
115 TextView dstart
= (TextView
)findViewById(R
.id
.date_start
);
116 TextView dend
= (TextView
)findViewById(R
.id
.date_end
);
117 d
.setTime(event
.dtstart
);
118 dstart
.setText(sd
.format(d
).toString());
119 d
.setTime(event
.dtend
);
120 dend
.setText(sd
.format(d
).toString());
121 EditText description
= (EditText
) findViewById(R
.id
.description
);
122 description
.setText(event
.description
);
127 public boolean onCreateOptionsMenu(final Menu menu
) {
128 final MenuInflater inflater
= getMenuInflater();
129 inflater
.inflate(R
.menu
.add_event
, menu
);
133 public void createCalendar() {
136 calUri
= CalendarContract
.Calendars
.CONTENT_URI
;
137 cv
= new ContentValues();
138 cv
.put(CalendarContract
.Calendars
.ACCOUNT_NAME
,"unical");
139 cv
.put(CalendarContract
.Calendars
.ACCOUNT_TYPE
,CalendarContract
.ACCOUNT_TYPE_LOCAL
);
140 cv
.put(CalendarContract
.Calendars
.NAME
,"unical");
141 cv
.put(CalendarContract
.Calendars
.CALENDAR_DISPLAY_NAME
, "Unical");
142 cv
.put(CalendarContract
.Calendars
.CALENDAR_COLOR
, Color
.BLACK
);
143 cv
.put(CalendarContract
.Calendars
.CALENDAR_ACCESS_LEVEL
, CalendarContract
.Calendars
.CAL_ACCESS_OWNER
);
144 cv
.put(CalendarContract
.Calendars
.OWNER_ACCOUNT
, "unical");
145 cv
.put(CalendarContract
.Calendars
.VISIBLE
, 1);
146 cv
.put(CalendarContract
.Calendars
.SYNC_EVENTS
, 1);
147 calUri
= calUri
.buildUpon()
148 .appendQueryParameter(CalendarContract
.CALLER_IS_SYNCADAPTER
, "true")
149 .appendQueryParameter(CalendarContract
.Calendars
.ACCOUNT_NAME
, "unical")
150 .appendQueryParameter(CalendarContract
.Calendars
.ACCOUNT_TYPE
, CalendarContract
.ACCOUNT_TYPE_LOCAL
)
153 this.getContentResolver().insert(calUri
, cv
);
154 Event
.calendarCreated
=true;
158 public boolean onOptionsItemSelected(final MenuItem item
) {
159 switch (item
.getItemId()) {
165 Event ev
=new Event();
166 ev
.title
=((EditText
)findViewById(R
.id
.eventtitle
)).getText().toString();
167 String start
=((TextView
)findViewById(R
.id
.date_start
)).getText().toString()+ " " +((TextView
)findViewById(R
.id
.time_start
)).getText().toString();
168 String end
=((TextView
)findViewById(R
.id
.date_end
)).getText().toString()+ " "+((TextView
)findViewById(R
.id
.time_end
)).getText().toString();
171 Date s
=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(start
);
172 Date e
=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(end
);
173 ev
.dtstart
=s
.getTime();
174 ev
.dtend
=s
.getTime();
175 } catch (ParseException e
) {
179 ev
.description
=((EditText
) findViewById(R
.id
.description
)).getText().toString();
181 if(!Event
.calendarCreated
) createCalendar();
183 Toast toast
= Toast
.makeText(getApplicationContext(),"Event saved!",Toast
.LENGTH_LONG
);
192 public void showDateTimePickerDialog(final View v
) {
193 dateTimeTextView
=(TextView
) v
;
194 if(v
.getId()==R
.id
.date_start
|| v
.getId()==R
.id
.date_end
|| v
.getId() == R
.id
.repeat_until
){
195 final String
[] parts
= dateTimeTextView
.getText().toString().split("-");
196 final DatePickerFragment fragment
= new DatePickerFragment();
197 final Bundle args
= new Bundle();
198 args
.putInt(DatePickerFragment
.ARGUMENT_YEAR
, Integer
.parseInt(parts
[0]));
199 args
.putInt(DatePickerFragment
.ARGUMENT_MONTH
, Integer
.parseInt(parts
[1]));
200 args
.putInt(DatePickerFragment
.ARGUMENT_DAY
, Integer
.parseInt(parts
[2]));
201 fragment
.setArguments(args
);
202 fragment
.show(getFragmentManager(), "datePicker");
204 final String
[] parts
= dateTimeTextView
.getText().toString().split(":");
205 final TimePickerFragment fragment
= new TimePickerFragment();
206 final Bundle args
= new Bundle();
207 args
.putInt(TimePickerFragment
.ARGUMENT_HOUR
, Integer
.parseInt(parts
[0]));
208 args
.putInt(TimePickerFragment
.ARGUMENT_MINUTE
, Integer
.parseInt(parts
[1]));
209 fragment
.setArguments(args
);
210 fragment
.show(getFragmentManager(), "timePicker");
215 public void onTimeSet(final TimePicker picker
, final int hour
, final int minute
) {
216 dateTimeTextView
.setText(String
.format("%02d:%02d",hour
,minute
));
217 if(dateTimeTextView
.getId() == R
.id
.time_start
)
218 ((TextView
)findViewById(R
.id
.time_end
)).setText(String
.format("%2d:%2d",hour
,minute
));
222 public void onDateSet(final DatePicker picker
, final int year
, final int monthOfYear
, final int dayOfMonth
) {
223 final Calendar calendar
=new GregorianCalendar(year
, monthOfYear
, dayOfMonth
);
224 dateTimeTextView
.setText(Utils
.formatDate(year
, monthOfYear
, dayOfMonth
));
225 //calendar.add(Calendar.DAY_OF_MONTH, 1);
226 if(dateTimeTextView
.getId() == R
.id
.date_start
)
227 ((TextView
)findViewById(R
.id
.date_end
)).setText(Utils
.formatDate(calendar
.get(Calendar
.YEAR
), calendar
.get(Calendar
.MONTH
), calendar
.get(Calendar
.DAY_OF_MONTH
)));