Back to project page Grocery-List.
The source code is released under:
MIT License
If you think the Android project Grocery-List listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.evanwaldron.grocerylist.storage; /* ww w. j av a 2 s . c o m*/ import android.content.ContentProvider; import android.content.ContentProviderOperation; import android.content.ContentProviderResult; import android.content.ContentValues; import android.content.OperationApplicationException; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.util.Log; import java.util.ArrayList; /** * Created by Evan on 9/28/14 12:08 PM. */ public class GroceryContentProvider extends ContentProvider { private static final String LOG_TAG = GroceryContentProvider.class.getSimpleName(); static final String AUTH = "com.evanwaldron.grocerylist.content"; private static final int GROCERIES = 1; private static final UriMatcher uriMatcher; static{ uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(AUTH, Storage.Groceries.CONTENT_URI.getLastPathSegment(), GROCERIES); } private DBHelper dbHelper; @Override public boolean onCreate() { dbHelper = new DBHelper(getContext()); return true; } @Override public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations){ if(operations.size() == 0){ return null; } SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentProviderResult[] results = new ContentProviderResult[operations.size()]; db.beginTransaction(); try { for (int i = 0; i < operations.size(); i++) { ContentProviderOperation operation = operations.get(i); results[i] = operation.apply(this, results, i); } }catch(OperationApplicationException e){ Log.e(LOG_TAG, "Error performing db operation " + e.getMessage()); return null; }finally { db.setTransactionSuccessful(); } return results; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = dbHelper.getReadableDatabase(); String tableName = null; switch(uriMatcher.match(uri)){ case GROCERIES: tableName = Storage.Groceries.TABLE_NAME; break; default: Log.e(LOG_TAG, "Uri not recognized: " + uri.toString()); return null; } Cursor result = db.query(tableName, projection, selection, selectionArgs, null, null, null, null); if(result == null){ Log.e(LOG_TAG, "Failed to query uri " + uri.toString() + ", table " + tableName); return null; } result.setNotificationUri(getContext().getContentResolver(), uri); return result; } @Override public String getType(Uri uri) { return null; } @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = dbHelper.getWritableDatabase(); String tableName = null; switch(uriMatcher.match(uri)){ case GROCERIES: tableName = Storage.Groceries.TABLE_NAME; break; default: Log.e(LOG_TAG, "Uri not recognized: " + uri.toString()); return null; } long id = db.insert(tableName, null, values); if(id < 0){ Log.i(LOG_TAG, "An error occurred inserting into table " + tableName); return null; } getContext().getContentResolver().notifyChange(uri, null); return Uri.withAppendedPath(uri, Long.toString(id)); } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { String tableName; switch(uriMatcher.match(uri)){ case GROCERIES: tableName = Storage.Groceries.TABLE_NAME; break; default: Log.e(LOG_TAG, "Uri not recognized: " + uri.toString()); return 0; } SQLiteDatabase db = dbHelper.getWritableDatabase(); int result = db.delete(tableName, selection, selectionArgs); if(result == 0){ Log.d(LOG_TAG, "No items were deleted during delete call"); return result; } getContext().getContentResolver().notifyChange(uri, null); return result; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { String tableName; switch(uriMatcher.match(uri)){ case GROCERIES: tableName = Storage.Groceries.TABLE_NAME; break; default: Log.e(LOG_TAG, "Uri not recognized: " + uri.toString()); return 0; } SQLiteDatabase db = dbHelper.getWritableDatabase(); int result = db.update(tableName, values, selection, selectionArgs); if(result == 0){ Log.d(LOG_TAG, "No items were updated during update call"); return result; } getContext().getContentResolver().notifyChange(uri, null); return result; } }