read image bounds on downloaded image

This commit is contained in:
iNPUTmice 2014-10-14 18:16:03 +02:00
parent 470410b389
commit e98460771b
6 changed files with 104 additions and 31 deletions

View file

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

View file

@ -110,9 +110,7 @@ public class PgpEngine {
+ ',' + imageWidth + ',' + imageHeight); + ',' + imageWidth + ',' + imageHeight);
message.setEncryption(Message.ENCRYPTION_DECRYPTED); message.setEncryption(Message.ENCRYPTION_DECRYPTED);
PgpEngine.this.mXmppConnectionService PgpEngine.this.mXmppConnectionService
.updateMessage(message); .updateMessage(message);;
PgpEngine.this.mXmppConnectionService
.updateConversationUi();
callback.success(message); callback.success(message);
return; return;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:

View file

@ -9,12 +9,12 @@ import eu.siacs.conversations.R;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.text.InputFilter.LengthFilter;
public class Message extends AbstractEntity { public class Message extends AbstractEntity {
public static final String TABLENAME = "messages"; 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_RECEPTION_FAILED = -3;
public static final int STATUS_RECEIVED_OFFER = -2; public static final int STATUS_RECEIVED_OFFER = -2;
public static final int STATUS_RECEIVING = -1; public static final int STATUS_RECEIVING = -1;
@ -396,4 +396,67 @@ public class Message extends AbstractEntity {
return false; 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;
}
} }

View file

@ -9,6 +9,7 @@ import java.net.URL;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import android.graphics.BitmapFactory;
import android.util.Log; import android.util.Log;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
@ -43,7 +44,7 @@ public class HttpConnection implements Downloadable {
mUrl = new URL(message.getBody()); mUrl = new URL(message.getBody());
this.file = mXmppConnectionService.getFileBackend().getConversationsFile(message,false); this.file = mXmppConnectionService.getFileBackend().getConversationsFile(message,false);
message.setType(Message.TYPE_IMAGE); message.setType(Message.TYPE_IMAGE);
mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVED_OFFER); mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVED_CHECKING);
checkFileSize(); checkFileSize();
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
this.cancel(); this.cancel();
@ -66,10 +67,14 @@ public class HttpConnection implements Downloadable {
try { try {
long size = retrieveFileSize(); long size = retrieveFileSize();
file.setExpectedSize(size); file.setExpectedSize(size);
message.setBody(mUrl.toString()+","+String.valueOf(size));
if (size <= mHttpConnectionManager.getAutoAcceptFileSize()) { if (size <= mHttpConnectionManager.getAutoAcceptFileSize()) {
mXmppConnectionService.updateMessage(message);
start(); start();
} else {
message.setStatus(Message.STATUS_RECEIVED_OFFER);
mXmppConnectionService.updateMessage(message);
} }
Log.d(Config.LOGTAG,"file size: "+size);
} catch (IOException e) { } catch (IOException e) {
cancel(); cancel();
} }
@ -101,7 +106,9 @@ public class HttpConnection implements Downloadable {
try { try {
mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVING); mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVING);
download(); download();
mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVED); updateImageBounds();
message.setStatus(Message.STATUS_RECEIVED);
mXmppConnectionService.updateMessage(message);
} catch (IOException e) { } catch (IOException e) {
cancel(); cancel();
} }
@ -122,7 +129,17 @@ public class HttpConnection implements Downloadable {
os.flush(); os.flush();
os.close(); os.close();
is.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);
} }
} }

View file

@ -1249,6 +1249,7 @@ public class XmppConnectionService extends Service {
public void updateMessage(Message message) { public void updateMessage(Message message) {
databaseBackend.updateMessage(message); databaseBackend.updateMessage(message);
updateConversationUi();
} }
protected void syncDirtyContacts(Account account) { protected void syncDirtyContacts(Account account) {

View file

@ -9,6 +9,7 @@ import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Downloadable; import eu.siacs.conversations.entities.Downloadable;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.Message.ImageParams;
import eu.siacs.conversations.ui.ConversationActivity; import eu.siacs.conversations.ui.ConversationActivity;
import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.UIHelper;
import android.content.Context; import android.content.Context;
@ -102,13 +103,7 @@ public class MessageAdapter extends ArrayAdapter<Message> {
boolean multiReceived = message.getConversation().getMode() == Conversation.MODE_MULTI boolean multiReceived = message.getConversation().getMode() == Conversation.MODE_MULTI
&& message.getMergedStatus() <= Message.STATUS_RECEIVED; && message.getMergedStatus() <= Message.STATUS_RECEIVED;
if (message.getType() == Message.TYPE_IMAGE) { if (message.getType() == Message.TYPE_IMAGE) {
String[] fileParams = message.getBody().split(","); filesize = message.getImageParams().size / 1024 + " KB";
try {
long size = Long.parseLong(fileParams[0]);
filesize = size / 1024 + " KB";
} catch (NumberFormatException e) {
filesize = "0 KB";
}
} }
switch (message.getMergedStatus()) { switch (message.getMergedStatus()) {
case Message.STATUS_WAITING: case Message.STATUS_WAITING:
@ -275,23 +270,19 @@ public class MessageAdapter extends ArrayAdapter<Message> {
} }
viewHolder.messageBody.setVisibility(View.GONE); viewHolder.messageBody.setVisibility(View.GONE);
viewHolder.image.setVisibility(View.VISIBLE); viewHolder.image.setVisibility(View.VISIBLE);
String[] fileParams = message.getBody().split(","); ImageParams params = message.getImageParams();
if (fileParams.length == 3) {
double target = metrics.density * 288; double target = metrics.density * 288;
int w = Integer.parseInt(fileParams[1]);
int h = Integer.parseInt(fileParams[2]);
int scalledW; int scalledW;
int scalledH; int scalledH;
if (w <= h) { if (params.width <= params.height) {
scalledW = (int) (w / ((double) h / target)); scalledW = (int) (params.width / ((double) params.height / target));
scalledH = (int) target; scalledH = (int) target;
} else { } else {
scalledW = (int) target; scalledW = (int) target;
scalledH = (int) (h / ((double) w / target)); scalledH = (int) (params.height / ((double) params.width / target));
} }
viewHolder.image.setLayoutParams(new LinearLayout.LayoutParams( viewHolder.image.setLayoutParams(new LinearLayout.LayoutParams(
scalledW, scalledH)); scalledW, scalledH));
}
activity.loadBitmap(message, viewHolder.image); activity.loadBitmap(message, viewHolder.image);
viewHolder.image.setOnClickListener(new OnClickListener() { viewHolder.image.setOnClickListener(new OnClickListener() {
@ -481,6 +472,8 @@ public class MessageAdapter extends ArrayAdapter<Message> {
if (item.getType() == Message.TYPE_IMAGE) { if (item.getType() == Message.TYPE_IMAGE) {
if (item.getStatus() == Message.STATUS_RECEIVING) { if (item.getStatus() == Message.STATUS_RECEIVING) {
displayInfoMessage(viewHolder, R.string.receiving_image); 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) { } else if (item.getStatus() == Message.STATUS_RECEIVED_OFFER) {
viewHolder.image.setVisibility(View.GONE); viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.GONE); viewHolder.messageBody.setVisibility(View.GONE);