Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching a url pattern in <intent-filter>

I'd like one of my activities to pick up a particular url. The pattern is:

http://www.example.com/abc123/foo/xyz789

The path components "abc123" and "xyz789" can be any sequence of alpha-numerics, length > 1.

Doing this in my manifest:

<activity>
  <intent-filter>
    <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"
      android:host="example.com" 
      android:pathPattern="/.*/foo/.*" />

but it seems that any pattern from my domain is getting matched, ie:

myexample.com
myexample.com/whatever

both get matched. I guess maybe the .* operator is not working as I expect here? Any help would be great,

Thanks

http://developer.android.com/guide/topics/manifest/data-element.html

like image 572
user291701 Avatar asked Jan 21 '11 18:01

user291701


People also ask

What is intent filters explain intent filters with example?

An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent.

Can an activity have multiple intent filters?

However, since a component can have multiple intent filters, an intent that does not pass through one of a component's filters might make it through on another. An <intent-filter> element in the manifest file lists actions as <action> subelements. For example: <intent-filter . . . >

What is the purpose of category in an intent filter?

Categories are used for implicit Intents. So, If your Activity can be started by an implicit Intent when no other specific category is assigned to activity, activity's Intent filter should include this category. (even if you have other categories in the Intent filter).

What is intent filter priority?

An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component.


3 Answers

I had the same problem today, my solution was:

   <data android:pathPattern="/remotes/..*/..*"/>

using (..*) as a replacement for (.+)

In the original question's case I guess that this can work:

<data
                android:host="example.com"
                android:pathPattern="..*/foo/..*"
                android:scheme="http"/>
like image 200
CronosNull Avatar answered Nov 15 '22 19:11

CronosNull


Since android OS uses PatternMatcher and observing from source code and documentation it can only work with . * and \\(escape characters) we might need to be a little creative - i.e., use \\ to stop regex evaluation and then resume from then onwards.

<data
                android:host="example.com"
                android:pathPattern=".*\\/foo/.*"
                android:scheme="http"/>

which will only match myexample.com/whatever and not myexample.com

To create different intent filters with varying uri use / at the end of the uri like

Sample solution, use http://www.example.com/abc123/foo/xyz789/ instead of http://www.example.com/abc123/foo/xyz789 and then use

example.com/abc123/ android:pathPattern=".*\\/

example.com/abc123/foo/xyz789/ android:pathPattern=".*\\/foo/.*\\/

example.com/abc123/foo/xyz789/bar/uvx456/ android:pathPattern=".*\\/foo/.*\\/bar/.*\\/

like image 37
raj Avatar answered Nov 15 '22 20:11

raj


The problem is that .* matches everything, -and nothing-, so "/.*/" matches "//", which is equivalent to just "/" in a path.

Also, you can ensure that the two path segments "abc123" in your example are equal with the expression matcher supported here.

In other words, you want want to alter your paths to something simpler that can be matched. if it's your component that's receiving the intent, you can pass a fake URL that is just the variable parts of the real URL. for example, if you want to match,

http://www.example.com/.+/foo/.+

Where the two captured groups are equal, just pass http://mystuff/abc123 in the intent, and set the data pattern to http://mystuff/*

Then parse that when you receive the intent, and transform it into the real URL by filling in the known constant parts.

like image 33
Jeffrey Blattman Avatar answered Nov 15 '22 20:11

Jeffrey Blattman