--- android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2010/01/26 21:17:26 552 +++ android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2010/06/11 14:05:24 835 @@ -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; @@ -35,9 +44,10 @@ ProgressDialog pgDialog; DepartureFetcher fetcher; - int stationId; + + StationBean station; - double latitude,longitude; + boolean arrival = false; @SuppressWarnings("unchecked") @Override @@ -49,34 +59,58 @@ setListAdapter(adapter); Intent launchedBy = getIntent(); + + station = (StationBean) launchedBy.getSerializableExtra("stationbean"); - latitude = launchedBy.getDoubleExtra("latitude", 0.0); - longitude = launchedBy.getDoubleExtra("longitude", 0.0); - - String name = launchedBy.getStringExtra("name"); - ((TextView) findViewById(R.id.stationName)).setText( name ); + ((TextView) findViewById(R.id.stationName)).setText( station.getName() ); + - String address = launchedBy.getStringExtra("address"); - ((TextView) findViewById(R.id.stationAddr)).setText( address ); + ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); + + final Button departureBtn = (Button) findViewById(R.id.departurebtn); + final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); + + 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(); + } + }); + + - stationId = launchedBy.getIntExtra("stationid", -1); findViewById(R.id.header).setOnClickListener( mapLauncher ); - NumberFormat format = NumberFormat.getNumberInstance(); - format.setMaximumFractionDigits(1); - format.setMinimumFractionDigits(1); - int distance = launchedBy.getIntExtra("distance", 0); - ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); - - boolean isRegional = launchedBy.getBooleanExtra("isregional", false); - boolean isSTrain = launchedBy.getBooleanExtra("isstrain", false); - //boolean isMetro = launchedBy.getBooleanExtra("ismetro", false); // not currently used + int distance = station.getDistance(); + 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 (isRegional == false && isSTrain == false) { + 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(); @@ -125,6 +159,8 @@ switch (id) { case DLG_PROGRESS: pgDialog = (ProgressDialog) dialog; + int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals; + pgDialog.setMessage( getString(messageId) ); break; } } @@ -133,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: @@ -145,7 +181,7 @@ void startDepartureFetcher() { showDialog(DLG_PROGRESS); fetcher = new DepartureFetcher(); - fetcher.execute(stationId); + fetcher.execute(station.getId()); } class DialogDismisser implements View.OnClickListener { @@ -164,8 +200,8 @@ View.OnClickListener mapLauncher = new View.OnClickListener() { @Override - public void onClick(View v) { - Uri uri = Uri.parse("geo:" + latitude + "," + longitude); + public void onClick(View v) { + Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); startActivity( new Intent(Intent.ACTION_VIEW, uri)); } }; @@ -192,26 +228,31 @@ 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(); } - }); - 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; }