1 /* 2 * Copyright (C) 2020 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.car.settings.search; 18 19 import static com.android.car.settings.common.PreferenceXmlParser.METADATA_KEY; 20 import static com.android.car.settings.common.PreferenceXmlParser.METADATA_SEARCHABLE; 21 import static com.android.car.settings.common.PreferenceXmlParser.MetadataFlag.FLAG_NEED_KEY; 22 import static com.android.car.settings.common.PreferenceXmlParser.MetadataFlag.FLAG_NEED_SEARCHABLE; 23 24 import android.annotation.Nullable; 25 import android.content.Context; 26 import android.os.Bundle; 27 import android.provider.SearchIndexableResource; 28 29 import androidx.annotation.NonNull; 30 import androidx.annotation.XmlRes; 31 32 import com.android.car.settings.common.Logger; 33 import com.android.car.settings.common.PreferenceXmlParser; 34 import com.android.settingslib.search.Indexable; 35 import com.android.settingslib.search.SearchIndexableRaw; 36 37 import org.xmlpull.v1.XmlPullParserException; 38 39 import java.io.IOException; 40 import java.util.ArrayList; 41 import java.util.Collections; 42 import java.util.List; 43 44 /** 45 * A basic SearchIndexProvider. 46 */ 47 public class CarBaseSearchIndexProvider implements Indexable.SearchIndexProvider { 48 private static final Logger LOG = new Logger(CarBaseSearchIndexProvider.class); 49 50 private final int mXmlRes; 51 private final String mIntentAction; 52 private final String mIntentClass; 53 CarBaseSearchIndexProvider(@mlRes int xmlRes, String intentAction)54 public CarBaseSearchIndexProvider(@XmlRes int xmlRes, String intentAction) { 55 mXmlRes = xmlRes; 56 mIntentAction = intentAction; 57 mIntentClass = null; 58 } 59 CarBaseSearchIndexProvider(@mlRes int xmlRes, @NonNull Class intentClass)60 public CarBaseSearchIndexProvider(@XmlRes int xmlRes, @NonNull Class intentClass) { 61 mXmlRes = xmlRes; 62 mIntentAction = null; 63 mIntentClass = intentClass.getName(); 64 } 65 66 @Override getXmlResourcesToIndex(Context context, boolean enabled)67 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context, boolean enabled) { 68 SearchIndexableResource sir = new SearchIndexableResource(context); 69 sir.xmlResId = mXmlRes; 70 sir.intentAction = mIntentAction; 71 sir.intentTargetPackage = context.getPackageName(); 72 sir.intentTargetClass = mIntentClass; 73 return Collections.singletonList(sir); 74 } 75 76 @Override getRawDataToIndex(Context context, boolean enabled)77 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) { 78 return null; 79 } 80 81 @Override getDynamicRawDataToIndex(Context context, boolean enabled)82 public List<SearchIndexableRaw> getDynamicRawDataToIndex(Context context, boolean enabled) { 83 return null; 84 } 85 86 @Override getNonIndexableKeys(Context context)87 public List<String> getNonIndexableKeys(Context context) { 88 boolean searchEnabled = isPageSearchEnabled(context); 89 List<String> keys = new ArrayList<>(); 90 try { 91 List<Bundle> metadata = PreferenceXmlParser.extractMetadata(context, mXmlRes, 92 FLAG_NEED_KEY | FLAG_NEED_SEARCHABLE); 93 for (Bundle bundle : metadata) { 94 if (!searchEnabled || !bundle.getBoolean(METADATA_SEARCHABLE, true)) { 95 keys.add(bundle.getString(METADATA_KEY)); 96 } 97 } 98 } catch (IOException | XmlPullParserException e) { 99 LOG.w("Error parsing non-indexable XML - " + mXmlRes); 100 } 101 102 return keys; 103 } 104 105 /** 106 * Returns true if the page should be considered in search query. If return false, entire page 107 * will be suppressed during search query. 108 */ isPageSearchEnabled(Context context)109 protected boolean isPageSearchEnabled(Context context) { 110 return true; 111 } 112 113 /** 114 * Creates a SearchIndexableRaw object from the provided parameters. 115 */ createRawDataEntry(Context context, String key, String title, @Nullable String screenTitle)116 protected SearchIndexableRaw createRawDataEntry(Context context, String key, String title, 117 @Nullable String screenTitle) { 118 SearchIndexableRaw raw = new SearchIndexableRaw(context); 119 raw.key = key; 120 raw.title = title; 121 raw.screenTitle = screenTitle; 122 raw.intentAction = mIntentAction; 123 raw.intentTargetPackage = context.getPackageName(); 124 raw.intentTargetClass = mIntentClass; 125 return raw; 126 } 127 } 128