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