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.java593
1 files changed, 307 insertions, 286 deletions
diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java
index cc7d7b9..58c7918 100644
--- a/src/net/cbaines/suma/MapActivity.java
+++ b/src/net/cbaines/suma/MapActivity.java
@@ -19,8 +19,11 @@
package net.cbaines.suma;
+import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
@@ -42,11 +45,13 @@ import org.osmdroid.views.util.constants.MapViewConstants;
import android.app.AlertDialog;
import android.app.Dialog;
+import android.app.SearchManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
+import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
@@ -63,20 +68,16 @@ import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import com.j256.ormlite.dao.Dao;
+import com.j256.ormlite.stmt.PreparedQuery;
+import com.j256.ormlite.stmt.QueryBuilder;
/**
*
* @author Christopher Baines <cbaines8@gmail.com>
*
*/
-public class MapActivity extends ToastHelperActivity implements MapViewConstants, Runnable, RouteColorConstants,
- OnItemClickListener, OnItemLongClickListener, OnSharedPreferenceChangeListener, Preferences {
-
- /**
- * Enable to use the database in the assets folder, if its not enabled, the database is built from the csv files in the assets
- * folder
- */
- private boolean useBundledDatabase = true;
+public class MapActivity extends ToastHelperActivity implements MapViewConstants, RouteColorConstants, OnItemClickListener,
+ OnItemLongClickListener, OnSharedPreferenceChangeListener, Preferences {
private MapView mapView;
private MapController mapController;
@@ -86,7 +87,12 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
static final int VIEW_DIALOG_ID = 0;
static final int FAVOURITE_DIALOG_ID = 1;
- private POIDialog favDialog;
+ static final int WELCOME_DIALOG_ID = 2;
+ static final int SEARCH_RESULTS_DIALOG_ID = 3;
+
+ private ArrayList<POI> searchResults = null;
+
+ private FavouritesDialog favDialog;
private HashMap<String, Overlay> overlays = new HashMap<String, Overlay>();
private HashMap<String, Overlay> pastOverlays;
@@ -173,6 +179,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
private MapActivity instance;
+ private SearchResultsDialog searchResultsDialog;
+
private static final String TAG = "MapActivity";
@SuppressWarnings("unchecked")
@@ -183,8 +191,45 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
instance = this;
- Thread databaseThread = new Thread(this); // Start the database thread
- databaseThread.start();
+ try {
+ File dbFile = new File(DATABASE_PATH + DATABASE_NAME);
+ if (dbFile.exists()) {
+ Log.i(TAG, "Database exists");
+
+ SQLiteDatabase checkDB = SQLiteDatabase.openDatabase(DATABASE_PATH + DATABASE_NAME, null,
+ SQLiteDatabase.OPEN_READONLY);
+ int version = checkDB.getVersion();
+ checkDB.close();
+
+ if (version != DATABASE_VERSION) {
+ Log.i(TAG, "Not the right version");
+
+ copyDatabase();
+ }
+ } else {
+ Log.i(TAG, "Database does not exist");
+
+ SQLiteDatabase db = getHelper().getWritableDatabase();
+
+ if (USE_BUNDLED_DATABASE) {
+ db.close();
+
+ copyDatabase();
+ } else {
+ Log.i(TAG, "Creating database");
+
+ try {
+ DataManager.createDatabase(instance);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
setContentView(R.layout.map_activity);
@@ -206,13 +251,8 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
myLocationOverlay.setEnabled(true);
Log.i(TAG, "Finished creating myLocationOverlay");
- while (databaseThread.isAlive()) {
- Thread.yield();
- }
-
new Thread(new Runnable() {
public void run() {
- Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
try {
showOverlays();
} catch (SQLException e) {
@@ -226,57 +266,163 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
mapController = mapView.getController();
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
- GeoPoint userLocation = null;
+ GeoPoint startLocation = null;
- Bundle extras = getIntent().getExtras();
- if (getIntent().getDataString() != null) {
+ Intent intent = getIntent();
+ Bundle extras = intent.getExtras();
- Log.i(TAG, "getIntent().getDataString() " + getIntent().getDataString());
+ if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
+ String query = intent.getStringExtra(SearchManager.QUERY);
- String str = getIntent().getDataString().substring(4, getIntent().getDataString().length());
- String[] strParts = str.split(",");
+ searchResults = new ArrayList<POI>();
- int lat = Util.doubleToIntE6(Double.valueOf(strParts[0]));
- int lng;
+ try {
+ Dao<Building, String> buildingDao = getHelper().getBuildingDao();
+
+ QueryBuilder<Building, String> buildingQueryBuilder = buildingDao.queryBuilder();
+ buildingQueryBuilder.where().like(Building.ID_FIELD_NAME, "%" + query + "%").or()
+ .like(Building.NAME_FIELD_NAME, "%" + query + "%");
+ PreparedQuery<Building> buildingPreparedQuery = buildingQueryBuilder.prepare();
+ List<Building> buildings = buildingDao.query(buildingPreparedQuery);
+ searchResults.addAll(buildings);
+ buildings = null;
+
+ Dao<BusStop, String> busStopDao = getHelper().getBusStopDao();
+
+ QueryBuilder<BusStop, String> busStopQueryBuilder = busStopDao.queryBuilder();
+ busStopQueryBuilder.where().like(BusStop.ID_FIELD_NAME, "%" + query + "%").or()
+ .like(BusStop.DESCRIPTION_FIELD_NAME, "%" + query + "%");
+ PreparedQuery<BusStop> busStopPreparedQuery = busStopQueryBuilder.prepare();
+ List<BusStop> busStops = busStopDao.query(busStopPreparedQuery);
+ 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);
- if (strParts[1].contains("?")) {
- String zoom = strParts[1].substring(strParts[1].indexOf("?") + 3, strParts[1].length());
- String strLng = strParts[1].substring(0, strParts[1].indexOf("?") - 1);
- lng = Util.doubleToIntE6(Double.valueOf(strLng));
+ showDialog(SEARCH_RESULTS_DIALOG_ID);
- mapController.setZoom(Integer.valueOf(zoom));
- } else {
- lng = Util.doubleToIntE6(Double.valueOf(strParts[1]));
- mapController.setZoom(15);
+ 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();
}
- userLocation = new GeoPoint(lat, lng);
+ } else if (intent.getDataString() != null) {
+ String dataString = getIntent().getDataString();
+
+ Log.i(TAG, "getIntent().getDataString() " + dataString);
+
+ if (dataString.startsWith("content")) {
+
+ Uri data = intent.getData();
+
+ Log.i("Data: ", data.toString());
+
+ startLocation = new GeoPoint(50935551, -1393488);
+ } else {
+
+ String str = getIntent().getDataString().substring(4, getIntent().getDataString().length());
+ String[] strParts = str.split(",");
+ int lat = Util.doubleToIntE6(Double.valueOf(strParts[0]));
+ int lng;
+
+ if (strParts[1].contains("?")) {
+ String zoom = strParts[1].substring(strParts[1].indexOf("?") + 3, strParts[1].length());
+ String strLng = strParts[1].substring(0, strParts[1].indexOf("?") - 1);
+ lng = Util.doubleToIntE6(Double.valueOf(strLng));
+
+ mapController.setZoom(Integer.valueOf(zoom));
+ } else {
+ lng = Util.doubleToIntE6(Double.valueOf(strParts[1]));
+ mapController.setZoom(15);
+ }
+
+ startLocation = new GeoPoint(lat, lng);
+ }
} else if (extras != null && extras.containsKey("poiPoint")) {
String poiPoint = getIntent().getExtras().getString("poiPoint");
Log.i(TAG, "poiPoint " + poiPoint);
String[] bits = poiPoint.split(",");
- userLocation = new GeoPoint(Double.valueOf(bits[0]), Double.valueOf(bits[1]));
+ startLocation = new GeoPoint(Double.valueOf(bits[0]), Double.valueOf(bits[1]));
mapController.setZoom(20);
} else {
- userLocation = myLocationOverlay.getMyLocation();
- if (userLocation == null) {
- userLocation = new GeoPoint(50935551, -1393488); // ECS
+ startLocation = myLocationOverlay.getMyLocation();
+ if (startLocation == null) {
+ startLocation = new GeoPoint(50935551, -1393488); // ECS
}
mapController.setZoom(15);
}
- mapController.setCenter(userLocation);
+ mapController.setCenter(startLocation);
+
+ final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
+ String appVersion = sharedPrefs.getString(APP_VERSION, APP_NOT_INSTALLED);
+
+ if (appVersion.equals(APP_NOT_INSTALLED) || !appVersion.equals(CURRENT_APP_VERSION)) {
+ showDialog(WELCOME_DIALOG_ID);
+ }
Log.i(TAG, "Finished onCreate " + (System.currentTimeMillis() - startTime));
}
+ private void copyDatabase() throws IOException {
+ Log.i(TAG, "Begining copy database");
+
+ InputStream myInput = getAssets().open(DATABASE_NAME);
+
+ // Path to the just created empty db
+ String outFileName = DATABASE_PATH + DATABASE_NAME;
+
+ File database = new File(outFileName);
+ if (database.exists()) {
+ database.delete();
+ }
+
+ // Open the empty db as the output stream
+ OutputStream myOutput = new FileOutputStream(outFileName);
+
+ // transfer bytes from the inputfile to the outputfile
+ byte[] buffer = new byte[1024];
+ int length;
+ while ((length = myInput.read(buffer)) > 0) {
+ myOutput.write(buffer, 0, length);
+ }
+
+ // Close the streams
+
+ myOutput.flush();
+
+ myOutput.close();
+ myInput.close();
+
+ Log.i(TAG, "Finished copying db");
+ }
+
public void onResume() {
super.onResume();
- Log.i(TAG, "OnResume");
+ Log.i(TAG, "OnResume " + (System.currentTimeMillis() - startTime));
if (myLocationOverlay != null) {
final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences activityPrefs = getPreferences(0);
@@ -299,7 +445,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
sharedPrefs.registerOnSharedPreferenceChangeListener(this);
activityPrefs.registerOnSharedPreferenceChangeListener(this);
}
- Log.i(TAG, "Finished OnResume");
+ Log.i(TAG, "Finished OnResume " + (System.currentTimeMillis() - startTime));
}
public void onPause() {
@@ -320,135 +466,6 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
return overlays;
}
- public void run() {
- Log.i(TAG, "Begining loading database " + (System.currentTimeMillis() - startTime));
-
- DatabaseHelper helper = getHelper();
- Log.i(TAG, "Got the helper at " + (System.currentTimeMillis() - startTime));
-
- boolean dbExist = helper.checkDataBase();
- Log.i(TAG, "Finished checking the database at " + (System.currentTimeMillis() - startTime));
-
- if (dbExist) {
- // do nothing - database already exist
- } else {
-
- if (useBundledDatabase) {
- try {
- // By calling this method and empty database will be created
- // into the default system path
- // of your application so we are gonna be able to overwrite
- // that database with our database.
- Log.i(TAG, "GetReadableDatabase");
- helper.getWritableDatabase().close();
-
- helper.copyDataBase();
- Log.i(TAG, "Out of copy database");
- } catch (IOException ioe) {
- throw new Error("Unable to create database");
- }
- } else {
- Thread buildingThread = null;
- Thread busStopThread = null;
- Thread siteThread = null;
-
- Log.i(TAG, "Begining loading databases " + (System.currentTimeMillis() - startTime));
- try {
- Dao<Building, String> buildingDao;
-
- buildingDao = helper.getBuildingDao();
-
- long buildingCount = buildingDao.countOf();
- Log.i(TAG, "Building count " + buildingCount);
- if (buildingCount < 260) {
- buildingThread = new Thread(new Runnable() {
- public void run() {
- try {
- DataManager.loadBuildings(instance);
- Log.i(TAG, "Loaded building database " + (System.currentTimeMillis() - startTime));
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
-
- buildingThread.start();
- }
-
- Dao<BusStop, String> busStopDao = helper.getBusStopDao();
- Dao<BusRoute, Integer> busRouteDao = helper.getBusRouteDao();
- Dao<RouteStop, Integer> routeStopsDao = helper.getRouteStopsDao();
-
- long busStopCount = busStopDao.countOf();
- long busRouteCount = busRouteDao.countOf();
- long routeStopsCount = routeStopsDao.countOf();
-
- Log.i(TAG, "BusStop count " + busStopCount);
- Log.i(TAG, "BusRoute count " + busRouteCount);
- Log.i(TAG, "RouteStops count " + routeStopsCount);
- if (busStopCount < 217 || busRouteCount < 5 || routeStopsCount < 327) {
- busStopThread = new Thread(new Runnable() {
- public void run() {
- try {
- DataManager.loadBusData(instance, true);
- Log.i(TAG, "Loaded bus stop database " + (System.currentTimeMillis() - startTime));
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
-
- busStopThread.start();
- }
-
- Dao<Site, String> siteDao = helper.getSiteDao();
-
- long siteCount = siteDao.countOf();
- Log.i(TAG, "Sites count " + siteCount);
- if (siteCount < 21) {
- siteThread = new Thread(new Runnable() {
- public void run() {
- try {
- DataManager.loadSiteData(instance);
- Log.i(TAG, "Loaded site database " + (System.currentTimeMillis() - startTime));
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
-
- siteThread.start();
- }
-
- while (true) {
- if ((buildingThread == null || !buildingThread.isAlive())
- && (busStopThread == null || !busStopThread.isAlive())
- && (siteThread == null || !siteThread.isAlive()))
- break;
-
- Thread.yield();
- }
-
- Log.i(TAG, "Finished loading databases " + (System.currentTimeMillis() - startTime));
-
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
-
- }
-
- Log.i(TAG, "Begining setting up the static values " + (System.currentTimeMillis() - startTime));
-
- Log.i(TAG, "Finished the database thread " + (System.currentTimeMillis() - startTime));
- }
-
private void showOverlays() throws SQLException {
Log.i(TAG, "Began showing overlays at " + (System.currentTimeMillis() - startTime));
@@ -473,10 +490,11 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
showBuildingOverlays();
}
- Log.i(TAG, "Begining to show the route overlays at " + (System.currentTimeMillis() - startTime));
+ Log.v(TAG, "Begining to show the route overlays at " + (System.currentTimeMillis() - startTime));
for (BusRoute busRoute : getHelper().getBusRouteDao()) {
if (!busRoute.uniLink) {
- // Log.v(TAG, "Bus route " + busRoute.code + "(" + busRoute.id + ") is not unilink");
+ // Log.v(TAG, "Bus route " + busRoute.code + "(" + busRoute.id +
+ // ") is not unilink");
continue;
}
Log.v(TAG, "Looking at showing " + busRoute.code + " route overlay");
@@ -484,12 +502,13 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
showRouteOverlay(busRoute);
}
}
- Log.i(TAG, "Finished loading routes " + (System.currentTimeMillis() - startTime));
+ Log.v(TAG, "Finished loading routes " + (System.currentTimeMillis() - startTime));
- Log.i(TAG, "Begining to show the site overlays at " + (System.currentTimeMillis() - startTime));
+ Log.v(TAG, "Begining to show the site overlays at " + (System.currentTimeMillis() - startTime));
try {
for (Site site : getHelper().getSiteDao()) {
- // Log.v(TAG, "Looking at showing " + site.name + " site overlay");
+ // Log.v(TAG, "Looking at showing " + site.name +
+ // " site overlay");
if (activityPrefs.getBoolean(SITE_OVERLAYS + site.name, SITE_OVERLAYS_ENABLED_BY_DEFAULT)) {
showSiteOverlay(site);
}
@@ -497,9 +516,9 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
} catch (SQLException e) {
e.printStackTrace();
}
- Log.i(TAG, "Finished showing the site overlays " + (System.currentTimeMillis() - startTime));
+ Log.v(TAG, "Finished showing the site overlays " + (System.currentTimeMillis() - startTime));
- Log.i(TAG, "Finished showing all the overlays " + (System.currentTimeMillis() - startTime));
+ Log.v(TAG, "Finished showing all the overlays " + (System.currentTimeMillis() - startTime));
}
private void showUtilityOverlays() {
@@ -915,8 +934,7 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_find:
- Intent i = new Intent(MapActivity.this, FindActivity.class);
- startActivityForResult(i, 0);
+ onSearchRequested();
return true;
case R.id.menu_preferences:
Intent settingsActivity = new Intent(getBaseContext(), PreferencesActivity.class);
@@ -968,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);
@@ -981,47 +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);
- }
- }
-
- @Override
- public boolean onSearchRequested() {
- Intent i = new Intent(MapActivity.this, FindActivity.class);
- startActivityForResult(i, 0);
- return false;
- }
-
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Got activity result");
if (resultCode == RESULT_OK) {
@@ -1058,10 +1033,6 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
Log.i(TAG, "Got null poi id");
}
- if (favDialog != null) {
- refreshFavouriteDialog();
- }
-
mapView.invalidate();
}
}
@@ -1074,52 +1045,92 @@ 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 SearchResultsDialog(instance);
+ searchResultsDialog.setOnItemClickListener(this);
+ searchResultsDialog.setOnItemLongClickListener(this);
+ return searchResultsDialog;
+ case WELCOME_DIALOG_ID:
+ WelcomeDialog welcomeDialog = new WelcomeDialog(instance);
+ return welcomeDialog;
}
return null;
}
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Log.i(TAG, "OnItemClick pos " + position + " id " + id);
-
- String poiId = favDialog.adapter.getItemStringId(position);
+ protected void onPrepareDialog(int id, Dialog dialog) {
+ if (dialog instanceof FavouritesDialog) {
+ favDialog = (FavouritesDialog) dialog;
- Log.i(TAG, "POI " + poiId + " selected");
+ ArrayList<POI> newFavouriteItems = new ArrayList<POI>();
- 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;
+ }
+ }
- favDialog.dismiss();
+ 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));
}
}
@@ -1131,66 +1142,76 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
Log.i(TAG, "OnItemClick pos " + position + " id " + id);
- String poiId = favDialog.adapter.getItemStringId(position);
+ POI poi = null;
+ if (favDialog != null) {
+ poi = favDialog.adapter.getPOIItem(position);
+ } else if (searchResultsDialog != null) {
+ 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);
+
+ Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath());
- myLocationOverlay.disableFollowLocation();
- mapController.setZoom(20);
- mapController.setCenter(poi.point);
+ 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));