Merge branch 'trz/rebase' into trz/merge_1.11.1

This commit is contained in:
lookshe 2016-03-20 02:14:45 +01:00
commit c9b7ed3d3f
7 changed files with 96 additions and 38 deletions

View file

@ -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);
}
}

View file

@ -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;
}
}

View file

@ -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) {

View file

@ -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

View file

@ -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);
}

View file

@ -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)));
}

View file

@ -19,6 +19,7 @@
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:visibility="gone"
app:riv_corner_radius="2dp" />
<LinearLayout
@ -26,7 +27,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/message_photo"
android:layout_alignParentRight="true"
android:background="@drawable/message_bubble_sent"
android:minHeight="53dp"
android:layout_marginLeft="-4dp"