下面列出了android.content.UriMatcher 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static UriMatcher buildUriMatcher() {
// I know what you're thinking. Why create a UriMatcher when you can use regular
// expressions instead? Because you're not crazy, that's why.
// All paths added to the UriMatcher have a corresponding code to return when a match is
// found. The code passed into the constructor represents the code to return for the root
// URI. It's common to use NO_MATCH as the code for this case.
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = WeatherContract.CONTENT_AUTHORITY;
// For each type of URI you want to add, create a corresponding code.
matcher.addURI(authority, WeatherContract.PATH_WEATHER, WEATHER);
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/*", WEATHER_WITH_LOCATION);
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/*/#", WEATHER_WITH_LOCATION_AND_DATE);
matcher.addURI(authority, WeatherContract.PATH_LOCATION, LOCATION);
return matcher;
}
static UriMatcher buildUriMatcher() {
// I know what you're thinking. Why create a UriMatcher when you can use regular
// expressions instead? Because you're not crazy, that's why.
// All paths added to the UriMatcher have a corresponding code to return when a match is
// found. The code passed into the constructor represents the code to return for the root
// URI. It's common to use NO_MATCH as the code for this case.
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = WeatherContract.CONTENT_AUTHORITY;
// For each type of URI you want to add, create a corresponding code.
matcher.addURI(authority, WeatherContract.PATH_WEATHER, WEATHER);
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/*", WEATHER_WITH_LOCATION);
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/*/#", WEATHER_WITH_LOCATION_AND_DATE);
matcher.addURI(authority, WeatherContract.PATH_LOCATION, LOCATION);
return matcher;
}
/**
* This function tests that the UriMatcher returns the correct integer value for
* each of the Uri types that the ContentProvider can handle. Uncomment this when you are
* ready to test your UriMatcher.
*/
@Test
public void testUriMatcher() {
/* Create a URI matcher that the TaskContentProvider uses */
UriMatcher testMatcher = TaskContentProvider.buildUriMatcher();
/* Test that the code returned from our matcher matches the expected TASKS int */
String tasksUriDoesNotMatch = "Error: The TASKS URI was matched incorrectly.";
int actualTasksMatchCode = testMatcher.match(TEST_TASKS);
int expectedTasksMatchCode = TaskContentProvider.TASKS;
assertEquals(tasksUriDoesNotMatch,
actualTasksMatchCode,
expectedTasksMatchCode);
/* Test that the code returned from our matcher matches the expected TASK_WITH_ID */
String taskWithIdDoesNotMatch =
"Error: The TASK_WITH_ID URI was matched incorrectly.";
int actualTaskWithIdCode = testMatcher.match(TEST_TASK_WITH_ID);
int expectedTaskWithIdCode = TaskContentProvider.TASK_WITH_ID;
assertEquals(taskWithIdDoesNotMatch,
actualTaskWithIdCode,
expectedTaskWithIdCode);
}
/**
Initialize a new matcher object without any matches,
then use .addURI(String authority, String path, int match) to add matches
*/
public static UriMatcher buildUriMatcher() {
// Initialize a UriMatcher with no matches by passing in NO_MATCH to the constructor
UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
/*
All paths added to the UriMatcher have a corresponding int.
For each kind of uri you may want to access, add the corresponding match with addURI.
The two calls below add matches for the task directory and a single item by ID.
*/
uriMatcher.addURI(TaskContract.AUTHORITY, TaskContract.PATH_TASKS, TASKS);
uriMatcher.addURI(TaskContract.AUTHORITY, TaskContract.PATH_TASKS + "/#", TASK_WITH_ID);
return uriMatcher;
}
/**
* This function tests that the UriMatcher returns the correct integer value for
* each of the Uri types that the ContentProvider can handle. Uncomment this when you are
* ready to test your UriMatcher.
*/
@Test
public void testUriMatcher() {
/* Create a URI matcher that the TaskContentProvider uses */
UriMatcher testMatcher = TaskContentProvider.buildUriMatcher();
/* Test that the code returned from our matcher matches the expected TASKS int */
String tasksUriDoesNotMatch = "Error: The TASKS URI was matched incorrectly.";
int actualTasksMatchCode = testMatcher.match(TEST_TASKS);
int expectedTasksMatchCode = TaskContentProvider.TASKS;
assertEquals(tasksUriDoesNotMatch,
actualTasksMatchCode,
expectedTasksMatchCode);
/* Test that the code returned from our matcher matches the expected TASK_WITH_ID */
String taskWithIdDoesNotMatch =
"Error: The TASK_WITH_ID URI was matched incorrectly.";
int actualTaskWithIdCode = testMatcher.match(TEST_TASK_WITH_ID);
int expectedTaskWithIdCode = TaskContentProvider.TASK_WITH_ID;
assertEquals(taskWithIdDoesNotMatch,
actualTaskWithIdCode,
expectedTaskWithIdCode);
}
@Override
public boolean onCreate() {
try {
Context appContext = getContext().getApplicationContext();
ApplicationInfo appInfo = appContext.getApplicationInfo();
String packageName = appInfo.packageName;
AUTHORITY = packageName + ".provider.availabledata";//NON-NLS
BASE_CONTENT_URI = Uri.parse("content://" + AUTHORITY);//NON-NLS
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, SPATIALITE, SPATIALITE_ID);
uriMatcher.addURI(AUTHORITY, BASEMAPS, BASEMAPS_ID);
uriMatcher.addURI(AUTHORITY, TAGS, TAGS_ID);
} catch (Exception e) {
GPLog.error(this, null, e);
return false;
}
return true;
}
/**
* This function tests that the UriMatcher returns the correct integer value for
* each of the Uri types that the ContentProvider can handle. Uncomment this when you are
* ready to test your UriMatcher.
*/
@Test
public void testUriMatcher() {
/* Create a URI matcher that the TaskContentProvider uses */
UriMatcher testMatcher = TaskContentProvider.buildUriMatcher();
/* Test that the code returned from our matcher matches the expected TASKS int */
String tasksUriDoesNotMatch = "Error: The TASKS URI was matched incorrectly.";
int actualTasksMatchCode = testMatcher.match(TEST_TASKS);
int expectedTasksMatchCode = TaskContentProvider.TASKS;
assertEquals(tasksUriDoesNotMatch,
actualTasksMatchCode,
expectedTasksMatchCode);
/* Test that the code returned from our matcher matches the expected TASK_WITH_ID */
String taskWithIdDoesNotMatch =
"Error: The TASK_WITH_ID URI was matched incorrectly.";
int actualTaskWithIdCode = testMatcher.match(TEST_TASK_WITH_ID);
int expectedTaskWithIdCode = TaskContentProvider.TASK_WITH_ID;
assertEquals(taskWithIdDoesNotMatch,
actualTaskWithIdCode,
expectedTaskWithIdCode);
}
@Override
public boolean onCreate() {
if (checkInitAuthority(getContext())) {
mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
mUriMatcher.addURI(AUTHORITY, PATH_WILDCARD + PATH_GET_ALL, GET_ALL);
mUriMatcher.addURI(AUTHORITY, PATH_WILDCARD + PATH_GET_STRING, GET_STRING);
mUriMatcher.addURI(AUTHORITY, PATH_WILDCARD + PATH_GET_INT, GET_INT);
mUriMatcher.addURI(AUTHORITY, PATH_WILDCARD + PATH_GET_LONG, GET_LONG);
mUriMatcher.addURI(AUTHORITY, PATH_WILDCARD + PATH_GET_FLOAT, GET_FLOAT);
mUriMatcher.addURI(AUTHORITY, PATH_WILDCARD + PATH_GET_BOOLEAN, GET_BOOLEAN);
mUriMatcher.addURI(AUTHORITY, PATH_WILDCARD + PATH_CONTAINS, CONTAINS);
mUriMatcher.addURI(AUTHORITY, PATH_WILDCARD + PATH_APPLY, APPLY);
mUriMatcher.addURI(AUTHORITY, PATH_WILDCARD + PATH_COMMIT, COMMIT);
mUriMatcher.addURI(AUTHORITY, PATH_WILDCARD + PATH_REGISTER_ON_SHARED_PREFERENCE_CHANGE_LISTENER, REGISTER_ON_SHARED_PREFERENCE_CHANGE_LISTENER);
mUriMatcher.addURI(AUTHORITY, PATH_WILDCARD + PATH_UNREGISTER_ON_SHARED_PREFERENCE_CHANGE_LISTENER, UNREGISTER_ON_SHARED_PREFERENCE_CHANGE_LISTENER);
mUriMatcher.addURI(AUTHORITY, PATH_WILDCARD + PATH_GET_STRING_SET, GET_STRING_SET);
return true;
} else {
return false;
}
}
static UriMatcher buildUriMatcher() {
final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = MoviesContract.CONTENT_AUTHORITY;
uriMatcher.addURI(authority, MoviesContract.PATH_MOVIES, MOVIES);
uriMatcher.addURI(authority, MoviesContract.PATH_MOVIES + "/#", MOVIE_BY_ID);
uriMatcher.addURI(authority, MoviesContract.PATH_MOVIES + "/" +
MoviesContract.PATH_MOST_POPULAR, MOST_POPULAR_MOVIES);
uriMatcher.addURI(authority, MoviesContract.PATH_MOVIES + "/" +
MoviesContract.PATH_HIGHEST_RATED, HIGHEST_RATED_MOVIES);
uriMatcher.addURI(authority, MoviesContract.PATH_MOVIES + "/" +
MoviesContract.PATH_MOST_RATED, MOST_RATED_MOVIES);
uriMatcher.addURI(authority, MoviesContract.PATH_MOVIES + "/" +
MoviesContract.PATH_FAVORITES, FAVORITES);
return uriMatcher;
}
@Test
public void testUriMatcher() {
UriMatcher uriMatcher = MoviesProvider.buildUriMatcher();
assertEquals("Error: The BASE CONTENT URI was matched incorrectly.",
UriMatcher.NO_MATCH, uriMatcher.match(TEST_BASE_URI));
assertEquals("Error: The MOVIES URI was matched incorrectly.",
MoviesProvider.MOVIES, uriMatcher.match(TEST_MOVIES_URI));
assertEquals("Error: The MOVIE BY ID URI was matched incorrectly.",
MoviesProvider.MOVIE_BY_ID, uriMatcher.match(TEST_MOVIE_BY_ID_URI));
assertEquals("Error: The MOST POPULAR MOVIES URI was matched incorrectly.",
MoviesProvider.MOST_POPULAR_MOVIES, uriMatcher.match(TEST_MOST_POPULAR_MOVIES_URI));
assertEquals("Error: The HIGHEST RATED MOVIES URI was matched incorrectly.",
MoviesProvider.HIGHEST_RATED_MOVIES, uriMatcher.match(TEST_HIGHEST_RATED_MOVIES_URI));
assertEquals("Error: The MOST RATED MOVIES URI was matched incorrectly.",
MoviesProvider.MOST_RATED_MOVIES, uriMatcher.match(TEST_MOST_RATED_MOVIES_URI));
assertEquals("Error: The FAVORITES URI was matched incorrectly.",
MoviesProvider.FAVORITES, uriMatcher.match(TEST_FAVORITES_URI));
}
@Override
protected UriMatcher createUriMatcher() {
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = RecipesDBContract.CONTENT_AUTHORITY;
matcher.addURI(authority, "recipes", RECIPES);
matcher.addURI(authority, "recipes/#", RECIPES_ID);
matcher.addURI(authority, "ingredients", INGREDIENTS);
matcher.addURI(authority, "ingredients/#", INGREDIENTS_ID);
matcher.addURI(authority, "categories", CATEGORIES);
matcher.addURI(authority, "categories/#", CATEGORIES_ID);
matcher.addURI(authority, "search", SEARCH);
matcher.addURI(authority, "search/#", SEARCH_ID);
matcher.addURI(authority, "search_with_recipe", SEARCH_WITH_RECIPE);
matcher.addURI(authority, "search_with_recipe/#", SEARCH_WITH_RECIPE_ID);
// User Actions
return matcher;
}
protected void onCreate() {
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
mTypes.clear();
final int count = getAdapterCount();
if (count <= 0)
return;
for (int i = 0; i < count; i++) {
final Adapter adapter = getAdapter(i);
if (adapter == null)
continue;
final String type = adapter.getType();
final String subType = adapter.getSubType();
mTypes.put(i, type);
mMatcher.addURI(getAuthority(), getPath() + "/" + subType, i);
}
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
setModel(uri);
setMatcher(uri);
int match = matcher.match(uri);
switch (match) {
case COLLECTION:
SQLiteDatabase db = mModel.getWritableDatabase();
count = db.delete(mModel.getTableName(), selection, selectionArgs);
break;
case SINGLE_ROW:
db = mModel.getWritableDatabase();
String row_id = uri.getLastPathSegment();
count = db.delete(mModel.getTableName(), OColumn.ROW_ID + " = ?", new String[]{row_id});
break;
case UriMatcher.NO_MATCH:
break;
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
notifyDataChange(uri);
return count;
}
/**
* Helper method to match each URI to the ACRONYM integers
* constant defined above.
*
* @return UriMatcher
*/
private static UriMatcher buildUriMatcher() {
// All paths added to the UriMatcher have a corresponding code
// to return when a match is found. The code passed into the
// constructor represents the code to return for the rootURI.
// It's common to use NO_MATCH as the code for this case.
final UriMatcher matcher =
new UriMatcher(UriMatcher.NO_MATCH);
// For each type of URI that is added, a corresponding code is
// created.
matcher.addURI(AcronymContract.CONTENT_AUTHORITY,
AcronymContract.PATH_ACRONYM,
ACRONYMS);
matcher.addURI(AcronymContract.CONTENT_AUTHORITY,
AcronymContract.PATH_ACRONYM
+ "/#",
ACRONYM);
return matcher;
}
private void registerAuthority(String authority) {
mAuthority = authority;
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
mMatcher.addURI(mAuthority, "root", MATCH_ROOTS);
mMatcher.addURI(mAuthority, "root/*", MATCH_ROOT);
mMatcher.addURI(mAuthority, "root/*/recent", MATCH_RECENT);
mMatcher.addURI(mAuthority, "root/*/search", MATCH_SEARCH);
mMatcher.addURI(mAuthority, "document/*", MATCH_DOCUMENT);
mMatcher.addURI(mAuthority, "document/*/children", MATCH_CHILDREN);
mMatcher.addURI(mAuthority, "tree/*/document/*", MATCH_DOCUMENT_TREE);
mMatcher.addURI(mAuthority, "tree/*/document/*/children", MATCH_CHILDREN_TREE);
}
/**
* Implementation is provided by the parent class.
*/
@Override
public void attachInfo(Context context, ProviderInfo info) {
mAuthority = info.authority;
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
mMatcher.addURI(mAuthority, SearchIndexablesContract.INDEXABLES_XML_RES_PATH,
MATCH_RES_CODE);
mMatcher.addURI(mAuthority, SearchIndexablesContract.INDEXABLES_RAW_PATH,
MATCH_RAW_CODE);
mMatcher.addURI(mAuthority, SearchIndexablesContract.NON_INDEXABLES_KEYS_PATH,
MATCH_NON_INDEXABLE_KEYS_CODE);
mMatcher.addURI(mAuthority, SearchIndexablesContract.SITE_MAP_PAIRS_PATH,
MATCH_SITE_MAP_PAIRS_CODE);
// Sanity check our setup
if (!info.exported) {
throw new SecurityException("Provider must be exported");
}
if (!info.grantUriPermissions) {
throw new SecurityException("Provider must grantUriPermissions");
}
if (!android.Manifest.permission.READ_SEARCH_INDEXABLES.equals(info.readPermission)) {
throw new SecurityException("Provider must be protected by READ_SEARCH_INDEXABLES");
}
super.attachInfo(context, info);
}
private boolean initialize() {
mDatabaseHelper = getDatabaseHelper(getContext());
mLocalDatabaseHelper.set(mDatabaseHelper);
// Set up the DB helper for keeping transactions serialized.
setDbHelperToSerializeOn(mDatabaseHelper, DB_TAG);
// Create the URI matcher based on user's authority
final String authority = getAuthorityUri().getAuthority();
final UriMatcher matcher = sUriMatcher;
// Returns all sessions
matcher.addURI(authority, "session", SESSION);
// Returns session with given id
matcher.addURI(authority, "session/#", SESSION_ID);
// Returns log entries from session with given id
matcher.addURI(authority, "session/#/log", SESSION_ID_LOG);
// Returns log entries from session with given id as a single String
matcher.addURI(authority, "session/#/log/content", SESSION_ID_LOG_CONTENT);
// Returns all sessions with given key
matcher.addURI(authority, "session/key/*", SESSION_KEY);
return true;
}
private UriMatcher buildUriMatcher() {
UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(
AUTHORITY, "/search/" + SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST);
uriMatcher.addURI(
AUTHORITY,
"/search/" + SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
SEARCH_SUGGEST);
return uriMatcher;
}
/**
* Sets up a uri matcher for search suggestion and shortcut refresh queries.
*/
private static UriMatcher buildUriMatcher() {
UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST);
matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST);
matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT, SHORTCUT_REFRESH);
matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT + "/*", SHORTCUT_REFRESH);
return matcher;
}
static UriMatcher buildUriMatcher() {
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = VideoContract.CONTENT_AUTHORITY;
// For each type of URI to add, create a corresponding code.
matcher.addURI(authority, VideoContract.PATH_VIDEO, VIDEO);
matcher.addURI(authority, VideoContract.PATH_VIDEO + "/*", VIDEO_WITH_CATEGORY);
// Search related URIs.
matcher.addURI(authority, "search/" + SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST);
matcher.addURI(authority, "search/" + SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST);
return matcher;
}
/**
* Creates the UriMatcher that will match each URI to the CODE_WEATHER and
* CODE_WEATHER_WITH_DATE constants defined above.
* <p>
* It's possible you might be thinking, "Why create a UriMatcher when you can use regular
* expressions instead? After all, we really just need to match some patterns, and we can
* use regular expressions to do that right?" Because you're not crazy, that's why.
* <p>
* UriMatcher does all the hard work for you. You just have to tell it which code to match
* with which URI, and it does the rest automagically. Remember, the best programmers try
* to never reinvent the wheel. If there is a solution for a problem that exists and has
* been tested and proven, you should almost always use it unless there is a compelling
* reason not to.
*
* @return A UriMatcher that correctly matches the constants for CODE_WEATHER and CODE_WEATHER_WITH_DATE
*/
public static UriMatcher buildUriMatcher() {
/*
* All paths added to the UriMatcher have a corresponding code to return when a match is
* found. The code passed into the constructor of UriMatcher here represents the code to
* return for the root URI. It's common to use NO_MATCH as the code for this case.
*/
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = WeatherContract.CONTENT_AUTHORITY;
/*
* For each type of URI you want to add, create a corresponding code. Preferably, these are
* constant fields in your class so that you can use them throughout the class and you no
* they aren't going to change. In Sunshine, we use CODE_WEATHER or CODE_WEATHER_WITH_DATE.
*/
/* This URI is content://com.example.android.sunshine/weather/ */
matcher.addURI(authority, WeatherContract.PATH_WEATHER, CODE_WEATHER);
/*
* This URI would look something like content://com.example.android.sunshine/weather/1472214172
* The "/#" signifies to the UriMatcher that if PATH_WEATHER is followed by ANY number,
* that it should return the CODE_WEATHER_WITH_DATE code
*/
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/#", CODE_WEATHER_WITH_DATE);
return matcher;
}
/**
* Creates the UriMatcher that will match each URI to the CODE_WEATHER and
* CODE_WEATHER_WITH_DATE constants defined above.
* <p>
* It's possible you might be thinking, "Why create a UriMatcher when you can use regular
* expressions instead? After all, we really just need to match some patterns, and we can
* use regular expressions to do that right?" Because you're not crazy, that's why.
* <p>
* UriMatcher does all the hard work for you. You just have to tell it which code to match
* with which URI, and it does the rest automagically. Remember, the best programmers try
* to never reinvent the wheel. If there is a solution for a problem that exists and has
* been tested and proven, you should almost always use it unless there is a compelling
* reason not to.
*
* @return A UriMatcher that correctly matches the constants for CODE_WEATHER and CODE_WEATHER_WITH_DATE
*/
public static UriMatcher buildUriMatcher() {
/*
* All paths added to the UriMatcher have a corresponding code to return when a match is
* found. The code passed into the constructor of UriMatcher here represents the code to
* return for the root URI. It's common to use NO_MATCH as the code for this case.
*/
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = WeatherContract.CONTENT_AUTHORITY;
/*
* For each type of URI you want to add, create a corresponding code. Preferably, these are
* constant fields in your class so that you can use them throughout the class and you no
* they aren't going to change. In Sunshine, we use CODE_WEATHER or CODE_WEATHER_WITH_DATE.
*/
/* This URI is content://com.example.android.sunshine/weather/ */
matcher.addURI(authority, WeatherContract.PATH_WEATHER, CODE_WEATHER);
/*
* This URI would look something like content://com.example.android.sunshine/weather/1472214172
* The "/#" signifies to the UriMatcher that if PATH_WEATHER is followed by ANY number,
* that it should return the CODE_WEATHER_WITH_DATE code
*/
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/#", CODE_WEATHER_WITH_DATE);
return matcher;
}
/**
* Creates the UriMatcher that will match each URI to the CODE_WEATHER and
* CODE_WEATHER_WITH_DATE constants defined above.
* <p>
* It's possible you might be thinking, "Why create a UriMatcher when you can use regular
* expressions instead? After all, we really just need to match some patterns, and we can
* use regular expressions to do that right?" Because you're not crazy, that's why.
* <p>
* UriMatcher does all the hard work for you. You just have to tell it which code to match
* with which URI, and it does the rest automagically. Remember, the best programmers try
* to never reinvent the wheel. If there is a solution for a problem that exists and has
* been tested and proven, you should almost always use it unless there is a compelling
* reason not to.
*
* @return A UriMatcher that correctly matches the constants for CODE_WEATHER and CODE_WEATHER_WITH_DATE
*/
public static UriMatcher buildUriMatcher() {
/*
* All paths added to the UriMatcher have a corresponding code to return when a match is
* found. The code passed into the constructor of UriMatcher here represents the code to
* return for the root URI. It's common to use NO_MATCH as the code for this case.
*/
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = WeatherContract.CONTENT_AUTHORITY;
/*
* For each type of URI you want to add, create a corresponding code. Preferably, these are
* constant fields in your class so that you can use them throughout the class and you no
* they aren't going to change. In Sunshine, we use CODE_WEATHER or CODE_WEATHER_WITH_DATE.
*/
/* This URI is content://com.example.android.sunshine/weather/ */
matcher.addURI(authority, WeatherContract.PATH_WEATHER, CODE_WEATHER);
/*
* This URI would look something like content://com.example.android.sunshine/weather/1472214172
* The "/#" signifies to the UriMatcher that if PATH_WEATHER is followed by ANY number,
* that it should return the CODE_WEATHER_WITH_DATE code
*/
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/#", CODE_WEATHER_WITH_DATE);
return matcher;
}
/**
* Creates the UriMatcher that will match each URI to the CODE_WEATHER and
* CODE_WEATHER_WITH_DATE constants defined above.
* <p>
* It's possible you might be thinking, "Why create a UriMatcher when you can use regular
* expressions instead? After all, we really just need to match some patterns, and we can
* use regular expressions to do that right?" Because you're not crazy, that's why.
* <p>
* UriMatcher does all the hard work for you. You just have to tell it which code to match
* with which URI, and it does the rest automagically. Remember, the best programmers try
* to never reinvent the wheel. If there is a solution for a problem that exists and has
* been tested and proven, you should almost always use it unless there is a compelling
* reason not to.
*
* @return A UriMatcher that correctly matches the constants for CODE_WEATHER and CODE_WEATHER_WITH_DATE
*/
public static UriMatcher buildUriMatcher() {
/*
* All paths added to the UriMatcher have a corresponding code to return when a match is
* found. The code passed into the constructor of UriMatcher here represents the code to
* return for the root URI. It's common to use NO_MATCH as the code for this case.
*/
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = WeatherContract.CONTENT_AUTHORITY;
/*
* For each type of URI you want to add, create a corresponding code. Preferably, these are
* constant fields in your class so that you can use them throughout the class and you no
* they aren't going to change. In Sunshine, we use CODE_WEATHER or CODE_WEATHER_WITH_DATE.
*/
/* This URI is content://com.example.android.sunshine/weather/ */
matcher.addURI(authority, WeatherContract.PATH_WEATHER, CODE_WEATHER);
/*
* This URI would look something like content://com.example.android.sunshine/weather/1472214172
* The "/#" signifies to the UriMatcher that if PATH_WEATHER is followed by ANY number,
* that it should return the CODE_WEATHER_WITH_DATE code
*/
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/#", CODE_WEATHER_WITH_DATE);
return matcher;
}
/**
* Creates the UriMatcher that will match each URI to the CODE_WEATHER and
* CODE_WEATHER_WITH_DATE constants defined above.
* <p>
* It's possible you might be thinking, "Why create a UriMatcher when you can use regular
* expressions instead? After all, we really just need to match some patterns, and we can
* use regular expressions to do that right?" Because you're not crazy, that's why.
* <p>
* UriMatcher does all the hard work for you. You just have to tell it which code to match
* with which URI, and it does the rest automagically. Remember, the best programmers try
* to never reinvent the wheel. If there is a solution for a problem that exists and has
* been tested and proven, you should almost always use it unless there is a compelling
* reason not to.
*
* @return A UriMatcher that correctly matches the constants for CODE_WEATHER and CODE_WEATHER_WITH_DATE
*/
public static UriMatcher buildUriMatcher() {
/*
* All paths added to the UriMatcher have a corresponding code to return when a match is
* found. The code passed into the constructor of UriMatcher here represents the code to
* return for the root URI. It's common to use NO_MATCH as the code for this case.
*/
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = WeatherContract.CONTENT_AUTHORITY;
/*
* For each type of URI you want to add, create a corresponding code. Preferably, these are
* constant fields in your class so that you can use them throughout the class and you no
* they aren't going to change. In Sunshine, we use CODE_WEATHER or CODE_WEATHER_WITH_DATE.
*/
/* This URI is content://com.example.android.sunshine/weather/ */
matcher.addURI(authority, WeatherContract.PATH_WEATHER, CODE_WEATHER);
/*
* This URI would look something like content://com.example.android.sunshine/weather/1472214172
* The "/#" signifies to the UriMatcher that if PATH_WEATHER is followed by ANY number,
* that it should return the CODE_WEATHER_WITH_DATE code
*/
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/#", CODE_WEATHER_WITH_DATE);
return matcher;
}
/**
* Creates the UriMatcher that will match each URI to the CODE_WEATHER and
* CODE_WEATHER_WITH_DATE constants defined above.
* <p>
* It's possible you might be thinking, "Why create a UriMatcher when you can use regular
* expressions instead? After all, we really just need to match some patterns, and we can
* use regular expressions to do that right?" Because you're not crazy, that's why.
* <p>
* UriMatcher does all the hard work for you. You just have to tell it which code to match
* with which URI, and it does the rest automagically. Remember, the best programmers try
* to never reinvent the wheel. If there is a solution for a problem that exists and has
* been tested and proven, you should almost always use it unless there is a compelling
* reason not to.
*
* @return A UriMatcher that correctly matches the constants for CODE_WEATHER and CODE_WEATHER_WITH_DATE
*/
public static UriMatcher buildUriMatcher() {
/*
* All paths added to the UriMatcher have a corresponding code to return when a match is
* found. The code passed into the constructor of UriMatcher here represents the code to
* return for the root URI. It's common to use NO_MATCH as the code for this case.
*/
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = WeatherContract.CONTENT_AUTHORITY;
/*
* For each type of URI you want to add, create a corresponding code. Preferably, these are
* constant fields in your class so that you can use them throughout the class and you no
* they aren't going to change. In Sunshine, we use CODE_WEATHER or CODE_WEATHER_WITH_DATE.
*/
/* This URI is content://com.example.android.sunshine/weather/ */
matcher.addURI(authority, WeatherContract.PATH_WEATHER, CODE_WEATHER);
/*
* This URI would look something like content://com.example.android.sunshine/weather/1472214172
* The "/#" signifies to the UriMatcher that if PATH_WEATHER is followed by ANY number,
* that it should return the CODE_WEATHER_WITH_DATE code
*/
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/#", CODE_WEATHER_WITH_DATE);
return matcher;
}
private Class<? extends Model> getModelType(Uri uri) {
final int code = URI_MATCHER.match(uri);
if (code != UriMatcher.NO_MATCH) {
return TYPE_CODES.get(code);
}
return null;
}
/**
* Creates the UriMatcher that will match each URI to the CODE_WEATHER and
* CODE_WEATHER_WITH_DATE constants defined above.
* <p>
* It's possible you might be thinking, "Why create a UriMatcher when you can use regular
* expressions instead? After all, we really just need to match some patterns, and we can
* use regular expressions to do that right?" Because you're not crazy, that's why.
* <p>
* UriMatcher does all the hard work for you. You just have to tell it which code to match
* with which URI, and it does the rest automagically. Remember, the best programmers try
* to never reinvent the wheel. If there is a solution for a problem that exists and has
* been tested and proven, you should almost always use it unless there is a compelling
* reason not to.
*
* @return A UriMatcher that correctly matches the constants for CODE_WEATHER and CODE_WEATHER_WITH_DATE
*/
public static UriMatcher buildUriMatcher() {
/*
* All paths added to the UriMatcher have a corresponding code to return when a match is
* found. The code passed into the constructor of UriMatcher here represents the code to
* return for the root URI. It's common to use NO_MATCH as the code for this case.
*/
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = WeatherContract.CONTENT_AUTHORITY;
/*
* For each type of URI you want to add, create a corresponding code. Preferably, these are
* constant fields in your class so that you can use them throughout the class and you no
* they aren't going to change. In Sunshine, we use CODE_WEATHER or CODE_WEATHER_WITH_DATE.
*/
/* This URI is content://com.example.android.sunshine/weather/ */
matcher.addURI(authority, WeatherContract.PATH_WEATHER, CODE_WEATHER);
/*
* This URI would look something like content://com.example.android.sunshine/weather/1472214172
* The "/#" signifies to the UriMatcher that if PATH_WEATHER is followed by ANY number,
* that it should return the CODE_WEATHER_WITH_DATE code
*/
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/#", CODE_WEATHER_WITH_DATE);
return matcher;
}
@Override
public boolean onCreate() {
openHelper = new ProviGenOpenHelper(getContext(), this, databaseName, contracts.getVersionSum());
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
for (ContractHolder contract : contracts) {
uriMatcher.addURI(contract.getAuthority(), contract.getTable(), ITEM);
uriMatcher.addURI(contract.getAuthority(), contract.getTable() + "/#", ITEM_ID);
}
return true;
}
/**
* Creates the UriMatcher that will match each URI to the CODE_WEATHER and
* CODE_WEATHER_WITH_DATE constants defined above.
* <p>
* It's possible you might be thinking, "Why create a UriMatcher when you can use regular
* expressions instead? After all, we really just need to match some patterns, and we can
* use regular expressions to do that right?" Because you're not crazy, that's why.
* <p>
* UriMatcher does all the hard work for you. You just have to tell it which code to match
* with which URI, and it does the rest automagically. Remember, the best programmers try
* to never reinvent the wheel. If there is a solution for a problem that exists and has
* been tested and proven, you should almost always use it unless there is a compelling
* reason not to.
*
* @return A UriMatcher that correctly matches the constants for CODE_WEATHER and CODE_WEATHER_WITH_DATE
*/
public static UriMatcher buildUriMatcher() {
/*
* All paths added to the UriMatcher have a corresponding code to return when a match is
* found. The code passed into the constructor of UriMatcher here represents the code to
* return for the root URI. It's common to use NO_MATCH as the code for this case.
*/
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = WeatherContract.CONTENT_AUTHORITY;
/*
* For each type of URI you want to add, create a corresponding code. Preferably, these are
* constant fields in your class so that you can use them throughout the class and you no
* they aren't going to change. In Sunshine, we use CODE_WEATHER or CODE_WEATHER_WITH_DATE.
*/
/* This URI is content://com.example.android.sunshine/weather/ */
matcher.addURI(authority, WeatherContract.PATH_WEATHER, CODE_WEATHER);
/*
* This URI would look something like content://com.example.android.sunshine/weather/1472214172
* The "/#" signifies to the UriMatcher that if PATH_WEATHER is followed by ANY number,
* that it should return the CODE_WEATHER_WITH_DATE code
*/
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/#", CODE_WEATHER_WITH_DATE);
return matcher;
}