--- android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2009/09/10 19:09:09 310 +++ android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2010/07/10 16:03:10 981 @@ -1,17 +1,27 @@ 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; import android.app.ListActivity; import android.app.ProgressDialog; +import android.content.DialogInterface; import android.content.Intent; 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; @@ -21,22 +31,24 @@ public class DepartureList extends ListActivity { public static final int DLG_PROGRESS = 1; - public static final int DLG_DETAILS = 2; + DepartureListAdapter adapter; DepartureProvider provider; - List departures; + DepartureBean departures; int selectedItemId; //DepartureBean currentDeparture; ProgressDialog pgDialog; - Dialog detailsDialog; + DepartureFetcher fetcher; + + StationBean station; - double latitude,longitude; + boolean arrival = false; - @SuppressWarnings("unchecked") + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -46,40 +58,69 @@ setListAdapter(adapter); Intent launchedBy = getIntent(); + + station = (StationBean) launchedBy.getSerializableExtra("stationbean"); - latitude = launchedBy.getDoubleExtra("latitude", 0.0); - longitude = launchedBy.getDoubleExtra("longitude", 0.0); + ((TextView) findViewById(R.id.stationName)).setText( station.getName() ); + + + ((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(); + } + }); - String name = launchedBy.getStringExtra("name"); - ((TextView) findViewById(R.id.stationName)).setText( name ); - String addr = launchedBy.getStringExtra("address"); - ((TextView) findViewById(R.id.stationAddr)).setText( addr ); - int 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(); - - fetcher = new DepartureFetcher(); - if (savedInstanceState == null) { - showDialog(DLG_PROGRESS); - fetcher.execute(stationId); + ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); } else { - departures = (List) savedInstanceState.getSerializable("departures"); - adapter.setDepartures(departures); - selectedItemId = savedInstanceState.getInt("selectedItemId"); - boolean detailsShowing = savedInstanceState.getBoolean("detailsShowing"); - if (detailsShowing) - showDialog(DLG_DETAILS); + ((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(); + + if (savedInstanceState == null) { + startDepartureFetcher(); + } else { + departures = (DepartureBean) savedInstanceState.getSerializable("departures"); + adapter.setDepartures(departures.entries); + selectedItemId = savedInstanceState.getInt("selectedItemId"); + } } } @@ -88,22 +129,36 @@ { if (pgDialog != null && pgDialog.isShowing()) dismissDialog(DLG_PROGRESS); - boolean detailsShowing = (detailsDialog != null && detailsDialog.isShowing()); - if (detailsShowing) { - dismissDialog(DLG_DETAILS); - } - outState.putBoolean("detailsShowing", detailsShowing); + 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; + + DepartureEntry dep = departures.entries.get(selectedItemId); - selectedItemId = position; - showDialog(DLG_DETAILS); + Intent intent = new Intent(this, TimetableList.class); + intent.putExtra("departure", dep); + + startActivity(intent); + } @@ -112,20 +167,10 @@ super.onPrepareDialog(id, dialog); switch (id) { - 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; case DLG_PROGRESS: pgDialog = (ProgressDialog) dialog; + int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals; + pgDialog.setMessage( getString(messageId) ); break; } } @@ -134,26 +179,21 @@ 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; - case DLG_DETAILS: - //Context mContext = getApplicationContext(); - Dialog dialog = new Dialog(this); - dialog.setCancelable(true); - - dialog.setContentView(R.layout.departuredetails); - dialog.setTitle("Departure details"); - - View root = dialog.findViewById(R.id.layout_root); - root.setOnClickListener( new DialogDismisser(dialog) ); - return dialog; + return dlg; default: return super.onCreateDialog(id); } } + void startDepartureFetcher() { + showDialog(DLG_PROGRESS); + fetcher = new DepartureFetcher(); + fetcher.execute(station.getId()); + } + class DialogDismisser implements View.OnClickListener { Dialog dlg; @@ -165,35 +205,68 @@ public void onClick(View v) { if (dlg.isShowing()) dlg.dismiss(); - } + } } 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)); } }; - + + class DepartureFetcher extends AsyncTask { + boolean success; + @Override protected void onPostExecute(Void result) { super.onPostExecute(result); - adapter.setDepartures(departures); + pgDialog.dismiss(); - if (departures.size() == 0) - MessageBox.showMessage(DepartureList.this, "No departures found"); + if (success) { + 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(getString(generic_retry), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + startDepartureFetcher(); + + } + }); + builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + DepartureList.this.finish(); + } + }); + + 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) { - provider.lookupDepartures(params[0]); - departures = provider.getDepartures(); + success = provider.lookupDepartures(params[0], DepartureList.this.arrival); + departures = provider.getDepartures(params[0], DepartureList.this.arrival); return null; }