blob: 02a5e6fd157f77290af06af72019e3da49d8ba2d [file] [log] [blame]
Fabrice Di Megliob49995d2014-03-14 19:06:20 -07001/*
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
17package android.provider;
18
Fabrice Di Meglio27e13342015-03-09 16:49:00 -070019import android.annotation.SystemApi;
Fabrice Di Megliob49995d2014-03-14 19:06:20 -070020import android.content.ContentProvider;
21import android.content.ContentValues;
22import android.content.Context;
23import android.content.UriMatcher;
24import android.content.pm.ProviderInfo;
25import android.database.Cursor;
26import android.net.Uri;
27
28/**
29 * Base class for a search indexable provider. Such provider offers data to be indexed either
30 * as a reference to an XML file (like a {@link android.preference.PreferenceScreen}) or either
31 * as some raw data.
32 *
33 * @see SearchIndexableResource
34 * @see SearchIndexableData
35 * @see SearchIndexablesContract
36 *
37 * To create a search indexables provider, extend this class, then implement the abstract methods,
38 * and add it to your manifest like this:
39 *
40 * <pre class="prettyprint">&lt;manifest&gt;
41 * ...
42 * &lt;application&gt;
43 * ...
44 * &lt;provider
45 * android:name="com.example.MyIndexablesProvider"
46 * android:authorities="com.example.myindexablesprovider"
47 * android:exported="true"
48 * android:grantUriPermissions="true"
49 * android:permission="android.permission.READ_SEARCH_INDEXABLES"
50 * &lt;intent-filter&gt;
51 * &lt;action android:name="android.content.action.SEARCH_INDEXABLES_PROVIDER" /&gt;
52 * &lt;/intent-filter&gt;
53 * &lt;/provider&gt;
54 * ...
55 * &lt;/application&gt;
56 *&lt;/manifest&gt;</pre>
57 * <p>
58 * When defining your provider, you must protect it with
59 * {@link android.Manifest.permission#READ_SEARCH_INDEXABLES}, which is a permission only the system
60 * can obtain.
61 * </p>
62 *
63 * @hide
64 */
Fabrice Di Meglio27e13342015-03-09 16:49:00 -070065@SystemApi
Fabrice Di Megliob49995d2014-03-14 19:06:20 -070066public abstract class SearchIndexablesProvider extends ContentProvider {
67 private static final String TAG = "IndexablesProvider";
68
69 private String mAuthority;
70 private UriMatcher mMatcher;
71
72 private static final int MATCH_RES_CODE = 1;
73 private static final int MATCH_RAW_CODE = 2;
Fabrice Di Meglio97babb12014-04-11 18:45:44 -070074 private static final int MATCH_NON_INDEXABLE_KEYS_CODE = 3;
Fan Zhang9a0f6fe2017-11-27 12:39:52 -080075 private static final int MATCH_SITE_MAP_PAIRS_CODE = 4;
Fabrice Di Megliob49995d2014-03-14 19:06:20 -070076
77 /**
78 * Implementation is provided by the parent class.
79 */
80 @Override
81 public void attachInfo(Context context, ProviderInfo info) {
82 mAuthority = info.authority;
83
84 mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
85 mMatcher.addURI(mAuthority, SearchIndexablesContract.INDEXABLES_XML_RES_PATH,
86 MATCH_RES_CODE);
87 mMatcher.addURI(mAuthority, SearchIndexablesContract.INDEXABLES_RAW_PATH,
88 MATCH_RAW_CODE);
Fabrice Di Meglio97babb12014-04-11 18:45:44 -070089 mMatcher.addURI(mAuthority, SearchIndexablesContract.NON_INDEXABLES_KEYS_PATH,
90 MATCH_NON_INDEXABLE_KEYS_CODE);
Fan Zhang9a0f6fe2017-11-27 12:39:52 -080091 mMatcher.addURI(mAuthority, SearchIndexablesContract.SITE_MAP_PAIRS_PATH,
92 MATCH_SITE_MAP_PAIRS_CODE);
Fabrice Di Megliob49995d2014-03-14 19:06:20 -070093
94 // Sanity check our setup
95 if (!info.exported) {
96 throw new SecurityException("Provider must be exported");
97 }
98 if (!info.grantUriPermissions) {
99 throw new SecurityException("Provider must grantUriPermissions");
100 }
101 if (!android.Manifest.permission.READ_SEARCH_INDEXABLES.equals(info.readPermission)) {
102 throw new SecurityException("Provider must be protected by READ_SEARCH_INDEXABLES");
103 }
104
105 super.attachInfo(context, info);
106 }
107
108 @Override
109 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
110 String sortOrder) {
111 switch (mMatcher.match(uri)) {
112 case MATCH_RES_CODE:
113 return queryXmlResources(null);
114 case MATCH_RAW_CODE:
115 return queryRawData(null);
Fabrice Di Meglio97babb12014-04-11 18:45:44 -0700116 case MATCH_NON_INDEXABLE_KEYS_CODE:
117 return queryNonIndexableKeys(null);
Fan Zhang9a0f6fe2017-11-27 12:39:52 -0800118 case MATCH_SITE_MAP_PAIRS_CODE:
119 return querySiteMapPairs();
Fabrice Di Megliob49995d2014-03-14 19:06:20 -0700120 default:
121 throw new UnsupportedOperationException("Unknown Uri " + uri);
122 }
123 }
124
125 /**
126 * Returns all {@link android.provider.SearchIndexablesContract.XmlResource}.
127 *
Fabrice Di Meglio97babb12014-04-11 18:45:44 -0700128 * Those are Xml resource IDs to some {@link android.preference.PreferenceScreen}.
Fabrice Di Megliob49995d2014-03-14 19:06:20 -0700129 *
130 * @param projection list of {@link android.provider.SearchIndexablesContract.XmlResource}
131 * columns to put into the cursor. If {@code null} all supported columns
132 * should be included.
133 */
134 public abstract Cursor queryXmlResources(String[] projection);
135
136 /**
137 * Returns all {@link android.provider.SearchIndexablesContract.RawData}.
138 *
Fabrice Di Meglio97babb12014-04-11 18:45:44 -0700139 * Those are the raw indexable data.
Fabrice Di Megliob49995d2014-03-14 19:06:20 -0700140 *
141 * @param projection list of {@link android.provider.SearchIndexablesContract.RawData} columns
142 * to put into the cursor. If {@code null} all supported columns should be
143 * included.
144 */
145 public abstract Cursor queryRawData(String[] projection);
146
Fabrice Di Meglio97babb12014-04-11 18:45:44 -0700147 /**
148 * Returns all {@link android.provider.SearchIndexablesContract.NonIndexableKey}.
149 *
150 * Those are the non indexable data keys.
151 *
152 * @param projection list of {@link android.provider.SearchIndexablesContract.NonIndexableKey}
153 * columns to put into the cursor. If {@code null} all supported columns
154 * should be included.
155 */
156 public abstract Cursor queryNonIndexableKeys(String[] projection);
157
Fan Zhang9a0f6fe2017-11-27 12:39:52 -0800158 /**
159 * Returns a {@link Cursor}, where rows are [parent class, child class] entries to form a site
160 * map. The list of pairs should be as complete as possible.
161 *
162 * @hide
163 */
164 public Cursor querySiteMapPairs() {
165 // By default no-op.
166 return null;
167 }
168
Fabrice Di Megliob49995d2014-03-14 19:06:20 -0700169 @Override
170 public String getType(Uri uri) {
171 switch (mMatcher.match(uri)) {
172 case MATCH_RES_CODE:
173 return SearchIndexablesContract.XmlResource.MIME_TYPE;
174 case MATCH_RAW_CODE:
175 return SearchIndexablesContract.RawData.MIME_TYPE;
Fabrice Di Meglio97babb12014-04-11 18:45:44 -0700176 case MATCH_NON_INDEXABLE_KEYS_CODE:
177 return SearchIndexablesContract.NonIndexableKey.MIME_TYPE;
Fabrice Di Megliob49995d2014-03-14 19:06:20 -0700178 default:
179 throw new IllegalArgumentException("Unknown URI " + uri);
180 }
181 }
182
183 /**
koprivadebd4ee2018-09-13 10:59:46 -0700184 * Implementation is provided by the parent class. Throws by default, and cannot be overridden.
Fabrice Di Megliob49995d2014-03-14 19:06:20 -0700185 */
186 @Override
187 public final Uri insert(Uri uri, ContentValues values) {
188 throw new UnsupportedOperationException("Insert not supported");
189 }
190
191 /**
koprivadebd4ee2018-09-13 10:59:46 -0700192 * Implementation is provided by the parent class. Throws by default, and cannot be overridden.
Fabrice Di Megliob49995d2014-03-14 19:06:20 -0700193 */
194 @Override
195 public final int delete(Uri uri, String selection, String[] selectionArgs) {
196 throw new UnsupportedOperationException("Delete not supported");
197 }
198
199 /**
koprivadebd4ee2018-09-13 10:59:46 -0700200 * Implementation is provided by the parent class. Throws by default, and cannot be overridden.
Fabrice Di Megliob49995d2014-03-14 19:06:20 -0700201 */
202 @Override
203 public final int update(
204 Uri uri, ContentValues values, String selection, String[] selectionArgs) {
205 throw new UnsupportedOperationException("Update not supported");
206 }
207}