Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SearchRecentSuggestionsProvider query limit

I'm working with search suggestion framework and here is the case. I get some query string and make query from content resolver, but problem is that I can't limit the results. Found several solutions but they dont work for me.

My content provider is extending SearchRecentSuggestionsProvider and declared in manifest, here query uri Uri URI = Uri.parse("content://" + AUTHORITY + "/search_suggest_query");

sol 1: adding query parameter to uri

SearchSuggestionProvider.URI.buildUpon().appendQueryParameter(SearchManager.SUGGEST_PARAMETER_LIMIT, String.valueOf(5)).build()

sol 2: adding limit in sortOrder query parameter

getContentResolver().query(URI, null, "?", new String[]{searchQuery}, "_id asc limit 5");

In both cases query returns all rows from search suggestions. Does anyone know how to limit such a query?

like image 408
greenfrvr Avatar asked Jun 24 '15 07:06

greenfrvr


2 Answers

Actually, we were searching in the wrong place. After all this investigations, finally I found the trick.

On the results screen (Where we save the query to the search SearchRecentSuggestions) we call this method

SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
                    SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE);
suggestions.saveRecentQuery(query, null);

SearchRecentSuggestions was the responsible to save this query to the content provider.

This class has a protected method called truncateHistory() with this documentation

Reduces the length of the history table, to prevent it from growing too large.

So the solution is simply, create a custom class, override this method, and call the super implementation with the required limit.

Here is the example

public class SearchRecentSuggestionsLimited extends SearchRecentSuggestions {

    private int limit;

    public SearchRecentSuggestionsLimited(Context context, String authority, int mode, int limit) {
        super(context, authority, mode);
        this.limit = limit;
    }

    @Override
    protected void truncateHistory(ContentResolver cr, int maxEntries) {
        super.truncateHistory(cr, limit);
    }
}
like image 66
Simon K. Gerges Avatar answered Nov 08 '22 19:11

Simon K. Gerges


Below is the query method in the base class SearchRecentSuggestionsProvider in the framework

/**
     * This method is provided for use by the ContentResolver.  Do not override, or directly
     * call from your own code.
     */
    // TODO: Confirm no injection attacks here, or rewrite.
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 
            String sortOrder) {

Potentially you can override it and implement the limiting - but as you can see the comment above it clear states "do not override". In the existing method no limiting is enforced.

However I see in some of the other answers on stack-overflow and other sites that people do override it.

Example : Use SearchRecentSuggestionsProvider with some predefined terms?

Based on this I think you can try it out - override the query method and add your own implementation which supports limiting the results. Parse the SearchManager.SUGGEST_PARAMETER_LIMIT that you have used in the query method and use it to limit the results returned.

like image 34
RocketRandom Avatar answered Nov 08 '22 20:11

RocketRandom