Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap EncryptedHelper in interface to reduce Art warnings #924

Merged
merged 1 commit into from
Feb 18, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import org.greenrobot.greendao.DaoException;

import java.lang.reflect.Constructor;

/**
* SQLiteOpenHelper to allow working with greenDAO's {@link Database} abstraction to create and update database schemas.
*/
Expand Down Expand Up @@ -126,9 +130,36 @@ public void onOpen(Database db) {
// Do nothing by default
}

interface EncryptedHelper {
Database getEncryptedReadableDb(String password);
Database getEncryptedReadableDb(char[] password);
Database getEncryptedWritableDb(String password);
Database getEncryptedWritableDb(char[] password);
}

private EncryptedHelper checkEncryptedHelper() {
if (encryptedHelper == null) {
encryptedHelper = new EncryptedHelper(context, name, version, loadSQLCipherNativeLibs);
try {
Class.forName("net.sqlcipher.database.SQLiteOpenHelper");
} catch (ClassNotFoundException e) {
throw new DaoException("Using an encrypted database requires SQLCipher, " +
"make sure to add it to dependencies: " +
"https://greenrobot.org/greendao/documentation/database-encryption/");
}

// Avoid referencing SqlCipherEncryptedHelper to avoid
// "Rejecting re-init on previously-failed class java.lang.NoClassDefFoundError" logs
// if SQLCipher is not in classpath.
try {
Class<?> helperClass = Class.forName(
"org.greenrobot.greendao.database.SqlCipherEncryptedHelper");
Constructor<?> constructor = helperClass.getConstructor(
DatabaseOpenHelper.class, Context.class, String.class, int.class, boolean.class);
encryptedHelper = (EncryptedHelper) constructor.newInstance(
this, context, name, version, loadSQLCipherNativeLibs);
} catch (Exception e) {
throw new DaoException(e);
}
}
return encryptedHelper;
}
Expand All @@ -141,7 +172,7 @@ private EncryptedHelper checkEncryptedHelper() {
*/
public Database getEncryptedWritableDb(String password) {
EncryptedHelper encryptedHelper = checkEncryptedHelper();
return encryptedHelper.wrap(encryptedHelper.getWritableDatabase(password));
return encryptedHelper.getEncryptedWritableDb(password);
}

/**
Expand All @@ -152,7 +183,7 @@ public Database getEncryptedWritableDb(String password) {
*/
public Database getEncryptedWritableDb(char[] password) {
EncryptedHelper encryptedHelper = checkEncryptedHelper();
return encryptedHelper.wrap(encryptedHelper.getWritableDatabase(password));
return encryptedHelper.getEncryptedWritableDb(password);
}

/**
Expand All @@ -163,7 +194,7 @@ public Database getEncryptedWritableDb(char[] password) {
*/
public Database getEncryptedReadableDb(String password) {
EncryptedHelper encryptedHelper = checkEncryptedHelper();
return encryptedHelper.wrap(encryptedHelper.getReadableDatabase(password));
return encryptedHelper.getEncryptedReadableDb(password);
}

/**
Expand All @@ -174,35 +205,7 @@ public Database getEncryptedReadableDb(String password) {
*/
public Database getEncryptedReadableDb(char[] password) {
EncryptedHelper encryptedHelper = checkEncryptedHelper();
return encryptedHelper.wrap(encryptedHelper.getReadableDatabase(password));
return encryptedHelper.getEncryptedReadableDb(password);
}

private class EncryptedHelper extends net.sqlcipher.database.SQLiteOpenHelper {
public EncryptedHelper(Context context, String name, int version, boolean loadLibs) {
super(context, name, null, version);
if (loadLibs) {
net.sqlcipher.database.SQLiteDatabase.loadLibs(context);
}
}

@Override
public void onCreate(net.sqlcipher.database.SQLiteDatabase db) {
DatabaseOpenHelper.this.onCreate(wrap(db));
}

@Override
public void onUpgrade(net.sqlcipher.database.SQLiteDatabase db, int oldVersion, int newVersion) {
DatabaseOpenHelper.this.onUpgrade(wrap(db), oldVersion, newVersion);
}

@Override
public void onOpen(net.sqlcipher.database.SQLiteDatabase db) {
DatabaseOpenHelper.this.onOpen(wrap(db));
}

protected Database wrap(net.sqlcipher.database.SQLiteDatabase sqLiteDatabase) {
return new EncryptedDatabase(sqLiteDatabase);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.greenrobot.greendao.database;

import android.content.Context;

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

class SqlCipherEncryptedHelper extends SQLiteOpenHelper implements DatabaseOpenHelper.EncryptedHelper {

private final DatabaseOpenHelper delegate;

public SqlCipherEncryptedHelper(DatabaseOpenHelper delegate, Context context, String name, int version, boolean loadLibs) {
super(context, name, null, version);
this.delegate = delegate;
if (loadLibs) {
SQLiteDatabase.loadLibs(context);
}
}

private Database wrap(SQLiteDatabase sqLiteDatabase) {
return new EncryptedDatabase(sqLiteDatabase);
}

@Override
public void onCreate(SQLiteDatabase db) {
delegate.onCreate(wrap(db));
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
delegate.onUpgrade(wrap(db), oldVersion, newVersion);
}

@Override
public void onOpen(SQLiteDatabase db) {
delegate.onOpen(wrap(db));
}

@Override
public Database getEncryptedReadableDb(String password) {
return wrap(getReadableDatabase(password));
}

@Override
public Database getEncryptedReadableDb(char[] password) {
return wrap(getReadableDatabase(password));
}

@Override
public Database getEncryptedWritableDb(String password) {
return wrap(getWritableDatabase(password));
}

@Override
public Database getEncryptedWritableDb(char[] password) {
return wrap(getWritableDatabase(password));
}

}