aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/download/HttpDownloadFileTransferEntity.java
blob: fcd3904b210d1944c593568961883a6d4c8ef9a6 (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
package de.thedevstack.conversationsplus.services.filetransfer.http.download;

import android.os.PowerManager;

import de.thedevstack.conversationsplus.entities.FileParams;
import de.thedevstack.conversationsplus.entities.Message;
import de.thedevstack.conversationsplus.http.ProgressListener;
import de.thedevstack.conversationsplus.services.filetransfer.FileTransferEntity;
import de.thedevstack.conversationsplus.utils.UiUpdateHelper;

/**
 *
 */
public class HttpDownloadFileTransferEntity extends FileTransferEntity implements ProgressListener {
    public PowerManager.WakeLock wakeLock;
    /**
     * Initializes the FileTransferEntity based on the associated message.
     * This initialization includes loading the file and associating this transferable to the message.
     *
     * @param message the message in which the file to transfer is contained.
     */
    public HttpDownloadFileTransferEntity(Message message) {
        super(message, false);
        FileParams fileParams = message.getFileParams();
        this.initFile();
        this.getFile().setExpectedSize(fileParams.getSize());
    }

    /**
     * Returns the global transferable status.
     *
     * @return {@value STATUS_FAILED} if #isFailed returns <code>true</code>, {@value STATUS_DOWNLOADING} otherwise
     */
    @Override
    public int getStatus() {
        int status = (isFailed()) ? STATUS_FAILED : STATUS_DOWNLOADING;
        return status;
    }

    @Override
    public void update(long bytesRead, long contentLength, boolean done) {
        if (0 >= getFile().getExpectedSize()) {
            getFile().setExpectedSize(contentLength);
            getMessage().getFileParams().setSize(contentLength);
        }
        if (done) {
            this.transferred();
        } else {
            this.updateProgress(bytesRead);
        }
        UiUpdateHelper.updateConversationUi();
    }
}