--- android/TrainInfo/src/dk/thoerup/traininfo/TrainInfoList.java 2009/09/23 05:39:04 333 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2009/09/30 19:09:59 371 @@ -4,9 +4,11 @@ import java.util.List; import java.util.Locale; +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.location.Address; import android.location.Geocoder; @@ -16,25 +18,35 @@ import android.os.Handler; import android.os.Message; import android.util.Log; +import android.view.Menu; +import android.view.MenuItem; import android.view.View; import android.widget.ListView; + + import dk.thoerup.traininfo.provider.ProviderFactory; import dk.thoerup.traininfo.provider.StationProvider; +import dk.thoerup.traininfo.stationmap.GeoPair; +import dk.thoerup.traininfo.stationmap.StationMapView; import dk.thoerup.traininfo.util.MessageBox; -public class TrainInfoList extends ListActivity { - public static final int GOTLOCATION = 1; - public static final int GOTSTATIONLIST = 2; - public static final int NOPROVIDER = 3; - public static final int FIXTIMEOUT = 4; - public static final int LOOKUPSTATIONFAILED = 5; +public class StationList extends ListActivity { + public static final int GOTLOCATION = 1001; + public static final int GOTSTATIONLIST = 1002; + public static final int NOPROVIDER = 1003; + public static final int LOCATIONFIXTIMEOUT = 1004; - public static final int DLG_PROGRESS = 1; + public static final int OPTIONS_MAP = 2001; + public static final int OPTIONS_ABOUT = 2002; + + + public static final int DLG_PROGRESS = 1001; /** Called when the activity is first created. */ + String dialogMessage = ""; ProgressDialog dialog; LocationLookup locator = null; - LocatorTask locatorTask = new LocatorTask(); + LocatorTask locatorTask; boolean isRunning = false; List stations = new ArrayList(); @@ -49,8 +61,6 @@ super.onCreate(savedInstanceState); setContentView(R.layout.main); - LocationLookup.removeMockLocation(this); - //LocationLookup.injectMockLocation(this); adapter = new StationListAdapter(this); setListAdapter(adapter); @@ -63,7 +73,8 @@ adapter.setStations(stations); } } - + + @Override public void onSaveInstanceState(Bundle outState) { @@ -75,13 +86,58 @@ @Override + public boolean onCreateOptionsMenu(Menu menu) { + menu.add(0, OPTIONS_MAP, 0, "Show station map"); + menu.add(0, OPTIONS_ABOUT, 0, "About"); + + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + boolean retval; + + switch (item.getItemId()) { + case OPTIONS_MAP: + + Intent intent = new Intent(this,StationMapView.class); + intent.putExtra("userlocation", GeoPair.fromLocation(locator.getLocation()) ); + + ArrayList stationPoints = new ArrayList(); + for (StationBean st : stations ) { + stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) ); + } + + intent.putExtra("stations", stationPoints); + + startActivity(intent); + retval = true; + break; + case OPTIONS_ABOUT: + String ver = this.getResources().getString(R.string.app_version); + + StringBuffer message = new StringBuffer(); + message.append("TrainInfo DK v").append(ver).append("\n"); + message.append("By Torben Nielsen\n"); + + MessageBox.showMessage(this, message.toString()); + retval = true; + break; + default: + retval = super.onOptionsItemSelected(item); + } + + return retval; + } + + @Override protected Dialog onCreateDialog(int id) { switch (id) { case DLG_PROGRESS: ProgressDialog dlg = new ProgressDialog(this); dlg.setMessage("Wait for location fix"); dlg.setCancelable(false); - return dlg; + return dlg; default: return super.onCreateDialog(id); } @@ -96,6 +152,10 @@ switch (id) { case DLG_PROGRESS: this.dialog = (ProgressDialog) dialog; + if (!dialogMessage.equals("")) { + this.dialog.setMessage(dialogMessage); + dialogMessage = ""; + } break; } } @@ -105,45 +165,67 @@ showDialog(DLG_PROGRESS); locator.locateStations(); - stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); + stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); } Handler stationsFetched = new Handler() { @Override public void handleMessage(Message msg) { + switch (msg.what) { case GOTLOCATION: - dialog.setMessage("Finding nearby stations"); - locatorTask.execute(); + dismissDialog(DLG_PROGRESS); + + startLocatorTask(); + break; case NOPROVIDER: - dialog.dismiss(); - MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); + dismissDialog(DLG_PROGRESS); + MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); break; - case FIXTIMEOUT: - dialog.dismiss(); + case LOCATIONFIXTIMEOUT: if (isRunning) { - locator.abortLocationListener(); + locator.stopSearch(); if (locator.hasLocation()) { - msg.what = GOTLOCATION; - handleMessage( msg ); // ToDo: ugly recursive call !!! - } else { - MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out"); + stationsFetched.sendEmptyMessage( GOTLOCATION ); + } else { + dismissDialog(DLG_PROGRESS); + + AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); + builder.setMessage("GPS fix timed out"); + builder.setCancelable(true); + builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + startLookup(); + + } + }); + builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + } + }); + builder.show(); + } } break; - case LOOKUPSTATIONFAILED: - dialog.dismiss(); - MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations"); - break; } isRunning = false; } }; - + void startLocatorTask() + { + dialogMessage = "Finding nearby stations"; + showDialog(DLG_PROGRESS); + + locatorTask = new LocatorTask(); + locatorTask.execute(); + } @Override protected void onListItemClick(ListView l, View v, int position, long id) { @@ -222,12 +304,32 @@ if (success) { if (stationProvider.getStations().size() == 0) - MessageBox.showMessage(TrainInfoList.this, "No stations found!"); // this should not be possible !?! + MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! stations = stationProvider.getStations(); adapter.setStations( stations ); } else { //communication or parse errors - MessageBox.showMessage(TrainInfoList.this, "Error finding stations!"); + AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); + builder.setMessage("Error on finding nearby stations"); + builder.setCancelable(true); + builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + + stationsFetched.post( new Runnable() { + @Override + public void run() { + startLocatorTask(); + } + }); + } + }); + builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + } + }); + builder.show(); } } }