Multiple SearchRecentSuggestionsProvider classes in Android app possible?

2k views Asked by At

I have two different Search activities implemented with SearchManager in my app. One is the default searchable for the whole app and the other is used for only one activity. These two searches perform lookups of different data, and it doesn't make sense to share the search history across the two different searches. The two different searchables are working fine functionally within the app, my issue is with the recent search histories.

The problem is that there are two different SearchRecentSuggestionsProvider classes defined within the app, but any search made in one shows up in both searches' recent search suggestions history. I've read through the SearchManager topic in the Dev Guide but don't see any info about multiple suggestions providers.

Here are the two providers in the AndroidManifest.xml, in the node:

<provider android:name=".SearchOneRecentSuggestionsProvider"
 android:authorities="com.company.SearchOneRecentSuggestionsProvider"></provider>
<provider android:name=".SearchTwoRecentSuggestionsProvider"
    android:authorities="com.company.SearchTwoRecentSuggestionsProvider"></provider>

Here's the class defined in SearchOneRecentSuggestionsProvider.java:

package com.company;

import android.content.SearchRecentSuggestionsProvider;

public class SearchOneRecentSuggestionsProvider extends SearchRecentSuggestionsProvider {
     public final static String AUTHORITY = "com.company.SearchOneRecentSuggestionsProvider";
     public final static int MODE = DATABASE_MODE_QUERIES;

     public SearchOneRecentSuggestionsProvider() {
      setupSuggestions(AUTHORITY, MODE);
     }
    }

(The SearchTwoRecentSuggestionsProvider class looks exactly the same except for replacing One with Two and is in it's own SearchTwoRecentSuggestionsProvider.java file.)

Here's what is inside the search suggestions database (at /data/data/com.company/databases/suggestions.db):

# sqlite3 suggestions.db
SQLite version 3.5.9
Enter ".help" for instructions
sqlite> .tables
android_metadata  suggestions
sqlite> .schema suggestions
CREATE TABLE suggestions (_id INTEGER PRIMARY KEY,display1 TEXT UNIQUE ON CONFLICT REPLACE,query TEXT,date LONG);
sqlite> select * from suggestions;
1|searchone|searchone|1296177852444
2|searchtwo|searchtwo|1296177878744

There's no column for the SearchRecentSuggestionsProvider, so perhaps there's no way to separate the history for the two searches?

The end result of all this is that when you do a search in either one of the two different searches, you see both searchone and searchtwo in the recent search history suggestions. I'd like each to have their own history if possible. Hopefully I didn't just miss something silly! Thanks for taking the time to read this.

3

There are 3 answers

0
Marc Bernstein On BEST ANSWER

Looks like it's not possible at this time. For those of you searching SO for this same issue, see https://code.google.com/p/android/issues/detail?id=13501 for future updates from Google.

1
Dalmas On

I looked at the SearchRecentSuggestionsProvider and SearchRecentSuggestions source code, and clearly these classes were designed to use a common database among all the application, not regarding if you use multiple providers or not (so using multiple SearchRecentSuggestionsProvider in the same app is useless). I cannot find a way to use two different databases.

But I thought about a trick : add a character (like '1' for activity1 recent searches, and '2' for activity2 recent searches) at the beginning of each query when saving them with saveRecentQuery(). If you're in activity1, you will do saveRecentQuery('1' + query, ...).

Then override SearchRecentSuggestionsProvider.query(), here you can filter the rows you want to show depending on the value of the first character, '1' or '2' (you must also remove it from the string, of course). If you're on activity1 you want to keep only values starting with '1', ... . This way you can handle multiple suggestion lists.

However it's a trick, and I'm not 100% sure that it will work like this...

0
Melllvar On

I have made a small modification to the class that makes this possible - see https://gist.github.com/pokebyte/a08a67282a4f19ebbf52

Specifically, the change adds a new indexed column (authority) that restricts searches to a specific authority.