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