aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/persistance/observers/FileDeletionObserver.java
blob: a313c8b1f6f9d3a45025c10bcd03e7af06cb9097 (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
package de.thedevstack.conversationsplus.persistance.observers;

import android.os.FileObserver;

import de.thedevstack.conversationsplus.utils.MessageUtil;
import de.thedevstack.conversationsplus.utils.UiUpdateHelper;
import de.thedevstack.conversationsplus.utils.XmppConnectionServiceAccessor;

import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.Transferable;
import eu.siacs.conversations.entities.TransferablePlaceholder;

/**
 * Observer to mark messages containing files which are deleted.
 */
public class FileDeletionObserver extends FileObserver {
    public FileDeletionObserver(String path) {
        super(path, FileObserver.DELETE);
    }

    @Override
    public void onEvent(int event, String path) {
        if (null != path) {
            markFileDeleted(path.split("\\.")[0]);
        }
    }

    private void markFileDeleted(String uuid) {
        if (null != XmppConnectionServiceAccessor.xmppConnectionService) {
            for (Conversation conversation : XmppConnectionServiceAccessor.xmppConnectionService.getConversations()) {
                Message message = conversation.findMessageWithFileAndUuid(uuid);
                if (message != null) {
                    message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_DELETED));
                    final int s = message.getStatus();
                    if (s == Message.STATUS_WAITING || s == Message.STATUS_OFFERED || s == Message.STATUS_UNSEND) {
                        MessageUtil.markMessage(message, Message.STATUS_SEND_FAILED);
                    } else {
                        UiUpdateHelper.updateConversationUi();
                    }
                    return;
                }
            }
        }
    }
}