package com.ved.musicmapapp.adapters; import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.net.Uri; public class DBContentProvider extends ContentProvider { private DatabaseHelper mDbHelper; private SQLiteDatabase database; private class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "Database_MusicMap"; private static final int DATABASE_VERSION = 1; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase database) { try { database.execSQL(DBAdapter.DATABASE_CREATE_ARTIST); database.execSQL(DBAdapter.DATABASE_CREATE_SONG); database.execSQL(DBAdapter.DATABASE_CREATE_USER); database.execSQL(DBAdapter.DATABASE_CREATE_USER_ARTIST); database.execSQL(DBAdapter.DATABASE_CREATE_PLAYLIST_ARTIST); database.execSQL(DBAdapter.DATABASE_CREATE_PL_ART_SONG); database.execSQL(DBAdapter.DATABASE_CREATE_FRIEND); database.execSQL(DBAdapter.DATABASE_CREATE_FB_BOOK); //Playlist related tables database.execSQL(DBAdapter.DATABASE_CREATE_PLAYLIST); database.execSQL(DBAdapter.DATABASE_CREATE_GENRE); } catch (Exception e) { e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { } } // content Provider Override @Override public int delete(Uri uri, String where, String[] selectionArgs) { String table = getTableName(uri); // SQLiteDatabase database = mDbHelper.getWritableDatabase(); int updatedRows = database.delete(table, where, selectionArgs); return updatedRows; } @Override public String getType(Uri uri) { return null; } @Override public Uri insert(Uri uri, ContentValues values) { // SQLiteDatabase database = mDbHelper.getWritableDatabase(); long rowID = database.insert(getTableName(uri), null, values); Uri _uri = null; if (rowID > 0) { _uri = ContentUris.withAppendedId(DBAdapter.CONTENT_URI, rowID); getContext().getContentResolver().notifyChange(_uri, null); }if(_uri == null) throw new SQLException("Failed to add a record into " + uri); else { return _uri; } } @Override public boolean onCreate() { mDbHelper = new DatabaseHelper(getContext()); /** * Create a write able database which will trigger its creation if it * doesn't already exist. */ database = mDbHelper.getWritableDatabase(); return (mDbHelper == null) ? false : true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { String table = getTableName(uri); // SQLiteDatabase database = mDbHelper.getReadableDatabase(); Cursor cursor = database.query(table, projection, selection, selectionArgs, null, null, sortOrder); return cursor; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { String table = getTableName(uri); // SQLiteDatabase database = mDbHelper.getWritableDatabase(); int updatedRows = database.update(table, values, selection, selectionArgs); return updatedRows; } public String getTableName(Uri uri) { String value = uri.getLastPathSegment(); return value; } @Override public int bulkInsert(Uri uri, ContentValues[] values) { int insertCount = 0; String table = getTableName(uri); // SQLiteDatabase database = null; try { // database = mDbHelper.getWritableDatabase(); database.beginTransaction(); for (ContentValues value : values) { long id = database.insert(table, null, value); if (id > 0) insertCount++; } database.setTransactionSuccessful(); } catch (Exception e) { e.printStackTrace(); } finally { if(database != null) { database.endTransaction(); } } return insertCount; } }