diff options
Diffstat (limited to 'src/net/cbaines/suma/MapContentProvider.java')
-rw-r--r-- | src/net/cbaines/suma/MapContentProvider.java | 141 |
1 files changed, 75 insertions, 66 deletions
diff --git a/src/net/cbaines/suma/MapContentProvider.java b/src/net/cbaines/suma/MapContentProvider.java index d920b21..efbdf2f 100644 --- a/src/net/cbaines/suma/MapContentProvider.java +++ b/src/net/cbaines/suma/MapContentProvider.java @@ -17,6 +17,8 @@ package net.cbaines.suma; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import android.app.SearchManager; @@ -48,10 +50,8 @@ public class MapContentProvider extends ContentProvider { // + "/building"); // MIME types used for searching words or looking up a single definition - public static final String ALLS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE - + "/vnd.net.cbaines.suma.provider.all"; - public static final String ALL_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE - + "/vnd.net.cbaines.suma.provider.all"; + public static final String ALLS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.net.cbaines.suma.provider.all"; + public static final String ALL_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.net.cbaines.suma.provider.all"; public static final String BUILDINGS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.net.cbaines.suma.provider.building"; public static final String BUILDING_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE @@ -60,14 +60,10 @@ public class MapContentProvider extends ContentProvider { + "/vnd.net.cbaines.suma.provider.bus-stop"; public static final String BUS_STOP_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.net.cbaines.suma.provider.bus-stop"; - public static final String SITES_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE - + "/vnd.net.cbaines.suma.provider.site"; - public static final String SITE_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE - + "/vnd.net.cbaines.suma.provider.site"; - public static final String BUSES_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE - + "/vnd.net.cbaines.suma.provider.bus"; - public static final String BUS_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE - + "/vnd.net.cbaines.suma.provider.bus"; + public static final String SITES_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.net.cbaines.suma.provider.site"; + public static final String SITE_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.net.cbaines.suma.provider.site"; + public static final String BUSES_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.net.cbaines.suma.provider.bus"; + public static final String BUS_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.net.cbaines.suma.provider.bus"; private DatabaseHelper helper; @@ -87,8 +83,7 @@ public class MapContentProvider extends ContentProvider { private static final UriMatcher sURIMatcher = buildUriMatcher(); /** - * Builds up a UriMatcher for search suggestion and shortcut refresh - * queries. + * Builds up a UriMatcher for search suggestion and shortcut refresh queries. */ private static UriMatcher buildUriMatcher() { UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); @@ -109,12 +104,10 @@ public class MapContentProvider extends ContentProvider { matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST); /* - * The following are unused in this implementation, but if we include - * {@link SearchManager#SUGGEST_COLUMN_SHORTCUT_ID} as a column in our - * suggestions table, we could expect to receive refresh queries when a - * shortcutted suggestion is displayed in Quick Search Box, in which - * case, the following Uris would be provided and we would return a - * cursor with a single item representing the refreshed suggestion data. + * The following are unused in this implementation, but if we include {@link SearchManager#SUGGEST_COLUMN_SHORTCUT_ID} as + * a column in our suggestions table, we could expect to receive refresh queries when a shortcutted suggestion is + * displayed in Quick Search Box, in which case, the following Uris would be provided and we would return a cursor with a + * single item representing the refreshed suggestion data. */ matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT, REFRESH_SHORTCUT); matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT + "/*", REFRESH_SHORTCUT); @@ -128,11 +121,9 @@ public class MapContentProvider extends ContentProvider { } /** - * Handles all the dictionary searches and suggestion queries from the - * Search Manager. When requesting a specific word, the uri alone is - * required. When searching all of the dictionary for matches, the - * selectionArgs argument must carry the search query as the first element. - * All other arguments are ignored. + * Handles all the dictionary searches and suggestion queries from the Search Manager. When requesting a specific word, the + * uri alone is required. When searching all of the dictionary for matches, the selectionArgs argument must carry the search + * query as the first element. All other arguments are ignored. */ @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { @@ -193,9 +184,10 @@ public class MapContentProvider extends ContentProvider { private Cursor getSuggestions(String query) throws SQLException { Log.v(TAG, "Got query for " + query); - String[] columnNames = { BaseColumns._ID, SearchManager.SUGGEST_COLUMN_ICON_1, - SearchManager.SUGGEST_COLUMN_TEXT_1, SearchManager.SUGGEST_COLUMN_TEXT_2, - SearchManager.SUGGEST_COLUMN_INTENT_DATA }; + String[] columnNames = { BaseColumns._ID, SearchManager.SUGGEST_COLUMN_ICON_1, SearchManager.SUGGEST_COLUMN_TEXT_1, + SearchManager.SUGGEST_COLUMN_TEXT_2, SearchManager.SUGGEST_COLUMN_INTENT_DATA }; + + List<POI> results = new ArrayList<POI>(); MatrixCursor cursor = new MatrixCursor(columnNames); int id = 0; @@ -203,24 +195,10 @@ public class MapContentProvider extends ContentProvider { Dao<Building, String> buildingDao = helper.getBuildingDao(); QueryBuilder<Building, String> qb = buildingDao.queryBuilder(); - qb.where().like(Building.ID_FIELD_NAME, "%" + query + "%").or() - .like(Building.NAME_FIELD_NAME, "%" + query + "%"); + qb.where().like(Building.ID_FIELD_NAME, "%" + query + "%").or().like(Building.NAME_FIELD_NAME, "%" + query + "%"); PreparedQuery<Building> preparedQuery = qb.prepare(); - List<Building> buildings = buildingDao.query(preparedQuery); - Log.v(TAG, "Returning " + buildings.size() + " buildings"); - - for (Building building : buildings) { - // Log.v(TAG, "Building " + id + ", " + building.name); - Object[] values = { - id++, - R.drawable.building, - building.name, - building.id, - "geo:" + Util.E6IntToDouble(building.point.getLatitudeE6()) + "," - + Util.E6IntToDouble(building.point.getLongitudeE6()) + "?z=18" }; - cursor.addRow(values); - } + results.addAll(buildingDao.query(preparedQuery)); Dao<BusStop, String> busStopDao = helper.getBusStopDao(); @@ -229,19 +207,52 @@ public class MapContentProvider extends ContentProvider { .like(BusStop.DESCRIPTION_FIELD_NAME, "%" + query + "%"); PreparedQuery<BusStop> busStopPreparedQuery = busStopQB.prepare(); - List<BusStop> busStops = busStopDao.query(busStopPreparedQuery); - Log.v(TAG, "Returning " + busStops.size() + " busStops"); - - for (BusStop busStop : busStops) { - // Log.v(TAG, "Building " + id + ", " + building.name); - Object[] values = { - id++, - R.drawable.busstop, - busStop.description, - busStop.id, - "geo:" + Util.E6IntToDouble(busStop.point.getLatitudeE6()) + "," - + Util.E6IntToDouble(busStop.point.getLongitudeE6()) + "?z=18" }; - cursor.addRow(values); + results.addAll(busStopDao.query(busStopPreparedQuery)); + + Dao<Site, String> siteDao = helper.getSiteDao(); + + QueryBuilder<Site, String> siteQB = siteDao.queryBuilder(); + siteQB.where().like(Site.NAME_FIELD_NAME, "%" + query + "%").or().like(Site.ID_FIELD_NAME, "%" + query + "%"); + PreparedQuery<Site> sitePreparedQuery = siteQB.prepare(); + + results.addAll(siteDao.query(sitePreparedQuery)); + + Collections.sort(results, new StringPOIDistanceComparator(query)); + + for (POI poi : results) { + if (poi instanceof Site) { + Site site = (Site) poi; + Object[] values = { + id++, + R.drawable.empty, + site.name, + site.id, + "geo:" + Util.E6IntToDouble(site.point.getLatitudeE6()) + "," + + Util.E6IntToDouble(site.point.getLongitudeE6()) + "?z=18" }; + cursor.addRow(values); + } else if (poi instanceof Building) { + Building building = (Building) poi; + Object[] values = { + id++, + R.drawable.building, + building.name, + building.id, + "geo:" + Util.E6IntToDouble(building.point.getLatitudeE6()) + "," + + Util.E6IntToDouble(building.point.getLongitudeE6()) + "?z=18" }; + cursor.addRow(values); + } else if (poi instanceof BusStop) { + BusStop busStop = (BusStop) poi; + Object[] values = { + id++, + R.drawable.busstop, + busStop.description, + busStop.id, + "geo:" + Util.E6IntToDouble(busStop.point.getLatitudeE6()) + "," + + Util.E6IntToDouble(busStop.point.getLongitudeE6()) + "?z=18" }; + cursor.addRow(values); + } else { + Log.e(TAG, "Error, unexpected class"); + } } return cursor; @@ -307,13 +318,11 @@ public class MapContentProvider extends ContentProvider { private Cursor refreshShortcut(Uri uri) throws SQLException { /* - * This won't be called with the current implementation, but if we - * include {@link SearchManager#SUGGEST_COLUMN_SHORTCUT_ID} as a column - * in our suggestions table, we could expect to receive refresh queries - * when a shortcutted suggestion is displayed in Quick Search Box. In - * which case, this method will query the table for the specific word, - * using the given item Uri and provide all the columns originally - * provided with the suggestion query. + * This won't be called with the current implementation, but if we include {@link + * SearchManager#SUGGEST_COLUMN_SHORTCUT_ID} as a column in our suggestions table, we could expect to receive refresh + * queries when a shortcutted suggestion is displayed in Quick Search Box. In which case, this method will query the table + * for the specific word, using the given item Uri and provide all the columns originally provided with the suggestion + * query. */ String buildingID = uri.getLastPathSegment(); Dao<Building, String> buildingDao = helper.getBuildingDao(); @@ -330,8 +339,8 @@ public class MapContentProvider extends ContentProvider { } /** - * This method is required in order to query the supported types. It's also - * useful in our own query() method to determine the type of Uri received. + * This method is required in order to query the supported types. It's also useful in our own query() method to determine the + * type of Uri received. */ @Override public String getType(Uri uri) { |