uib-events/app/src/main/java/no/nilsnh/uibevents/data/EventProvider.java
Nils Norman Haukås f8327b51f2 initial commit
2015-04-16 20:50:29 +02:00

84 lines
2.7 KiB
Java

package no.nilsnh.uibevents.data;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
public class EventProvider extends ContentProvider{
private EventDbHelper eventDbHelper;
private static final UriMatcher sUriMatcher = buildUriMatcher();
static final int EVENT = 100;
@Override
public boolean onCreate() {
eventDbHelper = new EventDbHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return null;
}
@Override
public String getType(Uri uri) {
final int match = sUriMatcher.match(uri);
switch (match) {
case EVENT:
return EventContract.EventEntry.CONTENT_ITEM_TYPE;
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
final int match = sUriMatcher.match(uri);
Uri returnUri;
switch (match) {
case EVENT: {
//Insert into db and get id.
long _id = eventDbHelper.insert(values);
if ( _id > 0 )
returnUri = EventContract.EventEntry.buildEventUri(_id);
else
throw new android.database.SQLException("Failed to insert row into " + uri);
break;
}
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
return returnUri;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
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 = EventContract.CONTENT_AUTHORITY;
// For each type of URI you want to add, create a corresponding code.
matcher.addURI(authority, EventContract.PATH_EVENT, EVENT);
return matcher;
}
}