made httpconnection (download) ready all kind of files

This commit is contained in:
Daniel Gultsch 2015-06-30 17:15:02 +02:00
parent 6af97c724c
commit aca9d8036c
9 changed files with 74 additions and 69 deletions

View File

@ -98,7 +98,7 @@ public class PgpEngine {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE,
OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
URL url = message.getImageParams().url;
URL url = message.getFileParams().url;
mXmppConnectionService.getFileBackend().updateFileParams(message,url);
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
PgpEngine.this.mXmppConnectionService
@ -147,7 +147,7 @@ public class PgpEngine {
params.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
String body;
if (message.hasFileOnRemoteHost()) {
body = message.getImageParams().url.toString();
body = message.getFileParams().url.toString();
} else {
body = message.getBody();
}

View File

@ -485,12 +485,12 @@ public class Message extends AbstractEntity {
return body != null && UIHelper.HEARTS.contains(body.trim());
}
public ImageParams getImageParams() {
ImageParams params = getLegacyImageParams();
public FileParams getFileParams() {
FileParams params = getLegacyFileParams();
if (params != null) {
return params;
}
params = new ImageParams();
params = new FileParams();
if (this.downloadable != null) {
params.size = this.downloadable.getFileSize();
}
@ -498,18 +498,42 @@ public class Message extends AbstractEntity {
return params;
}
String parts[] = body.split("\\|");
if (parts.length == 1) {
switch (parts.length) {
case 1:
try {
params.size = Long.parseLong(parts[0]);
} catch (NumberFormatException e) {
params.origin = parts[0];
try {
params.url = new URL(parts[0]);
} catch (MalformedURLException e1) {
params.url = null;
}
}
} else if (parts.length == 3) {
break;
case 2:
case 4:
try {
params.url = new URL(parts[0]);
} catch (MalformedURLException e1) {
params.url = null;
}
try {
params.size = Long.parseLong(parts[1]);
} catch (NumberFormatException e) {
params.size = 0;
}
try {
params.width = Integer.parseInt(parts[2]);
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
params.width = 0;
}
try {
params.height = Integer.parseInt(parts[3]);
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
params.height = 0;
}
break;
case 3:
try {
params.size = Long.parseLong(parts[0]);
} catch (NumberFormatException e) {
@ -525,34 +549,13 @@ public class Message extends AbstractEntity {
} catch (NumberFormatException e) {
params.height = 0;
}
} else if (parts.length == 4) {
params.origin = parts[0];
try {
params.url = new URL(parts[0]);
} catch (MalformedURLException e1) {
params.url = null;
}
try {
params.size = Long.parseLong(parts[1]);
} catch (NumberFormatException e) {
params.size = 0;
}
try {
params.width = Integer.parseInt(parts[2]);
} catch (NumberFormatException e) {
params.width = 0;
}
try {
params.height = Integer.parseInt(parts[3]);
} catch (NumberFormatException e) {
params.height = 0;
}
break;
}
return params;
}
public ImageParams getLegacyImageParams() {
ImageParams params = new ImageParams();
public FileParams getLegacyFileParams() {
FileParams params = new FileParams();
if (body == null) {
return params;
}
@ -589,18 +592,17 @@ public class Message extends AbstractEntity {
}
public boolean hasFileOnRemoteHost() {
return isFileOrImage() && getImageParams().url != null;
return isFileOrImage() && getFileParams().url != null;
}
public boolean needsUploading() {
return isFileOrImage() && getImageParams().url == null;
return isFileOrImage() && getFileParams().url == null;
}
public class ImageParams {
public class FileParams {
public URL url;
public long size = 0;
public int width = 0;
public int height = 0;
public String origin;
}
}

View File

@ -75,7 +75,7 @@ public class MessageGenerator extends AbstractGenerator {
try {
String content;
if (message.hasFileOnRemoteHost()) {
content = message.getImageParams().url.toString();
content = message.getFileParams().url.toString();
} else {
content = message.getBody();
}
@ -93,7 +93,7 @@ public class MessageGenerator extends AbstractGenerator {
public MessagePacket generateChat(Message message, boolean addDelay) {
MessagePacket packet = preparePacket(message, addDelay);
if (message.hasFileOnRemoteHost()) {
packet.setBody(message.getImageParams().url.toString());
packet.setBody(message.getFileParams().url.toString());
} else {
packet.setBody(message.getBody());
}

View File

@ -269,7 +269,7 @@ public class HttpConnection implements Downloadable {
}
private void updateImageBounds() {
message.setType(Message.TYPE_IMAGE);
message.setType(Message.TYPE_FILE);
mXmppConnectionService.getFileBackend().updateFileParams(message, mUrl);
mXmppConnectionService.updateMessage(message);
}

View File

@ -154,11 +154,11 @@ public class HttpUploadConnection implements Downloadable {
int code = connection.getResponseCode();
if (code == 200) {
Log.d(Config.LOGTAG, "finished uploading file");
Message.ImageParams params = message.getImageParams();
Message.FileParams params = message.getFileParams();
if (key != null) {
mGetUrl = new URL(mGetUrl.toString() + "#" + CryptoHelper.bytesToHex(key));
}
message.setBody(mGetUrl.toString()+"|"+String.valueOf(params.size)+"|"+String.valueOf(params.width)+"|"+String.valueOf(params.height));
mXmppConnectionService.getFileBackend().updateFileParams(message, mGetUrl);
message.setDownloadable(null);
message.setCounterpart(message.getConversation().getJid().toBareJid());
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {

View File

@ -499,7 +499,11 @@ public class FileBackend {
message.setBody(url.toString()+"|"+Long.toString(file.getSize()) + '|' + imageWidth + '|' + imageHeight);
}
} else {
if (url != null) {
message.setBody(url.toString()+"|"+Long.toString(file.getSize()));
} else {
message.setBody(Long.toString(file.getSize()));
}
}
}

View File

@ -543,7 +543,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
url = message.getBody();
} else {
resId = R.string.image_url;
url = message.getImageParams().url.toString();
url = message.getFileParams().url.toString();
}
if (activity.copyTextToClipboard(url, resId)) {
Toast.makeText(activity, R.string.url_copied_to_clipboard,

View File

@ -3,7 +3,6 @@ package eu.siacs.conversations.ui.adapter;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
@ -69,7 +68,7 @@ public class ConversationAdapter extends ArrayAdapter<Conversation> {
convName.setTypeface(null, Typeface.NORMAL);
}
if (message.getImageParams().width > 0
if (message.getFileParams().width > 0
&& (message.getDownloadable() == null
|| message.getDownloadable().getStatus() != Downloadable.STATUS_DELETED)) {
mLastMessage.setVisibility(View.GONE);

View File

@ -32,7 +32,7 @@ import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Downloadable;
import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.Message.ImageParams;
import eu.siacs.conversations.entities.Message.FileParams;
import eu.siacs.conversations.ui.ConversationActivity;
import eu.siacs.conversations.utils.GeoHelper;
import eu.siacs.conversations.utils.UIHelper;
@ -100,7 +100,7 @@ public class MessageAdapter extends ArrayAdapter<Message> {
boolean multiReceived = message.getConversation().getMode() == Conversation.MODE_MULTI
&& message.getMergedStatus() <= Message.STATUS_RECEIVED;
if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE || message.getDownloadable() != null) {
ImageParams params = message.getImageParams();
FileParams params = message.getFileParams();
if (params.size > (1.5 * 1024 * 1024)) {
filesize = params.size / (1024 * 1024)+ " MiB";
} else if (params.size > 0) {
@ -339,7 +339,7 @@ public class MessageAdapter extends ArrayAdapter<Message> {
}
viewHolder.messageBody.setVisibility(View.GONE);
viewHolder.image.setVisibility(View.VISIBLE);
ImageParams params = message.getImageParams();
FileParams params = message.getFileParams();
double target = metrics.density * 288;
int scalledW;
int scalledH;
@ -494,7 +494,7 @@ public class MessageAdapter extends ArrayAdapter<Message> {
} else if (message.getType() == Message.TYPE_IMAGE && message.getEncryption() != Message.ENCRYPTION_PGP && message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED) {
displayImageMessage(viewHolder, message);
} else if (message.getType() == Message.TYPE_FILE && message.getEncryption() != Message.ENCRYPTION_PGP && message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED) {
if (message.getImageParams().width > 0) {
if (message.getFileParams().width > 0) {
displayImageMessage(viewHolder,message);
} else {
displayOpenableMessage(viewHolder, message);