aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java
diff options
context:
space:
mode:
authorlookshe <github@lookshe.org>2016-03-20 02:14:45 +0100
committerlookshe <github@lookshe.org>2016-03-20 02:14:45 +0100
commitc9b7ed3d3f6cde5e78bf0fa7a77eae81f13cbc09 (patch)
tree84fcd6dca2e2b1904eca61fad16a85e465b87fae /src/main/java
parent98e61f189c5f8bce7a001997fa2352c0c27f2778 (diff)
parentd89d7a300058e87a4dbf1b1f5b3fe9275ebcb2ae (diff)
Merge branch 'trz/rebase' into trz/merge_1.11.1
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/exceptions/RemoteFileNotFoundException.java20
-rw-r--r--src/main/java/eu/siacs/conversations/entities/Message.java40
-rw-r--r--src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java51
-rw-r--r--src/main/java/eu/siacs/conversations/parser/MessageParser.java14
-rw-r--r--src/main/java/eu/siacs/conversations/persistance/FileBackend.java4
-rw-r--r--src/main/java/eu/siacs/conversations/ui/ConversationFragment.java2
6 files changed, 94 insertions, 37 deletions
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/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java
index 184d7e56..8d302634 100644
--- a/src/main/java/eu/siacs/conversations/entities/Message.java
+++ b/src/main/java/eu/siacs/conversations/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/eu/siacs/conversations/http/HttpDownloadConnection.java b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java
index 6d8024ee..7463a9a9 100644
--- a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java
+++ b/src/main/java/eu/siacs/conversations/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;
@@ -24,6 +20,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 eu.siacs.conversations.Config;
@@ -181,6 +178,10 @@ public class HttpDownloadConnection implements Transferable {
HttpDownloadConnection.this.acceptedAutomatically = false;
HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
return;
+ } catch (RemoteFileNotFoundException e) {
+ message.setNoDownloadable();
+ cancel();
+ return;
} catch (IOException e) {
Log.d(Config.LOGTAG, "io exception in http file size checker: " + e.getMessage());
if (interactive) {
@@ -205,23 +206,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) {
diff --git a/src/main/java/eu/siacs/conversations/parser/MessageParser.java b/src/main/java/eu/siacs/conversations/parser/MessageParser.java
index f9d8f508..073cc849 100644
--- a/src/main/java/eu/siacs/conversations/parser/MessageParser.java
+++ b/src/main/java/eu/siacs/conversations/parser/MessageParser.java
@@ -432,7 +432,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();
+ }
}
}
@@ -454,17 +457,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
diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java
index 2fc4f3ef..93b68809 100644
--- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java
+++ b/src/main/java/eu/siacs/conversations/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);
}
diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java
index 3c45792f..3828e957 100644
--- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java
+++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java
@@ -568,7 +568,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
copyUrl.setVisible(true);
}
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)));
}