From 8263e073362aed2ebfda910df8ddad1e84302db1 Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Sun, 19 Oct 2014 23:42:53 +0200 Subject: [PATCH] mtm support for image downloader --- .gitmodules | 2 +- libs/MemorizingTrustManager | 2 +- res/values/strings.xml | 1 + .../conversations/entities/Downloadable.java | 1 + .../conversations/http/HttpConnection.java | 62 +++++++++++++++---- .../ui/adapter/ConversationAdapter.java | 2 + .../ui/adapter/MessageAdapter.java | 7 ++- 7 files changed, 60 insertions(+), 17 deletions(-) diff --git a/.gitmodules b/.gitmodules index d0f561660..7ddaefe68 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,4 +7,4 @@ url = https://github.com/open-keychain/openpgp-api-lib.git [submodule "libs/MemorizingTrustManager"] path = libs/MemorizingTrustManager - url = https://github.com/ge0rg/MemorizingTrustManager + url = https://github.com/iNPUTmice/MemorizingTrustManager.git diff --git a/libs/MemorizingTrustManager b/libs/MemorizingTrustManager index 3f67eba2e..fad835037 160000 --- a/libs/MemorizingTrustManager +++ b/libs/MemorizingTrustManager @@ -1 +1 @@ -Subproject commit 3f67eba2e4663841dd0024908f8ffb2613078140 +Subproject commit fad835037adc1bd313bb56b694426fca4eb67346 diff --git a/res/values/strings.xml b/res/values/strings.xml index b08b793e1..bc3fc6e42 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -269,5 +269,6 @@ Checking image on HTTP host The image file has been deleted You are not connected. Try again later + Check image file size diff --git a/src/eu/siacs/conversations/entities/Downloadable.java b/src/eu/siacs/conversations/entities/Downloadable.java index 36584ab09..70516b204 100644 --- a/src/eu/siacs/conversations/entities/Downloadable.java +++ b/src/eu/siacs/conversations/entities/Downloadable.java @@ -11,6 +11,7 @@ public interface Downloadable { public static final int STATUS_OFFER = 0x203; public static final int STATUS_DOWNLOADING = 0x204; public static final int STATUS_DELETED = 0x205; + public static final int STATUS_OFFER_CHECK_FILESIZE = 0x206; public boolean start(); diff --git a/src/eu/siacs/conversations/http/HttpConnection.java b/src/eu/siacs/conversations/http/HttpConnection.java index 0254dc2f0..3eb8bb84a 100644 --- a/src/eu/siacs/conversations/http/HttpConnection.java +++ b/src/eu/siacs/conversations/http/HttpConnection.java @@ -6,15 +6,18 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLHandshakeException; +import javax.net.ssl.X509TrustManager; import android.content.Intent; import android.graphics.BitmapFactory; import android.net.Uri; -import android.util.Log; -import eu.siacs.conversations.Config; import eu.siacs.conversations.entities.Downloadable; import eu.siacs.conversations.entities.DownloadableFile; import eu.siacs.conversations.entities.Message; @@ -39,8 +42,12 @@ public class HttpConnection implements Downloadable { @Override public boolean start() { if (mXmppConnectionService.hasInternetConnection()) { - changeStatus(STATUS_DOWNLOADING); - new Thread(new FileDownloader()).start(); + if (this.mStatus == STATUS_OFFER_CHECK_FILESIZE) { + checkFileSize(true); + } else { + changeStatus(STATUS_DOWNLOADING); + new Thread(new FileDownloader()).start(); + } return true; } else { return false; @@ -52,18 +59,18 @@ public class HttpConnection implements Downloadable { this.message.setDownloadable(this); try { mUrl = new URL(message.getBody()); - this.file = mXmppConnectionService.getFileBackend() - .getFile(message, false); + this.file = mXmppConnectionService.getFileBackend().getFile( + message, false); this.mAutostart = true; - checkFileSize(); + checkFileSize(false); } catch (MalformedURLException e) { this.cancel(); } } - private void checkFileSize() { + private void checkFileSize(boolean interactive) { changeStatus(STATUS_CHECKING); - new Thread(new FileSizeChecker()).start(); + new Thread(new FileSizeChecker(interactive)).start(); } public void cancel() { @@ -84,33 +91,62 @@ public class HttpConnection implements Downloadable { this.mStatus = status; mXmppConnectionService.updateConversationUi(); } + + private void setupTrustManager(HttpsURLConnection connection, boolean interactive) { + X509TrustManager trustManager; + if (interactive) { + trustManager = mXmppConnectionService.getMemorizingTrustManager(); + } else { + trustManager = mXmppConnectionService.getMemorizingTrustManager().getNonInteractive(); + } + try { + SSLContext sc = SSLContext.getInstance("TLS"); + sc.init(null,new X509TrustManager[] { trustManager },mXmppConnectionService.getRNG()); + connection.setSSLSocketFactory(sc.getSocketFactory()); + } catch (KeyManagementException e) { + return; + } catch (NoSuchAlgorithmException e) { + return; + } + } private class FileSizeChecker implements Runnable { + + private boolean interactive = false; + + public FileSizeChecker(boolean interactive) { + this.interactive = interactive; + } @Override public void run() { long size; try { size = retrieveFileSize(); + } catch (SSLHandshakeException e) { + changeStatus(STATUS_OFFER_CHECK_FILESIZE); + return; } catch (IOException e) { cancel(); return; } file.setExpectedSize(size); - if (size <= mHttpConnectionManager.getAutoAcceptFileSize() && mAutostart) { + if (size <= mHttpConnectionManager.getAutoAcceptFileSize() + && mAutostart) { start(); } else { changeStatus(STATUS_OFFER); } } - private long retrieveFileSize() throws IOException { + private long retrieveFileSize() throws IOException, SSLHandshakeException { HttpURLConnection connection = (HttpURLConnection) mUrl .openConnection(); connection.setRequestMethod("HEAD"); if (connection instanceof HttpsURLConnection) { - + setupTrustManager((HttpsURLConnection) connection, interactive); } + connection.connect(); String contentLength = connection.getHeaderField("Content-Length"); if (contentLength == null) { throw new IOException(); @@ -141,7 +177,7 @@ public class HttpConnection implements Downloadable { HttpURLConnection connection = (HttpURLConnection) mUrl .openConnection(); if (connection instanceof HttpsURLConnection) { - + setupTrustManager((HttpsURLConnection) connection, true); } BufferedInputStream is = new BufferedInputStream( connection.getInputStream()); diff --git a/src/eu/siacs/conversations/ui/adapter/ConversationAdapter.java b/src/eu/siacs/conversations/ui/adapter/ConversationAdapter.java index bd37f7fcb..20c7bee21 100644 --- a/src/eu/siacs/conversations/ui/adapter/ConversationAdapter.java +++ b/src/eu/siacs/conversations/ui/adapter/ConversationAdapter.java @@ -92,6 +92,8 @@ public class ConversationAdapter extends ArrayAdapter { mLastMessage.setText(R.string.receiving_image); } else if (d.getStatus() == Downloadable.STATUS_OFFER) { mLastMessage.setText(R.string.image_offered_for_download); + } else if (d.getStatus() == Downloadable.STATUS_OFFER_CHECK_FILESIZE) { + mLastMessage.setText(R.string.image_offered_for_download); } else if (d.getStatus() == Downloadable.STATUS_DELETED) { mLastMessage.setText(R.string.image_file_deleted); } else { diff --git a/src/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/eu/siacs/conversations/ui/adapter/MessageAdapter.java index 6b2a00949..db783b7f6 100644 --- a/src/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -264,10 +264,11 @@ public class MessageAdapter extends ArrayAdapter { } private void displayDownloadableMessage(ViewHolder viewHolder, - final Message message) { + final Message message, int resid) { viewHolder.image.setVisibility(View.GONE); viewHolder.messageBody.setVisibility(View.GONE); viewHolder.download_button.setVisibility(View.VISIBLE); + viewHolder.download_button.setText(resid); viewHolder.download_button.setOnClickListener(new OnClickListener() { @Override @@ -493,7 +494,9 @@ public class MessageAdapter extends ArrayAdapter { && d.getStatus() == Downloadable.STATUS_DELETED) { displayInfoMessage(viewHolder, R.string.image_file_deleted); } else if (d != null && d.getStatus() == Downloadable.STATUS_OFFER) { - displayDownloadableMessage(viewHolder, item); + displayDownloadableMessage(viewHolder, item,R.string.download_image); + } else if (d != null && d.getStatus() == Downloadable.STATUS_OFFER_CHECK_FILESIZE) { + displayDownloadableMessage(viewHolder, item,R.string.check_image_filesize); } else if ((item.getEncryption() == Message.ENCRYPTION_DECRYPTED) || (item.getEncryption() == Message.ENCRYPTION_NONE) || (item.getEncryption() == Message.ENCRYPTION_OTR)) {