Easily make a ContentProvider from an annotated ContractClass.
Java
Failed to load latest commit information.
ProviGenLib Updated gradle version and android gradle plugin version. Dec 19, 2014
ProviGenSample Basic gradle build Jul 31, 2014
ProviGenTests Removed useless files. Sep 4, 2014
gradle/wrapper Updated gradle version and android gradle plugin version. Dec 19, 2014
.gitignore Updated poms - The lib is a jar and not an apklib Feb 3, 2014
.travis.yml
LICENSE.txt Added license informations. Apr 23, 2014
README.md Update README.md Sep 4, 2014
build.gradle Updated gradle version and android gradle plugin version. Dec 19, 2014
gradlew Basic gradle build Jul 31, 2014
gradlew.bat Basic gradle build Jul 31, 2014
settings.gradle Added basic gradle support for the test project. Sep 1, 2014

README.md

ProviGen Build Status Maven Central

Easily make a ContentProvider from a ContractClass.

Setup

public interface MyContract extends ProviGenBaseContract {

    @Column(Type.INTEGER)
    public static final String MY_INT_COLUMN = "int";

    @Column(Type.TEXT)
    public static final String MY_STRING_COLUMN = "string";

    @ContentUri
    public static final Uri CONTENT_URI = Uri.parse("content://com.myapp/table_name");
}
  • Extend the ProviGenProvider.
public class MyContentProvider extends ProviGenProvider {

    private static Class[] contracts = new Class[]{MyContract.class};

    @Override
    public SQLiteOpenHelper openHelper(Context context) {
        return new ProviGenOpenHelper(getContext(), "dbName", null, 1, contracts);
    }

    @Override
    public Class[] contractClasses() {
        return contracts;
    }
}
  • Add your provider in your manifest.
<provider
    android:name="com.myapp.MyContentProvider"
    android:authorities="com.myapp" >
</provider>
  • You're done.

Usage

You can make the usual insert, update, delete and query using a ContentResolver.
For example querying a single row boils down to:

getContentResolver().query( 
    Uri.withAppendedPath(MyContract.CONTENT_URI, myId),
    null, "", null, "");

or

getContentResolver().query(
    MyContract.CONTENT_URI, null, 
    MyContract._ID + " = ? ", new String[]{ myId }, "");

Features

Table creation and contract upgrades

ProviGen comes with an implementation of the SQLiteOpenHelper called ProviGenOpenHelper. This default implementation will

  • automatically create the needed tables on the first application launch
  • automatically add missing columns every time the database version increases

Notifications and observers

ProviGen fully supports the uri notification mechanism.
You can safely use it with CursorLoaders and ContentObservers.

Custom SQLiteOpenHelper

You can provide your own implementation of the SQLiteOpenHelper for initial population, complex contract upgrades or anything else database related you want to achieve.

public class MyContentProvider extends ProviGenProvider {

    @Override
    public Class[] contractClasses() {
        return new Class[]{MyContract.class};
    }

    @Override
    public SQLiteOpenHelper openHelper(Context context) {

        return new SQLiteOpenHelper(getContext(), "databaseName", null, 1) {

            @Override
            public void onCreate(SQLiteDatabase database) {
                // Automatically creates table and needed columns.
                new TableBuilder(MyContract.class).createTable(database);

                // Do initial population here.
            }

            @Override
            public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
                // Automatically adds new columns.
                TableUpdater.addMissingColumns(database, MyContract.class);

                // Anything else related to database upgrade should be done here.
            }
        };
    }
}

Data constraint

You can apply a UNIQUE or a NOT_NULL constraint to a column using the appropriate TableBuilder methods.

new TableBuilder(MyContract.class)
        .addConstraint(MyContract.MY_INT, Constraint.UNIQUE, OnConflict.ABORT)
        .addConstraint(MyContract.MY_STRING, Constraint.NOT_NULL, OnConflict.IGNORE)
        .createTable(database);

License

This content is released under the MIT License.