--- android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2010/01/27 10:04:28 557 +++ android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2010/06/13 13:45:37 843 @@ -1,5 +1,11 @@ package dk.thoerup.traininfo; +import static dk.thoerup.traininfo.R.string.departurelist_fetchdepartures; +import static dk.thoerup.traininfo.R.string.departurelist_fetcharrivals; +import static dk.thoerup.traininfo.R.string.generic_cancel; +import static dk.thoerup.traininfo.R.string.generic_retry; + + import java.text.NumberFormat; import java.util.ArrayList; import java.util.List; @@ -13,7 +19,10 @@ import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; +import android.util.Log; import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import dk.thoerup.traininfo.provider.DepartureProvider; @@ -38,6 +47,8 @@ StationBean station; + boolean arrival = false; + @SuppressWarnings("unchecked") @Override protected void onCreate(Bundle savedInstanceState) { @@ -56,21 +67,50 @@ ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); + final Button departureBtn = (Button) findViewById(R.id.departurebtn); + final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); - findViewById(R.id.header).setOnClickListener( mapLauncher ); + departureBtn.setOnClickListener( new OnClickListener() { + @Override + public void onClick(View arg0) { + arrivalBtn.setBackgroundResource(R.drawable.custom_button); + departureBtn.setBackgroundResource(R.drawable.custom_button_hilight); + arrival = false; + startDepartureFetcher(); + } + }); + arrivalBtn.setOnClickListener( new OnClickListener() { + @Override + public void onClick(View arg0) { + arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight); + departureBtn.setBackgroundResource(R.drawable.custom_button); + arrival = true; + startDepartureFetcher(); + } + }); - NumberFormat format = NumberFormat.getNumberInstance(); - format.setMaximumFractionDigits(1); - format.setMinimumFractionDigits(1); - + + + findViewById(R.id.header).setOnClickListener( mapLauncher ); + int distance = station.getDistance(); - ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); + if (distance != 0) { + NumberFormat format = NumberFormat.getNumberInstance(); + format.setMaximumFractionDigits(1); + format.setMinimumFractionDigits(1); + + ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); + } else { + ((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE); + } if (station.isRegional() == false && station.isSTrain() == false) { getListView().setVisibility( View.GONE ); findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); + departureBtn.setVisibility( View.GONE ); + arrivalBtn.setVisibility(View.GONE); } else { provider = ProviderFactory.getDepartureProvider(); @@ -119,6 +159,8 @@ switch (id) { case DLG_PROGRESS: pgDialog = (ProgressDialog) dialog; + int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals; + pgDialog.setMessage( getString(messageId) ); break; } } @@ -127,8 +169,8 @@ protected Dialog onCreateDialog(int id) { switch (id) { case DLG_PROGRESS: + ProgressDialog dlg = new ProgressDialog(this); - dlg.setMessage("Fetch departure data"); dlg.setCancelable(true); return dlg; default: @@ -186,26 +228,32 @@ AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); builder.setMessage("Error finding departures"); builder.setCancelable(true); - builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { + builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); startDepartureFetcher(); } }); - builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); + DepartureList.this.finish(); } - }); - builder.show(); + }); + + try { + builder.show(); + } catch (android.view.WindowManager.BadTokenException e) { + Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running + } } } @Override protected Void doInBackground(Integer... params) { - success = provider.lookupDepartures(params[0]); - departures = provider.getDepartures(params[0]); + success = provider.lookupDepartures(params[0], DepartureList.this.arrival); + departures = provider.getDepartures(params[0], DepartureList.this.arrival); return null; }