2025 年 3 月 27 日より、AOSP のビルドとコントリビューションには aosp-main
ではなく android-latest-release
を使用することをおすすめします。詳細については、AOSP の変更をご覧ください。
ユニバーサル検索
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Android 8.0 には、設定メニューの拡張検索機能が追加されています。このドキュメントでは、設定を追加する方法と、設定を検索したときにそれが確実に見つかるように適切にインデックス登録する方法を説明します。
インデックス登録できる設定を作成する
インデックス登録が必要な各設定フラグメントは Indexable
インターフェースを実装します。さらに、次の静的項目を必要とします。
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER
インデックス登録用にフラグメントを設定したら、以下の場所にある SearchIndexableResources
に追加します。
packages/apps/Settings/src/com/android/settings/search/SearchIndexableResources.java
オプションのメソッド
この SearchIndexProvider
インターフェースにはオプションのメソッドが 4 つあります。
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
である場合、これをオーバーライドする必要はほとんどありません。
- 指定のユーザー、デバイス、設定などについて表示されない結果に対応するキーのリストが返されます。ここで提供されるキーは、
SearchIndexableResource
と SearchIndexableRaw
の KEY 項目に一致する必要があります。
- たとえば、デバイスに 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
このフラグメントに関連付けられた設定コントローラのリストが返されます。
このリストは、インライン結果の作成や、インデックス登録できないデータの更新などに使用されます。
したがって、検索結果に表示するデータはすべて getXmlResourcesToIndex
または getRawDataToIndex
に含める必要があります。
設定用のキーワードを追加する
設定を簡単に検索できるようにするには、ユーザーが検索に使用できる、設定に関連したキーワードを追加します。
キーワードを追加する際の注意事項:
- キーワードは、ユーザーに表示されるとは限りませんが、設定の機能に関する心理モデルに含まれている可能性がある単語のリストです。
- ユーザーはこれらの単語を入力して設定にアクセスできます。
- 類義語も指定でき、設定に関連するあらゆる単語を使用可能です。
- たとえば、「ミュート」は [音量] 設定の検索に使用できます。
重複を回避する
設定ページを無条件に抑制する場合は、元のページのインデックス登録を削除して、結果が重複しないようにします。
- 抑制するページの
PreferenceFragment
を見つけます。
SearchIndexProvider
を削除します。
検証
新しい設定の検索可能性をテストするには:
- デバイスに O の最新バージョンをインストールします。
- 次のとおりに選択して、データベースをインデックスに再登録します。
[設定] > [アプリと通知] > [アプリ情報] > [設定] > [ストレージ] > [データを消去]
- ターゲット設定が検索結果に表示されることを確認します。
設定のタイトルのプレフィックスを検索すると、一致します。
次の robolectric テストを実施して、この機能の実装を検証できます。
packages/apps/Settings/tests/robotests/src/com/android/settings/search
ビルド ターゲット: RunSettingsRoboTests
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-08-26 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-08-26 UTC。"],[],[],null,["# Universal search\n\nAndroid 8.0 adds expanded search capabilities for the *Settings* menu. This\ndocument describes how to add a setting and ensure it is properly indexed for\nSettings search.\n\nCreate indexable settings\n-------------------------\n\n\nEach Settings fragment that needs to be indexed implements the\n`Indexable`interface, AND requires the static\nfield:\n\n```scdoc\npublic static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER\n```\n\n\nAfter you have your fragment set up for indexing, add it to\n`SearchIndexableResources` found at: \n\n`packages/apps/Settings/src/com/android/settings/search/SearchIndexableResources.java\n`\n\nOptional methods\n----------------\n\nThis `SearchIndexProvider`interface has four optional\nmethods.\n\n### getXmlResourcesToIndex\n\n- Override this if your fragment content is from: `preference xml`\n- Returns an XML preference as a list to be indexed.\n\nXML resources example: \n\n```world-of-warcraft-toc\npublic List\u003cSearchIndexableResource\u003e getXmlResourcesToIndex(Context context, boolean enabled) {\n ArrayList\u003cSearchIndexableResource\u003e result = new ArrayList\u003cSearchIndexableResource\u003e();\nSearchIndexableResource sir = new SearchIndexableResource(context);\n\tsir.xmlResId = R.xml.display_settings;\n\tresult.add(sir);\n\n return result;\n}\n```\n\n### getRawDataToIndex\n\n- Override this if your fragment content is NOT from: `preference\n xml`\n- Returns a list of Raw data (`SearchIndexableRaw`) to be indexed.\n\nRaw data example: \n\n```scdoc\npublic List\u003cSearchIndexableRaw\u003e getRawDataToIndex(Context context, boolean enabled) {\n final List\u003cSearchIndexableRaw\u003e result = new ArrayList\u003c\u003e();\n final Resources res = context.getResources();\n\n // Add fragment title\n SearchIndexableRaw data = new SearchIndexableRaw(context);\n data.title = res.getString(R.string.wifi_settings);\n data.screenTitle = res.getString(R.string.wifi_settings);\n data.keywords = res.getString(R.string.keywords_wifi);\n data.key = DATA_KEY_REFERENCE;\n result.add(data);\n\n return result;\n}\n```\n\n### getNonIndexableKeys\n\n- If your fragment is a `DashboardFragment`, you rarely need to override this.\n- Returns a list of keys that corresponds to results that should not show up for the given user, device, configuration, etc.The keys provided here should match the *KEY* field in `SearchIndexableResource` and `SearchIndexableRaw`.\n- For example: Data Usage should not show up for users who have never had a SIM card in their device.\n\nNon-indexable keys example: \n\n```scdoc\npublic List\u003cString\u003e getNonIndexableKeys(Context context) {\n final List\u003cString\u003e keys = super.getNonIndexableKeys(context);\n if (!checkIntentAction(context, \"android.settings.TERMS\")) {\n keys.add(KEY_TERMS);\n }\n if (!checkIntentAction(context, \"android.settings.LICENSE\")) {\n keys.add(KEY_LICENSE);\n }\n if (!checkIntentAction(context, \"android.settings.COPYRIGHT\")) {\n keys.add(KEY_COPYRIGHT);\n }\n if (!checkIntentAction(context, \"android.settings.WEBVIEW_LICENSE\")) {\n keys.add(KEY_WEBVIEW_LICENSE);\n }\n return keys;\n}\n```\n\n### getPreferenceControllers\n\n\nReturns a list of preference controllers associated with this fragment.\nThis list is used to form inline results, update non-indexables, etc.\n\n\nThus, everything you want to show up in search must be included in either\n`getXmlResourcesToIndex` or `getRawDataToIndex`.\n\nAdd keywords for your settings\n------------------------------\n\n\nTo ensure a setting is easily searchable, add keywords that are relevant for the\nsetting that a user may use to search for the setting.\n\n\nThings to consider when adding keywords:\n\n- Keywords are a list of words that the user does not necessarily see but may be part of their mental model for how the setting works.\n- These are words that the user might type to get to your setting.\n- They can be synonyms or any words associated to the setting can be used.\n- For example, \"mute\" might be used to find the Volume setting.\n\nAvoid duplication\n-----------------\n\n\nIf you are unconditionally suppressing a settings page, remove the indexing of\nthe original page to avoid duplication of results.\n\n1. Find the `PreferenceFragment` of the page you are suppressing.\n2. Remove the `SearchIndexProvider`.\n\nValidation\n----------\n\n\nTo test the searchability of a new setting:\n\n1. Instal a recent version of O on the device.\n2. Reindex the database by selecting:\n*Settings \\\u003e Apps \\& Notifications \\\u003e Apps info \\\u003e Settings \\\u003e Storage \\\u003e\n**Clear Data***\n3. Verify the target settings shows up in search. \n Searching for a prefix of the title of a setting will match it.\n\n\nThese robolectric tests may be run to validate the implementation of this\nfeature: \n\n`packages/apps/Settings/tests/robotests/src/com/android/settings/search`\n\n\nThe build target is: `RunSettingsRoboTests`"]]