Android Quick Search in Settings, androidquick
Welcome to join the group: 429664282
Android-6.0.1_r17 based on: f4b8ad6
There is a SearchIndexablesProvider in Android Settings, which provides Settings for quick retrieval. Through it, we can decide which system settings can be quickly searched, and those can be not retrieved.
Introduction to SearchIndexablesProvider
SearchIndexablesProvider is the Android standard API, which can be found in the SDK. The path is "android. provider. SearchIndexablesProvider ". It is a virtual base class that provides some abstract methods and common methods.
The abstract method is as follows:
Cursor queryXmlResources (String [])
Cursor queryRawData (String [])
Cursor queryNonIndexableKeys (String [])
The queryXmlResources method returns a Cursor, which contains all the xmlresources that can be indexed. the parameter of the method is a String array used to indicate the columns passed in the query.
The queryRawData method is similar to queryRawData. The only difference is that it returns all RawData that can be indexed.
QueryNonIndexableKeys returns all nonindexablekeys that can be disabled.
Search Data source: SearchIndexableResources
In Settings, all data resources available for retrieval are defined in the SearchIndexableResources class.
For example, all data available for retrieval on the Wifi settings page is provided through WifiSettings. java.
1 static { 2 3 sResMap.put(WifiSettings.class.getName(), 4 5 new SearchIndexableResource( 6 7 Ranking.getRankForClassName(WifiSettings.class.getName()), 8 9 NO_DATA_RES_ID,10 11 WifiSettings.class.getName(),12 13 R.drawable.ic_settings_wireless));
Special Data retrieval: Indexable
In Settings, a special type of data retrieval is provided, that is, the class that implements the Indexable interface.
Indexable is a retrieval resource defined in Settings. It returns the data available for retrieval through code, including SearchIndexableResource and SearchIndexRaw.
All classes that implement Indexable must provide a public static SEARCH_INDEX_DATA_PROVIDER variable for Settings to obtain the corresponding retrieval data using reflection.
Data Retrieval provider: SettingsSearchIndexablesProvider
In Settings, SettingsSearchIndexablesProvider implements SearchIndexablesProvider and provides data resources for retrieval. This data resource is defined in SearchIndexableResources in the form of static map, and is returned to the data requestor in the queryXmlResources method.
Settings does not provide any RawData and NonIndexableKey, so its queryRawData returns an empty Cursor.
Update Retrieval Database
The Index. update () method is called every time you open the Settings navigation page to update and retrieve data.
Step1. obtain all SearchIndexablesProvider.
Step2. Add the retrieved data to the cache: addIndexablesFromRemoteProvider
This operation is required for every SearchIndexablesProvider.
Step 2.1. Add the retrieved data from XmlResource: addIndexablesForXmlResourceUri
All retrieved data defined in SearchIndexableResources will be added in this step. Next, let's take a look at the specific implementation:
Step2.1.1 obtain the Cursor of the retrieved data
Step 2.1.2 create SearchIndexableResource and add it to the cache list of updated data:
Step 2.2. Add the retrieved data from RawData: addIndexablesForRawDataUri
Settings does not provide any RawData, so we can ignore this step.
Step add unavailable data to the cache
Settings does not provide any NonIndexableKey, so we can ignore this step.
Step 4. Update cache retrieval data
The updated cache retrieval data is implemented in the updateInternal () method. Next we will analyze it step by step.
Cache Data Structure
Until now, the search data provided by Settings has been added to the "dataToUpdate" list. The other two do not contain any retrieval data provided by Settings.
Step 4.1 create an UpdateIndexTask background task to update the Retrieval Database
We only care about the operations related to dataToUpdate.
Step4.2 update the dataToUpdate cache: processDataToUpdate
In this method, we use indexOneSearchIndexableData to update every SearchIndexableData.
Step4.3 index the Retrieved Data: indexOneSearchIndexableData
The retrieved data can include RawData and XmlResource, or SearchIndexableResource returned by Settings.
Why is SearchIndexableResource? Because SettingsSearchIndexablesProvider inserts all the data that can be searched into the Cursor and returns it to the data queryer, which is the Settings itself.
Step4.4 perform data retrieval for each retrieved data resource: indexOneResource
As mentioned above, Settings has a special data retrieval resource: Indexable.
Here, if we find that the xmlResId of this data retrieval resource is NO_DATA_RES_ID, that is, the current data retrieval resource is a special data retrieval resource. We need to use the reflection mechanism to obtain its pusblic static SEARCH_INDEX_DATA_PROVIDER.
Step 4.4.1. process common data retrieval resources: xmlResId! = NO_DATA_RES_ID
Step 4.4.1.1. parse and retrieve data from XML: indexFromResource
At the very end of the entire implementation process, parse the XMl file of the entire PreferenceScreen and add each qualified item to the Retrieval Database.
Step 4.4.2. process special data retrieval resources: Indexable
Step 4.4.2.1. retrieve the retrieved data from a Special Data source: indexFromProvider
After obtaining the SEARCH_INDEX_DATA_PROVIDER variable, we get the two return values of the SEARCH_INDEX_DATA_PROVIDER method. Based on the specific content of the return value, we update it to the database.