Search code examples
androidandroid-intentintentfilter

Strange Intent filter pathPattern behaviour


Documentation states that wildcards can be used in pathPattern.

A period followed by an asterisk (".*") matches any sequence of 0 to many characters.

So, I've created the following filter:

<intent-filter android:priority="600">
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <data android:scheme="http"/>
    <data android:scheme="https"/>
    <data android:host="*"/>
    <data android:pathPattern="/.*.exe"/>
</intent-filter>

But, it does not "work" for all links which ends with ".exe".

Works for these links:

https://subdomain.site.org/lite/appinst-lite-vc.exe

https://subdomain.site.org/appinst.exe

Does NOT work for this link:

https://subdomain.freedownloadmanager.org/5/5.1-latest/app_x86_setup.exe

It seems, it's not working for links with extra dots in their path part.

Am I missing something or is it Android bug (either in code, or in documentation)?

P.S. This filter catches ALL these links:

<intent-filter android:priority="600">
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <data android:scheme="http"/>
    <data android:scheme="https"/>
    <data android:host="*"/>
    <data android:pathPattern="/.*"/>
</intent-filter>

Solution

  • Yes, you are right. It's the . causing the problems.

    Unlike in regular expression, in path pattern the matching will stop at the first match of the first character of the pattern. In other words, the * matching is non greedy.

    One solution is adding multiple patters as below. The more you add, the more number or .s it can have without problems.

    <data android:scheme="http" android:host="*"
        android:pathPattern=".*\\.exe" />
    
    <data android:scheme="http" android:host="*"
        android:pathPattern=".*\\..*\\.exe" />
    
    <data android:scheme="http" android:host="*"
        android:pathPattern=".*\\..*\\..*\\..exe" />
    
    <data android:scheme="http" android:host="*"
        android:pathPattern=".*\\..*\\..*\\..*\\.exe" />
    

    Another solution is using this library.