--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/01/28 09:16:33 564 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/08/31 08:49:15 1025 @@ -4,6 +4,7 @@ import java.util.List; +import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.ListActivity; @@ -46,13 +47,14 @@ public static final int OPTIONS_MAP = 2003; public static final int OPTIONS_GPSINFO = 2004; - - - public static final int DLG_PROGRESS = 3001; public static final int DLG_STATIONNAME = 3002; + + public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds + + static enum LookupMethod { ByLocation, ByName, @@ -68,7 +70,8 @@ StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); GeoPair location = new GeoPair(); - + + boolean isLaunchedforShortcut; boolean isRunning = false; List stations = new ArrayList(); @@ -110,6 +113,8 @@ listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); setTitle(); + isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); + if (savedInstanceState == null) { @@ -117,8 +122,8 @@ case ListNearest: startLookup(); break; - case ListSearch: - this.showDialog(DLG_STATIONNAME); + case ListSearch: + showDialog(DLG_STATIONNAME); break; case ListFavorites: startFavoriteLookup(); @@ -134,6 +139,23 @@ } } + + + @Override + protected void onDestroy() { + super.onDestroy(); + + isRunning = false; + + if (locationLookup != null) { + locationLookup.stopSearch(); + } + if (findStationsTask != null) { + findStationsTask.cancel(true); + } + } + + protected void setTitle() { String dialogTitle = getResources().getString(app_name); switch (listType) { @@ -151,10 +173,10 @@ } setTitle(dialogTitle); + } - - + @Override public void onSaveInstanceState(Bundle outState) @@ -213,7 +235,7 @@ message.append( getString(stationlist_nolocation) ); } - MessageBox.showMessage(this, message.toString()); + MessageBox.showMessage(this, message.toString(), false); break; default: retval = super.onOptionsItemSelected(item); @@ -270,8 +292,9 @@ 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()); + String search = et.getText().toString().trim(); + if (search.length() >= 2) { + startNameSearch(search); } else { showMessageAndClose( getString(stationlist_twocharmin) ); } @@ -312,9 +335,16 @@ StationBean station = stations.get(position); - Intent intent = new Intent(this, DepartureList.class); - intent.putExtra("stationbean", station); - startActivity(intent); + if (isLaunchedforShortcut == true) { + Intent i = new Intent(); + i.putExtra("station", station); + setResult(Activity.RESULT_OK, i); + finish(); + } else { + Intent intent = new Intent(this, DepartureList.class); + intent.putExtra("stationbean", station); + startActivity(intent); + } } ///////////////////////////////////////////////////////////// @@ -326,7 +356,7 @@ showDialog(DLG_PROGRESS); locationLookup.locateStations(); - stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); + stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); } void startNameSearch(String name) { @@ -334,7 +364,7 @@ showDialog(DLG_PROGRESS); findStationsTask = new FindStationsTask(); - findStationsTask.searchByName(name, locationLookup.getLocation()); + findStationsTask.searchByName(name); findStationsTask.execute(); } @@ -346,7 +376,7 @@ showDialog(DLG_PROGRESS); findStationsTask = new FindStationsTask(); - findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation()); + findStationsTask.searchByIds( favorites.toString() ); findStationsTask.execute(); } else { showMessageAndClose( getString( stationlist_nofavorites ) ); @@ -364,35 +394,7 @@ findStationsTask.searchByLocation( locationLookup.getLocation() ); findStationsTask.execute(); } - - - /* TODO: Remove this no longer needed function - 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 @@ -412,7 +414,8 @@ case NOPROVIDER: dismissDialog(DLG_PROGRESS); - MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); + MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); + //StationList.this.finish(); break; case LOCATIONFIXTIMEOUT: if (isRunning) { @@ -436,8 +439,8 @@ public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } - }); - builder.show(); + }); + builder.show(); } } @@ -451,15 +454,13 @@ class FindStationsTask extends AsyncTask { LookupMethod method = LookupMethod.MethodNone; - boolean success; String name; Location loc; String ids; - public void searchByName(String n, Location l) { + public void searchByName(String n) { method = LookupMethod.ByName; - loc = l; name = n; } @@ -468,10 +469,9 @@ loc = l; } - public void searchByIds(String id, Location l) { + public void searchByIds(String id) { method = LookupMethod.ByList; - loc = l; ids = id; } @@ -488,36 +488,19 @@ switch (method) { case ByLocation: - success = stationProvider.lookupStations(loc); + stations = stationProvider.lookupStations(loc); break; case ByName: - success = stationProvider.lookupStationsByName(name); + stations = stationProvider.lookupStationsByName(name); break; case ByList: - success = stationProvider.lookupStationsByIds(ids); + stations = stationProvider.lookupStationsByIds(ids); break; default: - success = false; // not possible + stations = null; // not possible } - Location dummy = new Location("gps"); - List stations = stationProvider.getStations(); - - for (StationBean station : stations) { - - if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) { - if (loc != null) { //only do the distance calc if we have a location - dummy.setLatitude(station.getLatitude()); - dummy.setLongitude(station.getLongitude()); - station.setDistance( (int)loc.distanceTo(dummy) ); - } else { - station.setDistance(0); - } - } - - } - return null; } @@ -527,34 +510,62 @@ dialog.dismiss(); - if (success) { - if (stationProvider.getStations().size() == 0) - MessageBox.showMessage(StationList.this, getString(stationlist_nostations) ); // this should not be possible !?! - stations = stationProvider.getStations(); + if (stations != null) { + if (stations.size() == 0) { + showMessageAndClose(getString(stationlist_nostations)); + } + + StationList.this.getListView().invalidateViews(); adapter.setStations( stations ); + } else { //communication or parse errors AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); - builder.setMessage(getString(stationlist_nearbyerror)); + builder.setMessage(getString(stationlist_fetcherror)); builder.setCancelable(true); builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); - stationsFetched.post( new Runnable() { - @Override - public void run() { - startLocatorTask(); - } - }); + Runnable runner = null; + switch (method) { + case ByLocation: + runner = new Runnable() { + @Override + public void run() { + startLocatorTask(); + } + }; + break; + case ByName: + runner = new Runnable() { + @Override + public void run() { + startNameSearch( FindStationsTask.this.name ); + } + }; + break; + case ByList: + runner = new Runnable() { + @Override + public void run() { + startFavoriteLookup(); + } + }; + break; + } + + stationsFetched.post( runner ); } }); builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); + StationList.this.finish(); } - }); - builder.show(); + }); + + builder.show(); } } }