Skip to content

Instantly share code, notes, and snippets.

@osiloke
Created April 10, 2015 22:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osiloke/fb72c29fb69bd19c0026 to your computer and use it in GitHub Desktop.
Save osiloke/fb72c29fb69bd19c0026 to your computer and use it in GitHub Desktop.
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import com.progwebtech.android.foodisready.model.BackDrop;
import com.progwebtech.android.foodisready.model.Canteen;
import com.progwebtech.android.foodisready.model.ThumbnailImage;
import com.progwebtech.android.foodisready.model.User;
import com.progwebtech.android.foodisready.model.Versions;
import static nl.qbusict.cupboard.CupboardFactory.cupboard;
public class MainContentProvider extends ContentProvider {
public static String AUTHORITY = "com.progweb.android.foodisready.provider";
public static final Uri CANTEEN_URI = Uri.parse("content://"+AUTHORITY+"/canteen");
public static final Uri BACKDROP_URI = Uri.parse("content://"+AUTHORITY+"/back_drop");
public static final Uri VERSIONS_URI = Uri.parse("content://"+AUTHORITY+"/versions");
public static final Uri THUMBNAIL_URI = Uri.parse("content://"+AUTHORITY+"/thumbnail");
public static final Uri USER_URI = Uri.parse("content://"+AUTHORITY+"/user");
private CupboardSQLiteOpenHelper mDatabaseHelper;
private static UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int CANTEEN = 0;
private static final int CANTEENS = 1;
private static final int USER = 2;
private static final int BACK_DROP = 3;
private static final int VERSIONS = 4;
private static final int VERSION = 5;
private static final int THUMBNAIL = 6;
static {
sMatcher.addURI(AUTHORITY, "canteen/#", CANTEEN);
sMatcher.addURI(AUTHORITY, "canteen", CANTEENS);
sMatcher.addURI(AUTHORITY, "user/#", USER);
sMatcher.addURI(AUTHORITY, "back_drop/#", BACK_DROP);
sMatcher.addURI(AUTHORITY, "versions/#", VERSIONS);
sMatcher.addURI(AUTHORITY, "version/#", VERSION);
sMatcher.addURI(AUTHORITY, "thumbnail/#", THUMBNAIL);
}
@Override
public int delete(Uri uri, String selection, String[] args) {
return 0;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
@Override
public boolean onCreate() {
mDatabaseHelper = new CupboardSQLiteOpenHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
switch (sMatcher.match(uri)) {
case CANTEENS:
// this is the full query syntax, most of the time you can leave out projection etc
// if the content provider returns a fixed set of data
return cupboard().withDatabase(db).query(Canteen.class).
withProjection(projection).
withSelection(selection, selectionArgs).
orderBy(sortOrder).
getCursor();
case CANTEEN:
return cupboard().withDatabase(db).query(Canteen.class).
byId(ContentUris.parseId(uri)).
getCursor();
case USER:
return cupboard().withDatabase(db).query(User.class).
byId(ContentUris.parseId(uri)).
getCursor();
case BACK_DROP:
return cupboard().withDatabase(db).query(BackDrop.class).
byId(ContentUris.parseId(uri)).
getCursor();
case VERSIONS:
return cupboard().withDatabase(db).query(Versions.class).
withProjection(projection).
withSelection(selection, selectionArgs).
orderBy(sortOrder).
getCursor();
case VERSION:
return cupboard().withDatabase(db).query(Versions.class).
byId(ContentUris.parseId(uri)).
getCursor();
case THUMBNAIL:
return cupboard().withDatabase(db).query(ThumbnailImage.class).
byId(ContentUris.parseId(uri)).
getCursor();
}
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment