--- android/TrainInfo/src/dk/thoerup/traininfo/TimetableList.java 2009/09/30 06:02:03 364 +++ android/TrainInfo/src/dk/thoerup/traininfo/TimetableList.java 2010/05/03 11:19:18 699 @@ -1,40 +1,161 @@ package dk.thoerup.traininfo; -import android.app.ListActivity; + +import static dk.thoerup.traininfo.R.string.generic_cancel; +import static dk.thoerup.traininfo.R.string.generic_retry; +import static dk.thoerup.traininfo.R.string.timetablelist_fetchdata; +import static dk.thoerup.traininfo.R.string.timetablelist_fetcherror; +import static dk.thoerup.traininfo.R.string.timetablelist_nodata; + +import java.util.ArrayList; +import java.util.List; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.app.ProgressDialog; +import android.content.DialogInterface; import android.content.Intent; +import android.os.AsyncTask; import android.os.Bundle; +import android.util.Log; +import android.widget.ListView; import android.widget.TextView; +import dk.thoerup.traininfo.provider.ProviderFactory; +import dk.thoerup.traininfo.provider.TimetableProvider; +import dk.thoerup.traininfo.util.MessageBox; -public class TimetableList extends ListActivity { +public class TimetableList extends Activity { + private static final int DLG_PROGRESS = 8000; DepartureBean departure; + TimetableListAdapter adapter; + TimetableFetcher fetcher; + List timetables; + TimetableProvider provider; + + @SuppressWarnings("unchecked") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.timetablelist); + provider = ProviderFactory.getTimetableProvider(); + + adapter = new TimetableListAdapter(this); + + ListView lv = (ListView) findViewById(R.id.List); + lv.setAdapter(adapter); + + Intent launchedBy = getIntent(); departure = (DepartureBean) launchedBy.getSerializableExtra("departure"); ((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber()); ((TextView)findViewById(R.id.Status)).setText(departure.getStatus()); + ((TextView)findViewById(R.id.Location)).setText(departure.getLocation()); ((TextView)findViewById(R.id.Note)).setText(departure.getNote()); - ((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString()); + ((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString(this)); + + + if (savedInstanceState == null) { + startTimetableFetcher(); + } else { + timetables = (List) savedInstanceState.getSerializable("timetables"); + adapter.setTimetable(timetables); + } } + + @Override + public void onSaveInstanceState(Bundle outState) + { + dismissDialog(DLG_PROGRESS); + outState.putSerializable("timetables", (ArrayList) timetables); + } -/* case DLG_DETAILS: - DepartureBean currentDeparture = departures.get(selectedItemId); - ((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); - ((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); - ((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination()); - ((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin()); - ((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation()); - ((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString()); - ((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus()); - ((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote()); - detailsDialog = dialog; + + @Override + protected void onPrepareDialog(int id, Dialog dialog) { + super.onPrepareDialog(id, dialog); + + switch (id) { + case DLG_PROGRESS: + //pgDialog = (ProgressDialog) dialog; break; + } + } + + @Override + protected Dialog onCreateDialog(int id) { + switch (id) { + case DLG_PROGRESS: + ProgressDialog dlg = new ProgressDialog(this); + dlg.setMessage( getString(timetablelist_fetchdata) ); + dlg.setCancelable(true); + return dlg; + default: + return super.onCreateDialog(id); + } + } - */ + void startTimetableFetcher() { + showDialog(DLG_PROGRESS); + fetcher = new TimetableFetcher(); + fetcher.execute(departure.getType(), departure.getTrainNumber()); + } + + class TimetableFetcher extends AsyncTask { + + boolean success; + + @Override + protected void onPostExecute(Void result) { + super.onPostExecute(result); + dismissDialog(DLG_PROGRESS); + + + if (success) { + adapter.setTimetable(timetables); + if (timetables.size() == 0) { + MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata)); + } + } else { // communication or parse error + AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); + builder.setMessage(getString(timetablelist_fetcherror)); + builder.setCancelable(true); + builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + startTimetableFetcher(); + + } + }); + builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + } + }); + + try { + builder.show(); + } catch (android.view.WindowManager.BadTokenException e) { + Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running + } + + } + + } + + @Override + protected Void doInBackground(String... arg0) { + String type = arg0[0]; + String trainID = arg0[1]; + success = provider.lookupTimetable(type, trainID); + timetables = provider.getTimetable(type, trainID); + + return null; + } + + } }