綜合搜尋

Android 8.0 針對「設定」選單新增了更強大的搜尋功能。這份文件說明如何新增設定,並確保已正確為設定搜尋功能建立索引。

可建立索引的設定

需要建立索引的每個設定片段都會導入 Indexable 介面,且需要靜態欄位:

public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER

設定要建立索引的片段後,請將片段新增至在下列位置找到的 SearchIndexableResources
packages/apps/Settings/src/com/android/settings/search/SearchIndexableResources.java

選擇性方法

這個 SearchIndexProvider 介面有四種選用方法。

getXmlResourcesToIndex

  • 如果片段內容來自:preference xml,請覆寫這項設定
  • 以清單形式傳回要建立索引的 XML 偏好設定。

XML 資源範例:

public List<SearchIndexableResource> getXmlResourcesToIndex(Context context, boolean enabled) {
    ArrayList<SearchIndexableResource> result =  new ArrayList<SearchIndexableResource>();
SearchIndexableResource sir = new SearchIndexableResource(context);
	sir.xmlResId = R.xml.display_settings;
	result.add(sir);

    return result;
}

getRawDataToIndex

  • 如果片段內容「不是」來自 preference xml,請覆寫這項設定
  • 傳回要建立索引的原始資料 (SearchIndexableRaw) 清單。

原始資料範例:

public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
              final List<SearchIndexableRaw> result = new ArrayList<>();
              final Resources res = context.getResources();

              // Add fragment title
       SearchIndexableRaw data = new SearchIndexableRaw(context);
       data.title = res.getString(R.string.wifi_settings);
       data.screenTitle = res.getString(R.string.wifi_settings);
       data.keywords = res.getString(R.string.keywords_wifi);
       data.key = DATA_KEY_REFERENCE;
       result.add(data);

       return result;
}

getNonIndexableKeys

  • 如果片段是 DashboardFragment,您通常不需要覆寫這項設定。
  • 傳回與指定使用者、裝置和設定等不應顯示的結果對應的鍵清單。此處提供的鍵應與 SearchIndexableResourceSearchIndexableRawKEY 欄位相符。
  • 舉例來說,如果使用者的裝置從未裝有 SIM 卡,系統就不會顯示資料使用情形。

無法建立索引的鍵範例:

public List<String> getNonIndexableKeys(Context context) {
      final List<String> keys = super.getNonIndexableKeys(context);
              if (!checkIntentAction(context, "android.settings.TERMS")) {
                  keys.add(KEY_TERMS);
              }
              if (!checkIntentAction(context, "android.settings.LICENSE")) {
                  keys.add(KEY_LICENSE);
              }
              if (!checkIntentAction(context, "android.settings.COPYRIGHT")) {
                  keys.add(KEY_COPYRIGHT);
              }
              if (!checkIntentAction(context, "android.settings.WEBVIEW_LICENSE")) {
                  keys.add(KEY_WEBVIEW_LICENSE);
              }
              return keys;
}

getPreferenceControllers

傳回與此片段相關聯的偏好設定控制器清單。這份清單可用於建立內嵌結果、更新無法建立索引的項目等。

因此,您想要顯示在搜尋中的所有內容都必須包含在 getXmlResourcesToIndexgetRawDataToIndex 中。

根據設定新增關鍵字

為確保設定可供搜尋,請新增與使用者可在搜尋設定的設定相關的關鍵字。

新增關鍵字的注意事項:

  • 關鍵字是使用者不一定會看到的字詞清單,但可能是設定運作方式的心理模型的一部分。
  • 也就是使用者為前往您設定輸入的字詞。
  • 可以是同義詞或任何與設定相關聯的字詞。
  • 例如「靜音」可能用來找出音量設定。

避免重複

如果您無條件略過設定頁面,請移除原始網頁的索引,以免產生重複結果。

  1. 找出你要隱藏的網頁 PreferenceFragment
  2. 移除 SearchIndexProvider

驗證

如何測試新設定的可搜尋性:

  1. 將裝置上的 O 安裝至最新版本。
  2. 選取下列項目,以便重新為資料庫建立索引:
  3. 「設定」>「應用程式和通知」>「應用程式資訊」>「設定」>「儲存空間」>「清除資料」
  4. 確認搜尋結果能夠顯示目標設定。
    搜尋設定標題前置字串時,系統將會比對設定名稱。

系統可能會執行下列 robolectric 測試,驗證這項功能的實作情形:
packages/apps/Settings/tests/robotests/src/com/android/settings/search

建構目標為:RunSettingsRoboTests