provide more detailed error toasts for http file download

This commit is contained in:
Daniel Gultsch 2015-08-10 12:15:14 +02:00
parent d2ded8ad50
commit b5e90850d8
2 changed files with 61 additions and 38 deletions

View File

@ -6,6 +6,7 @@ import android.util.Log;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
@ -125,6 +126,17 @@ public class HttpDownloadConnection implements Transferable {
mXmppConnectionService.updateConversationUi(); 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 class FileSizeChecker implements Runnable {
private boolean interactive = false; private boolean interactive = false;
@ -146,7 +158,7 @@ public class HttpDownloadConnection implements Transferable {
} catch (IOException e) { } catch (IOException e) {
Log.d(Config.LOGTAG, "io exception in http file size checker: " + e.getMessage()); Log.d(Config.LOGTAG, "io exception in http file size checker: " + e.getMessage());
if (interactive) { if (interactive) {
mXmppConnectionService.showErrorToastInUi(R.string.file_not_found_on_remote_host); showToastForException(e);
} }
cancel(); cancel();
return; return;
@ -163,6 +175,7 @@ public class HttpDownloadConnection implements Transferable {
} }
private long retrieveFileSize() throws IOException { private long retrieveFileSize() throws IOException {
try {
Log.d(Config.LOGTAG, "retrieve file size. interactive:" + String.valueOf(interactive)); Log.d(Config.LOGTAG, "retrieve file size. interactive:" + String.valueOf(interactive));
changeStatus(STATUS_CHECKING); changeStatus(STATUS_CHECKING);
HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection(); HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
@ -172,11 +185,13 @@ public class HttpDownloadConnection implements Transferable {
} }
connection.connect(); connection.connect();
String contentLength = connection.getHeaderField("Content-Length"); String contentLength = connection.getHeaderField("Content-Length");
connection.disconnect();
if (contentLength == null) { if (contentLength == null) {
throw new IOException(); throw new IOException();
} }
try {
return Long.parseLong(contentLength, 10); return Long.parseLong(contentLength, 10);
} catch (IOException e) {
throw e;
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new IOException(); throw new IOException();
} }
@ -202,22 +217,24 @@ public class HttpDownloadConnection implements Transferable {
updateImageBounds(); updateImageBounds();
finish(); finish();
} catch (SSLHandshakeException e) { } catch (SSLHandshakeException e) {
FileBackend.close(os);
changeStatus(STATUS_OFFER); changeStatus(STATUS_OFFER);
} catch (IOException e) { } catch (IOException e) {
FileBackend.close(os); if (interactive) {
mXmppConnectionService.showErrorToastInUi(R.string.file_not_found_on_remote_host); showToastForException(e);
}
cancel(); cancel();
} }
} }
private void download() throws IOException { private void download() throws IOException {
InputStream is = null;
try {
HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection(); HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
if (connection instanceof HttpsURLConnection) { if (connection instanceof HttpsURLConnection) {
mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive); mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
} }
connection.connect(); connection.connect();
BufferedInputStream is = new BufferedInputStream(connection.getInputStream()); is = new BufferedInputStream(connection.getInputStream());
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
file.createNewFile(); file.createNewFile();
os = AbstractConnectionManager.createOutputStream(file, true); os = AbstractConnectionManager.createOutputStream(file, true);
@ -231,8 +248,12 @@ public class HttpDownloadConnection implements Transferable {
updateProgress((int) ((((double) transmitted) / expected) * 100)); updateProgress((int) ((((double) transmitted) / expected) * 100));
} }
os.flush(); os.flush();
os.close(); } catch (IOException e) {
is.close(); throw e;
} finally {
FileBackend.close(os);
FileBackend.close(is);
}
} }
private void updateImageBounds() { private void updateImageBounds() {

View File

@ -494,11 +494,13 @@
<string name="none">None</string> <string name="none">None</string>
<string name="recently_used">Most recently used</string> <string name="recently_used">Most recently used</string>
<string name="choose_quick_action">Choose quick action</string> <string name="choose_quick_action">Choose quick action</string>
<string name="file_not_found_on_remote_host">File not found on remote server</string>
<string name="search_for_contacts_or_groups">Search for contacts or groups</string> <string name="search_for_contacts_or_groups">Search for contacts or groups</string>
<string name="send_private_message">Send private message</string> <string name="send_private_message">Send private message</string>
<string name="user_has_left_conference">%s has left the conference!</string> <string name="user_has_left_conference">%s has left the conference!</string>
<string name="username">Username</string> <string name="username">Username</string>
<string name="username_hint">Username</string> <string name="username_hint">Username</string>
<string name="invalid_username">This is not a valid username</string> <string name="invalid_username">This is not a valid username</string>
<string name="download_failed_server_not_found">Download failed: Server not found</string>
<string name="download_failed_file_not_found">Download failed: File not found</string>
<string name="download_failed_could_not_connect">Download failed: Could not connect to host</string>
</resources> </resources>