diff options
Diffstat (limited to 'main/src/cgeo/geocaching/search')
3 files changed, 66 insertions, 13 deletions
diff --git a/main/src/cgeo/geocaching/search/AutoCompleteAdapter.java b/main/src/cgeo/geocaching/search/AutoCompleteAdapter.java index 45559f4..21cf089 100644 --- a/main/src/cgeo/geocaching/search/AutoCompleteAdapter.java +++ b/main/src/cgeo/geocaching/search/AutoCompleteAdapter.java @@ -1,6 +1,7 @@ package cgeo.geocaching.search; import org.apache.commons.lang3.StringUtils; + import rx.functions.Func1; import android.content.Context; @@ -14,11 +15,11 @@ import android.widget.Filter; */ public class AutoCompleteAdapter extends ArrayAdapter<String> { - private final String[] EMPTY = new String[0]; + private final static String[] EMPTY = new String[0]; private String[] suggestions = EMPTY; private final Func1<String, String[]> suggestionFunction; - public AutoCompleteAdapter(Context context, int textViewResourceId, final Func1<String, String[]> suggestionFunction) { + public AutoCompleteAdapter(final Context context, final int textViewResourceId, final Func1<String, String[]> suggestionFunction) { super(context, textViewResourceId); this.suggestionFunction = suggestionFunction; } @@ -29,7 +30,7 @@ public class AutoCompleteAdapter extends ArrayAdapter<String> { } @Override - public String getItem(int index) { + public String getItem(final int index) { return suggestions[index]; } @@ -38,14 +39,14 @@ public class AutoCompleteAdapter extends ArrayAdapter<String> { return new Filter() { @Override - protected FilterResults performFiltering(CharSequence constraint) { - FilterResults filterResults = new FilterResults(); + protected FilterResults performFiltering(final CharSequence constraint) { + final FilterResults filterResults = new FilterResults(); if (constraint == null) { return filterResults; } - String trimmed = StringUtils.trim(constraint.toString()); + final String trimmed = StringUtils.trim(constraint.toString()); if (StringUtils.length(trimmed) >= 2) { - String[] newResults = suggestionFunction.call(trimmed); + final String[] newResults = suggestionFunction.call(trimmed); // Assign the data to the FilterResults, but do not yet store in the global member. // Otherwise we might invalidate the adapter and cause an IllegalStateException. @@ -56,7 +57,7 @@ public class AutoCompleteAdapter extends ArrayAdapter<String> { } @Override - protected void publishResults(CharSequence constraint, FilterResults filterResults) { + protected void publishResults(final CharSequence constraint, final FilterResults filterResults) { if (filterResults != null && filterResults.count > 0) { suggestions = (String[]) filterResults.values; notifyDataSetChanged(); diff --git a/main/src/cgeo/geocaching/search/SearchSuggestionCursor.java b/main/src/cgeo/geocaching/search/SearchSuggestionCursor.java new file mode 100644 index 0000000..350e23a --- /dev/null +++ b/main/src/cgeo/geocaching/search/SearchSuggestionCursor.java @@ -0,0 +1,46 @@ +package cgeo.geocaching.search; + +import cgeo.geocaching.Intents; +import cgeo.geocaching.enumerations.CacheType; + +import org.eclipse.jdt.annotation.NonNull; + +import android.app.SearchManager; +import android.database.MatrixCursor; +import android.provider.BaseColumns; + +/** + * Fixed fields cursor holding the necessary data for the search provider of the global search bar. + * + */ +public class SearchSuggestionCursor extends MatrixCursor { + + /** + * id of the row for callbacks after selection + */ + private int rowId = 0; + + public SearchSuggestionCursor() { + super(new String[] { + BaseColumns._ID, + SearchManager.SUGGEST_COLUMN_TEXT_1, + SearchManager.SUGGEST_COLUMN_TEXT_2, + SearchManager.SUGGEST_COLUMN_INTENT_ACTION, + SearchManager.SUGGEST_COLUMN_QUERY, + SearchManager.SUGGEST_COLUMN_ICON_1 }); + } + + public void addCache(@NonNull final String geocode, @NonNull final String name, final String type) { + final int icon = CacheType.getById(type).markerId; + addRow(new String[] { + String.valueOf(rowId), + name, + geocode, + Intents.ACTION_GEOCACHE, + geocode, + String.valueOf(icon) + }); + rowId++; + } + +} diff --git a/main/src/cgeo/geocaching/search/SuggestionProvider.java b/main/src/cgeo/geocaching/search/SuggestionProvider.java index c0a7728..f60a43e 100644 --- a/main/src/cgeo/geocaching/search/SuggestionProvider.java +++ b/main/src/cgeo/geocaching/search/SuggestionProvider.java @@ -1,6 +1,7 @@ package cgeo.geocaching.search; import cgeo.geocaching.DataStore; +import cgeo.geocaching.Geocache; import org.apache.commons.lang3.StringUtils; @@ -12,8 +13,6 @@ import android.net.Uri; public class SuggestionProvider extends ContentProvider { - private static Cursor lastCursor; - @Override public boolean onCreate() { return true; @@ -29,14 +28,21 @@ public class SuggestionProvider extends ContentProvider { final String searchTerm = uri.getLastPathSegment(); // can be empty when deleting the query if (StringUtils.equals(searchTerm, SearchManager.SUGGEST_URI_PATH_QUERY)) { - return lastCursor; + return getLastOpenedCaches(); } return getSuggestions(searchTerm); } + private static Cursor getLastOpenedCaches() { + final SearchSuggestionCursor resultCursor = new SearchSuggestionCursor(); + for (final Geocache geocache : DataStore.getLastOpenedCaches()) { + resultCursor.addCache(geocache.getGeocode(), geocache.getName(), geocache.getType().id); + } + return resultCursor; + } + private static Cursor getSuggestions(final String searchTerm) { - lastCursor = DataStore.findSuggestions(searchTerm); - return lastCursor; + return DataStore.findSuggestions(searchTerm); } @Override |
