SearchRecentSuggestionsProvider


public class SearchRecentSuggestionsProvider
extends ContentProvider

java.lang.Object
   ↳ android.content.ContentProvider
     ↳ android.content.SearchRecentSuggestionsProvider


This superclass can be used to create a simple search suggestions provider for your application. It creates suggestions (as the user types) based on recent queries and/or recent views.

In order to use this class, you must do the following.

  • Implement and test query search, as described in SearchManager. (This provider will send any suggested queries via the standard ACTION_SEARCH Intent, which you'll already support once you have implemented and tested basic searchability.)
  • Create a Content Provider within your application by extending SearchRecentSuggestionsProvider. The class you create will be very simple - typically, it will have only a constructor. But the constructor has a very important responsibility: When it calls setupSuggestions(java.lang.String, int), it configures the provider to match the requirements of your searchable activity.
  • Create a manifest entry describing your provider. Typically this would be as simple as adding the following lines:
         <!-- Content provider for search suggestions -->
         <provider android:name="YourSuggestionProviderClass"
                   android:authorities="your.suggestion.authority" />
  • Please note that you do not instantiate this content provider directly from within your code. This is done automatically by the system Content Resolver, when the search dialog looks for suggestions.
  • In order for the Content Resolver to do this, you must update your searchable activity's XML configuration file with information about your content provider. The following additions are usually sufficient:
         android:searchSuggestAuthority="your.suggestion.authority"
         android:searchSuggestSelection=" ? "
  • In your searchable activities, capture any user-generated queries and record them for future searches by calling SearchRecentSuggestions.saveRecentQuery().

Developer Guides

For information about using search suggestions in your application, read the Search developer guide.

Summary

Constants

int DATABASE_MODE_2LINES

This mode bit configures the database to include a 2nd annotation line with each entry.

int DATABASE_MODE_QUERIES

This mode bit configures the database to record recent queries.

Inherited constants

int TRIM_MEMORY_BACKGROUND

Level for onTrimMemory(int): the process has gone on to the LRU list.

int TRIM_MEMORY_COMPLETE

This constant was deprecated in API level 35. Apps are not notified of this level since API level 34

int TRIM_MEMORY_MODERATE

This constant was deprecated in API level 35. Apps are not notified of this level since API level 34

int TRIM_MEMORY_RUNNING_CRITICAL

This constant was deprecated in API level 35. Apps are not notified of this level since API level 34

int TRIM_MEMORY_RUNNING_LOW

This constant was deprecated in API level 35. Apps are not notified of this level since API level 34

int TRIM_MEMORY_RUNNING_MODERATE

This constant was deprecated in API level 35. Apps are not notified of this level since API level 34

int TRIM_MEMORY_UI_HIDDEN

Level for onTrimMemory(int): the process had been showing a user interface, and is no longer doing so.

Public constructors

SearchRecentSuggestionsProvider()

Public methods

int delete(Uri uri, String selection, String[] selectionArgs)

This method is provided for use by the ContentResolver.

String getType(Uri uri)

This method is provided for use by the ContentResolver.

Uri insert(Uri uri, ContentValues values)

This method is provided for use by the ContentResolver.

boolean onCreate()

This method is provided for use by the ContentResolver.

Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

This method is provided for use by the ContentResolver.

int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)

This method is provided for use by the ContentResolver.

Protected methods

void setupSuggestions(String authority, int mode)

In order to use this class, you must extend it, and call this setup function from your constructor.

Inherited methods

ContentProviderResult[] applyBatch(String authority, ArrayList<ContentProviderOperation> operations)

Override this to handle requests to perform a batch of operations, or the default implementation will iterate over the operations and call ContentProviderOperation.apply on each of them.

ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
void attachInfo(Context context, ProviderInfo info)

After being instantiated, this is called to tell the content provider about itself.

int bulkInsert(Uri uri, ContentValues[] values)

Override this to handle requests to insert a set of new rows, or the default implementation will iterate over the values and call insert(Uri, ContentValues) on each of them.

Bundle call(String authority, String method, String arg, Bundle extras)

Call a provider-defined method.

Bundle call(String method, String arg, Bundle extras)
Uri canonicalize(Uri url)

Implement this to support canonicalization of URIs that refer to your content provider.

final ContentProvider.CallingIdentity clearCallingIdentity()

Reset the identity of the incoming IPC on the current thread.

abstract int delete(Uri uri, String selection, String[] selectionArgs)

Implement this to handle requests to delete one or more rows.

int delete(Uri uri, Bundle extras)

Implement this to handle requests to delete one or more rows.

void dump(FileDescriptor fd, PrintWriter writer, String[] args)

Print the Provider's state into the given stream.

final AttributionSource getCallingAttributionSource()

Gets the attribution source of the calling app.

final String getCallingAttributionTag()

Return the attribution tag of the caller that initiated the request being processed on the current thread.

final String getCallingPackage()

Return the package name of the caller that initiated the request being processed on the current thread.

final String getCallingPackageUnchecked()

Return the package name of the caller that initiated the request being processed on the current thread.

final Context getContext()

Retrieves the Context this provider is running in.

final PathPermission[] getPathPermissions()

Return the path-based permissions required for read and/or write access to this content provider.

final String getReadPermission()

Return the name of the permission required for read-only access to this content provider.

String[] getStreamTypes(Uri uri, String mimeTypeFilter)

Called by a client to determine the types of data streams that this content provider supports for the given URI.

abstract String getType(Uri uri)

Implement this to handle requests for the MIME type of the data at the given URI.

String getTypeAnonymous(Uri uri)

Implement this to handle requests for MIME type of URIs, that does not need to reveal any internal information which should be protected by any permission.

final String getWritePermission()

Return the name of the permission required for read/write access to this content provider.

Uri insert(Uri uri, ContentValues values, Bundle extras)

Implement this to handle requests to insert a new row.

abstract Uri insert(Uri uri, ContentValues values)

Implement this to handle requests to insert a new row.

boolean isTemporary()

Returns true if this instance is a temporary content provider.

void onCallingPackageChanged()

Called whenever the value of getCallingPackage() changes, giving the provider an opportunity to invalidate any security related caching it may be performing.

void onConfigurationChanged(Configuration newConfig)

Called by the system when the device configuration changes while your component is running. This method is always called on the application main thread, and must not perform lengthy operations.

abstract boolean onCreate()

Implement this to initialize your content provider on startup.

void onLowMemory()

This is called when the overall system is running low on memory, and actively running processes should trim their memory usage. This method is always called on the application main thread, and must not perform lengthy operations.

void onTrimMemory(int level)

Called when the operating system has determined that it is a good time for a process to trim unneeded memory from its process.

AssetFileDescriptor openAssetFile(Uri uri, String mode, CancellationSignal signal)

This is like openFile(Uri, String), but can be implemented by providers that need to be able to return sub-sections of files, often assets inside of their .apk.

AssetFileDescriptor openAssetFile(Uri uri, String mode)

This is like openFile(Uri, String), but can be implemented by providers that need to be able to return sub-sections of files, often assets inside of their .apk.

ParcelFileDescriptor openFile(Uri uri, String mode, CancellationSignal signal)

Override this to handle requests to open a file blob.

ParcelFileDescriptor openFile(Uri uri, String mode)

Override this to handle requests to open a file blob.

final ParcelFileDescriptor openFileHelper(Uri uri, String mode)

Convenience for subclasses that wish to implement openFile(Uri, String) by looking up a column named "_data" at the given URI.

<T> ParcelFileDescriptor openPipeHelper(Uri uri, String mimeType, Bundle opts, T args, PipeDataWriter<T> func)

A helper function for implementing openTypedAssetFile(Uri, String, Bundle), for creating a data pipe and background thread allowing you to stream generated data back to the client.

AssetFileDescriptor openTypedAssetFile(Uri uri, String mimeTypeFilter, Bundle opts, CancellationSignal signal)

Called by a client to open a read-only stream containing data of a particular MIME type.

AssetFileDescriptor openTypedAssetFile(Uri uri, String mimeTypeFilter, Bundle opts)

Called by a client to open a read-only stream containing data of a particular MIME type.

Cursor query(Uri uri, String[] projection, Bundle queryArgs, CancellationSignal cancellationSignal)

Implement this to handle query requests where the arguments are packed into a Bundle.

Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal)

Implement this to handle query requests from clients with support for cancellation.

abstract Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

Implement this to handle query requests from clients.

boolean refresh(Uri uri, Bundle extras, CancellationSignal cancellationSignal)

Implement this to support refresh of content identified by uri.

final Context requireContext()

Retrieves a Non-Nullable Context this provider is running in, this is intended to be called after onCreate().

final void restoreCallingIdentity(ContentProvider.CallingIdentity identity)

Restore the identity of the incoming IPC on the current thread back to a previously identity that was returned by clearCallingIdentity().

final void setPathPermissions(PathPermission[] permissions)

Change the path-based permission required to read and/or write data in the content provider.

final void setReadPermission(String permission)

Change the permission required to read data from the content provider.

final void setWritePermission(String permission)

Change the permission required to read and write data in the content provider.

void shutdown()

Implement this to shut down the ContentProvider instance.

Uri uncanonicalize(Uri url)

Remove canonicalization from canonical URIs previously returned by canonicalize(Uri).

int update(Uri uri, ContentValues values, Bundle extras)

Implement this to handle requests to update one or more rows.

abstract int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)

Implement this to handle requests to update one or more rows.

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

abstract void onTrimMemory(int level)

Called when the operating system has determined that it is a good time for a process to trim unneeded memory from its process.

abstract void onConfigurationChanged(Configuration newConfig)

Called by the system when the device configuration changes while your component is running.

abstract void onLowMemory()

This method was deprecated in API level 35. Since API level 14 this is superseded by ComponentCallbacks2.onTrimMemory. Since API level 34 this is never called. If you're overriding ComponentCallbacks2#onTrimMemory and your minSdkVersion is greater than API 14, you can provide an empty implementation for this method.

Constants

DATABASE_MODE_2LINES

Added in API level 1
public static final int DATABASE_MODE_2LINES

This mode bit configures the database to include a 2nd annotation line with each entry. optional

Constant Value: 2 (0x00000002)

DATABASE_MODE_QUERIES

Added in API level 1
public static final int DATABASE_MODE_QUERIES

This mode bit configures the database to record recent queries. required

Constant Value: 1 (0x00000001)

Public constructors

SearchRecentSuggestionsProvider

public SearchRecentSuggestionsProvider ()

Public methods

delete

Added in API level 1
public int delete (Uri uri, 
                String selection, 
                String[] selectionArgs)

This method is provided for use by the ContentResolver. Do not override, or directly call from your own code.

Parameters
uri Uri: The full URI to query, including a row ID (if a specific record is requested). This value cannot be null.

selection String: An optional restriction to apply to rows when deleting. This value may be null.

selectionArgs String: This value may be null.

Returns
int The number of rows affected.

getType

Added in API level 1
public String getType (Uri uri)

This method is provided for use by the ContentResolver. Do not override, or directly call from your own code.

Parameters
uri Uri: the URI to query. This value cannot be null.

Returns
String a MIME type string, or null if there is no type.

insert

Added in API level 1
public Uri insert (Uri uri, 
                ContentValues values)

This method is provided for use by the ContentResolver. Do not override, or directly call from your own code.

Parameters
uri Uri: The content:// URI of the insertion request. This value cannot be null.

values ContentValues: A set of column_name/value pairs to add to the database. This value may be null.

Returns
Uri The URI for the newly inserted item. This value may be null.

onCreate

Added in API level 1
public boolean onCreate ()

This method is provided for use by the ContentResolver. Do not override, or directly call from your own code.

Returns
boolean true if the provider was successfully loaded, false otherwise

query

Added in API level 1
public Cursor query (Uri uri, 
                String[] projection, 
                String selection, 
                String[] selectionArgs, 
                String sortOrder)

This method is provided for use by the ContentResolver. Do not override, or directly call from your own code.

Parameters
uri Uri: The URI to query. This will be the full URI sent by the client; if the client is requesting a specific record, the URI will end in a record number that the implementation should parse and add to a WHERE or HAVING clause, specifying that _id value. This value cannot be null.

projection String: The list of columns to put into the cursor. If null all columns are included.

selection String: A selection criteria to apply when filtering rows. If null then all rows are included.

selectionArgs String: You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings. This value may be null.

sortOrder String: How the rows in the cursor should be sorted. If null then the provider is free to define the sort order.

Returns
Cursor a Cursor or null.

update

Added in API level 1
public int update (Uri uri, 
                ContentValues values, 
                String selection, 
                String[] selectionArgs)

This method is provided for use by the ContentResolver. Do not override, or directly call from your own code.

Parameters
uri Uri: The URI to query. This can potentially have a record ID if this is an update request for a specific record. This value cannot be null.

values ContentValues: A set of column_name/value pairs to update in the database. This value may be null.

selection String: An optional filter to match rows to update. This value may be null.

selectionArgs String: This value may be null.

Returns
int the number of rows affected.

Protected methods

setupSuggestions

Added in API level 1
protected void setupSuggestions (String authority, 
                int mode)

In order to use this class, you must extend it, and call this setup function from your constructor. In your application or activities, you must provide the same values when you create the SearchRecentSuggestions helper.

Parameters
authority String: This must match the authority that you've declared in your manifest.

mode int: You can use mode flags here to determine certain functional aspects of your database. Note, this value should not change from run to run, because when it does change, your suggestions database may be wiped.