read image bounds on downloaded image

iNPUTmice created

Change summary

res/values/strings.xml                                         |  1 
src/eu/siacs/conversations/crypto/PgpEngine.java               |  4 
src/eu/siacs/conversations/entities/Message.java               | 65 +++
src/eu/siacs/conversations/http/HttpConnection.java            | 25 +
src/eu/siacs/conversations/services/XmppConnectionService.java |  1 
src/eu/siacs/conversations/ui/adapter/MessageAdapter.java      | 39 -
6 files changed, 104 insertions(+), 31 deletions(-)

Detailed changes

res/values/strings.xml 🔗

@@ -266,5 +266,6 @@
     <string name="conference_members_only">This conference is members only</string>
     <string name="conference_kicked">You have been kicked from this conference</string>
     <string name="using_account">using account %s</string>
+    <string name="checking_image">Checking image on HTTP host</string>
 
 </resources>

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:

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

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

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<Message> {
 		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<Message> {
 		}
 		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<Message> {
 		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);