Addressed review suggestions and moved lat/long logic to the TripStatistics class.

This commit is contained in:
Rodrigo Damazio
2012-01-10 16:27:41 -02:00
parent 83af7a30d8
commit dad0befca4
3 changed files with 87 additions and 39 deletions
@@ -106,7 +106,7 @@ public class SearchActivity extends ListActivity {
} }
private void doSearch(SearchQuery query) { private void doSearch(SearchQuery query) {
SortedSet<ScoredResult> scoredResults = engine.doSearch(query); SortedSet<ScoredResult> scoredResults = engine.search(query);
final List<? extends Map<String, ?>> displayResults = prepareResultsforDisplay(scoredResults); final List<? extends Map<String, ?>> displayResults = prepareResultsforDisplay(scoredResults);
@@ -34,6 +34,24 @@ import java.util.TreeSet;
*/ */
public class SearchEngine { public class SearchEngine {
/** WHERE query to get tracks by name. */
private static final String TRACK_SELECTION_QUERY =
TracksColumns.NAME + " LIKE ? OR " +
TracksColumns.DESCRIPTION + " LIKE ? OR " +
TracksColumns.CATEGORY + " LIKE ?";
/** WHERE query to get waypoints by name. */
private static final String WAYPOINT_SELECTION_QUERY =
WaypointsColumns.NAME + " LIKE ? OR " +
WaypointsColumns.DESCRIPTION + " LIKE ? OR " +
WaypointsColumns.CATEGORY + " LIKE ?";
/** Order of track results. */
private static final String TRACK_SELECTION_ORDER = TracksColumns._ID + " DESC LIMIT 1000";
/** Order of waypoint results. */
private static final String WAYPOINT_SELECTION_ORDER = WaypointsColumns._ID + " DESC";
/** How much we promote a match in the track category. */ /** How much we promote a match in the track category. */
private static final double TRACK_CATEGORY_PROMOTION = 2.0; private static final double TRACK_CATEGORY_PROMOTION = 2.0;
@@ -122,7 +140,7 @@ public class SearchEngine {
* @param query the query to execute * @param query the query to execute
* @return a set of results, sorted according to their score * @return a set of results, sorted according to their score
*/ */
public SortedSet<ScoredResult> doSearch(SearchQuery query) { public SortedSet<ScoredResult> search(SearchQuery query) {
ArrayList<Track> tracks = new ArrayList<Track>(); ArrayList<Track> tracks = new ArrayList<Track>();
ArrayList<Waypoint> waypoints = new ArrayList<Waypoint>(); ArrayList<Waypoint> waypoints = new ArrayList<Waypoint>();
TreeSet<ScoredResult> scoredResults = new TreeSet<ScoredResult>(SCORED_RESULT_COMPARATOR); TreeSet<ScoredResult> scoredResults = new TreeSet<ScoredResult>(SCORED_RESULT_COMPARATOR);
@@ -144,17 +162,13 @@ public class SearchEngine {
*/ */
private void retrieveTracks(SearchQuery query, ArrayList<Track> tracks) { private void retrieveTracks(SearchQuery query, ArrayList<Track> tracks) {
String queryLikeSelection = "%" + query.textQuery + "%"; String queryLikeSelection = "%" + query.textQuery + "%";
String trackSelection =
TracksColumns.NAME + " LIKE ? OR " +
TracksColumns.DESCRIPTION + " LIKE ? OR " +
TracksColumns.CATEGORY + " LIKE ?";
String order = TracksColumns._ID + " DESC LIMIT 1000"; // Favor recent tracks.
String[] trackSelectionArgs = new String[] { String[] trackSelectionArgs = new String[] {
queryLikeSelection, queryLikeSelection,
queryLikeSelection, queryLikeSelection,
queryLikeSelection }; queryLikeSelection };
Cursor tracksCursor = providerUtils.getTracksCursor(trackSelection, trackSelectionArgs, order); Cursor tracksCursor = providerUtils.getTracksCursor(
TRACK_SELECTION_QUERY, trackSelectionArgs, TRACK_SELECTION_ORDER);
if (tracksCursor != null) { if (tracksCursor != null) {
try { try {
tracks.ensureCapacity(tracksCursor.getCount()); tracks.ensureCapacity(tracksCursor.getCount());
@@ -176,16 +190,12 @@ public class SearchEngine {
*/ */
private void retrieveWaypoints(SearchQuery query, ArrayList<Waypoint> waypoints) { private void retrieveWaypoints(SearchQuery query, ArrayList<Waypoint> waypoints) {
String queryLikeSelection2 = "%" + query.textQuery + "%"; String queryLikeSelection2 = "%" + query.textQuery + "%";
String waypointSelection =
WaypointsColumns.NAME + " LIKE ? OR " +
WaypointsColumns.DESCRIPTION + " LIKE ? OR " +
WaypointsColumns.CATEGORY + " LIKE ?";
String order = WaypointsColumns._ID + " DESC"; // Favor recent waypoints.
String[] waypointSelectionArgs = new String[] { String[] waypointSelectionArgs = new String[] {
queryLikeSelection2, queryLikeSelection2,
queryLikeSelection2, queryLikeSelection2,
queryLikeSelection2 }; queryLikeSelection2 };
Cursor waypointsCursor = providerUtils.getWaypointsCursor(waypointSelection, waypointSelectionArgs, order, Cursor waypointsCursor = providerUtils.getWaypointsCursor(
WAYPOINT_SELECTION_QUERY, waypointSelectionArgs, WAYPOINT_SELECTION_ORDER,
MAX_SCORED_WAYPOINTS); MAX_SCORED_WAYPOINTS);
if (waypointsCursor != null) { if (waypointsCursor != null) {
try { try {
@@ -231,9 +241,7 @@ public class SearchEngine {
TripStatistics statistics = track.getStatistics(); TripStatistics statistics = track.getStatistics();
// TODO: Also boost for proximity to the currently-centered position on the map. // TODO: Also boost for proximity to the currently-centered position on the map.
double meanLatitude = (statistics.getTop() + statistics.getBottom()) / 2000000.0; score *= getDistanceBoost(query, statistics.getMeanLatitude(), statistics.getMeanLongitude());
double meanLongitude = (statistics.getRight() + statistics.getLeft()) / 2000000.0;
score *= getDistanceBoost(query, meanLatitude, meanLongitude);
long meanTimestamp = (statistics.getStartTime() + statistics.getStopTime()) / 2L; long meanTimestamp = (statistics.getStartTime() + statistics.getStopTime()) / 2L;
score *= getTimeBoost(query, meanTimestamp); score *= getTimeBoost(query, meanTimestamp);
@@ -233,35 +233,75 @@ public class TripStatistics implements Parcelable {
} }
/** /**
* Returns the leftmost position (lowest longitude) of the track, in signed * Returns the leftmost position (lowest longitude) of the track, in signed degrees.
* decimal degrees. */
public double getLeftDegrees() {
return longitudeExtremities.getMin();
}
/**
* Returns the leftmost position (lowest longitude) of the track, in signed millions of degrees.
*/ */
public int getLeft() { public int getLeft() {
return (int) (longitudeExtremities.getMin() * 1E6); return (int) (longitudeExtremities.getMin() * 1E6);
} }
/** /**
* Returns the rightmost position (highest longitude) of the track, in signed * Returns the rightmost position (highest longitude) of the track, in signed degrees.
* decimal degrees. */
public double getRightDegrees() {
return longitudeExtremities.getMax();
}
/**
* Returns the rightmost position (highest longitude) of the track, in signed millions of degrees.
*/ */
public int getRight() { public int getRight() {
return (int) (longitudeExtremities.getMax() * 1E6); return (int) (longitudeExtremities.getMax() * 1E6);
} }
/** /**
* Returns the bottommost position (lowest latitude) of the track, in meters. * Returns the bottommost position (lowest latitude) of the track, in signed degrees.
*/
public double getBottomDegrees() {
return latitudeExtremities.getMin();
}
/**
* Returns the bottommost position (lowest latitude) of the track, in signed millions of degrees.
*/ */
public int getBottom() { public int getBottom() {
return (int) (latitudeExtremities.getMin() * 1E6); return (int) (latitudeExtremities.getMin() * 1E6);
} }
/** /**
* Returns the topmost position (highest latitude) of the track, in meters. * Returns the topmost position (highest latitude) of the track, in signed degrees.
*/
public double getTopDegrees() {
return latitudeExtremities.getMax();
}
/**
* Returns the topmost position (highest latitude) of the track, in signed millions of degrees.
*/ */
public int getTop() { public int getTop() {
return (int) (latitudeExtremities.getMax() * 1E6); return (int) (latitudeExtremities.getMax() * 1E6);
} }
/**
* Returns the mean position (center latitude) of the track, in signed degrees.
*/
public double getMeanLatitude() {
return (getBottomDegrees() + getTopDegrees()) / 2.0;
}
/**
* Returns the mean position (center longitude) of the track, in signed degrees.
*/
public double getMeanLongitude() {
return (getLeftDegrees() + getRightDegrees()) / 2.0;
}
/** /**
* Gets the minimum elevation seen on this trip. This is calculated from the * Gets the minimum elevation seen on this trip. This is calculated from the
* smoothed elevation so this can actually be more than the current elevation. * smoothed elevation so this can actually be more than the current elevation.