Clover Coverage Report - RaspberryBusMalaysiaActivity Coverage Report
Coverage timestamp: mar dic 23 2014 15:39:35 EST
../../../img/srcFileCovDistChart8.png 72% of files have more coverage
347   777   92   6,8
58   664   0,27   17
51     1,8  
3    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  SubmitTripActivity       Line # 61 327 83 80,9% 0.80930233
  SubmitTripActivity.DataWrapper       Line # 81 3 1 100% 1.0
  SubmitTripActivity.PostTask       Line # 541 17 8 77,3% 0.77272725
 
No Tests
 
1    /*
2    Copyright (C) 2012,2013 Sweetie Piggy Apps <sweetiepiggyapps@gmail.com>
3   
4    This file is part of Raspberry Bus Malaysia.
5   
6    Raspberry Bus Malaysia is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10   
11    Raspberry Bus Malaysia is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14    GNU General Public License for more details.
15   
16    You should have received a copy of the GNU General Public License
17    along with Raspberry Bus Malaysia; if not, see <http://www.gnu.org/licenses/>.
18    */
19   
20    package com.sweetiepiggy.raspberrybusmalaysia;
21   
22    import java.io.IOException;
23    import java.io.UnsupportedEncodingException;
24    import java.text.SimpleDateFormat;
25    import java.util.ArrayList;
26    import java.util.Calendar;
27    import java.util.GregorianCalendar;
28    import java.util.List;
29   
30    import org.apache.http.HttpResponse;
31    import org.apache.http.NameValuePair;
32    import org.apache.http.client.ClientProtocolException;
33    import org.apache.http.client.HttpClient;
34    import org.apache.http.client.entity.UrlEncodedFormEntity;
35    import org.apache.http.client.methods.HttpPost;
36    import org.apache.http.impl.client.DefaultHttpClient;
37    import org.apache.http.message.BasicNameValuePair;
38   
39    import android.app.Activity;
40    import android.app.AlertDialog;
41    import android.app.DatePickerDialog;
42    import android.app.Dialog;
43    import android.app.TimePickerDialog;
44    import android.content.Context;
45    import android.content.DialogInterface;
46    import android.content.Intent;
47    import android.database.Cursor;
48    import android.os.AsyncTask;
49    import android.os.Bundle;
50    import android.text.format.DateFormat;
51    import android.view.View;
52    import android.widget.ArrayAdapter;
53    import android.widget.AutoCompleteTextView;
54    import android.widget.Button;
55    import android.widget.DatePicker;
56    import android.widget.EditText;
57    import android.widget.RatingBar;
58    import android.widget.TimePicker;
59    import android.widget.Toast;
60   
 
61    public class SubmitTripActivity extends Activity
62    {
63    private DataWrapper mData;
64    private DbAdapter mDbHelper;
65   
66    private static final int SCHED_DATE_DIALOG_ID = 0;
67    private static final int SCHED_TIME_DIALOG_ID = 1;
68    private static final int DEPART_DATE_DIALOG_ID = 2;
69    private static final int DEPART_TIME_DIALOG_ID = 3;
70    private static final int ARRIVAL_DATE_DIALOG_ID = 4;
71    private static final int ARRIVAL_TIME_DIALOG_ID = 5;
72   
73    private static final int ACTIVITY_FROM = 0;
74    private static final int ACTIVITY_TO = 1;
75   
76    /* TODO: move this to Constants.java */
77    private static final String EMAIL_ADDRESS = "sweetiepiggyapps@gmail.com";
78    private static final String EMAIL_SUBJECT = "Raspberry Bus Malaysia Trip Submission";
79    private static final String POST_WEBSITE = "http://raspberrybusmalaysia.appspot.com/submit_trip";
80   
 
81    private class DataWrapper
82    {
83    public Calendar sched_time;
84    public Calendar depart_time;
85    public Calendar arrival_time;
86   
 
87  1 toggle public DataWrapper()
88    {
89  1 sched_time = new GregorianCalendar();
90  1 depart_time = new GregorianCalendar();
91  1 arrival_time = new GregorianCalendar();
92    }
93    }
94   
 
95  1 toggle @Override
96    public void onCreate(Bundle savedInstanceState)
97    {
98  1 super.onCreate(savedInstanceState);
99  1 setContentView(R.layout.submit_trip);
100   
101  1 mDbHelper = new DbAdapter();
102  1 mDbHelper.open(this);
103   
104  1 if (savedInstanceState == null) {
105  1 mData = (DataWrapper) getLastNonConfigurationInstance();
106  1 if (mData == null) {
107  1 mData = new DataWrapper();
108  1 init_vars(mData);
109  1 init_entries();
110    }
111    } else {
112  0 mData = new DataWrapper();
113  0 restore_saved_state(savedInstanceState);
114    }
115   
116  1 init_date_time_buttons();
117  1 init_map_buttons();
118  1 init_cancel_button();
119  1 init_submit_button();
120    }
121   
 
122  1 toggle @Override
123    protected void onDestroy()
124    {
125  1 String sched_time = format_time(mData.sched_time);
126  1 String depart_time = format_time(mData.depart_time);
127  1 String arrival_time = format_time(mData.arrival_time);
128  1 String agent = ((AutoCompleteTextView) findViewById(R.id.agent_entry)).getText().toString();
129  1 String operator = ((AutoCompleteTextView) findViewById(R.id.operator_entry)).getText().toString();
130  1 String from_station = ((AutoCompleteTextView) findViewById(R.id.from_station_entry)).getText().toString();
131  1 String to_station = ((AutoCompleteTextView) findViewById(R.id.to_station_entry)).getText().toString();
132  1 int safety = (int) ((RatingBar) findViewById(R.id.safety_bar)).getRating();
133  1 int comfort = (int) ((RatingBar) findViewById(R.id.comfort_bar)).getRating();
134  1 int overall = (int) ((RatingBar) findViewById(R.id.overall_bar)).getRating();
135  1 String comment = ((EditText) findViewById(R.id.comment_entry)).getText().toString();
136   
137  1 if (mDbHelper != null) {
138  1 mDbHelper.save_tmp(agent, operator, from_station,
139    to_station, sched_time, depart_time,
140    arrival_time, safety, comfort, overall,
141    comment);
142  1 mDbHelper.close();
143    }
144  1 super.onDestroy();
145    }
146   
 
147  2 toggle @Override
148    public void onSaveInstanceState(Bundle savedInstanceState)
149    {
150  2 savedInstanceState.putInt("sched_year", mData.sched_time.get(Calendar.YEAR));
151  2 savedInstanceState.putInt("sched_month", mData.sched_time.get(Calendar.MONTH));
152  2 savedInstanceState.putInt("sched_day", mData.sched_time.get(Calendar.DAY_OF_MONTH));
153  2 savedInstanceState.putInt("sched_hour", mData.sched_time.get(Calendar.HOUR_OF_DAY));
154  2 savedInstanceState.putInt("sched_minute", mData.sched_time.get(Calendar.MINUTE));
155   
156  2 savedInstanceState.putInt("depart_year", mData.depart_time.get(Calendar.YEAR));
157  2 savedInstanceState.putInt("depart_month", mData.depart_time.get(Calendar.MONTH));
158  2 savedInstanceState.putInt("depart_day", mData.depart_time.get(Calendar.DAY_OF_MONTH));
159  2 savedInstanceState.putInt("depart_hour", mData.depart_time.get(Calendar.HOUR_OF_DAY));
160  2 savedInstanceState.putInt("depart_minute", mData.depart_time.get(Calendar.MINUTE));
161   
162  2 savedInstanceState.putInt("arrival_year", mData.arrival_time.get(Calendar.YEAR));
163  2 savedInstanceState.putInt("arrival_month", mData.arrival_time.get(Calendar.MONTH));
164  2 savedInstanceState.putInt("arrival_day", mData.arrival_time.get(Calendar.DAY_OF_MONTH));
165  2 savedInstanceState.putInt("arrival_hour", mData.arrival_time.get(Calendar.HOUR_OF_DAY));
166  2 savedInstanceState.putInt("arrival_minute", mData.arrival_time.get(Calendar.MINUTE));
167   
168  2 super.onSaveInstanceState(savedInstanceState);
169    }
170   
 
171  0 toggle @Override
172    public void onRestoreInstanceState(Bundle savedInstanceState)
173    {
174  0 super.onRestoreInstanceState(savedInstanceState);
175  0 restore_saved_state(savedInstanceState);
176    }
177   
 
178  0 toggle private void restore_saved_state(Bundle savedInstanceState)
179    {
180  0 int year = savedInstanceState.getInt("sched_year");
181  0 int month = savedInstanceState.getInt("sched_month");
182  0 int day = savedInstanceState.getInt("sched_day");
183  0 int hour = savedInstanceState.getInt("sched_hour");
184  0 int minute = savedInstanceState.getInt("sched_minute");
185  0 mData.sched_time.set(year, month, day, hour, minute);
186   
187  0 year = savedInstanceState.getInt("depart_year");
188  0 month = savedInstanceState.getInt("depart_month");
189  0 day = savedInstanceState.getInt("depart_day");
190  0 hour = savedInstanceState.getInt("depart_hour");
191  0 minute = savedInstanceState.getInt("depart_minute");
192  0 mData.depart_time.set(year, month, day, hour, minute);
193   
194  0 year = savedInstanceState.getInt("arrival_year");
195  0 month = savedInstanceState.getInt("arrival_month");
196  0 day = savedInstanceState.getInt("arrival_day");
197  0 hour = savedInstanceState.getInt("arrival_hour");
198  0 minute = savedInstanceState.getInt("arrival_minute");
199  0 mData.arrival_time.set(year, month, day, hour, minute);
200    }
201   
 
202  0 toggle @Override
203    public Object onRetainNonConfigurationInstance()
204    {
205  0 return mData;
206    }
207   
 
208  3 toggle private void init_date_time_buttons()
209    {
210  3 init_date_button(R.id.sched_date_button, SCHED_DATE_DIALOG_ID);
211  3 init_date_button(R.id.depart_date_button, DEPART_DATE_DIALOG_ID);
212  3 init_date_button(R.id.arrival_date_button, ARRIVAL_DATE_DIALOG_ID);
213   
214  3 init_time_button(R.id.sched_time_button, SCHED_TIME_DIALOG_ID);
215  3 init_time_button(R.id.depart_time_button, DEPART_TIME_DIALOG_ID);
216  3 init_time_button(R.id.arrival_time_button, ARRIVAL_TIME_DIALOG_ID);
217   
218  3 init_now_buttons();
219   
220  3 update_date_label(R.id.sched_date_button, mData.sched_time);
221  3 update_time_label(R.id.sched_time_button, mData.sched_time);
222  3 update_date_label(R.id.depart_date_button, mData.depart_time);
223  3 update_time_label(R.id.depart_time_button, mData.depart_time);
224  3 update_date_label(R.id.arrival_date_button, mData.arrival_time);
225  3 update_time_label(R.id.arrival_time_button, mData.arrival_time);
226    }
227   
 
228  9 toggle private void init_date_button(int button_id, final int dialog_id)
229    {
230  9 Button date_button = (Button)findViewById(button_id);
231  9 date_button.setOnClickListener(new View.OnClickListener() {
 
232  3 toggle public void onClick(View v) {
233  3 showDialog(dialog_id);
234    }
235    });
236   
237    }
238   
 
239  9 toggle private void init_time_button(int button_id, final int dialog_id)
240    {
241  9 Button time_button = (Button) findViewById(button_id);
242  9 time_button.setOnClickListener(new View.OnClickListener() {
 
243  2 toggle public void onClick(View v) {
244  2 showDialog(dialog_id);
245    }
246    });
247    }
248   
 
249  3 toggle private void init_now_buttons()
250    {
251  3 Button sched_now_button = (Button) findViewById(R.id.sched_now_button);
252  3 sched_now_button.setOnClickListener(new View.OnClickListener() {
 
253  1 toggle public void onClick(View v) {
254  1 mData.sched_time = new GregorianCalendar();
255   
256  1 update_date_label(R.id.sched_date_button, mData.sched_time);
257  1 update_time_label(R.id.sched_time_button, mData.sched_time);
258    }
259    });
260   
261  3 Button depart_now_button = (Button) findViewById(R.id.depart_now_button);
262  3 depart_now_button.setOnClickListener(new View.OnClickListener() {
 
263  1 toggle public void onClick(View v) {
264  1 mData.depart_time = new GregorianCalendar();
265   
266  1 update_date_label(R.id.depart_date_button, mData.depart_time);
267  1 update_time_label(R.id.depart_time_button, mData.depart_time);
268    }
269    });
270   
271  3 Button arrival_now_button = (Button) findViewById(R.id.arrival_now_button);
272  3 arrival_now_button.setOnClickListener(new View.OnClickListener() {
 
273  1 toggle public void onClick(View v) {
274  1 mData.arrival_time = new GregorianCalendar();
275   
276  1 update_date_label(R.id.arrival_date_button, mData.arrival_time);
277  1 update_time_label(R.id.arrival_time_button, mData.arrival_time);
278    }
279    });
280    }
281   
 
282  3 toggle private void init_vars(DataWrapper data)
283    {
284  3 Cursor c_sched_time = mDbHelper.fetch_tmp_sched_time();
285  3 init_time(c_sched_time, data.sched_time);
286   
287  3 Cursor c_depart_time = mDbHelper.fetch_tmp_depart_time();
288  3 init_time(c_depart_time, data.depart_time);
289   
290  3 Cursor c_arrival_time = mDbHelper.fetch_tmp_arrival_time();
291  3 init_time(c_arrival_time, data.arrival_time);
292    }
293   
 
294  9 toggle private void init_time(Cursor c, Calendar cal)
295    {
296    /* restore time from database */
297  9 if (c.moveToFirst()) {
298  0 int year = Integer.parseInt(c.getString(1));
299  0 int month = Integer.parseInt(c.getString(2)) - 1;
300  0 int day = Integer.parseInt(c.getString(3));
301  0 int hour = Integer.parseInt(c.getString(4));
302  0 int minute = Integer.parseInt(c.getString(5));
303   
304  0 cal.set(year, month, day, hour, minute);
305    /* use current time */
306    } else {
307  9 cal = new GregorianCalendar();
308    }
309    }
310   
 
311  3 toggle private void init_entries()
312    {
313  3 update_station_autocomplete(R.id.to_station_entry);
314  3 update_station_autocomplete(R.id.from_station_entry);
315  3 update_agent_autocomplete(R.id.agent_entry);
316  3 update_operator_autocomplete(R.id.operator_entry);
317   
318  3 String from_station = mDbHelper.fetch_tmp(DbAdapter.KEY_FROM_STN);
319  3 String to_station = mDbHelper.fetch_tmp(DbAdapter.KEY_TO_STN);
320  3 String agent = mDbHelper.fetch_tmp(DbAdapter.KEY_AGENT);
321  3 String operator = mDbHelper.fetch_tmp(DbAdapter.KEY_OPERATOR);
322  3 String comment = mDbHelper.fetch_tmp(DbAdapter.KEY_COMMENT);
323  3 int safety = mDbHelper.fetch_safety();
324  3 int comfort = mDbHelper.fetch_comfort();
325  3 int overall = mDbHelper.fetch_overall();
326   
327  3 ((AutoCompleteTextView) findViewById(R.id.from_station_entry)).setText(from_station);
328  3 ((AutoCompleteTextView) findViewById(R.id.to_station_entry)).setText(to_station);
329  3 ((AutoCompleteTextView) findViewById(R.id.agent_entry)).setText(agent);
330  3 ((AutoCompleteTextView) findViewById(R.id.operator_entry)).setText(operator);
331   
332  3 ((RatingBar) findViewById(R.id.safety_bar)).setRating(safety);
333  3 ((RatingBar) findViewById(R.id.comfort_bar)).setRating(comfort);
334  3 ((RatingBar) findViewById(R.id.overall_bar)).setRating(overall);
335  3 ((EditText) findViewById(R.id.comment_entry)).setText(comment);
336    }
337   
 
338  6 toggle private void update_station_autocomplete(int id)
339    {
340  6 ArrayAdapter<String> stations = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);
341  6 Cursor c = mDbHelper.fetch_stations();
342  6 if (c.moveToFirst()) do {
343  558 stations.add(c.getString(c.getColumnIndex(DbAdapter.KEY_STN)));
344  558 } while (c.moveToNext());
345  6 c.close();
346  6 AutoCompleteTextView stations_entry = (AutoCompleteTextView) findViewById(id);
347  6 stations_entry.setThreshold(1);
348  6 stations_entry.setAdapter(stations);
349    }
350   
 
351  3 toggle private void update_agent_autocomplete(int id)
352    {
353  3 ArrayAdapter<String> agents = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);
354  3 Cursor c = mDbHelper.fetch_agents();
355  3 if (c.moveToFirst()) do {
356  75 agents.add(c.getString(c.getColumnIndex(DbAdapter.KEY_AGENT)));
357  75 } while (c.moveToNext());
358  3 c.close();
359  3 AutoCompleteTextView agents_entry = (AutoCompleteTextView) findViewById(id);
360  3 agents_entry.setThreshold(1);
361  3 agents_entry.setAdapter(agents);
362    }
363   
 
364  3 toggle private void update_operator_autocomplete(int id)
365    {
366  3 ArrayAdapter<String> operators = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);
367  3 Cursor c = mDbHelper.fetch_operators();
368  3 if (c.moveToFirst()) do {
369  57 operators.add(c.getString(c.getColumnIndex(DbAdapter.KEY_OPERATOR)));
370  57 } while (c.moveToNext());
371  3 c.close();
372  3 AutoCompleteTextView operators_entry = (AutoCompleteTextView) findViewById(id);
373  3 operators_entry.setThreshold(1);
374  3 operators_entry.setAdapter(operators);
375    }
376   
 
377  18 toggle private void update_date_label(int button_id, Calendar cal)
378    {
379  18 Button date_button = (Button) findViewById(button_id);
380   
381  18 String date = translate_day_of_week(DateFormat.format("EEEE", cal.getTime()).toString()) +
382    " " + DateFormat.getLongDateFormat(getApplicationContext()).format(cal.getTime());
383  18 date_button.setText(date);
384    }
385   
386    /* TODO: there should be a better way to get the translated day of week */
 
387  18 toggle private String translate_day_of_week(String day)
388    {
389  18 String ret = day;
390  18 if (day.equals("Monday")) {
391  0 ret = getResources().getString(R.string.monday);
392  18 } else if (day.equals("Tuesday")) {
393  14 ret = getResources().getString(R.string.tuesday);
394  4 } else if (day.equals("Wednesday")) {
395  0 ret = getResources().getString(R.string.wednesday);
396  4 } else if (day.equals("Thursday")) {
397  0 ret = getResources().getString(R.string.thursday);
398  4 } else if (day.equals("Friday")) {
399  0 ret = getResources().getString(R.string.friday);
400  4 } else if (day.equals("Saturday")) {
401  0 ret = getResources().getString(R.string.saturday);
402  4 } else if (day.equals("Sunday")) {
403  4 ret = getResources().getString(R.string.sunday);
404    }
405  18 return ret;
406    }
407   
 
408  16 toggle private void update_time_label(int button_id, Calendar cal)
409    {
410  16 Button time_button = (Button)findViewById(button_id);
411  16 String time = DateFormat.getTimeFormat(getApplicationContext()).format(cal.getTime());
412  16 time_button.setText(time);
413    }
414   
 
415  1 toggle private void init_submit_button()
416    {
417  1 Button submit_button = (Button) findViewById(R.id.submit_button);
418  1 submit_button.setOnClickListener(new View.OnClickListener() {
 
419  3 toggle public void onClick(View v) {
420  3 boolean results_complete = false;
421  3 String incomplete_msg = "";
422  3 Calendar now = new GregorianCalendar();
423   
424  3 if (!mData.arrival_time.after(mData.depart_time)) {
425  0 incomplete_msg = getResources().getString(R.string.depart_before_arrival);
426  3 } else if (mData.arrival_time.after(now)) {
427  1 incomplete_msg = getResources().getString(R.string.future_arrival);
428  2 } else if (mData.depart_time.after(now)) {
429  0 incomplete_msg = getResources().getString(R.string.future_departure);
430  2 } else if (mData.sched_time.after(now)) {
431  0 incomplete_msg = getResources().getString(R.string.future_scheduled);
432    } else {
433  2 results_complete = true;
434    }
435   
436  3 if (results_complete) {
437  2 submit();
438    } else {
439  1 Toast.makeText(getApplicationContext(), incomplete_msg,
440    Toast.LENGTH_SHORT).show();
441    }
442    }
443    });
444    }
445   
 
446  1 toggle private void init_map_buttons()
447    {
448  1 Button from_map_button = (Button) findViewById(R.id.from_map_button);
449  1 from_map_button.setOnClickListener(new View.OnClickListener() {
 
450  1 toggle public void onClick(View v) {
451  1 Intent intent = new Intent(getApplicationContext(), RbmMapActivity.class);
452  1 Bundle b = new Bundle();
453  1 b.putBoolean("set_result", true);
454  1 intent.putExtras(b);
455  1 startActivityForResult(intent, ACTIVITY_FROM);
456    }
457    });
458   
459  1 Button to_map_button = (Button) findViewById(R.id.to_map_button);
460  1 to_map_button.setOnClickListener(new View.OnClickListener() {
 
461  1 toggle public void onClick(View v) {
462  1 Intent intent = new Intent(getApplicationContext(), RbmMapActivity.class);
463  1 Bundle b = new Bundle();
464  1 b.putBoolean("set_result", true);
465  1 intent.putExtras(b);
466  1 startActivityForResult(intent, ACTIVITY_TO);
467    }
468    });
469    }
470   
 
471  1 toggle private void init_cancel_button()
472    {
473  1 Button cancel_button = (Button)findViewById(R.id.cancel_button);
474  1 cancel_button.setOnClickListener(new View.OnClickListener() {
 
475  2 toggle public void onClick(View v) {
476  2 mDbHelper.clear_tmp_table();
477  2 init_vars(mData);
478    /* TODO: why aren't these set correctly by init_vars() ? */
479  2 mData.sched_time = new GregorianCalendar();
480  2 mData.depart_time = new GregorianCalendar();
481  2 mData.arrival_time = new GregorianCalendar();
482  2 init_entries();
483  2 init_date_time_buttons();
484    }
485    });
486    }
487   
 
488  2 toggle private void submit()
489    {
490  2 final String sched_time = format_time(mData.sched_time);
491  2 final String depart_time = format_time(mData.depart_time);
492  2 final String arrival_time = format_time(mData.arrival_time);
493  2 final String agent = ((AutoCompleteTextView) findViewById(R.id.agent_entry)).getText().toString();
494  2 final String operator = ((AutoCompleteTextView) findViewById(R.id.operator_entry)).getText().toString();
495  2 final String from_station = ((AutoCompleteTextView) findViewById(R.id.from_station_entry)).getText().toString();
496  2 final String to_station = ((AutoCompleteTextView) findViewById(R.id.to_station_entry)).getText().toString();
497  2 final String safety = Integer.toString((int) ((RatingBar) findViewById(R.id.safety_bar)).getRating());
498  2 final String comfort = Integer.toString((int) ((RatingBar) findViewById(R.id.comfort_bar)).getRating());
499  2 final String overall = Integer.toString((int) ((RatingBar) findViewById(R.id.overall_bar)).getRating());
500  2 final String comment = ((EditText) findViewById(R.id.comment_entry)).getText().toString();
501   
502  2 String disp_sched = DateFormat.getTimeFormat(getApplicationContext()).format(mData.sched_time.getTime());
503  2 String trip_time = format_time((mData.arrival_time.getTimeInMillis() -
504    mData.sched_time.getTimeInMillis()) / 1000);
505  2 String delay = format_time_min((mData.depart_time.getTimeInMillis() -
506    mData.sched_time.getTimeInMillis()) / 1000);
507  2 String info = getResources().getString(R.string.sched_time) + ": " + disp_sched + "\n" +
508    getResources().getString(R.string.trip_time) + ": " + trip_time + "\n" +
509    getResources().getString(R.string.delay) + ": " + delay;
510   
511  2 AlertDialog.Builder alert = new AlertDialog.Builder(this);
512  2 alert.setTitle(R.string.confirm_submit);
513  2 alert.setMessage(info);
514  2 alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
 
515  1 toggle public void onClick(DialogInterface dialog, int which) {
516  1 String msg = format_email(agent, operator,
517    from_station, to_station, sched_time,
518    depart_time, arrival_time, safety,
519    comfort, overall, comment);
520   
521  1 new PostTask(getApplicationContext(), msg).execute();
522    }
523    });
524  2 alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
 
525  1 toggle public void onClick(DialogInterface dialog, int which) {
526    }
527    });
528  2 alert.show();
529    }
530   
 
531  0 toggle private void send_email(String msg)
532    {
533  0 Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
534  0 intent.putExtra(Intent.EXTRA_EMAIL, new String[] {EMAIL_ADDRESS} );
535  0 intent.putExtra(Intent.EXTRA_SUBJECT, EMAIL_SUBJECT);
536  0 intent.putExtra(Intent.EXTRA_TEXT, msg);
537  0 intent.setType("text/plain");
538  0 startActivity(Intent.createChooser(intent, getResources().getString(R.string.send_email)));
539    }
540   
 
541    private class PostTask extends AsyncTask<Void, Void, Void>
542    {
543    private Context mCtx;
544    private String mMsg;
545    private int mStatusCode = 0;
546   
 
547  1 toggle public PostTask(Context ctx, String msg)
548    {
549  1 mCtx = ctx;
550  1 mMsg = msg;
551    }
552   
 
553  1 toggle @Override
554    protected Void doInBackground(Void... params)
555    {
556  1 HttpClient hc = new DefaultHttpClient();
557  1 HttpPost hp = new HttpPost(POST_WEBSITE);
558  1 List<NameValuePair> l = new ArrayList<NameValuePair>(2);
559  1 l.add(new BasicNameValuePair("msg", mMsg));
560   
561  1 try {
562  1 hp.setEntity(new UrlEncodedFormEntity(l, "UTF-8"));
563  1 HttpResponse response = hc.execute(hp);
564  1 mStatusCode = response.getStatusLine().getStatusCode();
565   
566    } catch (ClientProtocolException e) {
567  0 throw new Error(e);
568    } catch (UnsupportedEncodingException e) {
569  0 throw new Error(e);
570    /* probably no internet connection? */
571    } catch (IOException e) {
572  0 mStatusCode = 1;
573    }
574   
575  1 return null;
576    }
577   
 
578  1 toggle @Override
579    protected void onPostExecute(Void result)
580    {
581  1 if (200 <= mStatusCode && mStatusCode < 300) {
582  1 Toast.makeText(mCtx,
583    mCtx.getResources().getString(R.string.submit_trip_success),
584    Toast.LENGTH_SHORT).show();
585    } else {
586  0 send_email(mMsg);
587    }
588    }
589    }
590   
 
591  1 toggle private String format_email(String agent, String operator,
592    String from_station, String to_station,
593    String scheduled_departure, String actual_departure,
594    String arrival_time, String safety, String comfort,
595    String overall, String comment)
596    {
597  1 return "Agent: " + agent + "\n" +
598    "Operator: " + operator + "\n" +
599    "From: " + from_station + "\n" +
600    "To: " + to_station + "\n" +
601    "Scheduled departure: " + scheduled_departure + "\n" +
602    "Actual departure: " + actual_departure + "\n" +
603    "Arrival time: " + arrival_time + "\n" +
604    "Safety: " + safety + "\n" +
605    "Comfort: " + comfort + "\n" +
606    "Overall: " + overall + "\n" +
607    "Comment: " + comment + "\n";
608    }
609   
 
610  9 toggle private String format_time(Calendar cal)
611    {
612  9 return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(cal.getTime());
613    }
614   
615    /* TODO: move format_time() and format_time_min() to their own class */
 
616  2 toggle private String format_time_min(long time)
617    {
618  2 String negative = "";
619  2 if (time < 0) {
620  0 negative = "-";
621  0 time *= -1;
622    }
623   
624  2 long min = time / 60;
625  2 return String.format("%s%d%s", negative, min, getResources().getString(R.string.minute_abbr));
626    }
627   
 
628  2 toggle private String format_time(long time)
629    {
630  2 String negative = "";
631  2 if (time < 0) {
632  0 negative = "-";
633  0 time *= -1;
634    }
635   
636  2 long hr = time / 3600;
637  2 time -= hr * 3600;
638  2 long min = time / 60;
639  2 return String.format("%s%d%s %02d%s", negative, hr, getResources().getString(R.string.hour_abbr),
640    min, getResources().getString(R.string.minute_abbr));
641    }
642   
 
643  5 toggle @Override
644    protected Dialog onCreateDialog(int id)
645    {
646  5 DatePickerDialog.OnDateSetListener sched_date_listener =
647    new DatePickerDialog.OnDateSetListener() {
 
648  2 toggle public void onDateSet(DatePicker view, int year,
649    int monthOfYear, int dayOfMonth) {
650  2 mData.sched_time.set(year, monthOfYear, dayOfMonth);
651  2 update_date_label(R.id.sched_date_button,
652    mData.sched_time);
653    }
654    };
655   
656  5 TimePickerDialog.OnTimeSetListener sched_time_listener =
657    new TimePickerDialog.OnTimeSetListener() {
 
658  2 toggle public void onTimeSet(TimePicker view,
659    int hourOfDay, int minute) {
660  2 int year = mData.sched_time.get(Calendar.YEAR);
661  2 int month = mData.sched_time.get(Calendar.MONTH);
662  2 int day = mData.sched_time.get(Calendar.DAY_OF_MONTH);
663  2 mData.sched_time.set(year, month, day, hourOfDay, minute);
664  2 update_time_label(R.id.sched_time_button,
665    mData.sched_time);
666    }
667    };
668   
669  5 DatePickerDialog.OnDateSetListener depart_date_listener =
670    new DatePickerDialog.OnDateSetListener() {
 
671  2 toggle public void onDateSet(DatePicker view, int year,
672    int monthOfYear, int dayOfMonth) {
673  2 mData.depart_time.set(year, monthOfYear, dayOfMonth);
674  2 update_date_label(R.id.depart_date_button,
675    mData.depart_time);
676    }
677    };
678   
679  5 TimePickerDialog.OnTimeSetListener depart_time_listener =
680    new TimePickerDialog.OnTimeSetListener() {
 
681  2 toggle public void onTimeSet(TimePicker view,
682    int hourOfDay, int minute) {
683  2 int year = mData.depart_time.get(Calendar.YEAR);
684  2 int month = mData.depart_time.get(Calendar.MONTH);
685  2 int day = mData.depart_time.get(Calendar.DAY_OF_MONTH);
686  2 mData.depart_time.set(year, month, day, hourOfDay, minute);
687  2 update_time_label(R.id.depart_time_button,
688    mData.depart_time);
689    }
690    };
691  5 DatePickerDialog.OnDateSetListener arrival_date_listener =
692    new DatePickerDialog.OnDateSetListener() {
 
693  2 toggle public void onDateSet(DatePicker view, int year,
694    int monthOfYear, int dayOfMonth) {
695  2 mData.arrival_time.set(year, monthOfYear, dayOfMonth);
696  2 update_date_label(R.id.arrival_date_button,
697    mData.arrival_time);
698    }
699    };
700   
701  5 TimePickerDialog.OnTimeSetListener arrival_time_listener =
702    new TimePickerDialog.OnTimeSetListener() {
 
703  0 toggle public void onTimeSet(TimePicker view,
704    int hourOfDay, int minute) {
705  0 int year = mData.arrival_time.get(Calendar.YEAR);
706  0 int month = mData.arrival_time.get(Calendar.MONTH);
707  0 int day = mData.arrival_time.get(Calendar.DAY_OF_MONTH);
708  0 mData.arrival_time.set(year, month, day, hourOfDay, minute);
709  0 update_time_label(R.id.arrival_time_button,
710    mData.arrival_time);
711    }
712    };
713   
714  5 switch (id) {
715  1 case SCHED_DATE_DIALOG_ID:
716  1 return new DatePickerDialog(this, sched_date_listener,
717    mData.sched_time.get(Calendar.YEAR),
718    mData.sched_time.get(Calendar.MONTH),
719    mData.sched_time.get(Calendar.DAY_OF_MONTH));
720  1 case SCHED_TIME_DIALOG_ID:
721  1 return new TimePickerDialog(this, sched_time_listener,
722    mData.sched_time.get(Calendar.HOUR_OF_DAY),
723    mData.sched_time.get(Calendar.MINUTE),
724    false);
725  1 case DEPART_DATE_DIALOG_ID:
726  1 return new DatePickerDialog(this, depart_date_listener,
727    mData.depart_time.get(Calendar.YEAR),
728    mData.depart_time.get(Calendar.MONTH),
729    mData.depart_time.get(Calendar.DAY_OF_MONTH));
730  1 case DEPART_TIME_DIALOG_ID:
731  1 return new TimePickerDialog(this, depart_time_listener,
732    mData.depart_time.get(Calendar.HOUR_OF_DAY),
733    mData.depart_time.get(Calendar.MINUTE),
734    false);
735  1 case ARRIVAL_DATE_DIALOG_ID:
736  1 return new DatePickerDialog(this, arrival_date_listener,
737    mData.arrival_time.get(Calendar.YEAR),
738    mData.arrival_time.get(Calendar.MONTH),
739    mData.arrival_time.get(Calendar.DAY_OF_MONTH));
740  0 case ARRIVAL_TIME_DIALOG_ID:
741  0 return new TimePickerDialog(this, arrival_time_listener,
742    mData.arrival_time.get(Calendar.HOUR_OF_DAY),
743    mData.arrival_time.get(Calendar.MINUTE),
744    false);
745    }
746   
747  0 return null;
748    }
749   
 
750  2 toggle @Override
751    protected void onActivityResult(int request_code, int result_code, Intent data)
752    {
753  2 super.onActivityResult(request_code, result_code, data);
754   
755  2 switch (request_code) {
756  1 case ACTIVITY_FROM:
757  1 if (result_code == RESULT_OK) {
758  1 Bundle b = data.getExtras();
759  1 if (b != null) {
760  1 String station = b.getString("station");
761  1 ((AutoCompleteTextView) findViewById(R.id.from_station_entry)).setText(station);
762    }
763    }
764  1 break;
765  1 case ACTIVITY_TO:
766  1 if (result_code == RESULT_OK) {
767  1 Bundle b = data.getExtras();
768  1 if (b != null) {
769  1 String station = b.getString("station");
770  1 ((AutoCompleteTextView) findViewById(R.id.to_station_entry)).setText(station);
771    }
772    }
773  1 break;
774    }
775    }
776    }
777