| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.provider; |
| 18 |
|
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.annotation.SystemApi; |
| 21 |
import android.app.slice.Slice; |
| 22 |
import android.content.ContentProvider; |
| 23 |
import android.content.ContentValues; |
| 24 |
import android.content.Context; |
| 25 |
import android.content.UriMatcher; |
| 26 |
import android.content.pm.ProviderInfo; |
| 27 |
import android.database.Cursor; |
| 28 |
import android.net.Uri; |
| 29 |
import android.util.Log; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
@SystemApi |
| 69 |
public abstract class SearchIndexablesProvider extends ContentProvider { |
| 70 |
private static final String TAG = "IndexablesProvider"; |
| 71 |
|
| 72 |
private String mAuthority; |
| 73 |
private UriMatcher mMatcher; |
| 74 |
|
| 75 |
private static final int MATCH_RES_CODE = 1; |
| 76 |
private static final int MATCH_RAW_CODE = 2; |
| 77 |
private static final int MATCH_NON_INDEXABLE_KEYS_CODE = 3; |
| 78 |
private static final int MATCH_SITE_MAP_PAIRS_CODE = 4; |
| 79 |
private static final int MATCH_SLICE_URI_PAIRS_CODE = 5; |
| 80 |
private static final int MATCH_DYNAMIC_RAW_CODE = 6; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
@Override |
| 86 |
public void attachInfo(Context context, ProviderInfo info) { |
| 87 |
mAuthority = info.authority; |
| 88 |
|
| 89 |
mMatcher = new UriMatcher(UriMatcher.NO_MATCH); |
| 90 |
mMatcher.addURI(mAuthority, SearchIndexablesContract.INDEXABLES_XML_RES_PATH, |
| 91 |
MATCH_RES_CODE); |
| 92 |
mMatcher.addURI(mAuthority, SearchIndexablesContract.INDEXABLES_RAW_PATH, |
| 93 |
MATCH_RAW_CODE); |
| 94 |
mMatcher.addURI(mAuthority, SearchIndexablesContract.NON_INDEXABLES_KEYS_PATH, |
| 95 |
MATCH_NON_INDEXABLE_KEYS_CODE); |
| 96 |
mMatcher.addURI(mAuthority, SearchIndexablesContract.SITE_MAP_PAIRS_PATH, |
| 97 |
MATCH_SITE_MAP_PAIRS_CODE); |
| 98 |
mMatcher.addURI(mAuthority, SearchIndexablesContract.SLICE_URI_PAIRS_PATH, |
| 99 |
MATCH_SLICE_URI_PAIRS_CODE); |
| 100 |
mMatcher.addURI(mAuthority, SearchIndexablesContract.DYNAMIC_INDEXABLES_RAW_PATH, |
| 101 |
MATCH_DYNAMIC_RAW_CODE); |
| 102 |
|
| 103 |
|
| 104 |
if (!info.exported) { |
| 105 |
throw new SecurityException("Provider must be exported"); |
| 106 |
} |
| 107 |
if (!info.grantUriPermissions) { |
| 108 |
throw new SecurityException("Provider must grantUriPermissions"); |
| 109 |
} |
| 110 |
if (!android.Manifest.permission.READ_SEARCH_INDEXABLES.equals(info.readPermission)) { |
| 111 |
throw new SecurityException("Provider must be protected by READ_SEARCH_INDEXABLES"); |
| 112 |
} |
| 113 |
|
| 114 |
super.attachInfo(context, info); |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, |
| 119 |
String sortOrder) { |
| 120 |
try { |
| 121 |
switch (mMatcher.match(uri)) { |
| 122 |
case MATCH_RES_CODE: |
| 123 |
return queryXmlResources(null); |
| 124 |
case MATCH_RAW_CODE: |
| 125 |
return queryRawData(null); |
| 126 |
case MATCH_NON_INDEXABLE_KEYS_CODE: |
| 127 |
return queryNonIndexableKeys(null); |
| 128 |
case MATCH_SITE_MAP_PAIRS_CODE: |
| 129 |
return querySiteMapPairs(); |
| 130 |
case MATCH_SLICE_URI_PAIRS_CODE: |
| 131 |
return querySliceUriPairs(); |
| 132 |
case MATCH_DYNAMIC_RAW_CODE: |
| 133 |
return queryDynamicRawData(null); |
| 134 |
default: |
| 135 |
throw new UnsupportedOperationException("Unknown Uri " + uri); |
| 136 |
} |
| 137 |
} catch (UnsupportedOperationException e) { |
| 138 |
throw e; |
| 139 |
} catch (Exception e) { |
| 140 |
Log.e(TAG, "Provider querying exception:", e); |
| 141 |
return null; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public abstract Cursor queryXmlResources(String[] projection); |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public abstract Cursor queryRawData(String[] projection); |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public abstract Cursor queryNonIndexableKeys(String[] projection); |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public Cursor querySiteMapPairs() { |
| 185 |
|
| 186 |
return null; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
@Nullable |
| 194 |
public Cursor querySliceUriPairs() { |
| 195 |
|
| 196 |
return null; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
@Nullable |
| 209 |
public Cursor queryDynamicRawData(@Nullable String[] projection) { |
| 210 |
|
| 211 |
return null; |
| 212 |
} |
| 213 |
|
| 214 |
@Override |
| 215 |
public String getType(Uri uri) { |
| 216 |
switch (mMatcher.match(uri)) { |
| 217 |
case MATCH_RES_CODE: |
| 218 |
return SearchIndexablesContract.XmlResource.MIME_TYPE; |
| 219 |
case MATCH_RAW_CODE: |
| 220 |
case MATCH_DYNAMIC_RAW_CODE: |
| 221 |
return SearchIndexablesContract.RawData.MIME_TYPE; |
| 222 |
case MATCH_NON_INDEXABLE_KEYS_CODE: |
| 223 |
return SearchIndexablesContract.NonIndexableKey.MIME_TYPE; |
| 224 |
default: |
| 225 |
throw new IllegalArgumentException("Unknown URI " + uri); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
@Override |
| 233 |
public final Uri insert(Uri uri, ContentValues values) { |
| 234 |
throw new UnsupportedOperationException("Insert not supported"); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
@Override |
| 241 |
public final int delete(Uri uri, String selection, String[] selectionArgs) { |
| 242 |
throw new UnsupportedOperationException("Delete not supported"); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
@Override |
| 249 |
public final int update( |
| 250 |
Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
| 251 |
throw new UnsupportedOperationException("Update not supported"); |
| 252 |
} |
| 253 |
} |