Intent Filter Match Process source code analysis

Source: Internet
Author: User

Main process
  • Main Line process: Match action first, match data, last match category
  • Timing Diagram
  • Simplified code

     PublicFinal intMatch(StringActionString type,StringScheme, UriData,Set<String>CategoriesStringLogTag) {if(Action!= NULL && !Matchaction (action)) {returnNo_match_action; } int Datamatch=Matchdata (type, Scheme,Data);if(Datamatch< 0) {returnDatamatch; }StringCategorymismatch=Matchcategories (categories);if(Categorymismatch!= NULL) {returnNo_match_category; }returnDatamatch;}
Sub-process Match action
  • This sub-process is simple and only determines if the action is contained in the intent filter
  • Code

    /** * Match this filter  Against an Intent ' s action. If the filter does not * Specify any actions, the match would always fail. * * @param  action the desired action to the. * * @return  True If the action is a listed in the filter. */ public  final  boolean  matchaction  (String Action) {return  hasaction (action);} public  final  boolean  hasaction  (String Action) {return  action! = null  && Mactions.contains (action);}  
Match data
  • This sub-process is slightly more complex, the code is exported too much, combined with the syntax of the URI to see [scheme:]scheme-specific-part[#fragment]/[scheme:][//authority][path][?query][# Fragment
  • Code

    /** * Match This filter against an Intent ' s data ( type, scheme  , and path). If * The filter does not specify any types anddoes not specify any * schemes/paths, theMatchwould only succeedifThe intent does not * also specify a type or data. If  the Filter does  not Specify  any schemes,* It'll implicitlyMatchIntents withNo scheme,orThe schemes"Content:"*or "File:"(Basically performing a mime- type  only match). If  the Filter * Does not specify any MIME types, the Intent also must not specify a MIME * type.* * <p>be aware that to MatchAgainst an authority, you must also specify aBase* Scheme the Authority isinch. ToMatchAgainst a data path, both a scheme * andAuthority must be specified. If The filter does not specify any * typesorSchemes that it matches against, it's considered tobe empty * (any authorityorData path given is ignored, as ifIt were empty as* well). * * <p><em>note:mime type, Uri scheme,  and host name matching inch   the* Android Framework is case-sensitive, unlike the formal RFC definitions. * As a result, you should always write these elements withlower case Letters, * andNormalize any MIME typesorUris you receive from * Outside ofAndroid toEnsure these elements is lower case before * supplying them here.</em></p> * * @param type  the desired data type   to look for,  as returned   by* Intent.resolvetype (). * @param scheme the desired data scheme toLook for, asReturned by * Intent.getscheme (). * @param data the full data string to MatchAgainst, asSuppliedinch* Intent.data. * * @returnReturns either a validMatchConstant (a combination of* {@link #MATCH_CATEGORY_MASK} and{@link #MATCH_ADJUSTMENT_MASK}), *orOne ofThe error codes {@link #NO_MATCH_TYPE}ifThe type didn't match *or{@link #NO_MATCH_DATA}ifThe Scheme/path didn ' tMatch. * * @see #Match*/ PublicFinal int Matchdata (String type, String scheme, Uri data) {Final arraylist<string> types = mdatatypes;    Final arraylist<string> schemes = Mdataschemes; IntMatch= Match_category_empty;//1. If the filter's type scheme is empty and intent's type data is empty then match    //Otherwise does not match    if(Types = =NULL&& schemes = =NULL) {return(( Type ==NULL&& data = =NULL)            ?    (match_category_empty+match_adjustment_normal): No_match_data); }if(Schemes! =NULL) {//2. If the filter's schemes is not empty and the intent scheme is included in it, then the subsequent judgment is made;    //Otherwise does not match        if(schemes.contains (Scheme! =NULL? Scheme:"")) {Match= Match_category_scheme; }Else{returnNo_match_data; } final arraylist<patternmatcher> schemespecificparts = mdataschemespecificparts;if(Schemespecificparts! =NULL) {//3. If Schemespecificparts is not empty, use the schemespecificpart of data to make a match        //and modify the match value. URI syntax: [scheme:]scheme-specific-part[#fragment]/[scheme:][//authority][path][?query][#fragment]            Match= Hasdataschemespecificpart (Data.getschemespecificpart ())?        Match_category_scheme_specific_part:no_match_data; }if(Match! = Match_category_scheme_specific_part) {//If there isn ' t matching SSP, we need to match an authority.Final arraylist<authorityentry> authorities = Mdataauthorities;//4. If Schemespecificpart (SSP) does not match, match authority            //is to judge the host port part            if(Authorities! =NULL) {int authmatch = matchdataauthority (data);if(Authmatch >=0) {//5. The host port matches, then the path match is continuedFinal arraylist<patternmatcher> paths = mdatapaths;if(Paths = =NULL) {Match= Authmatch; }Else if(Hasdatapath (Data.getpath ())) {Match= Match_category_path; }Else{returnNo_match_data; }                }Else{//Host port does not match                    returnNo_match_data; }            }        }//If Neither an SSP or an authority matched, we ' re -done.        if(Match= = No_match_data) {returnNo_match_data; }    }Else{//Schemespecificparts of filter is not empty and matches intent Schemespecificpart    //If scheme is not "" "Content" "file" does not match;        //Special Case:match either a Intent with no data URI,        //or with a scheme:uri. Give a convenience for        //The common case where you want to deal with data in a        //content provider, which is do by type, and we don ' t want        //To force everyone to say they handle content:or file:uris.        if(Scheme! =NULL&&!"". Equals (Scheme) &&!"Content". Equals (Scheme) &&!"File". Equals (Scheme)) {returnNo_match_data; }    }if(Types! =NULL) {//6. Type is MIME    //If type is not empty, determine if the type of filter and intent are inclusive, otherwise it will not match        if(Findmimetype ( type)) {            Match= Match_category_type; }Else{returnNo_match_type; }    }Else{//If No MIME types is specified, then we'll only match against        //An Intent this does not has a MIME type.        if( type ! = NULL) {//If the types collection of filter is empty, but intent is not empty, does not match            returnNo_match_type; }    }return Match+ Match_adjustment_normal;}
Match category
  • This sub-process is also not complex, only to determine whether the intent category is completely contained in the filter
  • Code

    /** * Match This filter against an Intent ' s categories. Each category in * The Intent must is specified by the filter; If any is not in the * filter, the match fails. * * @param Categories The categories included in the intent, as returned by * Intent.getcategor IES (). * * @return If All categories Match (success), NULL; else the name of the * first category that didn ' t match. */ Public FinalStringmatchcategories(set<string> categories) {if(Categories = =NULL) {return NULL;//Match Success} iterator<string> it = Categories.iterator ();if(Mcategories = =NULL) {//filter No categories;intent, no failure, no success.        returnIt.hasnext ()? It.next ():NULL; } while(It.hasnext ()) {//filter has categories, intent's categories to be all included in the filter to be counted as successful .        FinalString category = It.next ();if(!mcategories.contains (category)) {returnCategory }    }return NULL;}
Summary
    • The matching process is generally divided into three steps, match action-> match data-> match categories, any one step failure is not carried out subsequent processing.
    • The match action is whether the action matching intent is included in the actions of the filter.
    • The idea of match data and the grammatical composition of the URI relate to [scheme:]scheme-specific-part[#fragment]
      • The types of filter and intent (that is, Mimi) schemes are all null, if they match, the value match_category_empty, otherwise continue
      • Match scheme, the mismatch is over, the match continues
      • The Schemespecificpart section, if matched, makes the final type (MIME) judgment
      • Schemespecificpart section, if it does not match, the
        • Match host port section, success continues, otherwise does not match
        • Match path section, success continues, otherwise does not match
      • Finally, a type (MIME) match is made
    • Match categories is to determine whether all the category intent are included in the filter categories.
    • The syntax of the URI consists of [scheme:]scheme-specific-part[#fragment]/[scheme:][//authority][path][?query][#fragment]
Reference
    • Http://www.cjsdn.net/doc/jdk50/java/net/URI.html

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Intent Filter Match process source code analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.