Code example for Uri

Methods: getAuthority, getPath

0
        mDirType     = dirType;
        mItemType    = itemType;
        mTableName   = tableName;
        mContentUri  = contentUri;
 
        String basePath = contentUri.getPath().substring(1);
 
        mUriMatcher = createUriMatcher(contentUri, basePath);
    } 
 
    @Override 
    public boolean onCreate() { 
        try { 
            mHelper = mHelperClass.getConstructor(Context.class).newInstance(getContext());
        } catch (Exception e) {
            throw new RuntimeException(e);
        } 
        return true; 
    } 
 
    @Override 
    public Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        builder.setTables(mTableName);
        appendWheres(builder, mUriMatcher, uri);
 
        SQLiteDatabase db = mHelper.getReadableDatabase();
 
        Cursor cursor = builder.query(db, null, selection, selectionArgs, null, null, sortOrder);
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    } 
 
    @Override 
    public String getType(Uri uri) {
        switch (mUriMatcher.match(uri)) {
            case CODE_COLLECTION:
                return mDirType;
            case CODE_SINGLE:
                return mItemType;
            default: 
                throw new IllegalArgumentException("Unknown URI: " + uri);
        } 
    } 
 
    @Override 
    public Uri insert(Uri uri, ContentValues values) {
        if (mUriMatcher.match(uri) != CODE_COLLECTION) {
            throw new IllegalArgumentException("Incorrect URI: " + uri);
        } 
 
        SQLiteDatabase db = mHelper.getWritableDatabase();
        long rowId = db.insertOrThrow(mTableName, null, values);
 
        Uri itemUri = ContentUris.withAppendedId(mContentUri, rowId);
        getContext().getContentResolver().notifyChange(itemUri, null);
 
        return itemUri;
    } 
 
    @Override 
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = mHelper.getWritableDatabase();
        int count = 0;
        switch (mUriMatcher.match(uri)) {
            case CODE_SINGLE:
                String rowId = uri.getPathSegments().get(1);
                if (TextUtils.isEmpty(selection)) {
                    count = db.delete(mTableName, BaseColumns._ID + "=?", new String[] { rowId });
                } else { 
                    count = db.delete(mTableName,
                        selection + " AND " + BaseColumns._ID + "=" + rowId,
                        selectionArgs);
                } 
                break; 
            case CODE_COLLECTION:
                count = db.delete(mTableName, selection, selectionArgs);
                break; 
        } 
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    } 
 
    @Override 
    public int update (Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        SQLiteDatabase db = mHelper.getWritableDatabase();
        int count = 0;
        switch (mUriMatcher.match(uri)) {
            case CODE_COLLECTION:
                count = db.update(mTableName, values, selection,  selectionArgs);
                break; 
            case CODE_SINGLE:
                String rowId = uri.getPathSegments().get(1);
                if (TextUtils.isEmpty(selection)) {
                    count = db.update(mTableName, values, BaseColumns._ID + "=" + rowId, null);
                } else { 
                    count = db.update(mTableName, values,
                        selection + " AND " + BaseColumns._ID + "=" + rowId,
                        selectionArgs);
                } 
                break; 
            default: 
                throw new IllegalArgumentException("Unknown URI: " + uri);
        } 
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    } 
 
    protected UriMatcher createUriMatcher(Uri contentUri, String basePath) {
        UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
        matcher.addURI(contentUri.getAuthority(), basePath,        CODE_COLLECTION);
        matcher.addURI(contentUri.getAuthority(), basePath + "/#", CODE_SINGLE);
        return matcher;
    } 
 
    protected void appendWheres(SQLiteQueryBuilder builder, UriMatcher matcher, Uri uri) {
        switch (matcher.match(uri)) {