Created
August 12, 2017 06:09
-
-
Save nikhilbansal97/c01fce9f59ac0cb10945b83d2a92d352 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.nikhil.simpledatabase.database; | |
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 android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
public class DataContentProvider extends ContentProvider{ | |
private static final String TAG = "DataContentProvider"; | |
public static final int ITEM_CODE = 1001; | |
public static final int ITEM_ID_CODE = 1002; | |
public static final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); | |
private DataHelper mDbHelper; | |
static { | |
matcher.addURI(DataContract.DataProvider.AUTHORITY, DataContract.DataProvider.PATH_TABLE,ITEM_CODE); | |
matcher.addURI(DataContract.DataProvider.AUTHORITY, DataContract.DataProvider.PATH_ID_TABLE, ITEM_ID_CODE); | |
} | |
@Override | |
public boolean onCreate() { | |
mDbHelper = new DataHelper(getContext()); | |
return true; | |
} | |
@Nullable | |
@Override | |
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String s1) { | |
SQLiteDatabase database = mDbHelper.getReadableDatabase(); | |
int code = matcher.match(uri); | |
Cursor cursor = null; | |
switch (code) { | |
case ITEM_CODE : | |
cursor = database.query(DataContract.DataProvider.TABLE_NAME, | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
null); | |
break; | |
case ITEM_ID_CODE : | |
selection = DataContract.DataProvider.COLUMN_ID + "=?"; | |
selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))}; | |
cursor = database.query(DataContract.DataProvider.TABLE_NAME, | |
projection, | |
selection, | |
selectionArgs, | |
null,null,null); | |
break; | |
default: | |
throw new IllegalArgumentException("Failed to query data for uri : " + uri); | |
} | |
cursor.setNotificationUri(getContext().getContentResolver(),uri); | |
return cursor; | |
} | |
@Nullable | |
@Override | |
public String getType(@NonNull Uri uri) { | |
int code = matcher.match(uri); | |
if(code == ITEM_CODE) | |
return DataContract.DataProvider.CONTENT_LIST_TYPE; | |
else | |
return DataContract.DataProvider.CONTENT_ITEM_TYPE; | |
} | |
@Nullable | |
@Override | |
public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) { | |
SQLiteDatabase database = mDbHelper.getWritableDatabase(); | |
String item_name = contentValues.getAsString(DataContract.DataProvider.COLUMN_DATA_ITEM); | |
if(item_name.isEmpty()) | |
throw new IllegalArgumentException("Incomplete Data! Insert failed."); | |
long id; | |
id = database.insert(DataContract.DataProvider.TABLE_NAME, null, contentValues); | |
if(id != -1) | |
{ | |
Uri uriReturn = ContentUris.withAppendedId(uri,id); | |
getContext().getContentResolver().notifyChange(uri,null); | |
return uriReturn; | |
} | |
else | |
throw new IllegalArgumentException("Insert failed!"); | |
} | |
@Override | |
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) { | |
SQLiteDatabase database = mDbHelper.getWritableDatabase(); | |
int code = matcher.match(uri); | |
int row; | |
switch (code){ | |
case ITEM_CODE : | |
row = database.delete(DataContract.DataProvider.TABLE_NAME,null,null); | |
getContext().getContentResolver().notifyChange(uri,null); | |
return row; | |
case ITEM_ID_CODE : | |
selection = DataContract.DataProvider.COLUMN_ID + "=?"; | |
selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))}; | |
row = database.delete(DataContract.DataProvider.TABLE_NAME,selection,selectionArgs); | |
getContext().getContentResolver().notifyChange(uri,null); | |
return row; | |
} | |
return 0; | |
} | |
@Override | |
public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String selection, @Nullable String[] selectionArgs) { | |
SQLiteDatabase database = mDbHelper.getWritableDatabase(); | |
int id; | |
selection = DataContract.DataProvider.COLUMN_ID + "=?"; | |
selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))}; | |
id = database.update(DataContract.DataProvider.TABLE_NAME,contentValues,selection,selectionArgs); | |
getContext().getContentResolver().notifyChange(uri,null); | |
return id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment