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);
@@ -1,12 +1,12 @@
/* /*
* Copyright 2010 Google Inc. * Copyright 2010 Google Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not * 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 * use this file except in compliance with the License. You may obtain a copy of
* the License at * the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -26,7 +26,7 @@ import android.os.Parcelable;
* *
* @author Rodrigo Damazio * @author Rodrigo Damazio
*/ */
public class TripStatistics implements Parcelable { public class TripStatistics implements Parcelable {
/** /**
* The start time for the trip. This is system time which might not match gps * The start time for the trip. This is system time which might not match gps
@@ -144,7 +144,7 @@ public class TripStatistics implements Parcelable {
/** /**
* Gets the time that this track started. * Gets the time that this track started.
* *
* @return The number of milliseconds since epoch to the time when this track * @return The number of milliseconds since epoch to the time when this track
* started * started
*/ */
@@ -154,7 +154,7 @@ public class TripStatistics implements Parcelable {
/** /**
* Gets the time that this track stopped. * Gets the time that this track stopped.
* *
* @return The number of milliseconds since epoch to the time when this track * @return The number of milliseconds since epoch to the time when this track
* stopped * stopped
*/ */
@@ -176,7 +176,7 @@ public class TripStatistics implements Parcelable {
/** /**
* Gets the total distance the user traveled. * Gets the total distance the user traveled.
* *
* @return The total distance traveled in meters * @return The total distance traveled in meters
*/ */
public double getTotalDistance() { public double getTotalDistance() {
@@ -197,7 +197,7 @@ public class TripStatistics implements Parcelable {
/** /**
* Gets the the average speed the user traveled when they were actively * Gets the the average speed the user traveled when they were actively
* moving. * moving.
* *
* @return The average moving speed in m/s * @return The average moving speed in m/s
*/ */
public double getAverageMovingSpeed() { public double getAverageMovingSpeed() {
@@ -206,7 +206,7 @@ public class TripStatistics implements Parcelable {
/** /**
* Gets the the maximum speed for this track. * Gets the the maximum speed for this track.
* *
* @return The maximum speed in m/s * @return The maximum speed in m/s
*/ */
public double getMaxSpeed() { public double getMaxSpeed() {
@@ -215,7 +215,7 @@ public class TripStatistics implements Parcelable {
/** /**
* Gets the moving time. * Gets the moving time.
* *
* @return The total number of milliseconds the user was moving * @return The total number of milliseconds the user was moving
*/ */
public long getMovingTime() { public long getMovingTime() {
@@ -225,7 +225,7 @@ public class TripStatistics implements Parcelable {
/** /**
* Gets the total elevation gain for this trip. This is calculated as the sum * Gets the total elevation gain for this trip. This is calculated as the sum
* of all positive differences in the smoothed elevation. * of all positive differences in the smoothed elevation.
* *
* @return The elevation gain in meters for this trip * @return The elevation gain in meters for this trip
*/ */
public double getTotalElevationGain() { public double getTotalElevationGain() {
@@ -233,39 +233,79 @@ 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.
* *
* @return The smallest elevation reading for this trip in meters * @return The smallest elevation reading for this trip in meters
*/ */
public double getMinElevation() { public double getMinElevation() {
@@ -275,7 +315,7 @@ public class TripStatistics implements Parcelable {
/** /**
* Gets the maximum elevation seen on this trip. This is calculated from the * Gets the maximum elevation seen on this trip. This is calculated from the
* smoothed elevation so this can actually be less than the current elevation. * smoothed elevation so this can actually be less than the current elevation.
* *
* @return The largest elevation reading for this trip in meters * @return The largest elevation reading for this trip in meters
*/ */
public double getMaxElevation() { public double getMaxElevation() {
@@ -284,7 +324,7 @@ public class TripStatistics implements Parcelable {
/** /**
* Gets the maximum grade for this trip. * Gets the maximum grade for this trip.
* *
* @return The maximum grade for this trip as a fraction * @return The maximum grade for this trip as a fraction
*/ */
public double getMaxGrade() { public double getMaxGrade() {
@@ -293,7 +333,7 @@ public class TripStatistics implements Parcelable {
/** /**
* Gets the minimum grade for this trip. * Gets the minimum grade for this trip.
* *
* @return The minimum grade for this trip as a fraction * @return The minimum grade for this trip as a fraction
*/ */
public double getMinGrade() { public double getMinGrade() {
@@ -443,7 +483,7 @@ public class TripStatistics implements Parcelable {
void addMovingTime(long time) { void addMovingTime(long time) {
movingTime += time; movingTime += time;
} }
/** /**
* Accounts for a new latitude value for the bounding box. * Accounts for a new latitude value for the bounding box.
* *