From 8000d51caf0d02fd690dab8186cda5c46c88f8a6 Mon Sep 17 00:00:00 2001 From: Rodrigo Damazio Date: Fri, 6 Jan 2012 04:39:07 -0200 Subject: [PATCH] Adding search suggestions --- MyTracks/AndroidManifest.xml | 7 +++ MyTracks/res/xml/search.xml | 7 +-- .../android/apps/mytracks/SearchActivity.java | 12 ++++- .../content/SearchEngineProvider.java | 44 +++++++++++++++++++ 4 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 MyTracks/src/com/google/android/apps/mytracks/content/SearchEngineProvider.java diff --git a/MyTracks/AndroidManifest.xml b/MyTracks/AndroidManifest.xml index ae51af768..b009e2245 100755 --- a/MyTracks/AndroidManifest.xml +++ b/MyTracks/AndroidManifest.xml @@ -112,12 +112,19 @@ limitations under the License. + + + + + diff --git a/MyTracks/res/xml/search.xml b/MyTracks/res/xml/search.xml index 22c82610d..46ce021ed 100644 --- a/MyTracks/res/xml/search.xml +++ b/MyTracks/res/xml/search.xml @@ -5,6 +5,7 @@ android:label="@string/my_tracks_app_name" android:searchSettingsDescription="@string/search_global_description" android:voicePromptText="@string/search_voice_prompt" - android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"> - - \ No newline at end of file + android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" + android:searchSuggestAuthority="com.google.android.maps.mytracks.search" + android:searchSuggestThreshold="2" + android:searchSuggestSelection=" ? " /> \ No newline at end of file diff --git a/MyTracks/src/com/google/android/apps/mytracks/SearchActivity.java b/MyTracks/src/com/google/android/apps/mytracks/SearchActivity.java index 59df27b4a..d2e1eb15e 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/SearchActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/SearchActivity.java @@ -18,12 +18,13 @@ package com.google.android.apps.mytracks; import static com.google.android.apps.mytracks.Constants.TAG; import com.google.android.apps.mytracks.content.SearchEngine; +import com.google.android.apps.mytracks.content.SearchEngine.ScoredResult; +import com.google.android.apps.mytracks.content.SearchEngine.SearchQuery; +import com.google.android.apps.mytracks.content.SearchEngineProvider; import com.google.android.apps.mytracks.content.Track; import com.google.android.apps.mytracks.content.TracksColumns; import com.google.android.apps.mytracks.content.Waypoint; import com.google.android.apps.mytracks.content.WaypointsColumns; -import com.google.android.apps.mytracks.content.SearchEngine.ScoredResult; -import com.google.android.apps.mytracks.content.SearchEngine.SearchQuery; import com.google.android.maps.mytracks.R; import android.app.ListActivity; @@ -34,6 +35,7 @@ import android.location.Location; import android.location.LocationManager; import android.net.Uri; import android.os.Bundle; +import android.provider.SearchRecentSuggestions; import android.util.Log; import android.view.View; import android.widget.ListView; @@ -60,11 +62,14 @@ public class SearchActivity extends ListActivity { private SearchEngine engine; private LocationManager locationManager; + private SearchRecentSuggestions suggestions; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); engine = new SearchEngine(this); + suggestions = SearchEngineProvider.newHelper(this); locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); handleIntent(getIntent()); @@ -116,6 +121,9 @@ public class SearchActivity extends ListActivity { showSearchResults(displayResults); } }); + + // Save the query as a suggestion for the future. + suggestions.saveRecentQuery(query.textQuery, null); } private List> prepareResultsforDisplay( diff --git a/MyTracks/src/com/google/android/apps/mytracks/content/SearchEngineProvider.java b/MyTracks/src/com/google/android/apps/mytracks/content/SearchEngineProvider.java new file mode 100644 index 000000000..ae7c64fe1 --- /dev/null +++ b/MyTracks/src/com/google/android/apps/mytracks/content/SearchEngineProvider.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012 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 + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.android.apps.mytracks.content; + +import android.content.Context; +import android.content.SearchRecentSuggestionsProvider; +import android.provider.SearchRecentSuggestions; + +/** + * Content provider for search suggestions. + * + * @author Rodrigo Damazio + */ +public class SearchEngineProvider extends SearchRecentSuggestionsProvider { + + private static final String AUTHORITY = "com.google.android.maps.mytracks.search"; + private static final int MODE = DATABASE_MODE_QUERIES; + + public SearchEngineProvider() { + setupSuggestions(AUTHORITY, MODE); + } + + // TODO: Also add suggestions from the database. + + /** + * Creates and returns a helper for adding recent queries or clearing the recent query history. + */ + public static SearchRecentSuggestions newHelper(Context context) { + return new SearchRecentSuggestions(context, AUTHORITY, MODE); + } +}