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