--- android/TrainInfo/src/dk/thoerup/traininfo/TimetableList.java 2010/07/10 16:03:10 981 +++ android/TrainInfo/src/dk/thoerup/traininfo/TimetableList.java 2011/04/04 18:33:27 1260 @@ -7,8 +7,7 @@ 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 java.util.regex.Pattern; import android.app.AlertDialog; import android.app.Dialog; @@ -22,6 +21,10 @@ 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.StationBean.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; @@ -32,11 +35,12 @@ 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); @@ -53,16 +57,34 @@ 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_equipmentcircumstances) ); + + + ((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); } } @@ -80,12 +102,13 @@ protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); - TimetableBean tt = timetables.get(position); + TimetableEntry tt = timetables.entries.get(position); - StationBean station = new StationBean(); + StationEntry station = new StationEntry(); station.setName( tt.getStation() ); station.setId( tt.getStationId() ); - station.setRegional(true); + station.setIsRegional(true); + Intent intent = new Intent(this, DepartureList.class); intent.putExtra("stationbean", station); @@ -99,7 +122,7 @@ public void onSaveInstanceState(Bundle outState) { dismissDialog(DLG_PROGRESS); - outState.putSerializable("timetables", (ArrayList) timetables); + outState.putSerializable("timetables", (TimetableBean) timetables); } @@ -133,9 +156,27 @@ 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) { @@ -143,23 +184,27 @@ dismissDialog(DLG_PROGRESS); - if (success) { + if (timetables != null) { + commFailCounter = 0; TimetableList.this.getListView().invalidateViews(); adapter.setTimetable(timetables); - if (timetables.size() == 0) { + 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)); 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(); @@ -181,8 +226,7 @@ protected Void doInBackground(String... arg0) { String type = arg0[0]; String trainID = arg0[1]; - success = provider.lookupTimetable(type, trainID); - timetables = provider.getTimetable(type, trainID); + timetables = provider.lookupTimetable(type, trainID); return null; }