diff options
author | steckbrief <steckbrief@chefmail.de> | 2017-02-06 10:01:13 +0100 |
---|---|---|
committer | steckbrief <steckbrief@chefmail.de> | 2017-02-06 10:01:13 +0100 |
commit | 754de6bb0449a577d2bb9c28cca6adf0ef9554f6 (patch) | |
tree | 279b405d94e0d86d10ed94bd34d919457944ead6 /src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/download/HttpFileDownloadCallback.java | |
parent | cd633f13b8d7327e47994bb5a000f0c0b7089e7f (diff) |
relates FS#241: Implementation of http download based on okhttp
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/download/HttpFileDownloadCallback.java | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/download/HttpFileDownloadCallback.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/download/HttpFileDownloadCallback.java new file mode 100644 index 00000000..2c31754c --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/download/HttpFileDownloadCallback.java @@ -0,0 +1,57 @@ +package de.thedevstack.conversationsplus.services.filetransfer.http.download; + +import java.io.IOException; +import java.io.OutputStream; + +import de.thedevstack.android.logcat.Logging; +import de.thedevstack.conversationsplus.entities.DownloadableFile; +import de.thedevstack.conversationsplus.enums.FileStatus; +import de.thedevstack.conversationsplus.persistance.FileBackend; +import de.thedevstack.conversationsplus.services.AbstractConnectionManager; +import de.thedevstack.conversationsplus.utils.MessageUtil; +import de.thedevstack.conversationsplus.utils.StreamUtil; +import de.thedevstack.conversationsplus.utils.XmppConnectionServiceAccessor; +import okhttp3.Call; +import okhttp3.Callback; +import okhttp3.Response; + +/** + * + */ +public class HttpFileDownloadCallback implements Callback { + private HttpDownloadFileTransferEntity entity; + + public HttpFileDownloadCallback(HttpDownloadFileTransferEntity entity) { + this.entity = entity; + } + + + @Override + public void onFailure(Call call, IOException e) { + changeStatus(FileStatus.DOWNLOAD_FAILED); + } + + @Override + public void onResponse(Call call, Response response) throws IOException { + if (response.isSuccessful()) { + Logging.d("http-download", "Receiving file from remote host"); + DownloadableFile file = this.entity.getFile(); + OutputStream os = AbstractConnectionManager.createOutputStream(file, true); + os.write(response.body().bytes()); + StreamUtil.close(os); + FileBackend.updateMediaScanner(file, XmppConnectionServiceAccessor.xmppConnectionService); + this.entity.transferred(); + changeStatus(FileStatus.DOWNLOADED); + } else { + Logging.e("http-download", "Failed to retrieve file from remote host. HTTP response: " + response.code() + ", " + response.body().string()); + changeStatus(FileStatus.DOWNLOAD_FAILED); + } + if (entity.wakeLock.isHeld()) { + entity.wakeLock.release(); + } + } + + private void changeStatus(FileStatus status) { + MessageUtil.setAndSaveFileStatus(this.entity.getMessage(), status); + } +} |