private InputStream loadResourceFromUri(Uri uri, ContentResolver contentResolver) throws FileNotFoundException { switch (URI_MATCHER.match(uri)) { case ID_CONTACTS_CONTACT: return openContactPhotoInputStream(contentResolver, uri); case ID_CONTACTS_LOOKUP: // If it was a Lookup uri then resolve it first, then continue loading the contact uri. uri = ContactsContract.Contacts.lookupContact(contentResolver, uri); if (uri == null) { throw new FileNotFoundException("Contact cannot be found"); } return openContactPhotoInputStream(contentResolver, uri); case ID_CONTACTS_THUMBNAIL: case ID_CONTACTS_PHOTO: case UriMatcher.NO_MATCH: default: return contentResolver.openInputStream(uri); } }
private InputStream loadResourceFromUri(Uri uri, ContentResolver contentResolver) throws FileNotFoundException { switch (URI_MATCHER.match(uri)) { case ID_CONTACTS_CONTACT: return openContactPhotoInputStream(contentResolver, uri); case ID_CONTACTS_LOOKUP: case ID_LOOKUP_BY_PHONE: // If it was a Lookup uri then resolve it first, then continue loading the contact uri. uri = ContactsContract.Contacts.lookupContact(contentResolver, uri); if (uri == null) { throw new FileNotFoundException("Contact cannot be found"); } return openContactPhotoInputStream(contentResolver, uri); case ID_CONTACTS_THUMBNAIL: case ID_CONTACTS_PHOTO: case UriMatcher.NO_MATCH: default: return contentResolver.openInputStream(uri); } }
@Nullable public static DatabaseIdRange createFromContentProviderResults(ContentProviderResult[] results, String path){ UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); sUriMatcher.addURI(MessengerContentProvider.AUTHORITY, path, MATCH_CODE); Integer f = null; Integer l = null; for(ContentProviderResult result : results){ if(result.uri != null && !result.uri.toString().isEmpty()){ if(sUriMatcher.match(result.uri) != MATCH_CODE){ continue; } int dbid = Integer.parseInt(result.uri.getPathSegments().get(1)); if(f == null || dbid < f){ f = dbid; } if(l == null || dbid > l){ l = dbid; } } } return nonNull(f) && nonNull(l) ? new DatabaseIdRange(f, l) : null; }
/** * Tests that UriMatcher returns the correct integer value for each of the Uri types that * our ContentProvider can handle. */ @Test public void testUriMatcher() { UriMatcher testMatcher = FavoritesProvider.buildUriMatcher(); assertEquals("Error: The movie was matched incorrectly.", testMatcher.match(TEST_MOVIES_DIR), FavoritesProvider.MOVIES); assertEquals("Error: The movie was matched incorrectly.", testMatcher.match(TEST_MOVIE_ID_DIR), FavoritesProvider.MOVIE_ID); assertEquals("Error: The TV Show was matched incorrectly.", testMatcher.match(TEST_TV_SHOW_DIR), FavoritesProvider.TV_SHOWS); assertEquals("Error: The TV Show was matched incorrectly.", testMatcher.match(TEST_TV_SHOW_ID_DIR), FavoritesProvider.TV_SHOW_ID); assertEquals("Error: The Person was matched incorrectly.", testMatcher.match(TEST_PERSON_DIR), FavoritesProvider.PERSONS); assertEquals("Error: The Person was matched incorrectly.", testMatcher.match(TEST_PERSON_ID_DIR), FavoritesProvider.PERSON_ID); }
/** * 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(ArticleContract.AUTHORITY, ArticleContract.PATH_ARTICLES, ARTICLES); return uriMatcher; }
/** 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(AUTHORITY, PATH_TASKS, TASKS); uriMatcher.addURI(AUTHORITY, 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); }
/** 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; }
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; }
private static UriMatcher buildUriMatcher() { //The code passed into the constructor represents the code to return for the root URI. final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); final String authority = GalleryContract.CONTENT_AUTHORITY; //for the type of URI we want to add, create a corresponding code //for gallery matcher.addURI(authority, GalleryContract.PATH_IMAGE, IMAGE); matcher.addURI(authority, GalleryContract.PATH_IMAGE + "/#", IMAGE_ID); //for article matcher.addURI(authority, ArticleContract.PATH_ARTICLE, ARTICLE); matcher.addURI(authority, ArticleContract.PATH_ARTICLE + "/#", ARTICLE_ID); return matcher; }
/** * Builds up a UriMatcher for search suggestion and shortcut refresh queries. */ private static UriMatcher buildUriMatcher() { UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); // to get definitions... matcher.addURI(AUTHORITY, "dictionary", SEARCH_WORDS); matcher.addURI(AUTHORITY, "dictionary/#", GET_WORD); // to get suggestions... matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST); matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST); /* The following are unused in this implementation, but if we include * {@link SearchManager#SUGGEST_COLUMN_SHORTCUT_ID} as a column in our suggestions table, we * could expect to receive refresh queries when a shortcutted suggestion is displayed in * Quick Search Box, in which case, the following Uris would be provided and we * would return a cursor with a single item representing the refreshed suggestion data. */ matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT, REFRESH_SHORTCUT); matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT + "/*", REFRESH_SHORTCUT); return matcher; }
@Override public boolean onCreate() { /*if (KillSelfHelper.DEBUG) { LogUtils.d(KillSelfHelper.TAG, TAG + ".onCreate = " + android.os.Process.myPid()); }*/ 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; } }
@Override public int delete(Uri uri, String selection, String[] selectionArgs) { int code = matcher.match(uri); int result = 0; switch (code) { case UriMatcher.NO_MATCH: break; case 1: // 删除所有 result = db.delete(DBHelper.USERTABLE, null, null); Log.d("qf", "删除所有数据!"); break; case 2: // content://com.lenve.cphost.mycontentprovider/user/10 // 按条件删除,id result = db.delete(DBHelper.USERTABLE, "_id=?", new String[] { ContentUris.parseId(uri) + "" }); Log.d("qf", "根据删除一条数据"); break; case 3: // content://com.lenve.cphost.mycontentprovider/user/zhangsan // uri.getPathSegments()拿到一个List<String>,里边的值分别是0-->user、1-->zhangsan result = db.delete(DBHelper.USERTABLE, "USERNAME=?", new String[] { uri.getPathSegments().get(1) }); break; default: break; } return result; }
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; }
/** * @see TrayContract#setAuthority(String) */ static void setAuthority(final String authority) { sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH); sURIMatcher.addURI(authority, TrayContract.Preferences.BASE_PATH, ALL_PREFERENCE); // BASE/module sURIMatcher.addURI(authority, TrayContract.Preferences.BASE_PATH + "/*", MODULE_PREFERENCE); // BASE/module/key sURIMatcher.addURI(authority, TrayContract.Preferences.BASE_PATH + "/*/*", SINGLE_PREFERENCE); sURIMatcher.addURI(authority, TrayContract.InternalPreferences.BASE_PATH, INTERNAL_ALL_PREFERENCE); // INTERNAL_BASE/module sURIMatcher.addURI(authority, TrayContract.InternalPreferences.BASE_PATH + "/*", INTERNAL_MODULE_PREFERENCE); // INTERNAL_BASE/module/key sURIMatcher.addURI(authority, TrayContract.InternalPreferences.BASE_PATH + "/*/*", INTERNAL_SINGLE_PREFERENCE); }
public static UriMatcher buildUriMatcher() { final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); //Mapeamento das URIs referente aos logs de utilização de energia: final String energyUseLogAuthority = OhaEnergyUseContract.CONTENT_AUTHORITY; matcher.addURI(energyUseLogAuthority, PATH_LOG, CODE_ENERGY_USER_LOG); matcher.addURI(energyUseLogAuthority, PATH_DAYS, CODE_ENERGY_USER_DAYS); matcher.addURI(energyUseLogAuthority, PATH_BILL, CODE_ENERGY_USER_BILL); return matcher; }
/** * A static method to construct a UriMatcher * @return a {@link UriMatcher} */ public static UriMatcher buildUriMatcher() { //initialise a uri matcher UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); //add uris for detection uriMatcher.addURI(PlacesContract.AUTHORITY, PlacesContract.PATH, PLACES); uriMatcher.addURI(PlacesContract.AUTHORITY, PlacesContract.PATH + "/#", SINGLE_PLACE_WITH_ID); uriMatcher.addURI(PlacesContract.AUTHORITY,PlacesContract.TIME_PATH,TIME); uriMatcher.addURI(PlacesContract.AUTHORITY,PlacesContract.TIME_PATH + "/#",SINGLE_TIME_WITH_ID); return uriMatcher; }
private static UriMatcher buildUriMatcher() { final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); final String authority = ItemsContract.CONTENT_AUTHORITY; matcher.addURI(authority, "items", ITEMS); matcher.addURI(authority, "items/#", ITEMS__ID); 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; }
static UriMatcher buildUriMatcher() { final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); final String authority = TvContract.CONTENT_AUTHORITY; // For each desc of URI you want to add, create a corresponding code. matcher.addURI(authority, TvContract.PATH_CATEGORY, CATEGORY); matcher.addURI(authority, TvContract.PATH_CATEGORY +"/#", CATEGORY_WITH_ID); matcher.addURI(authority, TvContract.PATH_CHANEL, CHANNEL); matcher.addURI(authority, TvContract.PATH_CHANEL +"/#", CHANNEL_WITH_ID); matcher.addURI(authority, TvContract.PATH_PROGRAM, PROGRAM); matcher.addURI(authority, TvContract.PATH_PROGRAM +"/#", PROGRAM_WITH_ID); return matcher; }
public void testUriMatcher() { UriMatcher testMatcher = LeaderProvider.buildUriMatcher(); assertEquals("Error: The LEADER URI was matched incorrectly.", testMatcher.match(TEST_LEADER_DIR), LeaderProvider.LEADER); assertEquals("Error: The PROFILE URI was matched incorrectly", testMatcher.match(TEST_PROFILE_ITEM), LeaderProvider.PROFILE); }
private static UriMatcher buildUriMatcher() { UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); matcher.addURI(AUTHORITY, "quran/search", SEARCH_VERSES); matcher.addURI(AUTHORITY, "quran/search/*", SEARCH_VERSES); matcher.addURI(AUTHORITY, "quran/search/*/*", SEARCH_VERSES); matcher.addURI(AUTHORITY, "quran/verse/#/#", GET_VERSE); matcher.addURI(AUTHORITY, "quran/verse/*/#/#", GET_VERSE); matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST); matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST); return matcher; }
public static UriMatcher buildUriMatcher() { UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(TaskItemsContract.AUTHORITY, TaskItemsContract.PATH_TASK_ITEMS, ITEM_TASKS); uriMatcher.addURI(TaskItemsContract.AUTHORITY, TaskItemsContract.PATH_TASK_ITEMS + "/#", ITEM_TASKS_WITH_ID); uriMatcher.addURI(TaskItemsContract.AUTHORITY, TaskItemsContract.PATH_TASK_ITEMS + "/" + COLUMN_NAME_IS_TODAY + "/#", ITEM_TASKS_TODAY); return uriMatcher; }
static UriMatcher buildUriMatcher() { UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); // The calls to addURI() go here, for all of the content URI patterns that the provider // should recognize. All paths added to the UriMatcher have a corresponding code to return // when a match is found. // The content URI of the form "content://com.xengar.android.movieguide/movie" will map to // the integer code {@link #MOVIES}. This URI is used to provide access to MULTIPLE rows // of the verbs table. matcher.addURI(FavoritesContract.AUTHORITY, FavoritesContract.PATH_MOVIE, MOVIES); // The content URI of the form "content://com.xengar.android.movieguide/movie/#" will map // to the integer code {@link #MOVIE_ID}. This URI is used to provide access to ONE single // row of the verbs table. // // In this case, the "#" wildcard is used where "#" can be substituted for an integer. // For example, "content://com.xengar.android.movieguide/movie/3" matches, but // "content://com.xengar.android.movieguide/movie" (without a number at the end) doesn't. matcher.addURI(FavoritesContract.AUTHORITY, FavoritesContract.PATH_MOVIE_ID, MOVIE_ID); matcher.addURI(FavoritesContract.AUTHORITY, FavoritesContract.PATH_TV_SHOW, TV_SHOWS); matcher.addURI(FavoritesContract.AUTHORITY, FavoritesContract.PATH_TV_SHOW_ID, TV_SHOW_ID); matcher.addURI(FavoritesContract.AUTHORITY, FavoritesContract.PATH_PERSON, PERSONS); matcher.addURI(FavoritesContract.AUTHORITY, FavoritesContract.PATH_PERSON_ID, PERSON_ID); return matcher; }
/** * 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, "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); // 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.MANAGE_DOCUMENTS.equals(info.readPermission) || !android.Manifest.permission.MANAGE_DOCUMENTS.equals(info.writePermission)) { throw new SecurityException("Provider must be protected by MANAGE_DOCUMENTS"); } */ super.attachInfo(context, info); }
private static UriMatcher buildUriMatcher() { final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); final String authority = ActivityItemsContract.CONTENT_AUTHORITY; matcher.addURI(authority, "items", ITEMS); matcher.addURI(authority, "items/#", ITEMS_ID); matcher.addURI(authority, "range/#/#", ITEMS_RANGE); return matcher; }