From 0de9c2aa23990749360ee0e9f509e13a54b14137 Mon Sep 17 00:00:00 2001 From: lookshe Date: Wed, 16 Mar 2016 18:45:18 +0100 Subject: partially implements FS#161 do not show download button on 404 --- .../exceptions/RemoteFileNotFoundException.java | 20 ++++++++++ .../http/HttpDownloadConnection.java | 46 ++++++++++++++-------- 2 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 src/main/java/de/thedevstack/conversationsplus/exceptions/RemoteFileNotFoundException.java (limited to 'src/main/java') diff --git a/src/main/java/de/thedevstack/conversationsplus/exceptions/RemoteFileNotFoundException.java b/src/main/java/de/thedevstack/conversationsplus/exceptions/RemoteFileNotFoundException.java new file mode 100644 index 00000000..41a548cb --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/exceptions/RemoteFileNotFoundException.java @@ -0,0 +1,20 @@ +package de.thedevstack.conversationsplus.exceptions; + +import java.io.IOException; + +/** + * Created by lookshe on 15.03.16. + * + * Exception class if HTTP status code 404 occured + */ +public class RemoteFileNotFoundException extends IOException { + private static final long serialVersionUID = -1010013599132881427L; + + public RemoteFileNotFoundException() { + super(); + } + + public RemoteFileNotFoundException(Throwable e) { + super(e); + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java b/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java index bbd47089..0e50dd4d 100644 --- a/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java +++ b/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java @@ -24,6 +24,7 @@ import javax.net.ssl.SSLHandshakeException; import de.thedevstack.android.logcat.Logging; import de.thedevstack.conversationsplus.ConversationsPlusApplication; import de.thedevstack.conversationsplus.ConversationsPlusPreferences; +import de.thedevstack.conversationsplus.exceptions.RemoteFileNotFoundException; import de.thedevstack.conversationsplus.utils.MessageUtil; import de.thedevstack.conversationsplus.utils.StreamUtil; import de.thedevstack.conversationsplus.Config; @@ -181,6 +182,9 @@ public class HttpDownloadConnection implements Transferable { HttpDownloadConnection.this.acceptedAutomatically = false; HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message); return; + } catch (RemoteFileNotFoundException e) { + cancel(); + return; } catch (IOException e) { Log.d(Config.LOGTAG, "io exception in http file size checker: " + e.getMessage()); if (interactive) { @@ -205,23 +209,31 @@ public class HttpDownloadConnection implements Transferable { private long retrieveFileSize() throws IOException { try { - Logging.d(Config.LOGTAG, "retrieve file size. interactive:" + String.valueOf(interactive)); - changeStatus(STATUS_CHECKING); - HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection(); - connection.setRequestMethod("HEAD"); - Logging.d(Config.LOGTAG, "url: "+connection.getURL().toString()); - Logging.d(Config.LOGTAG, "connection: "+connection.toString()); - connection.setRequestProperty("User-Agent", ConversationsPlusApplication.getNameAndVersion()); - if (connection instanceof HttpsURLConnection) { - mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive); - } - connection.connect(); - String contentLength = connection.getHeaderField("Content-Length"); - connection.disconnect(); - if (contentLength == null) { - return -1; - } - return Long.parseLong(contentLength, 10); + Logging.d(Config.LOGTAG, "retrieve file size. interactive:" + String.valueOf(interactive)); + changeStatus(STATUS_CHECKING); + HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection(); + connection.setRequestMethod("HEAD"); + Logging.d(Config.LOGTAG, "url: " + connection.getURL().toString()); + Logging.d(Config.LOGTAG, "connection: " + connection.toString()); + connection.setRequestProperty("User-Agent", ConversationsPlusApplication.getNameAndVersion()); + // https://code.google.com/p/android/issues/detail?id=24672 + connection.setRequestProperty("Accept-Encoding", ""); + if (connection instanceof HttpsURLConnection) { + mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive); + } + connection.connect(); + if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { + Logging.d(Config.LOGTAG, "remote file not found"); + throw new RemoteFileNotFoundException(); + } + String contentLength = connection.getHeaderField("Content-Length"); + connection.disconnect(); + if (contentLength == null) { + return -1; + } + return Long.parseLong(contentLength, 10); + } catch (RemoteFileNotFoundException e) { + throw e; } catch (IOException e) { return -1; } catch (NumberFormatException e) { -- cgit v1.2.3 From cb55d2af52c1007b577b1bb1f94a4206aec58ef0 Mon Sep 17 00:00:00 2001 From: lookshe Date: Thu, 17 Mar 2016 22:14:49 +0100 Subject: completely implements FS#161 save state of treatAsDownloadable() and set on error to specified value --- .../conversationsplus/entities/Message.java | 40 +++++++++++++++++----- .../http/HttpDownloadConnection.java | 5 +-- .../conversationsplus/ui/ConversationFragment.java | 10 +++++- 3 files changed, 41 insertions(+), 14 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/de/thedevstack/conversationsplus/entities/Message.java b/src/main/java/de/thedevstack/conversationsplus/entities/Message.java index bb3140c3..8dbb0535 100644 --- a/src/main/java/de/thedevstack/conversationsplus/entities/Message.java +++ b/src/main/java/de/thedevstack/conversationsplus/entities/Message.java @@ -81,6 +81,7 @@ public class Message extends AbstractEntity { private Message mNextMessage = null; private Message mPreviousMessage = null; private String axolotlFingerprint = null; + private Decision mTreatAsDownloadAble = Decision.NOT_DECIDED; private Message() { @@ -467,6 +468,7 @@ public class Message extends AbstractEntity { MUST, SHOULD, NEVER, + NOT_DECIDED, } private String extractRelevantExtension(URL url) { @@ -515,44 +517,64 @@ public class Message extends AbstractEntity { } } + /** + * in case of later found error with decision, set it to a value which does not affect anything, hopefully + */ + public void setNoDownloadable() { + mTreatAsDownloadAble = Decision.NEVER; + } + public Decision treatAsDownloadable() { + // only test this ones, body will not change + if (mTreatAsDownloadAble != Decision.NOT_DECIDED) { + return mTreatAsDownloadAble; + } /** * there are a few cases where spaces result in an unwanted behavior, e.g. * "http://example.com/image.jpg" text that will not be shown /abc.png" * or more than one image link in one message. */ if (getBody().contains(" ")) { - return Decision.NEVER; + mTreatAsDownloadAble = Decision.NEVER; + return mTreatAsDownloadAble; } try { URL url = new URL(body); if (!url.getProtocol().equalsIgnoreCase("http") && !url.getProtocol().equalsIgnoreCase("https")) { - return Decision.NEVER; + mTreatAsDownloadAble = Decision.NEVER; + return mTreatAsDownloadAble; } else if (oob) { - return Decision.MUST; + mTreatAsDownloadAble = Decision.MUST; + return mTreatAsDownloadAble; } String extension = extractRelevantExtension(url); if (extension == null) { - return Decision.NEVER; + mTreatAsDownloadAble = Decision.NEVER; + return mTreatAsDownloadAble; } String ref = url.getRef(); boolean encrypted = ref != null && ref.matches("([A-Fa-f0-9]{2}){48}"); if (encrypted) { if (MimeUtils.guessMimeTypeFromExtension(extension) != null) { - return Decision.MUST; + mTreatAsDownloadAble = Decision.MUST; + return mTreatAsDownloadAble; } else { - return Decision.NEVER; + mTreatAsDownloadAble = Decision.NEVER; + return mTreatAsDownloadAble; } } else if (Transferable.VALID_IMAGE_EXTENSIONS.contains(extension) || Transferable.WELL_KNOWN_EXTENSIONS.contains(extension)) { - return Decision.SHOULD; + mTreatAsDownloadAble = Decision.SHOULD; + return mTreatAsDownloadAble; } else { - return Decision.NEVER; + mTreatAsDownloadAble = Decision.NEVER; + return mTreatAsDownloadAble; } } catch (MalformedURLException e) { - return Decision.NEVER; + mTreatAsDownloadAble = Decision.NEVER; + return mTreatAsDownloadAble; } } diff --git a/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java b/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java index 0e50dd4d..1517e5d1 100644 --- a/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java +++ b/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java @@ -10,12 +10,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; -import java.net.InetAddress; -import java.net.InetSocketAddress; import java.net.MalformedURLException; -import java.net.Proxy; import java.net.URL; -import java.util.Arrays; import java.util.concurrent.CancellationException; import javax.net.ssl.HttpsURLConnection; @@ -183,6 +179,7 @@ public class HttpDownloadConnection implements Transferable { HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message); return; } catch (RemoteFileNotFoundException e) { + message.setNoDownloadable(); cancel(); return; } catch (IOException e) { diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java b/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java index e700d6e9..e35c0f97 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java @@ -561,8 +561,16 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa || (t != null && t instanceof HttpDownloadConnection)) { copyUrl.setVisible(true); } + boolean b1 = true; + b1 &= m.getType() == Message.TYPE_TEXT; + b1 &= t == null; + b1 &= m.treatAsDownloadable() != Message.Decision.NEVER; + boolean b2 = true; + b2 &= m.isFileOrImage(); + b2 &= t instanceof TransferablePlaceholder; + b2 &= m.hasFileOnRemoteHost(); if ((m.getType() == Message.TYPE_TEXT && t == null && m.treatAsDownloadable() != Message.Decision.NEVER) - || (m.isFileOrImage() && t instanceof TransferablePlaceholder && m.hasFileOnRemoteHost())){ + || (t instanceof TransferablePlaceholder && m.hasFileOnRemoteHost())){ downloadFile.setVisible(true); downloadFile.setTitle(activity.getString(R.string.download_x_file,UIHelper.getFileDescriptionString(activity, m))); } -- cgit v1.2.3 From b5d1ee194c25b0e4c0f0ad6c68c4390fbbc85e4c Mon Sep 17 00:00:00 2001 From: lookshe Date: Thu, 17 Mar 2016 23:08:58 +0100 Subject: Fixes FS#156 and FS#135 (occured again) --- .../conversationsplus/parser/MessageParser.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/de/thedevstack/conversationsplus/parser/MessageParser.java b/src/main/java/de/thedevstack/conversationsplus/parser/MessageParser.java index e64fe6a4..39e06bf6 100644 --- a/src/main/java/de/thedevstack/conversationsplus/parser/MessageParser.java +++ b/src/main/java/de/thedevstack/conversationsplus/parser/MessageParser.java @@ -431,7 +431,10 @@ public class MessageParser extends AbstractParser implements account.activateGracePeriod(); } } else { - message.markUnread(); + // only not mam messages should be marked as unread + if (query == null) { + message.markUnread(); + } } } @@ -453,17 +456,20 @@ public class MessageParser extends AbstractParser implements if (message.getEncryption() == Message.ENCRYPTION_NONE || !ConversationsPlusPreferences.dontSaveEncrypted()) { mXmppConnectionService.databaseBackend.createMessage(message); } - final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager(); if (message.trusted() && message.treatAsDownloadable() != Message.Decision.NEVER && ConversationsPlusPreferences.autoAcceptFileSize() > 0 && ConversationsPlusPreferences.autoDownloadFileLink()) { - manager.createNewDownloadConnection(message); + this.mXmppConnectionService.getHttpConnectionManager().createNewDownloadConnection(message); } else { if (query == null) { mXmppConnectionService.getNotificationService().push(message); } else if (query.getWith() == null) { // mam catchup - mXmppConnectionService.getNotificationService().pushFromBacklog(message); + /* + Like suggested in https://bugs.thedevstack.de/task/156 user should be notified + in some other way of loaded messages. + */ + // mXmppConnectionService.getNotificationService().pushFromBacklog(message); } } } else if (!packet.hasChild("body")){ //no body -- cgit v1.2.3 From 67586766919f5d531d610a24331c0302311f8911 Mon Sep 17 00:00:00 2001 From: lookshe Date: Fri, 18 Mar 2016 09:08:18 +0100 Subject: Fixes FS#164 --- .../de/thedevstack/conversationsplus/ui/ConversationFragment.java | 8 -------- 1 file changed, 8 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java b/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java index e35c0f97..1bf95910 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java @@ -561,14 +561,6 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa || (t != null && t instanceof HttpDownloadConnection)) { copyUrl.setVisible(true); } - boolean b1 = true; - b1 &= m.getType() == Message.TYPE_TEXT; - b1 &= t == null; - b1 &= m.treatAsDownloadable() != Message.Decision.NEVER; - boolean b2 = true; - b2 &= m.isFileOrImage(); - b2 &= t instanceof TransferablePlaceholder; - b2 &= m.hasFileOnRemoteHost(); if ((m.getType() == Message.TYPE_TEXT && t == null && m.treatAsDownloadable() != Message.Decision.NEVER) || (t instanceof TransferablePlaceholder && m.hasFileOnRemoteHost())){ downloadFile.setVisible(true); -- cgit v1.2.3 From b99714161b48b276242d484cdeddfb3ef8aef4c4 Mon Sep 17 00:00:00 2001 From: steckbrief Date: Fri, 18 Mar 2016 17:19:37 +0100 Subject: Fixes FS#167: Resized images are larger than original images --- .../de/thedevstack/conversationsplus/persistance/FileBackend.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java b/src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java index 611f573c..07537fe3 100644 --- a/src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java +++ b/src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java @@ -110,7 +110,7 @@ public class FileBackend { } public static DownloadableFile compressImageAndCopyToPrivateStorage(Message message, Bitmap scaledBitmap) throws FileCopyException { - message.setRelativeFilePath(FileBackend.getPrivateImageDirectoryPath() + message.getUuid() + ".png"); + message.setRelativeFilePath(FileBackend.getPrivateImageDirectoryPath() + message.getUuid() + ".jpg"); DownloadableFile file = getFile(message); file.getParentFile().mkdirs(); OutputStream os = null; @@ -118,7 +118,7 @@ public class FileBackend { file.createNewFile(); os = new FileOutputStream(file); - boolean success = scaledBitmap.compress(Bitmap.CompressFormat.PNG, 75, os); + boolean success = scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 75, os); if (!success) { throw new FileCopyException(R.string.error_compressing_image); } -- cgit v1.2.3