OSDN Git Service

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