/* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.content; import android.net.Uri; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; /** Utility class to aid in matching URIs in content providers.
To use this class, build up a tree of UriMatcher objects. Typically, it looks something like this:
private static final int PEOPLE = 1; private static final int PEOPLE_ID = 2; private static final int PEOPLE_PHONES = 3; private static final int PEOPLE_PHONES_ID = 4; private static final int PEOPLE_CONTACTMETHODS = 7; private static final int PEOPLE_CONTACTMETHODS_ID = 8; private static final int DELETED_PEOPLE = 20; private static final int PHONES = 9; private static final int PHONES_ID = 10; private static final int PHONES_FILTER = 14; private static final int CONTACTMETHODS = 18; private static final int CONTACTMETHODS_ID = 19; private static final int CALLS = 11; private static final int CALLS_ID = 12; private static final int CALLS_FILTER = 15; private static final UriMatcher sURIMatcher = new UriMatcher(); static { sURIMatcher.addURI("contacts", "/people", PEOPLE); sURIMatcher.addURI("contacts", "/people/#", PEOPLE_ID); sURIMatcher.addURI("contacts", "/people/#/phones", PEOPLE_PHONES); sURIMatcher.addURI("contacts", "/people/#/phones/#", PEOPLE_PHONES_ID); sURIMatcher.addURI("contacts", "/people/#/contact_methods", PEOPLE_CONTACTMETHODS); sURIMatcher.addURI("contacts", "/people/#/contact_methods/#", PEOPLE_CONTACTMETHODS_ID); sURIMatcher.addURI("contacts", "/deleted_people", DELETED_PEOPLE); sURIMatcher.addURI("contacts", "/phones", PHONES); sURIMatcher.addURI("contacts", "/phones/filter/*", PHONES_FILTER); sURIMatcher.addURI("contacts", "/phones/#", PHONES_ID); sURIMatcher.addURI("contacts", "/contact_methods", CONTACTMETHODS); sURIMatcher.addURI("contacts", "/contact_methods/#", CONTACTMETHODS_ID); sURIMatcher.addURI("call_log", "/calls", CALLS); sURIMatcher.addURI("call_log", "/calls/filter/*", CALLS_FILTER); sURIMatcher.addURI("call_log", "/calls/#", CALLS_ID); }
Then when you need to match match against a URI, call {@link #match}, providing the tokenized url you've been given, and the value you want if there isn't a match. You can use the result to build a query, return a type, insert or delete a row, or whatever you need, without duplicating all of the if-else logic you'd otherwise need. Like this:
public String getType(String[] url) { int match = sURIMatcher.match(url, NO_MATCH); switch (match) { case PEOPLE: return "vnd.android.cursor.dir/person"; case PEOPLE_ID: return "vnd.android.cursor.item/person"; ... snip ... return "vnd.android.cursor.dir/snail-mail"; case PEOPLE_ADDRESS_ID: return "vnd.android.cursor.item/snail-mail"; default: return null; } }instead of
public String getType(String[] url) { if (url.length >= 2) { if (url[1].equals("people")) { if (url.length == 2) { return "vnd.android.cursor.dir/person"; } else if (url.length == 3) { return "vnd.android.cursor.item/person"; ... snip ... return "vnd.android.cursor.dir/snail-mail"; } else if (url.length == 3) { return "vnd.android.cursor.item/snail-mail"; } } } return null; }*/ public class UriMatcher { public static final int NO_MATCH = -1; /** * Creates the root node of the URI tree. * * @param code the code to match for the root URI */ public UriMatcher(int code) { mCode = code; mWhich = -1; mChildren = new ArrayList