mtm support for image downloader

This commit is contained in:
iNPUTmice 2014-10-19 23:42:53 +02:00
parent 44f9022d95
commit 8263e07336
7 changed files with 60 additions and 17 deletions

2
.gitmodules vendored
View File

@ -7,4 +7,4 @@
url = https://github.com/open-keychain/openpgp-api-lib.git url = https://github.com/open-keychain/openpgp-api-lib.git
[submodule "libs/MemorizingTrustManager"] [submodule "libs/MemorizingTrustManager"]
path = libs/MemorizingTrustManager path = libs/MemorizingTrustManager
url = https://github.com/ge0rg/MemorizingTrustManager url = https://github.com/iNPUTmice/MemorizingTrustManager.git

@ -1 +1 @@
Subproject commit 3f67eba2e4663841dd0024908f8ffb2613078140 Subproject commit fad835037adc1bd313bb56b694426fca4eb67346

View File

@ -269,5 +269,6 @@
<string name="checking_image">Checking image on HTTP host</string> <string name="checking_image">Checking image on HTTP host</string>
<string name="image_file_deleted">The image file has been deleted</string> <string name="image_file_deleted">The image file has been deleted</string>
<string name="not_connected_try_again">You are not connected. Try again later</string> <string name="not_connected_try_again">You are not connected. Try again later</string>
<string name="check_image_filesize">Check image file size</string>
</resources> </resources>

View File

@ -11,6 +11,7 @@ public interface Downloadable {
public static final int STATUS_OFFER = 0x203; public static final int STATUS_OFFER = 0x203;
public static final int STATUS_DOWNLOADING = 0x204; public static final int STATUS_DOWNLOADING = 0x204;
public static final int STATUS_DELETED = 0x205; public static final int STATUS_DELETED = 0x205;
public static final int STATUS_OFFER_CHECK_FILESIZE = 0x206;
public boolean start(); public boolean start();

View File

@ -6,15 +6,18 @@ import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.HttpsURLConnection; 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.content.Intent;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.util.Log;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Downloadable; import eu.siacs.conversations.entities.Downloadable;
import eu.siacs.conversations.entities.DownloadableFile; import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
@ -39,8 +42,12 @@ public class HttpConnection implements Downloadable {
@Override @Override
public boolean start() { public boolean start() {
if (mXmppConnectionService.hasInternetConnection()) { if (mXmppConnectionService.hasInternetConnection()) {
if (this.mStatus == STATUS_OFFER_CHECK_FILESIZE) {
checkFileSize(true);
} else {
changeStatus(STATUS_DOWNLOADING); changeStatus(STATUS_DOWNLOADING);
new Thread(new FileDownloader()).start(); new Thread(new FileDownloader()).start();
}
return true; return true;
} else { } else {
return false; return false;
@ -52,18 +59,18 @@ public class HttpConnection implements Downloadable {
this.message.setDownloadable(this); this.message.setDownloadable(this);
try { try {
mUrl = new URL(message.getBody()); mUrl = new URL(message.getBody());
this.file = mXmppConnectionService.getFileBackend() this.file = mXmppConnectionService.getFileBackend().getFile(
.getFile(message, false); message, false);
this.mAutostart = true; this.mAutostart = true;
checkFileSize(); checkFileSize(false);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
this.cancel(); this.cancel();
} }
} }
private void checkFileSize() { private void checkFileSize(boolean interactive) {
changeStatus(STATUS_CHECKING); changeStatus(STATUS_CHECKING);
new Thread(new FileSizeChecker()).start(); new Thread(new FileSizeChecker(interactive)).start();
} }
public void cancel() { public void cancel() {
@ -85,32 +92,61 @@ public class HttpConnection implements Downloadable {
mXmppConnectionService.updateConversationUi(); 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 class FileSizeChecker implements Runnable {
private boolean interactive = false;
public FileSizeChecker(boolean interactive) {
this.interactive = interactive;
}
@Override @Override
public void run() { public void run() {
long size; long size;
try { try {
size = retrieveFileSize(); size = retrieveFileSize();
} catch (SSLHandshakeException e) {
changeStatus(STATUS_OFFER_CHECK_FILESIZE);
return;
} catch (IOException e) { } catch (IOException e) {
cancel(); cancel();
return; return;
} }
file.setExpectedSize(size); file.setExpectedSize(size);
if (size <= mHttpConnectionManager.getAutoAcceptFileSize() && mAutostart) { if (size <= mHttpConnectionManager.getAutoAcceptFileSize()
&& mAutostart) {
start(); start();
} else { } else {
changeStatus(STATUS_OFFER); changeStatus(STATUS_OFFER);
} }
} }
private long retrieveFileSize() throws IOException { private long retrieveFileSize() throws IOException, SSLHandshakeException {
HttpURLConnection connection = (HttpURLConnection) mUrl HttpURLConnection connection = (HttpURLConnection) mUrl
.openConnection(); .openConnection();
connection.setRequestMethod("HEAD"); connection.setRequestMethod("HEAD");
if (connection instanceof HttpsURLConnection) { if (connection instanceof HttpsURLConnection) {
setupTrustManager((HttpsURLConnection) connection, interactive);
} }
connection.connect();
String contentLength = connection.getHeaderField("Content-Length"); String contentLength = connection.getHeaderField("Content-Length");
if (contentLength == null) { if (contentLength == null) {
throw new IOException(); throw new IOException();
@ -141,7 +177,7 @@ public class HttpConnection implements Downloadable {
HttpURLConnection connection = (HttpURLConnection) mUrl HttpURLConnection connection = (HttpURLConnection) mUrl
.openConnection(); .openConnection();
if (connection instanceof HttpsURLConnection) { if (connection instanceof HttpsURLConnection) {
setupTrustManager((HttpsURLConnection) connection, true);
} }
BufferedInputStream is = new BufferedInputStream( BufferedInputStream is = new BufferedInputStream(
connection.getInputStream()); connection.getInputStream());

View File

@ -92,6 +92,8 @@ public class ConversationAdapter extends ArrayAdapter<Conversation> {
mLastMessage.setText(R.string.receiving_image); mLastMessage.setText(R.string.receiving_image);
} else if (d.getStatus() == Downloadable.STATUS_OFFER) { } else if (d.getStatus() == Downloadable.STATUS_OFFER) {
mLastMessage.setText(R.string.image_offered_for_download); 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) { } else if (d.getStatus() == Downloadable.STATUS_DELETED) {
mLastMessage.setText(R.string.image_file_deleted); mLastMessage.setText(R.string.image_file_deleted);
} else { } else {

View File

@ -264,10 +264,11 @@ public class MessageAdapter extends ArrayAdapter<Message> {
} }
private void displayDownloadableMessage(ViewHolder viewHolder, private void displayDownloadableMessage(ViewHolder viewHolder,
final Message message) { final Message message, int resid) {
viewHolder.image.setVisibility(View.GONE); viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.GONE); viewHolder.messageBody.setVisibility(View.GONE);
viewHolder.download_button.setVisibility(View.VISIBLE); viewHolder.download_button.setVisibility(View.VISIBLE);
viewHolder.download_button.setText(resid);
viewHolder.download_button.setOnClickListener(new OnClickListener() { viewHolder.download_button.setOnClickListener(new OnClickListener() {
@Override @Override
@ -493,7 +494,9 @@ public class MessageAdapter extends ArrayAdapter<Message> {
&& d.getStatus() == Downloadable.STATUS_DELETED) { && d.getStatus() == Downloadable.STATUS_DELETED) {
displayInfoMessage(viewHolder, R.string.image_file_deleted); displayInfoMessage(viewHolder, R.string.image_file_deleted);
} else if (d != null && d.getStatus() == Downloadable.STATUS_OFFER) { } 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) } else if ((item.getEncryption() == Message.ENCRYPTION_DECRYPTED)
|| (item.getEncryption() == Message.ENCRYPTION_NONE) || (item.getEncryption() == Message.ENCRYPTION_NONE)
|| (item.getEncryption() == Message.ENCRYPTION_OTR)) { || (item.getEncryption() == Message.ENCRYPTION_OTR)) {