Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement getType() and use UriMatcher #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
Expand All @@ -23,9 +24,13 @@ public abstract class AbstractProvider extends ContentProvider {

protected final String mLogTag;
protected SQLiteDatabase mDatabase;
protected final Object mMatcherSynchronizer;
protected UriMatcher mMatcher;
protected MatchDetail[] mMatchDetails;

protected AbstractProvider() {
mLogTag = getClass().getName();
mMatcherSynchronizer = new Object();
}

@Override
Expand Down Expand Up @@ -54,6 +59,58 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
return false;
}

/**
* Initializes the matcher, based on the tables contained within the subclass,
* also guarantees to have initialized the mTableNames[] array as well.
*/
protected void initializeMatcher() {
// initialize the UriMatcher once, use a synchronized block
// here, so that subclasses can implement this by static initialization if
// they want.
synchronized (mMatcherSynchronizer) {
if(mMatcher != null) {
return;
}

mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
List<MatchDetail> details = new ArrayList<>();

String authority = getAuthority();

for (Class<?> clazz : getClass().getClasses()) {
Table table = clazz.getAnnotation(Table.class);
if (table != null) {
String tableName = Utils.getTableName(clazz, table);
String mimeName = Utils.getMimeName(clazz, table);

// Add the plural version
mMatcher.addURI(authority, tableName, details.size());
details.add(new MatchDetail(tableName, "vnd.android.cursor.dir/vnd." + getAuthority() + "." + mimeName, false));

// Add the singular version
mMatcher.addURI(authority, tableName + "/#", details.size());
details.add(new MatchDetail(tableName, "vnd.android.cursor.item/vnd." + getAuthority() + "." + mimeName, true));
}
}

// Populate the rest.
mMatchDetails = details.toArray(new MatchDetail[0]);
}
}

private static class MatchDetail
{
public final String tableName;
public final String mimeType;
public final boolean forceIdColumn;

public MatchDetail(String tableName, String mimeType, boolean forceIdColumn) {
this.tableName = tableName;
this.mimeType = mimeType;
this.forceIdColumn = forceIdColumn;
}
}

/**
* Called when the database needs to be updated and after <code>AbstractProvider</code> has
* done its own work. That is, after creating columns that have been added using the
Expand Down Expand Up @@ -93,7 +150,15 @@ protected int getSchemaVersion() {

@Override
public String getType(Uri uri) {
return null;
initializeMatcher();

int match = mMatcher.match(uri);

if(match == UriMatcher.NO_MATCH) {
return null;
}

return mMatchDetails[match].mimeType;
}

@Override
Expand All @@ -118,14 +183,19 @@ private ContentResolver getContentResolver() {
}

private SelectionBuilder buildBaseQuery(Uri uri) {
List<String> pathSegments = uri.getPathSegments();
if (pathSegments == null) {
return null;
initializeMatcher();

int match = mMatcher.match(uri);

if(match == UriMatcher.NO_MATCH) {
throw new IllegalArgumentException("Unsupported content uri");
}

SelectionBuilder builder = new SelectionBuilder(pathSegments.get(0));
MatchDetail detail = mMatchDetails[match];

SelectionBuilder builder = new SelectionBuilder(detail.tableName);

if (pathSegments.size() == 2) {
if(detail.forceIdColumn) {
builder.whereEquals(BaseColumns._ID, uri.getLastPathSegment());
}

Expand All @@ -134,12 +204,15 @@ private SelectionBuilder buildBaseQuery(Uri uri) {

@Override
public Uri insert(Uri uri, ContentValues values) {
List<String> segments = uri.getPathSegments();
if (segments == null || segments.size() != 1) {
return null;
initializeMatcher();

int match = mMatcher.match(uri);

if(match == UriMatcher.NO_MATCH) {
throw new IllegalArgumentException("Unsupported content uri");
}

long rowId = mDatabase.insert(segments.get(0), null, values);
long rowId = mDatabase.insert(mMatchDetails[match].tableName, null, values);

if (rowId > -1) {
getContentResolver().notifyChange(uri, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@

int since() default 1;

String mimeSuffix() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ static String getTableName(Class<?> clazz, Table table) {
}
}

static String getMimeName(Class<?> clazz, Table table) {
String mimeName = table.mimeSuffix();
if(TextUtils.isEmpty(mimeName)) {
return clazz.getSimpleName().toLowerCase(java.util.Locale.US);
}
return mimeName;
}

static String pluralize(String string) {
string = string.toLowerCase();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.triplet.simpleprovider;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
Expand Down Expand Up @@ -84,4 +85,15 @@ public void testQueryById() {
CONTENT_2, c.getString(c.getColumnIndex(TestProvider.Post.CONTENT)));
assertFalse("There shouldn't be any more entries", c.moveToNext());
}

@Test
public void testGetType() {
String actual = mContentResolver.getType(mPostsUri);

assertEquals("vnd.android.cursor.dir/vnd." + TestProvider.AUTHORITY + ".post", actual);

actual = mContentResolver.getType(ContentUris.withAppendedId(mPostsUri , 1));

assertEquals("vnd.android.cursor.item/vnd." + TestProvider.AUTHORITY + ".post", actual);
}
}