aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/FileTransferEntity.java
blob: a44cf49d845203388fb2c12bbff2803edcdbc176 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package de.thedevstack.conversationsplus.services.filetransfer;

import java.io.InputStream;

import de.thedevstack.conversationsplus.entities.DownloadableFile;
import de.thedevstack.conversationsplus.entities.Message;
import de.thedevstack.conversationsplus.entities.Transferable;
import de.thedevstack.conversationsplus.persistance.FileBackend;
import de.thedevstack.conversationsplus.utils.MessageUtil;

/**
 *
 */
public class FileTransferEntity implements Transferable {
    private final Message message;
    private int tries = 0;
    private boolean transferred = false;
    private boolean canceled = false;
    private boolean failed = false;
    private long transmitted = 0;
    private DownloadableFile file;
    private InputStream fileInputStream;

    public FileTransferEntity(Message message) {
        this.message = message;
        this.message.setTransferable(this);
        this.file = FileBackend.getFile(message, false);
    }

    @Override
    public boolean equals(Object o) {
        FileTransferEntity other = (FileTransferEntity)o;
        if ((this.message == null && other.message != null)
            || (this.message != null && other.message == null)) {
            return false;
        } else if (this.message == null && other.message == null) {
            return true;
        }
        return this.message.getUuid().equals(other.message.getUuid());
    }

    @Override
    public boolean start() {
        return false;
    }

    @Override
    public int getStatus() {
        int status = (failed) ? STATUS_FAILED : STATUS_UPLOADING;
        return status;
    }

    @Override
    public long getFileSize() {
        return file == null ? 0 : file.getExpectedSize();
    }

    @Override
    public int getProgress() {
        if (file == null) {
            return 0;
        }
        return (int) ((((double) transmitted) / file.getExpectedSize()) * 100);
    }

    @Override
    public void cancel() {
        this.canceled = true;
    }

    public void fail() {
        this.failed = true;
        this.getMessage().setTransferable(null);
        MessageUtil.markMessage(this.getMessage(), Message.STATUS_SEND_FAILED);
    }

    public Message getMessage() {
        return message;
    }

    public boolean isCanceled() {
        return this.canceled;
    }

    public boolean isFailed() {
        return failed;
    }

    public void updateProgress(long progress) {
        this.transmitted += progress;
    }

    public DownloadableFile getFile() {
        return file;
    }

    public void transferred() {
        this.transferred = true;
    }

    public void setFileInputStream(InputStream fileInputStream) {
        this.fileInputStream = fileInputStream;
    }

    public InputStream getFileInputStream() {
        return fileInputStream;
    }
}