simpleprovider

A simple way to write ContentProviders and SQL Databases for Android Apps

Overview

Writing Content Providers in Android Applications can be really annoying. In most cases there is not much complexity in regards of the data. Still, you have to write a great deal of boilerplate code to get going. This library is intended to ease up the creation of very simple Content Providers using annotations.

  • Define your schema using Annotations.
  • Access your data using automatically generated URIs
  • Let simpleprovider handle schema upgrades.

Examples

Create your own Provider

Subclass AbstractProvider and override getAuthority().

public class BlogProvider extends AbstractProvider {

  protected String getAuthority() {
    return "com.example.blog.DATA";
  }

}

Define your Database schema via inner classes. Use @Table and @Column Annotations.

public class BlogProvider extends AbstractProvider {

  // ...

  @Table
  public class Post {

    @Column(Column.FieldType.INTEGER, primaryKey = true)
    public static final String KEY_ID = "_id";

    @Column(Column.FieldType.TEXT)
    public static final String KEY_TITLE = "title";

    @Column(Column.FieldType.TEXT)
    public static final String KEY_CONTENT = "content";

    @Column(Column.FieldType.TEXT)
    public static final String KEY_AUTHOR = "author";

  }

  @Table
  public class Comment {

    @Column(Column.FieldType.INTEGER, primaryKey = true)
    public static final String KEY_ID = "_id";

    @Column(Column.FieldType.INTEGER)
    public static final String KEY_POST_ID = "post_id";

    @Column(Column.FieldType.TEXT)
    public static final String KEY_CONTENT = "content";

    @Column(Column.FieldType.TEXT)
    public static final String KEY_AUTHOR = "author";

  }
}

Accessing your Provider

Access your data using a ContentResolver.

Uri uri = Uri.parse("content://com.example.blog.DATA/posts");
getContentResolver().query(uri, projection, selection, selectionArgs, order);

Or use a CursorLoader.

new CursorLoader(context, uri, projection, selection, selectionArgs, order);

AbstractProvider has automatically created the following URIs based on your table classes:

content://com.example.blog.DATA/posts
content://com.example.blog.DATA/posts/*
content://com.example.blog.DATA/comments
content://com.example.blog.DATA/comments/*

Schema Upgrades

Add fields to your schema classes using the since key.

  @Table
  public class Post {

    // ... (previously defined columns)

    @Column(Column.FieldType.INTEGER, since = 2)
    public static final String KEY_CREATION_DATE = "creation_date";

  }

And tell AbstractProvider that we upgraded the schema.

public class BlogProvider extends AbstractProvider {

    // ...

    @Override
    protected int getSchemaVersion() {
        return 2;
    }
}

Download

Latest JAR

The source code to the simpleprovider, and this website is available on GitHub.

Gradle

simpleprovider is available on Maven Central

compile 'com.github.triplet.simpleprovider:simpleprovider:(insert latest version)'

ProGuard

To prevent ProGuard obfuscating anything that define the schema the following keep options should be used.

# Keep the nested structure
-keepattributes InnerClasses

# Don't obfuscate the FieldType enum as those values are added
# to the CREATE TABLE statement directly
-keep enum de.triplet.simpleprovider.Column$FieldType { public *; }

# Do not obfuscate anything that is annotated with @Table or @Column
-keep @de.triplet.simpleprovider.Table class * { @de.triplet.simpleprovider.Column *; }

License

 The MIT License (MIT)

 Copyright (c) 2014 Christian Becker
 Copyright (c) 2014 Björn Hurling

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.