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