aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/MapContentProvider.java
blob: d920b2133a1bd56f449cd4b6c82789cea2309bfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.cbaines.suma;

import java.sql.SQLException;
import java.util.List;

import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.util.Log;

import com.j256.ormlite.android.AndroidCompiledStatement;
import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.StatementBuilder.StatementType;

/**
 * Provides access to the dictionary database.
 */
public class MapContentProvider extends ContentProvider {
	String TAG = "MapContentProvider";

	public static String AUTHORITY = "net.cbaines.suma.provider";
	// public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
	// + "/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
			+ "/vnd.net.cbaines.suma.provider.building";
	public static final String BUS_STOPS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
			+ "/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";

	private DatabaseHelper helper;

	// UriMatcher stuff
	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();

	/**
	 * Builds up a UriMatcher for search suggestion and shortcut refresh
	 * queries.
	 */
	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);
		matcher.addURI(AUTHORITY, "bus-stop/*", GET_BUS_STOP);
		matcher.addURI(AUTHORITY, "site", SEARCH_SITES);
		matcher.addURI(AUTHORITY, "site/*", GET_SITE);
		matcher.addURI(AUTHORITY, "bus", SEARCH_BUSES);
		matcher.addURI(AUTHORITY, "bus/*", GET_BUS);

		// to get suggestions...
		matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST);
		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.
		 */
		matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT, REFRESH_SHORTCUT);
		matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT + "/*", REFRESH_SHORTCUT);
		return matcher;
	}

	@Override
	public boolean onCreate() {
		helper = OpenHelperManager.getHelper(this.getContext(), DatabaseHelper.class);
		return true;
	}

	/**
	 * 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) {

		// Use the UriMatcher to see what kind of query we have and format the
		// db query accordingly
		switch (sURIMatcher.match(uri)) {
		case SEARCH_SUGGEST:
			if (selectionArgs == null) {
				throw new IllegalArgumentException("selectionArgs must be provided for the Uri: " + uri);
			}
			try {
				return getSuggestions(selectionArgs[0]);
			} 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);
			}
			try {
				return searchBuildings(selectionArgs[0]);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		case GET_BUILDING:
			try {
				return getBuilding(uri);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		case REFRESH_SHORTCUT:
			try {
				return refreshShortcut(uri);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		default:
			throw new IllegalArgumentException("Unknown Uri: " + uri);
		}
	}

	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<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 + "%");
		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);
		}

		Dao<BusStop, String> busStopDao = helper.getBusStopDao();

		QueryBuilder<BusStop, String> busStopQB = busStopDao.queryBuilder();
		busStopQB.where().like(BusStop.ID_FIELD_NAME, "%" + query + "%").or()
				.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);
		}

		return cursor;
	}

	private Cursor searchBuildings(String query) throws SQLException {
		Dao<Building, String> buildingDao = helper.getBuildingDao();

		QueryBuilder<Building, String> qb = buildingDao.queryBuilder();
		qb.where().eq(Building.ID_FIELD_NAME, "%" + query + "%").or().eq(Building.NAME_FIELD_NAME, "%" + query + "%");
		PreparedQuery<Building> preparedQuery = qb.prepare();

		AndroidCompiledStatement compiledStatement = (AndroidCompiledStatement) preparedQuery.compile(helper
				.getConnectionSource().getReadOnlyConnection(), StatementType.SELECT);
		Cursor cursor = compiledStatement.getCursor();

		return cursor;
	}

	private Cursor getBuilding(Uri uri) throws SQLException {
		String buildingID = uri.getLastPathSegment();
		Dao<Building, String> buildingDao = helper.getBuildingDao();

		QueryBuilder<Building, String> qb = buildingDao.queryBuilder();
		qb.where().eq(Building.ID_FIELD_NAME, buildingID);
		PreparedQuery<Building> preparedQuery = qb.prepare();

		AndroidCompiledStatement compiledStatement = (AndroidCompiledStatement) preparedQuery.compile(helper
				.getConnectionSource().getReadOnlyConnection(), StatementType.SELECT);
		Cursor cursor = compiledStatement.getCursor();

		return cursor;
	}

	private Cursor searchAll(String query) throws SQLException {
		Dao<Building, String> buildingDao = helper.getBuildingDao();

		QueryBuilder<Building, String> qb = buildingDao.queryBuilder();
		qb.where().eq(Building.ID_FIELD_NAME, "%" + query + "%").or().eq(Building.NAME_FIELD_NAME, "%" + query + "%");
		PreparedQuery<Building> 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<Building, String> buildingDao = helper.getBuildingDao();

		QueryBuilder<Building, String> qb = buildingDao.queryBuilder();
		qb.where().eq(Building.ID_FIELD_NAME, buildingID);
		PreparedQuery<Building> 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
		 * 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();

		QueryBuilder<Building, String> qb = buildingDao.queryBuilder();
		qb.where().eq(Building.ID_FIELD_NAME, buildingID);
		PreparedQuery<Building> preparedQuery = qb.prepare();

		AndroidCompiledStatement compiledStatement = (AndroidCompiledStatement) preparedQuery.compile(helper
				.getConnectionSource().getReadOnlyConnection(), StatementType.SELECT);
		Cursor cursor = compiledStatement.getCursor();

		return cursor;
	}

	/**
	 * 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) {
		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:
			return BUILDING_MIME_TYPE;
		case SEARCH_BUS_STOPS:
			return BUS_STOPS_MIME_TYPE;
		case GET_BUS_STOP:
			return BUS_STOP_MIME_TYPE;
		case SEARCH_SITES:
			return SITES_MIME_TYPE;
		case GET_SITE:
			return SITE_MIME_TYPE;
		case SEARCH_BUSES:
			return BUSES_MIME_TYPE;
		case GET_BUS:
			return BUS_MIME_TYPE;
		case SEARCH_SUGGEST:
			return SearchManager.SUGGEST_MIME_TYPE;
		case REFRESH_SHORTCUT:
			return SearchManager.SHORTCUT_MIME_TYPE;
		default:
			throw new IllegalArgumentException("Unknown URL " + uri);
		}
	}

	// Other required implementations...

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		throw new UnsupportedOperationException();
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		throw new UnsupportedOperationException();
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		throw new UnsupportedOperationException();
	}

}