]> iEval git - unical.git/blob - src/ro/ieval/unical/AddEventActivity.java
Now events are added to calendar. Edit also works now
[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 import java.util.TimeZone;
8
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.*;
26
27 public final class AddEventActivity extends Activity implements OnDateSetListener, OnTimeSetListener{
28
29 private class OnCheckedChangeListener implements android.widget.CompoundButton.OnCheckedChangeListener{
30 private final LinearLayout layout;
31 public OnCheckedChangeListener(final LinearLayout layout){
32 this.layout=layout;
33 }
34
35 private class OnItemSelectedListener implements android.widget.AdapterView.OnItemSelectedListener{
36 private final View repeatCount;
37 private final View repeatUntil;
38
39 public OnItemSelectedListener(final View repeatCount, final View repeatUntil) {
40 this.repeatCount=repeatCount;
41 this.repeatUntil=repeatUntil;
42 }
43
44 @Override
45 public void onItemSelected(final AdapterView<?> whatever, final View ignored, final int pos, final long id) {
46 switch(pos){
47 case 0:
48 repeatCount.setVisibility(View.GONE);
49 repeatUntil.setVisibility(View.GONE);
50 break;
51 case 1:
52 repeatCount.setVisibility(View.GONE);
53 repeatUntil.setVisibility(View.VISIBLE);
54 break;
55 case 2:
56 repeatCount.setVisibility(View.VISIBLE);
57 repeatUntil.setVisibility(View.GONE);
58 break;
59 default:
60 }
61 }
62
63 @Override public void onNothingSelected(final AdapterView<?> arg0) { /* do nothing */ }
64 }
65
66 @Override
67 public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
68 if(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)));
72 }else {
73 Utils.setEnabledRecursively(layout, false);
74 }
75 }
76 }
77
78 private TextView dateTimeTextView;
79 public static final String EXTRA_EVENT = "event";
80 private static Event event=new Event();
81
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);
85 event=e;
86 c.startActivity(i);
87 }
88
89
90 @Override
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);
97
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);
102
103 repeatSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(layout));
104
105 EditText title = (EditText) findViewById(R.id.eventtitle);
106 title.setText(event.title);
107
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());
115
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);
125 }
126 }
127
128 @Override
129 public boolean onCreateOptionsMenu(final Menu menu) {
130 final MenuInflater inflater = getMenuInflater();
131 inflater.inflate(R.menu.add_event, menu);
132 return true;
133 }
134
135 public void createCalendar() {
136 Uri calUri,result;
137 ContentValues cv;
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)
154 .build();
155
156 this.getContentResolver().delete(calUri, CalendarContract.Calendars.ACCOUNT_NAME + " = 'unical'", new String[0]);
157 this.getContentResolver().insert(calUri, cv);
158 Event.calendarCreated=true;
159 }
160
161 @Override
162 public boolean onOptionsItemSelected(final MenuItem item) {
163 switch (item.getItemId()) {
164 case R.id.cancel:
165 finish();
166 return true;
167 case R.id.save:
168
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();
173
174 try {
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) {
180 e.printStackTrace();
181 }
182
183 event.description=((EditText) findViewById(R.id.description)).getText().toString();
184
185 if(!Event.calendarCreated)createCalendar();
186 event.calendarID=Event.getUnicalId(this);
187 event.insert(this);
188
189 Toast toast = Toast.makeText(getApplicationContext(),"Event saved!",Toast.LENGTH_LONG);
190 toast.show();
191 finish();
192 return true;
193 default:
194 return false;
195 }
196 }
197
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");
209 } else {
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");
217 }
218 }
219
220 @Override
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));
225 }
226
227 @Override
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)));
234 }
235 }
This page took 0.055088 seconds and 4 git commands to generate.