1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.search;
18 
19 import android.content.Context;
20 import android.content.res.XmlResourceParser;
21 import android.provider.SearchIndexableResource;
22 import android.support.annotation.CallSuper;
23 import android.text.TextUtils;
24 import android.util.AttributeSet;
25 import android.util.Log;
26 import android.util.Xml;
27 
28 import com.android.settings.core.PreferenceController;
29 import com.android.settings.search2.XmlParserUtils;
30 
31 import org.xmlpull.v1.XmlPullParser;
32 import org.xmlpull.v1.XmlPullParserException;
33 
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
38 
39 /**
40  * A basic SearchIndexProvider that returns no data to index.
41  */
42 public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider {
43 
44     private static final String TAG = "BaseSearchIndex";
45     private static final List<String> EMPTY_LIST = new ArrayList<>();
46 
BaseSearchIndexProvider()47     public BaseSearchIndexProvider() {
48     }
49 
50     @Override
getXmlResourcesToIndex(Context context, boolean enabled)51     public List<SearchIndexableResource> getXmlResourcesToIndex(Context context, boolean enabled) {
52         return null;
53     }
54 
55     @Override
getRawDataToIndex(Context context, boolean enabled)56     public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
57         return null;
58     }
59 
60     @Override
61     @CallSuper
getNonIndexableKeys(Context context)62     public List<String> getNonIndexableKeys(Context context) {
63         if (!isPageSearchEnabled(context)) {
64             // Entire page should be suppressed, mark all keys from this page as non-indexable.
65             return getNonIndexableKeysFromXml(context);
66         }
67         final List<PreferenceController> controllers = getPreferenceControllers(context);
68         if (controllers != null && !controllers.isEmpty()) {
69             final List<String> nonIndexableKeys = new ArrayList<>();
70             for (PreferenceController controller : controllers) {
71                 controller.updateNonIndexableKeys(nonIndexableKeys);
72             }
73             return nonIndexableKeys;
74         } else {
75             return new ArrayList<>();
76         }
77     }
78 
79     @Override
getPreferenceControllers(Context context)80     public List<PreferenceController> getPreferenceControllers(Context context) {
81         return null;
82     }
83 
84     /**
85      * Returns true if the page should be considered in search query. If return false, entire page
86      * will be suppressed during search query.
87      */
isPageSearchEnabled(Context context)88     protected boolean isPageSearchEnabled(Context context) {
89         return true;
90     }
91 
getNonIndexableKeysFromXml(Context context)92     private List<String> getNonIndexableKeysFromXml(Context context) {
93         final List<SearchIndexableResource> resources = getXmlResourcesToIndex(
94                 context, true /* not used*/);
95         if (resources == null || resources.isEmpty()) {
96             return EMPTY_LIST;
97         }
98         final List<String> nonIndexableKeys = new ArrayList<>();
99         for (SearchIndexableResource res : resources) {
100             final XmlResourceParser parser = context.getResources().getXml(res.xmlResId);
101             final AttributeSet attrs = Xml.asAttributeSet(parser);
102             try {
103                 while (parser.next() != XmlPullParser.END_DOCUMENT) {
104                     final String key = XmlParserUtils.getDataKey(context, attrs);
105                     if (!TextUtils.isEmpty(key)) {
106                         nonIndexableKeys.add(key);
107                     }
108                 }
109             } catch (IOException | XmlPullParserException e) {
110                 Log.w(TAG, "Error parsing non-indexable from xml " + res.xmlResId);
111             }
112         }
113         return nonIndexableKeys;
114     }
115 
116 }
117