From e98460771bac4e2252556ea6cf483c93a1ecf176 Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Tue, 14 Oct 2014 18:16:03 +0200 Subject: read image bounds on downloaded image --- src/eu/siacs/conversations/crypto/PgpEngine.java | 4 +- src/eu/siacs/conversations/entities/Message.java | 65 +++++++++++++++++++++- .../siacs/conversations/http/HttpConnection.java | 25 +++++++-- .../services/XmppConnectionService.java | 1 + .../conversations/ui/adapter/MessageAdapter.java | 39 ++++++------- 5 files changed, 103 insertions(+), 31 deletions(-) (limited to 'src/eu/siacs') diff --git a/src/eu/siacs/conversations/crypto/PgpEngine.java b/src/eu/siacs/conversations/crypto/PgpEngine.java index 9eab7b6d..c70f0907 100644 --- a/src/eu/siacs/conversations/crypto/PgpEngine.java +++ b/src/eu/siacs/conversations/crypto/PgpEngine.java @@ -110,9 +110,7 @@ public class PgpEngine { + ',' + imageWidth + ',' + imageHeight); message.setEncryption(Message.ENCRYPTION_DECRYPTED); PgpEngine.this.mXmppConnectionService - .updateMessage(message); - PgpEngine.this.mXmppConnectionService - .updateConversationUi(); + .updateMessage(message);; callback.success(message); return; case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: diff --git a/src/eu/siacs/conversations/entities/Message.java b/src/eu/siacs/conversations/entities/Message.java index b459510c..863288bb 100644 --- a/src/eu/siacs/conversations/entities/Message.java +++ b/src/eu/siacs/conversations/entities/Message.java @@ -9,12 +9,12 @@ import eu.siacs.conversations.R; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; -import android.text.InputFilter.LengthFilter; public class Message extends AbstractEntity { public static final String TABLENAME = "messages"; + public static final int STATUS_RECEIVED_CHECKING = -4; public static final int STATUS_RECEPTION_FAILED = -3; public static final int STATUS_RECEIVED_OFFER = -2; public static final int STATUS_RECEIVING = -1; @@ -396,4 +396,67 @@ public class Message extends AbstractEntity { return false; } } + + public ImageParams getImageParams() { + ImageParams params = new ImageParams(); + if (body==null) { + return params; + } + String parts[] = body.split(","); + if (parts.length==1) { + try { + params.size = Long.parseLong(parts[0]); + } catch (NumberFormatException e) { + params.origin = parts[0]; + } + } else if (parts.length == 2) { + params.origin = parts[0]; + try { + params.size = Long.parseLong(parts[1]); + } catch (NumberFormatException e) { + params.size = 0; + } + } else if (parts.length==3) { + try { + params.size = Long.parseLong(parts[0]); + } catch (NumberFormatException e) { + params.size = 0; + } + try { + params.width = Integer.parseInt(parts[1]); + } catch (NumberFormatException e) { + params.width = 0; + } + try { + params.height = Integer.parseInt(parts[2]); + } catch (NumberFormatException e) { + params.height = 0; + } + } else if (parts.length == 4) { + params.origin = parts[0]; + 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; + } + } + return params; + } + + public class ImageParams { + public long size = 0; + public int width = 0; + public int height = 0; + public String origin; + } } diff --git a/src/eu/siacs/conversations/http/HttpConnection.java b/src/eu/siacs/conversations/http/HttpConnection.java index 003007d8..7ed98136 100644 --- a/src/eu/siacs/conversations/http/HttpConnection.java +++ b/src/eu/siacs/conversations/http/HttpConnection.java @@ -9,6 +9,7 @@ import java.net.URL; import javax.net.ssl.HttpsURLConnection; +import android.graphics.BitmapFactory; import android.util.Log; import eu.siacs.conversations.Config; @@ -43,7 +44,7 @@ public class HttpConnection implements Downloadable { mUrl = new URL(message.getBody()); this.file = mXmppConnectionService.getFileBackend().getConversationsFile(message,false); message.setType(Message.TYPE_IMAGE); - mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVED_OFFER); + mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVED_CHECKING); checkFileSize(); } catch (MalformedURLException e) { this.cancel(); @@ -66,10 +67,14 @@ public class HttpConnection implements Downloadable { try { long size = retrieveFileSize(); file.setExpectedSize(size); + message.setBody(mUrl.toString()+","+String.valueOf(size)); if (size <= mHttpConnectionManager.getAutoAcceptFileSize()) { + mXmppConnectionService.updateMessage(message); start(); + } else { + message.setStatus(Message.STATUS_RECEIVED_OFFER); + mXmppConnectionService.updateMessage(message); } - Log.d(Config.LOGTAG,"file size: "+size); } catch (IOException e) { cancel(); } @@ -101,7 +106,9 @@ public class HttpConnection implements Downloadable { try { mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVING); download(); - mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVED); + updateImageBounds(); + message.setStatus(Message.STATUS_RECEIVED); + mXmppConnectionService.updateMessage(message); } catch (IOException e) { cancel(); } @@ -122,7 +129,17 @@ public class HttpConnection implements Downloadable { os.flush(); os.close(); is.close(); - Log.d(Config.LOGTAG,"finished downloading "+file.getAbsolutePath().toString()); + } + + private void updateImageBounds() { + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeFile(file.getAbsolutePath(), options); + int imageHeight = options.outHeight; + int imageWidth = options.outWidth; + message.setBody(mUrl.toString()+","+file.getSize() + ',' + + imageWidth + ',' + imageHeight); + } } diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index 0dda4b31..5b0d9ea5 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -1249,6 +1249,7 @@ public class XmppConnectionService extends Service { public void updateMessage(Message message) { databaseBackend.updateMessage(message); + updateConversationUi(); } protected void syncDirtyContacts(Account account) { diff --git a/src/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/eu/siacs/conversations/ui/adapter/MessageAdapter.java index dbf2184b..581a410c 100644 --- a/src/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -9,6 +9,7 @@ import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Downloadable; import eu.siacs.conversations.entities.Message; +import eu.siacs.conversations.entities.Message.ImageParams; import eu.siacs.conversations.ui.ConversationActivity; import eu.siacs.conversations.utils.UIHelper; import android.content.Context; @@ -102,13 +103,7 @@ public class MessageAdapter extends ArrayAdapter { boolean multiReceived = message.getConversation().getMode() == Conversation.MODE_MULTI && message.getMergedStatus() <= Message.STATUS_RECEIVED; if (message.getType() == Message.TYPE_IMAGE) { - String[] fileParams = message.getBody().split(","); - try { - long size = Long.parseLong(fileParams[0]); - filesize = size / 1024 + " KB"; - } catch (NumberFormatException e) { - filesize = "0 KB"; - } + filesize = message.getImageParams().size / 1024 + " KB"; } switch (message.getMergedStatus()) { case Message.STATUS_WAITING: @@ -275,23 +270,19 @@ public class MessageAdapter extends ArrayAdapter { } viewHolder.messageBody.setVisibility(View.GONE); viewHolder.image.setVisibility(View.VISIBLE); - String[] fileParams = message.getBody().split(","); - if (fileParams.length == 3) { - double target = metrics.density * 288; - int w = Integer.parseInt(fileParams[1]); - int h = Integer.parseInt(fileParams[2]); - int scalledW; - int scalledH; - if (w <= h) { - scalledW = (int) (w / ((double) h / target)); - scalledH = (int) target; - } else { - scalledW = (int) target; - scalledH = (int) (h / ((double) w / target)); - } - viewHolder.image.setLayoutParams(new LinearLayout.LayoutParams( - scalledW, scalledH)); + ImageParams params = message.getImageParams(); + double target = metrics.density * 288; + int scalledW; + int scalledH; + if (params.width <= params.height) { + scalledW = (int) (params.width / ((double) params.height / target)); + scalledH = (int) target; + } else { + scalledW = (int) target; + scalledH = (int) (params.height / ((double) params.width / target)); } + viewHolder.image.setLayoutParams(new LinearLayout.LayoutParams( + scalledW, scalledH)); activity.loadBitmap(message, viewHolder.image); viewHolder.image.setOnClickListener(new OnClickListener() { @@ -481,6 +472,8 @@ public class MessageAdapter extends ArrayAdapter { if (item.getType() == Message.TYPE_IMAGE) { if (item.getStatus() == Message.STATUS_RECEIVING) { displayInfoMessage(viewHolder, R.string.receiving_image); + } else if (item.getStatus() == Message.STATUS_RECEIVED_CHECKING) { + displayInfoMessage(viewHolder, R.string.checking_image); } else if (item.getStatus() == Message.STATUS_RECEIVED_OFFER) { viewHolder.image.setVisibility(View.GONE); viewHolder.messageBody.setVisibility(View.GONE); -- cgit v1.2.3