From 606d952ef33ee74dbb303a8870451252d7785f32 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Fri, 9 Mar 2012 12:35:17 +0000 Subject: More search improvements. --- src/net/cbaines/suma/MapContentProvider.java | 120 +++++++++++++++++++++++---- 1 file changed, 103 insertions(+), 17 deletions(-) (limited to 'src/net/cbaines/suma/MapContentProvider.java') diff --git a/src/net/cbaines/suma/MapContentProvider.java b/src/net/cbaines/suma/MapContentProvider.java index a2b42c3..d920b21 100644 --- a/src/net/cbaines/suma/MapContentProvider.java +++ b/src/net/cbaines/suma/MapContentProvider.java @@ -48,6 +48,10 @@ 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 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 @@ -68,16 +72,18 @@ public class MapContentProvider extends ContentProvider { private DatabaseHelper helper; // UriMatcher stuff - private static final int SEARCH_BUILDINGS = 0; - private static final int GET_BUILDING = 1; - private static final int SEARCH_BUS_STOPS = 2; - private static final int GET_BUS_STOP = 3; - private static final int SEARCH_SITES = 4; - private static final int GET_SITE = 5; - private static final int SEARCH_BUSES = 6; - private static final int GET_BUS = 7; - private static final int SEARCH_SUGGEST = 8; - private static final int REFRESH_SHORTCUT = 9; + private static final int SEARCH_ALL = 0; + private static final int GET_ALL = 1; + private static final int SEARCH_BUILDINGS = 2; + private static final int GET_BUILDING = 3; + private static final int SEARCH_BUS_STOPS = 4; + private static final int GET_BUS_STOP = 5; + private static final int SEARCH_SITES = 6; + private static final int GET_SITE = 7; + private static final int SEARCH_BUSES = 8; + private static final int GET_BUS = 9; + private static final int SEARCH_SUGGEST = 10; + private static final int REFRESH_SHORTCUT = 11; private static final UriMatcher sURIMatcher = buildUriMatcher(); /** @@ -87,6 +93,8 @@ public class MapContentProvider extends ContentProvider { private static UriMatcher buildUriMatcher() { UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); // to get definitions... + matcher.addURI(AUTHORITY, "all", SEARCH_BUILDINGS); + matcher.addURI(AUTHORITY, "all/*", GET_BUILDING); matcher.addURI(AUTHORITY, "building", SEARCH_BUILDINGS); matcher.addURI(AUTHORITY, "building/*", GET_BUILDING); matcher.addURI(AUTHORITY, "bus-stop", SEARCH_BUS_STOPS); @@ -141,6 +149,21 @@ public class MapContentProvider extends ContentProvider { } catch (SQLException e1) { e1.printStackTrace(); } + case SEARCH_ALL: + if (selectionArgs == null) { + throw new IllegalArgumentException("selectionArgs must be provided for the Uri: " + uri); + } + try { + return searchAll(selectionArgs[0]); + } catch (SQLException e) { + e.printStackTrace(); + } + case GET_ALL: + try { + return getAll(uri); + } catch (SQLException e) { + e.printStackTrace(); + } case SEARCH_BUILDINGS: if (selectionArgs == null) { throw new IllegalArgumentException("selectionArgs must be provided for the Uri: " + uri); @@ -170,6 +193,13 @@ 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 }; + + MatrixCursor cursor = new MatrixCursor(columnNames); + int id = 0; + Dao buildingDao = helper.getBuildingDao(); QueryBuilder qb = buildingDao.queryBuilder(); @@ -180,14 +210,37 @@ public class MapContentProvider extends ContentProvider { List buildings = buildingDao.query(preparedQuery); Log.v(TAG, "Returning " + buildings.size() + " buildings"); - String[] columnNames = { BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1 }; - - MatrixCursor cursor = new MatrixCursor(columnNames, buildings.size()); - - int id = 0; for (Building building : buildings) { - Log.v(TAG, "Building " + id + ", " + building.name); - Object[] values = { id++, building.name }; + // 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); + } + + Dao busStopDao = helper.getBusStopDao(); + + QueryBuilder busStopQB = busStopDao.queryBuilder(); + busStopQB.where().like(BusStop.ID_FIELD_NAME, "%" + query + "%").or() + .like(BusStop.DESCRIPTION_FIELD_NAME, "%" + query + "%"); + PreparedQuery busStopPreparedQuery = busStopQB.prepare(); + + List 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); } @@ -223,6 +276,35 @@ public class MapContentProvider extends ContentProvider { return cursor; } + private Cursor searchAll(String query) throws SQLException { + Dao buildingDao = helper.getBuildingDao(); + + QueryBuilder qb = buildingDao.queryBuilder(); + qb.where().eq(Building.ID_FIELD_NAME, "%" + query + "%").or().eq(Building.NAME_FIELD_NAME, "%" + query + "%"); + PreparedQuery preparedQuery = qb.prepare(); + + AndroidCompiledStatement compiledStatement = (AndroidCompiledStatement) preparedQuery.compile(helper + .getConnectionSource().getReadOnlyConnection(), StatementType.SELECT); + Cursor cursor = compiledStatement.getCursor(); + + return cursor; + } + + private Cursor getAll(Uri uri) throws SQLException { + String buildingID = uri.getLastPathSegment(); + Dao buildingDao = helper.getBuildingDao(); + + QueryBuilder qb = buildingDao.queryBuilder(); + qb.where().eq(Building.ID_FIELD_NAME, buildingID); + PreparedQuery preparedQuery = qb.prepare(); + + AndroidCompiledStatement compiledStatement = (AndroidCompiledStatement) preparedQuery.compile(helper + .getConnectionSource().getReadOnlyConnection(), StatementType.SELECT); + Cursor cursor = compiledStatement.getCursor(); + + return cursor; + } + private Cursor refreshShortcut(Uri uri) throws SQLException { /* * This won't be called with the current implementation, but if we @@ -254,6 +336,10 @@ public class MapContentProvider extends ContentProvider { @Override public String getType(Uri uri) { switch (sURIMatcher.match(uri)) { + case SEARCH_ALL: + return ALLS_MIME_TYPE; + case GET_ALL: + return ALL_MIME_TYPE; case SEARCH_BUILDINGS: return BUILDINGS_MIME_TYPE; case GET_BUILDING: -- cgit v1.2.3