Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ProviGenLib/src/com/tjeannin/provigen/OpenHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.tjeannin.provigen;

import com.tjeannin.provigen.database.Database;

/**
* this interface defines the methods used from the SQLite and SqlCipher implementations of SQLiteOpenHelper,
* {@link net.sqlcipher.database.SQLiteOpenHelper} and {@link android.database.sqlite.SQLiteOpenHelper}.
*/
public interface OpenHelper {
Database getWritableDb();
Database getReadableDb();
}
50 changes: 0 additions & 50 deletions ProviGenLib/src/com/tjeannin/provigen/ProviGenOpenHelper.java

This file was deleted.

21 changes: 12 additions & 9 deletions ProviGenLib/src/com/tjeannin/provigen/ProviGenProvider.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.tjeannin.provigen;

import android.content.*;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.text.TextUtils;
import com.tjeannin.provigen.database.Database;
import com.tjeannin.provigen.model.Contract;

import java.util.ArrayList;
Expand All @@ -21,7 +24,7 @@ public abstract class ProviGenProvider extends ContentProvider {
private UriMatcher uriMatcher;
private static final int ITEM = 1;
private static final int ITEM_ID = 2;
private SQLiteOpenHelper openHelper;
private OpenHelper openHelper;

/**
* This method should return an instance of a {@link android.database.sqlite.SQLiteOpenHelper}.
Expand All @@ -30,7 +33,7 @@ public abstract class ProviGenProvider extends ContentProvider {
* @param context A context to pass to the SQLiteOpenHelper instance while creating it.
* @return the SQLiteOpenHelper that the ProviGenProvider will use.
*/
public abstract SQLiteOpenHelper openHelper(Context context);
public abstract OpenHelper openHelper(Context context);

/**
* This method should return the list of contract classes that the ProviGenProvider will use.
Expand Down Expand Up @@ -59,7 +62,7 @@ public boolean onCreate() {

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase database = openHelper.getWritableDatabase();
Database database = openHelper.getWritableDb();

int numberOfRowsAffected = 0;
Contract contract = findMatchingContract(uri);
Expand Down Expand Up @@ -105,7 +108,7 @@ public String getType(Uri uri) {

@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase database = openHelper.getWritableDatabase();
Database database = openHelper.getWritableDb();

Contract contract = findMatchingContract(uri);
switch (uriMatcher.match(uri)) {
Expand All @@ -120,7 +123,7 @@ public Uri insert(Uri uri, ContentValues values) {

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase database = openHelper.getReadableDatabase();
Database database = openHelper.getReadableDb();

Contract contract = findMatchingContract(uri);
Cursor cursor = null;
Expand Down Expand Up @@ -151,7 +154,7 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase database = openHelper.getWritableDatabase();
Database database = openHelper.getWritableDb();

Contract contract = findMatchingContract(uri);
int numberOfRowsAffected = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.tjeannin.provigen;

import android.content.Context;
import com.tjeannin.provigen.database.Database;
import com.tjeannin.provigen.database.SQLCipherDatabase;
import com.tjeannin.provigen.helper.TableBuilder;
import com.tjeannin.provigen.helper.TableUpdater;

import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteDatabase.CursorFactory;
import net.sqlcipher.database.SQLiteOpenHelper;

/**
* A simple implementation of a {@link SQLiteOpenHelper} that:
* <ul>
* <li>Create a table for each supplied contract when the database is created for the first time.</li>
* <li>Add missing table columns when the database version is increased.</li>
* <li>Accepts the password used to decrypt the database file when it is first opened.</li>
* </ul>
*/
public class ProviGenSQLCipherOpenHelper extends SQLiteOpenHelper implements OpenHelper {

private static String sPassword;
private static boolean sIsPasswordInitialized;

private final Class[] contracts;

public static void setPassword(String value) {
sPassword = value;
sIsPasswordInitialized = true;
}

private static String getPassword() {
if ( ! sIsPasswordInitialized) {
throw new IllegalStateException("ProviGenSQLCipherOpenHelper.setPassword must be called before we get here. Consider calling it early in your Application.onCreate method.");
}

return sPassword;
}

/**
* Create a helper object to create, open, and/or manage a database.
* This method always returns very quickly. The database is not actually
* created or opened until one of {@link #getWritableDatabase} or
* {@link #getReadableDatabase} is called.
*
* @param context the context to use to open or create the database.
* @param databaseName the name of the database file, or null for an in-memory database.
* @param factory the factory to use for creating cursor objects, or null for the default.
* @param version the version of the database. Each time the version is increased, missing columns will be added.
* @param contractClasses the list of contract classes
*/
public ProviGenSQLCipherOpenHelper(Context context, String databaseName, CursorFactory factory, int version, Class[] contractClasses) {
super(context, databaseName, factory, version);
this.contracts = contractClasses;
}

@Override
public void onCreate(SQLiteDatabase database) {
Database db = new SQLCipherDatabase(database);
for (Class contract : contracts)
new TableBuilder(contract).createTable(db);
}

@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
if (newVersion > oldVersion) {
Database db = new SQLCipherDatabase(database);
for (Class contract : contracts)
TableUpdater.addMissingColumns(db, contract);
}
}

@Override
public Database getWritableDb() {
return new SQLCipherDatabase(getWritableDatabase(getPassword()));
}

@Override
public Database getReadableDb() {
return new SQLCipherDatabase(getReadableDatabase(getPassword()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.tjeannin.provigen;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.tjeannin.provigen.database.AndroidSQLiteDatabase;
import com.tjeannin.provigen.database.Database;
import com.tjeannin.provigen.helper.TableBuilder;
import com.tjeannin.provigen.helper.TableUpdater;

/**
* A simple implementation of a {@link SQLiteOpenHelper} that:
* <ul>
* <li>Create a table for each supplied contract when the database is created for the first time.</li>
* <li>Add missing table columns when the database version is increased.</li>
* </ul>
*/
public class ProviGenSQLiteOpenHelper extends SQLiteOpenHelper implements OpenHelper {

private final Class[] contracts;

/**
* Create a helper object to create, open, and/or manage a database.
* This method always returns very quickly. The database is not actually
* created or opened until one of {@link #getWritableDatabase} or
* {@link #getReadableDatabase} is called.
*
* @param context the context to use to open or create the database.
* @param databaseName the name of the database file, or null for an in-memory database.
* @param factory the factory to use for creating cursor objects, or null for the default.
* @param version the version of the database. Each time the version is increased, missing columns will be added.
* @param contractClasses the list of contract classes
*/
public ProviGenSQLiteOpenHelper(Context context, String databaseName, SQLiteDatabase.CursorFactory factory, int version, Class[] contractClasses) {
super(context, databaseName, factory, version);
this.contracts = contractClasses;
}

@Override
public void onCreate(SQLiteDatabase database) {
for (Class contract : contracts)
new TableBuilder(contract).createTable(new AndroidSQLiteDatabase(database));
}

@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
if (newVersion > oldVersion) {
for (Class contract : contracts)
TableUpdater.addMissingColumns(new AndroidSQLiteDatabase(database), contract);
}
}

@Override
public Database getWritableDb() {
return new AndroidSQLiteDatabase(getWritableDatabase());
}

@Override
public Database getReadableDb() {
return new AndroidSQLiteDatabase(getReadableDatabase());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.tjeannin.provigen.database;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

/**
* the {@link SQLiteDatabase} implementation of the {@link Database} interface. it delegates
* to a concrete {@link SQLiteDatabase}.
*/
public class AndroidSQLiteDatabase implements Database {
private final SQLiteDatabase delegate;

public AndroidSQLiteDatabase(SQLiteDatabase delegate) {
this.delegate = delegate;
}

@Override
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
return delegate.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
}

@Override
public Cursor rawQuery(String sql, String[] selectionArgs) {
return delegate.rawQuery(sql, selectionArgs);
}

@Override
public long insert(String table, String nullColumnHack, ContentValues values) {
return delegate.insert(table, nullColumnHack, values);
}

@Override
public int delete(String table, String whereClause, String[] whereArgs) {
return delegate.delete(table, whereClause, whereArgs);
}

@Override
public int update(String table, ContentValues values, String whereClause, String[] whereArgs) {
return delegate.update(table, values, whereClause, whereArgs);
}

@Override
public void execSQL(String sql) throws SQLException {
delegate.execSQL(sql);
}
}
23 changes: 23 additions & 0 deletions ProviGenLib/src/com/tjeannin/provigen/database/Database.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.tjeannin.provigen.database;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;

/**
* this interface defines the methods used from {@link net.sqlcipher.database.SQLiteDatabase} and
* {@link android.database.sqlite.SQLiteDatabase}
*/
public interface Database {
Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy);

Cursor rawQuery(String sql, String[] selectionArgs);

long insert(String table, String nullColumnHack, ContentValues values);

int delete(String table, String whereClause, String[] whereArgs);

int update(String table, ContentValues values, String whereClause, String[] whereArgs);

void execSQL(String sql) throws SQLException;
}
Loading