Allow page to suppress all of its content from search.

To suppress things from search, we usually use preferencecontroller to
control individual items. But a page can sometimes to disabled
entirely. Now it can set isPageSearchEnabled to false to achieve that.

Change-Id: I39424b7fdc6460e27fd7c83cf09b5ace12d1cc37
Fix: 36201167
Test: make RunSettingsRoboTests
This commit is contained in:
Fan Zhang
2017-03-14 12:38:43 -07:00
parent ae4d32e2ab
commit cabc509e4f
5 changed files with 139 additions and 13 deletions

View File

@@ -17,10 +17,20 @@
package com.android.settings.search;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.provider.SearchIndexableResource;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Xml;
import com.android.settings.core.PreferenceController;
import com.android.settings.search2.XmlParserUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -30,6 +40,7 @@ import java.util.List;
*/
public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider {
private static final String TAG = "BaseSearchIndex";
private static final List<String> EMPTY_LIST = Collections.emptyList();
public BaseSearchIndexProvider() {
@@ -47,6 +58,10 @@ public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider {
@Override
public List<String> getNonIndexableKeys(Context context) {
if (!isPageSearchEnabled(context)) {
// Entire page should be suppressed, mark all keys from this page as non-indexable.
return getNonIndexableKeysFromXml(context);
}
final List<PreferenceController> controllers = getPreferenceControllers(context);
if (controllers != null && !controllers.isEmpty()) {
final List<String> nonIndexableKeys = new ArrayList<>();
@@ -63,4 +78,37 @@ public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider {
public List<PreferenceController> getPreferenceControllers(Context context) {
return null;
}
/**
* Returns true if the page should be considered in search query. If return false, entire page
* will be suppressed during search query.
*/
protected boolean isPageSearchEnabled(Context context) {
return true;
}
private List<String> getNonIndexableKeysFromXml(Context context) {
final List<SearchIndexableResource> resources = getXmlResourcesToIndex(
context, true /* not used*/);
if (resources == null || resources.isEmpty()) {
return EMPTY_LIST;
}
final List<String> nonIndexableKeys = new ArrayList<>();
for (SearchIndexableResource res : resources) {
final XmlResourceParser parser = context.getResources().getXml(res.xmlResId);
final AttributeSet attrs = Xml.asAttributeSet(parser);
try {
while (parser.next() != XmlPullParser.END_DOCUMENT) {
final String key = XmlParserUtils.getDataKey(context, attrs);
if (!TextUtils.isEmpty(key)) {
nonIndexableKeys.add(key);
}
}
} catch (IOException | XmlPullParserException e) {
Log.w(TAG, "Error parsing non-indexable from xml " + res.xmlResId);
}
}
return nonIndexableKeys;
}
}