aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/persistance
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/eu/siacs/conversations/persistance')
-rw-r--r--src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java62
-rw-r--r--src/main/java/eu/siacs/conversations/persistance/FileBackend.java42
2 files changed, 102 insertions, 2 deletions
diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java
index e8acd400..bd20e694 100644
--- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java
+++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java
@@ -34,6 +34,8 @@ import java.util.concurrent.CopyOnWriteArrayList;
import org.json.JSONException;
import de.thedevstack.android.logcat.Logging;
+import de.thedevstack.conversationsplus.persistance.MessageDatabaseAccess;
+
import eu.siacs.conversations.Config;
import eu.siacs.conversations.crypto.axolotl.AxolotlServiceImpl;
import eu.siacs.conversations.crypto.axolotl.SQLiteAxolotlStore;
@@ -53,6 +55,10 @@ public class DatabaseBackend extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "history";
private static final int DATABASE_VERSION = 25;
+ private static final int C_TO_CPLUS_VERSION_OFFSET = 1000;
+ private static final int CPLUS_DATABASE_VERSION = 1;
+ private static final int CPLUS_DATABASE_VERSION_MULTIPLIER = 100;
+ private static final int PHYSICAL_DATABASE_VERSION = DATABASE_VERSION + C_TO_CPLUS_VERSION_OFFSET + (CPLUS_DATABASE_VERSION * CPLUS_DATABASE_VERSION_MULTIPLIER);
private static String CREATE_CONTATCS_STATEMENT = "create table "
+ Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, "
@@ -129,8 +135,17 @@ public class DatabaseBackend extends SQLiteOpenHelper {
+ ") ON CONFLICT IGNORE"
+ ");";
+ private static int calculateCDatabaseVersion(int physicalDatabaseVersion) {
+ return physicalDatabaseVersion % CPLUS_DATABASE_VERSION_MULTIPLIER;
+ }
+
+ private static int calculateCPLusDatabaseVersion(int physicalDatabaseVersion) {
+ int cPlusDatabaseVersion = (physicalDatabaseVersion - C_TO_CPLUS_VERSION_OFFSET) / CPLUS_DATABASE_VERSION_MULTIPLIER;
+ return cPlusDatabaseVersion < 0 ? 0 : cPlusDatabaseVersion;
+ }
+
private DatabaseBackend(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
+ super(context, DATABASE_NAME, null, PHYSICAL_DATABASE_VERSION);
}
@Override
@@ -176,10 +191,34 @@ public class DatabaseBackend extends SQLiteOpenHelper {
db.execSQL(CREATE_PREKEYS_STATEMENT);
db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
db.execSQL(CREATE_IDENTITIES_STATEMENT);
+
+ // Create Conversations+ related tables
+ db.execSQL(MessageDatabaseAccess.TABLE_ADDITIONAL_PARAMETERS_CREATE_V0);
}
+ protected void onUpgradeCPlusDatabase(SQLiteDatabase db, int oldVersion, int newVersion) {
+ Logging.d("db.upgrade.cplus", "Updating Conversations+ database from version '" + oldVersion + "' to '" + newVersion + "'");
+ if (oldVersion < newVersion) {
+ if (oldVersion == 0 && newVersion == 1) {
+ Logging.d("db.upgrade.cplus", "Creating additional parameters table for messages.");
+ db.execSQL(MessageDatabaseAccess.TABLE_ADDITIONAL_PARAMETERS_CREATE_V0);
+ db.execSQL("INSERT INTO " + MessageDatabaseAccess.TABLE_NAME_ADDITIONAL_PARAMETERS + "(" + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_MSGUUID + ") "
+ + " SELECT " + Message.UUID + " FROM " + Message.TABLENAME);
+ }
+ }
+ }
+
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+ onUpgradeConversationsDatabase(db, calculateCDatabaseVersion(oldVersion), calculateCDatabaseVersion(newVersion));
+ onUpgradeCPlusDatabase(db, calculateCPLusDatabaseVersion(oldVersion), calculateCPLusDatabaseVersion(newVersion));
+ }
+
+ protected void onUpgradeConversationsDatabase(SQLiteDatabase db, int oldVersion, int newVersion) {
+ Logging.d("db.upgrade.conversations", "Updating Conversations database from version '" + oldVersion + "' to '" + newVersion + "'");
+ if (oldVersion == newVersion) {
+ return;
+ }
if (oldVersion < 2 && newVersion >= 2) {
db.execSQL("update " + Account.TABLENAME + " set "
+ Account.OPTIONS + " = " + Account.OPTIONS + " | 8");
@@ -396,8 +435,14 @@ public class DatabaseBackend extends SQLiteOpenHelper {
}
public void createMessage(Message message) {
+ Logging.d("db.msg.insert", "Inserting new message with uuid '" + message.getUuid() + "', isRead: " + message.isRead());
+
SQLiteDatabase db = this.getWritableDatabase();
+ db.beginTransaction();
db.insert(Message.TABLENAME, null, message.getContentValues());
+ db.insert(MessageDatabaseAccess.TABLE_NAME_ADDITIONAL_PARAMETERS, null, MessageDatabaseAccess.getAdditionalParametersContentValues(message));
+ db.setTransactionSuccessful();
+ db.endTransaction();
}
public void createAccount(Account account) {
@@ -471,6 +516,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
cursor.moveToLast();
do {
Message message = Message.fromCursor(cursor);
+ MessageDatabaseAccess.populateMessageParameters(db, message);
message.setConversation(conversation);
list.add(message);
} while (cursor.moveToPrevious());
@@ -502,6 +548,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
@Override
public Message next() {
Message message = Message.fromCursor(cursor);
+ MessageDatabaseAccess.populateMessageParameters(db, message);
cursor.moveToNext();
return message;
}
@@ -598,17 +645,28 @@ public class DatabaseBackend extends SQLiteOpenHelper {
}
public void updateMessage(Message message) {
+ Logging.d("db.msg.update", "Updating message with uuid '" + message.getUuid() + "', isRead: " + message.isRead());
SQLiteDatabase db = this.getWritableDatabase();
String[] args = {message.getUuid()};
+ db.beginTransaction();
db.update(Message.TABLENAME, message.getContentValues(), Message.UUID
+ "=?", args);
+ db.update(MessageDatabaseAccess.TABLE_NAME_ADDITIONAL_PARAMETERS, MessageDatabaseAccess.getAdditionalParametersContentValues(message), MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_MSGUUID + "=?", args);
+ db.setTransactionSuccessful();
+ db.endTransaction();
}
public void updateMessage(Message message, String uuid) {
+ Logging.d("db.msg.update", "Updating message with uuid '" + uuid + "', isRead: " + message.isRead());
+
SQLiteDatabase db = this.getWritableDatabase();
String[] args = {uuid};
+ db.beginTransaction();
db.update(Message.TABLENAME, message.getContentValues(), Message.UUID
+ "=?", args);
+ db.update(MessageDatabaseAccess.TABLE_NAME_ADDITIONAL_PARAMETERS, MessageDatabaseAccess.getAdditionalParametersContentValues(message), MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_MSGUUID + "=?", args);
+ db.setTransactionSuccessful();
+ db.endTransaction();
}
public void readRoster(Roster roster) {
@@ -642,6 +700,8 @@ public class DatabaseBackend extends SQLiteOpenHelper {
}
public void deleteMessagesInConversation(Conversation conversation) {
+ Logging.d("db.msg.delete", "Deleting messages in conversation with uuid '" + conversation.getUuid());
+
SQLiteDatabase db = this.getWritableDatabase();
String[] args = {conversation.getUuid()};
db.delete(Message.TABLENAME, Message.CONVERSATION + "=?", args);
diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java
index c0d09c07..8abbb0cb 100644
--- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java
+++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java
@@ -21,6 +21,7 @@ import de.thedevstack.android.logcat.Logging;
import de.thedevstack.conversationsplus.ConversationsPlusApplication;
import de.thedevstack.conversationsplus.ConversationsPlusPreferences;
import de.thedevstack.conversationsplus.exceptions.FileCopyException;
+import de.thedevstack.conversationsplus.persistance.observers.FileDeletionObserver;
import de.thedevstack.conversationsplus.utils.StreamUtil;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
@@ -30,8 +31,34 @@ import eu.siacs.conversations.services.XmppConnectionService;
public class FileBackend {
private static final SimpleDateFormat imageDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.US);
+ private static FileBackend INSTANCE;
- public static void createNoMedia() {
+ private FileDeletionObserver privateFilesDirectoryObserver;
+ private FileDeletionObserver privateFilesImageDirectoryObserver;
+ private FileDeletionObserver conversationsFilesDirectoryObserver;
+ private FileDeletionObserver conversationsImagesDirectoryObserver;
+
+ public static void init() {
+ if (null == INSTANCE) {
+ INSTANCE = new FileBackend();
+ }
+ INSTANCE.initFileObservers();
+ INSTANCE.createNoMedia();
+ }
+
+ private void initFileObservers() {
+ this.privateFilesDirectoryObserver = new FileDeletionObserver(FileBackend.getPrivateFileDirectoryPath());
+ this.privateFilesImageDirectoryObserver = new FileDeletionObserver(FileBackend.getConversationsFileDirectory());
+ this.conversationsFilesDirectoryObserver = new FileDeletionObserver(FileBackend.getConversationsImageDirectory());
+ this.conversationsImagesDirectoryObserver = new FileDeletionObserver(FileBackend.getPrivateImageDirectoryPath());
+
+ this.privateFilesDirectoryObserver.startWatching();
+ this.privateFilesImageDirectoryObserver.startWatching();
+ this.conversationsFilesDirectoryObserver.startWatching();
+ this.conversationsImagesDirectoryObserver.startWatching();
+ }
+
+ private void createNoMedia() {
final File nomedia = new File(getConversationsFileDirectory()+".nomedia");
if (!nomedia.exists()) {
try {
@@ -42,6 +69,19 @@ public class FileBackend {
}
}
+ public static void onFileTransferFolderChanged() {
+ INSTANCE.conversationsFilesDirectoryObserver.stopWatching();
+ INSTANCE.conversationsFilesDirectoryObserver = new FileDeletionObserver(FileBackend.getConversationsFileDirectory());
+ INSTANCE.conversationsFilesDirectoryObserver.startWatching();
+ INSTANCE.createNoMedia();
+ }
+
+ public static void onImageTransferFolderChanged() {
+ INSTANCE.conversationsImagesDirectoryObserver.stopWatching();
+ INSTANCE.conversationsImagesDirectoryObserver = new FileDeletionObserver(FileBackend.getConversationsImageDirectory());
+ INSTANCE.conversationsImagesDirectoryObserver.startWatching();
+ }
+
public static void updateMediaScanner(File file, XmppConnectionService xmppConnectionService) {
if (file.getAbsolutePath().startsWith(getConversationsImageDirectory())) {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);