--- android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2009/08/08 19:02:20 237 +++ android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2010/06/26 11:02:53 918 @@ -1,34 +1,55 @@ 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.DepartureFactory; import dk.thoerup.traininfo.provider.DepartureProvider; +import dk.thoerup.traininfo.provider.ProviderFactory; +import dk.thoerup.traininfo.util.MessageBox; 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; - static int itemId; - static DepartureBean currentDeparture; + int selectedItemId; + //DepartureBean currentDeparture; ProgressDialog pgDialog; + DepartureFetcher fetcher; + + StationBean station; + + boolean arrival = false; + + @SuppressWarnings("unchecked") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -38,52 +59,119 @@ setListAdapter(adapter); Intent launchedBy = getIntent(); - String name = launchedBy.getStringExtra("name"); - ((TextView) findViewById(R.id.stationName)).setText( name ); + + station = (StationBean) launchedBy.getSerializableExtra("stationbean"); + + ((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 addr = launchedBy.getStringExtra("address"); - ((TextView) findViewById(R.id.stationAddr)).setText( addr ); - 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." ); + findViewById(R.id.header).setOnClickListener( mapLauncher ); + + 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); + } + - showDialog(DLG_PROGRESS); - provider = DepartureFactory.getProvider(); - provider.lookupDepartures(name); - departures = provider.getDepartures(); - adapter.setDepartures(departures); - pgDialog.dismiss(); + 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 = (List) savedInstanceState.getSerializable("departures"); + adapter.setDepartures(departures); + selectedItemId = savedInstanceState.getInt("selectedItemId"); + } + } } + @Override + public void onSaveInstanceState(Bundle outState) + { + if (pgDialog != null && pgDialog.isShowing()) + dismissDialog(DLG_PROGRESS); + + outState.putInt("selectedItemId", selectedItemId); + + outState.putSerializable("departures", (ArrayList) 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); - currentDeparture = departures.get(position); - showDialog(DLG_DETAILS); + Intent intent = new Intent(this, TimetableList.class); + intent.putExtra("departure", dep); + + startActivity(intent); + } @Override protected void onPrepareDialog(int id, Dialog dialog) { super.onPrepareDialog(id, dialog); - + switch (id) { - case DLG_DETAILS: - ((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); - ((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()); - break; case DLG_PROGRESS: pgDialog = (ProgressDialog) dialog; + int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals; + pgDialog.setMessage( getString(messageId) ); break; } } @@ -92,26 +180,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; @@ -123,6 +206,70 @@ 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:" + 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); + + + pgDialog.dismiss(); + + if (success) { + DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html + adapter.setDepartures(departures); + DepartureList.this.getListView().setVisibility(View.VISIBLE); + + if (departures.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) { + success = provider.lookupDepartures(params[0], DepartureList.this.arrival); + departures = provider.getDepartures(params[0], DepartureList.this.arrival); + return null; } + } }