aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/utils/ConversationUtil.java
blob: 709b3f2030404c4a1c1d533fecb7343330ecef85 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package de.thedevstack.conversationsplus.utils;

import android.net.Uri;

import de.thedevstack.conversationsplus.ConversationsPlusApplication;
import de.thedevstack.conversationsplus.crypto.PgpEngine;
import de.thedevstack.conversationsplus.entities.Conversation;
import de.thedevstack.conversationsplus.entities.FileParams;
import de.thedevstack.conversationsplus.entities.Message;
import de.thedevstack.conversationsplus.enums.FileStatus;
import de.thedevstack.conversationsplus.exceptions.FileCopyException;
import de.thedevstack.conversationsplus.persistance.FileBackend;
import de.thedevstack.conversationsplus.ui.UiCallback;

/**
 * Utility class to work with conversations.
 */
public class ConversationUtil {

    public static void attachLocationToConversation(final Conversation conversation,
                                             final Uri uri,
                                             final UiCallback<Message> callback) {
        int encryption = conversation.getNextEncryption();
        if (encryption == Message.ENCRYPTION_PGP) {
            encryption = Message.ENCRYPTION_DECRYPTED;
        }
        Message message = new Message(conversation, uri.toString(), encryption);
        if (conversation.getNextCounterpart() != null) {
            message.setCounterpart(conversation.getNextCounterpart());
        }
        if (encryption == Message.ENCRYPTION_DECRYPTED) {
            PgpEngine.getInstance().encrypt(message, callback);
        } else {
            callback.success(message);
        }
    }

    public static void attachFileToConversation(final Conversation conversation,
                                         final Uri uri,
                                         final UiCallback<Message> callback) {
        final Message message;
        if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
            message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
        } else {
            message = new Message(conversation, "", conversation.getNextEncryption());
        }
        message.setCounterpart(conversation.getNextCounterpart());
        //message.setType(Message.TYPE_FILE);
        if (null == message.getFileParams()) {
            message.setFileParams(new FileParams());
        }
        message.getFileParams().setFileStatus(FileStatus.NEEDS_UPLOAD);
        String path = FileUtils.getPath(uri);
        if (path != null) {
            message.getFileParams().setPath(path);
            message.setRelativeFilePath(path); // TODO: Remove when everything is moved to fileparams
            MessageUtil.updateFileParams(message);
            if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
                PgpEngine.getInstance().encrypt(message, callback);
            } else {
                callback.success(message);
            }
        } else {
            ConversationsPlusApplication.executeFileAdding(new Runnable() {
                @Override
                public void run() {
                    try {
                        FileBackend.copyFileToPrivateStorage(message, uri);
                        MessageUtil.updateFileParams(message);
                        if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
                            PgpEngine.getInstance().encrypt(message, callback);
                        } else {
                            callback.success(message);
                        }
                    } catch (FileCopyException e) {
                        callback.error(e.getResId(), message);
                    }
                }
            });
        }
    }
}