On slow devices even blurhash decode costs enough to do on background thread

Stephen Paul Weber created

Change summary

src/main/java/eu/siacs/conversations/persistance/FileBackend.java |  4 
src/main/java/eu/siacs/conversations/ui/XmppActivity.java         | 15 
2 files changed, 10 insertions(+), 9 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/persistance/FileBackend.java 🔗

@@ -1076,7 +1076,7 @@ public class FileBackend {
         }
     }
 
-    public BitmapDrawable getFallbackThumbnail(final Message message, int size) {
+    public BitmapDrawable getFallbackThumbnail(final Message message, int size, boolean cacheOnly) {
         List<Element> thumbs = message.getFileParams() != null ? message.getFileParams().getThumbnails() : null;
         if (thumbs != null && !thumbs.isEmpty()) {
             for (Element thumb : thumbs) {
@@ -1086,7 +1086,7 @@ public class FileBackend {
                     if (parts[0].equals("image/blurhash")) {
                         final LruCache<String, Drawable> cache = mXmppConnectionService.getDrawableCache();
                         BitmapDrawable cached = (BitmapDrawable) cache.get(parts[1]);
-                        if (cached != null) return cached;
+                        if (cached != null || cacheOnly) return cached;
 
                         int width = message.getFileParams().width;
                         if (width < 1 && thumb.getAttribute("width") != null) width = Integer.parseInt(thumb.getAttribute("width"));

src/main/java/eu/siacs/conversations/ui/XmppActivity.java 🔗

@@ -928,7 +928,7 @@ public abstract class XmppActivity extends ActionBarActivity {
                 imageView.setBackgroundColor(0xff333333);
                 imageView.setImageDrawable(null);
                 final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
-                final BitmapDrawable fallbackThumb = xmppConnectionService.getFileBackend().getFallbackThumbnail(message, (int) (metrics.density * 288));
+                final BitmapDrawable fallbackThumb = xmppConnectionService.getFileBackend().getFallbackThumbnail(message, (int) (metrics.density * 288), true);
                 final AsyncDrawable asyncDrawable = new AsyncDrawable(
                         getResources(), fallbackThumb != null ? fallbackThumb.getBitmap() : null, task);
                 imageView.setImageDrawable(asyncDrawable);
@@ -990,17 +990,18 @@ public abstract class XmppActivity extends ActionBarActivity {
             if (isCancelled()) {
                 return null;
             }
+            final XmppActivity activity = find(imageViewReference);
+            Drawable d = null;
             message = params[0];
             try {
-                final XmppActivity activity = find(imageViewReference);
                 if (activity != null && activity.xmppConnectionService != null) {
-                    return activity.xmppConnectionService.getFileBackend().getThumbnail(message, imageViewReference.get().getContext().getResources(), (int) (activity.metrics.density * 288), false);
-                } else {
-                    return null;
+                    d = activity.xmppConnectionService.getFileBackend().getThumbnail(message, imageViewReference.get().getContext().getResources(), (int) (activity.metrics.density * 288), false);
                 }
-            } catch (IOException e) {
-                return null;
+            } catch (IOException e) { }
+            if (activity != null && activity.xmppConnectionService != null) {
+                d =  d == null ? activity.xmppConnectionService.getFileBackend().getFallbackThumbnail(message, (int) (activity.metrics.density * 288), false) : d;
             }
+            return d;
         }
 
         @Override