--- android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2009/10/27 13:40:15 474 +++ android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2010/07/10 16:03:10 981 @@ -1,8 +1,13 @@ 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; + import android.app.AlertDialog; import android.app.Dialog; @@ -13,7 +18,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; @@ -27,7 +35,7 @@ DepartureListAdapter adapter; DepartureProvider provider; - List departures; + DepartureBean departures; int selectedItemId; //DepartureBean currentDeparture; @@ -35,11 +43,12 @@ ProgressDialog pgDialog; DepartureFetcher fetcher; - int stationId; + + StationBean station; - double latitude,longitude; + boolean arrival = false; - @SuppressWarnings("unchecked") + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -49,35 +58,69 @@ 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." ); - + int distance = station.getDistance(); + if (distance != 0) { + NumberFormat format = NumberFormat.getNumberInstance(); + format.setMaximumFractionDigits(1); + format.setMinimumFractionDigits(1); - provider = ProviderFactory.getDepartureProvider(); + ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); + } else { + ((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE); + } - if (savedInstanceState == null) { - startDepartureFetcher(); + + 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 { - departures = (List) savedInstanceState.getSerializable("departures"); - adapter.setDepartures(departures); - selectedItemId = savedInstanceState.getInt("selectedItemId"); + provider = ProviderFactory.getDepartureProvider(); + + if (savedInstanceState == null) { + startDepartureFetcher(); + } else { + departures = (DepartureBean) savedInstanceState.getSerializable("departures"); + adapter.setDepartures(departures.entries); + selectedItemId = savedInstanceState.getInt("selectedItemId"); + } } } @@ -89,16 +132,27 @@ outState.putInt("selectedItemId", selectedItemId); - outState.putSerializable("departures", (ArrayList) departures); + outState.putSerializable("departures", departures); } + + @Override + protected void onDestroy() { + super.onDestroy(); + + if (fetcher != null) { + fetcher.cancel(true); + } + } + + @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); selectedItemId = position; - DepartureBean dep = departures.get(selectedItemId); + DepartureEntry dep = departures.entries.get(selectedItemId); Intent intent = new Intent(this, TimetableList.class); intent.putExtra("departure", dep); @@ -115,6 +169,8 @@ switch (id) { case DLG_PROGRESS: pgDialog = (ProgressDialog) dialog; + int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals; + pgDialog.setMessage( getString(messageId) ); break; } } @@ -123,8 +179,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: @@ -135,7 +191,7 @@ void startDepartureFetcher() { showDialog(DLG_PROGRESS); fetcher = new DepartureFetcher(); - fetcher.execute(stationId); + fetcher.execute(station.getId()); } class DialogDismisser implements View.OnClickListener { @@ -154,8 +210,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)); } }; @@ -174,34 +230,43 @@ pgDialog.dismiss(); if (success) { - adapter.setDepartures(departures); - if (departures.size() == 0) { - MessageBox.showMessage(DepartureList.this, "No departures found"); + DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html + adapter.setDepartures(departures.entries); + DepartureList.this.getListView().setVisibility(View.VISIBLE); + + if (departures.entries.size() == 0) { + MessageBox.showMessage(DepartureList.this, "No departures found", true); } } else { // communication or parse error 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; }