Android Open Source - ContentProviderProcessor Contract Content Provider






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 va 2  s  .c  o m*/
import java.util.Arrays;
import java.util.List;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

public abstract class ContractContentProvider extends ContentProvider {
  
  private final String authority;  
  private final String databaseName;
  private final int databaseVersion;
  private final List<Contract> contracts;
  private final GenericUriMatcher<ContentProviderProcessor> uriMatcher;
  
  private SQLiteOpenHelper sqliteHelper;
    
  public ContractContentProvider( String authority, String databaseName, int databaseVersion, Contract... contracts ) {
    this.authority = authority;
    this.databaseName = databaseName;
    this.databaseVersion = databaseVersion;
    this.contracts = Arrays.asList( contracts );
    this.uriMatcher = new GenericUriMatcher<ContentProviderProcessor>( null );
    
    // register fo each Contract a default ContentProviderProcessor
    for( Contract contract : contracts ) {
      addContractWithProcessor( contract, new DefaultContentProviderProcessor( authority, contract.getTable() ) );
    }
  }
  
  protected void addContractWithProcessor( Contract contract, ContentProviderProcessor processor ) {
    // ContentProvider support directory operations
    uriMatcher.addURI( authority, contract.getTable(), processor );
    
    // and item operations
    uriMatcher.addURI( authority, contract.getTable() + "/#", processor );
  }
  
  @Override
  public boolean onCreate() {
    // Context is now available
    sqliteHelper = onCreateSQLiteHelper( getContext(), databaseName, databaseVersion, contracts );
    
    // ContentProvider can successfully started
    return (sqliteHelper != null);
  }
  
  protected SQLiteOpenHelper onCreateSQLiteHelper( Context context, String databaseName, int databaseVersion, List<Contract> contracts ) {
    return new ContractSQLiteOpenHelper( context, databaseName, databaseVersion, contracts );
  }

  @Override
  public String getType( Uri uri ) {
    final ContentProviderProcessor processor = uriMatcher.match( uri );
    if( processor == null ) {
      return null;
    }
    
    if( processor.isItemType( uri ) ) {
      return new StringBuilder().append( ContentResolver.CURSOR_ITEM_BASE_TYPE ).append( "/vnd." ).append( authority ).append( "." ).append( processor.getTable() ).toString();
    } else if( processor.isDirectoryType( uri )) {
      return new StringBuilder().append( ContentResolver.CURSOR_DIR_BASE_TYPE ).append( "/vnd." ).append( authority ).append( "." ).append( processor.getTable() ).toString();
    } else {
      return null;
    }
  }

  @Override
  public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder ) {
    final ContentProviderProcessor processor = uriMatcher.match( uri );
    if( processor == null ) {
      throw new IllegalArgumentException( "Found no ContentProviderProcessor for URI '" + uri + "'." );
    }
    return processor.query( sqliteHelper.getWritableDatabase(), getContext().getContentResolver(), uri, projection, selection, selectionArgs, sortOrder );
  }

  @Override
  public Uri insert( Uri uri, ContentValues values ) {
    final ContentProviderProcessor processor = uriMatcher.match( uri );
    if( processor == null ) {
      throw new IllegalArgumentException( "Found no ContentProviderProcessor for URI '" + uri + "'." );
    }
    return processor.insert( sqliteHelper.getWritableDatabase(), getContext().getContentResolver(), uri, values );
  }

  @Override
  public int update( Uri uri, ContentValues values, String selection, String[] selectionArgs ) {
    final ContentProviderProcessor processor = uriMatcher.match( uri );
    if( processor == null ) {
      throw new IllegalArgumentException( "Found no ContentProviderProcessor for URI '" + uri + "'." );
    }
    return processor.update( sqliteHelper.getWritableDatabase(), getContext().getContentResolver(), uri, values, selection, selectionArgs );
  }

  @Override
  public int delete( Uri uri, String selection, String[] selectionArgs ) {
    final ContentProviderProcessor processor = uriMatcher.match( uri );
    if( processor == null ) {
      throw new IllegalArgumentException( "Found no ContentProviderProcessor for URI '" + uri + "'." );
    }
    return processor.delete( sqliteHelper.getWritableDatabase(), getContext().getContentResolver(), uri, selection, selectionArgs );
  }
}




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