2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.content;
19 import org.xmlpull.v1.XmlPullParser;
20 import org.xmlpull.v1.XmlPullParserException;
21 import org.xmlpull.v1.XmlSerializer;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Iterator;
29 import android.net.Uri;
30 import android.os.Parcel;
31 import android.os.Parcelable;
32 import android.os.PatternMatcher;
33 import android.util.AndroidException;
34 import android.util.Config;
35 import android.util.Log;
36 import android.util.Printer;
38 import com.android.internal.util.XmlUtils;
41 * Structured description of Intent values to be matched. An IntentFilter can
42 * match against actions, categories, and data (either via its type, scheme,
43 * and/or path) in an Intent. It also includes a "priority" value which is
44 * used to order multiple matching filters.
46 * <p>IntentFilter objects are often created in XML as part of a package's
47 * {@link android.R.styleable#AndroidManifest AndroidManifest.xml} file,
48 * using {@link android.R.styleable#AndroidManifestIntentFilter intent-filter}
51 * <p>There are three Intent characteristics you can filter on: the
52 * <em>action</em>, <em>data</em>, and <em>categories</em>. For each of these
53 * characteristics you can provide
54 * multiple possible matching values (via {@link #addAction},
55 * {@link #addDataType}, {@link #addDataScheme} {@link #addDataAuthority},
56 * {@link #addDataPath}, and {@link #addCategory}, respectively).
57 * For actions, the field
58 * will not be tested if no values have been given (treating it as a wildcard);
59 * if no data characteristics are specified, however, then the filter will
60 * only match intents that contain no data.
62 * <p>The data characteristic is
63 * itself divided into three attributes: type, scheme, authority, and path.
65 * specified must match the contents of the Intent. If you specify a scheme
66 * but no type, only Intent that does not have a type (such as mailto:) will
67 * match; a content: URI will never match because they always have a MIME type
68 * that is supplied by their content provider. Specifying a type with no scheme
69 * has somewhat special meaning: it will match either an Intent with no URI
70 * field, or an Intent with a content: or file: URI. If you specify neither,
71 * then only an Intent with no data or type will match. To specify an authority,
72 * you must also specify one or more schemes that it is associated with.
73 * To specify a path, you also must specify both one or more authorities and
74 * one or more schemes it is associated with.
76 * <p>A match is based on the following rules. Note that
77 * for an IntentFilter to match an Intent, three conditions must hold:
78 * the <strong>action</strong> and <strong>category</strong> must match, and
79 * the data (both the <strong>data type</strong> and
80 * <strong>data scheme+authority+path</strong> if specified) must match.
82 * <p><strong>Action</strong> matches if any of the given values match the
83 * Intent action, <em>or</em> if no actions were specified in the filter.
85 * <p><strong>Data Type</strong> matches if any of the given values match the
86 * Intent type. The Intent
87 * type is determined by calling {@link Intent#resolveType}. A wildcard can be
88 * used for the MIME sub-type, in both the Intent and IntentFilter, so that the
89 * type "audio/*" will match "audio/mpeg", "audio/aiff", "audio/*", etc.
90 * <em>Note that MIME type matching here is <b>case sensitive</b>, unlike
91 * formal RFC MIME types!</em> You should thus always use lower case letters
92 * for your MIME types.
94 * <p><strong>Data Scheme</strong> matches if any of the given values match the
95 * Intent data's scheme.
96 * The Intent scheme is determined by calling {@link Intent#getData}
97 * and {@link android.net.Uri#getScheme} on that URI.
98 * <em>Note that scheme matching here is <b>case sensitive</b>, unlike
99 * formal RFC schemes!</em> You should thus always use lower case letters
102 * <p><strong>Data Authority</strong> matches if any of the given values match
103 * the Intent's data authority <em>and</em> one of the data scheme's in the filter
104 * has matched the Intent, <em>or</em> no authories were supplied in the filter.
105 * The Intent authority is determined by calling
106 * {@link Intent#getData} and {@link android.net.Uri#getAuthority} on that URI.
107 * <em>Note that authority matching here is <b>case sensitive</b>, unlike
108 * formal RFC host names!</em> You should thus always use lower case letters
109 * for your authority.
111 * <p><strong>Data Path</strong> matches if any of the given values match the
112 * Intent's data path <em>and</em> both a scheme and authority in the filter
113 * has matched against the Intent, <em>or</em> no paths were supplied in the
114 * filter. The Intent authority is determined by calling
115 * {@link Intent#getData} and {@link android.net.Uri#getPath} on that URI.
117 * <p><strong>Categories</strong> match if <em>all</em> of the categories in
118 * the Intent match categories given in the filter. Extra categories in the
119 * filter that are not in the Intent will not cause the match to fail. Note
120 * that unlike the action, an IntentFilter with no categories
121 * will only match an Intent that does not have any categories.
123 public class IntentFilter implements Parcelable {
124 private static final String SGLOB_STR = "sglob";
125 private static final String PREFIX_STR = "prefix";
126 private static final String LITERAL_STR = "literal";
127 private static final String PATH_STR = "path";
128 private static final String PORT_STR = "port";
129 private static final String HOST_STR = "host";
130 private static final String AUTH_STR = "auth";
131 private static final String SCHEME_STR = "scheme";
132 private static final String TYPE_STR = "type";
133 private static final String CAT_STR = "cat";
134 private static final String NAME_STR = "name";
135 private static final String ACTION_STR = "action";
138 * The filter {@link #setPriority} value at which system high-priority
139 * receivers are placed; that is, receivers that should execute before
140 * application code. Applications should never use filters with this or
145 public static final int SYSTEM_HIGH_PRIORITY = 1000;
148 * The filter {@link #setPriority} value at which system low-priority
149 * receivers are placed; that is, receivers that should execute after
150 * application code. Applications should never use filters with this or
155 public static final int SYSTEM_LOW_PRIORITY = -1000;
158 * The part of a match constant that describes the category of match
159 * that occurred. May be either {@link #MATCH_CATEGORY_EMPTY},
160 * {@link #MATCH_CATEGORY_SCHEME}, {@link #MATCH_CATEGORY_HOST},
161 * {@link #MATCH_CATEGORY_PORT},
162 * {@link #MATCH_CATEGORY_PATH}, or {@link #MATCH_CATEGORY_TYPE}. Higher
163 * values indicate a better match.
165 public static final int MATCH_CATEGORY_MASK = 0xfff0000;
168 * The part of a match constant that applies a quality adjustment to the
169 * basic category of match. The value {@link #MATCH_ADJUSTMENT_NORMAL}
170 * is no adjustment; higher numbers than that improve the quality, while
171 * lower numbers reduce it.
173 public static final int MATCH_ADJUSTMENT_MASK = 0x000ffff;
176 * Quality adjustment applied to the category of match that signifies
177 * the default, base value; higher numbers improve the quality while
178 * lower numbers reduce it.
180 public static final int MATCH_ADJUSTMENT_NORMAL = 0x8000;
183 * The filter matched an intent that had no data specified.
185 public static final int MATCH_CATEGORY_EMPTY = 0x0100000;
187 * The filter matched an intent with the same data URI scheme.
189 public static final int MATCH_CATEGORY_SCHEME = 0x0200000;
191 * The filter matched an intent with the same data URI scheme and
194 public static final int MATCH_CATEGORY_HOST = 0x0300000;
196 * The filter matched an intent with the same data URI scheme and
197 * authority host and port.
199 public static final int MATCH_CATEGORY_PORT = 0x0400000;
201 * The filter matched an intent with the same data URI scheme,
202 * authority, and path.
204 public static final int MATCH_CATEGORY_PATH = 0x0500000;
206 * The filter matched an intent with the same data MIME type.
208 public static final int MATCH_CATEGORY_TYPE = 0x0600000;
211 * The filter didn't match due to different MIME types.
213 public static final int NO_MATCH_TYPE = -1;
215 * The filter didn't match due to different data URIs.
217 public static final int NO_MATCH_DATA = -2;
219 * The filter didn't match due to different actions.
221 public static final int NO_MATCH_ACTION = -3;
223 * The filter didn't match because it required one or more categories
224 * that were not in the Intent.
226 public static final int NO_MATCH_CATEGORY = -4;
228 private int mPriority;
229 private final ArrayList<String> mActions;
230 private ArrayList<String> mCategories = null;
231 private ArrayList<String> mDataSchemes = null;
232 private ArrayList<AuthorityEntry> mDataAuthorities = null;
233 private ArrayList<PatternMatcher> mDataPaths = null;
234 private ArrayList<String> mDataTypes = null;
235 private boolean mHasPartialTypes = false;
237 // These functions are the start of more optimized code for managing
238 // the string sets... not yet implemented.
240 private static int findStringInSet(String[] set, String string,
241 int[] lengths, int lenPos) {
242 if (set == null) return -1;
243 final int N = lengths[lenPos];
244 for (int i=0; i<N; i++) {
245 if (set[i].equals(string)) return i;
250 private static String[] addStringToSet(String[] set, String string,
251 int[] lengths, int lenPos) {
252 if (findStringInSet(set, string, lengths, lenPos) >= 0) return set;
259 final int N = lengths[lenPos];
260 if (N < set.length) {
262 lengths[lenPos] = N+1;
266 String[] newSet = new String[(N*3)/2 + 2];
267 System.arraycopy(set, 0, newSet, 0, N);
270 lengths[lenPos] = N+1;
274 private static String[] removeStringFromSet(String[] set, String string,
275 int[] lengths, int lenPos) {
276 int pos = findStringInSet(set, string, lengths, lenPos);
277 if (pos < 0) return set;
278 final int N = lengths[lenPos];
279 if (N > (set.length/4)) {
280 int copyLen = N-(pos+1);
282 System.arraycopy(set, pos+1, set, pos, copyLen);
285 lengths[lenPos] = N-1;
289 String[] newSet = new String[set.length/3];
290 if (pos > 0) System.arraycopy(set, 0, newSet, 0, pos);
291 if ((pos+1) < N) System.arraycopy(set, pos+1, newSet, pos, N-(pos+1));
296 * This exception is thrown when a given MIME type does not have a valid
299 public static class MalformedMimeTypeException extends AndroidException {
300 public MalformedMimeTypeException() {
303 public MalformedMimeTypeException(String name) {
309 * Create a new IntentFilter instance with a specified action and MIME
310 * type, where you know the MIME type is correctly formatted. This catches
311 * the {@link MalformedMimeTypeException} exception that the constructor
312 * can call and turns it into a runtime exception.
314 * @param action The action to match, i.e. Intent.ACTION_VIEW.
315 * @param dataType The type to match, i.e. "vnd.android.cursor.dir/person".
317 * @return A new IntentFilter for the given action and type.
319 * @see #IntentFilter(String, String)
321 public static IntentFilter create(String action, String dataType) {
323 return new IntentFilter(action, dataType);
324 } catch (MalformedMimeTypeException e) {
325 throw new RuntimeException("Bad MIME type", e);
330 * New empty IntentFilter.
332 public IntentFilter() {
334 mActions = new ArrayList<String>();
338 * New IntentFilter that matches a single action with no data. If
339 * no data characteristics are subsequently specified, then the
340 * filter will only match intents that contain no data.
342 * @param action The action to match, i.e. Intent.ACTION_MAIN.
344 public IntentFilter(String action) {
346 mActions = new ArrayList<String>();
351 * New IntentFilter that matches a single action and data type.
353 * <p><em>Note: MIME type matching in the Android framework is
354 * case-sensitive, unlike formal RFC MIME types. As a result,
355 * you should always write your MIME types with lower case letters,
356 * and any MIME types you receive from outside of Android should be
357 * converted to lower case before supplying them here.</em></p>
359 * <p>Throws {@link MalformedMimeTypeException} if the given MIME type is
360 * not syntactically correct.
362 * @param action The action to match, i.e. Intent.ACTION_VIEW.
363 * @param dataType The type to match, i.e. "vnd.android.cursor.dir/person".
366 public IntentFilter(String action, String dataType)
367 throws MalformedMimeTypeException {
369 mActions = new ArrayList<String>();
371 addDataType(dataType);
375 * New IntentFilter containing a copy of an existing filter.
377 * @param o The original filter to copy.
379 public IntentFilter(IntentFilter o) {
380 mPriority = o.mPriority;
381 mActions = new ArrayList<String>(o.mActions);
382 if (o.mCategories != null) {
383 mCategories = new ArrayList<String>(o.mCategories);
385 if (o.mDataTypes != null) {
386 mDataTypes = new ArrayList<String>(o.mDataTypes);
388 if (o.mDataSchemes != null) {
389 mDataSchemes = new ArrayList<String>(o.mDataSchemes);
391 if (o.mDataAuthorities != null) {
392 mDataAuthorities = new ArrayList<AuthorityEntry>(o.mDataAuthorities);
394 if (o.mDataPaths != null) {
395 mDataPaths = new ArrayList<PatternMatcher>(o.mDataPaths);
397 mHasPartialTypes = o.mHasPartialTypes;
401 * Modify priority of this filter. The default priority is 0. Positive
402 * values will be before the default, lower values will be after it.
403 * Applications must use a value that is larger than
404 * {@link #SYSTEM_LOW_PRIORITY} and smaller than
405 * {@link #SYSTEM_HIGH_PRIORITY} .
407 * @param priority The new priority value.
410 * @see #SYSTEM_LOW_PRIORITY
411 * @see #SYSTEM_HIGH_PRIORITY
413 public final void setPriority(int priority) {
414 mPriority = priority;
418 * Return the priority of this filter.
420 * @return The priority of the filter.
424 public final int getPriority() {
429 * Add a new Intent action to match against. If any actions are included
430 * in the filter, then an Intent's action must be one of those values for
431 * it to match. If no actions are included, the Intent action is ignored.
433 * @param action Name of the action to match, i.e. Intent.ACTION_VIEW.
435 public final void addAction(String action) {
436 if (!mActions.contains(action)) {
437 mActions.add(action.intern());
442 * Return the number of actions in the filter.
444 public final int countActions() {
445 return mActions.size();
449 * Return an action in the filter.
451 public final String getAction(int index) {
452 return mActions.get(index);
456 * Is the given action included in the filter? Note that if the filter
457 * does not include any actions, false will <em>always</em> be returned.
459 * @param action The action to look for.
461 * @return True if the action is explicitly mentioned in the filter.
463 public final boolean hasAction(String action) {
464 return mActions.contains(action);
468 * Match this filter against an Intent's action. If the filter does not
469 * specify any actions, the match will always fail.
471 * @param action The desired action to look for.
473 * @return True if the action is listed in the filter or the filter does
474 * not specify any actions.
476 public final boolean matchAction(String action) {
477 if (action == null || mActions == null || mActions.size() == 0) {
480 return mActions.contains(action);
484 * Return an iterator over the filter's actions. If there are no actions,
487 public final Iterator<String> actionsIterator() {
488 return mActions != null ? mActions.iterator() : null;
492 * Add a new Intent data type to match against. If any types are
493 * included in the filter, then an Intent's data must be <em>either</em>
494 * one of these types <em>or</em> a matching scheme. If no data types
495 * are included, then an Intent will only match if it specifies no data.
497 * <p><em>Note: MIME type matching in the Android framework is
498 * case-sensitive, unlike formal RFC MIME types. As a result,
499 * you should always write your MIME types with lower case letters,
500 * and any MIME types you receive from outside of Android should be
501 * converted to lower case before supplying them here.</em></p>
503 * <p>Throws {@link MalformedMimeTypeException} if the given MIME type is
504 * not syntactically correct.
506 * @param type Name of the data type to match, i.e. "vnd.android.cursor.dir/person".
510 public final void addDataType(String type)
511 throws MalformedMimeTypeException {
512 final int slashpos = type.indexOf('/');
513 final int typelen = type.length();
514 if (slashpos > 0 && typelen >= slashpos+2) {
515 if (mDataTypes == null) mDataTypes = new ArrayList<String>();
516 if (typelen == slashpos+2 && type.charAt(slashpos+1) == '*') {
517 String str = type.substring(0, slashpos);
518 if (!mDataTypes.contains(str)) {
519 mDataTypes.add(str.intern());
521 mHasPartialTypes = true;
523 if (!mDataTypes.contains(type)) {
524 mDataTypes.add(type.intern());
530 throw new MalformedMimeTypeException(type);
534 * Is the given data type included in the filter? Note that if the filter
535 * does not include any type, false will <em>always</em> be returned.
537 * @param type The data type to look for.
539 * @return True if the type is explicitly mentioned in the filter.
541 public final boolean hasDataType(String type) {
542 return mDataTypes != null && findMimeType(type);
546 * Return the number of data types in the filter.
548 public final int countDataTypes() {
549 return mDataTypes != null ? mDataTypes.size() : 0;
553 * Return a data type in the filter.
555 public final String getDataType(int index) {
556 return mDataTypes.get(index);
560 * Return an iterator over the filter's data types.
562 public final Iterator<String> typesIterator() {
563 return mDataTypes != null ? mDataTypes.iterator() : null;
567 * Add a new Intent data scheme to match against. If any schemes are
568 * included in the filter, then an Intent's data must be <em>either</em>
569 * one of these schemes <em>or</em> a matching data type. If no schemes
570 * are included, then an Intent will match only if it includes no data.
572 * <p><em>Note: scheme matching in the Android framework is
573 * case-sensitive, unlike formal RFC schemes. As a result,
574 * you should always write your schemes with lower case letters,
575 * and any schemes you receive from outside of Android should be
576 * converted to lower case before supplying them here.</em></p>
578 * @param scheme Name of the scheme to match, i.e. "http".
582 public final void addDataScheme(String scheme) {
583 if (mDataSchemes == null) mDataSchemes = new ArrayList<String>();
584 if (!mDataSchemes.contains(scheme)) {
585 mDataSchemes.add(scheme.intern());
590 * Return the number of data schemes in the filter.
592 public final int countDataSchemes() {
593 return mDataSchemes != null ? mDataSchemes.size() : 0;
597 * Return a data scheme in the filter.
599 public final String getDataScheme(int index) {
600 return mDataSchemes.get(index);
604 * Is the given data scheme included in the filter? Note that if the
605 * filter does not include any scheme, false will <em>always</em> be
608 * @param scheme The data scheme to look for.
610 * @return True if the scheme is explicitly mentioned in the filter.
612 public final boolean hasDataScheme(String scheme) {
613 return mDataSchemes != null && mDataSchemes.contains(scheme);
617 * Return an iterator over the filter's data schemes.
619 public final Iterator<String> schemesIterator() {
620 return mDataSchemes != null ? mDataSchemes.iterator() : null;
624 * This is an entry for a single authority in the Iterator returned by
625 * {@link #authoritiesIterator()}.
627 public final static class AuthorityEntry {
628 private final String mOrigHost;
629 private final String mHost;
630 private final boolean mWild;
631 private final int mPort;
633 public AuthorityEntry(String host, String port) {
635 mWild = host.length() > 0 && host.charAt(0) == '*';
636 mHost = mWild ? host.substring(1).intern() : host;
637 mPort = port != null ? Integer.parseInt(port) : -1;
640 AuthorityEntry(Parcel src) {
641 mOrigHost = src.readString();
642 mHost = src.readString();
643 mWild = src.readInt() != 0;
644 mPort = src.readInt();
647 void writeToParcel(Parcel dest) {
648 dest.writeString(mOrigHost);
649 dest.writeString(mHost);
650 dest.writeInt(mWild ? 1 : 0);
651 dest.writeInt(mPort);
654 public String getHost() {
658 public int getPort() {
663 * Determine whether this AuthorityEntry matches the given data Uri.
664 * <em>Note that this comparison is case-sensitive, unlike formal
665 * RFC host names. You thus should always normalize to lower-case.</em>
667 * @param data The Uri to match.
668 * @return Returns either {@link IntentFilter#NO_MATCH_DATA},
669 * {@link IntentFilter#MATCH_CATEGORY_PORT}, or
670 * {@link IntentFilter#MATCH_CATEGORY_HOST}.
672 public int match(Uri data) {
673 String host = data.getHost();
675 return NO_MATCH_DATA;
677 if (Config.LOGV) Log.v("IntentFilter",
678 "Match host " + host + ": " + mHost);
680 if (host.length() < mHost.length()) {
681 return NO_MATCH_DATA;
683 host = host.substring(host.length()-mHost.length());
685 if (host.compareToIgnoreCase(mHost) != 0) {
686 return NO_MATCH_DATA;
689 if (mPort != data.getPort()) {
690 return NO_MATCH_DATA;
692 return MATCH_CATEGORY_PORT;
694 return MATCH_CATEGORY_HOST;
699 * Add a new Intent data authority to match against. The filter must
700 * include one or more schemes (via {@link #addDataScheme}) for the
701 * authority to be considered. If any authorities are
702 * included in the filter, then an Intent's data must match one of
703 * them. If no authorities are included, then only the scheme must match.
705 * <p><em>Note: host name in the Android framework is
706 * case-sensitive, unlike formal RFC host names. As a result,
707 * you should always write your host names with lower case letters,
708 * and any host names you receive from outside of Android should be
709 * converted to lower case before supplying them here.</em></p>
711 * @param host The host part of the authority to match. May start with a
712 * single '*' to wildcard the front of the host name.
713 * @param port Optional port part of the authority to match. If null, any
717 * @see #addDataScheme
719 public final void addDataAuthority(String host, String port) {
720 if (mDataAuthorities == null) mDataAuthorities =
721 new ArrayList<AuthorityEntry>();
722 if (port != null) port = port.intern();
723 mDataAuthorities.add(new AuthorityEntry(host.intern(), port));
727 * Return the number of data authorities in the filter.
729 public final int countDataAuthorities() {
730 return mDataAuthorities != null ? mDataAuthorities.size() : 0;
734 * Return a data authority in the filter.
736 public final AuthorityEntry getDataAuthority(int index) {
737 return mDataAuthorities.get(index);
741 * Is the given data authority included in the filter? Note that if the
742 * filter does not include any authorities, false will <em>always</em> be
745 * @param data The data whose authority is being looked for.
747 * @return Returns true if the data string matches an authority listed in the
750 public final boolean hasDataAuthority(Uri data) {
751 return matchDataAuthority(data) >= 0;
755 * Return an iterator over the filter's data authorities.
757 public final Iterator<AuthorityEntry> authoritiesIterator() {
758 return mDataAuthorities != null ? mDataAuthorities.iterator() : null;
762 * Add a new Intent data oath to match against. The filter must
763 * include one or more schemes (via {@link #addDataScheme}) <em>and</em>
764 * one or more authorities (via {@link #addDataAuthority}) for the
765 * path to be considered. If any paths are
766 * included in the filter, then an Intent's data must match one of
767 * them. If no paths are included, then only the scheme/authority must
770 * <p>The path given here can either be a literal that must directly
771 * match or match against a prefix, or it can be a simple globbing pattern.
772 * If the latter, you can use '*' anywhere in the pattern to match zero
773 * or more instances of the previous character, '.' as a wildcard to match
774 * any character, and '\' to escape the next character.
776 * @param path Either a raw string that must exactly match the file
777 * path, or a simple pattern, depending on <var>type</var>.
778 * @param type Determines how <var>path</var> will be compared to
779 * determine a match: either {@link PatternMatcher#PATTERN_LITERAL},
780 * {@link PatternMatcher#PATTERN_PREFIX}, or
781 * {@link PatternMatcher#PATTERN_SIMPLE_GLOB}.
784 * @see #addDataScheme
785 * @see #addDataAuthority
787 public final void addDataPath(String path, int type) {
788 if (mDataPaths == null) mDataPaths = new ArrayList<PatternMatcher>();
789 mDataPaths.add(new PatternMatcher(path.intern(), type));
793 * Return the number of data paths in the filter.
795 public final int countDataPaths() {
796 return mDataPaths != null ? mDataPaths.size() : 0;
800 * Return a data path in the filter.
802 public final PatternMatcher getDataPath(int index) {
803 return mDataPaths.get(index);
807 * Is the given data path included in the filter? Note that if the
808 * filter does not include any paths, false will <em>always</em> be
811 * @param data The data path to look for. This is without the scheme
814 * @return True if the data string matches a path listed in the
817 public final boolean hasDataPath(String data) {
818 if (mDataPaths == null) {
821 Iterator<PatternMatcher> i = mDataPaths.iterator();
822 while (i.hasNext()) {
823 final PatternMatcher pe = i.next();
824 if (pe.match(data)) {
832 * Return an iterator over the filter's data paths.
834 public final Iterator<PatternMatcher> pathsIterator() {
835 return mDataPaths != null ? mDataPaths.iterator() : null;
839 * Match this intent filter against the given Intent data. This ignores
840 * the data scheme -- unlike {@link #matchData}, the authority will match
841 * regardless of whether there is a matching scheme.
843 * @param data The data whose authority is being looked for.
845 * @return Returns either {@link #MATCH_CATEGORY_HOST},
846 * {@link #MATCH_CATEGORY_PORT}, {@link #NO_MATCH_DATA}.
848 public final int matchDataAuthority(Uri data) {
849 if (mDataAuthorities == null) {
850 return NO_MATCH_DATA;
852 Iterator<AuthorityEntry> i = mDataAuthorities.iterator();
853 while (i.hasNext()) {
854 final AuthorityEntry ae = i.next();
855 int match = ae.match(data);
860 return NO_MATCH_DATA;
864 * Match this filter against an Intent's data (type, scheme and path). If
865 * the filter does not specify any types and does not specify any
866 * schemes/paths, the match will only succeed if the intent does not
867 * also specify a type or data.
869 * <p>Be aware that to match against an authority, you must also specify a base
870 * scheme the authority is in. To match against a data path, both a scheme
871 * and authority must be specified. If the filter does not specify any
872 * types or schemes that it matches against, it is considered to be empty
873 * (any authority or data path given is ignored, as if it were empty as
876 * <p><em>Note: MIME type, Uri scheme, and host name matching in the
877 * Android framework is case-sensitive, unlike the formal RFC definitions.
878 * As a result, you should always write these elements with lower case letters,
879 * and normalize any MIME types or Uris you receive from
880 * outside of Android to ensure these elements are lower case before
881 * supplying them here.</em></p>
883 * @param type The desired data type to look for, as returned by
884 * Intent.resolveType().
885 * @param scheme The desired data scheme to look for, as returned by
886 * Intent.getScheme().
887 * @param data The full data string to match against, as supplied in
890 * @return Returns either a valid match constant (a combination of
891 * {@link #MATCH_CATEGORY_MASK} and {@link #MATCH_ADJUSTMENT_MASK}),
892 * or one of the error codes {@link #NO_MATCH_TYPE} if the type didn't match
893 * or {@link #NO_MATCH_DATA} if the scheme/path didn't match.
897 public final int matchData(String type, String scheme, Uri data) {
898 final ArrayList<String> types = mDataTypes;
899 final ArrayList<String> schemes = mDataSchemes;
900 final ArrayList<AuthorityEntry> authorities = mDataAuthorities;
901 final ArrayList<PatternMatcher> paths = mDataPaths;
903 int match = MATCH_CATEGORY_EMPTY;
905 if (types == null && schemes == null) {
906 return ((type == null && data == null)
907 ? (MATCH_CATEGORY_EMPTY+MATCH_ADJUSTMENT_NORMAL) : NO_MATCH_DATA);
910 if (schemes != null) {
911 if (schemes.contains(scheme != null ? scheme : "")) {
912 match = MATCH_CATEGORY_SCHEME;
914 return NO_MATCH_DATA;
917 if (authorities != null) {
918 int authMatch = matchDataAuthority(data);
919 if (authMatch >= 0) {
922 } else if (hasDataPath(data.getPath())) {
923 match = MATCH_CATEGORY_PATH;
925 return NO_MATCH_DATA;
928 return NO_MATCH_DATA;
932 // Special case: match either an Intent with no data URI,
933 // or with a scheme: URI. This is to give a convenience for
934 // the common case where you want to deal with data in a
935 // content provider, which is done by type, and we don't want
936 // to force everyone to say they handle content: or file: URIs.
937 if (scheme != null && !"".equals(scheme)
938 && !"content".equals(scheme)
939 && !"file".equals(scheme)) {
940 return NO_MATCH_DATA;
945 if (findMimeType(type)) {
946 match = MATCH_CATEGORY_TYPE;
948 return NO_MATCH_TYPE;
951 // If no MIME types are specified, then we will only match against
952 // an Intent that does not have a MIME type.
954 return NO_MATCH_TYPE;
958 return match + MATCH_ADJUSTMENT_NORMAL;
962 * Add a new Intent category to match against. The semantics of
963 * categories is the opposite of actions -- an Intent includes the
964 * categories that it requires, all of which must be included in the
965 * filter in order to match. In other words, adding a category to the
966 * filter has no impact on matching unless that category is specified in
969 * @param category Name of category to match, i.e. Intent.CATEGORY_EMBED.
971 public final void addCategory(String category) {
972 if (mCategories == null) mCategories = new ArrayList<String>();
973 if (!mCategories.contains(category)) {
974 mCategories.add(category.intern());
979 * Return the number of categories in the filter.
981 public final int countCategories() {
982 return mCategories != null ? mCategories.size() : 0;
986 * Return a category in the filter.
988 public final String getCategory(int index) {
989 return mCategories.get(index);
993 * Is the given category included in the filter?
995 * @param category The category that the filter supports.
997 * @return True if the category is explicitly mentioned in the filter.
999 public final boolean hasCategory(String category) {
1000 return mCategories != null && mCategories.contains(category);
1004 * Return an iterator over the filter's categories.
1006 public final Iterator<String> categoriesIterator() {
1007 return mCategories != null ? mCategories.iterator() : null;
1011 * Match this filter against an Intent's categories. Each category in
1012 * the Intent must be specified by the filter; if any are not in the
1013 * filter, the match fails.
1015 * @param categories The categories included in the intent, as returned by
1016 * Intent.getCategories().
1018 * @return If all categories match (success), null; else the name of the
1019 * first category that didn't match.
1021 public final String matchCategories(Set<String> categories) {
1022 if (categories == null) {
1026 Iterator<String> it = categories.iterator();
1028 if (mCategories == null) {
1029 return it.hasNext() ? it.next() : null;
1032 while (it.hasNext()) {
1033 final String category = it.next();
1034 if (!mCategories.contains(category)) {
1043 * Test whether this filter matches the given <var>intent</var>.
1045 * @param intent The Intent to compare against.
1046 * @param resolve If true, the intent's type will be resolved by calling
1047 * Intent.resolveType(); otherwise a simple match against
1048 * Intent.type will be performed.
1049 * @param logTag Tag to use in debugging messages.
1051 * @return Returns either a valid match constant (a combination of
1052 * {@link #MATCH_CATEGORY_MASK} and {@link #MATCH_ADJUSTMENT_MASK}),
1053 * or one of the error codes {@link #NO_MATCH_TYPE} if the type didn't match,
1054 * {@link #NO_MATCH_DATA} if the scheme/path didn't match,
1055 * {@link #NO_MATCH_ACTION if the action didn't match, or
1056 * {@link #NO_MATCH_CATEGORY} if one or more categories didn't match.
1058 * @return How well the filter matches. Negative if it doesn't match,
1059 * zero or positive positive value if it does with a higher
1060 * value representing a better match.
1062 * @see #match(String, String, String, android.net.Uri , Set, String)
1064 public final int match(ContentResolver resolver, Intent intent,
1065 boolean resolve, String logTag) {
1066 String type = resolve ? intent.resolveType(resolver) : intent.getType();
1067 return match(intent.getAction(), type, intent.getScheme(),
1068 intent.getData(), intent.getCategories(), logTag);
1072 * Test whether this filter matches the given intent data. A match is
1073 * only successful if the actions and categories in the Intent match
1074 * against the filter, as described in {@link IntentFilter}; in that case,
1075 * the match result returned will be as per {@link #matchData}.
1077 * @param action The intent action to match against (Intent.getAction).
1078 * @param type The intent type to match against (Intent.resolveType()).
1079 * @param scheme The data scheme to match against (Intent.getScheme()).
1080 * @param data The data URI to match against (Intent.getData()).
1081 * @param categories The categories to match against
1082 * (Intent.getCategories()).
1083 * @param logTag Tag to use in debugging messages.
1085 * @return Returns either a valid match constant (a combination of
1086 * {@link #MATCH_CATEGORY_MASK} and {@link #MATCH_ADJUSTMENT_MASK}),
1087 * or one of the error codes {@link #NO_MATCH_TYPE} if the type didn't match,
1088 * {@link #NO_MATCH_DATA} if the scheme/path didn't match,
1089 * {@link #NO_MATCH_ACTION if the action didn't match, or
1090 * {@link #NO_MATCH_CATEGORY} if one or more categories didn't match.
1093 * @see Intent#getAction
1094 * @see Intent#resolveType
1095 * @see Intent#getScheme
1096 * @see Intent#getData
1097 * @see Intent#getCategories
1099 public final int match(String action, String type, String scheme,
1100 Uri data, Set<String> categories, String logTag) {
1101 if (action != null && !matchAction(action)) {
1102 if (Config.LOGV) Log.v(
1103 logTag, "No matching action " + action + " for " + this);
1104 return NO_MATCH_ACTION;
1107 int dataMatch = matchData(type, scheme, data);
1108 if (dataMatch < 0) {
1110 if (dataMatch == NO_MATCH_TYPE) {
1111 Log.v(logTag, "No matching type " + type
1114 if (dataMatch == NO_MATCH_DATA) {
1115 Log.v(logTag, "No matching scheme/path " + data
1122 String categoryMatch = matchCategories(categories);
1123 if (categoryMatch != null) {
1124 if (Config.LOGV) Log.v(
1125 logTag, "No matching category "
1126 + categoryMatch + " for " + this);
1127 return NO_MATCH_CATEGORY;
1130 // It would be nice to treat container activities as more
1131 // important than ones that can be embedded, but this is not the way...
1133 if (categories != null) {
1134 dataMatch -= mCategories.size() - categories.size();
1142 * Write the contents of the IntentFilter as an XML stream.
1144 public void writeToXml(XmlSerializer serializer) throws IOException {
1145 int N = countActions();
1146 for (int i=0; i<N; i++) {
1147 serializer.startTag(null, ACTION_STR);
1148 serializer.attribute(null, NAME_STR, mActions.get(i));
1149 serializer.endTag(null, ACTION_STR);
1151 N = countCategories();
1152 for (int i=0; i<N; i++) {
1153 serializer.startTag(null, CAT_STR);
1154 serializer.attribute(null, NAME_STR, mCategories.get(i));
1155 serializer.endTag(null, CAT_STR);
1157 N = countDataTypes();
1158 for (int i=0; i<N; i++) {
1159 serializer.startTag(null, TYPE_STR);
1160 String type = mDataTypes.get(i);
1161 if (type.indexOf('/') < 0) type = type + "/*";
1162 serializer.attribute(null, NAME_STR, type);
1163 serializer.endTag(null, TYPE_STR);
1165 N = countDataSchemes();
1166 for (int i=0; i<N; i++) {
1167 serializer.startTag(null, SCHEME_STR);
1168 serializer.attribute(null, NAME_STR, mDataSchemes.get(i));
1169 serializer.endTag(null, SCHEME_STR);
1171 N = countDataAuthorities();
1172 for (int i=0; i<N; i++) {
1173 serializer.startTag(null, AUTH_STR);
1174 AuthorityEntry ae = mDataAuthorities.get(i);
1175 serializer.attribute(null, HOST_STR, ae.getHost());
1176 if (ae.getPort() >= 0) {
1177 serializer.attribute(null, PORT_STR, Integer.toString(ae.getPort()));
1179 serializer.endTag(null, AUTH_STR);
1181 N = countDataPaths();
1182 for (int i=0; i<N; i++) {
1183 serializer.startTag(null, PATH_STR);
1184 PatternMatcher pe = mDataPaths.get(i);
1185 switch (pe.getType()) {
1186 case PatternMatcher.PATTERN_LITERAL:
1187 serializer.attribute(null, LITERAL_STR, pe.getPath());
1189 case PatternMatcher.PATTERN_PREFIX:
1190 serializer.attribute(null, PREFIX_STR, pe.getPath());
1192 case PatternMatcher.PATTERN_SIMPLE_GLOB:
1193 serializer.attribute(null, SGLOB_STR, pe.getPath());
1196 serializer.endTag(null, PATH_STR);
1200 public void readFromXml(XmlPullParser parser) throws XmlPullParserException,
1202 int outerDepth = parser.getDepth();
1204 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1205 && (type != XmlPullParser.END_TAG
1206 || parser.getDepth() > outerDepth)) {
1207 if (type == XmlPullParser.END_TAG
1208 || type == XmlPullParser.TEXT) {
1212 String tagName = parser.getName();
1213 if (tagName.equals(ACTION_STR)) {
1214 String name = parser.getAttributeValue(null, NAME_STR);
1218 } else if (tagName.equals(CAT_STR)) {
1219 String name = parser.getAttributeValue(null, NAME_STR);
1223 } else if (tagName.equals(TYPE_STR)) {
1224 String name = parser.getAttributeValue(null, NAME_STR);
1228 } catch (MalformedMimeTypeException e) {
1231 } else if (tagName.equals(SCHEME_STR)) {
1232 String name = parser.getAttributeValue(null, NAME_STR);
1234 addDataScheme(name);
1236 } else if (tagName.equals(AUTH_STR)) {
1237 String host = parser.getAttributeValue(null, HOST_STR);
1238 String port = parser.getAttributeValue(null, PORT_STR);
1240 addDataAuthority(host, port);
1242 } else if (tagName.equals(PATH_STR)) {
1243 String path = parser.getAttributeValue(null, LITERAL_STR);
1245 addDataPath(path, PatternMatcher.PATTERN_LITERAL);
1246 } else if ((path=parser.getAttributeValue(null, PREFIX_STR)) != null) {
1247 addDataPath(path, PatternMatcher.PATTERN_PREFIX);
1248 } else if ((path=parser.getAttributeValue(null, SGLOB_STR)) != null) {
1249 addDataPath(path, PatternMatcher.PATTERN_SIMPLE_GLOB);
1252 Log.w("IntentFilter", "Unknown tag parsing IntentFilter: " + tagName);
1254 XmlUtils.skipCurrentTag(parser);
1258 public void dump(Printer du, String prefix) {
1259 StringBuilder sb = new StringBuilder(256);
1260 if (mActions.size() > 0) {
1261 Iterator<String> it = mActions.iterator();
1262 while (it.hasNext()) {
1264 sb.append(prefix); sb.append("Action: \"");
1265 sb.append(it.next()); sb.append("\"");
1266 du.println(sb.toString());
1269 if (mCategories != null) {
1270 Iterator<String> it = mCategories.iterator();
1271 while (it.hasNext()) {
1273 sb.append(prefix); sb.append("Category: \"");
1274 sb.append(it.next()); sb.append("\"");
1275 du.println(sb.toString());
1278 if (mDataSchemes != null) {
1279 Iterator<String> it = mDataSchemes.iterator();
1280 while (it.hasNext()) {
1282 sb.append(prefix); sb.append("Scheme: \"");
1283 sb.append(it.next()); sb.append("\"");
1284 du.println(sb.toString());
1287 if (mDataAuthorities != null) {
1288 Iterator<AuthorityEntry> it = mDataAuthorities.iterator();
1289 while (it.hasNext()) {
1290 AuthorityEntry ae = it.next();
1292 sb.append(prefix); sb.append("Authority: \"");
1293 sb.append(ae.mHost); sb.append("\": ");
1294 sb.append(ae.mPort);
1295 if (ae.mWild) sb.append(" WILD");
1296 du.println(sb.toString());
1299 if (mDataPaths != null) {
1300 Iterator<PatternMatcher> it = mDataPaths.iterator();
1301 while (it.hasNext()) {
1302 PatternMatcher pe = it.next();
1304 sb.append(prefix); sb.append("Path: \"");
1305 sb.append(pe); sb.append("\"");
1306 du.println(sb.toString());
1309 if (mDataTypes != null) {
1310 Iterator<String> it = mDataTypes.iterator();
1311 while (it.hasNext()) {
1313 sb.append(prefix); sb.append("Type: \"");
1314 sb.append(it.next()); sb.append("\"");
1315 du.println(sb.toString());
1318 if (mPriority != 0 || mHasPartialTypes) {
1320 sb.append(prefix); sb.append("mPriority="); sb.append(mPriority);
1321 sb.append(", mHasPartialTypes="); sb.append(mHasPartialTypes);
1322 du.println(sb.toString());
1326 public static final Parcelable.Creator<IntentFilter> CREATOR
1327 = new Parcelable.Creator<IntentFilter>() {
1328 public IntentFilter createFromParcel(Parcel source) {
1329 return new IntentFilter(source);
1332 public IntentFilter[] newArray(int size) {
1333 return new IntentFilter[size];
1337 public final int describeContents() {
1341 public final void writeToParcel(Parcel dest, int flags) {
1342 dest.writeStringList(mActions);
1343 if (mCategories != null) {
1345 dest.writeStringList(mCategories);
1349 if (mDataSchemes != null) {
1351 dest.writeStringList(mDataSchemes);
1355 if (mDataTypes != null) {
1357 dest.writeStringList(mDataTypes);
1361 if (mDataAuthorities != null) {
1362 final int N = mDataAuthorities.size();
1364 for (int i=0; i<N; i++) {
1365 mDataAuthorities.get(i).writeToParcel(dest);
1370 if (mDataPaths != null) {
1371 final int N = mDataPaths.size();
1373 for (int i=0; i<N; i++) {
1374 mDataPaths.get(i).writeToParcel(dest, 0);
1379 dest.writeInt(mPriority);
1380 dest.writeInt(mHasPartialTypes ? 1 : 0);
1384 * For debugging -- perform a check on the filter, return true if it passed
1385 * or false if it failed.
1389 public boolean debugCheck() {
1392 // This code looks for intent filters that do not specify data.
1394 if (mActions != null && mActions.size() == 1
1395 && mActions.contains(Intent.ACTION_MAIN)) {
1399 if (mDataTypes == null && mDataSchemes == null) {
1400 Log.w("IntentFilter", "QUESTIONABLE INTENT FILTER:");
1401 dump(Log.WARN, "IntentFilter", " ");
1409 private IntentFilter(Parcel source) {
1410 mActions = new ArrayList<String>();
1411 source.readStringList(mActions);
1412 if (source.readInt() != 0) {
1413 mCategories = new ArrayList<String>();
1414 source.readStringList(mCategories);
1416 if (source.readInt() != 0) {
1417 mDataSchemes = new ArrayList<String>();
1418 source.readStringList(mDataSchemes);
1420 if (source.readInt() != 0) {
1421 mDataTypes = new ArrayList<String>();
1422 source.readStringList(mDataTypes);
1424 int N = source.readInt();
1426 mDataAuthorities = new ArrayList<AuthorityEntry>();
1427 for (int i=0; i<N; i++) {
1428 mDataAuthorities.add(new AuthorityEntry(source));
1431 N = source.readInt();
1433 mDataPaths = new ArrayList<PatternMatcher>();
1434 for (int i=0; i<N; i++) {
1435 mDataPaths.add(new PatternMatcher(source));
1438 mPriority = source.readInt();
1439 mHasPartialTypes = source.readInt() > 0;
1442 private final boolean findMimeType(String type) {
1443 final ArrayList<String> t = mDataTypes;
1449 if (t.contains(type)) {
1453 // Deal with an Intent wanting to match every type in the IntentFilter.
1454 final int typeLength = type.length();
1455 if (typeLength == 3 && type.equals("*/*")) {
1456 return !t.isEmpty();
1459 // Deal with this IntentFilter wanting to match every Intent type.
1460 if (mHasPartialTypes && t.contains("*")) {
1464 final int slashpos = type.indexOf('/');
1466 if (mHasPartialTypes && t.contains(type.substring(0, slashpos))) {
1469 if (typeLength == slashpos+2 && type.charAt(slashpos+1) == '*') {
1470 // Need to look through all types for one that matches
1472 final Iterator<String> it = t.iterator();
1473 while (it.hasNext()) {
1474 String v = it.next();
1475 if (type.regionMatches(0, v, 0, slashpos+1)) {