show thumbnail while waiting for resized image
[pub/Android/ownCloud.git] / src / com / owncloud / android / datamodel / ThumbnailsCacheManager.java
index 49bda88..0e2a3e7 100644 (file)
@@ -36,7 +36,9 @@ import android.graphics.Bitmap;
 import android.graphics.Bitmap.CompressFormat;
 import android.graphics.BitmapFactory;
 import android.graphics.Point;
+import android.graphics.Canvas;
 import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.ColorDrawable;
 import android.graphics.drawable.Drawable;
 import android.media.ThumbnailUtils;
 import android.net.Uri;
@@ -79,8 +81,8 @@ public class ThumbnailsCacheManager {
 
     public static Bitmap mDefaultImg = 
             BitmapFactory.decodeResource(
-                    MainApp.getAppContext().getResources(), 
-                    DisplayUtils.getFileTypeIconId("image/png", "default.png")
+                    MainApp.getAppContext().getResources(),
+                    R.drawable.file_image
             );
 
     
@@ -210,10 +212,6 @@ public class ThumbnailsCacheManager {
         }
 
         protected void onPostExecute(Bitmap bitmap){
-            if (isCancelled()) {
-                bitmap = null;
-            }
-
             if (bitmap != null) {
                 final ImageView imageView = mImageViewReference.get();
                 final ThumbnailGenerationTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
@@ -232,7 +230,7 @@ public class ThumbnailsCacheManager {
                             }
                         }
                         imageView.setImageBitmap(bitmap);
-                        imageView.setVisibility(View.VISIBLE);
+                        // imageView.setVisibility(View.VISIBLE);
                     }
                 }
             }
@@ -312,6 +310,11 @@ public class ThumbnailsCacheManager {
                             file.getStoragePath(), pxW, pxH);
 
                     if (bitmap != null) {
+                        // Handle PNG
+                        if (file.getMimetype().equalsIgnoreCase("image/png")) {
+                            bitmap = handlePNG(bitmap, pxW);
+                        }
+
                         thumbnail = addThumbnailToCache(imageKey, bitmap, file.getStoragePath(), pxW, pxH);
 
                         file.setNeedsUpdateThumbnail(false);
@@ -331,32 +334,22 @@ public class ThumbnailsCacheManager {
                                 GetMethod get = new GetMethod(uri);
                                 int status = mClient.executeMethod(get);
                                 if (status == HttpStatus.SC_OK) {
-//                                    byte[] bytes = get.getResponseBody();
-//                                    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0,
-//                                            bytes.length);
                                     InputStream inputStream = get.getResponseBodyAsStream();
                                     Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                                     thumbnail = ThumbnailUtils.extractThumbnail(bitmap, pxW, pxH);
                                     byte[] bytes = get.getResponseBody();
 
-                                    String type = "";
-                                    if (mIsThumbnail){
-                                        type = "Thumbnail";
-                                    } else {
-                                        type = "Resized image";
-                                    }
-                                    Log_OC.d("Thumbnail",
-                                            type + " size of " + file.getRemotePath()
-                                                 + ": " + bytes.length);
-
-                                    // bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
-
                                     if (mIsThumbnail) {
                                         thumbnail = ThumbnailUtils.extractThumbnail(bitmap, pxW, pxH);
                                     } else {
                                         thumbnail = bitmap;
                                     }
 
+                                    // Handle PNG
+                                    if (file.getMimetype().equalsIgnoreCase("image/png")) {
+                                        thumbnail = handlePNG(thumbnail, pxW);
+                                    }
+
                                     // Add thumbnail to cache
                                     if (thumbnail != null) {
                                         addBitmapToCache(imageKey, thumbnail);
@@ -376,14 +369,34 @@ public class ThumbnailsCacheManager {
 
         }
 
+        private Bitmap handlePNG(Bitmap bitmap, int px){
+            Bitmap resultBitmap = Bitmap.createBitmap(px,
+                    px,
+                    Bitmap.Config.ARGB_8888);
+            Canvas c = new Canvas(resultBitmap);
+
+            c.drawColor(MainApp.getAppContext().getResources().
+                    getColor(R.color.background_color));
+            c.drawBitmap(bitmap, 0, 0, null);
+
+            return resultBitmap;
+        }
+
         private Bitmap doFileInBackground(Boolean mIsThumbnail) {
-            Bitmap thumbnail = null;
             File file = (File)mFile;
 
-            final String imageKey = String.valueOf(file.hashCode());
+            // distinguish between thumbnail and resized image
+            String temp = String.valueOf(file.hashCode());
+            if (mIsThumbnail){
+                temp = "t" + temp;
+            } else {
+                temp = "r" + temp;
+            }
+
+            final String imageKey = temp;
 
             // Check disk cache in background thread
-            thumbnail = getBitmapFromDiskCache(imageKey);
+            Bitmap thumbnail = getBitmapFromDiskCache(imageKey);
 
             // Not found in disk cache
             if (thumbnail == null) {
@@ -418,6 +431,7 @@ public class ThumbnailsCacheManager {
             if (bitmapData == null || bitmapData != file) {
                 // Cancel previous task
                 bitmapWorkerTask.cancel(true);
+                Log_OC.v(TAG, "Cancelled generation of thumbnail for a reused imageView");
             } else {
                 // The same work is already in progress
                 return false;