1 /* 2 * Copyright (C) 2019 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.settingslib.searchprovider; 18 19 import static android.provider.SearchIndexablesContract.INDEXABLES_XML_RES_COLUMNS; 20 21 import android.content.Context; 22 import android.database.Cursor; 23 import android.database.MatrixCursor; 24 import android.provider.SearchIndexableResource; 25 import android.provider.SearchIndexablesContract.XmlResource; 26 import android.provider.SearchIndexablesProvider; 27 import android.text.TextUtils; 28 29 import java.util.Collection; 30 31 /** 32 * An abstract SearchIndexProvider using {@link SearchIndexableIntentResource} for indexing 33 */ 34 public abstract class SettingsXmlIndexProvider extends SearchIndexablesProvider { 35 private static final String TAG = "XmlIndexProvider"; 36 37 @Override onCreate()38 public boolean onCreate() { 39 return true; 40 } 41 42 @Override queryXmlResources(String[] projection)43 public Cursor queryXmlResources(String[] projection) { 44 final Context context = getContext(); 45 final MatrixCursor cursor = new MatrixCursor(INDEXABLES_XML_RES_COLUMNS); 46 final Collection<SearchIndexableIntentResource> resources = getIntentResources(); 47 48 for (SearchIndexableIntentResource indexableResource : resources) { 49 cursor.newRow() 50 .add(XmlResource.COLUMN_RANK, indexableResource.rank) 51 .add(XmlResource.COLUMN_XML_RESID, indexableResource.xmlResId) 52 .add(XmlResource.COLUMN_CLASS_NAME, indexableResource.className) 53 .add(XmlResource.COLUMN_INTENT_ACTION, indexableResource.intentAction) 54 .add(XmlResource.COLUMN_INTENT_TARGET_PACKAGE, context.getPackageName()) 55 .add(XmlResource.COLUMN_INTENT_TARGET_CLASS, 56 indexableResource.intentTargetClass); 57 } 58 return cursor; 59 } 60 61 /** 62 * Returns all {@link android.provider.SearchIndexablesContract.RawData}. 63 * 64 * Those are the raw indexable data. 65 * 66 * @param projection list of {@link android.provider.SearchIndexablesContract.RawData} columns 67 * to put into the cursor. If {@code null} all supported columns should be 68 * included. 69 */ queryRawData(String[] projection)70 public Cursor queryRawData(String[] projection) { 71 return null; 72 } 73 74 /** 75 * Returns all {@link android.provider.SearchIndexablesContract.NonIndexableKey}. 76 * 77 * Those are the non indexable data keys. 78 * 79 * @param projection list of {@link android.provider.SearchIndexablesContract.NonIndexableKey} 80 * columns to put into the cursor. If {@code null} all supported columns 81 * should be included. 82 */ queryNonIndexableKeys(String[] projection)83 public Cursor queryNonIndexableKeys(String[] projection) { 84 return null; 85 } 86 87 /** 88 * Returns a Collection of {@link SearchIndexableIntentResource} that should be indexed for 89 * search. 90 */ getIntentResources()91 protected abstract Collection<SearchIndexableIntentResource> getIntentResources(); 92 93 /** 94 * Wrapper class of {@link SearchIndexableResource}. It is for setting the search indexable 95 * resource of corresponding XML and intent action with class. 96 */ 97 public static final class SearchIndexableIntentResource extends SearchIndexableResource { 98 /** 99 * Constructor of {@link SearchIndexableIntentResource}. 100 * 101 * @param xmlResId preference xml of target {@link prefereceFragment} 102 * @param intentAction the intent to open target {@link Activity} 103 * @param className the target {@link Activity} class name 104 */ SearchIndexableIntentResource(int xmlResId, String intentAction, String className)105 public SearchIndexableIntentResource(int xmlResId, String intentAction, 106 String className) { 107 super( 108 0 /* rank */, 109 xmlResId, 110 className, 111 0 /* icon resource id */); 112 if (TextUtils.isEmpty(intentAction)) { 113 this.intentAction = "android.intent.action.MAIN"; 114 } else { 115 this.intentAction = intentAction; 116 } 117 this.intentTargetClass = className; 118 } 119 } 120 } 121