20
20
import net .sqlcipher .DatabaseUtils ;
21
21
import net .sqlcipher .SQLException ;
22
22
import net .sqlcipher .database .SQLiteDebug .DbStats ;
23
+ import net .sqlcipher .database .SQLiteDatabaseHook ;
23
24
24
25
import java .io .File ;
25
26
import java .io .FileOutputStream ;
@@ -71,11 +72,6 @@ public class SQLiteDatabase extends SQLiteClosable {
71
72
private static final String TAG = "Database" ;
72
73
private static final int EVENT_DB_OPERATION = 52000 ;
73
74
private static final int EVENT_DB_CORRUPT = 75004 ;
74
- private static boolean useHMACPageProtection = true ;
75
-
76
- public static void setUseHMACPageProtection (boolean value ){
77
- SQLiteDatabase .useHMACPageProtection = value ;
78
- }
79
75
80
76
private static void loadICUData (Context context ) {
81
77
@@ -862,11 +858,11 @@ public Cursor newCursor(SQLiteDatabase db,
862
858
* @return the newly opened database
863
859
* @throws SQLiteException if the database cannot be opened
864
860
*/
865
- public static SQLiteDatabase openDatabase (String path , String password , CursorFactory factory , int flags ) {
861
+ public static SQLiteDatabase openDatabase (String path , String password , CursorFactory factory , int flags , SQLiteDatabaseHook databaseHook ) {
866
862
SQLiteDatabase sqliteDatabase = null ;
867
863
try {
868
864
// Open the database.
869
- sqliteDatabase = new SQLiteDatabase (path , password , factory , flags );
865
+ sqliteDatabase = new SQLiteDatabase (path , password , factory , flags , databaseHook );
870
866
if (SQLiteDebug .DEBUG_SQL_STATEMENTS ) {
871
867
sqliteDatabase .enableSqlTracing (path );
872
868
}
@@ -882,25 +878,38 @@ public static SQLiteDatabase openDatabase(String path, String password, CursorFa
882
878
// delete is only for non-memory database files
883
879
new File (path ).delete ();
884
880
}
885
- sqliteDatabase = new SQLiteDatabase (path , password , factory , flags );
881
+ sqliteDatabase = new SQLiteDatabase (path , password , factory , flags , databaseHook );
886
882
}
887
883
ActiveDatabases .getInstance ().mActiveDatabases .add (
888
884
new WeakReference <SQLiteDatabase >(sqliteDatabase ));
889
885
return sqliteDatabase ;
890
886
}
887
+
888
+ public static SQLiteDatabase openOrCreateDatabase (File file , String password , CursorFactory factory , SQLiteDatabaseHook databaseHook ){
889
+ return openOrCreateDatabase (file .getPath (), password , factory , databaseHook );
890
+ }
891
891
892
+ public static SQLiteDatabase openOrCreateDatabase (String path , String password , CursorFactory factory , SQLiteDatabaseHook databaseHook ) {
893
+ return openDatabase (path , password , factory , CREATE_IF_NECESSARY , databaseHook );
894
+ }
895
+
892
896
/**
893
897
* Equivalent to openDatabase(file.getPath(), factory, CREATE_IF_NECESSARY).
894
898
*/
895
899
public static SQLiteDatabase openOrCreateDatabase (File file , String password , CursorFactory factory ) {
896
- return openOrCreateDatabase (file .getPath (), password , factory );
900
+ return openOrCreateDatabase (file .getPath (), password , factory , null );
897
901
}
898
902
899
903
/**
900
904
* Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY).
901
905
*/
906
+
902
907
public static SQLiteDatabase openOrCreateDatabase (String path , String password , CursorFactory factory ) {
903
- return openDatabase (path , password , factory , CREATE_IF_NECESSARY );
908
+ return openDatabase (path , password , factory , CREATE_IF_NECESSARY , null );
909
+ }
910
+
911
+ public static SQLiteDatabase openDatabase (String path , String password , CursorFactory factory , int flags ) {
912
+ return openDatabase (path , password , factory , CREATE_IF_NECESSARY , null );
904
913
}
905
914
906
915
/**
@@ -1881,6 +1890,10 @@ protected void finalize() {
1881
1890
}
1882
1891
}
1883
1892
1893
+ public SQLiteDatabase (String path , String password , CursorFactory factory , int flags ) {
1894
+ this (path , password , factory , flags , null );
1895
+ }
1896
+
1884
1897
/**
1885
1898
* Private constructor. See {@link #create} and {@link #openDatabase}.
1886
1899
*
@@ -1889,8 +1902,7 @@ protected void finalize() {
1889
1902
* @param flags 0 or {@link #NO_LOCALIZED_COLLATORS}. If the database file already
1890
1903
* exists, mFlags will be updated appropriately.
1891
1904
*/
1892
- public SQLiteDatabase (String path , String password , CursorFactory factory , int flags ) {
1893
-
1905
+ public SQLiteDatabase (String path , String password , CursorFactory factory , int flags , SQLiteDatabaseHook databaseHook ) {
1894
1906
1895
1907
if (path == null ) {
1896
1908
throw new IllegalArgumentException ("path should not be null" );
@@ -1901,15 +1913,17 @@ public SQLiteDatabase(String path, String password, CursorFactory factory, int f
1901
1913
mStackTrace = new DatabaseObjectNotClosedException ().fillInStackTrace ();
1902
1914
mFactory = factory ;
1903
1915
dbopen (mPath , mFlags );
1904
-
1905
- if (SQLiteDatabase .useHMACPageProtection ){
1906
- execSQL ("PRAGMA cipher_default_use_hmac = ON" );
1907
- } else {
1908
- execSQL ("PRAGMA cipher_default_use_hmac = OFF" );
1916
+
1917
+ if (databaseHook != null ){
1918
+ databaseHook .preKey (this );
1909
1919
}
1910
1920
1911
1921
execSQL ("PRAGMA key = '" + password + "'" );
1912
1922
1923
+ if (databaseHook != null ){
1924
+ databaseHook .postKey (this );
1925
+ }
1926
+
1913
1927
if (SQLiteDebug .DEBUG_SQL_CACHE ) {
1914
1928
mTimeOpened = getTime ();
1915
1929
}
0 commit comments