Intent-Filterでpathありとpathなしを一瞬で見分ける方法


AndroidManifest.xmlでschemeとhostを指定するとき、
myapp://myhost/mypathmyapp://myhost を見分けるのに地味に困りました。

この問題は正規表現で一発解決しました。

「myapp://myhost/mypath」

    <intent-filter>
         <action android:name="android.intent.action.VIEW" />
         <category android:name="android.intent.category.DEFAULT" />
         <data
             android:host="myhost"
             android:pathPattern="mypath"
             android:scheme="myapp" />
    </intent-filter>

「myapp://myhost」

    <intent-filter>
         <action android:name="android.intent.action.VIEW" />
         <category android:name="android.intent.category.DEFAULT" />
         <data
             android:host="myhost"
             android:scheme="myapp" />
    </intent-filter>

↑ これだとmyapp://myhost/mypathmyapp://myhost の両方に反応してしまう。

    <intent-filter>
         <action android:name="android.intent.action.VIEW" />
         <category android:name="android.intent.category.DEFAULT" />
         <data
             android:host="myhost"
             android:pathPattern="\s"
             android:scheme="myapp" />
    </intent-filter>

↑ そこで空文字を表す正規表現の\sを使うとmyapp://myhost にしか反応しなくなる。

これでRESTFULなスキーマにも対応できる。