--- android/TrainInfo/src/dk/thoerup/traininfo/TimetableList.java 2010/01/28 09:10:44 563 +++ android/TrainInfo/src/dk/thoerup/traininfo/TimetableList.java 2011/05/02 16:08:01 1416 @@ -1,35 +1,46 @@ package dk.thoerup.traininfo; -import java.util.ArrayList; -import java.util.List; +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.regex.Pattern; -import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; +import android.app.ListActivity; 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.view.View; import android.widget.ListView; import android.widget.TextView; +import dk.thoerup.android.traininfo.common.DepartureEntry; +import dk.thoerup.android.traininfo.common.StationEntry; +import dk.thoerup.android.traininfo.common.TimetableBean; +import dk.thoerup.android.traininfo.common.TimetableEntry; import dk.thoerup.traininfo.provider.ProviderFactory; import dk.thoerup.traininfo.provider.TimetableProvider; import dk.thoerup.traininfo.util.MessageBox; -import static dk.thoerup.traininfo.R.string.*; -public class TimetableList extends Activity { +public class TimetableList extends ListActivity { private static final int DLG_PROGRESS = 8000; - DepartureBean departure; + DepartureEntry departure; TimetableListAdapter adapter; TimetableFetcher fetcher; - List timetables; + TimetableBean timetables; + int commFailCounter = 0; TimetableProvider provider; - @SuppressWarnings("unchecked") + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -37,51 +48,81 @@ provider = ProviderFactory.getTimetableProvider(); - adapter = new TimetableListAdapter(this); - - ListView lv = (ListView) findViewById(R.id.List); - lv.setAdapter(adapter); + adapter = new TimetableListAdapter(this); + setListAdapter(adapter); + Intent launchedBy = getIntent(); - departure = (DepartureBean) launchedBy.getSerializableExtra("departure"); + departure = (DepartureEntry) 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(this)); - + + ((TextView)findViewById(R.id.Location)).setText(departure.getLocation()); + ((TextView)findViewById(R.id.Updated)).setText( getLastUpdateString( departure.getUpdated() )); + + String status = departure.getStatus() != null ? departure.getStatus() : ""; + if ( Pattern.matches("[0-9]+.+min", status) ) { + status += " " + this.getString(R.string.departurebean_delayed); + } + String note = departure.getNote() != null ? departure.getNote() : ""; + note = note.replace("Aflyst", this.getString(R.string.timetablelist_cancelled) ); + note = note.replace("Kører kun til", this.getString(R.string.timetablelist_goesonlyto) ); + note = note.replace("Afgår fra", this.getString(R.string.timetablelist_startsat) ); + note = note.replace("Erstattet af", this.getString(R.string.timetablelist_replacedby) ); + note = note.replace("Eksterne forhold", this.getString(R.string.timetablelist_externalconditions) ); + note = note.replace("Teknisk fejl på et signal", this.getString(R.string.timetablelist_technicalerroronsignal) ); + note = note.replace("Materielforhold", this.getString(R.string.timetablelist_equipmentrelated) ); + note = note.replace("Passagerforhold", this.getString(R.string.timetablelist_passengerrelated) ); + note = note.replace("Forventet rettidig afgang", this.getString(R.string.timetablelist_expectedtimelydeparture) ); + + + + ((TextView)findViewById(R.id.Status)).setText(status); + ((TextView)findViewById(R.id.Note)).setText(note); + + + ProviderFactory.purgeOldEntries(); //cleanup before fetching more data if (savedInstanceState == null) { startTimetableFetcher(); } else { - timetables = (List) savedInstanceState.getSerializable("timetables"); + timetables = (TimetableBean) savedInstanceState.getSerializable("timetables"); adapter.setTimetable(timetables); } } + + @Override + protected void onDestroy() { + super.onDestroy(); + + if (fetcher != null) { + fetcher.cancel(true); + } + } - @Override + @Override + protected void onListItemClick(ListView l, View v, int position, long id) { + super.onListItemClick(l, v, position, id); + + TimetableEntry tt = timetables.entries.get(position); + + + Intent intent = new Intent(this, DepartureList.class); + intent.putExtra("stationbean", tt.getStationEntry() ); + startActivity(intent); + + } + + + + @Override public void onSaveInstanceState(Bundle outState) { dismissDialog(DLG_PROGRESS); - outState.putSerializable("timetables", (ArrayList) timetables); + outState.putSerializable("timetables", (TimetableBean) 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; - break; - - */ @Override protected void onPrepareDialog(int id, Dialog dialog) { @@ -110,50 +151,88 @@ void startTimetableFetcher() { showDialog(DLG_PROGRESS); fetcher = new TimetableFetcher(); - fetcher.execute(departure.getTrainNumber()); + fetcher.execute(departure.getType(), departure.getTrainNumber()); + } + + + public String getLastUpdateString(int lastUpdate) { + String minutes = this.getString(R.string.departurebean_minutes); + String unknown = this.getString(R.string.departurebean_unknown); + switch (lastUpdate) { + case 1: + return "<3 " + minutes; + case 2: + return "3-10 " + minutes; + case 3: + return ">3 " + minutes; + case 4: + return unknown; + default: + return ""; + } } + class TimetableFetcher extends AsyncTask { - boolean success; @Override protected void onPostExecute(Void result) { super.onPostExecute(result); dismissDialog(DLG_PROGRESS); - - if (success) { + //TODO: differentiate between a communication error and a downed bane.dk site + if (timetables != null && timetables.errorCode == null) { + commFailCounter = 0; + TimetableList.this.getListView().invalidateViews(); adapter.setTimetable(timetables); - if (timetables.size() == 0) { - MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata)); + if (timetables.entries.size() == 0) { + MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true); } } else { // communication or parse error + commFailCounter++; AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); - builder.setMessage(getString(timetablelist_fetcherror)); + + + if (timetables != null && timetables.errorCode != null ) { //got an error xml back + commFailCounter = 10; + builder.setMessage( getString(R.string.no_backend) ); + } else { + 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(); - - } - }); + if (commFailCounter < 3) { + 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(); + TimetableList.this.finish(); } - }); - builder.show(); + }); + + 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 trainID = arg0[0]; - success = provider.lookupTimetable(trainID); - timetables = provider.getTimetable(trainID); + String type = arg0[0]; + String trainID = arg0[1]; + timetables = provider.lookupTimetable(type, trainID); return null; }