2525/**
2626Utility 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;
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