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