Android Open Source - ContentProviderProcessor Abstract Content Provider Processor






From Project

Back to project page ContentProviderProcessor.

License

The source code is released under:

Apache License

If you think the Android project ContentProviderProcessor listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.wackernagel.android.contractcontentprovider;
// w w w  .j a v a 2s . c  o m
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;

public abstract class AbstractContentProviderProcessor implements ContentProviderProcessor {
  public static final String QUERY_PARAMETER_GROUP_BY = "parameter_group_by";
  public static final String QUERY_PARAMETER_HAVING = "parameter_having";

  @Override
  public Cursor query( SQLiteDatabase db, ContentResolver resolver, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder ) {
    final SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
    builder.setTables( getTable() );
    
    if( isItemType( uri ) ) {
      builder.appendWhere( Contract.COLUMN_ID + "=" + uri.getLastPathSegment() );
    }
    
    // if no query parameter set then null is returned which is the default value
    final String groupBy = uri.getQueryParameter( QUERY_PARAMETER_GROUP_BY );
    final String having = uri.getQueryParameter( QUERY_PARAMETER_HAVING );
    
    final Cursor cursor = builder.query( db, projection, selection, selectionArgs, groupBy, having, sortOrder );
    cursor.setNotificationUri( resolver, uri );
    return cursor;
  }

  @Override
  public Uri insert( SQLiteDatabase db, ContentResolver resolver, Uri uri, ContentValues values ) {
    long id = 0;
    String table = getTable();
    
    if( !isItemType( uri ) ) {
      id = db.insert( table, null, values );
    }
    
    resolver.notifyChange( uri, null );
    return ContentUris.withAppendedId( uri, id );
  }

  @Override
  public int update( SQLiteDatabase db, ContentResolver resolver, Uri uri, ContentValues values, String selection, String[] selectionArgs ) {
    int updatedRows = 0;
    String table = getTable();
    
    if( isItemType( uri ) ) {
      String id = uri.getLastPathSegment();
      if( TextUtils.isEmpty( selection ) ) {
        updatedRows = db.update( table, values, Contract.COLUMN_ID + "=" + id, null );
      } else {
        updatedRows = db.update( table, values, Contract.COLUMN_ID + "=" + id + " and " + selection, selectionArgs );
      }
    } else {
      updatedRows = db.update( table, values, selection, selectionArgs );
    }
    
    resolver.notifyChange( uri, null );
    return updatedRows;
  }

  @Override
  public int delete( SQLiteDatabase db, ContentResolver resolver, Uri uri, String selection, String[] selectionArgs ) {
    int deletedRows = 0;
    String table = getTable();
    
    if( isItemType( uri ) ) {
      String id = uri.getLastPathSegment();
      if( TextUtils.isEmpty( selection ) ) {
        deletedRows = db.delete( table, Contract.COLUMN_ID + "=" + id, null );
      } else {
        deletedRows = db.delete( table, Contract.COLUMN_ID + "=" + id + " and " + selection, selectionArgs );
      }
    } else {
      deletedRows = db.delete( table, selection, selectionArgs );
    }
    
    resolver.notifyChange( uri, null );
    return deletedRows;
  }
}




Java Source Code List

de.wackernagel.android.contractcontentprovider.AbstractContentProviderProcessor.java
de.wackernagel.android.contractcontentprovider.ContentProviderProcessor.java
de.wackernagel.android.contractcontentprovider.ContentProviderUtils.java
de.wackernagel.android.contractcontentprovider.ContractContentProvider.java
de.wackernagel.android.contractcontentprovider.ContractSQLiteOpenHelper.java
de.wackernagel.android.contractcontentprovider.Contract.java
de.wackernagel.android.contractcontentprovider.DefaultContentProviderProcessor.java
de.wackernagel.android.contractcontentprovider.GenericUriMatcher.java
de.wackernagel.android.contractcontentprovider.sample.MainActivity.java
de.wackernagel.android.contractcontentprovider.sample.provider.CustomerContract.java
de.wackernagel.android.contractcontentprovider.sample.provider.SampleProvider.java