From 645139eb68cb966b90d36790e5b60c0bc6727c78 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 25 Apr 2014 23:06:20 +0200 Subject: couple of more optimazations on image loading --- .../conversations/persistance/FileBackend.java | 43 +--------------------- .../conversations/ui/ConversationActivity.java | 35 ++++++++++-------- .../conversations/ui/ConversationFragment.java | 6 +-- .../xmpp/jingle/JingleConnection.java | 16 ++++++-- 4 files changed, 38 insertions(+), 62 deletions(-) (limited to 'src/eu/siacs') diff --git a/src/eu/siacs/conversations/persistance/FileBackend.java b/src/eu/siacs/conversations/persistance/FileBackend.java index 129176a4..c451b906 100644 --- a/src/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/eu/siacs/conversations/persistance/FileBackend.java @@ -118,10 +118,10 @@ public class FileBackend { .getAbsolutePath()); } - public Bitmap getThumbnail(Message message, int size) + public Bitmap getThumbnail(Message message, int size, boolean cacheOnly) throws FileNotFoundException { Bitmap thumbnail = thumbnailCache.get(message.getUuid()); - if (thumbnail == null) { + if ((thumbnail == null)&&(!cacheOnly)) { Bitmap fullsize = BitmapFactory.decodeFile(getJingleFile(message) .getAbsolutePath()); if (fullsize == null) { @@ -132,45 +132,6 @@ public class FileBackend { } return thumbnail; } - - public void getThumbnailAsync(final Message message, final int size, ImageView imageView, TextView textView) { - - Bitmap thumbnail = thumbnailCache.get(message.getUuid()); - if (thumbnail == null) { - final WeakReference image = new WeakReference(imageView); - final WeakReference text = new WeakReference(textView); - new Thread(new Runnable() { - - @Override - public void run() { - if (image.get()!=null) { - image.get().setVisibility(View.GONE); - } - if (text.get()!=null) { - text.get().setVisibility(View.VISIBLE); - text.get().setText("loading image"); - } - Bitmap fullsize = BitmapFactory.decodeFile(getJingleFile(message) - .getAbsolutePath()); - if (fullsize!=null) { - Bitmap thumbnail = resize(fullsize, size); - thumbnailCache.put(message.getUuid(), thumbnail); - if (image.get()!=null) { - image.get().setVisibility(View.VISIBLE); - image.get().setImageBitmap(thumbnail); - } - if (text.get()!=null) { - text.get().setVisibility(View.GONE); - } - } - } - }).start(); - } else { - textView.setVisibility(View.GONE); - imageView.setVisibility(View.VISIBLE); - imageView.setImageBitmap(thumbnail); - } - } public void removeFiles(Conversation conversation) { String prefix = context.getFilesDir().getAbsolutePath(); diff --git a/src/eu/siacs/conversations/ui/ConversationActivity.java b/src/eu/siacs/conversations/ui/ConversationActivity.java index 32e3588b..91fde31e 100644 --- a/src/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/eu/siacs/conversations/ui/ConversationActivity.java @@ -649,42 +649,51 @@ public class ConversationActivity extends XmppActivity { private Message message = null; public BitmapWorkerTask(ImageView imageView) { - // Use a WeakReference to ensure the ImageView can be garbage collected imageViewReference = new WeakReference(imageView); } - // Decode image in background. @Override protected Bitmap doInBackground(Message... params) { message = params[0]; try { - return xmppConnectionService.getFileBackend().getThumbnail(message, (int) (metrics.density * 288)); + return xmppConnectionService.getFileBackend().getThumbnail(message, (int) (metrics.density * 288),false); } catch (FileNotFoundException e) { Log.d("xmppService","file not found!"); return null; } } - // Once complete, see if ImageView is still around and set bitmap. @Override protected void onPostExecute(Bitmap bitmap) { if (imageViewReference != null && bitmap != null) { final ImageView imageView = imageViewReference.get(); if (imageView != null) { imageView.setImageBitmap(bitmap); + imageView.setBackgroundColor(0x00000000); } } } } public void loadBitmap(Message message, ImageView imageView) { - if (cancelPotentialWork(message, imageView)) { - final BitmapWorkerTask task = new BitmapWorkerTask(imageView); - final AsyncDrawable asyncDrawable = - new AsyncDrawable(getResources(), null, task); - imageView.setImageDrawable(asyncDrawable); - task.execute(message); - } + Bitmap bm; + try { + bm = xmppConnectionService.getFileBackend().getThumbnail(message, (int) (metrics.density * 288), true); + } catch (FileNotFoundException e) { + bm = null; + } + if (bm!=null) { + imageView.setImageBitmap(bm); + } else { + if (cancelPotentialWork(message, imageView)) { + imageView.setBackgroundColor(0xff333333); + final BitmapWorkerTask task = new BitmapWorkerTask(imageView); + final AsyncDrawable asyncDrawable = + new AsyncDrawable(getResources(), null, task); + imageView.setImageDrawable(asyncDrawable); + task.execute(message); + } + } } public static boolean cancelPotentialWork(Message message, ImageView imageView) { @@ -692,16 +701,12 @@ public class ConversationActivity extends XmppActivity { if (bitmapWorkerTask != null) { final Message oldMessage = bitmapWorkerTask.message; - // If bitmapData is not yet set or it differs from the new data if (oldMessage == null || message != oldMessage) { - // Cancel previous task bitmapWorkerTask.cancel(true); } else { - // The same work is already in progress return false; } } - // No task associated with the ImageView, or an existing task was cancelled return true; } diff --git a/src/eu/siacs/conversations/ui/ConversationFragment.java b/src/eu/siacs/conversations/ui/ConversationFragment.java index d42b33c4..5ade8a53 100644 --- a/src/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/eu/siacs/conversations/ui/ConversationFragment.java @@ -301,16 +301,16 @@ public class ConversationFragment extends Fragment { viewHolder.image.setVisibility(View.VISIBLE); String[] params = item.getBody().split(","); if (params.length==3) { - int target = (int) (metrics.density * 288); + double target = metrics.density * 288; int w = Integer.parseInt(params[1]); int h = Integer.parseInt(params[2]); int scalledW; int scalledH; if (w <= h) { scalledW = (int) (w / ((double) h / target)); - scalledH = target; + scalledH = (int) target; } else { - scalledW = target; + scalledW = (int) target; scalledH = (int) (h / ((double) w / target)); } viewHolder.image.setLayoutParams(new LinearLayout.LayoutParams(scalledW, scalledH)); diff --git a/src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java b/src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java index 0959031b..4f383f52 100644 --- a/src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java +++ b/src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java @@ -7,6 +7,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map.Entry; +import android.graphics.BitmapFactory; import android.util.Log; import eu.siacs.conversations.entities.Account; @@ -78,10 +79,16 @@ public class JingleConnection { if (acceptedAutomatically) { message.markUnread(); } + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeFile(file.getAbsolutePath(),options); + int imageHeight = options.outHeight; + int imageWidth = options.outWidth; + message.setBody(""+file.getSize()+","+imageWidth+","+imageHeight); mXmppConnectionService.databaseBackend.createMessage(message); mXmppConnectionService.markMessage(message, Message.STATUS_RECIEVED); } - Log.d("xmppService","sucessfully transmitted file. sha1:"+file.getSha1Sum()); + Log.d("xmppService","sucessfully transmitted file. sha1:"+file.getSha1Sum()+" "+message.getBody()); } }; @@ -197,7 +204,7 @@ public class JingleConnection { public void init(Account account, JinglePacket packet) { this.status = STATUS_INITIATED; Conversation conversation = this.mXmppConnectionService.findOrCreateConversation(account, packet.getFrom().split("/")[0], false); - this.message = new Message(conversation, "receiving image file", Message.ENCRYPTION_NONE); + this.message = new Message(conversation, "", Message.ENCRYPTION_NONE); this.message.setType(Message.TYPE_IMAGE); this.message.setStatus(Message.STATUS_RECEIVED_OFFER); this.message.setJingleConnection(this); @@ -230,6 +237,7 @@ public class JingleConnection { } if (supportedFile) { this.file.setExpectedSize(Long.parseLong(fileSize.getContent())); + message.setBody(""+this.file.getExpectedSize()); conversation.getMessages().add(message); if (this.file.getExpectedSize()<=this.mJingleConnectionManager.getAutoAcceptFileSize()) { Log.d("xmppService","auto accepting file from "+packet.getFrom()); @@ -413,7 +421,7 @@ public class JingleConnection { this.status = STATUS_TRANSMITTING; if (connection.needsActivation()) { if (connection.getCandidate().isOurs()) { - Log.d("xmppService","candidate "+connection.getCandidate().getCid()+" was our proxy and needs activation"); + Log.d("xmppService","candidate "+connection.getCandidate().getCid()+" was our proxy. going to activate"); IqPacket activation = new IqPacket(IqPacket.TYPE_SET); activation.setTo(connection.getCandidate().getJid()); activation.query("http://jabber.org/protocol/bytestreams").setAttribute("sid", this.getSessionId()); @@ -430,6 +438,8 @@ public class JingleConnection { } } }); + } else { + Log.d("xmppService","candidate "+connection.getCandidate().getCid()+" was a proxy. waiting for other party to activate"); } } else { if (initiator.equals(account.getFullJid())) { -- cgit v1.2.3