Skip to content

Commit 0a675fd

Browse files
committed
Fixes Issue 7907 in the public bugs database (http://code.google.com/p/android/issues/detail?id=7907).
The Javadoc comment for class `android.content.UriMatcher` had four issues: 1. The example calls to `addURI` should not be using a leading forward slash in the path parameter (reported by Ester Ytterbrink). 2. The sample code to construct a `UriMatcher` was incorrect because the `UriMatcher` constructor takes a parameter (reported by Ross Light). 3. The code example for using `match` was incorrect because it showed two parameters being passed, when `match` only takes one (reported by Ross Light). 4. The sample `getType` implementations were incorrect because `getType` takes a `Uri` object, not an array of `String`s. Change-Id: I560bff6f021c13cabf736f40ff0f47a205074291
1 parent b4ae2f1 commit 0a675fd

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

core/java/android/content/UriMatcher.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
/**
2626
Utility class to aid in matching URIs in content providers.
2727
28-
<p>To use this class, build up a tree of UriMatcher objects.
29-
Typically, it looks something like this:
28+
<p>To use this class, build up a tree of <code>UriMatcher</code> objects.
29+
For example:
3030
<pre>
3131
private static final int PEOPLE = 1;
3232
private static final int PEOPLE_ID = 2;
@@ -48,36 +48,35 @@
4848
private static final int CALLS_ID = 12;
4949
private static final int CALLS_FILTER = 15;
5050
51-
private static final UriMatcher sURIMatcher = new UriMatcher();
51+
private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
5252
5353
static
5454
{
55-
sURIMatcher.addURI("contacts", "/people", PEOPLE);
56-
sURIMatcher.addURI("contacts", "/people/#", PEOPLE_ID);
57-
sURIMatcher.addURI("contacts", "/people/#/phones", PEOPLE_PHONES);
58-
sURIMatcher.addURI("contacts", "/people/#/phones/#", PEOPLE_PHONES_ID);
59-
sURIMatcher.addURI("contacts", "/people/#/contact_methods", PEOPLE_CONTACTMETHODS);
60-
sURIMatcher.addURI("contacts", "/people/#/contact_methods/#", PEOPLE_CONTACTMETHODS_ID);
61-
sURIMatcher.addURI("contacts", "/deleted_people", DELETED_PEOPLE);
62-
sURIMatcher.addURI("contacts", "/phones", PHONES);
63-
sURIMatcher.addURI("contacts", "/phones/filter/*", PHONES_FILTER);
64-
sURIMatcher.addURI("contacts", "/phones/#", PHONES_ID);
65-
sURIMatcher.addURI("contacts", "/contact_methods", CONTACTMETHODS);
66-
sURIMatcher.addURI("contacts", "/contact_methods/#", CONTACTMETHODS_ID);
67-
sURIMatcher.addURI("call_log", "/calls", CALLS);
68-
sURIMatcher.addURI("call_log", "/calls/filter/*", CALLS_FILTER);
69-
sURIMatcher.addURI("call_log", "/calls/#", CALLS_ID);
55+
sURIMatcher.addURI("contacts", "people", PEOPLE);
56+
sURIMatcher.addURI("contacts", "people/#", PEOPLE_ID);
57+
sURIMatcher.addURI("contacts", "people/#/phones", PEOPLE_PHONES);
58+
sURIMatcher.addURI("contacts", "people/#/phones/#", PEOPLE_PHONES_ID);
59+
sURIMatcher.addURI("contacts", "people/#/contact_methods", PEOPLE_CONTACTMETHODS);
60+
sURIMatcher.addURI("contacts", "people/#/contact_methods/#", PEOPLE_CONTACTMETHODS_ID);
61+
sURIMatcher.addURI("contacts", "deleted_people", DELETED_PEOPLE);
62+
sURIMatcher.addURI("contacts", "phones", PHONES);
63+
sURIMatcher.addURI("contacts", "phones/filter/*", PHONES_FILTER);
64+
sURIMatcher.addURI("contacts", "phones/#", PHONES_ID);
65+
sURIMatcher.addURI("contacts", "contact_methods", CONTACTMETHODS);
66+
sURIMatcher.addURI("contacts", "contact_methods/#", CONTACTMETHODS_ID);
67+
sURIMatcher.addURI("call_log", "calls", CALLS);
68+
sURIMatcher.addURI("call_log", "calls/filter/*", CALLS_FILTER);
69+
sURIMatcher.addURI("call_log", "calls/#", CALLS_ID);
7070
}
7171
</pre>
72-
<p>Then when you need to match match against a URI, call {@link #match}, providing
73-
the tokenized url you've been given, and the value you want if there isn't
74-
a match. You can use the result to build a query, return a type, insert or
75-
delete a row, or whatever you need, without duplicating all of the if-else
76-
logic you'd otherwise need. Like this:
72+
<p>Then when you need to match against a URI, call {@link #match}, providing
73+
the URL that you have been given. You can use the result to build a query,
74+
return a type, insert or delete a row, or whatever you need, without duplicating
75+
all of the if-else logic that you would otherwise need. For example:
7776
<pre>
78-
public String getType(String[] url)
77+
public String getType(Uri url)
7978
{
80-
int match = sURIMatcher.match(url, NO_MATCH);
79+
int match = sURIMatcher.match(url);
8180
switch (match)
8281
{
8382
case PEOPLE:
@@ -93,19 +92,20 @@ public String getType(String[] url)
9392
}
9493
}
9594
</pre>
96-
instead of
95+
instead of:
9796
<pre>
98-
public String getType(String[] url)
97+
public String getType(Uri url)
9998
{
100-
if (url.length >= 2) {
101-
if (url[1].equals("people")) {
102-
if (url.length == 2) {
99+
List<String> pathSegments = url.getPathSegments();
100+
if (pathSegments.size() >= 2) {
101+
if ("people".equals(pathSegments.get(1))) {
102+
if (pathSegments.size() == 2) {
103103
return "vnd.android.cursor.dir/person";
104-
} else if (url.length == 3) {
104+
} else if (pathSegments.size() == 3) {
105105
return "vnd.android.cursor.item/person";
106106
... snip ...
107107
return "vnd.android.cursor.dir/snail-mail";
108-
} else if (url.length == 3) {
108+
} else if (pathSegments.size() == 3) {
109109
return "vnd.android.cursor.item/snail-mail";
110110
}
111111
}

0 commit comments

Comments
 (0)