--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2009/10/02 10:17:44 380 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2009/10/02 10:39:09 381 @@ -18,12 +18,12 @@ import android.os.Handler; import android.os.Message; import android.util.Log; +import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.widget.EditText; import android.widget.ListView; - - import dk.thoerup.traininfo.provider.ProviderFactory; import dk.thoerup.traininfo.provider.StationProvider; import dk.thoerup.traininfo.stationmap.GeoPair; @@ -36,17 +36,21 @@ public static final int NOPROVIDER = 1003; public static final int LOCATIONFIXTIMEOUT = 1004; - public static final int OPTIONS_MAP = 2001; - public static final int OPTIONS_ABOUT = 2002; + public static final int OPTIONS_RESCAN = 2001; + public static final int OPTIONS_NAMESEARCH = 2002; + public static final int OPTIONS_MAP = 2003; + public static final int OPTIONS_ABOUT = 2004; - public static final int DLG_PROGRESS = 1001; + public static final int DLG_PROGRESS = 3001; + public static final int DLG_STATIONNAME = 3002; /** Called when the activity is first created. */ String dialogMessage = ""; ProgressDialog dialog; LocationLookup locator = null; LocatorTask locatorTask; + StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); GeoPair location = new GeoPair(); @@ -54,9 +58,18 @@ List stations = new ArrayList(); StationProvider stationProvider = ProviderFactory.getStationProvider(); + + StationListAdapter adapter = null; + + static enum LookupMethod { + ByLocation, + ByName, + MethodNone + } + /////////////////////////////////////////////////////////////////////////////////////////// + //Activity call backs - StationListAdapter adapter = null; @SuppressWarnings("unchecked") @Override public void onCreate(Bundle savedInstanceState) { @@ -91,17 +104,25 @@ @Override public boolean onCreateOptionsMenu(Menu menu) { + menu.add(0, OPTIONS_RESCAN, 0, "Find nearest stations"); + menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station"); 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; + boolean retval = true; + switch (item.getItemId()) { + case OPTIONS_RESCAN: + startLookup(); + break; + case OPTIONS_NAMESEARCH: + showDialog(DLG_STATIONNAME); + break; case OPTIONS_MAP: Intent intent = new Intent(this,StationMapView.class); @@ -115,7 +136,6 @@ intent.putExtra("stations", stationPoints); startActivity(intent); - retval = true; break; case OPTIONS_ABOUT: String ver = this.getResources().getString(R.string.app_version); @@ -125,7 +145,6 @@ message.append("By Torben Nielsen\n"); MessageBox.showMessage(this, message.toString()); - retval = true; break; default: retval = super.onOptionsItemSelected(item); @@ -142,6 +161,34 @@ dlg.setMessage("Wait for location fix"); dlg.setCancelable(false); return dlg; + case DLG_STATIONNAME: + LayoutInflater factory = LayoutInflater.from(this); + final View rootView = factory.inflate(R.layout.textinput, null); + + + AlertDialog.Builder builder = new AlertDialog.Builder(this); + + builder.setTitle("Station search"); + builder.setView(rootView); + builder.setCancelable(true); + builder.setPositiveButton("Search", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + EditText et = (EditText) rootView.findViewById(R.id.EditText); + dialog.dismiss(); + if (et.getText().toString().length() >= 2) { + startNameSearch(et.getText().toString()); + } else { + MessageBox.showMessage(StationList.this, "To characters, minimum" ); + } + } + }); + builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + return builder.create(); + default: return super.onCreateDialog(id); } @@ -149,7 +196,6 @@ } - @Override protected void onPrepareDialog(int id, Dialog dialog) { super.onPrepareDialog(id, dialog); @@ -163,17 +209,94 @@ break; } } + + @Override + protected void onListItemClick(ListView l, View v, int position, long id) { + super.onListItemClick(l, v, position, id); + + StationBean station = stations.get(position); + + double latitude = station.getLatitude(); + double longitude = station.getLongitude(); + + + + Intent intent = new Intent(this, DepartureList.class); + intent.putExtra("name", station.getName()); + intent.putExtra("distance", station.getDistance()); + intent.putExtra("latitude", latitude); + intent.putExtra("longitude", longitude); + intent.putExtra("stationid", station.getId()); + intent.putExtra("address", station.getAddress()); + startActivity(intent); + } + + ///////////////////////////////////////////////////////////// + // public void startLookup() { isRunning = true; + dialogMessage = "Wait for location fix"; showDialog(DLG_PROGRESS); locator.locateStations(); stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); } + + void startNameSearch(String name) { + dialogMessage = "Finding stations by name"; + showDialog(DLG_PROGRESS); + + locatorTask = new LocatorTask(); + locatorTask.searchByName(name, locator.getLocation()); + locatorTask.execute(); + + } - Handler stationsFetched = new Handler() { + + void startLocatorTask() + { + dialogMessage = "Finding nearby stations"; + showDialog(DLG_PROGRESS); + + locatorTask = new LocatorTask(); + locatorTask.searchByLocation( locator.getLocation() ); + locatorTask.execute(); + } + + + 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(); + } + + + //////////////////////////////////////////////////////////////////////////// + // Inner classes + + class StationsFetchedHandler extends Handler { @Override public void handleMessage(Message msg) { @@ -222,82 +345,57 @@ 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) { - super.onListItemClick(l, v, position, id); - - StationBean station = stations.get(position); - - double latitude = station.getLatitude(); - double longitude = station.getLongitude(); - + + class LocatorTask extends AsyncTask { - Intent intent = new Intent(this, DepartureList.class); - intent.putExtra("name", station.getName()); - intent.putExtra("distance", station.getDistance()); - intent.putExtra("latitude", latitude); - intent.putExtra("longitude", longitude); - intent.putExtra("stationid", station.getId()); - intent.putExtra("address", station.getAddress()); - startActivity(intent); - } - - String lookupAddress(double latitude, double longitude) { + LookupMethod method = LookupMethod.MethodNone; + boolean success; + String name; + Location loc; - 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)); - } + public void searchByName(String n, Location l) { - - } catch (Exception e) { - Log.e("DepartureList", "geocoder failed", e); + method = LookupMethod.ByName; + loc = l; + name = n; + } + + public void searchByLocation(Location l) { + method = LookupMethod.ByLocation; + loc = l; } - return sb.toString(); - } - - class LocatorTask extends AsyncTask { - boolean success; @Override protected void onPreExecute() { + if (method.equals(LookupMethod.MethodNone)) + throw new RuntimeException("Method not set"); super.onPreExecute(); } @Override protected Void doInBackground(Void... params) { - Location loc = locator.getLocation(); - success = stationProvider.lookupStations(loc); + + if (method.equals(LookupMethod.ByLocation)) + success = stationProvider.lookupStations(loc); + if (method.equals(LookupMethod.ByName)) + success = stationProvider.lookupStations(name); + Location dummy = new Location("gps"); List stations = stationProvider.getStations(); - for (StationBean station : stations) { + + for (StationBean station : stations) { String addr = lookupAddress(station.getLatitude(), station.getLongitude()); station.setAddress(addr); - } + + if (method.equals(LookupMethod.ByName) ) { + dummy.setLatitude(station.getLatitude()); + dummy.setLongitude(station.getLongitude()); + station.setDistance( (int)loc.distanceTo(dummy) ); + } + } return null; }