forked from LJYcoder/DevRing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
255 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
237 changes: 237 additions & 0 deletions
237
devring/src/main/java/com/ljy/devring/db/support/DatabaseOpenHelperFixed.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
/* | ||
* Copyright (C) 2011-2016 Markus Junginger, greenrobot (http://greenrobot.org) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.ljy.devring.db.support; | ||
|
||
import android.content.Context; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.database.sqlite.SQLiteDatabase.CursorFactory; | ||
import android.database.sqlite.SQLiteOpenHelper; | ||
|
||
import org.greenrobot.greendao.database.Database; | ||
import org.greenrobot.greendao.database.EncryptedDatabase; | ||
import org.greenrobot.greendao.database.StandardDatabase; | ||
|
||
/** | ||
* SQLiteOpenHelper to allow working with greenDAO's {@link Database} abstraction to create and update database schemas. | ||
* | ||
* 修复在部分高版本系统手机上运行时报错崩溃 | ||
* Rejecting re-init on previously-failed class java.lang.Class<org.greenrobot.greendao.database.DatabaseOpenHelper$EncryptedHelper>: | ||
* java.lang.NoClassDefFoundError: Failed resolution of: Lnet/sqlcipher/database/SQLiteOpenHelper; | ||
* 具体原因及解决方案请查看issue <a>https://github.com/greenrobot/greenDAO/issues/428</a> | ||
*/ | ||
public abstract class DatabaseOpenHelperFixed extends SQLiteOpenHelper { | ||
|
||
private final Context context; | ||
private final String name; | ||
private final int version; | ||
|
||
private EncryptedHelper encryptedHelper; | ||
private boolean loadSQLCipherNativeLibs = true; | ||
|
||
public DatabaseOpenHelperFixed(Context context, String name, int version) { | ||
this(context, name, null, version); | ||
} | ||
|
||
public DatabaseOpenHelperFixed(Context context, String name, CursorFactory factory, int version) { | ||
super(context, name, factory, version); | ||
this.context = context; | ||
this.name = name; | ||
this.version = version; | ||
} | ||
|
||
/** | ||
* Flag to load SQLCipher native libs (default: true). | ||
*/ | ||
public void setLoadSQLCipherNativeLibs(boolean loadSQLCipherNativeLibs) { | ||
this.loadSQLCipherNativeLibs = loadSQLCipherNativeLibs; | ||
} | ||
|
||
/** | ||
* Like {@link #getWritableDatabase()}, but returns a greenDAO abstraction of the database. | ||
* The backing DB is an standard {@link SQLiteDatabase}. | ||
*/ | ||
public Database getWritableDb() { | ||
return wrap(getWritableDatabase()); | ||
} | ||
|
||
/** | ||
* Like {@link #getReadableDatabase()}, but returns a greenDAO abstraction of the database. | ||
* The backing DB is an standard {@link SQLiteDatabase}. | ||
*/ | ||
public Database getReadableDb() { | ||
return wrap(getReadableDatabase()); | ||
} | ||
|
||
protected Database wrap(SQLiteDatabase sqLiteDatabase) { | ||
return new StandardDatabase(sqLiteDatabase); | ||
} | ||
|
||
/** | ||
* Delegates to {@link #onCreate(Database)}, which uses greenDAO's database abstraction. | ||
*/ | ||
@Override | ||
public void onCreate(SQLiteDatabase db) { | ||
onCreate(wrap(db)); | ||
} | ||
|
||
/** | ||
* Override this if you do not want to depend on {@link SQLiteDatabase}. | ||
*/ | ||
public void onCreate(Database db) { | ||
// Do nothing by default | ||
} | ||
|
||
/** | ||
* Delegates to {@link #onUpgrade(Database, int, int)}, which uses greenDAO's database abstraction. | ||
*/ | ||
@Override | ||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
onUpgrade(wrap(db), oldVersion, newVersion); | ||
} | ||
|
||
/** | ||
* Override this if you do not want to depend on {@link SQLiteDatabase}. | ||
*/ | ||
public void onUpgrade(Database db, int oldVersion, int newVersion) { | ||
// Do nothing by default | ||
} | ||
|
||
/** | ||
* Delegates to {@link #onOpen(Database)}, which uses greenDAO's database abstraction. | ||
*/ | ||
@Override | ||
public void onOpen(SQLiteDatabase db) { | ||
onOpen(wrap(db)); | ||
} | ||
|
||
/** | ||
* Override this if you do not want to depend on {@link SQLiteDatabase}. | ||
*/ | ||
public void onOpen(Database db) { | ||
// Do nothing by default | ||
} | ||
|
||
private EncryptedHelper checkEncryptedHelper() { | ||
if (encryptedHelper == null) { | ||
encryptedHelper = new EncryptedHelperImpl(context, name, version, loadSQLCipherNativeLibs, this); | ||
} | ||
return encryptedHelper; | ||
} | ||
|
||
/** | ||
* Use this to initialize an encrypted SQLCipher database. | ||
* | ||
* @see #onCreate(Database) | ||
* @see #onUpgrade(Database, int, int) | ||
*/ | ||
public Database getEncryptedWritableDb(String password) { | ||
EncryptedHelper encryptedHelper = checkEncryptedHelper(); | ||
return encryptedHelper.getEncryptedWritableDb(password); | ||
} | ||
|
||
/** | ||
* Use this to initialize an encrypted SQLCipher database. | ||
* | ||
* @see #onCreate(Database) | ||
* @see #onUpgrade(Database, int, int) | ||
*/ | ||
public Database getEncryptedWritableDb(char[] password) { | ||
EncryptedHelper encryptedHelper = checkEncryptedHelper(); | ||
return encryptedHelper.getEncryptedWritableDb(password); | ||
} | ||
|
||
/** | ||
* Use this to initialize an encrypted SQLCipher database. | ||
* | ||
* @see #onCreate(Database) | ||
* @see #onUpgrade(Database, int, int) | ||
*/ | ||
public Database getEncryptedReadableDb(String password) { | ||
EncryptedHelper encryptedHelper = checkEncryptedHelper(); | ||
return encryptedHelper.getEncryptedReadableDb(password); | ||
} | ||
|
||
/** | ||
* Use this to initialize an encrypted SQLCipher database. | ||
* | ||
* @see #onCreate(Database) | ||
* @see #onUpgrade(Database, int, int) | ||
*/ | ||
public Database getEncryptedReadableDb(char[] password) { | ||
EncryptedHelper encryptedHelper = checkEncryptedHelper(); | ||
return encryptedHelper.getEncryptedReadableDb(password); | ||
} | ||
|
||
interface EncryptedHelper { | ||
Database getEncryptedReadableDb(String password); | ||
Database getEncryptedReadableDb(char[] password); | ||
Database getEncryptedWritableDb(String password); | ||
Database getEncryptedWritableDb(char[] password); | ||
} | ||
|
||
static final class EncryptedHelperImpl extends net.sqlcipher.database.SQLiteOpenHelper implements DatabaseOpenHelperFixed.EncryptedHelper { | ||
private final DatabaseOpenHelperFixed delegate; | ||
|
||
EncryptedHelperImpl(Context context, String name, int version, boolean loadLibs, DatabaseOpenHelperFixed delegate) { | ||
super(context, name, null, version); | ||
this.delegate = delegate; | ||
if (loadLibs) { | ||
net.sqlcipher.database.SQLiteDatabase.loadLibs(context); | ||
} | ||
} | ||
|
||
@Override | ||
public void onCreate(net.sqlcipher.database.SQLiteDatabase db) { | ||
delegate.onCreate(wrap(db)); | ||
} | ||
|
||
@Override | ||
public void onUpgrade(net.sqlcipher.database.SQLiteDatabase db, int oldVersion, int newVersion) { | ||
delegate.onUpgrade(wrap(db), oldVersion, newVersion); | ||
} | ||
|
||
@Override | ||
public void onOpen(net.sqlcipher.database.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)); | ||
} | ||
|
||
private Database wrap(net.sqlcipher.database.SQLiteDatabase sqLiteDatabase) { | ||
return new EncryptedDatabase(sqLiteDatabase); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters