use of net.sqlcipher.database.SQLiteDatabase in project Signal-Android by signalapp.
the class SmsDatabase method incrementReceiptCount.
public void incrementReceiptCount(SyncMessageId messageId, boolean deliveryReceipt, boolean readReceipt) {
SQLiteDatabase database = databaseHelper.getWritableDatabase();
Cursor cursor = null;
boolean foundMessage = false;
try {
cursor = database.query(TABLE_NAME, new String[] { ID, THREAD_ID, ADDRESS, TYPE }, DATE_SENT + " = ?", new String[] { String.valueOf(messageId.getTimetamp()) }, null, null, null, null);
while (cursor.moveToNext()) {
if (Types.isOutgoingMessageType(cursor.getLong(cursor.getColumnIndexOrThrow(TYPE)))) {
Address theirAddress = messageId.getAddress();
Address ourAddress = Address.fromSerialized(cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS)));
String columnName = deliveryReceipt ? DELIVERY_RECEIPT_COUNT : READ_RECEIPT_COUNT;
if (ourAddress.equals(theirAddress)) {
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(THREAD_ID));
database.execSQL("UPDATE " + TABLE_NAME + " SET " + columnName + " = " + columnName + " + 1 WHERE " + ID + " = ?", new String[] { String.valueOf(cursor.getLong(cursor.getColumnIndexOrThrow(ID))) });
DatabaseFactory.getThreadDatabase(context).update(threadId, false);
notifyConversationListeners(threadId);
foundMessage = true;
}
}
}
if (!foundMessage) {
if (deliveryReceipt)
earlyDeliveryReceiptCache.increment(messageId.getTimetamp(), messageId.getAddress());
if (readReceipt)
earlyReadReceiptCache.increment(messageId.getTimetamp(), messageId.getAddress());
}
} finally {
if (cursor != null)
cursor.close();
}
}
use of net.sqlcipher.database.SQLiteDatabase in project remote-desktop-clients by iiordanov.
the class EnterTextDialog method saveText.
private String saveText(boolean wasSent) {
CharSequence cs = _textEnterText.getText();
if (cs.length() == 0)
return "";
String s = cs.toString();
if (wasSent || _historyIndex >= _history.size() || !s.equals(_history.get(_historyIndex).getSentText())) {
SentTextBean added = new SentTextBean();
added.setSentText(s);
SQLiteDatabase db = _canvasActivity.getDatabase().getWritableDatabase();
added.Gen_insert(db);
_history.add(added);
for (int i = 0; i < _historyIndex - NUMBER_SENT_SAVED; i++) {
SentTextBean deleteCandidate = _history.get(i);
if (deleteCandidate.get_Id() != DELETED_ID) {
deleteCandidate.Gen_delete(db);
deleteCandidate.set_Id(DELETED_ID);
}
}
}
return s;
}
use of net.sqlcipher.database.SQLiteDatabase in project remote-desktop-clients by iiordanov.
the class IntroTextDialog method showAgain.
private void showAgain(boolean show) {
SQLiteDatabase db = database.getWritableDatabase();
MostRecentBean mostRecent = ConnectionBean.getMostRecent(db);
if (mostRecent != null) {
int value = -1;
if (!show) {
value = packageInfo.versionCode;
}
mostRecent.setShowSplashVersion(value);
mostRecent.Gen_update(db);
}
database.close();
dismiss();
dialog = null;
}
use of net.sqlcipher.database.SQLiteDatabase in project remote-desktop-clients by iiordanov.
the class ConnectionBean method saveAndWriteRecent.
public void saveAndWriteRecent(boolean saveEmpty, Database database) {
// Alternately, perhaps we could process some extra data
if ((getConnectionType() == Constants.CONN_TYPE_SSH && getSshServer().equals("") || getAddress().equals("")) && !saveEmpty) {
return;
}
SQLiteDatabase db = database.getWritableDatabase();
db.beginTransaction();
try {
save(db);
MostRecentBean mostRecent = getMostRecent(db);
if (mostRecent == null) {
mostRecent = new MostRecentBean();
mostRecent.setConnectionId(get_Id());
mostRecent.Gen_insert(db);
} else {
mostRecent.setConnectionId(get_Id());
mostRecent.Gen_update(db);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
db.close();
}
if (db.isOpen()) {
db.close();
}
}
use of net.sqlcipher.database.SQLiteDatabase in project remote-desktop-clients by iiordanov.
the class Database method changeDatabasePassword.
public boolean changeDatabasePassword(String newPassword) {
// Get readable database with old password
SQLiteDatabase db = getReadableDatabase(Database.password);
int version = db.getVersion();
String pathToDb = db.getPath();
// Delete any temp db that may be in the way.
deleteTempDatabase(pathToDb, "-temp");
String newFormat = null;
// If the previous key is an empty string, we must encrypt a plaintext DB.
if (Database.password.equals("")) {
Log.i(TAG, "Previous database unencrypted, encrypting.");
newFormat = "encrypted";
// If the previous key is not an empty string, then we must rekey an existing DB.
} else if (newPassword.equals("")) {
Log.i(TAG, "Previous database encrypted, decrypting.");
newFormat = "plaintext";
// If both the previous and new password are non-empty, we are rekeying the DB.
} else {
Log.i(TAG, "Previous database encrypted, rekeying.");
db.rawExecSQL(String.format("PRAGMA key = '%s'", Database.password));
Database.setPassword(newPassword);
// Rekey the database
db.rawExecSQL(String.format("PRAGMA rekey = '%s'", Database.password));
}
if (newFormat != null) {
// Write out the database in the new format.
db.rawExecSQL(String.format("ATTACH DATABASE '%s' AS %s KEY '%s'", db.getPath() + "-temp", newFormat, newPassword));
db.rawExecSQL(String.format("select sqlcipher_export('%s')", newFormat));
db.rawExecSQL(String.format("DETACH DATABASE '%s'", newFormat));
db.close();
this.close();
Log.i(TAG, "Done exporting to: " + newFormat);
try {
// Set version of database
SQLiteDatabase tempDb = SQLiteDatabase.openDatabase(db.getPath() + "-temp", newPassword, null, SQLiteDatabase.OPEN_READWRITE);
tempDb.setVersion(version);
tempDb.close();
} catch (Exception e) {
// If we could not open the temp database, delete it and return failure.
deleteTempDatabase(pathToDb, "-temp");
return false;
}
try {
// Save away the old database
moveFile(pathToDb, pathToDb + "-BAK");
// Move the temp database file over the old database
moveFile(pathToDb + "-temp", pathToDb);
// Test-open the new database and read off the version.
SQLiteDatabase newDb = SQLiteDatabase.openDatabase(db.getPath(), newPassword, null, SQLiteDatabase.OPEN_READWRITE);
newDb.getVersion();
newDb.close();
} catch (Exception e) {
// Having received an exception in testing the DB above, restore the old DB and return failure.
moveFile(pathToDb + "-BAK", pathToDb);
return false;
} finally {
// Having succeeded to open the database without an exception, delete the backup of the old DB.
deleteTempDatabase(pathToDb, "-BAK");
// And set the password to the new password.
Database.setPassword(newPassword);
}
} else {
db.close();
this.close();
}
return true;
}
Aggregations