@Override
  public Cursor query(
      Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    String table = null;
    final int code = uriMatcher.match(uri);

    if (code == CONTACT || code == CONTACT_ID) {
      table = ContactTable.TABLE_NAME;
      if (sortOrder == null) {
        sortOrder = ContactTable.DEFAULT_SORT_ORDER;
      }

      if (code == CONTACT_ID) {
        selection = ContactTable._ID + "=?";
        selectionArgs = new String[] {uri.getLastPathSegment()};
      }
    } else if (code == CONTACT_REQUEST || code == CONTACT_REQUEST_ID) {
      table = ContactRequestTable.TABLE_NAME;
      if (sortOrder == null) {
        sortOrder = ContactRequestTable.DEFAULT_SORT_ORDER;
      }

      if (code == CONTACT_REQUEST_ID) {
        selection = ContactRequestTable._ID + "=?";
        selectionArgs = new String[] {uri.getLastPathSegment()};
      }
    } else if (code == CHAT_MESSAGE || code == CHAT_MESSAGE_ID) {
      table = ChatMessageTable.TABLE_NAME;
      if (sortOrder == null) {
        sortOrder = ChatMessageTable.DEFAULT_SORT_ORDER;
      }

      if (code == CHAT_MESSAGE_ID) {
        selection = ChatMessageTable._ID + "=?";
        selectionArgs = new String[] {uri.getLastPathSegment()};
      }
    } else if (code == CONVERSATION || code == CONVERSATION_ID) {
      table = ConversationTable.TABLE_NAME;
      if (sortOrder == null) {
        sortOrder = ConversationTable.DEFAULT_SORT_ORDER;
      }

      if (code == CONVERSATION_ID) {
        selection = ConversationTable._ID + "=?";
        selectionArgs = new String[] {uri.getLastPathSegment()};
      }
    } else {
      throw new IllegalArgumentException("Unknown URI " + uri);
    }

    SQLiteDatabase db = dbHelper.getReadableDatabase();
    Cursor c = db.query(table, projection, selection, selectionArgs, null, null, sortOrder);
    c.setNotificationUri(getContext().getContentResolver(), uri);

    return c;
  }
  @Override
  public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
    String table = null;

    switch (uriMatcher.match(uri)) {
      case CONTACT:
        table = ContactTable.TABLE_NAME;
        break;

      case CONTACT_REQUEST:
        table = ContactRequestTable.TABLE_NAME;
        break;

      case CONTACT_REQUEST_ID:
        table = ContactRequestTable.TABLE_NAME;
        where =
            DatabaseUtils.concatenateWhere(
                ContactRequestTable._ID + " = " + ContentUris.parseId(uri), where);
        break;

      case CHAT_MESSAGE_ID:
        table = ChatMessageTable.TABLE_NAME;
        where =
            DatabaseUtils.concatenateWhere(
                ChatMessageTable._ID + " = " + ContentUris.parseId(uri), where);
        break;

      case CONVERSATION:
        table = ConversationTable.TABLE_NAME;
        break;

      case CONVERSATION_ID:
        table = ConversationTable.TABLE_NAME;
        where =
            DatabaseUtils.concatenateWhere(
                ConversationTable._ID + " = " + ContentUris.parseId(uri), where);
        break;

      default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    SQLiteDatabase db = dbHelper.getWritableDatabase();
    int count = db.update(table, values, where, whereArgs);
    getContext().getContentResolver().notifyChange(uri, null);

    return count;
  }
  @Override
  public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) {
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    db.beginTransaction();
    try {
      ContentProviderResult[] results = super.applyBatch(operations);
      db.setTransactionSuccessful();

      return results;
    } catch (OperationApplicationException e) {
      e.printStackTrace();
      return null;
    } finally {
      db.endTransaction();
    }
  }
  @Override
  public Uri insert(Uri uri, ContentValues values) {
    Uri contentUri = null;
    String table = null;
    SQLiteDatabase db = dbHelper.getWritableDatabase();

    switch (uriMatcher.match(uri)) {
      case CONTACT:
        table = ContactTable.TABLE_NAME;
        contentUri = ContactTable.CONTENT_URI;
        break;

      case CONTACT_REQUEST:
        table = ContactRequestTable.TABLE_NAME;
        contentUri = ContactRequestTable.CONTENT_URI;
        break;

      case CHAT_MESSAGE:
        table = ChatMessageTable.TABLE_NAME;
        contentUri = ChatMessageTable.CONTENT_URI;
        break;

      case CONVERSATION:
        table = ConversationTable.TABLE_NAME;
        contentUri = ConversationTable.CONTENT_URI;
        break;

      default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    long rowId = db.insert(table, null, values);
    if (rowId > 0) {
      Uri noteUri = ContentUris.withAppendedId(contentUri, rowId);
      getContext().getContentResolver().notifyChange(noteUri, null);
      return noteUri;
    }

    throw new SQLException("Failed to insert row into " + uri);
  }
  @Override
  public boolean onCreate() {
    dbHelper = ChatDbHelper.getInstance(getContext());

    return true;
  }