IntentFilter.java revision 43a17654cf4bfe7f1ec22bd8b7b32daccdf27c09
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
17package android.content;
18
19import org.xmlpull.v1.XmlPullParser;
20import org.xmlpull.v1.XmlPullParserException;
21import org.xmlpull.v1.XmlSerializer;
22
23
24import java.io.IOException;
25import java.util.ArrayList;
26import java.util.Iterator;
27import java.util.Set;
28
29import android.net.Uri;
30import android.os.Parcel;
31import android.os.Parcelable;
32import android.os.PatternMatcher;
33import android.util.AndroidException;
34import android.util.Log;
35import android.util.Printer;
36
37import com.android.internal.util.XmlUtils;
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 #addDataAuthority},
55 * {@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 * <p>A match is based on the following rules.  Note that
76 * for an IntentFilter to match an Intent, three conditions must hold:
77 * the <strong>action</strong> and <strong>category</strong> must match, and
78 * the data (both the <strong>data type</strong> and
79 * <strong>data scheme+authority+path</strong> if specified) must match.
80 *
81 * <p><strong>Action</strong> matches if any of the given values match the
82 * Intent action, <em>or</em> if no actions were specified in the filter.
83 *
84 * <p><strong>Data Type</strong> matches if any of the given values match the
85 * Intent type.  The Intent
86 * type is determined by calling {@link Intent#resolveType}.  A wildcard can be
87 * used for the MIME sub-type, in both the Intent and IntentFilter, so that the
88 * type "audio/*" will match "audio/mpeg", "audio/aiff", "audio/*", etc.
89 * <em>Note that MIME type matching here is <b>case sensitive</b>, unlike
90 * formal RFC MIME types!</em>  You should thus always use lower case letters
91 * for your MIME types.
92 *
93 * <p><strong>Data Scheme</strong> matches if any of the given values match the
94 * Intent data's scheme.
95 * The Intent scheme is determined by calling {@link Intent#getData}
96 * and {@link android.net.Uri#getScheme} on that URI.
97 * <em>Note that scheme matching here is <b>case sensitive</b>, unlike
98 * formal RFC schemes!</em>  You should thus always use lower case letters
99 * for your schemes.
100 *
101 * <p><strong>Data Authority</strong> matches if any of the given values match
102 * the Intent's data authority <em>and</em> one of the data scheme's in the filter
103 * has matched the Intent, <em>or</em> no authories were supplied in the filter.
104 * The Intent authority is determined by calling
105 * {@link Intent#getData} and {@link android.net.Uri#getAuthority} on that URI.
106 * <em>Note that authority matching here is <b>case sensitive</b>, unlike
107 * formal RFC host names!</em>  You should thus always use lower case letters
108 * for your authority.
109 *
110 * <p><strong>Data Path</strong> matches if any of the given values match the
111 * Intent's data path <em>and</em> both a scheme and authority in the filter
112 * has matched against the Intent, <em>or</em> no paths were supplied in the
113 * filter.  The Intent authority is determined by calling
114 * {@link Intent#getData} and {@link android.net.Uri#getPath} on that URI.
115 *
116 * <p><strong>Categories</strong> match if <em>all</em> of the categories in
117 * the Intent match categories given in the filter.  Extra categories in the
118 * filter that are not in the Intent will not cause the match to fail.  Note
119 * that unlike the action, an IntentFilter with no categories
120 * will only match an Intent that does not have any categories.
121 */
122public class IntentFilter implements Parcelable {
123    private static final String SGLOB_STR = "sglob";
124    private static final String PREFIX_STR = "prefix";
125    private static final String LITERAL_STR = "literal";
126    private static final String PATH_STR = "path";
127    private static final String PORT_STR = "port";
128    private static final String HOST_STR = "host";
129    private static final String AUTH_STR = "auth";
130    private static final String SCHEME_STR = "scheme";
131    private static final String TYPE_STR = "type";
132    private static final String CAT_STR = "cat";
133    private static final String NAME_STR = "name";
134    private static final String ACTION_STR = "action";
135
136    /**
137     * The filter {@link #setPriority} value at which system high-priority
138     * receivers are placed; that is, receivers that should execute before
139     * application code. Applications should never use filters with this or
140     * higher priorities.
141     *
142     * @see #setPriority
143     */
144    public static final int SYSTEM_HIGH_PRIORITY = 1000;
145
146    /**
147     * The filter {@link #setPriority} value at which system low-priority
148     * receivers are placed; that is, receivers that should execute after
149     * application code. Applications should never use filters with this or
150     * lower priorities.
151     *
152     * @see #setPriority
153     */
154    public static final int SYSTEM_LOW_PRIORITY = -1000;
155
156    /**
157     * The part of a match constant that describes the category of match
158     * that occurred.  May be either {@link #MATCH_CATEGORY_EMPTY},
159     * {@link #MATCH_CATEGORY_SCHEME}, {@link #MATCH_CATEGORY_HOST},
160     * {@link #MATCH_CATEGORY_PORT},
161     * {@link #MATCH_CATEGORY_PATH}, or {@link #MATCH_CATEGORY_TYPE}.  Higher
162     * values indicate a better match.
163     */
164    public static final int MATCH_CATEGORY_MASK = 0xfff0000;
165
166    /**
167     * The part of a match constant that applies a quality adjustment to the
168     * basic category of match.  The value {@link #MATCH_ADJUSTMENT_NORMAL}
169     * is no adjustment; higher numbers than that improve the quality, while
170     * lower numbers reduce it.
171     */
172    public static final int MATCH_ADJUSTMENT_MASK = 0x000ffff;
173
174    /**
175     * Quality adjustment applied to the category of match that signifies
176     * the default, base value; higher numbers improve the quality while
177     * lower numbers reduce it.
178     */
179    public static final int MATCH_ADJUSTMENT_NORMAL = 0x8000;
180
181    /**
182     * The filter matched an intent that had no data specified.
183     */
184    public static final int MATCH_CATEGORY_EMPTY = 0x0100000;
185    /**
186     * The filter matched an intent with the same data URI scheme.
187     */
188    public static final int MATCH_CATEGORY_SCHEME = 0x0200000;
189    /**
190     * The filter matched an intent with the same data URI scheme and
191     * authority host.
192     */
193    public static final int MATCH_CATEGORY_HOST = 0x0300000;
194    /**
195     * The filter matched an intent with the same data URI scheme and
196     * authority host and port.
197     */
198    public static final int MATCH_CATEGORY_PORT = 0x0400000;
199    /**
200     * The filter matched an intent with the same data URI scheme,
201     * authority, and path.
202     */
203    public static final int MATCH_CATEGORY_PATH = 0x0500000;
204    /**
205     * The filter matched an intent with the same data MIME type.
206     */
207    public static final int MATCH_CATEGORY_TYPE = 0x0600000;
208
209    /**
210     * The filter didn't match due to different MIME types.
211     */
212    public static final int NO_MATCH_TYPE = -1;
213    /**
214     * The filter didn't match due to different data URIs.
215     */
216    public static final int NO_MATCH_DATA = -2;
217    /**
218     * The filter didn't match due to different actions.
219     */
220    public static final int NO_MATCH_ACTION = -3;
221    /**
222     * The filter didn't match because it required one or more categories
223     * that were not in the Intent.
224     */
225    public static final int NO_MATCH_CATEGORY = -4;
226
227    private int mPriority;
228    private final ArrayList<String> mActions;
229    private ArrayList<String> mCategories = null;
230    private ArrayList<String> mDataSchemes = null;
231    private ArrayList<AuthorityEntry> mDataAuthorities = null;
232    private ArrayList<PatternMatcher> mDataPaths = null;
233    private ArrayList<String> mDataTypes = null;
234    private boolean mHasPartialTypes = false;
235
236    // These functions are the start of more optimized code for managing
237    // the string sets...  not yet implemented.
238
239    private static int findStringInSet(String[] set, String string,
240            int[] lengths, int lenPos) {
241        if (set == null) return -1;
242        final int N = lengths[lenPos];
243        for (int i=0; i<N; i++) {
244            if (set[i].equals(string)) return i;
245        }
246        return -1;
247    }
248
249    private static String[] addStringToSet(String[] set, String string,
250            int[] lengths, int lenPos) {
251        if (findStringInSet(set, string, lengths, lenPos) >= 0) return set;
252        if (set == null) {
253            set = new String[2];
254            set[0] = string;
255            lengths[lenPos] = 1;
256            return set;
257        }
258        final int N = lengths[lenPos];
259        if (N < set.length) {
260            set[N] = string;
261            lengths[lenPos] = N+1;
262            return set;
263        }
264
265        String[] newSet = new String[(N*3)/2 + 2];
266        System.arraycopy(set, 0, newSet, 0, N);
267        set = newSet;
268        set[N] = string;
269        lengths[lenPos] = N+1;
270        return set;
271    }
272
273    private static String[] removeStringFromSet(String[] set, String string,
274            int[] lengths, int lenPos) {
275        int pos = findStringInSet(set, string, lengths, lenPos);
276        if (pos < 0) return set;
277        final int N = lengths[lenPos];
278        if (N > (set.length/4)) {
279            int copyLen = N-(pos+1);
280            if (copyLen > 0) {
281                System.arraycopy(set, pos+1, set, pos, copyLen);
282            }
283            set[N-1] = null;
284            lengths[lenPos] = N-1;
285            return set;
286        }
287
288        String[] newSet = new String[set.length/3];
289        if (pos > 0) System.arraycopy(set, 0, newSet, 0, pos);
290        if ((pos+1) < N) System.arraycopy(set, pos+1, newSet, pos, N-(pos+1));
291        return newSet;
292    }
293
294    /**
295     * This exception is thrown when a given MIME type does not have a valid
296     * syntax.
297     */
298    public static class MalformedMimeTypeException extends AndroidException {
299        public MalformedMimeTypeException() {
300        }
301
302        public MalformedMimeTypeException(String name) {
303            super(name);
304        }
305    };
306
307    /**
308     * Create a new IntentFilter instance with a specified action and MIME
309     * type, where you know the MIME type is correctly formatted.  This catches
310     * the {@link MalformedMimeTypeException} exception that the constructor
311     * can call and turns it into a runtime exception.
312     *
313     * @param action The action to match, i.e. Intent.ACTION_VIEW.
314     * @param dataType The type to match, i.e. "vnd.android.cursor.dir/person".
315     *
316     * @return A new IntentFilter for the given action and type.
317     *
318     * @see #IntentFilter(String, String)
319     */
320    public static IntentFilter create(String action, String dataType) {
321        try {
322            return new IntentFilter(action, dataType);
323        } catch (MalformedMimeTypeException e) {
324            throw new RuntimeException("Bad MIME type", e);
325        }
326    }
327
328    /**
329     * New empty IntentFilter.
330     */
331    public IntentFilter() {
332        mPriority = 0;
333        mActions = new ArrayList<String>();
334    }
335
336    /**
337     * New IntentFilter that matches a single action with no data.  If
338     * no data characteristics are subsequently specified, then the
339     * filter will only match intents that contain no data.
340     *
341     * @param action The action to match, i.e. Intent.ACTION_MAIN.
342     */
343    public IntentFilter(String action) {
344        mPriority = 0;
345        mActions = new ArrayList<String>();
346        addAction(action);
347    }
348
349    /**
350     * New IntentFilter that matches a single action and data type.
351     *
352     * <p><em>Note: MIME type matching in the Android framework is
353     * case-sensitive, unlike formal RFC MIME types.  As a result,
354     * you should always write your MIME types with lower case letters,
355     * and any MIME types you receive from outside of Android should be
356     * converted to lower case before supplying them here.</em></p>
357     *
358     * <p>Throws {@link MalformedMimeTypeException} if the given MIME type is
359     * not syntactically correct.
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     */
365    public IntentFilter(String action, String dataType)
366        throws MalformedMimeTypeException {
367        mPriority = 0;
368        mActions = new ArrayList<String>();
369        addAction(action);
370        addDataType(dataType);
371    }
372
373    /**
374     * New IntentFilter containing a copy of an existing filter.
375     *
376     * @param o The original filter to copy.
377     */
378    public IntentFilter(IntentFilter o) {
379        mPriority = o.mPriority;
380        mActions = new ArrayList<String>(o.mActions);
381        if (o.mCategories != null) {
382            mCategories = new ArrayList<String>(o.mCategories);
383        }
384        if (o.mDataTypes != null) {
385            mDataTypes = new ArrayList<String>(o.mDataTypes);
386        }
387        if (o.mDataSchemes != null) {
388            mDataSchemes = new ArrayList<String>(o.mDataSchemes);
389        }
390        if (o.mDataAuthorities != null) {
391            mDataAuthorities = new ArrayList<AuthorityEntry>(o.mDataAuthorities);
392        }
393        if (o.mDataPaths != null) {
394            mDataPaths = new ArrayList<PatternMatcher>(o.mDataPaths);
395        }
396        mHasPartialTypes = o.mHasPartialTypes;
397    }
398
399    /**
400     * Modify priority of this filter.  The default priority is 0. Positive
401     * values will be before the default, lower values will be after it.
402     * Applications must use a value that is larger than
403     * {@link #SYSTEM_LOW_PRIORITY} and smaller than
404     * {@link #SYSTEM_HIGH_PRIORITY} .
405     *
406     * @param priority The new priority value.
407     *
408     * @see #getPriority
409     * @see #SYSTEM_LOW_PRIORITY
410     * @see #SYSTEM_HIGH_PRIORITY
411     */
412    public final void setPriority(int priority) {
413        mPriority = priority;
414    }
415
416    /**
417     * Return the priority of this filter.
418     *
419     * @return The priority of the filter.
420     *
421     * @see #setPriority
422     */
423    public final int getPriority() {
424        return mPriority;
425    }
426
427    /**
428     * Add a new Intent action to match against.  If any actions are included
429     * in the filter, then an Intent's action must be one of those values for
430     * it to match.  If no actions are included, the Intent action is ignored.
431     *
432     * @param action Name of the action to match, i.e. Intent.ACTION_VIEW.
433     */
434    public final void addAction(String action) {
435        if (!mActions.contains(action)) {
436            mActions.add(action.intern());
437        }
438    }
439
440    /**
441     * Return the number of actions in the filter.
442     */
443    public final int countActions() {
444        return mActions.size();
445    }
446
447    /**
448     * Return an action in the filter.
449     */
450    public final String getAction(int index) {
451        return mActions.get(index);
452    }
453
454    /**
455     * Is the given action included in the filter?  Note that if the filter
456     * does not include any actions, false will <em>always</em> be returned.
457     *
458     * @param action The action to look for.
459     *
460     * @return True if the action is explicitly mentioned in the filter.
461     */
462    public final boolean hasAction(String action) {
463        return action != null && mActions.contains(action);
464    }
465
466    /**
467     * Match this filter against an Intent's action.  If the filter does not
468     * specify any actions, the match will always fail.
469     *
470     * @param action The desired action to look for.
471     *
472     * @return True if the action is listed in the filter.
473     */
474    public final boolean matchAction(String action) {
475        return hasAction(action);
476    }
477
478    /**
479     * Return an iterator over the filter's actions.  If there are no actions,
480     * returns null.
481     */
482    public final Iterator<String> actionsIterator() {
483        return mActions != null ? mActions.iterator() : null;
484    }
485
486    /**
487     * Add a new Intent data type to match against.  If any types are
488     * included in the filter, then an Intent's data must be <em>either</em>
489     * one of these types <em>or</em> a matching scheme.  If no data types
490     * are included, then an Intent will only match if it specifies no data.
491     *
492     * <p><em>Note: MIME type matching in the Android framework is
493     * case-sensitive, unlike formal RFC MIME types.  As a result,
494     * you should always write your MIME types with lower case letters,
495     * and any MIME types you receive from outside of Android should be
496     * converted to lower case before supplying them here.</em></p>
497     *
498     * <p>Throws {@link MalformedMimeTypeException} if the given MIME type is
499     * not syntactically correct.
500     *
501     * @param type Name of the data type to match, i.e. "vnd.android.cursor.dir/person".
502     *
503     * @see #matchData
504     */
505    public final void addDataType(String type)
506        throws MalformedMimeTypeException {
507        final int slashpos = type.indexOf('/');
508        final int typelen = type.length();
509        if (slashpos > 0 && typelen >= slashpos+2) {
510            if (mDataTypes == null) mDataTypes = new ArrayList<String>();
511            if (typelen == slashpos+2 && type.charAt(slashpos+1) == '*') {
512                String str = type.substring(0, slashpos);
513                if (!mDataTypes.contains(str)) {
514                    mDataTypes.add(str.intern());
515                }
516                mHasPartialTypes = true;
517            } else {
518                if (!mDataTypes.contains(type)) {
519                    mDataTypes.add(type.intern());
520                }
521            }
522            return;
523        }
524
525        throw new MalformedMimeTypeException(type);
526    }
527
528    /**
529     * Is the given data type included in the filter?  Note that if the filter
530     * does not include any type, false will <em>always</em> be returned.
531     *
532     * @param type The data type to look for.
533     *
534     * @return True if the type is explicitly mentioned in the filter.
535     */
536    public final boolean hasDataType(String type) {
537        return mDataTypes != null && findMimeType(type);
538    }
539
540    /**
541     * Return the number of data types in the filter.
542     */
543    public final int countDataTypes() {
544        return mDataTypes != null ? mDataTypes.size() : 0;
545    }
546
547    /**
548     * Return a data type in the filter.
549     */
550    public final String getDataType(int index) {
551        return mDataTypes.get(index);
552    }
553
554    /**
555     * Return an iterator over the filter's data types.
556     */
557    public final Iterator<String> typesIterator() {
558        return mDataTypes != null ? mDataTypes.iterator() : null;
559    }
560
561    /**
562     * Add a new Intent data scheme to match against.  If any schemes are
563     * included in the filter, then an Intent's data must be <em>either</em>
564     * one of these schemes <em>or</em> a matching data type.  If no schemes
565     * are included, then an Intent will match only if it includes no data.
566     *
567     * <p><em>Note: scheme matching in the Android framework is
568     * case-sensitive, unlike formal RFC schemes.  As a result,
569     * you should always write your schemes with lower case letters,
570     * and any schemes you receive from outside of Android should be
571     * converted to lower case before supplying them here.</em></p>
572     *
573     * @param scheme Name of the scheme to match, i.e. "http".
574     *
575     * @see #matchData
576     */
577    public final void addDataScheme(String scheme) {
578        if (mDataSchemes == null) mDataSchemes = new ArrayList<String>();
579        if (!mDataSchemes.contains(scheme)) {
580            mDataSchemes.add(scheme.intern());
581        }
582    }
583
584    /**
585     * Return the number of data schemes in the filter.
586     */
587    public final int countDataSchemes() {
588        return mDataSchemes != null ? mDataSchemes.size() : 0;
589    }
590
591    /**
592     * Return a data scheme in the filter.
593     */
594    public final String getDataScheme(int index) {
595        return mDataSchemes.get(index);
596    }
597
598    /**
599     * Is the given data scheme included in the filter?  Note that if the
600     * filter does not include any scheme, false will <em>always</em> be
601     * returned.
602     *
603     * @param scheme The data scheme to look for.
604     *
605     * @return True if the scheme is explicitly mentioned in the filter.
606     */
607    public final boolean hasDataScheme(String scheme) {
608        return mDataSchemes != null && mDataSchemes.contains(scheme);
609    }
610
611    /**
612     * Return an iterator over the filter's data schemes.
613     */
614    public final Iterator<String> schemesIterator() {
615        return mDataSchemes != null ? mDataSchemes.iterator() : null;
616    }
617
618    /**
619     * This is an entry for a single authority in the Iterator returned by
620     * {@link #authoritiesIterator()}.
621     */
622    public final static class AuthorityEntry {
623        private final String mOrigHost;
624        private final String mHost;
625        private final boolean mWild;
626        private final int mPort;
627
628        public AuthorityEntry(String host, String port) {
629            mOrigHost = host;
630            mWild = host.length() > 0 && host.charAt(0) == '*';
631            mHost = mWild ? host.substring(1).intern() : host;
632            mPort = port != null ? Integer.parseInt(port) : -1;
633        }
634
635        AuthorityEntry(Parcel src) {
636            mOrigHost = src.readString();
637            mHost = src.readString();
638            mWild = src.readInt() != 0;
639            mPort = src.readInt();
640        }
641
642        void writeToParcel(Parcel dest) {
643            dest.writeString(mOrigHost);
644            dest.writeString(mHost);
645            dest.writeInt(mWild ? 1 : 0);
646            dest.writeInt(mPort);
647        }
648
649        public String getHost() {
650            return mOrigHost;
651        }
652
653        public int getPort() {
654            return mPort;
655        }
656
657        /**
658         * Determine whether this AuthorityEntry matches the given data Uri.
659         * <em>Note that this comparison is case-sensitive, unlike formal
660         * RFC host names.  You thus should always normalize to lower-case.</em>
661         *
662         * @param data The Uri to match.
663         * @return Returns either {@link IntentFilter#NO_MATCH_DATA},
664         * {@link IntentFilter#MATCH_CATEGORY_PORT}, or
665         * {@link IntentFilter#MATCH_CATEGORY_HOST}.
666         */
667        public int match(Uri data) {
668            String host = data.getHost();
669            if (host == null) {
670                return NO_MATCH_DATA;
671            }
672            if (false) Log.v("IntentFilter",
673                    "Match host " + host + ": " + mHost);
674            if (mWild) {
675                if (host.length() < mHost.length()) {
676                    return NO_MATCH_DATA;
677                }
678                host = host.substring(host.length()-mHost.length());
679            }
680            if (host.compareToIgnoreCase(mHost) != 0) {
681                return NO_MATCH_DATA;
682            }
683            if (mPort >= 0) {
684                if (mPort != data.getPort()) {
685                    return NO_MATCH_DATA;
686                }
687                return MATCH_CATEGORY_PORT;
688            }
689            return MATCH_CATEGORY_HOST;
690        }
691    };
692
693    /**
694     * Add a new Intent data authority to match against.  The filter must
695     * include one or more schemes (via {@link #addDataScheme}) for the
696     * authority to be considered.  If any authorities are
697     * included in the filter, then an Intent's data must match one of
698     * them.  If no authorities are included, then only the scheme must match.
699     *
700     * <p><em>Note: host name in the Android framework is
701     * case-sensitive, unlike formal RFC host names.  As a result,
702     * you should always write your host names with lower case letters,
703     * and any host names you receive from outside of Android should be
704     * converted to lower case before supplying them here.</em></p>
705     *
706     * @param host The host part of the authority to match.  May start with a
707     *             single '*' to wildcard the front of the host name.
708     * @param port Optional port part of the authority to match.  If null, any
709     *             port is allowed.
710     *
711     * @see #matchData
712     * @see #addDataScheme
713     */
714    public final void addDataAuthority(String host, String port) {
715        if (mDataAuthorities == null) mDataAuthorities =
716                new ArrayList<AuthorityEntry>();
717        if (port != null) port = port.intern();
718        mDataAuthorities.add(new AuthorityEntry(host.intern(), port));
719    }
720
721    /**
722     * Return the number of data authorities in the filter.
723     */
724    public final int countDataAuthorities() {
725        return mDataAuthorities != null ? mDataAuthorities.size() : 0;
726    }
727
728    /**
729     * Return a data authority in the filter.
730     */
731    public final AuthorityEntry getDataAuthority(int index) {
732        return mDataAuthorities.get(index);
733    }
734
735    /**
736     * Is the given data authority included in the filter?  Note that if the
737     * filter does not include any authorities, false will <em>always</em> be
738     * returned.
739     *
740     * @param data The data whose authority is being looked for.
741     *
742     * @return Returns true if the data string matches an authority listed in the
743     *         filter.
744     */
745    public final boolean hasDataAuthority(Uri data) {
746        return matchDataAuthority(data) >= 0;
747    }
748
749    /**
750     * Return an iterator over the filter's data authorities.
751     */
752    public final Iterator<AuthorityEntry> authoritiesIterator() {
753        return mDataAuthorities != null ? mDataAuthorities.iterator() : null;
754    }
755
756    /**
757     * Add a new Intent data oath to match against.  The filter must
758     * include one or more schemes (via {@link #addDataScheme}) <em>and</em>
759     * one or more authorities (via {@link #addDataAuthority}) for the
760     * path to be considered.  If any paths are
761     * included in the filter, then an Intent's data must match one of
762     * them.  If no paths are included, then only the scheme/authority must
763     * match.
764     *
765     * <p>The path given here can either be a literal that must directly
766     * match or match against a prefix, or it can be a simple globbing pattern.
767     * If the latter, you can use '*' anywhere in the pattern to match zero
768     * or more instances of the previous character, '.' as a wildcard to match
769     * any character, and '\' to escape the next character.
770     *
771     * @param path Either a raw string that must exactly match the file
772     * path, or a simple pattern, depending on <var>type</var>.
773     * @param type Determines how <var>path</var> will be compared to
774     * determine a match: either {@link PatternMatcher#PATTERN_LITERAL},
775     * {@link PatternMatcher#PATTERN_PREFIX}, or
776     * {@link PatternMatcher#PATTERN_SIMPLE_GLOB}.
777     *
778     * @see #matchData
779     * @see #addDataScheme
780     * @see #addDataAuthority
781     */
782    public final void addDataPath(String path, int type) {
783        if (mDataPaths == null) mDataPaths = new ArrayList<PatternMatcher>();
784        mDataPaths.add(new PatternMatcher(path.intern(), type));
785    }
786
787    /**
788     * Return the number of data paths in the filter.
789     */
790    public final int countDataPaths() {
791        return mDataPaths != null ? mDataPaths.size() : 0;
792    }
793
794    /**
795     * Return a data path in the filter.
796     */
797    public final PatternMatcher getDataPath(int index) {
798        return mDataPaths.get(index);
799    }
800
801    /**
802     * Is the given data path included in the filter?  Note that if the
803     * filter does not include any paths, false will <em>always</em> be
804     * returned.
805     *
806     * @param data The data path to look for.  This is without the scheme
807     *             prefix.
808     *
809     * @return True if the data string matches a path listed in the
810     *         filter.
811     */
812    public final boolean hasDataPath(String data) {
813        if (mDataPaths == null) {
814            return false;
815        }
816        final int numDataPaths = mDataPaths.size();
817        for (int i = 0; i < numDataPaths; i++) {
818            final PatternMatcher pe = mDataPaths.get(i);
819            if (pe.match(data)) {
820                return true;
821            }
822        }
823        return false;
824    }
825
826    /**
827     * Return an iterator over the filter's data paths.
828     */
829    public final Iterator<PatternMatcher> pathsIterator() {
830        return mDataPaths != null ? mDataPaths.iterator() : null;
831    }
832
833    /**
834     * Match this intent filter against the given Intent data.  This ignores
835     * the data scheme -- unlike {@link #matchData}, the authority will match
836     * regardless of whether there is a matching scheme.
837     *
838     * @param data The data whose authority is being looked for.
839     *
840     * @return Returns either {@link #MATCH_CATEGORY_HOST},
841     * {@link #MATCH_CATEGORY_PORT}, {@link #NO_MATCH_DATA}.
842     */
843    public final int matchDataAuthority(Uri data) {
844        if (mDataAuthorities == null) {
845            return NO_MATCH_DATA;
846        }
847        final int numDataAuthorities = mDataAuthorities.size();
848        for (int i = 0; i < numDataAuthorities; i++) {
849            final AuthorityEntry ae = mDataAuthorities.get(i);
850            int match = ae.match(data);
851            if (match >= 0) {
852                return match;
853            }
854        }
855        return NO_MATCH_DATA;
856    }
857
858    /**
859     * Match this filter against an Intent's data (type, scheme and path). If
860     * the filter does not specify any types and does not specify any
861     * schemes/paths, the match will only succeed if the intent does not
862     * also specify a type or data.
863     *
864     * <p>Be aware that to match against an authority, you must also specify a base
865     * scheme the authority is in.  To match against a data path, both a scheme
866     * and authority must be specified.  If the filter does not specify any
867     * types or schemes that it matches against, it is considered to be empty
868     * (any authority or data path given is ignored, as if it were empty as
869     * well).
870     *
871     * <p><em>Note: MIME type, Uri scheme, and host name matching in the
872     * Android framework is case-sensitive, unlike the formal RFC definitions.
873     * As a result, you should always write these elements with lower case letters,
874     * and normalize any MIME types or Uris you receive from
875     * outside of Android to ensure these elements are lower case before
876     * supplying them here.</em></p>
877     *
878     * @param type The desired data type to look for, as returned by
879     *             Intent.resolveType().
880     * @param scheme The desired data scheme to look for, as returned by
881     *               Intent.getScheme().
882     * @param data The full data string to match against, as supplied in
883     *             Intent.data.
884     *
885     * @return Returns either a valid match constant (a combination of
886     * {@link #MATCH_CATEGORY_MASK} and {@link #MATCH_ADJUSTMENT_MASK}),
887     * or one of the error codes {@link #NO_MATCH_TYPE} if the type didn't match
888     * or {@link #NO_MATCH_DATA} if the scheme/path didn't match.
889     *
890     * @see #match
891     */
892    public final int matchData(String type, String scheme, Uri data) {
893        final ArrayList<String> types = mDataTypes;
894        final ArrayList<String> schemes = mDataSchemes;
895        final ArrayList<AuthorityEntry> authorities = mDataAuthorities;
896        final ArrayList<PatternMatcher> paths = mDataPaths;
897
898        int match = MATCH_CATEGORY_EMPTY;
899
900        if (types == null && schemes == null) {
901            return ((type == null && data == null)
902                ? (MATCH_CATEGORY_EMPTY+MATCH_ADJUSTMENT_NORMAL) : NO_MATCH_DATA);
903        }
904
905        if (schemes != null) {
906            if (schemes.contains(scheme != null ? scheme : "")) {
907                match = MATCH_CATEGORY_SCHEME;
908            } else {
909                return NO_MATCH_DATA;
910            }
911
912            if (authorities != null) {
913                int authMatch = matchDataAuthority(data);
914                if (authMatch >= 0) {
915                    if (paths == null) {
916                        match = authMatch;
917                    } else if (hasDataPath(data.getPath())) {
918                        match = MATCH_CATEGORY_PATH;
919                    } else {
920                        return NO_MATCH_DATA;
921                    }
922                } else {
923                    return NO_MATCH_DATA;
924                }
925            }
926        } else {
927            // Special case: match either an Intent with no data URI,
928            // or with a scheme: URI.  This is to give a convenience for
929            // the common case where you want to deal with data in a
930            // content provider, which is done by type, and we don't want
931            // to force everyone to say they handle content: or file: URIs.
932            if (scheme != null && !"".equals(scheme)
933                    && !"content".equals(scheme)
934                    && !"file".equals(scheme)) {
935                return NO_MATCH_DATA;
936            }
937        }
938
939        if (types != null) {
940            if (findMimeType(type)) {
941                match = MATCH_CATEGORY_TYPE;
942            } else {
943                return NO_MATCH_TYPE;
944            }
945        } else {
946            // If no MIME types are specified, then we will only match against
947            // an Intent that does not have a MIME type.
948            if (type != null) {
949                return NO_MATCH_TYPE;
950            }
951        }
952
953        return match + MATCH_ADJUSTMENT_NORMAL;
954    }
955
956    /**
957     * Add a new Intent category to match against.  The semantics of
958     * categories is the opposite of actions -- an Intent includes the
959     * categories that it requires, all of which must be included in the
960     * filter in order to match.  In other words, adding a category to the
961     * filter has no impact on matching unless that category is specified in
962     * the intent.
963     *
964     * @param category Name of category to match, i.e. Intent.CATEGORY_EMBED.
965     */
966    public final void addCategory(String category) {
967        if (mCategories == null) mCategories = new ArrayList<String>();
968        if (!mCategories.contains(category)) {
969            mCategories.add(category.intern());
970        }
971    }
972
973    /**
974     * Return the number of categories in the filter.
975     */
976    public final int countCategories() {
977        return mCategories != null ? mCategories.size() : 0;
978    }
979
980    /**
981     * Return a category in the filter.
982     */
983    public final String getCategory(int index) {
984        return mCategories.get(index);
985    }
986
987    /**
988     * Is the given category included in the filter?
989     *
990     * @param category The category that the filter supports.
991     *
992     * @return True if the category is explicitly mentioned in the filter.
993     */
994    public final boolean hasCategory(String category) {
995        return mCategories != null && mCategories.contains(category);
996    }
997
998    /**
999     * Return an iterator over the filter's categories.
1000     */
1001    public final Iterator<String> categoriesIterator() {
1002        return mCategories != null ? mCategories.iterator() : null;
1003    }
1004
1005    /**
1006     * Match this filter against an Intent's categories.  Each category in
1007     * the Intent must be specified by the filter; if any are not in the
1008     * filter, the match fails.
1009     *
1010     * @param categories The categories included in the intent, as returned by
1011     *                   Intent.getCategories().
1012     *
1013     * @return If all categories match (success), null; else the name of the
1014     *         first category that didn't match.
1015     */
1016    public final String matchCategories(Set<String> categories) {
1017        if (categories == null) {
1018            return null;
1019        }
1020
1021        Iterator<String> it = categories.iterator();
1022
1023        if (mCategories == null) {
1024            return it.hasNext() ? it.next() : null;
1025        }
1026
1027        while (it.hasNext()) {
1028            final String category = it.next();
1029            if (!mCategories.contains(category)) {
1030                return category;
1031            }
1032        }
1033
1034        return null;
1035    }
1036
1037    /**
1038     * Test whether this filter matches the given <var>intent</var>.
1039     *
1040     * @param intent The Intent to compare against.
1041     * @param resolve If true, the intent's type will be resolved by calling
1042     *                Intent.resolveType(); otherwise a simple match against
1043     *                Intent.type will be performed.
1044     * @param logTag Tag to use in debugging messages.
1045     *
1046     * @return Returns either a valid match constant (a combination of
1047     * {@link #MATCH_CATEGORY_MASK} and {@link #MATCH_ADJUSTMENT_MASK}),
1048     * or one of the error codes {@link #NO_MATCH_TYPE} if the type didn't match,
1049     * {@link #NO_MATCH_DATA} if the scheme/path didn't match,
1050     * {@link #NO_MATCH_ACTION if the action didn't match, or
1051     * {@link #NO_MATCH_CATEGORY} if one or more categories didn't match.
1052     *
1053     * @return How well the filter matches.  Negative if it doesn't match,
1054     *         zero or positive positive value if it does with a higher
1055     *         value representing a better match.
1056     *
1057     * @see #match(String, String, String, android.net.Uri , Set, String)
1058     */
1059    public final int match(ContentResolver resolver, Intent intent,
1060            boolean resolve, String logTag) {
1061        String type = resolve ? intent.resolveType(resolver) : intent.getType();
1062        return match(intent.getAction(), type, intent.getScheme(),
1063                     intent.getData(), intent.getCategories(), logTag);
1064    }
1065
1066    /**
1067     * Test whether this filter matches the given intent data.  A match is
1068     * only successful if the actions and categories in the Intent match
1069     * against the filter, as described in {@link IntentFilter}; in that case,
1070     * the match result returned will be as per {@link #matchData}.
1071     *
1072     * @param action The intent action to match against (Intent.getAction).
1073     * @param type The intent type to match against (Intent.resolveType()).
1074     * @param scheme The data scheme to match against (Intent.getScheme()).
1075     * @param data The data URI to match against (Intent.getData()).
1076     * @param categories The categories to match against
1077     *                   (Intent.getCategories()).
1078     * @param logTag Tag to use in debugging messages.
1079     *
1080     * @return Returns either a valid match constant (a combination of
1081     * {@link #MATCH_CATEGORY_MASK} and {@link #MATCH_ADJUSTMENT_MASK}),
1082     * or one of the error codes {@link #NO_MATCH_TYPE} if the type didn't match,
1083     * {@link #NO_MATCH_DATA} if the scheme/path didn't match,
1084     * {@link #NO_MATCH_ACTION if the action didn't match, or
1085     * {@link #NO_MATCH_CATEGORY} if one or more categories didn't match.
1086     *
1087     * @see #matchData
1088     * @see Intent#getAction
1089     * @see Intent#resolveType
1090     * @see Intent#getScheme
1091     * @see Intent#getData
1092     * @see Intent#getCategories
1093     */
1094    public final int match(String action, String type, String scheme,
1095            Uri data, Set<String> categories, String logTag) {
1096        if (action != null && !matchAction(action)) {
1097            if (false) Log.v(
1098                logTag, "No matching action " + action + " for " + this);
1099            return NO_MATCH_ACTION;
1100        }
1101
1102        int dataMatch = matchData(type, scheme, data);
1103        if (dataMatch < 0) {
1104            if (false) {
1105                if (dataMatch == NO_MATCH_TYPE) {
1106                    Log.v(logTag, "No matching type " + type
1107                          + " for " + this);
1108                }
1109                if (dataMatch == NO_MATCH_DATA) {
1110                    Log.v(logTag, "No matching scheme/path " + data
1111                          + " for " + this);
1112                }
1113            }
1114            return dataMatch;
1115        }
1116
1117        String categoryMismatch = matchCategories(categories);
1118        if (categoryMismatch != null) {
1119            if (false) {
1120                Log.v(logTag, "No matching category " + categoryMismatch + " for " + this);
1121            }
1122            return NO_MATCH_CATEGORY;
1123        }
1124
1125        // It would be nice to treat container activities as more
1126        // important than ones that can be embedded, but this is not the way...
1127        if (false) {
1128            if (categories != null) {
1129                dataMatch -= mCategories.size() - categories.size();
1130            }
1131        }
1132
1133        return dataMatch;
1134    }
1135
1136    /**
1137     * Write the contents of the IntentFilter as an XML stream.
1138     */
1139    public void writeToXml(XmlSerializer serializer) throws IOException {
1140        int N = countActions();
1141        for (int i=0; i<N; i++) {
1142            serializer.startTag(null, ACTION_STR);
1143            serializer.attribute(null, NAME_STR, mActions.get(i));
1144            serializer.endTag(null, ACTION_STR);
1145        }
1146        N = countCategories();
1147        for (int i=0; i<N; i++) {
1148            serializer.startTag(null, CAT_STR);
1149            serializer.attribute(null, NAME_STR, mCategories.get(i));
1150            serializer.endTag(null, CAT_STR);
1151        }
1152        N = countDataTypes();
1153        for (int i=0; i<N; i++) {
1154            serializer.startTag(null, TYPE_STR);
1155            String type = mDataTypes.get(i);
1156            if (type.indexOf('/') < 0) type = type + "/*";
1157            serializer.attribute(null, NAME_STR, type);
1158            serializer.endTag(null, TYPE_STR);
1159        }
1160        N = countDataSchemes();
1161        for (int i=0; i<N; i++) {
1162            serializer.startTag(null, SCHEME_STR);
1163            serializer.attribute(null, NAME_STR, mDataSchemes.get(i));
1164            serializer.endTag(null, SCHEME_STR);
1165        }
1166        N = countDataAuthorities();
1167        for (int i=0; i<N; i++) {
1168            serializer.startTag(null, AUTH_STR);
1169            AuthorityEntry ae = mDataAuthorities.get(i);
1170            serializer.attribute(null, HOST_STR, ae.getHost());
1171            if (ae.getPort() >= 0) {
1172                serializer.attribute(null, PORT_STR, Integer.toString(ae.getPort()));
1173            }
1174            serializer.endTag(null, AUTH_STR);
1175        }
1176        N = countDataPaths();
1177        for (int i=0; i<N; i++) {
1178            serializer.startTag(null, PATH_STR);
1179            PatternMatcher pe = mDataPaths.get(i);
1180            switch (pe.getType()) {
1181                case PatternMatcher.PATTERN_LITERAL:
1182                    serializer.attribute(null, LITERAL_STR, pe.getPath());
1183                    break;
1184                case PatternMatcher.PATTERN_PREFIX:
1185                    serializer.attribute(null, PREFIX_STR, pe.getPath());
1186                    break;
1187                case PatternMatcher.PATTERN_SIMPLE_GLOB:
1188                    serializer.attribute(null, SGLOB_STR, pe.getPath());
1189                    break;
1190            }
1191            serializer.endTag(null, PATH_STR);
1192        }
1193    }
1194
1195    public void readFromXml(XmlPullParser parser) throws XmlPullParserException,
1196            IOException {
1197        int outerDepth = parser.getDepth();
1198        int type;
1199        while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1200               && (type != XmlPullParser.END_TAG
1201                       || parser.getDepth() > outerDepth)) {
1202            if (type == XmlPullParser.END_TAG
1203                    || type == XmlPullParser.TEXT) {
1204                continue;
1205            }
1206
1207            String tagName = parser.getName();
1208            if (tagName.equals(ACTION_STR)) {
1209                String name = parser.getAttributeValue(null, NAME_STR);
1210                if (name != null) {
1211                    addAction(name);
1212                }
1213            } else if (tagName.equals(CAT_STR)) {
1214                String name = parser.getAttributeValue(null, NAME_STR);
1215                if (name != null) {
1216                    addCategory(name);
1217                }
1218            } else if (tagName.equals(TYPE_STR)) {
1219                String name = parser.getAttributeValue(null, NAME_STR);
1220                if (name != null) {
1221                    try {
1222                        addDataType(name);
1223                    } catch (MalformedMimeTypeException e) {
1224                    }
1225                }
1226            } else if (tagName.equals(SCHEME_STR)) {
1227                String name = parser.getAttributeValue(null, NAME_STR);
1228                if (name != null) {
1229                    addDataScheme(name);
1230                }
1231            } else if (tagName.equals(AUTH_STR)) {
1232                String host = parser.getAttributeValue(null, HOST_STR);
1233                String port = parser.getAttributeValue(null, PORT_STR);
1234                if (host != null) {
1235                    addDataAuthority(host, port);
1236                }
1237            } else if (tagName.equals(PATH_STR)) {
1238                String path = parser.getAttributeValue(null, LITERAL_STR);
1239                if (path != null) {
1240                    addDataPath(path, PatternMatcher.PATTERN_LITERAL);
1241                } else if ((path=parser.getAttributeValue(null, PREFIX_STR)) != null) {
1242                    addDataPath(path, PatternMatcher.PATTERN_PREFIX);
1243                } else if ((path=parser.getAttributeValue(null, SGLOB_STR)) != null) {
1244                    addDataPath(path, PatternMatcher.PATTERN_SIMPLE_GLOB);
1245                }
1246            } else {
1247                Log.w("IntentFilter", "Unknown tag parsing IntentFilter: " + tagName);
1248            }
1249            XmlUtils.skipCurrentTag(parser);
1250        }
1251    }
1252
1253    public void dump(Printer du, String prefix) {
1254        StringBuilder sb = new StringBuilder(256);
1255        if (mActions.size() > 0) {
1256            Iterator<String> it = mActions.iterator();
1257            while (it.hasNext()) {
1258                sb.setLength(0);
1259                sb.append(prefix); sb.append("Action: \"");
1260                        sb.append(it.next()); sb.append("\"");
1261                du.println(sb.toString());
1262            }
1263        }
1264        if (mCategories != null) {
1265            Iterator<String> it = mCategories.iterator();
1266            while (it.hasNext()) {
1267                sb.setLength(0);
1268                sb.append(prefix); sb.append("Category: \"");
1269                        sb.append(it.next()); sb.append("\"");
1270                du.println(sb.toString());
1271            }
1272        }
1273        if (mDataSchemes != null) {
1274            Iterator<String> it = mDataSchemes.iterator();
1275            while (it.hasNext()) {
1276                sb.setLength(0);
1277                sb.append(prefix); sb.append("Scheme: \"");
1278                        sb.append(it.next()); sb.append("\"");
1279                du.println(sb.toString());
1280            }
1281        }
1282        if (mDataAuthorities != null) {
1283            Iterator<AuthorityEntry> it = mDataAuthorities.iterator();
1284            while (it.hasNext()) {
1285                AuthorityEntry ae = it.next();
1286                sb.setLength(0);
1287                sb.append(prefix); sb.append("Authority: \"");
1288                        sb.append(ae.mHost); sb.append("\": ");
1289                        sb.append(ae.mPort);
1290                if (ae.mWild) sb.append(" WILD");
1291                du.println(sb.toString());
1292            }
1293        }
1294        if (mDataPaths != null) {
1295            Iterator<PatternMatcher> it = mDataPaths.iterator();
1296            while (it.hasNext()) {
1297                PatternMatcher pe = it.next();
1298                sb.setLength(0);
1299                sb.append(prefix); sb.append("Path: \"");
1300                        sb.append(pe); sb.append("\"");
1301                du.println(sb.toString());
1302            }
1303        }
1304        if (mDataTypes != null) {
1305            Iterator<String> it = mDataTypes.iterator();
1306            while (it.hasNext()) {
1307                sb.setLength(0);
1308                sb.append(prefix); sb.append("Type: \"");
1309                        sb.append(it.next()); sb.append("\"");
1310                du.println(sb.toString());
1311            }
1312        }
1313        if (mPriority != 0 || mHasPartialTypes) {
1314            sb.setLength(0);
1315            sb.append(prefix); sb.append("mPriority="); sb.append(mPriority);
1316                    sb.append(", mHasPartialTypes="); sb.append(mHasPartialTypes);
1317            du.println(sb.toString());
1318        }
1319    }
1320
1321    public static final Parcelable.Creator<IntentFilter> CREATOR
1322            = new Parcelable.Creator<IntentFilter>() {
1323        public IntentFilter createFromParcel(Parcel source) {
1324            return new IntentFilter(source);
1325        }
1326
1327        public IntentFilter[] newArray(int size) {
1328            return new IntentFilter[size];
1329        }
1330    };
1331
1332    public final int describeContents() {
1333        return 0;
1334    }
1335
1336    public final void writeToParcel(Parcel dest, int flags) {
1337        dest.writeStringList(mActions);
1338        if (mCategories != null) {
1339            dest.writeInt(1);
1340            dest.writeStringList(mCategories);
1341        } else {
1342            dest.writeInt(0);
1343        }
1344        if (mDataSchemes != null) {
1345            dest.writeInt(1);
1346            dest.writeStringList(mDataSchemes);
1347        } else {
1348            dest.writeInt(0);
1349        }
1350        if (mDataTypes != null) {
1351            dest.writeInt(1);
1352            dest.writeStringList(mDataTypes);
1353        } else {
1354            dest.writeInt(0);
1355        }
1356        if (mDataAuthorities != null) {
1357            final int N = mDataAuthorities.size();
1358            dest.writeInt(N);
1359            for (int i=0; i<N; i++) {
1360                mDataAuthorities.get(i).writeToParcel(dest);
1361            }
1362        } else {
1363            dest.writeInt(0);
1364        }
1365        if (mDataPaths != null) {
1366            final int N = mDataPaths.size();
1367            dest.writeInt(N);
1368            for (int i=0; i<N; i++) {
1369                mDataPaths.get(i).writeToParcel(dest, 0);
1370            }
1371        } else {
1372            dest.writeInt(0);
1373        }
1374        dest.writeInt(mPriority);
1375        dest.writeInt(mHasPartialTypes ? 1 : 0);
1376    }
1377
1378    /**
1379     * For debugging -- perform a check on the filter, return true if it passed
1380     * or false if it failed.
1381     *
1382     * {@hide}
1383     */
1384    public boolean debugCheck() {
1385        return true;
1386
1387        // This code looks for intent filters that do not specify data.
1388        /*
1389        if (mActions != null && mActions.size() == 1
1390                && mActions.contains(Intent.ACTION_MAIN)) {
1391            return true;
1392        }
1393
1394        if (mDataTypes == null && mDataSchemes == null) {
1395            Log.w("IntentFilter", "QUESTIONABLE INTENT FILTER:");
1396            dump(Log.WARN, "IntentFilter", "  ");
1397            return false;
1398        }
1399
1400        return true;
1401        */
1402    }
1403
1404    private IntentFilter(Parcel source) {
1405        mActions = new ArrayList<String>();
1406        source.readStringList(mActions);
1407        if (source.readInt() != 0) {
1408            mCategories = new ArrayList<String>();
1409            source.readStringList(mCategories);
1410        }
1411        if (source.readInt() != 0) {
1412            mDataSchemes = new ArrayList<String>();
1413            source.readStringList(mDataSchemes);
1414        }
1415        if (source.readInt() != 0) {
1416            mDataTypes = new ArrayList<String>();
1417            source.readStringList(mDataTypes);
1418        }
1419        int N = source.readInt();
1420        if (N > 0) {
1421            mDataAuthorities = new ArrayList<AuthorityEntry>();
1422            for (int i=0; i<N; i++) {
1423                mDataAuthorities.add(new AuthorityEntry(source));
1424            }
1425        }
1426        N = source.readInt();
1427        if (N > 0) {
1428            mDataPaths = new ArrayList<PatternMatcher>();
1429            for (int i=0; i<N; i++) {
1430                mDataPaths.add(new PatternMatcher(source));
1431            }
1432        }
1433        mPriority = source.readInt();
1434        mHasPartialTypes = source.readInt() > 0;
1435    }
1436
1437    private final boolean findMimeType(String type) {
1438        final ArrayList<String> t = mDataTypes;
1439
1440        if (type == null) {
1441            return false;
1442        }
1443
1444        if (t.contains(type)) {
1445            return true;
1446        }
1447
1448        // Deal with an Intent wanting to match every type in the IntentFilter.
1449        final int typeLength = type.length();
1450        if (typeLength == 3 && type.equals("*/*")) {
1451            return !t.isEmpty();
1452        }
1453
1454        // Deal with this IntentFilter wanting to match every Intent type.
1455        if (mHasPartialTypes && t.contains("*")) {
1456            return true;
1457        }
1458
1459        final int slashpos = type.indexOf('/');
1460        if (slashpos > 0) {
1461            if (mHasPartialTypes && t.contains(type.substring(0, slashpos))) {
1462                return true;
1463            }
1464            if (typeLength == slashpos+2 && type.charAt(slashpos+1) == '*') {
1465                // Need to look through all types for one that matches
1466                // our base...
1467                final int numTypes = t.size();
1468                for (int i = 0; i < numTypes; i++) {
1469                    final String v = t.get(i);
1470                    if (type.regionMatches(0, v, 0, slashpos+1)) {
1471                        return true;
1472                    }
1473                }
1474            }
1475        }
1476
1477        return false;
1478    }
1479}
1480