]> iEval git - unical.git/blob - src/ro/ieval/unical/AddEventActivity.java
AddEvent - creates calendar if it doesn't exist
[unical.git] / src / ro / ieval / unical / AddEventActivity.java
1 package ro.ieval.unical;
2 import java.text.ParseException;
3 import java.text.SimpleDateFormat;
4 import java.util.Calendar;
5 import java.util.Date;
6 import java.util.GregorianCalendar;
7
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.*;
25
26 public final class AddEventActivity extends Activity implements OnDateSetListener, OnTimeSetListener{
27
28 private class OnCheckedChangeListener implements android.widget.CompoundButton.OnCheckedChangeListener{
29 private final LinearLayout layout;
30 public OnCheckedChangeListener(final LinearLayout layout){
31 this.layout=layout;
32 }
33
34 private class OnItemSelectedListener implements android.widget.AdapterView.OnItemSelectedListener{
35 private final View repeatCount;
36 private final View repeatUntil;
37
38 public OnItemSelectedListener(final View repeatCount, final View repeatUntil) {
39 this.repeatCount=repeatCount;
40 this.repeatUntil=repeatUntil;
41 }
42
43 @Override
44 public void onItemSelected(final AdapterView<?> whatever, final View ignored, final int pos, final long id) {
45 switch(pos){
46 case 0:
47 repeatCount.setVisibility(View.GONE);
48 repeatUntil.setVisibility(View.GONE);
49 break;
50 case 1:
51 repeatCount.setVisibility(View.GONE);
52 repeatUntil.setVisibility(View.VISIBLE);
53 break;
54 case 2:
55 repeatCount.setVisibility(View.VISIBLE);
56 repeatUntil.setVisibility(View.GONE);
57 break;
58 default:
59 }
60 }
61
62 @Override public void onNothingSelected(final AdapterView<?> arg0) { /* do nothing */ }
63 }
64
65 @Override
66 public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
67 if(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)));
71 }else {
72 Utils.setEnabledRecursively(layout, false);
73 }
74 }
75 }
76
77 private TextView dateTimeTextView;
78 public static final String EXTRA_EVENT = "event";
79 private Event event;
80
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);
84 c.startActivity(i);
85 }
86
87
88 @Override
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);
95
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);
100
101 repeatSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(layout));
102
103 EditText title = (EditText) findViewById(R.id.eventtitle);
104 title.setText(event.title);
105
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());
113
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);
123 }
124 }
125
126 @Override
127 public boolean onCreateOptionsMenu(final Menu menu) {
128 final MenuInflater inflater = getMenuInflater();
129 inflater.inflate(R.menu.add_event, menu);
130 return true;
131 }
132
133 public void createCalendar() {
134 Uri calUri,result;
135 ContentValues cv;
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)
151 .build();
152
153 this.getContentResolver().insert(calUri, cv);
154 Event.calendarCreated=true;
155 }
156
157 @Override
158 public boolean onOptionsItemSelected(final MenuItem item) {
159 switch (item.getItemId()) {
160 case R.id.cancel:
161 finish();
162 return true;
163 case R.id.save:
164
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();
169
170 try {
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) {
176 e.printStackTrace();
177 }
178
179 ev.description=((EditText) findViewById(R.id.description)).getText().toString();
180
181 if(!Event.calendarCreated) createCalendar();
182
183 Toast toast = Toast.makeText(getApplicationContext(),"Event saved!",Toast.LENGTH_LONG);
184 toast.show();
185 finish();
186 return true;
187 default:
188 return false;
189 }
190 }
191
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");
203 } else {
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");
211 }
212 }
213
214 @Override
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));
219 }
220
221 @Override
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)));
228 }
229 }
This page took 0.056109 seconds and 4 git commands to generate.