1616
1717package info .guardianproject .database .sqlcipher ;
1818
19- import android .database .Cursor ;
2019import info .guardianproject .database .DatabaseUtils ;
2120import info .guardianproject .database .SQLException ;
2221import info .guardianproject .database .sqlcipher .SQLiteDebug .DbStats ;
2322
2423import java .io .File ;
24+ import java .io .FileInputStream ;
25+ import java .io .FileOutputStream ;
26+ import java .io .IOException ;
27+ import java .io .InputStream ;
28+ import java .io .OutputStream ;
2529import java .lang .ref .WeakReference ;
2630import java .text .SimpleDateFormat ;
2731import java .util .ArrayList ;
3640import java .util .concurrent .locks .ReentrantLock ;
3741import java .util .regex .Pattern ;
3842
39- import com .google .common .collect .Maps ;
40-
4143import android .content .ContentValues ;
44+ import android .content .Context ;
45+ import android .database .Cursor ;
4246import android .os .Debug ;
47+ import android .os .Environment ;
4348import android .os .SystemClock ;
4449import android .text .TextUtils ;
4550import android .util .Config ;
46- import android .util .EventLog ;
4751import android .util .Log ;
4852import android .util .Pair ;
4953
54+ import com .google .common .collect .Maps ;
55+
5056/**
5157 * Exposes methods to manage a SQLite database.
5258 * <p>SQLiteDatabase has methods to create, delete, execute SQL commands, and
@@ -67,12 +73,52 @@ public class SQLiteDatabase extends SQLiteClosable {
6773 private static final int EVENT_DB_OPERATION = 52000 ;
6874 private static final int EVENT_DB_CORRUPT = 75004 ;
6975
70- static
76+ public static void loadLibs ( Context context )
7177 {
72- System .loadLibrary ("stlport_shared" );
73- System .loadLibrary ("sqlcipher" );
74- System .loadLibrary ("sqlcipher_android" );
75- System .loadLibrary ("database_sqlcipher" );
78+
79+ System .loadLibrary ("stlport_shared" );
80+
81+ System .loadLibrary ("sqlcipher" );
82+
83+ String soFileName = "libsqlcipher_android" ;
84+
85+ File baseFile = context .getFilesDir ();
86+
87+ File soFile = new File (baseFile ,soFileName + ".so" );
88+
89+ boolean soLoaded = soFile .exists ();
90+
91+ if (!soLoaded )
92+ {
93+ File libFile = new File (baseFile .getParent (),"lib" );
94+ File soSrcFile = new File (libFile ,soFileName + "-" + android .os .Build .VERSION .SDK_INT + ".so" );
95+
96+ try
97+ {
98+ InputStream in = new FileInputStream (soSrcFile );
99+
100+ soFile .getParentFile ().mkdirs ();
101+ OutputStream out = new FileOutputStream (soFile );
102+
103+ // Transfer bytes from in to out
104+ byte [] buf = new byte [1024 ];
105+ int len ;
106+ while ((len = in .read (buf )) > 0 ) {
107+ out .write (buf , 0 , len );
108+ }
109+ in .close ();
110+ out .close ();
111+ }
112+ catch (IOException ioe )
113+ {
114+
115+ }
116+
117+ }
118+
119+ System .load (soFile .getAbsolutePath ());
120+
121+ System .loadLibrary ("database_sqlcipher" );
76122 }
77123
78124 /**
@@ -355,7 +401,7 @@ public void setLockingEnabled(boolean lockingEnabled) {
355401
356402 /* package */ void onCorruption () {
357403 Log .e (TAG , "Removing corrupt database: " + mPath );
358- EventLog .writeEvent (EVENT_DB_CORRUPT , mPath );
404+ // EventLog.writeEvent(EVENT_DB_CORRUPT, mPath);
359405 try {
360406 // Close the database (if we can), which will cause subsequent operations to fail.
361407 close ();
@@ -817,11 +863,11 @@ public Cursor newCursor(SQLiteDatabase db,
817863 * @return the newly opened database
818864 * @throws SQLiteException if the database cannot be opened
819865 */
820- public static SQLiteDatabase openDatabase (String path , CursorFactory factory , int flags ) {
866+ public static SQLiteDatabase openDatabase (String path , String password , CursorFactory factory , int flags ) {
821867 SQLiteDatabase sqliteDatabase = null ;
822868 try {
823869 // Open the database.
824- sqliteDatabase = new SQLiteDatabase (path , factory , flags );
870+ sqliteDatabase = new SQLiteDatabase (path , password , factory , flags );
825871 if (SQLiteDebug .DEBUG_SQL_STATEMENTS ) {
826872 sqliteDatabase .enableSqlTracing (path );
827873 }
@@ -837,7 +883,7 @@ public static SQLiteDatabase openDatabase(String path, CursorFactory factory, in
837883 // delete is only for non-memory database files
838884 new File (path ).delete ();
839885 }
840- sqliteDatabase = new SQLiteDatabase (path , factory , flags );
886+ sqliteDatabase = new SQLiteDatabase (path , password , factory , flags );
841887 }
842888 ActiveDatabases .getInstance ().mActiveDatabases .add (
843889 new WeakReference <SQLiteDatabase >(sqliteDatabase ));
@@ -847,15 +893,15 @@ public static SQLiteDatabase openDatabase(String path, CursorFactory factory, in
847893 /**
848894 * Equivalent to openDatabase(file.getPath(), factory, CREATE_IF_NECESSARY).
849895 */
850- public static SQLiteDatabase openOrCreateDatabase (File file , CursorFactory factory ) {
851- return openOrCreateDatabase (file .getPath (), factory );
896+ public static SQLiteDatabase openOrCreateDatabase (File file , String password , CursorFactory factory ) {
897+ return openOrCreateDatabase (file .getPath (), password , factory );
852898 }
853899
854900 /**
855901 * Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY).
856902 */
857- public static SQLiteDatabase openOrCreateDatabase (String path , CursorFactory factory ) {
858- return openDatabase (path , factory , CREATE_IF_NECESSARY );
903+ public static SQLiteDatabase openOrCreateDatabase (String path , String password , CursorFactory factory ) {
904+ return openDatabase (path , password , factory , CREATE_IF_NECESSARY );
859905 }
860906
861907 /**
@@ -869,9 +915,9 @@ public static SQLiteDatabase openOrCreateDatabase(String path, CursorFactory fac
869915 * cursor when query is called
870916 * @return a SQLiteDatabase object, or null if the database can't be created
871917 */
872- public static SQLiteDatabase create (CursorFactory factory ) {
918+ public static SQLiteDatabase create (CursorFactory factory , String password ) {
873919 // This is a magic string with special meaning for SQLite.
874- return openDatabase (":memory:" , factory , CREATE_IF_NECESSARY );
920+ return openDatabase (":memory:" , password , factory , CREATE_IF_NECESSARY );
875921 }
876922
877923 /**
@@ -1816,7 +1862,9 @@ protected void finalize() {
18161862 * @param flags 0 or {@link #NO_LOCALIZED_COLLATORS}. If the database file already
18171863 * exists, mFlags will be updated appropriately.
18181864 */
1819- public SQLiteDatabase (String path , CursorFactory factory , int flags ) {
1865+ public SQLiteDatabase (String path , String password , CursorFactory factory , int flags ) {
1866+
1867+
18201868 if (path == null ) {
18211869 throw new IllegalArgumentException ("path should not be null" );
18221870 }
@@ -1827,6 +1875,8 @@ public SQLiteDatabase(String path, CursorFactory factory, int flags) {
18271875 mFactory = factory ;
18281876 dbopen (mPath , mFlags );
18291877
1878+ execSQL ("PRAGMA key = '" + password + "'" );
1879+
18301880 if (SQLiteDebug .DEBUG_SQL_CACHE ) {
18311881 mTimeOpened = getTime ();
18321882 }
@@ -1926,13 +1976,14 @@ public final String getPath() {
19261976 String blockingPackage = "unknown" ;//ActivityThread.currentPackageName();
19271977 if (blockingPackage == null ) blockingPackage = "" ;
19281978
1979+ /*
19291980 EventLog.writeEvent(
19301981 EVENT_DB_OPERATION,
19311982 getPathForLogs(),
19321983 sql,
19331984 durationMillis,
19341985 blockingPackage,
1935- samplePercent );
1986+ samplePercent);*/
19361987 }
19371988
19381989 /**
0 commit comments