A brief analysis of android5.0 system setting search function

Source: Internet
Author: User

First, the purpose:
This paper introduces the implementation of the system setting search function, and is familiar with the search database building rules and matching logic.
Ii. answers to related questions
1, the system settings can be set to build the search database?
A: System settings for the construction of data items are defined in the Com.android.settings.search.SearchIndexableResources class, such as the following, the WiFi settings, WiFi Advanced settings settings if the search data matching library.

Sresmap. Put(wifisettings. Class. GetName(), New Searchindexableresource (Ranking. Getrankforclassname(wifisettings. Class. GetName()), no_data_res_id, wifisettings. Class. GetName(), R. drawable. IC_settings_wireless));Sresmap. Put(advancedwifisettings. Class. GetName(), New Searchindexableresource (Ranking. Getrankforclassname(advancedwifisettings. Class. GetName()), R. XML. WiFi_advanced_settings, Advancedwifisettings. Class. GetName(), R. drawable. IC_settings_wireless));

2, when the keyword search, there are multiple matches, at the time of presentation, what is their collation?
A: Com.android.settings.search.Ranking, which defines the corresponding rand_id for each set item. For example:

sRankMap.put(WifiSettings.class.getName(), RANK_WIFI);        sRankMap.put(AdvancedWifiSettings.class.getName(), RANK_WIFI);        sRankMap.put(SavedAccessPointsWifiSettings.class.getName(), RANK_WIFI);

When a search match is made, it is sorted according to rank_id:

privatebuildSearchSQLboolean withOrderBy) {        new StringBuilder();        sb.append(buildSearchSQLForColumn(query, colums));        if (withOrderBy) {            sb.append(" ORDER BY ");            sb.append(IndexColumns.DATA_RANK);        }        return sb.toString();    }

3. How are settings items in the settings resolved and added to the search matching database?
Total two categories:
Class One:
As shown in the code, for WiFi Advanced settings:

sresmap.put  ( Advancedwifisettings.class  .getName  (), New Searchindexableresource (Ranking.ge Trankforclassname  (Advancedwifisettings.class  .getname  ()), R.xml  .wifi  _advanced_settings, Advancedwifisettings.class  .getname  (), R.drawable  .ic  _settings_wireless)"  

Where r.xml.wifi_advanced_settings is defined, the XML file is parsed and added to the search database in Index.java. such as code:

if(!NodeName.equals(node_name_check_box_preference)) {Summary=Getdatasummary (context, attrs);StringEntries= NULL;if(NodeName.EndsWith (node_name_list_preference)) {entries=Getdataentries (context, attrs); }//Insert rows for the child nodes of PreferencescreenUpdateonerowwithfiltereddata (Database, LOCALESTR, title, summary,NULL, entries, Fragmentname, Screentitle, iconresid, rank, keywords, i Ntentaction, Intenttargetpackage, Intenttargetclass,true, Key,-1 / * Default User ID * /); }Else{StringSummaryon=Getdatasummaryon (context, attrs);StringSummaryoff=Getdatasummaryoff (context, attrs);if(textutils.IsEmpty (Summaryon)&&Textutils.IsEmpty (Summaryoff)) {Summaryon=Getdatasummary (context, attrs); } updateonerowwithfiltereddata (Database, Localestr, title, Summaryon, Summaryoff,NULL, Fragmentname, Screentitle, iconresid, rank, keywords, intentaction, intenttargetpackage, int Enttargetclass,true, Key,-1 / * Default User ID * /); }

Non-checkboxpreference settings will parse the title of preference and summary. Checkboxpreference types of settings will parse Summaryon,summaryoff and so on. That is, there are subtle differences in parsing.
The detailed parsing process can focus on the Indexfromresource () method in Index.java.
Class Two:
As shown in the code:

sresmap.put  (                 Wifisettings.class  .getName  (), New Searchindexableresource (Ranking.getrankforclassname  (                         Wifisettings.class  .getName  ()), no_data_res_id, Wifisettings.class  .getname  (), R.drawable  .ic  _settings_wireless)) ;   

This class differs from class one in that it does not explicitly specify an XML file, which is: no_data_res_id. In this case, it defines a searchindexprovider in its class with the name: Search_index_data_provider, in which those settings are defined to be loaded.
4. How can I define multiple matching names for a set item?
A: The matching logic itself is a weak match, and we look at the matching logic set to match the following:

privatestaticfinal String[] MATCH_COLUMNS_PRIMARY = {             IndexColumns.DATA_TITLE,             IndexColumns.DATA_TITLE_NORMALIZED,             IndexColumns.DATA_KEYWORDS     };

Title is the title name of each set item, and title_normalized just does some formatting for the title. The most important thing is our keyword, we can use keywords to specify an additional matching name for a set item, for example:

< Preferencescreen  android:key  = "Brightne SS " android:title  =" @string/brightness "  settings:keywords  = "@string/keywords_d Isplay_brightness_level ";  <intent  Span class= "Hljs-attribute" >android:action  = "android.intent.action.SHOW_ Brightness_dialog "/>  </preferencescreen
      >   

5. Does the system settings support search match loading external settings?
A: The system setup has the ability to search for matching external app settings. The system settings load the PROVIDER configured with "Android.content.action.SEARCH_INDEXABLES_PROVIDER" in Androidmanifest.xml in all apps, and read the settings entry from the provider to the set database.
Since android5.0 initially implemented this function, many applications in the system have not yet been created provider, so the experience of searching matches other applications is not very good.
Third, how do we add our newly developed settings to the system settings.
1, first if, the newly added setting item in the first level directory, or the multi-level directory, and this setting item is Leui new, namely the native does not exist. You need to add the first-level setting to the SRESMAP member variable of searchindexableresources.
2, if it is in the original settings to add new settings, to maintain the title level Summary,fragmeng and other configuration OK.

A brief analysis of android5.0 system setting search function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.