Permalink
Browse files

fix MockLocations, update the ContentResolver class and add a method …

…to get the most recent location records by phone number
  • Loading branch information...
1 parent b7b08e9 commit eba44f62993a2ac4692e6e4556dba56fa98b6525 techxplorer committed Jan 30, 2012
File renamed without changes.
@@ -20,8 +20,11 @@
package org.servalproject.maps;
import org.mapsforge.android.maps.MapView;
+import org.servalproject.maps.provider.MapItemsContract;
+import android.content.ContentResolver;
import android.content.Intent;
+import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
@@ -113,6 +116,18 @@ public void run() {
Log.v(TAG, "update map task running");
}
+ // resolve the content uri
+ ContentResolver mContentResolver = getApplicationContext().getContentResolver();
+
+ // get the content
+ Cursor mCursor = mContentResolver.query(MapItemsContract.Locations.LATEST_CONTENT_URI, null, null, null, null);
+
+ if(V_LOG) {
+ Log.v(TAG, "rows in cursor: " + mCursor.getCount());
+ }
+
+ mCursor.close();
+
// add the task back onto the queue
updateHandler.postDelayed(updateMapTask, updateDelay);
}
@@ -52,8 +52,8 @@
private String timeZone = TimeZone.getDefault().getID();
// TODO fill in with real phone and sid values
- private String phoneNumber = "myphonenumber";
- private String subscriberId = "mysubscriberid";
+ private String phoneNumber = "myphonenumber2";
+ private String subscriberId = "mysubscriberid2";
private ContentResolver contentResolver;
@@ -38,7 +38,7 @@
* private class level constants
*/
private final String TAG = "MockLocations";
- private final boolean V_LOG = true;
+ private final boolean V_LOG = false;
private final String LOCATION_FILE = "mock-locations.txt";
private final String LOCATION_ZIP_FILE = "mock-locations.zip";
@@ -122,6 +122,11 @@ public MockLocations(Context context) throws IOException {
* request that this thread stops
*/
public void requestStop() {
+
+ if(V_LOG) {
+ Log.v(TAG, "thread requested to stop");
+ }
+
keepGoing = false;
}
@@ -148,7 +153,7 @@ public void run() {
if(keepGoing == false) {
if(V_LOG) {
- Log.v(TAG, "thread requested to stop");
+ Log.v(TAG, "thread stopped");
}
return;
@@ -37,8 +37,9 @@
// private class level constants
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
- private final int LOCATION_DIR_URI = 0;
+ private final int LOCATION_LIST_URI = 0;
private final int LOCATION_ITEM_URI = 1;
+ private final int LOCATION_LATEST_LIST_URI = 3;
private final String TAG = "MapItems";
//private final boolean V_LOG = true;
@@ -58,8 +59,9 @@ public boolean onCreate() {
// define URis that we'll match against
//uriMatcher.addURI(MapItemsContract.Locations.CONTENT_URI, LOCATION_DIR_URI);
- uriMatcher.addURI(MapItemsContract.AUTHORITY, MapItemsContract.Locations.CONTENT_URI_PATH, LOCATION_DIR_URI);
+ uriMatcher.addURI(MapItemsContract.AUTHORITY, MapItemsContract.Locations.CONTENT_URI_PATH, LOCATION_LIST_URI);
uriMatcher.addURI(MapItemsContract.AUTHORITY, MapItemsContract.Locations.CONTENT_URI_PATH + "/#", LOCATION_ITEM_URI);
+ uriMatcher.addURI(MapItemsContract.AUTHORITY, MapItemsContract.Locations.CONTENT_URI_PATH + "/latest", LOCATION_LATEST_LIST_URI);
// create the database connection
databaseHelper = new MainDatabaseHelper(getContext());
@@ -73,15 +75,19 @@ public boolean onCreate() {
* @see android.content.ContentProvider#query(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)
*/
@Override
- public Cursor query(Uri uri, String[] projection, String selection, String[] selctionArgs, String sortOrder) {
+ public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
+
+ int mMatchedUri = -1;
+ Cursor mResults = null;
// choose the table name and sort order based on the URI
switch(uriMatcher.match(uri)) {
- case LOCATION_DIR_URI:
+ case LOCATION_LIST_URI:
// uri matches all of the table
if(TextUtils.isEmpty(sortOrder) == true) {
sortOrder = MapItemsContract.Locations.Table._ID + " ASC";
}
+ mMatchedUri = LOCATION_LIST_URI;
break;
case LOCATION_ITEM_URI:
// uri matches one record
@@ -90,15 +96,48 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel
} else {
selection += "AND " + MapItemsContract.Locations.Table._ID + " = " + uri.getLastPathSegment();
}
+ mMatchedUri = LOCATION_ITEM_URI;
+ break;
+ case LOCATION_LATEST_LIST_URI:
+ // uri matches the group by for latest records
+ mMatchedUri = LOCATION_LATEST_LIST_URI;
break;
default:
// unknown uri found
Log.e(TAG, "unknown URI detected on query: " + uri.toString());
throw new IllegalArgumentException("unknwon URI detected");
}
- Cursor mResults = null;
+ // get a connection to the database
+ database = databaseHelper.getReadableDatabase();
+ if(mMatchedUri == LOCATION_LATEST_LIST_URI) {
+ // get the latest location records
+
+ String[] mColumns = new String[MapItemsContract.Locations.Table.COLUMNS.length + 1];
+
+ for(int i = 0; i < MapItemsContract.Locations.Table.COLUMNS.length; i++) {
+ mColumns[i] = MapItemsContract.Locations.Table.COLUMNS[i];
+ }
+
+ mColumns[mColumns.length -1] = "MAX(" + MapItemsContract.Locations.Table.TIMESTAMP + ")";
+
+
+ mResults = database.query(MapItemsContract.Locations.Table.TABLE_NAME, mColumns, null, null, MapItemsContract.Locations.Table.PHONE_NUMBER, null, null);
+
+// String mSql = "SELECT *, MAX(timestamp) "
+// + "FROM locations "
+// + "GROUP BY phone_number";
+//
+// mResults = database.rawQuery(mSql, null);
+ } else if (mMatchedUri == LOCATION_LIST_URI || mMatchedUri == LOCATION_ITEM_URI){
+ // execute the query as provided
+ mResults = database.query(MapItemsContract.Locations.CONTENT_URI_PATH, projection, selection, selectionArgs, null, null, sortOrder);
+ }
+
+ // play nice and tidy up
+ //database.close();
+
// return the results
return mResults;
}
@@ -118,13 +157,13 @@ public Uri insert(Uri uri, ContentValues values) {
// chose the table name
switch(uriMatcher.match(uri)) {
- case LOCATION_DIR_URI:
+ case LOCATION_LIST_URI:
mTable = MapItemsContract.Locations.CONTENT_URI_PATH;
mContentUri = MapItemsContract.Locations.CONTENT_URI;
break;
default:
// unknown uri found
- Log.e(TAG, "unknown URI detected on insert: " + uri.toString());
+ Log.e(TAG, "invalid URI detected for insert: " + uri.toString());
throw new IllegalArgumentException("unknwon URI detected");
}
@@ -151,10 +190,12 @@ public String getType(Uri uri) {
// choose the mime type
switch(uriMatcher.match(uri)) {
- case LOCATION_DIR_URI:
+ case LOCATION_LIST_URI:
return MapItemsContract.Locations.CONTENT_TYPE_LIST;
case LOCATION_ITEM_URI:
return MapItemsContract.Locations.CONTENT_TYPE_ITEM;
+ case LOCATION_LATEST_LIST_URI:
+ return MapItemsContract.Locations.CONTENT_TYPE_LIST;
default:
// unknown uri found
Log.e(TAG, "unknown URI detected on getType: " + uri.toString());
@@ -55,14 +55,19 @@
*/
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + CONTENT_URI_PATH);
+ /**
+ * content URI for the most recent locations data, grouped by phone number
+ */
+ public static final Uri LATEST_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + CONTENT_URI_PATH + "/latest");
+
/**
- * mime type for the table
+ * content type for a list of items
*/
public static final String CONTENT_TYPE_LIST = "vnd.android.cursor.dir/vnd.org.servalproject.maps.provider.items." + CONTENT_URI_PATH;
/**
- * mime type for an item
+ * content type for an individual item
*/
public static final String CONTENT_TYPE_ITEM = "vnd.android.cursor.item/vnd.org.servalproject.maps.provider.items." + CONTENT_URI_PATH;
@@ -72,6 +77,11 @@
public static final class Table implements BaseColumns {
/**
+ * table name
+ */
+ public static final String TABLE_NAME = Locations.CONTENT_URI_PATH;
+
+ /**
* unique id column
*/
public static final String _ID = BaseColumns._ID;
@@ -106,6 +116,10 @@
*/
public static final String TIMEZONE = "timezone";
+ /**
+ * a list of all of the columns
+ */
+ public static final String[] COLUMNS = {_ID, PHONE_NUMBER, SUBSCRIBER_ID, LATITUDE, LONGITUDE, TIMESTAMP, TIMEZONE};
}
}

0 comments on commit eba44f6

Please sign in to comment.