content: package com.ladwa.aditya.offlinefirstapp.data.local;
  
  import android.content.ContentProvider;
  import android.content.ContentValues;
  import android.content.UriMatcher;
  import android.database.Cursor;
  import android.database.SQLException;
  import android.database.sqlite.SQLiteDatabase;
  import android.net.Uri;
  import android.support.annotation.Nullable;
  
  /**
   *
   * Created by Aditya on 23-Oct-16.
   */
  
  public class Provider extends ContentProvider {
  
      private static final int POST_ITEM = 100;
      private static final int POST_DIR = 101;
  
      private static final UriMatcher sUriMatcher = buildUriMatcher();
      private DatabaseHelper mDbHelper;
  
  
      private static UriMatcher buildUriMatcher() {
          final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
          final String authority = DatabaseContract.CONTENT_AUTHORITY;
  
          matcher.addURI(authority, DatabaseContract.PATH_POST + "/#", POST_ITEM);
          matcher.addURI(authority, DatabaseContract.PATH_POST, POST_DIR);
  
          return matcher;
      }
  
      @Override
      public boolean onCreate() {
          mDbHelper = new DatabaseHelper(getContext());
          return true;
      }
  
      @Nullable
      @Override
      public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
          Cursor retCursor;
          switch (sUriMatcher.match(uri)) {
              case POST_ITEM:
                  retCursor = mDbHelper.getReadableDatabase().query(
                          DatabaseContract.Post.TABLE_NAME,
                          projection,
                          selection,
                          selectionArgs,
                          null,
                          null,
                          sortOrder
                  );
                  break;
              case POST_DIR:
                  retCursor = mDbHelper.getReadableDatabase().query(
                          DatabaseContract.Post.TABLE_NAME,
                          projection,
                          selection,
                          selectionArgs,
                          null,
                          null,
                          sortOrder
                  );
                  break;
              default:
                  throw new UnsupportedOperationException("Unknown Uri " + uri);
          }
          retCursor.setNotificationUri(getContext().getContentResolver(), uri);
          return retCursor;
      }
  
      @Nullable
      @Override
      public String getType(Uri uri) {
          final int match = sUriMatcher.match(uri);
          switch (match) {
              //Case for user
              case POST_ITEM:
                  return DatabaseContract.Post.CONTENT_USER_ITEM_TYPE;
              case POST_DIR:
                  return DatabaseContract.Post.CONTENT_USER_TYPE;
              default:
                  throw new UnsupportedOperationException("Unknown URI " + uri);
          }
      }
  
      @Nullable
      @Override
      public Uri insert(Uri uri, ContentValues contentValues) {
          final SQLiteDatabase db = mDbHelper.getWritableDatabase();
          Uri returnUri;
          switch (sUriMatcher.match(uri)) {
              //Case for Post
              case POST_DIR:
                  long _id = db.insert(DatabaseContract.Post.TABLE_NAME, null, contentValues);
                  if (_id > 0)
                      returnUri = DatabaseContract.Post.buildUserUri(_id);
                  else
                      throw new SQLException("Failed to insert row " + uri);
                  break;
              default:
                  throw new UnsupportedOperationException("Unknown URI " + uri);
          }
          getContext().getContentResolver().notifyChange(uri, null);
          return returnUri;
      }
  
      @Override
      public int delete(Uri uri, String selection, String[] selectionArgs) {
          final SQLiteDatabase db = mDbHelper.getWritableDatabase();
          int rowsDeleted;
          switch (sUriMatcher.match(uri)) {
              case POST_DIR:
                  rowsDeleted = db.delete(DatabaseContract.Post.TABLE_NAME, selection, selectionArgs);
                  break;
              default:
                  throw new UnsupportedOperationException("Unknown URI " + uri);
          }
          if (selection == null || 0 != rowsDeleted)
              getContext().getContentResolver().notifyChange(uri, null);
          return rowsDeleted;
      }
  
      @Override
      public int update(Uri uri,  ContentValues values, String selection, String[] selectionArgs) {
          final SQLiteDatabase db = mDbHelper.getWritableDatabase();
          int update;
          switch (sUriMatcher.match(uri)) {
              //Case for User
              case POST_DIR:
                  update = db.update(DatabaseContract.Post.TABLE_NAME, values, selection, selectionArgs);
                  break;
              default:
                  throw new UnsupportedOperationException("Unknown URI " + uri);
          }
          if (update > 0)
              getContext().getContentResolver().notifyChange(uri, null);
          return update;
      }
  }
  