aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/MapActivity.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/cbaines/suma/MapActivity.java')
-rw-r--r--src/net/cbaines/suma/MapActivity.java249
1 files changed, 123 insertions, 126 deletions
diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java
index effd9b4..58c7918 100644
--- a/src/net/cbaines/suma/MapActivity.java
+++ b/src/net/cbaines/suma/MapActivity.java
@@ -92,7 +92,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
private ArrayList<POI> searchResults = null;
- private POIDialog favDialog;
+ private FavouritesDialog favDialog;
private HashMap<String, Overlay> overlays = new HashMap<String, Overlay>();
private HashMap<String, Overlay> pastOverlays;
@@ -179,7 +179,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
private MapActivity instance;
- private POIDialog searchResultsDialog;
+ private SearchResultsDialog searchResultsDialog;
private static final String TAG = "MapActivity";
@@ -297,11 +297,30 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
searchResults.addAll(busStops);
busStops = null;
+ Dao<Site, String> siteDao = getHelper().getSiteDao();
+
+ QueryBuilder<Site, String> siteQueryBuilder = siteDao.queryBuilder();
+ siteQueryBuilder.where().like(Site.NAME_FIELD_NAME, "%" + query + "%").or()
+ .like(Site.ID_FIELD_NAME, "%" + query + "%");
+ PreparedQuery<Site> sitePreparedQuery = siteQueryBuilder.prepare();
+ List<Site> sites = siteDao.query(sitePreparedQuery);
+ searchResults.addAll(sites);
+ sites = null;
+
+ Collections.sort(searchResults, new StringPOIDistanceComparator(query));
+
startLocation = new GeoPoint(50935551, -1393488);
mapController.setZoom(15);
showDialog(SEARCH_RESULTS_DIALOG_ID);
+ searchResultsDialog.setTitle(getResources().getString(R.string.search_results_dialog_title) + query);
+ if (searchResults.size() == 0) {
+ searchResultsDialog.setMessage("No results found");
+ } else {
+ searchResultsDialog.setItems(searchResults);
+ }
+
} catch (SQLException e) {
e.printStackTrace();
}
@@ -967,8 +986,6 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
return false;
}
- refreshFavouriteDialog();
-
return false;
case R.id.menu_about:
Intent aboutIntent = new Intent(MapActivity.this, AboutActivity.class);
@@ -980,40 +997,6 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
}
}
- private void refreshFavouriteDialog() {
- ArrayList<POI> newFavouriteItems = new ArrayList<POI>();
-
- try {
- Dao<Building, String> buildingDao = getHelper().getBuildingDao();
- Dao<BusStop, String> busStopDao = getHelper().getBusStopDao();
-
- final SharedPreferences favouritesPrefs = getSharedPreferences(FAVOURITES_PREFERENCES, MODE_PRIVATE);
- for (String id : favouritesPrefs.getAll().keySet()) {
- Building building;
- BusStop busStop;
- if ((building = buildingDao.queryForId(id)) != null) {
- newFavouriteItems.add(building);
- } else if ((busStop = busStopDao.queryForId(id)) != null) {
- newFavouriteItems.add(busStop);
- } else {
- Log.e(TAG, "Item in favourites " + id + " cannot be found");
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
-
- Log.i(TAG, "There are " + newFavouriteItems.size() + " favourites");
- if (newFavouriteItems.size() == 0) {
- Log.i(TAG, "Favourite dialog has no favourites, displaying message");
- favDialog.setMessage(getResources().getString(R.string.favourites_dialog_message));
- favDialog.setItems(null);
- } else {
- favDialog.setMessage("");
- favDialog.setItems(newFavouriteItems);
- }
- }
-
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Got activity result");
if (resultCode == RESULT_OK) {
@@ -1050,10 +1033,6 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
Log.i(TAG, "Got null poi id");
}
- if (favDialog != null) {
- refreshFavouriteDialog();
- }
-
mapView.invalidate();
}
}
@@ -1066,17 +1045,15 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
ViewDialog viewDialog = new ViewDialog(instance);
return viewDialog;
case FAVOURITE_DIALOG_ID:
- favDialog = new POIDialog(instance);
+ favDialog = new FavouritesDialog(instance);
favDialog.setOnItemClickListener(this);
favDialog.setOnItemLongClickListener(this);
favDialog.setTitle(R.string.favourites_dialog_title);
return favDialog;
case SEARCH_RESULTS_DIALOG_ID:
- searchResultsDialog = new POIDialog(instance);
+ searchResultsDialog = new SearchResultsDialog(instance);
searchResultsDialog.setOnItemClickListener(this);
searchResultsDialog.setOnItemLongClickListener(this);
- searchResultsDialog.setTitle(R.string.search_results_dialog_title);
- searchResultsDialog.setItems(searchResults);
return searchResultsDialog;
case WELCOME_DIALOG_ID:
WelcomeDialog welcomeDialog = new WelcomeDialog(instance);
@@ -1085,54 +1062,75 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
return null;
}
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Log.i(TAG, "OnItemClick pos " + position + " id " + id);
+ protected void onPrepareDialog(int id, Dialog dialog) {
+ if (dialog instanceof FavouritesDialog) {
+ favDialog = (FavouritesDialog) dialog;
- String poiId = null;
- if (favDialog != null) {
- poiId = favDialog.adapter.getItemStringId(position);
- } else if (searchResultsDialog != null) {
- poiId = searchResultsDialog.adapter.getItemStringId(position);
- }
+ ArrayList<POI> newFavouriteItems = new ArrayList<POI>();
- Log.i(TAG, "POI " + poiId + " selected");
-
- POI poi = null;
-
- if (poiId != null) {
- Log.i(TAG, "Got id " + poiId);
try {
- poi = getHelper().getBuildingDao().queryForId(poiId);
- if (poi == null) {
- poi = getHelper().getBusStopDao().queryForId(poiId);
+ Dao<Building, String> buildingDao = getHelper().getBuildingDao();
+ Dao<BusStop, String> busStopDao = getHelper().getBusStopDao();
+
+ final SharedPreferences favouritesPrefs = getSharedPreferences(FAVOURITES_PREFERENCES, 0);
+ for (String idStr : favouritesPrefs.getAll().keySet()) {
+ Building building;
+ BusStop busStop;
+ if ((building = buildingDao.queryForId(idStr)) != null) {
+ newFavouriteItems.add(building);
+ } else if ((busStop = busStopDao.queryForId(idStr)) != null) {
+ newFavouriteItems.add(busStop);
+ } else {
+ Log.e(TAG, "Item in favourites " + idStr + " cannot be found");
+ }
}
} catch (SQLException e) {
e.printStackTrace();
}
- if (poi == null) {
- Log.e(TAG, "Could not find poi " + poiId + " in onActivityResult");
+ Log.i(TAG, "There are " + newFavouriteItems.size() + " favourites");
+ if (newFavouriteItems.size() == 0) {
+ Log.i(TAG, "Favourite dialog has no favourites, displaying message");
+ favDialog.setMessage(getResources().getString(R.string.favourites_dialog_message));
+ favDialog.setItems(null);
} else {
- if (myLocationOverlay != null) {
- myLocationOverlay.disableFollowLocation();
- }
- mapController.setZoom(20);
- mapController.setCenter(poi.point);
+ favDialog.setMessage("");
+ favDialog.setItems(newFavouriteItems);
+ }
+ } else if (dialog instanceof SearchResultsDialog) {
+ searchResultsDialog = (SearchResultsDialog) dialog;
+ }
+ }
- if (favDialog != null) {
- favDialog.dismiss();
- favDialog = null;
- } else if (searchResultsDialog != null) {
- searchResultsDialog.dismiss();
- searchResultsDialog = null;
- }
+ public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+ Log.i(TAG, "OnItemClick pos " + position + " id " + id);
+
+ POI poi = null;
+ if (favDialog != null) {
+ poi = favDialog.adapter.getPOIItem(position);
+ } else if (searchResultsDialog != null) {
+ poi = searchResultsDialog.adapter.getPOIItem(position);
+ }
+
+ if (poi != null) {
+ Log.i(TAG, "Got id " + poi.id);
+ if (myLocationOverlay != null) {
+ myLocationOverlay.disableFollowLocation();
+ }
+ mapController.setZoom(20);
+ mapController.setCenter(poi.point);
+
+ if (favDialog != null) {
+ favDialog.dismiss();
+ favDialog = null;
+ } else if (searchResultsDialog != null) {
+ searchResultsDialog.dismiss();
+ searchResultsDialog = null;
}
+
} else {
Log.i(TAG, "Got null poi id");
-
- // mapController.setZoom(15);
- // mapController.setCenter(new GeoPoint(50935551, -1393488));
}
}
@@ -1144,77 +1142,76 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
Log.i(TAG, "OnItemClick pos " + position + " id " + id);
- String poiId = null;
+ POI poi = null;
if (favDialog != null) {
- poiId = favDialog.adapter.getItemStringId(position);
+ poi = favDialog.adapter.getPOIItem(position);
} else if (searchResultsDialog != null) {
- poiId = searchResultsDialog.adapter.getItemStringId(position);
+ poi = searchResultsDialog.adapter.getPOIItem(position);
}
- Log.i(TAG, "POI " + poiId + " selected");
+ if (poi != null) {
+ Log.i(TAG, "Got id " + poi.id);
- POI poi = null;
+ if (poi.type == POI.BUS_STOP) {
+ BusStop busStop = (BusStop) poi;
- if (poiId != null) {
- Log.i(TAG, "Got id " + poiId);
- try {
- poi = getHelper().getBuildingDao().queryForId(poiId);
- if (poi == null) {
- poi = getHelper().getBusStopDao().queryForId(poiId);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
+ Log.i(TAG, "Pressed " + busStop.id);
- if (poi == null) {
- Log.e(TAG, "Could not find poi " + poiId + " in onActivityResult");
- } else {
- if (poi.type == POI.BUS_STOP) {
- BusStop busStop = (BusStop) poi;
+ Uri uri = Uri.parse("http://id.southampton.ac.uk/bus-stop/" + busStop.id);
- Log.i(TAG, "Pressed " + busStop.id);
+ Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath());
- Uri uri = Uri.parse("http://id.southampton.ac.uk/bus-stop/" + busStop.id);
+ Intent busStopIntent = new Intent(Intent.ACTION_VIEW, uri);
+ startActivity(busStopIntent);
- Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath());
+ return true;
- Intent busStopIntent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(busStopIntent);
+ } else if (poi.type == POI.BUILDING) {
+ Building building = (Building) poi;
- return true;
+ Log.i(TAG, "Pressed " + building.id);
- } else if (poi.type == POI.BUILDING) {
- Building building = (Building) poi;
+ Uri uri = Uri.parse("http://id.southampton.ac.uk/building/" + building.id);
- Log.i(TAG, "Pressed " + building.id);
+ Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath());
- Uri uri = Uri.parse("http://id.southampton.ac.uk/building/" + building.id);
+ Intent buildingIntent = new Intent(Intent.ACTION_VIEW, uri);
+ startActivity(buildingIntent);
- Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath());
+ return true;
- Intent buildingIntent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(buildingIntent);
+ } else if (poi.type == POI.SITE) {
+ Site site = (Site) poi;
- return true;
+ Log.i(TAG, "Pressed " + site.id);
- } else {
+ Uri uri = Uri.parse("http://id.southampton.ac.uk/site/" + site.id);
- myLocationOverlay.disableFollowLocation();
- mapController.setZoom(20);
- mapController.setCenter(poi.point);
+ Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath());
- if (favDialog != null) {
- favDialog.dismiss();
- favDialog = null;
- } else if (searchResultsDialog != null) {
- searchResultsDialog.dismiss();
- searchResultsDialog = null;
- }
+ Intent siteIntent = new Intent(Intent.ACTION_VIEW, uri);
+ startActivity(siteIntent);
+
+ return true;
+
+ } else {
+
+ myLocationOverlay.disableFollowLocation();
+ mapController.setZoom(20);
+ mapController.setCenter(poi.point);
+ if (favDialog != null) {
+ favDialog.dismiss();
+ favDialog = null;
+ } else if (searchResultsDialog != null) {
+ searchResultsDialog.dismiss();
+ searchResultsDialog = null;
}
+
}
+
} else {
- Log.i(TAG, "Got null poi id");
+ Log.i(TAG, "Got null poi");
// mapController.setZoom(15);
// mapController.setCenter(new GeoPoint(50935551, -1393488));