diff --git a/MyTracks/src/com/google/android/apps/mytracks/SearchActivity.java b/MyTracks/src/com/google/android/apps/mytracks/SearchActivity.java index 8db8453c6..80d5fa920 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/SearchActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/SearchActivity.java @@ -106,7 +106,7 @@ public class SearchActivity extends ListActivity { } private void doSearch(SearchQuery query) { - SortedSet scoredResults = engine.doSearch(query); + SortedSet scoredResults = engine.search(query); final List> displayResults = prepareResultsforDisplay(scoredResults); diff --git a/MyTracks/src/com/google/android/apps/mytracks/content/SearchEngine.java b/MyTracks/src/com/google/android/apps/mytracks/content/SearchEngine.java index 3f4957356..dcdbe1a00 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/content/SearchEngine.java +++ b/MyTracks/src/com/google/android/apps/mytracks/content/SearchEngine.java @@ -34,6 +34,24 @@ import java.util.TreeSet; */ 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. */ private static final double TRACK_CATEGORY_PROMOTION = 2.0; @@ -122,7 +140,7 @@ public class SearchEngine { * @param query the query to execute * @return a set of results, sorted according to their score */ - public SortedSet doSearch(SearchQuery query) { + public SortedSet search(SearchQuery query) { ArrayList tracks = new ArrayList(); ArrayList waypoints = new ArrayList(); TreeSet scoredResults = new TreeSet(SCORED_RESULT_COMPARATOR); @@ -144,17 +162,13 @@ public class SearchEngine { */ private void retrieveTracks(SearchQuery query, ArrayList tracks) { 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[] { queryLikeSelection, queryLikeSelection, queryLikeSelection }; - Cursor tracksCursor = providerUtils.getTracksCursor(trackSelection, trackSelectionArgs, order); + Cursor tracksCursor = providerUtils.getTracksCursor( + TRACK_SELECTION_QUERY, trackSelectionArgs, TRACK_SELECTION_ORDER); if (tracksCursor != null) { try { tracks.ensureCapacity(tracksCursor.getCount()); @@ -176,16 +190,12 @@ public class SearchEngine { */ private void retrieveWaypoints(SearchQuery query, ArrayList waypoints) { 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[] { queryLikeSelection2, queryLikeSelection2, queryLikeSelection2 }; - Cursor waypointsCursor = providerUtils.getWaypointsCursor(waypointSelection, waypointSelectionArgs, order, + Cursor waypointsCursor = providerUtils.getWaypointsCursor( + WAYPOINT_SELECTION_QUERY, waypointSelectionArgs, WAYPOINT_SELECTION_ORDER, MAX_SCORED_WAYPOINTS); if (waypointsCursor != null) { try { @@ -231,9 +241,7 @@ public class SearchEngine { TripStatistics statistics = track.getStatistics(); // TODO: Also boost for proximity to the currently-centered position on the map. - double meanLatitude = (statistics.getTop() + statistics.getBottom()) / 2000000.0; - double meanLongitude = (statistics.getRight() + statistics.getLeft()) / 2000000.0; - score *= getDistanceBoost(query, meanLatitude, meanLongitude); + score *= getDistanceBoost(query, statistics.getMeanLatitude(), statistics.getMeanLongitude()); long meanTimestamp = (statistics.getStartTime() + statistics.getStopTime()) / 2L; score *= getTimeBoost(query, meanTimestamp); diff --git a/MyTracksLib/src/com/google/android/apps/mytracks/stats/TripStatistics.java b/MyTracksLib/src/com/google/android/apps/mytracks/stats/TripStatistics.java index 09508bf41..dd318095e 100644 --- a/MyTracksLib/src/com/google/android/apps/mytracks/stats/TripStatistics.java +++ b/MyTracksLib/src/com/google/android/apps/mytracks/stats/TripStatistics.java @@ -1,12 +1,12 @@ /* * Copyright 2010 Google Inc. - * + * * 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 @@ -26,7 +26,7 @@ import android.os.Parcelable; * * @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 @@ -144,7 +144,7 @@ public class TripStatistics implements Parcelable { /** * Gets the time that this track started. - * + * * @return The number of milliseconds since epoch to the time when this track * started */ @@ -154,7 +154,7 @@ public class TripStatistics implements Parcelable { /** * Gets the time that this track stopped. - * + * * @return The number of milliseconds since epoch to the time when this track * stopped */ @@ -176,7 +176,7 @@ public class TripStatistics implements Parcelable { /** * Gets the total distance the user traveled. - * + * * @return The total distance traveled in meters */ public double getTotalDistance() { @@ -197,7 +197,7 @@ public class TripStatistics implements Parcelable { /** * Gets the the average speed the user traveled when they were actively * moving. - * + * * @return The average moving speed in m/s */ public double getAverageMovingSpeed() { @@ -206,7 +206,7 @@ public class TripStatistics implements Parcelable { /** * Gets the the maximum speed for this track. - * + * * @return The maximum speed in m/s */ public double getMaxSpeed() { @@ -215,7 +215,7 @@ public class TripStatistics implements Parcelable { /** * Gets the moving time. - * + * * @return The total number of milliseconds the user was moving */ 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 * of all positive differences in the smoothed elevation. - * + * * @return The elevation gain in meters for this trip */ public double getTotalElevationGain() { @@ -233,39 +233,79 @@ public class TripStatistics implements Parcelable { } /** - * Returns the leftmost position (lowest longitude) of the track, in signed - * decimal degrees. + * Returns the leftmost position (lowest longitude) of the track, in signed degrees. + */ + public double getLeftDegrees() { + return longitudeExtremities.getMin(); + } + + /** + * Returns the leftmost position (lowest longitude) of the track, in signed millions of degrees. */ public int getLeft() { return (int) (longitudeExtremities.getMin() * 1E6); } /** - * Returns the rightmost position (highest longitude) of the track, in signed - * decimal degrees. + * Returns the rightmost position (highest longitude) of the track, in signed degrees. + */ + public double getRightDegrees() { + return longitudeExtremities.getMax(); + } + + /** + * Returns the rightmost position (highest longitude) of the track, in signed millions of degrees. */ public int getRight() { 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() { 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() { 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 * smoothed elevation so this can actually be more than the current elevation. - * + * * @return The smallest elevation reading for this trip in meters */ 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 * smoothed elevation so this can actually be less than the current elevation. - * + * * @return The largest elevation reading for this trip in meters */ public double getMaxElevation() { @@ -284,7 +324,7 @@ public class TripStatistics implements Parcelable { /** * Gets the maximum grade for this trip. - * + * * @return The maximum grade for this trip as a fraction */ public double getMaxGrade() { @@ -293,7 +333,7 @@ public class TripStatistics implements Parcelable { /** * Gets the minimum grade for this trip. - * + * * @return The minimum grade for this trip as a fraction */ public double getMinGrade() { @@ -443,7 +483,7 @@ public class TripStatistics implements Parcelable { void addMovingTime(long time) { movingTime += time; } - + /** * Accounts for a new latitude value for the bounding box. *