aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java
diff options
context:
space:
mode:
authorlookshe <github@lookshe.org>2015-08-24 22:34:53 +0200
committerlookshe <github@lookshe.org>2015-08-24 22:34:53 +0200
commitbceedba4963adea4de14dab4f96635d9fbf2777f (patch)
tree6044fdbb2bf98bab193c026bfaaf9225ccf5353a /src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java
parentef0f8d4507391dafba2fc9d2b691a117e84b40fc (diff)
parent5a53ff80fbaafb405f88cb4949bb46d8ffc450cc (diff)
Merge tag '1.6.3' into trz/rebasetrz/merge_1.6.3
Conflicts: CHANGELOG.md README.md art/render.rb build.gradle src/main/java/eu/siacs/conversations/Config.java src/main/java/eu/siacs/conversations/crypto/OtrService.java src/main/java/eu/siacs/conversations/crypto/PgpEngine.java src/main/java/eu/siacs/conversations/entities/Account.java src/main/java/eu/siacs/conversations/entities/Contact.java src/main/java/eu/siacs/conversations/entities/DownloadableFile.java src/main/java/eu/siacs/conversations/entities/Message.java src/main/java/eu/siacs/conversations/generator/AbstractGenerator.java src/main/java/eu/siacs/conversations/generator/IqGenerator.java src/main/java/eu/siacs/conversations/generator/MessageGenerator.java src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java src/main/java/eu/siacs/conversations/http/HttpUploadConnection.java src/main/java/eu/siacs/conversations/parser/AbstractParser.java src/main/java/eu/siacs/conversations/parser/MessageParser.java src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java src/main/java/eu/siacs/conversations/persistance/FileBackend.java src/main/java/eu/siacs/conversations/services/NotificationService.java src/main/java/eu/siacs/conversations/services/XmppConnectionService.java src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java src/main/java/eu/siacs/conversations/ui/ConversationActivity.java src/main/java/eu/siacs/conversations/ui/ConversationFragment.java src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java src/main/java/eu/siacs/conversations/ui/SettingsActivity.java src/main/java/eu/siacs/conversations/ui/XmppActivity.java src/main/java/eu/siacs/conversations/ui/adapter/AccountAdapter.java src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java src/main/java/eu/siacs/conversations/ui/adapter/ListItemAdapter.java src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java src/main/java/eu/siacs/conversations/utils/CryptoHelper.java src/main/java/eu/siacs/conversations/utils/DNSHelper.java src/main/java/eu/siacs/conversations/utils/MimeUtils.java src/main/java/eu/siacs/conversations/utils/UIHelper.java src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java src/main/java/eu/siacs/conversations/xmpp/jingle/JingleInbandTransport.java src/main/java/eu/siacs/conversations/xmpp/jingle/JingleSocks5Transport.java src/main/java/eu/siacs/conversations/xmpp/stanzas/MessagePacket.java src/main/res/layout/account_row.xml src/main/res/layout/activity_edit_account.xml src/main/res/layout/activity_muc_details.xml src/main/res/layout/contact.xml src/main/res/layout/contact_key.xml src/main/res/layout/conversation_list_row.xml src/main/res/layout/fragment_conversation.xml src/main/res/layout/message_received.xml src/main/res/layout/message_sent.xml src/main/res/values-bg/strings.xml src/main/res/values-ca/strings.xml src/main/res/values-cs/strings.xml src/main/res/values-de/strings.xml src/main/res/values-el/strings.xml src/main/res/values-es/strings.xml src/main/res/values-eu/strings.xml src/main/res/values-id/strings.xml src/main/res/values-it/strings.xml src/main/res/values-ja/strings.xml src/main/res/values-ko/strings.xml src/main/res/values-nl/strings.xml src/main/res/values-pl/strings.xml src/main/res/values-pt/strings.xml src/main/res/values-ru/strings.xml src/main/res/values-sk/strings.xml src/main/res/values-sr/strings.xml src/main/res/values-sv/strings.xml src/main/res/values-v21/themes.xml src/main/res/values-zh-rCN/strings.xml src/main/res/values/arrays.xml src/main/res/values/colors.xml src/main/res/values/dimens.xml src/main/res/values/strings.xml src/main/res/xml/preferences.xml
Diffstat (limited to 'src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java')
-rw-r--r--src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java94
1 files changed, 58 insertions, 36 deletions
diff --git a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java
index 45f1e965..356f9417 100644
--- a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java
+++ b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java
@@ -2,11 +2,12 @@ package eu.siacs.conversations.http;
import android.content.Intent;
import android.net.Uri;
-import android.os.SystemClock;
+import android.os.PowerManager;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
@@ -18,9 +19,11 @@ import javax.net.ssl.SSLHandshakeException;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
-import eu.siacs.conversations.entities.Transferable;
import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.entities.Message;
+import eu.siacs.conversations.entities.Transferable;
+import eu.siacs.conversations.persistance.FileBackend;
+import eu.siacs.conversations.services.AbstractConnectionManager;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.CryptoHelper;
@@ -35,7 +38,6 @@ public class HttpDownloadConnection implements Transferable {
private int mStatus = Transferable.STATUS_UNKNOWN;
private boolean acceptedAutomatically = false;
private int mProgress = 0;
- private long mLastGuiRefresh = 0;
public HttpDownloadConnection(HttpConnectionManager manager) {
this.mHttpConnectionManager = manager;
@@ -70,7 +72,8 @@ public class HttpDownloadConnection implements Transferable {
String secondToLast = parts.length >= 2 ? parts[parts.length -2] : null;
if ("pgp".equals(lastPart) || "gpg".equals(lastPart)) {
this.message.setEncryption(Message.ENCRYPTION_PGP);
- } else if (message.getEncryption() != Message.ENCRYPTION_OTR) {
+ } else if (message.getEncryption() != Message.ENCRYPTION_OTR
+ && message.getEncryption() != Message.ENCRYPTION_AXOLOTL) {
this.message.setEncryption(Message.ENCRYPTION_NONE);
}
String extension;
@@ -83,10 +86,11 @@ public class HttpDownloadConnection implements Transferable {
this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
String reference = mUrl.getRef();
if (reference != null && reference.length() == 96) {
- this.file.setKey(CryptoHelper.hexToBytes(reference));
+ this.file.setKeyAndIv(CryptoHelper.hexToBytes(reference));
}
- if (this.message.getEncryption() == Message.ENCRYPTION_OTR
+ if ((this.message.getEncryption() == Message.ENCRYPTION_OTR
+ || this.message.getEncryption() == Message.ENCRYPTION_AXOLOTL)
&& this.file.getKey() == null) {
this.message.setEncryption(Message.ENCRYPTION_NONE);
}
@@ -123,6 +127,17 @@ public class HttpDownloadConnection implements Transferable {
mXmppConnectionService.updateConversationUi();
}
+ private void showToastForException(Exception e) {
+ e.printStackTrace();
+ if (e instanceof java.net.UnknownHostException) {
+ mXmppConnectionService.showErrorToastInUi(R.string.download_failed_server_not_found);
+ } else if (e instanceof java.net.ConnectException) {
+ mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_connect);
+ } else {
+ mXmppConnectionService.showErrorToastInUi(R.string.download_failed_file_not_found);
+ }
+ }
+
private class FileSizeChecker implements Runnable {
private boolean interactive = false;
@@ -144,7 +159,7 @@ public class HttpDownloadConnection implements Transferable {
} catch (IOException e) {
Log.d(Config.LOGTAG, "io exception in http file size checker: " + e.getMessage());
if (interactive) {
- mXmppConnectionService.showErrorToastInUi(R.string.file_not_found_on_remote_host);
+ showToastForException(e);
}
cancel();
return;
@@ -186,6 +201,8 @@ public class HttpDownloadConnection implements Transferable {
private boolean interactive = false;
+ private OutputStream os;
+
public FileDownloader(boolean interactive) {
this.interactive = interactive;
}
@@ -200,36 +217,44 @@ public class HttpDownloadConnection implements Transferable {
} catch (SSLHandshakeException e) {
changeStatus(STATUS_OFFER);
} catch (IOException e) {
- mXmppConnectionService.showErrorToastInUi(R.string.file_not_found_on_remote_host);
+ if (interactive) {
+ showToastForException(e);
+ }
cancel();
}
}
- private void download() throws SSLHandshakeException, IOException {
- HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
- if (connection instanceof HttpsURLConnection) {
- mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
- }
- connection.connect();
- BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
- file.getParentFile().mkdirs();
- file.createNewFile();
- OutputStream os = file.createOutputStream();
- if (os == null) {
- throw new IOException();
- }
- long transmitted = 0;
- long expected = file.getExpectedSize();
- int count = -1;
- byte[] buffer = new byte[1024];
- while ((count = is.read(buffer)) != -1) {
- transmitted += count;
- os.write(buffer, 0, count);
- updateProgress((int) ((((double) transmitted) / expected) * 100));
+ private void download() throws IOException {
+ InputStream is = null;
+ PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_download_"+message.getUuid());
+ try {
+ wakeLock.acquire();
+ HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
+ if (connection instanceof HttpsURLConnection) {
+ mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
+ }
+ connection.connect();
+ is = new BufferedInputStream(connection.getInputStream());
+ file.getParentFile().mkdirs();
+ file.createNewFile();
+ os = AbstractConnectionManager.createOutputStream(file, true);
+ long transmitted = 0;
+ long expected = file.getExpectedSize();
+ int count = -1;
+ byte[] buffer = new byte[1024];
+ while ((count = is.read(buffer)) != -1) {
+ transmitted += count;
+ os.write(buffer, 0, count);
+ updateProgress((int) ((((double) transmitted) / expected) * 100));
+ }
+ os.flush();
+ } catch (IOException e) {
+ throw e;
+ } finally {
+ FileBackend.close(os);
+ FileBackend.close(is);
+ wakeLock.release();
}
- os.flush();
- os.close();
- is.close();
}
private void updateImageBounds() {
@@ -242,10 +267,7 @@ public class HttpDownloadConnection implements Transferable {
public void updateProgress(int i) {
this.mProgress = i;
- if (SystemClock.elapsedRealtime() - this.mLastGuiRefresh > Config.PROGRESS_UI_UPDATE_INTERVAL) {
- this.mLastGuiRefresh = SystemClock.elapsedRealtime();
- mXmppConnectionService.updateConversationUi();
- }
+ mXmppConnectionService.updateConversationUi();
}
@Override