Skip to content

Commit

Permalink
Merge pull request #156 from senseobservationsystems/hotfix/sqlitecip…
Browse files Browse the repository at this point in the history
…her_load_lib

Hotfix/sqlitecipher load lib
  • Loading branch information
RonaldOlsthoorn committed May 18, 2015
2 parents 9c014ff + f34b1e7 commit 13e3a3e
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 41 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v3.4.2 (2015-05-13)

### Fixed
* Fixed crash on sqlite cipher when loading and sending data too early

## v3.4.1 (2015-05-12)

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion samples/sense-android-demo/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# Project target.
target=android-16
android.library.reference.1=../../sense-android-library
android.library.reference.2=../../../google-play-services_lib_brightr
android.library.reference.2=../../../google-play-services_lib
2 changes: 1 addition & 1 deletion sense-android-library/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.sense_os.service"
android:versionName="3.4.1">
android:versionName="3.4.2">
<!-- <uses-sdk -->
<uses-sdk android:minSdkVersion="7"/>
</manifest>
Binary file not shown.
Binary file modified sense-android-library/libs/armeabi/libsqlcipher_android.so
Binary file not shown.
Binary file modified sense-android-library/libs/sqlcipher.jar
Binary file not shown.
Binary file modified sense-android-library/libs/x86/libsqlcipher_android.so
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import nl.sense_os.service.provider.SNTP;
import nl.sense_os.service.scheduler.ScheduleAlarmTool;
import nl.sense_os.service.subscription.SubscriptionManager;
import nl.sense_os.service.storage.LocalStorage;

import org.json.JSONObject;

Expand Down Expand Up @@ -135,6 +136,8 @@ public SenseServiceStub getService() {
private AppInfoSensor appInfoSensor;
private FusedLocationSensor fusedLocationListener;

private LocalStorage mLocalStorage;

/**
* Handler on main application thread to display toasts to the user.
*/
Expand Down Expand Up @@ -334,6 +337,8 @@ public void onCreate() {
Log.v(TAG, "Sense Platform service is being created");
state = ServiceStateHelper.getInstance(this);
mSubscrMgr = SubscriptionManager.getInstance();
// Create the instance to avoid concurrency problems in the SQLCipher lib
mLocalStorage = LocalStorage.getInstance(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public abstract class AppInfoVersion {

public final static String SENSE_LIBRARY_VERSION = "v3.4.1";
public final static String SENSE_LIBRARY_VERSION = "v3.4.2";
public final static String CORTEX_VERSION = "";
}
107 changes: 69 additions & 38 deletions sense-android-library/src/nl/sense_os/service/storage/DbHelper.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package nl.sense_os.service.storage;

import nl.sense_os.service.constants.SensorData.DataPoint;
import nl.sense_os.service.constants.SensePrefs;
import nl.sense_os.service.constants.SensePrefs.Main.Advanced;
import android.content.Context;
import android.content.SharedPreferences;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;
import net.sqlcipher.database.SQLiteException;
import android.provider.BaseColumns;
import android.util.Log;
import android.telephony.TelephonyManager;
import android.util.Log;

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

import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import nl.sense_os.service.constants.SensePrefs;
import nl.sense_os.service.constants.SensePrefs.Main.Advanced;
import nl.sense_os.service.constants.SensorData.DataPoint;

/**
* Helper class that assist in creating, opening and managing the SQLite3 database for data points.
*/
Expand All @@ -41,8 +44,12 @@ public class DbHelper extends SQLiteOpenHelper {

private static String passphrase = "";

private static boolean libsLoaded = false;

private static final String TAG = "DbHelper";

private Context mContext;

/**
* Constructor. The database is not actually created or opened until one of
* {@link #getWritableDatabase()} or {@link #getReadableDatabase()} is called.
Expand All @@ -55,45 +62,44 @@ public class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context, boolean persistent) {
// if the database name is null, it will be created in-memory
super(context, persistent ? DATABASE_NAME : null, null, DATABASE_VERSION);
this.mContext = context;

if (null == sMainPrefs) {
sMainPrefs = context.getSharedPreferences(SensePrefs.MAIN_PREFS,
Context.MODE_PRIVATE);
sMainPrefs = mContext.getSharedPreferences(SensePrefs.MAIN_PREFS,
Context.MODE_PRIVATE);
}
loadLibs(context);
updateEncryption();
// add a listener to update the encryption settings when it changes
sMainPrefs.registerOnSharedPreferenceChangeListener(encryptionChanged);
}

public void updateEncryption()
{
boolean encrypt = sMainPrefs.getBoolean(Advanced.ENCRYPT_DATABASE, false);

if (encrypt) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
String imei = telephonyManager.getDeviceId();

setPassphrase(imei);
}
}

SQLiteDatabase.loadLibs(context);

// check for old plain database
if (persistent && encrypt) {
// try to open the database with the password from the user.
// The only possible error now must be a wrong password or using plain database.
try {
getWritableDatabase(passphrase);
} catch (SQLiteException e) {
Log.v(TAG, "Trying to encrypte old plain database");
File plain = context.getDatabasePath(DATABASE_NAME);
File encrypted = context.getDatabasePath("tmp.db");

// try to open the database without password
SQLiteDatabase migrate_db = SQLiteDatabase.openOrCreateDatabase(plain, "", null);
migrate_db.rawExecSQL(String.format("ATTACH DATABASE '%s' AS encrypted KEY '%s'",
encrypted.getAbsolutePath(), passphrase));
migrate_db.rawExecSQL("SELECT sqlcipher_export('encrypted');");
migrate_db.execSQL("DETACH DATABASE encrypted;");
migrate_db.close();

// rename the encrypted file name back
encrypted.renameTo(new File(plain.getAbsolutePath()));
}
/**
* Monitor changes in the database encryption settings
*/
SharedPreferences.OnSharedPreferenceChangeListener encryptionChanged = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
if(key.equals(Advanced.ENCRYPT_DATABASE) || key.equals(Advanced.ENCRYPT_DATABASE_SALT))
updateEncryption();
}
};

public static synchronized void loadLibs(Context context) {
if(!libsLoaded) {
SQLiteDatabase.loadLibs(context);
libsLoaded = true;
}
}

Expand Down Expand Up @@ -127,11 +133,36 @@ public void onUpgrade(SQLiteDatabase db, int oldVers, int newVers) {
}

public SQLiteDatabase getWritableDatabase(){
return getWritableDatabase(passphrase);
try {
return getWritableDatabase(passphrase);
} catch (SQLiteException e) {
migrateDatabase();
return getWritableDatabase(passphrase);
}
}

public SQLiteDatabase getReadableDatabase(){
return getReadableDatabase(passphrase);
try {
return getWritableDatabase(passphrase);
} catch (SQLiteException e) {
migrateDatabase();
return getWritableDatabase(passphrase);
}
}

private void migrateDatabase() {
File plain = mContext.getDatabasePath(DATABASE_NAME);
File encrypted = mContext.getDatabasePath("tmp.db");

// try to open the database without password
SQLiteDatabase migrate_db = SQLiteDatabase.openOrCreateDatabase(plain, "", null);
migrate_db.rawExecSQL(String.format("ATTACH DATABASE '%s' AS encrypted KEY '%s'", encrypted.getAbsolutePath(), passphrase));
migrate_db.rawExecSQL("SELECT sqlcipher_export('encrypted');");
migrate_db.execSQL("DETACH DATABASE encrypted;");
migrate_db.close();

// rename the encrypted file name back
encrypted.renameTo(new File(plain.getAbsolutePath()));
}

private void setPassphrase(String imei) {
Expand Down

0 comments on commit 13e3a3e

Please sign in to comment.