• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package android.content;
18 
19 import android.net.Uri;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 import android.os.PatternMatcher;
23 import android.text.TextUtils;
24 import android.util.AndroidException;
25 import android.util.Log;
26 import android.util.Printer;
27 
28 import com.android.internal.util.XmlUtils;
29 
30 import org.xmlpull.v1.XmlPullParser;
31 import org.xmlpull.v1.XmlPullParserException;
32 import org.xmlpull.v1.XmlSerializer;
33 
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.Iterator;
37 import java.util.Set;
38 
39 /**
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},
54  * {@link #addDataType}, {@link #addDataScheme}, {@link #addDataSchemeSpecificPart},
55  * {@link #addDataAuthority}, {@link #addDataPath}, and {@link #addCategory}, respectively).
56  * 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  *
75  * <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>
83  * <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
87  * <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).
90  *
91  * <p><strong>Action</strong> matches if any of the given values match the
92  * Intent action; if the filter specifies no actions, then it will only match
93  * Intents that do not contain an action.
94  *
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.
100  * <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.
103  *
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.
108  * <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.
111  *
112  * <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  *
119  * <p><strong>Data Authority</strong> matches if any of the given values match
120  * the Intent's data authority <em>and</em> one of the data schemes in the filter
121  * 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.
124  * <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  *
128  * <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  */
140 public 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";
148     private static final String SSP_STR = "ssp";
149     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";
154     private static final String AUTO_VERIFY_STR = "autoVerify";
155 
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},
179      * {@link #MATCH_CATEGORY_SCHEME}, {@link #MATCH_CATEGORY_SCHEME_SPECIFIC_PART},
180      * {@link #MATCH_CATEGORY_HOST}, {@link #MATCH_CATEGORY_PORT},
181      * {@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     /**
225      * 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     /**
230      * 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 
252     /**
253      * HTTP scheme.
254      *
255      * @see #addDataScheme(String)
256      * @hide
257      */
258     public static final String SCHEME_HTTP = "http";
259     /**
260      * HTTPS scheme.
261      *
262      * @see #addDataScheme(String)
263      * @hide
264      */
265     public static final String SCHEME_HTTPS = "https";
266 
267     private int mPriority;
268     private final ArrayList<String> mActions;
269     private ArrayList<String> mCategories = null;
270     private ArrayList<String> mDataSchemes = null;
271     private ArrayList<PatternMatcher> mDataSchemeSpecificParts = null;
272     private ArrayList<AuthorityEntry> mDataAuthorities = null;
273     private ArrayList<PatternMatcher> mDataPaths = null;
274     private ArrayList<String> mDataTypes = null;
275     private boolean mHasPartialTypes = false;
276 
277     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 
284     // These functions are the start of more optimized code for managing
285     // the string sets...  not yet implemented.
286 
findStringInSet(String[] set, String string, int[] lengths, int lenPos)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 
addStringToSet(String[] set, String string, int[] lengths, int lenPos)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 
removeStringFromSet(String[] set, String string, int[] lengths, int lenPos)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 {
MalformedMimeTypeException()347         public MalformedMimeTypeException() {
348         }
349 
MalformedMimeTypeException(String name)350         public MalformedMimeTypeException(String name) {
351             super(name);
352         }
353     }
354 
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      */
create(String action, String dataType)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      */
IntentFilter()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      */
IntentFilter(String action)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      *
400      * <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      *
406      * <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      */
IntentFilter(String action, String dataType)413     public IntentFilter(String action, String dataType)
414         throws MalformedMimeTypeException {
415         mPriority = 0;
416         mActions = new ArrayList<String>();
417         addAction(action);
418         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      */
IntentFilter(IntentFilter o)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         }
438         if (o.mDataSchemeSpecificParts != null) {
439             mDataSchemeSpecificParts = new ArrayList<PatternMatcher>(o.mDataSchemeSpecificParts);
440         }
441         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;
448         mVerifyState = o.mVerifyState;
449     }
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      */
setPriority(int priority)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      */
getPriority()475     public final int getPriority() {
476         return mPriority;
477     }
478 
479     /**
480      * 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      */
setAutoVerify(boolean autoVerify)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      */
getAutoVerify()516     public final boolean getAutoVerify() {
517         return ((mVerifyState & STATE_VERIFY_AUTO) == 1);
518     }
519 
520     /**
521      * 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".
523      *
524      * @return True if the filter handle all HTTP or HTTPS data URI. False otherwise.
525      *
526      * 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
530      * 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      */
handleAllWebDataURI()535     public final boolean handleAllWebDataURI() {
536         return hasCategory(Intent.CATEGORY_APP_BROWSER) ||
537                 (handlesWebUris(false) && countDataAuthorities() == 0);
538     }
539 
540     /**
541      * Return if this filter handles HTTP or HTTPS data URIs.
542      *
543      * @return True if the filter handles ACTION_VIEW/CATEGORY_BROWSABLE,
544      * has at least one HTTP or HTTPS data URI pattern defined, and optionally
545      * does not define any non-http/https data URI patterns.
546      *
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      *
551      * @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.
554      * @hide
555      */
handlesWebUris(boolean onlyWebSchemes)556     public final boolean handlesWebUris(boolean onlyWebSchemes) {
557         // 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);
569             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                 }
584             }
585         }
586 
587         // 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;
591     }
592 
593     /**
594      * 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      */
needsVerification()606     public final boolean needsVerification() {
607         return getAutoVerify() && handlesWebUris(true);
608     }
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      */
isVerified()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      */
setVerified(boolean verified)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     /**
638      * 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      */
addAction(String action)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      */
countActions()653     public final int countActions() {
654         return mActions.size();
655     }
656 
657     /**
658      * Return an action in the filter.
659      */
getAction(int index)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      */
hasAction(String action)672     public final boolean hasAction(String action) {
673         return action != null && mActions.contains(action);
674     }
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      *
682      * @return True if the action is listed in the filter.
683      */
matchAction(String action)684     public final boolean matchAction(String action) {
685         return hasAction(action);
686     }
687 
688     /**
689      * Return an iterator over the filter's actions.  If there are no actions,
690      * returns null.
691      */
actionsIterator()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      *
702      * <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      *
708      * <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      */
addDataType(String type)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      */
hasDataType(String type)746     public final boolean hasDataType(String type) {
747         return mDataTypes != null && findMimeType(type);
748     }
749 
750     /** @hide */
hasExactDataType(String type)751     public final boolean hasExactDataType(String type) {
752         return mDataTypes != null && mDataTypes.contains(type);
753     }
754 
755     /**
756      * Return the number of data types in the filter.
757      */
countDataTypes()758     public final int countDataTypes() {
759         return mDataTypes != null ? mDataTypes.size() : 0;
760     }
761 
762     /**
763      * Return a data type in the filter.
764      */
getDataType(int index)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      */
typesIterator()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      *
782      * <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      *
788      * @param scheme Name of the scheme to match, i.e. "http".
789      *
790      * @see #matchData
791      */
addDataScheme(String scheme)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      */
countDataSchemes()802     public final int countDataSchemes() {
803         return mDataSchemes != null ? mDataSchemes.size() : 0;
804     }
805 
806     /**
807      * Return a data scheme in the filter.
808      */
getDataScheme(int index)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      */
hasDataScheme(String scheme)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      */
schemesIterator()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 
AuthorityEntry(String host, String port)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 
AuthorityEntry(Parcel src)850         AuthorityEntry(Parcel src) {
851             mOrigHost = src.readString();
852             mHost = src.readString();
853             mWild = src.readInt() != 0;
854             mPort = src.readInt();
855         }
856 
writeToParcel(Parcel dest)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 
getHost()864         public String getHost() {
865             return mOrigHost;
866         }
867 
getPort()868         public int getPort() {
869             return mPort;
870         }
871 
872         /** @hide */
match(AuthorityEntry other)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 
886         @Override
equals(Object obj)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 
895         /**
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          */
match(Uri data)905         public int match(Uri data) {
906             String host = data.getHost();
907             if (host == null) {
908                 return NO_MATCH_DATA;
909             }
910             if (false) Log.v("IntentFilter",
911                     "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         }
929     }
930 
931     /**
932      * 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      *
938      * <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      *
946      * @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      */
addDataSchemeSpecificPart(String ssp, int type)956     public final void addDataSchemeSpecificPart(String ssp, int type) {
957         addDataSchemeSpecificPart(new PatternMatcher(ssp, type));
958     }
959 
960     /** @hide */
addDataSchemeSpecificPart(PatternMatcher ssp)961     public final void addDataSchemeSpecificPart(PatternMatcher ssp) {
962         if (mDataSchemeSpecificParts == null) {
963             mDataSchemeSpecificParts = new ArrayList<PatternMatcher>();
964         }
965         mDataSchemeSpecificParts.add(ssp);
966     }
967 
968     /**
969      * Return the number of data scheme specific parts in the filter.
970      */
countDataSchemeSpecificParts()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      */
getDataSchemeSpecificPart(int index)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      */
hasDataSchemeSpecificPart(String data)992     public final boolean hasDataSchemeSpecificPart(String data) {
993         if (mDataSchemeSpecificParts == null) {
994             return false;
995         }
996         final int numDataSchemeSpecificParts = mDataSchemeSpecificParts.size();
997         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 
1006     /** @hide */
hasDataSchemeSpecificPart(PatternMatcher ssp)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 
1021     /**
1022      * Return an iterator over the filter's data scheme specific parts.
1023      */
schemeSpecificPartsIterator()1024     public final Iterator<PatternMatcher> schemeSpecificPartsIterator() {
1025         return mDataSchemeSpecificParts != null ? mDataSchemeSpecificParts.iterator() : null;
1026     }
1027 
1028     /**
1029      * 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      *
1035      * <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      *
1041      * @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      */
addDataAuthority(String host, String port)1049     public final void addDataAuthority(String host, String port) {
1050         if (port != null) port = port.intern();
1051         addDataAuthority(new AuthorityEntry(host.intern(), port));
1052     }
1053 
1054     /** @hide */
addDataAuthority(AuthorityEntry ent)1055     public final void addDataAuthority(AuthorityEntry ent) {
1056         if (mDataAuthorities == null) mDataAuthorities =
1057                 new ArrayList<AuthorityEntry>();
1058         mDataAuthorities.add(ent);
1059     }
1060 
1061     /**
1062      * Return the number of data authorities in the filter.
1063      */
countDataAuthorities()1064     public final int countDataAuthorities() {
1065         return mDataAuthorities != null ? mDataAuthorities.size() : 0;
1066     }
1067 
1068     /**
1069      * Return a data authority in the filter.
1070      */
getDataAuthority(int index)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      */
hasDataAuthority(Uri data)1085     public final boolean hasDataAuthority(Uri data) {
1086         return matchDataAuthority(data) >= 0;
1087     }
1088 
1089     /** @hide */
hasDataAuthority(AuthorityEntry auth)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 
1103     /**
1104      * Return an iterator over the filter's data authorities.
1105      */
authoritiesIterator()1106     public final Iterator<AuthorityEntry> authoritiesIterator() {
1107         return mDataAuthorities != null ? mDataAuthorities.iterator() : null;
1108     }
1109 
1110     /**
1111      * Add a new Intent data path to match against.  The filter must
1112      * 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      */
addDataPath(String path, int type)1136     public final void addDataPath(String path, int type) {
1137         addDataPath(new PatternMatcher(path.intern(), type));
1138     }
1139 
1140     /** @hide */
addDataPath(PatternMatcher path)1141     public final void addDataPath(PatternMatcher path) {
1142         if (mDataPaths == null) mDataPaths = new ArrayList<PatternMatcher>();
1143         mDataPaths.add(path);
1144     }
1145 
1146     /**
1147      * Return the number of data paths in the filter.
1148      */
countDataPaths()1149     public final int countDataPaths() {
1150         return mDataPaths != null ? mDataPaths.size() : 0;
1151     }
1152 
1153     /**
1154      * Return a data path in the filter.
1155      */
getDataPath(int index)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      */
hasDataPath(String data)1171     public final boolean hasDataPath(String data) {
1172         if (mDataPaths == null) {
1173             return false;
1174         }
1175         final int numDataPaths = mDataPaths.size();
1176         for (int i = 0; i < numDataPaths; i++) {
1177             final PatternMatcher pe = mDataPaths.get(i);
1178             if (pe.match(data)) {
1179                 return true;
1180             }
1181         }
1182         return false;
1183     }
1184 
1185     /** @hide */
hasDataPath(PatternMatcher path)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 
1200     /**
1201      * Return an iterator over the filter's data paths.
1202      */
pathsIterator()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      */
matchDataAuthority(Uri data)1217     public final int matchDataAuthority(Uri data) {
1218         if (mDataAuthorities == null || data == null) {
1219             return NO_MATCH_DATA;
1220         }
1221         final int numDataAuthorities = mDataAuthorities.size();
1222         for (int i = 0; i < numDataAuthorities; i++) {
1223             final AuthorityEntry ae = mDataAuthorities.get(i);
1224             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
1236      * 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.
1241      *
1242      * <p>Be aware that to match against an authority, you must also specify a base
1243      * 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      *
1249      * <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      *
1256      * @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      */
matchData(String type, String scheme, Uri data)1270     public final int matchData(String type, String scheme, Uri data) {
1271         final ArrayList<String> types = mDataTypes;
1272         final ArrayList<String> schemes = mDataSchemes;
1273 
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 
1288             final ArrayList<PatternMatcher> schemeSpecificParts = mDataSchemeSpecificParts;
1289             if (schemeSpecificParts != null && data != null) {
1290                 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                         }
1307                     } else {
1308                         return NO_MATCH_DATA;
1309                     }
1310                 }
1311             }
1312             // If neither an ssp nor an authority matched, we're done.
1313             if (match == NO_MATCH_DATA) {
1314                 return NO_MATCH_DATA;
1315             }
1316         } 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      */
addCategory(String category)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      */
countCategories()1366     public final int countCategories() {
1367         return mCategories != null ? mCategories.size() : 0;
1368     }
1369 
1370     /**
1371      * Return a category in the filter.
1372      */
getCategory(int index)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      */
hasCategory(String category)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.
1390      *
1391      * @return Iterator if this filter has categories or {@code null} if none.
1392      */
categoriesIterator()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      */
matchCategories(Set<String> categories)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,
1442      * {@link #NO_MATCH_ACTION} if the action didn't match, or
1443      * {@link #NO_MATCH_CATEGORY} if one or more categories didn't match.
1444      *
1445      * @see #match(String, String, String, android.net.Uri , Set, String)
1446      */
match(ContentResolver resolver, Intent intent, boolean resolve, String logTag)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,
1472      * {@link #NO_MATCH_ACTION} if the action didn't match, or
1473      * {@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      */
match(String action, String type, String scheme, Uri data, Set<String> categories, String logTag)1482     public final int match(String action, String type, String scheme,
1483             Uri data, Set<String> categories, String logTag) {
1484         if (action != null && !matchAction(action)) {
1485             if (false) Log.v(
1486                 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) {
1492             if (false) {
1493                 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 
1505         String categoryMismatch = matchCategories(categories);
1506         if (categoryMismatch != null) {
1507             if (false) {
1508                 Log.v(logTag, "No matching category " + categoryMismatch + " for " + this);
1509             }
1510             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      */
writeToXml(XmlSerializer serializer)1527     public void writeToXml(XmlSerializer serializer) throws IOException {
1528 
1529         if (getAutoVerify()) {
1530             serializer.attribute(null, AUTO_VERIFY_STR, Boolean.toString(true));
1531         }
1532 
1533         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         }
1559         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         }
1576         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 
readFromXml(XmlPullParser parser)1605     public void readFromXml(XmlPullParser parser) throws XmlPullParserException,
1606             IOException {
1607         String autoVerify = parser.getAttributeValue(null, AUTO_VERIFY_STR);
1608         setAutoVerify(TextUtils.isEmpty(autoVerify) ? false : Boolean.getBoolean(autoVerify));
1609 
1610         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                 }
1644             } 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                 }
1653             } 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 
dump(Printer du, String prefix)1675     public void dump(Printer du, String prefix) {
1676         StringBuilder sb = new StringBuilder(256);
1677         if (mActions.size() > 0) {
1678             Iterator<String> it = mActions.iterator();
1679             while (it.hasNext()) {
1680                 sb.setLength(0);
1681                 sb.append(prefix); sb.append("Action: \"");
1682                         sb.append(it.next()); sb.append("\"");
1683                 du.println(sb.toString());
1684             }
1685         }
1686         if (mCategories != null) {
1687             Iterator<String> it = mCategories.iterator();
1688             while (it.hasNext()) {
1689                 sb.setLength(0);
1690                 sb.append(prefix); sb.append("Category: \"");
1691                         sb.append(it.next()); sb.append("\"");
1692                 du.println(sb.toString());
1693             }
1694         }
1695         if (mDataSchemes != null) {
1696             Iterator<String> it = mDataSchemes.iterator();
1697             while (it.hasNext()) {
1698                 sb.setLength(0);
1699                 sb.append(prefix); sb.append("Scheme: \"");
1700                         sb.append(it.next()); sb.append("\"");
1701                 du.println(sb.toString());
1702             }
1703         }
1704         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         }
1714         if (mDataAuthorities != null) {
1715             Iterator<AuthorityEntry> it = mDataAuthorities.iterator();
1716             while (it.hasNext()) {
1717                 AuthorityEntry ae = it.next();
1718                 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());
1724             }
1725         }
1726         if (mDataPaths != null) {
1727             Iterator<PatternMatcher> it = mDataPaths.iterator();
1728             while (it.hasNext()) {
1729                 PatternMatcher pe = it.next();
1730                 sb.setLength(0);
1731                 sb.append(prefix); sb.append("Path: \"");
1732                         sb.append(pe); sb.append("\"");
1733                 du.println(sb.toString());
1734             }
1735         }
1736         if (mDataTypes != null) {
1737             Iterator<String> it = mDataTypes.iterator();
1738             while (it.hasNext()) {
1739                 sb.setLength(0);
1740                 sb.append(prefix); sb.append("Type: \"");
1741                         sb.append(it.next()); sb.append("\"");
1742                 du.println(sb.toString());
1743             }
1744         }
1745         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         }
1751         {
1752             sb.setLength(0);
1753             sb.append(prefix); sb.append("AutoVerify="); sb.append(getAutoVerify());
1754             du.println(sb.toString());
1755         }
1756     }
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 
describeContents()1769     public final int describeContents() {
1770         return 0;
1771     }
1772 
writeToParcel(Parcel dest, int flags)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         }
1793         if (mDataSchemeSpecificParts != null) {
1794             final int N = mDataSchemeSpecificParts.size();
1795             dest.writeInt(N);
1796             for (int i=0; i<N; i++) {
1797                 mDataSchemeSpecificParts.get(i).writeToParcel(dest, flags);
1798             }
1799         } else {
1800             dest.writeInt(0);
1801         }
1802         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++) {
1815                 mDataPaths.get(i).writeToParcel(dest, flags);
1816             }
1817         } else {
1818             dest.writeInt(0);
1819         }
1820         dest.writeInt(mPriority);
1821         dest.writeInt(mHasPartialTypes ? 1 : 0);
1822         dest.writeInt(getAutoVerify() ? 1 : 0);
1823     }
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      */
debugCheck()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 
IntentFilter(Parcel source)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) {
1868             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);
1876             for (int i=0; i<N; i++) {
1877                 mDataAuthorities.add(new AuthorityEntry(source));
1878             }
1879         }
1880         N = source.readInt();
1881         if (N > 0) {
1882             mDataPaths = new ArrayList<PatternMatcher>(N);
1883             for (int i=0; i<N; i++) {
1884                 mDataPaths.add(new PatternMatcher(source));
1885             }
1886         }
1887         mPriority = source.readInt();
1888         mHasPartialTypes = source.readInt() > 0;
1889         setAutoVerify(source.readInt() > 0);
1890     }
1891 
findMimeType(String type)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...
1922                 final int numTypes = t.size();
1923                 for (int i = 0; i < numTypes; i++) {
1924                     final String v = t.get(i);
1925                     if (type.regionMatches(0, v, 0, slashpos+1)) {
1926                         return true;
1927                     }
1928                 }
1929             }
1930         }
1931 
1932         return false;
1933     }
1934 
1935     /**
1936      * @hide
1937      */
getHostsList()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      */
getHosts()1953     public String[] getHosts() {
1954         ArrayList<String> list = getHostsList();
1955         return list.toArray(new String[list.size()]);
1956     }
1957 }
1958