aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/persistance/MessageDatabaseAccess.java
blob: ba5f4a2c44a55cdcd561e192a9554ba332134482 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package de.thedevstack.conversationsplus.persistance;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import de.thedevstack.android.logcat.Logging;

import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid;

/**
 * Created by steckbrief on 15.04.2016.
 */
public class MessageDatabaseAccess {
    public static final String TABLE_NAME_ADDITIONAL_PARAMETERS = "message_parameters";
    public static final String COLUMN_NAME_MSG_PARAMS_HTTPUPLOAD = "httpupload";
    public static final String COLUMN_NAME_MSG_PARAMS_MSGUUID = "message_uuid";
    public static final String COLUMN_NAME_MSG_PARAMS_TREATASDOWNLOADABLE_DECISION = "treatasdownloadable_decision";

    public static final String TABLE_ADDITIONAL_PARAMETERS_CREATE_V0 = "CREATE TABLE " + MessageDatabaseAccess.TABLE_NAME_ADDITIONAL_PARAMETERS + " ("
            + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_MSGUUID + " TEXT, "
            + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_HTTPUPLOAD + " INTEGER DEFAULT 0, "
            + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_TREATASDOWNLOADABLE_DECISION + " TEXT DEFAULT 'NOT_DECIDED', "
            + "FOREIGN KEY(" + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_MSGUUID + ") REFERENCES " + Message.TABLENAME + "(" + Message.UUID + ") ON DELETE CASCADE)";

    public static ContentValues getAdditionalParametersContentValues(Message message) {
        ContentValues additionalParameters = new ContentValues();
        additionalParameters.put(MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_MSGUUID, message.getUuid());
        additionalParameters.put(MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_HTTPUPLOAD, message.isHttpUploaded() ? 1 : 0);
        additionalParameters.put(MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_TREATASDOWNLOADABLE_DECISION, message.treatAsDownloadable().name());

        return additionalParameters;
    }

    public static void populateMessageParametersFromCursor(Cursor cursor, Message message) {
        boolean isHttpUploaded = CursorHelper.getInt(cursor, COLUMN_NAME_MSG_PARAMS_HTTPUPLOAD) == 1;
        message.setHttpUploaded(isHttpUploaded);
        String downloadable = CursorHelper.getString(cursor, COLUMN_NAME_MSG_PARAMS_TREATASDOWNLOADABLE_DECISION);
        Message.Decision treatAsDownloadable = Message.Decision.NOT_DECIDED;
        try {
            treatAsDownloadable = Message.Decision.valueOf(downloadable);
        } catch (IllegalArgumentException e) {
            // Should only happen if the database is corrupted, but to be on the save side catch it here
            Logging.e("db.msg", "Unknown Decision for treatAsDownloadable found: '" + downloadable + "'");
        }
        message.setTreatAsDownloadable(treatAsDownloadable);
    }

    public static void populateMessageParameters(SQLiteDatabase db, Message message) {
        Cursor paramsCursor = db.query(MessageDatabaseAccess.TABLE_NAME_ADDITIONAL_PARAMETERS,
                null, MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_MSGUUID + "=?",
                new String[] {message.getUuid()},
                null, null, null, null);
        paramsCursor.moveToNext();
        MessageDatabaseAccess.populateMessageParametersFromCursor(paramsCursor, message);
        paramsCursor.close();
    }
}