Skip to content

Instantly share code, notes, and snippets.

@silmood
Created February 7, 2015 02:29
Show Gist options
  • Select an option

  • Save silmood/eab687aae0d62e355202 to your computer and use it in GitHub Desktop.

Select an option

Save silmood/eab687aae0d62e355202 to your computer and use it in GitHub Desktop.
Content Provider & DB model
public class CardsContract {
//TODO: rename package
public static final String CONTENT_AUTHORITY = "com.example.joseluisrosasbaza.marco_polo1";
public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
public static final String PATH_CARDS = "cards";
public static final String PATH_DECK = "deck";
public static final class CardsEntry implements BaseColumns {
public static final String TABLE_NAME = "cards";
public static final String COLUMN_CARD_ID = "card_server_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_LAT = "latitude";
public static final String COLUMN_LNG = "longitude";
public static final String COLUMN_ATK = "attack";
public static final String COLUMN_DEF = "defense";
public static final Uri CONTENT_URI =
BASE_CONTENT_URI.buildUpon().appendPath(PATH_CARDS).build();
public static final String CONTENT_TYPE =
"vnd.android.cursor.dir/" + CONTENT_AUTHORITY + "/" + PATH_CARDS;
public static final String CONTENT_ITEM_TYPE =
"vnd.android.cursor.item/" + CONTENT_AUTHORITY + "/" + PATH_CARDS;
/**
* Create an uri to search a card by ID
*
* @param id Card id
* */
public static Uri buildCardUri (long id) {
return ContentUris.withAppendedId(CONTENT_URI, id);
}
public static Uri buildCardLatLngUri (float lat, float lng) {
return BASE_CONTENT_URI.buildUpon()
.appendPath(String.valueOf(lat))
.appendPath(String.valueOf(lng))
.build();
}
public static String getLatFromUri (Uri uri) {
return uri.getPathSegments().get(1);
}
public static String getLngFromUri (Uri uri) {
return uri.getPathSegments().get(2);
}
};
public static final class DeckEntry implements BaseColumns {
public static final String TABLE_NAME = "deck";
public static final String COLUMN_CARD_ID = "card_id";
public static final Uri CONTENT_URI =
BASE_CONTENT_URI.buildUpon().appendPath(PATH_CARDS).build();
public static final String CONTENT_TYPE =
"vnd.android.cursor.dir/" + CONTENT_AUTHORITY + "/" + PATH_CARDS;
public static final String CONTENT_ITEM_TYPE =
"vnd.android.cursor.item/" + CONTENT_AUTHORITY + "/" + PATH_CARDS;
/**
* Create an uri to search a card by ID
*
* @param id Card id
* */
public static Uri buildCardUri (long id) {
return ContentUris.withAppendedId(CONTENT_URI, id);
}
};
}
public class CardsDBHelper extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "marcopolo.db";
public CardsDBHelper (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
final String SQL_CREATE_CARDS_TABLE = "CREATE TABLE "+ CardsContract.CardsEntry.TABLE_NAME +" (" +
CardsContract.CardsEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
CardsContract.CardsEntry.COLUMN_CARD_ID + " INTEGER NOT NULL " +
CardsContract.CardsEntry.COLUMN_NAME + " TEXT NOT NULL " +
CardsContract.CardsEntry.COLUMN_LAT + " REAL NOT NULL " +
CardsContract.CardsEntry.COLUMN_LNG + " REAL NOT NULL " +
CardsContract.CardsEntry.COLUMN_ATK+ " INTEGER NOT NULL " +
CardsContract.CardsEntry.COLUMN_DEF+ " INTEGER NOT NULL " +
"UNIQUE (" + CardsContract.CardsEntry.COLUMN_CARD_ID + ") ON CONFLICT IGNORE " +
");";
db.execSQL(SQL_CREATE_CARDS_TABLE);
final String SQL_CREATE_DECK_TABLE = "CREATE TABLE "+ CardsContract.DeckEntry.TABLE_NAME +" (" +
CardsContract.DeckEntry.COLUMN_CARD_ID +
" FOREIGN KEY (" + CardsContract.DeckEntry.COLUMN_CARD_ID +") REFERENCES "+
CardsContract.CardsEntry.TABLE_NAME +" ("+ CardsContract.CardsEntry._ID +"));";
db.execSQL(SQL_CREATE_CARDS_TABLE);
db.execSQL(SQL_CREATE_DECK_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + CardsContract.CardsEntry.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + CardsContract.DeckEntry.TABLE_NAME);
onCreate(db);
}
}
public class CardsProvider extends ContentProvider{
private static final UriMatcher sUriMatcher = buildUriMatcher();
private CardsDBHelper mOpenHelper;
public static final int CARDS = 100;
public static final int CARD_ID = 101;
public static final int CARD_LATLNG = 102;
public static final int DECK = 200;
private static UriMatcher buildUriMatcher() {
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = GowiContract.CONTENT_AUTHORITY;
matcher.addURI(authority, CardsContract.PATH_CARDS, CARDS);
matcher.addURI(authority, CardsContract.PATH_CARDS + "/#", CARD_ID);
matcher.addURI(authority, CardsContract.PATH_CARDS + "/#" + "/#", CARD_LATLNG);
matcher.addURI(authority, CardsContract.PATH_DECK , DECK);
return matcher;
}
@Override
public boolean onCreate() {
mOpenHelper = new CardsDBHelper(getContext());
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Cursor retCursor;
switch (sUriMatcher.match(uri)){
case CARDS:{
retCursor = mOpenHelper.getReadableDatabase().query(
CardsContract.CardsEntry.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
sortOrder
);
return retCursor;
}
case CARD_ID: {
retCursor = mOpenHelper.getReadableDatabase().query(
CardsContract.CardsEntry.TABLE_NAME,
projection,
CardsContract.CardsEntry._ID+ " = " + ContentUris.parseId(uri),
null,
null,
null,
sortOrder
);
return retCursor;
}
case CARD_LATLNG: {
retCursor = mOpenHelper.getReadableDatabase().query(
CardsContract.CardsEntry.TABLE_NAME,
projection,
CardsContract.CardsEntry.COLUMN_LAT+ " = " +
CardsContract.CardsEntry.getLatFromUri(uri) +" AND "+
CardsContract.CardsEntry.COLUMN_LNG + " = " + CardsContract.CardsEntry.getLngFromUri(uri) ,
null,
null,
null,
sortOrder
);
return retCursor;
}
case DECK: {
retCursor = mOpenHelper.getReadableDatabase().query(
CardsContract.DeckEntry.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
sortOrder
);
return retCursor;
}
default:
throw new UnsupportedOperationException("Unknow Uri: "+uri);
}
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
final int match = sUriMatcher.match(uri);
Uri returnUri;
switch (match){
case CARDS: {
long _id = db.insert(CardsContract.CardsEntry.TABLE_NAME, null, values);
if ( _id > 0 )
returnUri = CardsContract.CardsEntry.buildCardUri(_id);
else
throw new android.database.SQLException("Failed to insert row into " + uri);
break;
}
case DECK: {
long _id = db.insert(CardsContract.DeckEntry.TABLE_NAME, null, values);
if ( _id > 0 )
returnUri = CardsContract.CardsEntry.buildCardUri(_id);
else
throw new android.database.SQLException("Failed to insert row into " + uri);
break;
}
default:
throw new UnsupportedOperationException("Unknow Uri: "+uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return returnUri;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
final int match = sUriMatcher.match(uri);
int rowsDeleted;
switch (match){
case CARDS:
rowsDeleted = db.delete(CardsContract.CardsEntry.TABLE_NAME,selection,selectionArgs);
break;
case DECK:
rowsDeleted = db.delete(CardsContract.DeckEntry.TABLE_NAME,selection,selectionArgs);
break;
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
// Because a null deletes all rows
if (selection == null || rowsDeleted != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return rowsDeleted;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
final int match = sUriMatcher.match(uri);
int rowsUpdated;
switch (match){
case CARDS:
rowsUpdated = db.update(CardsContract.CardsEntry.TABLE_NAME,
values,
selection,
selectionArgs);
break;
case DECK:
rowsUpdated = db.update(CardsContract.DeckEntry.TABLE_NAME,
values,
selection,
selectionArgs);
break;
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
if ( rowsUpdated != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return rowsUpdated;
}
@Override
public int bulkInsert(Uri uri, ContentValues[] values) {
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
final int match = sUriMatcher.match(uri);
switch (match) {
case CARDS:
db.beginTransaction();
int returnCount = 0;
try {
for (ContentValues value : values){
long _id = db.insert(CardsContract.CardsEntry.TABLE_NAME, null, value);
if (_id != -1) {
returnCount++;
}
}
db.setTransactionSuccessful();
}finally {
db.endTransaction();
}
getContext().getContentResolver().notifyChange(uri,null);
return returnCount;
default:
return super.bulkInsert(uri, values);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment