Skip to content

Commit 2416926

Browse files
committed
updated source to handle SDK version library loading
1 parent 191bc61 commit 2416926

5 files changed

Lines changed: 99 additions & 65 deletions

File tree

default.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
# project structure.
99

1010
# Project target.
11-
target=android-8
11+
target=android-6

jni/Android.mk

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
LOCAL_PATH:= $(call my-dir)
1+
uOCAL_PATH:= $(call my-dir)
22

33
EXTERNAL_PATH := ../external
44

@@ -50,7 +50,8 @@ LOCAL_SHARED_LIBRARIES := \
5050
libsqlite3_android
5151

5252
LOCAL_CFLAGS += -U__APPLE__
53-
LOCAL_LDFLAGS += -L../obj/local/armeabi/ -L/home/n8fr8/android/mydroid/out/target/product/generic/obj/SHARED_LIBRARIES/libutils_intermediates/LINKED/ -L/home/n8fr8/android/mydroid/out/target/product/generic/obj/SHARED_LIBRARIES/libbinder_intermediates/LINKED/
53+
LOCAL_LDFLAGS += -L../external/android-2.2/ -L../external/libs/armeabi/
54+
LOCAL_LDFLAGS += -L/home/n8fr8/android/mydroid/out/target/product/generic/obj/SHARED_LIBRARIES/libutils_intermediates/LINKED/ -L/home/n8fr8/android/mydroid/out/target/product/generic/obj/SHARED_LIBRARIES/libbinder_intermediates/LINKED/ -L/home/n8fr8/android/mydroid/out/target/product/generic/obj/SHARED_LIBRARIES/libandroid_runtime_intermediates/LINKED/
5455

5556
# libs from the NDK
5657
LOCAL_LDLIBS += -ldl -llog

src/example/SQLDemoActivity.java

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,24 @@
1010
public class SQLDemoActivity extends Activity {
1111
EventDataSQLHelper eventsData;
1212

13-
/*
14-
0x00000001 (NEEDED) Shared library: [libstlport_shared.so]
15-
0x00000001 (NEEDED) Shared library: [libc.so]
16-
0x00000001 (NEEDED) Shared library: [libstdc++.so]
17-
0x00000001 (NEEDED) Shared library: [libm.so]
18-
0x00000001 (NEEDED) Shared library: [libsqlcipher.so]
19-
0x00000001 (NEEDED) Shared library: [liblog.so]
20-
0x00000001 (NEEDED) Shared library: [libdl.s
21-
*/
2213

2314

2415
@Override
2516
public void onCreate(Bundle savedInstanceState) {
2617
super.onCreate(savedInstanceState);
18+
19+
//you must set Context on SQLiteDatabase first
20+
SQLiteDatabase.loadLibs(this);
2721

28-
// System.loadLibrary("nativehelper");
29-
// System.loadLibrary("android_runtime");
30-
31-
// System.loadLibrary("crypto");
32-
// System.loadLibrary("ssl");
33-
34-
/*
35-
System.loadLibrary("icudata");
36-
System.loadLibrary("icui18n");
37-
System.loadLibrary("icuuc");
38-
*/
39-
40-
41-
/*
42-
System.loadLibrary("lib/libstlport_shared.so");
43-
System.loadLibrary("lib/libsqlcipher.so");
44-
System.loadLibrary("lib/libdatabase_sqlcipher.so");
45-
System.loadLibrary("lib/libsqlcipher_android.so");
46-
*/
22+
String password = "foo123";
4723

24+
//then you can open the database using a password
25+
SQLiteDatabase db = eventsData.getWritableDatabase(password);
26+
db.close();
27+
4828
eventsData = new EventDataSQLHelper(this);
49-
addEvent("Hello Android Event");
50-
Cursor cursor = getEvents();
29+
addEvent("Hello Android Event", password);
30+
Cursor cursor = getEvents(password);
5131
showEvents(cursor);
5232
}
5333

@@ -56,9 +36,8 @@ public void onDestroy() {
5636
eventsData.close();
5737
}
5838

59-
private void addEvent(String title) {
60-
SQLiteDatabase db = eventsData.getWritableDatabase();
61-
39+
private void addEvent(String title, String password) {
40+
SQLiteDatabase db = eventsData.getWritableDatabase(password);
6241

6342
ContentValues values = new ContentValues();
6443
values.put(EventDataSQLHelper.TIME, System.currentTimeMillis());
@@ -67,8 +46,11 @@ private void addEvent(String title) {
6746

6847
}
6948

70-
private Cursor getEvents() {
71-
SQLiteDatabase db = eventsData.getReadableDatabase();
49+
private Cursor getEvents(String password) {
50+
SQLiteDatabase db = eventsData.getReadableDatabase(password);
51+
52+
53+
7254
Cursor cursor = db.query(EventDataSQLHelper.TABLE, null, null, null, null,
7355
null, null);
7456

src/info/guardianproject/database/sqlcipher/SQLiteDatabase.java

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616

1717
package info.guardianproject.database.sqlcipher;
1818

19-
import android.database.Cursor;
2019
import info.guardianproject.database.DatabaseUtils;
2120
import info.guardianproject.database.SQLException;
2221
import info.guardianproject.database.sqlcipher.SQLiteDebug.DbStats;
2322

2423
import 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;
2529
import java.lang.ref.WeakReference;
2630
import java.text.SimpleDateFormat;
2731
import java.util.ArrayList;
@@ -36,17 +40,19 @@
3640
import java.util.concurrent.locks.ReentrantLock;
3741
import java.util.regex.Pattern;
3842

39-
import com.google.common.collect.Maps;
40-
4143
import android.content.ContentValues;
44+
import android.content.Context;
45+
import android.database.Cursor;
4246
import android.os.Debug;
47+
import android.os.Environment;
4348
import android.os.SystemClock;
4449
import android.text.TextUtils;
4550
import android.util.Config;
46-
import android.util.EventLog;
4751
import android.util.Log;
4852
import 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
/**

src/info/guardianproject/database/sqlcipher/SQLiteOpenHelper.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public SQLiteOpenHelper(Context context, String name, CursorFactory factory, int
7474
* @throws SQLiteException if the database cannot be opened for writing
7575
* @return a read/write database object valid until {@link #close} is called
7676
*/
77-
public synchronized SQLiteDatabase getWritableDatabase() {
77+
public synchronized SQLiteDatabase getWritableDatabase(String password) {
7878
if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
7979
return mDatabase; // The database is already open for business
8080
}
@@ -95,7 +95,7 @@ public synchronized SQLiteDatabase getWritableDatabase() {
9595
try {
9696
mIsInitializing = true;
9797
if (mName == null) {
98-
db = SQLiteDatabase.create(null);
98+
db = SQLiteDatabase.create(null, password);
9999

100100
} else {
101101
String path = mContext.getDatabasePath(mName).getPath();
@@ -104,7 +104,7 @@ public synchronized SQLiteDatabase getWritableDatabase() {
104104
if (!dbPathFile.exists())
105105
dbPathFile.getParentFile().mkdirs();
106106

107-
db = SQLiteDatabase.openOrCreateDatabase(path, mFactory);
107+
db = SQLiteDatabase.openOrCreateDatabase(path, password, mFactory);
108108

109109
// db = SQLiteDatabase.openDatabase(path,mFactory , SQLiteDatabase.OPEN_READWRITE);
110110

@@ -160,7 +160,7 @@ public synchronized SQLiteDatabase getWritableDatabase() {
160160
* @return a database object valid until {@link #getWritableDatabase}
161161
* or {@link #close} is called.
162162
*/
163-
public synchronized SQLiteDatabase getReadableDatabase() {
163+
public synchronized SQLiteDatabase getReadableDatabase(String password) {
164164
if (mDatabase != null && mDatabase.isOpen()) {
165165
return mDatabase; // The database is already open for business
166166
}
@@ -170,7 +170,7 @@ public synchronized SQLiteDatabase getReadableDatabase() {
170170
}
171171

172172
try {
173-
return getWritableDatabase();
173+
return getWritableDatabase(password);
174174
} catch (SQLiteException e) {
175175
if (mName == null) throw e; // Can't open a temp database read-only!
176176
Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);
@@ -180,7 +180,7 @@ public synchronized SQLiteDatabase getReadableDatabase() {
180180
try {
181181
mIsInitializing = true;
182182
String path = mContext.getDatabasePath(mName).getPath();
183-
db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY);
183+
db = SQLiteDatabase.openDatabase(path, password, mFactory, SQLiteDatabase.OPEN_READONLY);
184184
if (db.getVersion() != mNewVersion) {
185185
throw new SQLiteException("Can't upgrade read-only database from version " +
186186
db.getVersion() + " to " + mNewVersion + ": " + path);

0 commit comments

Comments
 (0)