blob: ed5dfa5393ecb5d10e4e8c7f0dfbc2c4e41721df [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2006 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.content;
18
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070019import android.net.Uri;
20import android.os.Parcel;
21import android.os.Parcelable;
22import android.os.PatternMatcher;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080023import android.text.TextUtils;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070024import android.util.AndroidException;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070025import android.util.Log;
26import android.util.Printer;
Dianne Hackborn2269d1572010-02-24 19:54:22 -080027
28import com.android.internal.util.XmlUtils;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070029
Gilles Debunne37051cd2011-05-25 16:27:13 -070030import org.xmlpull.v1.XmlPullParser;
31import org.xmlpull.v1.XmlPullParserException;
32import org.xmlpull.v1.XmlSerializer;
33
34import java.io.IOException;
35import java.util.ArrayList;
36import java.util.Iterator;
37import java.util.Set;
38
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070039/**
40 * Structured description of Intent values to be matched. An IntentFilter can
41 * match against actions, categories, and data (either via its type, scheme,
42 * and/or path) in an Intent. It also includes a "priority" value which is
43 * used to order multiple matching filters.
44 *
45 * <p>IntentFilter objects are often created in XML as part of a package's
46 * {@link android.R.styleable#AndroidManifest AndroidManifest.xml} file,
47 * using {@link android.R.styleable#AndroidManifestIntentFilter intent-filter}
48 * tags.
49 *
50 * <p>There are three Intent characteristics you can filter on: the
51 * <em>action</em>, <em>data</em>, and <em>categories</em>. For each of these
52 * characteristics you can provide
53 * multiple possible matching values (via {@link #addAction},
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -070054 * {@link #addDataType}, {@link #addDataScheme}, {@link #addDataSchemeSpecificPart},
55 * {@link #addDataAuthority}, {@link #addDataPath}, and {@link #addCategory}, respectively).
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070056 * For actions, the field
57 * will not be tested if no values have been given (treating it as a wildcard);
58 * if no data characteristics are specified, however, then the filter will
59 * only match intents that contain no data.
60 *
61 * <p>The data characteristic is
62 * itself divided into three attributes: type, scheme, authority, and path.
63 * Any that are
64 * specified must match the contents of the Intent. If you specify a scheme
65 * but no type, only Intent that does not have a type (such as mailto:) will
66 * match; a content: URI will never match because they always have a MIME type
67 * that is supplied by their content provider. Specifying a type with no scheme
68 * has somewhat special meaning: it will match either an Intent with no URI
69 * field, or an Intent with a content: or file: URI. If you specify neither,
70 * then only an Intent with no data or type will match. To specify an authority,
71 * you must also specify one or more schemes that it is associated with.
72 * To specify a path, you also must specify both one or more authorities and
73 * one or more schemes it is associated with.
74 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070075 * <div class="special reference">
76 * <h3>Developer Guides</h3>
77 * <p>For information about how to create and resolve intents, read the
78 * <a href="{@docRoot}guide/topics/intents/intents-filters.html">Intents and Intent Filters</a>
79 * developer guide.</p>
80 * </div>
81 *
82 * <h3>Filter Rules</h3>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070083 * <p>A match is based on the following rules. Note that
84 * for an IntentFilter to match an Intent, three conditions must hold:
85 * the <strong>action</strong> and <strong>category</strong> must match, and
86 * the data (both the <strong>data type</strong> and
Dianne Hackborn0ea920d2013-10-11 09:40:18 -070087 * <strong>data scheme+authority+path</strong> if specified) must match
88 * (see {@link #match(ContentResolver, Intent, boolean, String)} for more details
89 * on how the data fields match).
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070090 *
91 * <p><strong>Action</strong> matches if any of the given values match the
Dianne Hackborna53ee352013-02-20 12:47:02 -080092 * Intent action; if the filter specifies no actions, then it will only match
93 * Intents that do not contain an action.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070094 *
95 * <p><strong>Data Type</strong> matches if any of the given values match the
96 * Intent type. The Intent
97 * type is determined by calling {@link Intent#resolveType}. A wildcard can be
98 * used for the MIME sub-type, in both the Intent and IntentFilter, so that the
99 * type "audio/*" will match "audio/mpeg", "audio/aiff", "audio/*", etc.
Dianne Hackbornb3cddae2009-04-13 16:54:00 -0700100 * <em>Note that MIME type matching here is <b>case sensitive</b>, unlike
101 * formal RFC MIME types!</em> You should thus always use lower case letters
102 * for your MIME types.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700103 *
104 * <p><strong>Data Scheme</strong> matches if any of the given values match the
105 * Intent data's scheme.
106 * The Intent scheme is determined by calling {@link Intent#getData}
107 * and {@link android.net.Uri#getScheme} on that URI.
Dianne Hackbornb3cddae2009-04-13 16:54:00 -0700108 * <em>Note that scheme matching here is <b>case sensitive</b>, unlike
109 * formal RFC schemes!</em> You should thus always use lower case letters
110 * for your schemes.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700111 *
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700112 * <p><strong>Data Scheme Specific Part</strong> matches if any of the given values match
113 * the Intent's data scheme specific part <em>and</em> one of the data schemes in the filter
114 * has matched the Intent, <em>or</em> no scheme specific parts were supplied in the filter.
115 * The Intent scheme specific part is determined by calling
116 * {@link Intent#getData} and {@link android.net.Uri#getSchemeSpecificPart} on that URI.
117 * <em>Note that scheme specific part matching is <b>case sensitive</b>.</em>
118 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700119 * <p><strong>Data Authority</strong> matches if any of the given values match
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700120 * the Intent's data authority <em>and</em> one of the data schemes in the filter
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700121 * has matched the Intent, <em>or</em> no authories were supplied in the filter.
122 * The Intent authority is determined by calling
123 * {@link Intent#getData} and {@link android.net.Uri#getAuthority} on that URI.
Dianne Hackbornb3cddae2009-04-13 16:54:00 -0700124 * <em>Note that authority matching here is <b>case sensitive</b>, unlike
125 * formal RFC host names!</em> You should thus always use lower case letters
126 * for your authority.
127 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700128 * <p><strong>Data Path</strong> matches if any of the given values match the
129 * Intent's data path <em>and</em> both a scheme and authority in the filter
130 * has matched against the Intent, <em>or</em> no paths were supplied in the
131 * filter. The Intent authority is determined by calling
132 * {@link Intent#getData} and {@link android.net.Uri#getPath} on that URI.
133 *
134 * <p><strong>Categories</strong> match if <em>all</em> of the categories in
135 * the Intent match categories given in the filter. Extra categories in the
136 * filter that are not in the Intent will not cause the match to fail. Note
137 * that unlike the action, an IntentFilter with no categories
138 * will only match an Intent that does not have any categories.
139 */
140public class IntentFilter implements Parcelable {
141 private static final String SGLOB_STR = "sglob";
142 private static final String PREFIX_STR = "prefix";
143 private static final String LITERAL_STR = "literal";
144 private static final String PATH_STR = "path";
145 private static final String PORT_STR = "port";
146 private static final String HOST_STR = "host";
147 private static final String AUTH_STR = "auth";
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700148 private static final String SSP_STR = "ssp";
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700149 private static final String SCHEME_STR = "scheme";
150 private static final String TYPE_STR = "type";
151 private static final String CAT_STR = "cat";
152 private static final String NAME_STR = "name";
153 private static final String ACTION_STR = "action";
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800154 private static final String AUTO_VERIFY_STR = "autoVerify";
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700155
156 /**
157 * The filter {@link #setPriority} value at which system high-priority
158 * receivers are placed; that is, receivers that should execute before
159 * application code. Applications should never use filters with this or
160 * higher priorities.
161 *
162 * @see #setPriority
163 */
164 public static final int SYSTEM_HIGH_PRIORITY = 1000;
165
166 /**
167 * The filter {@link #setPriority} value at which system low-priority
168 * receivers are placed; that is, receivers that should execute after
169 * application code. Applications should never use filters with this or
170 * lower priorities.
171 *
172 * @see #setPriority
173 */
174 public static final int SYSTEM_LOW_PRIORITY = -1000;
175
176 /**
177 * The part of a match constant that describes the category of match
178 * that occurred. May be either {@link #MATCH_CATEGORY_EMPTY},
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700179 * {@link #MATCH_CATEGORY_SCHEME}, {@link #MATCH_CATEGORY_SCHEME_SPECIFIC_PART},
180 * {@link #MATCH_CATEGORY_HOST}, {@link #MATCH_CATEGORY_PORT},
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700181 * {@link #MATCH_CATEGORY_PATH}, or {@link #MATCH_CATEGORY_TYPE}. Higher
182 * values indicate a better match.
183 */
184 public static final int MATCH_CATEGORY_MASK = 0xfff0000;
185
186 /**
187 * The part of a match constant that applies a quality adjustment to the
188 * basic category of match. The value {@link #MATCH_ADJUSTMENT_NORMAL}
189 * is no adjustment; higher numbers than that improve the quality, while
190 * lower numbers reduce it.
191 */
192 public static final int MATCH_ADJUSTMENT_MASK = 0x000ffff;
193
194 /**
195 * Quality adjustment applied to the category of match that signifies
196 * the default, base value; higher numbers improve the quality while
197 * lower numbers reduce it.
198 */
199 public static final int MATCH_ADJUSTMENT_NORMAL = 0x8000;
200
201 /**
202 * The filter matched an intent that had no data specified.
203 */
204 public static final int MATCH_CATEGORY_EMPTY = 0x0100000;
205 /**
206 * The filter matched an intent with the same data URI scheme.
207 */
208 public static final int MATCH_CATEGORY_SCHEME = 0x0200000;
209 /**
210 * The filter matched an intent with the same data URI scheme and
211 * authority host.
212 */
213 public static final int MATCH_CATEGORY_HOST = 0x0300000;
214 /**
215 * The filter matched an intent with the same data URI scheme and
216 * authority host and port.
217 */
218 public static final int MATCH_CATEGORY_PORT = 0x0400000;
219 /**
220 * The filter matched an intent with the same data URI scheme,
221 * authority, and path.
222 */
223 public static final int MATCH_CATEGORY_PATH = 0x0500000;
224 /**
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700225 * The filter matched an intent with the same data URI scheme and
226 * scheme specific part.
227 */
228 public static final int MATCH_CATEGORY_SCHEME_SPECIFIC_PART = 0x0580000;
229 /**
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700230 * The filter matched an intent with the same data MIME type.
231 */
232 public static final int MATCH_CATEGORY_TYPE = 0x0600000;
233
234 /**
235 * The filter didn't match due to different MIME types.
236 */
237 public static final int NO_MATCH_TYPE = -1;
238 /**
239 * The filter didn't match due to different data URIs.
240 */
241 public static final int NO_MATCH_DATA = -2;
242 /**
243 * The filter didn't match due to different actions.
244 */
245 public static final int NO_MATCH_ACTION = -3;
246 /**
247 * The filter didn't match because it required one or more categories
248 * that were not in the Intent.
249 */
250 public static final int NO_MATCH_CATEGORY = -4;
251
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800252 /**
253 * HTTP scheme.
254 *
255 * @see #addDataScheme(String)
Dianne Hackbornece0f4f2015-06-11 13:29:01 -0700256 * @hide
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800257 */
258 public static final String SCHEME_HTTP = "http";
259 /**
260 * HTTPS scheme.
261 *
262 * @see #addDataScheme(String)
Dianne Hackbornece0f4f2015-06-11 13:29:01 -0700263 * @hide
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800264 */
265 public static final String SCHEME_HTTPS = "https";
266
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700267 private int mPriority;
268 private final ArrayList<String> mActions;
269 private ArrayList<String> mCategories = null;
270 private ArrayList<String> mDataSchemes = null;
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700271 private ArrayList<PatternMatcher> mDataSchemeSpecificParts = null;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700272 private ArrayList<AuthorityEntry> mDataAuthorities = null;
273 private ArrayList<PatternMatcher> mDataPaths = null;
274 private ArrayList<String> mDataTypes = null;
275 private boolean mHasPartialTypes = false;
276
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800277 private static final int STATE_VERIFY_AUTO = 0x00000001;
278 private static final int STATE_NEED_VERIFY = 0x00000010;
279 private static final int STATE_NEED_VERIFY_CHECKED = 0x00000100;
280 private static final int STATE_VERIFIED = 0x00001000;
281
282 private int mVerifyState;
283
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700284 // These functions are the start of more optimized code for managing
285 // the string sets... not yet implemented.
286
287 private static int findStringInSet(String[] set, String string,
288 int[] lengths, int lenPos) {
289 if (set == null) return -1;
290 final int N = lengths[lenPos];
291 for (int i=0; i<N; i++) {
292 if (set[i].equals(string)) return i;
293 }
294 return -1;
295 }
296
297 private static String[] addStringToSet(String[] set, String string,
298 int[] lengths, int lenPos) {
299 if (findStringInSet(set, string, lengths, lenPos) >= 0) return set;
300 if (set == null) {
301 set = new String[2];
302 set[0] = string;
303 lengths[lenPos] = 1;
304 return set;
305 }
306 final int N = lengths[lenPos];
307 if (N < set.length) {
308 set[N] = string;
309 lengths[lenPos] = N+1;
310 return set;
311 }
312
313 String[] newSet = new String[(N*3)/2 + 2];
314 System.arraycopy(set, 0, newSet, 0, N);
315 set = newSet;
316 set[N] = string;
317 lengths[lenPos] = N+1;
318 return set;
319 }
320
321 private static String[] removeStringFromSet(String[] set, String string,
322 int[] lengths, int lenPos) {
323 int pos = findStringInSet(set, string, lengths, lenPos);
324 if (pos < 0) return set;
325 final int N = lengths[lenPos];
326 if (N > (set.length/4)) {
327 int copyLen = N-(pos+1);
328 if (copyLen > 0) {
329 System.arraycopy(set, pos+1, set, pos, copyLen);
330 }
331 set[N-1] = null;
332 lengths[lenPos] = N-1;
333 return set;
334 }
335
336 String[] newSet = new String[set.length/3];
337 if (pos > 0) System.arraycopy(set, 0, newSet, 0, pos);
338 if ((pos+1) < N) System.arraycopy(set, pos+1, newSet, pos, N-(pos+1));
339 return newSet;
340 }
341
342 /**
343 * This exception is thrown when a given MIME type does not have a valid
344 * syntax.
345 */
346 public static class MalformedMimeTypeException extends AndroidException {
347 public MalformedMimeTypeException() {
348 }
349
350 public MalformedMimeTypeException(String name) {
351 super(name);
352 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800353 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700354
355 /**
356 * Create a new IntentFilter instance with a specified action and MIME
357 * type, where you know the MIME type is correctly formatted. This catches
358 * the {@link MalformedMimeTypeException} exception that the constructor
359 * can call and turns it into a runtime exception.
360 *
361 * @param action The action to match, i.e. Intent.ACTION_VIEW.
362 * @param dataType The type to match, i.e. "vnd.android.cursor.dir/person".
363 *
364 * @return A new IntentFilter for the given action and type.
365 *
366 * @see #IntentFilter(String, String)
367 */
368 public static IntentFilter create(String action, String dataType) {
369 try {
370 return new IntentFilter(action, dataType);
371 } catch (MalformedMimeTypeException e) {
372 throw new RuntimeException("Bad MIME type", e);
373 }
374 }
375
376 /**
377 * New empty IntentFilter.
378 */
379 public IntentFilter() {
380 mPriority = 0;
381 mActions = new ArrayList<String>();
382 }
383
384 /**
385 * New IntentFilter that matches a single action with no data. If
386 * no data characteristics are subsequently specified, then the
387 * filter will only match intents that contain no data.
388 *
389 * @param action The action to match, i.e. Intent.ACTION_MAIN.
390 */
391 public IntentFilter(String action) {
392 mPriority = 0;
393 mActions = new ArrayList<String>();
394 addAction(action);
395 }
396
397 /**
398 * New IntentFilter that matches a single action and data type.
399 *
Dianne Hackbornb3cddae2009-04-13 16:54:00 -0700400 * <p><em>Note: MIME type matching in the Android framework is
401 * case-sensitive, unlike formal RFC MIME types. As a result,
402 * you should always write your MIME types with lower case letters,
403 * and any MIME types you receive from outside of Android should be
404 * converted to lower case before supplying them here.</em></p>
405 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700406 * <p>Throws {@link MalformedMimeTypeException} if the given MIME type is
407 * not syntactically correct.
408 *
409 * @param action The action to match, i.e. Intent.ACTION_VIEW.
410 * @param dataType The type to match, i.e. "vnd.android.cursor.dir/person".
411 *
412 */
413 public IntentFilter(String action, String dataType)
414 throws MalformedMimeTypeException {
415 mPriority = 0;
416 mActions = new ArrayList<String>();
Tom Gibara24847f32008-11-04 02:25:42 +0000417 addAction(action);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700418 addDataType(dataType);
419 }
420
421 /**
422 * New IntentFilter containing a copy of an existing filter.
423 *
424 * @param o The original filter to copy.
425 */
426 public IntentFilter(IntentFilter o) {
427 mPriority = o.mPriority;
428 mActions = new ArrayList<String>(o.mActions);
429 if (o.mCategories != null) {
430 mCategories = new ArrayList<String>(o.mCategories);
431 }
432 if (o.mDataTypes != null) {
433 mDataTypes = new ArrayList<String>(o.mDataTypes);
434 }
435 if (o.mDataSchemes != null) {
436 mDataSchemes = new ArrayList<String>(o.mDataSchemes);
437 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700438 if (o.mDataSchemeSpecificParts != null) {
439 mDataSchemeSpecificParts = new ArrayList<PatternMatcher>(o.mDataSchemeSpecificParts);
440 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700441 if (o.mDataAuthorities != null) {
442 mDataAuthorities = new ArrayList<AuthorityEntry>(o.mDataAuthorities);
443 }
444 if (o.mDataPaths != null) {
445 mDataPaths = new ArrayList<PatternMatcher>(o.mDataPaths);
446 }
447 mHasPartialTypes = o.mHasPartialTypes;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800448 mVerifyState = o.mVerifyState;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700449 }
450
451 /**
452 * Modify priority of this filter. The default priority is 0. Positive
453 * values will be before the default, lower values will be after it.
454 * Applications must use a value that is larger than
455 * {@link #SYSTEM_LOW_PRIORITY} and smaller than
456 * {@link #SYSTEM_HIGH_PRIORITY} .
457 *
458 * @param priority The new priority value.
459 *
460 * @see #getPriority
461 * @see #SYSTEM_LOW_PRIORITY
462 * @see #SYSTEM_HIGH_PRIORITY
463 */
464 public final void setPriority(int priority) {
465 mPriority = priority;
466 }
467
468 /**
469 * Return the priority of this filter.
470 *
471 * @return The priority of the filter.
472 *
473 * @see #setPriority
474 */
475 public final int getPriority() {
476 return mPriority;
477 }
478
479 /**
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800480 * Set whether this filter will needs to be automatically verified against its data URIs or not.
481 * The default is false.
482 *
483 * The verification would need to happen only and only if the Intent action is
484 * {@link android.content.Intent#ACTION_VIEW} and the Intent category is
485 * {@link android.content.Intent#CATEGORY_BROWSABLE} and the Intent data scheme
486 * is "http" or "https".
487 *
488 * True means that the filter will need to use its data URIs to be verified.
489 *
490 * @param autoVerify The new autoVerify value.
491 *
492 * @see #getAutoVerify()
493 * @see #addAction(String)
494 * @see #getAction(int)
495 * @see #addCategory(String)
496 * @see #getCategory(int)
497 * @see #addDataScheme(String)
498 * @see #getDataScheme(int)
499 *
500 * @hide
501 */
502 public final void setAutoVerify(boolean autoVerify) {
503 mVerifyState &= ~STATE_VERIFY_AUTO;
504 if (autoVerify) mVerifyState |= STATE_VERIFY_AUTO;
505 }
506
507 /**
508 * Return if this filter will needs to be automatically verified again its data URIs or not.
509 *
510 * @return True if the filter will needs to be automatically verified. False otherwise.
511 *
512 * @see #setAutoVerify(boolean)
513 *
514 * @hide
515 */
516 public final boolean getAutoVerify() {
517 return ((mVerifyState & STATE_VERIFY_AUTO) == 1);
518 }
519
520 /**
Christopher Tate21347442015-06-26 17:23:32 -0700521 * Return if this filter handle all HTTP or HTTPS data URI or not. This is the
522 * core check for whether a given activity qualifies as a "browser".
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -0700523 *
524 * @return True if the filter handle all HTTP or HTTPS data URI. False otherwise.
525 *
Fabrice Di Meglio2d897fb2015-04-09 20:03:23 -0700526 * This will check if:
527 *
528 * - either the Intent category is {@link android.content.Intent#CATEGORY_APP_BROWSER}
529 * - either the Intent action is {@link android.content.Intent#ACTION_VIEW} and
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -0700530 * the Intent category is {@link android.content.Intent#CATEGORY_BROWSABLE} and the Intent
531 * data scheme is "http" or "https" and that there is no specific host defined.
532 *
533 * @hide
534 */
535 public final boolean handleAllWebDataURI() {
Fabrice Di Meglio2d897fb2015-04-09 20:03:23 -0700536 return hasCategory(Intent.CATEGORY_APP_BROWSER) ||
Christopher Tate21347442015-06-26 17:23:32 -0700537 (handlesWebUris(false) && countDataAuthorities() == 0);
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -0700538 }
539
540 /**
Christopher Tate21347442015-06-26 17:23:32 -0700541 * Return if this filter handles HTTP or HTTPS data URIs.
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -0700542 *
Christopher Tate413020a2015-06-23 19:23:46 -0700543 * @return True if the filter handles ACTION_VIEW/CATEGORY_BROWSABLE,
Christopher Tate21347442015-06-26 17:23:32 -0700544 * has at least one HTTP or HTTPS data URI pattern defined, and optionally
545 * does not define any non-http/https data URI patterns.
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -0700546 *
547 * This will check if if the Intent action is {@link android.content.Intent#ACTION_VIEW} and
548 * the Intent category is {@link android.content.Intent#CATEGORY_BROWSABLE} and the Intent
549 * data scheme is "http" or "https".
550 *
Christopher Tate21347442015-06-26 17:23:32 -0700551 * @param onlyWebSchemes When true, requires that the intent filter declare
552 * that it handles *only* http: or https: schemes. This is a requirement for
553 * the intent filter's domain linkage being verifiable.
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -0700554 * @hide
555 */
Christopher Tate21347442015-06-26 17:23:32 -0700556 public final boolean handlesWebUris(boolean onlyWebSchemes) {
Christopher Tate413020a2015-06-23 19:23:46 -0700557 // Require ACTION_VIEW, CATEGORY_BROWSEABLE, and at least one scheme
558 if (!hasAction(Intent.ACTION_VIEW)
559 || !hasCategory(Intent.CATEGORY_BROWSABLE)
560 || mDataSchemes == null
561 || mDataSchemes.size() == 0) {
562 return false;
563 }
564
565 // Now allow only the schemes "http" and "https"
566 final int N = mDataSchemes.size();
567 for (int i = 0; i < N; i++) {
568 final String scheme = mDataSchemes.get(i);
Christopher Tate21347442015-06-26 17:23:32 -0700569 final boolean isWebScheme =
570 SCHEME_HTTP.equals(scheme) || SCHEME_HTTPS.equals(scheme);
571 if (onlyWebSchemes) {
572 // If we're specifically trying to ensure that there are no non-web schemes
573 // declared in this filter, then if we ever see a non-http/https scheme then
574 // we know it's a failure.
575 if (!isWebScheme) {
576 return false;
577 }
578 } else {
579 // If we see any http/https scheme declaration in this case then the
580 // filter matches what we're looking for.
581 if (isWebScheme) {
582 return true;
583 }
Christopher Tate413020a2015-06-23 19:23:46 -0700584 }
585 }
586
Christopher Tate21347442015-06-26 17:23:32 -0700587 // We get here if:
588 // 1) onlyWebSchemes and no non-web schemes were found, i.e success; or
589 // 2) !onlyWebSchemes and no http/https schemes were found, i.e. failure.
590 return onlyWebSchemes;
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -0700591 }
592
593 /**
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800594 * Return if this filter needs to be automatically verified again its data URIs or not.
595 *
596 * @return True if the filter needs to be automatically verified. False otherwise.
597 *
598 * This will check if if the Intent action is {@link android.content.Intent#ACTION_VIEW} and
599 * the Intent category is {@link android.content.Intent#CATEGORY_BROWSABLE} and the Intent
600 * data scheme is "http" or "https".
601 *
602 * @see #setAutoVerify(boolean)
603 *
604 * @hide
605 */
606 public final boolean needsVerification() {
Christopher Tate21347442015-06-26 17:23:32 -0700607 return getAutoVerify() && handlesWebUris(true);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800608 }
609
610 /**
611 * Return if this filter has been verified
612 *
613 * @return true if the filter has been verified or if autoVerify is false.
614 *
615 * @hide
616 */
617 public final boolean isVerified() {
618 if ((mVerifyState & STATE_NEED_VERIFY_CHECKED) == STATE_NEED_VERIFY_CHECKED) {
619 return ((mVerifyState & STATE_NEED_VERIFY) == STATE_NEED_VERIFY);
620 }
621 return false;
622 }
623
624 /**
625 * Set if this filter has been verified
626 *
627 * @param verified true if this filter has been verified. False otherwise.
628 *
629 * @hide
630 */
631 public void setVerified(boolean verified) {
632 mVerifyState |= STATE_NEED_VERIFY_CHECKED;
633 mVerifyState &= ~STATE_VERIFIED;
634 if (verified) mVerifyState |= STATE_VERIFIED;
635 }
636
637 /**
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700638 * Add a new Intent action to match against. If any actions are included
639 * in the filter, then an Intent's action must be one of those values for
640 * it to match. If no actions are included, the Intent action is ignored.
641 *
642 * @param action Name of the action to match, i.e. Intent.ACTION_VIEW.
643 */
644 public final void addAction(String action) {
645 if (!mActions.contains(action)) {
646 mActions.add(action.intern());
647 }
648 }
649
650 /**
651 * Return the number of actions in the filter.
652 */
653 public final int countActions() {
654 return mActions.size();
655 }
656
657 /**
658 * Return an action in the filter.
659 */
660 public final String getAction(int index) {
661 return mActions.get(index);
662 }
663
664 /**
665 * Is the given action included in the filter? Note that if the filter
666 * does not include any actions, false will <em>always</em> be returned.
667 *
668 * @param action The action to look for.
669 *
670 * @return True if the action is explicitly mentioned in the filter.
671 */
672 public final boolean hasAction(String action) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800673 return action != null && mActions.contains(action);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700674 }
675
676 /**
677 * Match this filter against an Intent's action. If the filter does not
678 * specify any actions, the match will always fail.
679 *
680 * @param action The desired action to look for.
681 *
Jeff Brown2c376fc2011-01-28 17:34:01 -0800682 * @return True if the action is listed in the filter.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700683 */
684 public final boolean matchAction(String action) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800685 return hasAction(action);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700686 }
687
688 /**
689 * Return an iterator over the filter's actions. If there are no actions,
690 * returns null.
691 */
692 public final Iterator<String> actionsIterator() {
693 return mActions != null ? mActions.iterator() : null;
694 }
695
696 /**
697 * Add a new Intent data type to match against. If any types are
698 * included in the filter, then an Intent's data must be <em>either</em>
699 * one of these types <em>or</em> a matching scheme. If no data types
700 * are included, then an Intent will only match if it specifies no data.
701 *
Dianne Hackbornb3cddae2009-04-13 16:54:00 -0700702 * <p><em>Note: MIME type matching in the Android framework is
703 * case-sensitive, unlike formal RFC MIME types. As a result,
704 * you should always write your MIME types with lower case letters,
705 * and any MIME types you receive from outside of Android should be
706 * converted to lower case before supplying them here.</em></p>
707 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700708 * <p>Throws {@link MalformedMimeTypeException} if the given MIME type is
709 * not syntactically correct.
710 *
711 * @param type Name of the data type to match, i.e. "vnd.android.cursor.dir/person".
712 *
713 * @see #matchData
714 */
715 public final void addDataType(String type)
716 throws MalformedMimeTypeException {
717 final int slashpos = type.indexOf('/');
718 final int typelen = type.length();
719 if (slashpos > 0 && typelen >= slashpos+2) {
720 if (mDataTypes == null) mDataTypes = new ArrayList<String>();
721 if (typelen == slashpos+2 && type.charAt(slashpos+1) == '*') {
722 String str = type.substring(0, slashpos);
723 if (!mDataTypes.contains(str)) {
724 mDataTypes.add(str.intern());
725 }
726 mHasPartialTypes = true;
727 } else {
728 if (!mDataTypes.contains(type)) {
729 mDataTypes.add(type.intern());
730 }
731 }
732 return;
733 }
734
735 throw new MalformedMimeTypeException(type);
736 }
737
738 /**
739 * Is the given data type included in the filter? Note that if the filter
740 * does not include any type, false will <em>always</em> be returned.
741 *
742 * @param type The data type to look for.
743 *
744 * @return True if the type is explicitly mentioned in the filter.
745 */
746 public final boolean hasDataType(String type) {
747 return mDataTypes != null && findMimeType(type);
748 }
749
Dianne Hackbornf2ac2762014-08-16 11:44:40 -0700750 /** @hide */
751 public final boolean hasExactDataType(String type) {
752 return mDataTypes != null && mDataTypes.contains(type);
753 }
754
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700755 /**
756 * Return the number of data types in the filter.
757 */
758 public final int countDataTypes() {
759 return mDataTypes != null ? mDataTypes.size() : 0;
760 }
761
762 /**
763 * Return a data type in the filter.
764 */
765 public final String getDataType(int index) {
766 return mDataTypes.get(index);
767 }
768
769 /**
770 * Return an iterator over the filter's data types.
771 */
772 public final Iterator<String> typesIterator() {
773 return mDataTypes != null ? mDataTypes.iterator() : null;
774 }
775
776 /**
777 * Add a new Intent data scheme to match against. If any schemes are
778 * included in the filter, then an Intent's data must be <em>either</em>
779 * one of these schemes <em>or</em> a matching data type. If no schemes
780 * are included, then an Intent will match only if it includes no data.
781 *
Dianne Hackbornb3cddae2009-04-13 16:54:00 -0700782 * <p><em>Note: scheme matching in the Android framework is
783 * case-sensitive, unlike formal RFC schemes. As a result,
784 * you should always write your schemes with lower case letters,
785 * and any schemes you receive from outside of Android should be
786 * converted to lower case before supplying them here.</em></p>
787 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700788 * @param scheme Name of the scheme to match, i.e. "http".
789 *
790 * @see #matchData
791 */
792 public final void addDataScheme(String scheme) {
793 if (mDataSchemes == null) mDataSchemes = new ArrayList<String>();
794 if (!mDataSchemes.contains(scheme)) {
795 mDataSchemes.add(scheme.intern());
796 }
797 }
798
799 /**
800 * Return the number of data schemes in the filter.
801 */
802 public final int countDataSchemes() {
803 return mDataSchemes != null ? mDataSchemes.size() : 0;
804 }
805
806 /**
807 * Return a data scheme in the filter.
808 */
809 public final String getDataScheme(int index) {
810 return mDataSchemes.get(index);
811 }
812
813 /**
814 * Is the given data scheme included in the filter? Note that if the
815 * filter does not include any scheme, false will <em>always</em> be
816 * returned.
817 *
818 * @param scheme The data scheme to look for.
819 *
820 * @return True if the scheme is explicitly mentioned in the filter.
821 */
822 public final boolean hasDataScheme(String scheme) {
823 return mDataSchemes != null && mDataSchemes.contains(scheme);
824 }
825
826 /**
827 * Return an iterator over the filter's data schemes.
828 */
829 public final Iterator<String> schemesIterator() {
830 return mDataSchemes != null ? mDataSchemes.iterator() : null;
831 }
832
833 /**
834 * This is an entry for a single authority in the Iterator returned by
835 * {@link #authoritiesIterator()}.
836 */
837 public final static class AuthorityEntry {
838 private final String mOrigHost;
839 private final String mHost;
840 private final boolean mWild;
841 private final int mPort;
842
843 public AuthorityEntry(String host, String port) {
844 mOrigHost = host;
845 mWild = host.length() > 0 && host.charAt(0) == '*';
846 mHost = mWild ? host.substring(1).intern() : host;
847 mPort = port != null ? Integer.parseInt(port) : -1;
848 }
849
850 AuthorityEntry(Parcel src) {
851 mOrigHost = src.readString();
852 mHost = src.readString();
853 mWild = src.readInt() != 0;
854 mPort = src.readInt();
855 }
856
857 void writeToParcel(Parcel dest) {
858 dest.writeString(mOrigHost);
859 dest.writeString(mHost);
860 dest.writeInt(mWild ? 1 : 0);
861 dest.writeInt(mPort);
862 }
863
864 public String getHost() {
865 return mOrigHost;
866 }
867
868 public int getPort() {
869 return mPort;
870 }
871
Dianne Hackbornf2ac2762014-08-16 11:44:40 -0700872 /** @hide */
873 public boolean match(AuthorityEntry other) {
874 if (mWild != other.mWild) {
875 return false;
876 }
877 if (!mHost.equals(other.mHost)) {
878 return false;
879 }
880 if (mPort != other.mPort) {
881 return false;
882 }
883 return true;
884 }
885
Todd Kennedy5585dd92016-03-29 13:07:01 -0700886 @Override
887 public boolean equals(Object obj) {
888 if (obj instanceof AuthorityEntry) {
889 final AuthorityEntry other = (AuthorityEntry)obj;
890 return match(other);
891 }
892 return false;
893 }
894
Dianne Hackbornb3cddae2009-04-13 16:54:00 -0700895 /**
896 * Determine whether this AuthorityEntry matches the given data Uri.
897 * <em>Note that this comparison is case-sensitive, unlike formal
898 * RFC host names. You thus should always normalize to lower-case.</em>
899 *
900 * @param data The Uri to match.
901 * @return Returns either {@link IntentFilter#NO_MATCH_DATA},
902 * {@link IntentFilter#MATCH_CATEGORY_PORT}, or
903 * {@link IntentFilter#MATCH_CATEGORY_HOST}.
904 */
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700905 public int match(Uri data) {
906 String host = data.getHost();
907 if (host == null) {
908 return NO_MATCH_DATA;
909 }
Joe Onorato43a17652011-04-06 19:22:23 -0700910 if (false) Log.v("IntentFilter",
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700911 "Match host " + host + ": " + mHost);
912 if (mWild) {
913 if (host.length() < mHost.length()) {
914 return NO_MATCH_DATA;
915 }
916 host = host.substring(host.length()-mHost.length());
917 }
918 if (host.compareToIgnoreCase(mHost) != 0) {
919 return NO_MATCH_DATA;
920 }
921 if (mPort >= 0) {
922 if (mPort != data.getPort()) {
923 return NO_MATCH_DATA;
924 }
925 return MATCH_CATEGORY_PORT;
926 }
927 return MATCH_CATEGORY_HOST;
928 }
Todd Kennedy5585dd92016-03-29 13:07:01 -0700929 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700930
931 /**
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700932 * Add a new Intent data "scheme specific part" to match against. The filter must
933 * include one or more schemes (via {@link #addDataScheme}) for the
934 * scheme specific part to be considered. If any scheme specific parts are
935 * included in the filter, then an Intent's data must match one of
936 * them. If no scheme specific parts are included, then only the scheme must match.
937 *
Dianne Hackbornebc15ef2013-10-09 17:36:57 -0700938 * <p>The "scheme specific part" that this matches against is the string returned
939 * by {@link android.net.Uri#getSchemeSpecificPart() Uri.getSchemeSpecificPart}.
940 * For Uris that contain a path, this kind of matching is not generally of interest,
941 * since {@link #addDataAuthority(String, String)} and
942 * {@link #addDataPath(String, int)} can provide a better mechanism for matching
943 * them. However, for Uris that do not contain a path, the authority and path
944 * are empty, so this is the only way to match against the non-scheme part.</p>
945 *
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700946 * @param ssp Either a raw string that must exactly match the scheme specific part
947 * path, or a simple pattern, depending on <var>type</var>.
948 * @param type Determines how <var>ssp</var> will be compared to
949 * determine a match: either {@link PatternMatcher#PATTERN_LITERAL},
950 * {@link PatternMatcher#PATTERN_PREFIX}, or
951 * {@link PatternMatcher#PATTERN_SIMPLE_GLOB}.
952 *
953 * @see #matchData
954 * @see #addDataScheme
955 */
956 public final void addDataSchemeSpecificPart(String ssp, int type) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -0700957 addDataSchemeSpecificPart(new PatternMatcher(ssp, type));
958 }
959
960 /** @hide */
961 public final void addDataSchemeSpecificPart(PatternMatcher ssp) {
Dianne Hackborn439e9bc82013-06-12 19:13:49 -0700962 if (mDataSchemeSpecificParts == null) {
963 mDataSchemeSpecificParts = new ArrayList<PatternMatcher>();
964 }
Dianne Hackbornb09491f2013-07-22 15:30:11 -0700965 mDataSchemeSpecificParts.add(ssp);
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700966 }
967
968 /**
969 * Return the number of data scheme specific parts in the filter.
970 */
971 public final int countDataSchemeSpecificParts() {
972 return mDataSchemeSpecificParts != null ? mDataSchemeSpecificParts.size() : 0;
973 }
974
975 /**
976 * Return a data scheme specific part in the filter.
977 */
978 public final PatternMatcher getDataSchemeSpecificPart(int index) {
979 return mDataSchemeSpecificParts.get(index);
980 }
981
982 /**
983 * Is the given data scheme specific part included in the filter? Note that if the
984 * filter does not include any scheme specific parts, false will <em>always</em> be
985 * returned.
986 *
987 * @param data The scheme specific part that is being looked for.
988 *
989 * @return Returns true if the data string matches a scheme specific part listed in the
990 * filter.
991 */
992 public final boolean hasDataSchemeSpecificPart(String data) {
993 if (mDataSchemeSpecificParts == null) {
994 return false;
995 }
996 final int numDataSchemeSpecificParts = mDataSchemeSpecificParts.size();
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700997 for (int i = 0; i < numDataSchemeSpecificParts; i++) {
998 final PatternMatcher pe = mDataSchemeSpecificParts.get(i);
999 if (pe.match(data)) {
1000 return true;
1001 }
1002 }
1003 return false;
1004 }
1005
Dianne Hackbornf2ac2762014-08-16 11:44:40 -07001006 /** @hide */
1007 public final boolean hasDataSchemeSpecificPart(PatternMatcher ssp) {
1008 if (mDataSchemeSpecificParts == null) {
1009 return false;
1010 }
1011 final int numDataSchemeSpecificParts = mDataSchemeSpecificParts.size();
1012 for (int i = 0; i < numDataSchemeSpecificParts; i++) {
1013 final PatternMatcher pe = mDataSchemeSpecificParts.get(i);
1014 if (pe.getType() == ssp.getType() && pe.getPath().equals(ssp.getPath())) {
1015 return true;
1016 }
1017 }
1018 return false;
1019 }
1020
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07001021 /**
1022 * Return an iterator over the filter's data scheme specific parts.
1023 */
1024 public final Iterator<PatternMatcher> schemeSpecificPartsIterator() {
1025 return mDataSchemeSpecificParts != null ? mDataSchemeSpecificParts.iterator() : null;
1026 }
1027
1028 /**
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001029 * Add a new Intent data authority to match against. The filter must
1030 * include one or more schemes (via {@link #addDataScheme}) for the
1031 * authority to be considered. If any authorities are
1032 * included in the filter, then an Intent's data must match one of
1033 * them. If no authorities are included, then only the scheme must match.
1034 *
Dianne Hackbornb3cddae2009-04-13 16:54:00 -07001035 * <p><em>Note: host name in the Android framework is
1036 * case-sensitive, unlike formal RFC host names. As a result,
1037 * you should always write your host names with lower case letters,
1038 * and any host names you receive from outside of Android should be
1039 * converted to lower case before supplying them here.</em></p>
1040 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001041 * @param host The host part of the authority to match. May start with a
1042 * single '*' to wildcard the front of the host name.
1043 * @param port Optional port part of the authority to match. If null, any
1044 * port is allowed.
1045 *
1046 * @see #matchData
1047 * @see #addDataScheme
1048 */
1049 public final void addDataAuthority(String host, String port) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07001050 if (port != null) port = port.intern();
1051 addDataAuthority(new AuthorityEntry(host.intern(), port));
1052 }
1053
1054 /** @hide */
1055 public final void addDataAuthority(AuthorityEntry ent) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001056 if (mDataAuthorities == null) mDataAuthorities =
1057 new ArrayList<AuthorityEntry>();
Dianne Hackbornb09491f2013-07-22 15:30:11 -07001058 mDataAuthorities.add(ent);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001059 }
1060
1061 /**
1062 * Return the number of data authorities in the filter.
1063 */
1064 public final int countDataAuthorities() {
1065 return mDataAuthorities != null ? mDataAuthorities.size() : 0;
1066 }
1067
1068 /**
1069 * Return a data authority in the filter.
1070 */
1071 public final AuthorityEntry getDataAuthority(int index) {
1072 return mDataAuthorities.get(index);
1073 }
1074
1075 /**
1076 * Is the given data authority included in the filter? Note that if the
1077 * filter does not include any authorities, false will <em>always</em> be
1078 * returned.
1079 *
1080 * @param data The data whose authority is being looked for.
1081 *
1082 * @return Returns true if the data string matches an authority listed in the
1083 * filter.
1084 */
1085 public final boolean hasDataAuthority(Uri data) {
1086 return matchDataAuthority(data) >= 0;
1087 }
1088
Dianne Hackbornf2ac2762014-08-16 11:44:40 -07001089 /** @hide */
1090 public final boolean hasDataAuthority(AuthorityEntry auth) {
1091 if (mDataAuthorities == null) {
1092 return false;
1093 }
1094 final int numDataAuthorities = mDataAuthorities.size();
1095 for (int i = 0; i < numDataAuthorities; i++) {
1096 if (mDataAuthorities.get(i).match(auth)) {
1097 return true;
1098 }
1099 }
1100 return false;
1101 }
1102
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001103 /**
1104 * Return an iterator over the filter's data authorities.
1105 */
1106 public final Iterator<AuthorityEntry> authoritiesIterator() {
1107 return mDataAuthorities != null ? mDataAuthorities.iterator() : null;
1108 }
1109
1110 /**
Gilles Debunne37051cd2011-05-25 16:27:13 -07001111 * Add a new Intent data path to match against. The filter must
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001112 * include one or more schemes (via {@link #addDataScheme}) <em>and</em>
1113 * one or more authorities (via {@link #addDataAuthority}) for the
1114 * path to be considered. If any paths are
1115 * included in the filter, then an Intent's data must match one of
1116 * them. If no paths are included, then only the scheme/authority must
1117 * match.
1118 *
1119 * <p>The path given here can either be a literal that must directly
1120 * match or match against a prefix, or it can be a simple globbing pattern.
1121 * If the latter, you can use '*' anywhere in the pattern to match zero
1122 * or more instances of the previous character, '.' as a wildcard to match
1123 * any character, and '\' to escape the next character.
1124 *
1125 * @param path Either a raw string that must exactly match the file
1126 * path, or a simple pattern, depending on <var>type</var>.
1127 * @param type Determines how <var>path</var> will be compared to
1128 * determine a match: either {@link PatternMatcher#PATTERN_LITERAL},
1129 * {@link PatternMatcher#PATTERN_PREFIX}, or
1130 * {@link PatternMatcher#PATTERN_SIMPLE_GLOB}.
1131 *
1132 * @see #matchData
1133 * @see #addDataScheme
1134 * @see #addDataAuthority
1135 */
1136 public final void addDataPath(String path, int type) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07001137 addDataPath(new PatternMatcher(path.intern(), type));
1138 }
1139
1140 /** @hide */
1141 public final void addDataPath(PatternMatcher path) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001142 if (mDataPaths == null) mDataPaths = new ArrayList<PatternMatcher>();
Dianne Hackbornb09491f2013-07-22 15:30:11 -07001143 mDataPaths.add(path);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001144 }
1145
1146 /**
1147 * Return the number of data paths in the filter.
1148 */
1149 public final int countDataPaths() {
1150 return mDataPaths != null ? mDataPaths.size() : 0;
1151 }
1152
1153 /**
1154 * Return a data path in the filter.
1155 */
1156 public final PatternMatcher getDataPath(int index) {
1157 return mDataPaths.get(index);
1158 }
1159
1160 /**
1161 * Is the given data path included in the filter? Note that if the
1162 * filter does not include any paths, false will <em>always</em> be
1163 * returned.
1164 *
1165 * @param data The data path to look for. This is without the scheme
1166 * prefix.
1167 *
1168 * @return True if the data string matches a path listed in the
1169 * filter.
1170 */
1171 public final boolean hasDataPath(String data) {
1172 if (mDataPaths == null) {
1173 return false;
1174 }
Jeff Brown2c376fc2011-01-28 17:34:01 -08001175 final int numDataPaths = mDataPaths.size();
1176 for (int i = 0; i < numDataPaths; i++) {
1177 final PatternMatcher pe = mDataPaths.get(i);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001178 if (pe.match(data)) {
1179 return true;
1180 }
1181 }
1182 return false;
1183 }
1184
Dianne Hackbornf2ac2762014-08-16 11:44:40 -07001185 /** @hide */
1186 public final boolean hasDataPath(PatternMatcher path) {
1187 if (mDataPaths == null) {
1188 return false;
1189 }
1190 final int numDataPaths = mDataPaths.size();
1191 for (int i = 0; i < numDataPaths; i++) {
1192 final PatternMatcher pe = mDataPaths.get(i);
1193 if (pe.getType() == path.getType() && pe.getPath().equals(path.getPath())) {
1194 return true;
1195 }
1196 }
1197 return false;
1198 }
1199
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001200 /**
1201 * Return an iterator over the filter's data paths.
1202 */
1203 public final Iterator<PatternMatcher> pathsIterator() {
1204 return mDataPaths != null ? mDataPaths.iterator() : null;
1205 }
1206
1207 /**
1208 * Match this intent filter against the given Intent data. This ignores
1209 * the data scheme -- unlike {@link #matchData}, the authority will match
1210 * regardless of whether there is a matching scheme.
1211 *
1212 * @param data The data whose authority is being looked for.
1213 *
1214 * @return Returns either {@link #MATCH_CATEGORY_HOST},
1215 * {@link #MATCH_CATEGORY_PORT}, {@link #NO_MATCH_DATA}.
1216 */
1217 public final int matchDataAuthority(Uri data) {
Dianne Hackborn9dc6c612015-07-28 17:30:59 -07001218 if (mDataAuthorities == null || data == null) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001219 return NO_MATCH_DATA;
1220 }
Jeff Brown2c376fc2011-01-28 17:34:01 -08001221 final int numDataAuthorities = mDataAuthorities.size();
1222 for (int i = 0; i < numDataAuthorities; i++) {
1223 final AuthorityEntry ae = mDataAuthorities.get(i);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001224 int match = ae.match(data);
1225 if (match >= 0) {
1226 return match;
1227 }
1228 }
1229 return NO_MATCH_DATA;
1230 }
1231
1232 /**
1233 * Match this filter against an Intent's data (type, scheme and path). If
1234 * the filter does not specify any types and does not specify any
1235 * schemes/paths, the match will only succeed if the intent does not
Dianne Hackborn0ea920d2013-10-11 09:40:18 -07001236 * also specify a type or data. If the filter does not specify any schemes,
1237 * it will implicitly match intents with no scheme, or the schemes "content:"
1238 * or "file:" (basically performing a MIME-type only match). If the filter
1239 * does not specify any MIME types, the Intent also must not specify a MIME
1240 * type.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001241 *
Dianne Hackbornb3cddae2009-04-13 16:54:00 -07001242 * <p>Be aware that to match against an authority, you must also specify a base
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001243 * scheme the authority is in. To match against a data path, both a scheme
1244 * and authority must be specified. If the filter does not specify any
1245 * types or schemes that it matches against, it is considered to be empty
1246 * (any authority or data path given is ignored, as if it were empty as
1247 * well).
1248 *
Dianne Hackbornb3cddae2009-04-13 16:54:00 -07001249 * <p><em>Note: MIME type, Uri scheme, and host name matching in the
1250 * Android framework is case-sensitive, unlike the formal RFC definitions.
1251 * As a result, you should always write these elements with lower case letters,
1252 * and normalize any MIME types or Uris you receive from
1253 * outside of Android to ensure these elements are lower case before
1254 * supplying them here.</em></p>
1255 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001256 * @param type The desired data type to look for, as returned by
1257 * Intent.resolveType().
1258 * @param scheme The desired data scheme to look for, as returned by
1259 * Intent.getScheme().
1260 * @param data The full data string to match against, as supplied in
1261 * Intent.data.
1262 *
1263 * @return Returns either a valid match constant (a combination of
1264 * {@link #MATCH_CATEGORY_MASK} and {@link #MATCH_ADJUSTMENT_MASK}),
1265 * or one of the error codes {@link #NO_MATCH_TYPE} if the type didn't match
1266 * or {@link #NO_MATCH_DATA} if the scheme/path didn't match.
1267 *
1268 * @see #match
1269 */
1270 public final int matchData(String type, String scheme, Uri data) {
1271 final ArrayList<String> types = mDataTypes;
1272 final ArrayList<String> schemes = mDataSchemes;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001273
1274 int match = MATCH_CATEGORY_EMPTY;
1275
1276 if (types == null && schemes == null) {
1277 return ((type == null && data == null)
1278 ? (MATCH_CATEGORY_EMPTY+MATCH_ADJUSTMENT_NORMAL) : NO_MATCH_DATA);
1279 }
1280
1281 if (schemes != null) {
1282 if (schemes.contains(scheme != null ? scheme : "")) {
1283 match = MATCH_CATEGORY_SCHEME;
1284 } else {
1285 return NO_MATCH_DATA;
1286 }
1287
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07001288 final ArrayList<PatternMatcher> schemeSpecificParts = mDataSchemeSpecificParts;
Dianne Hackborn9dc6c612015-07-28 17:30:59 -07001289 if (schemeSpecificParts != null && data != null) {
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07001290 match = hasDataSchemeSpecificPart(data.getSchemeSpecificPart())
1291 ? MATCH_CATEGORY_SCHEME_SPECIFIC_PART : NO_MATCH_DATA;
1292 }
1293 if (match != MATCH_CATEGORY_SCHEME_SPECIFIC_PART) {
1294 // If there isn't any matching ssp, we need to match an authority.
1295 final ArrayList<AuthorityEntry> authorities = mDataAuthorities;
1296 if (authorities != null) {
1297 int authMatch = matchDataAuthority(data);
1298 if (authMatch >= 0) {
1299 final ArrayList<PatternMatcher> paths = mDataPaths;
1300 if (paths == null) {
1301 match = authMatch;
1302 } else if (hasDataPath(data.getPath())) {
1303 match = MATCH_CATEGORY_PATH;
1304 } else {
1305 return NO_MATCH_DATA;
1306 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001307 } else {
1308 return NO_MATCH_DATA;
1309 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001310 }
1311 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07001312 // If neither an ssp nor an authority matched, we're done.
1313 if (match == NO_MATCH_DATA) {
1314 return NO_MATCH_DATA;
1315 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001316 } else {
1317 // Special case: match either an Intent with no data URI,
1318 // or with a scheme: URI. This is to give a convenience for
1319 // the common case where you want to deal with data in a
1320 // content provider, which is done by type, and we don't want
1321 // to force everyone to say they handle content: or file: URIs.
1322 if (scheme != null && !"".equals(scheme)
1323 && !"content".equals(scheme)
1324 && !"file".equals(scheme)) {
1325 return NO_MATCH_DATA;
1326 }
1327 }
1328
1329 if (types != null) {
1330 if (findMimeType(type)) {
1331 match = MATCH_CATEGORY_TYPE;
1332 } else {
1333 return NO_MATCH_TYPE;
1334 }
1335 } else {
1336 // If no MIME types are specified, then we will only match against
1337 // an Intent that does not have a MIME type.
1338 if (type != null) {
1339 return NO_MATCH_TYPE;
1340 }
1341 }
1342
1343 return match + MATCH_ADJUSTMENT_NORMAL;
1344 }
1345
1346 /**
1347 * Add a new Intent category to match against. The semantics of
1348 * categories is the opposite of actions -- an Intent includes the
1349 * categories that it requires, all of which must be included in the
1350 * filter in order to match. In other words, adding a category to the
1351 * filter has no impact on matching unless that category is specified in
1352 * the intent.
1353 *
1354 * @param category Name of category to match, i.e. Intent.CATEGORY_EMBED.
1355 */
1356 public final void addCategory(String category) {
1357 if (mCategories == null) mCategories = new ArrayList<String>();
1358 if (!mCategories.contains(category)) {
1359 mCategories.add(category.intern());
1360 }
1361 }
1362
1363 /**
1364 * Return the number of categories in the filter.
1365 */
1366 public final int countCategories() {
1367 return mCategories != null ? mCategories.size() : 0;
1368 }
1369
1370 /**
1371 * Return a category in the filter.
1372 */
1373 public final String getCategory(int index) {
1374 return mCategories.get(index);
1375 }
1376
1377 /**
1378 * Is the given category included in the filter?
1379 *
1380 * @param category The category that the filter supports.
1381 *
1382 * @return True if the category is explicitly mentioned in the filter.
1383 */
1384 public final boolean hasCategory(String category) {
1385 return mCategories != null && mCategories.contains(category);
1386 }
1387
1388 /**
1389 * Return an iterator over the filter's categories.
Kenny Rootd2d29252011-08-08 11:27:57 -07001390 *
1391 * @return Iterator if this filter has categories or {@code null} if none.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001392 */
1393 public final Iterator<String> categoriesIterator() {
1394 return mCategories != null ? mCategories.iterator() : null;
1395 }
1396
1397 /**
1398 * Match this filter against an Intent's categories. Each category in
1399 * the Intent must be specified by the filter; if any are not in the
1400 * filter, the match fails.
1401 *
1402 * @param categories The categories included in the intent, as returned by
1403 * Intent.getCategories().
1404 *
1405 * @return If all categories match (success), null; else the name of the
1406 * first category that didn't match.
1407 */
1408 public final String matchCategories(Set<String> categories) {
1409 if (categories == null) {
1410 return null;
1411 }
1412
1413 Iterator<String> it = categories.iterator();
1414
1415 if (mCategories == null) {
1416 return it.hasNext() ? it.next() : null;
1417 }
1418
1419 while (it.hasNext()) {
1420 final String category = it.next();
1421 if (!mCategories.contains(category)) {
1422 return category;
1423 }
1424 }
1425
1426 return null;
1427 }
1428
1429 /**
1430 * Test whether this filter matches the given <var>intent</var>.
1431 *
1432 * @param intent The Intent to compare against.
1433 * @param resolve If true, the intent's type will be resolved by calling
1434 * Intent.resolveType(); otherwise a simple match against
1435 * Intent.type will be performed.
1436 * @param logTag Tag to use in debugging messages.
1437 *
1438 * @return Returns either a valid match constant (a combination of
1439 * {@link #MATCH_CATEGORY_MASK} and {@link #MATCH_ADJUSTMENT_MASK}),
1440 * or one of the error codes {@link #NO_MATCH_TYPE} if the type didn't match,
1441 * {@link #NO_MATCH_DATA} if the scheme/path didn't match,
Dianne Hackborndc894042014-08-12 18:59:28 -07001442 * {@link #NO_MATCH_ACTION} if the action didn't match, or
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001443 * {@link #NO_MATCH_CATEGORY} if one or more categories didn't match.
1444 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001445 * @see #match(String, String, String, android.net.Uri , Set, String)
1446 */
1447 public final int match(ContentResolver resolver, Intent intent,
1448 boolean resolve, String logTag) {
1449 String type = resolve ? intent.resolveType(resolver) : intent.getType();
1450 return match(intent.getAction(), type, intent.getScheme(),
1451 intent.getData(), intent.getCategories(), logTag);
1452 }
1453
1454 /**
1455 * Test whether this filter matches the given intent data. A match is
1456 * only successful if the actions and categories in the Intent match
1457 * against the filter, as described in {@link IntentFilter}; in that case,
1458 * the match result returned will be as per {@link #matchData}.
1459 *
1460 * @param action The intent action to match against (Intent.getAction).
1461 * @param type The intent type to match against (Intent.resolveType()).
1462 * @param scheme The data scheme to match against (Intent.getScheme()).
1463 * @param data The data URI to match against (Intent.getData()).
1464 * @param categories The categories to match against
1465 * (Intent.getCategories()).
1466 * @param logTag Tag to use in debugging messages.
1467 *
1468 * @return Returns either a valid match constant (a combination of
1469 * {@link #MATCH_CATEGORY_MASK} and {@link #MATCH_ADJUSTMENT_MASK}),
1470 * or one of the error codes {@link #NO_MATCH_TYPE} if the type didn't match,
1471 * {@link #NO_MATCH_DATA} if the scheme/path didn't match,
Dianne Hackborndc894042014-08-12 18:59:28 -07001472 * {@link #NO_MATCH_ACTION} if the action didn't match, or
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001473 * {@link #NO_MATCH_CATEGORY} if one or more categories didn't match.
1474 *
1475 * @see #matchData
1476 * @see Intent#getAction
1477 * @see Intent#resolveType
1478 * @see Intent#getScheme
1479 * @see Intent#getData
1480 * @see Intent#getCategories
1481 */
1482 public final int match(String action, String type, String scheme,
1483 Uri data, Set<String> categories, String logTag) {
Jeff Brown239f77d2011-02-26 16:03:48 -08001484 if (action != null && !matchAction(action)) {
Joe Onorato43a17652011-04-06 19:22:23 -07001485 if (false) Log.v(
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001486 logTag, "No matching action " + action + " for " + this);
1487 return NO_MATCH_ACTION;
1488 }
1489
1490 int dataMatch = matchData(type, scheme, data);
1491 if (dataMatch < 0) {
Joe Onorato43a17652011-04-06 19:22:23 -07001492 if (false) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001493 if (dataMatch == NO_MATCH_TYPE) {
1494 Log.v(logTag, "No matching type " + type
1495 + " for " + this);
1496 }
1497 if (dataMatch == NO_MATCH_DATA) {
1498 Log.v(logTag, "No matching scheme/path " + data
1499 + " for " + this);
1500 }
1501 }
1502 return dataMatch;
1503 }
1504
Jeff Brown2c376fc2011-01-28 17:34:01 -08001505 String categoryMismatch = matchCategories(categories);
1506 if (categoryMismatch != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07001507 if (false) {
Jeff Brown2c376fc2011-01-28 17:34:01 -08001508 Log.v(logTag, "No matching category " + categoryMismatch + " for " + this);
1509 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001510 return NO_MATCH_CATEGORY;
1511 }
1512
1513 // It would be nice to treat container activities as more
1514 // important than ones that can be embedded, but this is not the way...
1515 if (false) {
1516 if (categories != null) {
1517 dataMatch -= mCategories.size() - categories.size();
1518 }
1519 }
1520
1521 return dataMatch;
1522 }
1523
1524 /**
1525 * Write the contents of the IntentFilter as an XML stream.
1526 */
1527 public void writeToXml(XmlSerializer serializer) throws IOException {
Wale Ogunwale15b9fd22015-06-19 08:59:52 -07001528
1529 if (getAutoVerify()) {
1530 serializer.attribute(null, AUTO_VERIFY_STR, Boolean.toString(true));
1531 }
1532
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001533 int N = countActions();
1534 for (int i=0; i<N; i++) {
1535 serializer.startTag(null, ACTION_STR);
1536 serializer.attribute(null, NAME_STR, mActions.get(i));
1537 serializer.endTag(null, ACTION_STR);
1538 }
1539 N = countCategories();
1540 for (int i=0; i<N; i++) {
1541 serializer.startTag(null, CAT_STR);
1542 serializer.attribute(null, NAME_STR, mCategories.get(i));
1543 serializer.endTag(null, CAT_STR);
1544 }
1545 N = countDataTypes();
1546 for (int i=0; i<N; i++) {
1547 serializer.startTag(null, TYPE_STR);
1548 String type = mDataTypes.get(i);
1549 if (type.indexOf('/') < 0) type = type + "/*";
1550 serializer.attribute(null, NAME_STR, type);
1551 serializer.endTag(null, TYPE_STR);
1552 }
1553 N = countDataSchemes();
1554 for (int i=0; i<N; i++) {
1555 serializer.startTag(null, SCHEME_STR);
1556 serializer.attribute(null, NAME_STR, mDataSchemes.get(i));
1557 serializer.endTag(null, SCHEME_STR);
1558 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07001559 N = countDataSchemeSpecificParts();
1560 for (int i=0; i<N; i++) {
1561 serializer.startTag(null, SSP_STR);
1562 PatternMatcher pe = mDataSchemeSpecificParts.get(i);
1563 switch (pe.getType()) {
1564 case PatternMatcher.PATTERN_LITERAL:
1565 serializer.attribute(null, LITERAL_STR, pe.getPath());
1566 break;
1567 case PatternMatcher.PATTERN_PREFIX:
1568 serializer.attribute(null, PREFIX_STR, pe.getPath());
1569 break;
1570 case PatternMatcher.PATTERN_SIMPLE_GLOB:
1571 serializer.attribute(null, SGLOB_STR, pe.getPath());
1572 break;
1573 }
1574 serializer.endTag(null, SSP_STR);
1575 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001576 N = countDataAuthorities();
1577 for (int i=0; i<N; i++) {
1578 serializer.startTag(null, AUTH_STR);
1579 AuthorityEntry ae = mDataAuthorities.get(i);
1580 serializer.attribute(null, HOST_STR, ae.getHost());
1581 if (ae.getPort() >= 0) {
1582 serializer.attribute(null, PORT_STR, Integer.toString(ae.getPort()));
1583 }
1584 serializer.endTag(null, AUTH_STR);
1585 }
1586 N = countDataPaths();
1587 for (int i=0; i<N; i++) {
1588 serializer.startTag(null, PATH_STR);
1589 PatternMatcher pe = mDataPaths.get(i);
1590 switch (pe.getType()) {
1591 case PatternMatcher.PATTERN_LITERAL:
1592 serializer.attribute(null, LITERAL_STR, pe.getPath());
1593 break;
1594 case PatternMatcher.PATTERN_PREFIX:
1595 serializer.attribute(null, PREFIX_STR, pe.getPath());
1596 break;
1597 case PatternMatcher.PATTERN_SIMPLE_GLOB:
1598 serializer.attribute(null, SGLOB_STR, pe.getPath());
1599 break;
1600 }
1601 serializer.endTag(null, PATH_STR);
1602 }
1603 }
1604
1605 public void readFromXml(XmlPullParser parser) throws XmlPullParserException,
1606 IOException {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001607 String autoVerify = parser.getAttributeValue(null, AUTO_VERIFY_STR);
1608 setAutoVerify(TextUtils.isEmpty(autoVerify) ? false : Boolean.getBoolean(autoVerify));
1609
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001610 int outerDepth = parser.getDepth();
1611 int type;
1612 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1613 && (type != XmlPullParser.END_TAG
1614 || parser.getDepth() > outerDepth)) {
1615 if (type == XmlPullParser.END_TAG
1616 || type == XmlPullParser.TEXT) {
1617 continue;
1618 }
1619
1620 String tagName = parser.getName();
1621 if (tagName.equals(ACTION_STR)) {
1622 String name = parser.getAttributeValue(null, NAME_STR);
1623 if (name != null) {
1624 addAction(name);
1625 }
1626 } else if (tagName.equals(CAT_STR)) {
1627 String name = parser.getAttributeValue(null, NAME_STR);
1628 if (name != null) {
1629 addCategory(name);
1630 }
1631 } else if (tagName.equals(TYPE_STR)) {
1632 String name = parser.getAttributeValue(null, NAME_STR);
1633 if (name != null) {
1634 try {
1635 addDataType(name);
1636 } catch (MalformedMimeTypeException e) {
1637 }
1638 }
1639 } else if (tagName.equals(SCHEME_STR)) {
1640 String name = parser.getAttributeValue(null, NAME_STR);
1641 if (name != null) {
1642 addDataScheme(name);
1643 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07001644 } else if (tagName.equals(SSP_STR)) {
1645 String ssp = parser.getAttributeValue(null, LITERAL_STR);
1646 if (ssp != null) {
1647 addDataSchemeSpecificPart(ssp, PatternMatcher.PATTERN_LITERAL);
1648 } else if ((ssp=parser.getAttributeValue(null, PREFIX_STR)) != null) {
1649 addDataSchemeSpecificPart(ssp, PatternMatcher.PATTERN_PREFIX);
1650 } else if ((ssp=parser.getAttributeValue(null, SGLOB_STR)) != null) {
1651 addDataSchemeSpecificPart(ssp, PatternMatcher.PATTERN_SIMPLE_GLOB);
1652 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001653 } else if (tagName.equals(AUTH_STR)) {
1654 String host = parser.getAttributeValue(null, HOST_STR);
1655 String port = parser.getAttributeValue(null, PORT_STR);
1656 if (host != null) {
1657 addDataAuthority(host, port);
1658 }
1659 } else if (tagName.equals(PATH_STR)) {
1660 String path = parser.getAttributeValue(null, LITERAL_STR);
1661 if (path != null) {
1662 addDataPath(path, PatternMatcher.PATTERN_LITERAL);
1663 } else if ((path=parser.getAttributeValue(null, PREFIX_STR)) != null) {
1664 addDataPath(path, PatternMatcher.PATTERN_PREFIX);
1665 } else if ((path=parser.getAttributeValue(null, SGLOB_STR)) != null) {
1666 addDataPath(path, PatternMatcher.PATTERN_SIMPLE_GLOB);
1667 }
1668 } else {
1669 Log.w("IntentFilter", "Unknown tag parsing IntentFilter: " + tagName);
1670 }
1671 XmlUtils.skipCurrentTag(parser);
1672 }
1673 }
1674
1675 public void dump(Printer du, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001676 StringBuilder sb = new StringBuilder(256);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001677 if (mActions.size() > 0) {
1678 Iterator<String> it = mActions.iterator();
1679 while (it.hasNext()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001680 sb.setLength(0);
1681 sb.append(prefix); sb.append("Action: \"");
1682 sb.append(it.next()); sb.append("\"");
1683 du.println(sb.toString());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001684 }
1685 }
1686 if (mCategories != null) {
1687 Iterator<String> it = mCategories.iterator();
1688 while (it.hasNext()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001689 sb.setLength(0);
1690 sb.append(prefix); sb.append("Category: \"");
1691 sb.append(it.next()); sb.append("\"");
1692 du.println(sb.toString());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001693 }
1694 }
1695 if (mDataSchemes != null) {
1696 Iterator<String> it = mDataSchemes.iterator();
1697 while (it.hasNext()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001698 sb.setLength(0);
1699 sb.append(prefix); sb.append("Scheme: \"");
1700 sb.append(it.next()); sb.append("\"");
1701 du.println(sb.toString());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001702 }
1703 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07001704 if (mDataSchemeSpecificParts != null) {
1705 Iterator<PatternMatcher> it = mDataSchemeSpecificParts.iterator();
1706 while (it.hasNext()) {
1707 PatternMatcher pe = it.next();
1708 sb.setLength(0);
1709 sb.append(prefix); sb.append("Ssp: \"");
1710 sb.append(pe); sb.append("\"");
1711 du.println(sb.toString());
1712 }
1713 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001714 if (mDataAuthorities != null) {
1715 Iterator<AuthorityEntry> it = mDataAuthorities.iterator();
1716 while (it.hasNext()) {
1717 AuthorityEntry ae = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001718 sb.setLength(0);
1719 sb.append(prefix); sb.append("Authority: \"");
1720 sb.append(ae.mHost); sb.append("\": ");
1721 sb.append(ae.mPort);
1722 if (ae.mWild) sb.append(" WILD");
1723 du.println(sb.toString());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001724 }
1725 }
1726 if (mDataPaths != null) {
1727 Iterator<PatternMatcher> it = mDataPaths.iterator();
1728 while (it.hasNext()) {
1729 PatternMatcher pe = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001730 sb.setLength(0);
1731 sb.append(prefix); sb.append("Path: \"");
1732 sb.append(pe); sb.append("\"");
1733 du.println(sb.toString());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001734 }
1735 }
1736 if (mDataTypes != null) {
1737 Iterator<String> it = mDataTypes.iterator();
1738 while (it.hasNext()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001739 sb.setLength(0);
1740 sb.append(prefix); sb.append("Type: \"");
1741 sb.append(it.next()); sb.append("\"");
1742 du.println(sb.toString());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001743 }
1744 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001745 if (mPriority != 0 || mHasPartialTypes) {
1746 sb.setLength(0);
1747 sb.append(prefix); sb.append("mPriority="); sb.append(mPriority);
1748 sb.append(", mHasPartialTypes="); sb.append(mHasPartialTypes);
1749 du.println(sb.toString());
1750 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001751 {
1752 sb.setLength(0);
1753 sb.append(prefix); sb.append("AutoVerify="); sb.append(getAutoVerify());
1754 du.println(sb.toString());
1755 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001756 }
1757
1758 public static final Parcelable.Creator<IntentFilter> CREATOR
1759 = new Parcelable.Creator<IntentFilter>() {
1760 public IntentFilter createFromParcel(Parcel source) {
1761 return new IntentFilter(source);
1762 }
1763
1764 public IntentFilter[] newArray(int size) {
1765 return new IntentFilter[size];
1766 }
1767 };
1768
1769 public final int describeContents() {
1770 return 0;
1771 }
1772
1773 public final void writeToParcel(Parcel dest, int flags) {
1774 dest.writeStringList(mActions);
1775 if (mCategories != null) {
1776 dest.writeInt(1);
1777 dest.writeStringList(mCategories);
1778 } else {
1779 dest.writeInt(0);
1780 }
1781 if (mDataSchemes != null) {
1782 dest.writeInt(1);
1783 dest.writeStringList(mDataSchemes);
1784 } else {
1785 dest.writeInt(0);
1786 }
1787 if (mDataTypes != null) {
1788 dest.writeInt(1);
1789 dest.writeStringList(mDataTypes);
1790 } else {
1791 dest.writeInt(0);
1792 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07001793 if (mDataSchemeSpecificParts != null) {
1794 final int N = mDataSchemeSpecificParts.size();
1795 dest.writeInt(N);
1796 for (int i=0; i<N; i++) {
Dianne Hackborn439e9bc82013-06-12 19:13:49 -07001797 mDataSchemeSpecificParts.get(i).writeToParcel(dest, flags);
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07001798 }
1799 } else {
1800 dest.writeInt(0);
1801 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001802 if (mDataAuthorities != null) {
1803 final int N = mDataAuthorities.size();
1804 dest.writeInt(N);
1805 for (int i=0; i<N; i++) {
1806 mDataAuthorities.get(i).writeToParcel(dest);
1807 }
1808 } else {
1809 dest.writeInt(0);
1810 }
1811 if (mDataPaths != null) {
1812 final int N = mDataPaths.size();
1813 dest.writeInt(N);
1814 for (int i=0; i<N; i++) {
Dianne Hackborn439e9bc82013-06-12 19:13:49 -07001815 mDataPaths.get(i).writeToParcel(dest, flags);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001816 }
1817 } else {
1818 dest.writeInt(0);
1819 }
1820 dest.writeInt(mPriority);
1821 dest.writeInt(mHasPartialTypes ? 1 : 0);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001822 dest.writeInt(getAutoVerify() ? 1 : 0);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001823 }
1824
1825 /**
1826 * For debugging -- perform a check on the filter, return true if it passed
1827 * or false if it failed.
1828 *
1829 * {@hide}
1830 */
1831 public boolean debugCheck() {
1832 return true;
1833
1834 // This code looks for intent filters that do not specify data.
1835 /*
1836 if (mActions != null && mActions.size() == 1
1837 && mActions.contains(Intent.ACTION_MAIN)) {
1838 return true;
1839 }
1840
1841 if (mDataTypes == null && mDataSchemes == null) {
1842 Log.w("IntentFilter", "QUESTIONABLE INTENT FILTER:");
1843 dump(Log.WARN, "IntentFilter", " ");
1844 return false;
1845 }
1846
1847 return true;
1848 */
1849 }
1850
1851 private IntentFilter(Parcel source) {
1852 mActions = new ArrayList<String>();
1853 source.readStringList(mActions);
1854 if (source.readInt() != 0) {
1855 mCategories = new ArrayList<String>();
1856 source.readStringList(mCategories);
1857 }
1858 if (source.readInt() != 0) {
1859 mDataSchemes = new ArrayList<String>();
1860 source.readStringList(mDataSchemes);
1861 }
1862 if (source.readInt() != 0) {
1863 mDataTypes = new ArrayList<String>();
1864 source.readStringList(mDataTypes);
1865 }
1866 int N = source.readInt();
1867 if (N > 0) {
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07001868 mDataSchemeSpecificParts = new ArrayList<PatternMatcher>(N);
1869 for (int i=0; i<N; i++) {
1870 mDataSchemeSpecificParts.add(new PatternMatcher(source));
1871 }
1872 }
1873 N = source.readInt();
1874 if (N > 0) {
1875 mDataAuthorities = new ArrayList<AuthorityEntry>(N);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001876 for (int i=0; i<N; i++) {
1877 mDataAuthorities.add(new AuthorityEntry(source));
1878 }
1879 }
1880 N = source.readInt();
1881 if (N > 0) {
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07001882 mDataPaths = new ArrayList<PatternMatcher>(N);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001883 for (int i=0; i<N; i++) {
1884 mDataPaths.add(new PatternMatcher(source));
1885 }
1886 }
1887 mPriority = source.readInt();
1888 mHasPartialTypes = source.readInt() > 0;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001889 setAutoVerify(source.readInt() > 0);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001890 }
1891
1892 private final boolean findMimeType(String type) {
1893 final ArrayList<String> t = mDataTypes;
1894
1895 if (type == null) {
1896 return false;
1897 }
1898
1899 if (t.contains(type)) {
1900 return true;
1901 }
1902
1903 // Deal with an Intent wanting to match every type in the IntentFilter.
1904 final int typeLength = type.length();
1905 if (typeLength == 3 && type.equals("*/*")) {
1906 return !t.isEmpty();
1907 }
1908
1909 // Deal with this IntentFilter wanting to match every Intent type.
1910 if (mHasPartialTypes && t.contains("*")) {
1911 return true;
1912 }
1913
1914 final int slashpos = type.indexOf('/');
1915 if (slashpos > 0) {
1916 if (mHasPartialTypes && t.contains(type.substring(0, slashpos))) {
1917 return true;
1918 }
1919 if (typeLength == slashpos+2 && type.charAt(slashpos+1) == '*') {
1920 // Need to look through all types for one that matches
1921 // our base...
Jeff Brown2c376fc2011-01-28 17:34:01 -08001922 final int numTypes = t.size();
1923 for (int i = 0; i < numTypes; i++) {
1924 final String v = t.get(i);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001925 if (type.regionMatches(0, v, 0, slashpos+1)) {
1926 return true;
1927 }
1928 }
1929 }
1930 }
1931
1932 return false;
1933 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001934
1935 /**
1936 * @hide
1937 */
1938 public ArrayList<String> getHostsList() {
1939 ArrayList<String> result = new ArrayList<>();
1940 Iterator<IntentFilter.AuthorityEntry> it = authoritiesIterator();
1941 if (it != null) {
1942 while (it.hasNext()) {
1943 IntentFilter.AuthorityEntry entry = it.next();
1944 result.add(entry.getHost());
1945 }
1946 }
1947 return result;
1948 }
1949
1950 /**
1951 * @hide
1952 */
1953 public String[] getHosts() {
1954 ArrayList<String> list = getHostsList();
1955 return list.toArray(new String[list.size()]);
1956 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001957}