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

import android.os.FileObserver;

import de.thedevstack.conversationsplus.entities.Conversation;
import de.thedevstack.conversationsplus.entities.Message;
import de.thedevstack.conversationsplus.entities.Transferable;
import de.thedevstack.conversationsplus.entities.TransferablePlaceholder;
import de.thedevstack.conversationsplus.utils.MessageUtil;
import de.thedevstack.conversationsplus.utils.UiUpdateHelper;
import de.thedevstack.conversationsplus.utils.XmppConnectionServiceAccessor;

/**
 * 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;
                }
            }
        }
    }
}