--- android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2009/08/09 09:09:16 239 +++ android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2009/09/11 08:48:18 316 @@ -1,21 +1,26 @@ package dk.thoerup.traininfo; import java.text.NumberFormat; +import java.util.ArrayList; import java.util.List; +import java.util.Locale; import android.app.Dialog; import android.app.ListActivity; import android.app.ProgressDialog; import android.content.Intent; +import android.location.Address; +import android.location.Geocoder; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; +import android.util.Log; import android.view.View; import android.widget.ListView; import android.widget.TextView; -import android.widget.Toast; -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 { @@ -26,15 +31,16 @@ DepartureProvider provider; List departures; - static int itemId; - static DepartureBean currentDeparture; + int selectedItemId; + //DepartureBean currentDeparture; ProgressDialog pgDialog; - + Dialog detailsDialog; DepartureFetcher fetcher; double latitude,longitude; + @SuppressWarnings("unchecked") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -51,8 +57,9 @@ String name = launchedBy.getStringExtra("name"); ((TextView) findViewById(R.id.stationName)).setText( name ); - String addr = launchedBy.getStringExtra("address"); - ((TextView) findViewById(R.id.stationAddr)).setText( addr ); + ((TextView) findViewById(R.id.stationAddr)).setText( "searching..." ); + + int stationId = launchedBy.getIntExtra("stationid", -1); findViewById(R.id.header).setOnClickListener( mapLauncher ); @@ -63,18 +70,42 @@ ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); - showDialog(DLG_PROGRESS); - provider = DepartureFactory.getProvider(); + provider = ProviderFactory.getDepartureProvider(); fetcher = new DepartureFetcher(); - fetcher.execute(name); + if (savedInstanceState == null) { + showDialog(DLG_PROGRESS); + fetcher.execute(stationId); + } else { + departures = (List) savedInstanceState.getSerializable("departures"); + adapter.setDepartures(departures); + selectedItemId = savedInstanceState.getInt("selectedItemId"); + boolean detailsShowing = savedInstanceState.getBoolean("detailsShowing"); + if (detailsShowing) + showDialog(DLG_DETAILS); + } } + @Override + public void onSaveInstanceState(Bundle outState) + { + 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); + } + @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); - currentDeparture = departures.get(position); + selectedItemId = position; showDialog(DLG_DETAILS); } @@ -82,16 +113,19 @@ @Override protected void onPrepareDialog(int id, Dialog dialog) { 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; @@ -123,6 +157,32 @@ } } + String lookupAddress(double latitude, double longitude) { + + Geocoder coder = new Geocoder(this, new Locale("da")); + StringBuilder sb = new StringBuilder(); + Log.i("lookupaddr", "" + latitude + "/" + longitude); + try { + List
addressList = coder.getFromLocation(latitude, longitude, 1); + Address addr = addressList.get(0); + + + int max = addr.getMaxAddressLineIndex(); + for (int i=0; i0) + sb.append(", "); + + sb.append(addr.getAddressLine(i)); + } + + + } catch (Exception e) { + Log.e("DepartureList", "geocoder failed", e); + } + + return sb.toString(); + } + class DialogDismisser implements View.OnClickListener { Dialog dlg; @@ -146,8 +206,9 @@ }; - class DepartureFetcher extends AsyncTask { + class DepartureFetcher extends AsyncTask { + String addr; @Override protected void onPostExecute(Void result) { super.onPostExecute(result); @@ -155,12 +216,16 @@ adapter.setDepartures(departures); pgDialog.dismiss(); + + ((TextView) findViewById(R.id.stationAddr)).setText( addr ); + if (departures.size() == 0) - Toast.makeText(DepartureList.this, "No departures found", Toast.LENGTH_LONG); + MessageBox.showMessage(DepartureList.this, "No departures found"); } @Override - protected Void doInBackground(String... params) { + protected Void doInBackground(Integer... params) { + addr = lookupAddress( latitude , longitude); provider.lookupDepartures(params[0]); departures = provider.getDepartures(); return null;