- moved rotateImage() to BitmapUtils
[pub/Android/ownCloud.git] / src / com / owncloud / android / datamodel / ThumbnailsCacheManager.java
index 9bff1ff..5472777 100644 (file)
@@ -23,9 +23,11 @@ import java.lang.ref.WeakReference;
 import android.content.res.Resources;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
+import android.graphics.Matrix;
 import android.graphics.Bitmap.CompressFormat;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
+import android.media.ExifInterface;
 import android.media.ThumbnailUtils;
 import android.os.AsyncTask;
 import android.util.TypedValue;
@@ -50,7 +52,7 @@ public class ThumbnailsCacheManager {
     private static final String CACHE_FOLDER = "thumbnailCache"; 
     
     private static final Object mThumbnailsDiskCacheLock = new Object();
-    private static DiskLruImageCache mThumbnailCache;
+    private static DiskLruImageCache mThumbnailCache = null;
     private static boolean mThumbnailCacheStarting = true;
     
     private static final int DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB
@@ -68,23 +70,26 @@ public class ThumbnailsCacheManager {
         @Override
         protected Void doInBackground(File... params) {
             synchronized (mThumbnailsDiskCacheLock) {
-                try {
-                    // Check if media is mounted or storage is built-in, if so, 
-                    // try and use external cache dir; otherwise use internal cache dir
-                    final String cachePath = 
-                            MainApp.getAppContext().getExternalCacheDir().getPath() + 
-                            File.separator + CACHE_FOLDER;
-                    Log_OC.d(TAG, "create dir: " + cachePath);
-                    final File diskCacheDir = new File(cachePath);
-                    mThumbnailCache = new DiskLruImageCache(
-                            diskCacheDir, 
-                            DISK_CACHE_SIZE, 
-                            mCompressFormat, 
-                            mCompressQuality
-                    );
-                } catch (Exception e) {
-                    Log_OC.d(TAG, "Thumbnail cache could not be opened ", e);
-                    mThumbnailCache = null;
+                mThumbnailCacheStarting = true;
+                if (mThumbnailCache == null) {
+                    try {
+                        // Check if media is mounted or storage is built-in, if so, 
+                        // try and use external cache dir; otherwise use internal cache dir
+                        final String cachePath = 
+                                MainApp.getAppContext().getExternalCacheDir().getPath() + 
+                                File.separator + CACHE_FOLDER;
+                        Log_OC.d(TAG, "create dir: " + cachePath);
+                        final File diskCacheDir = new File(cachePath);
+                        mThumbnailCache = new DiskLruImageCache(
+                                diskCacheDir, 
+                                DISK_CACHE_SIZE, 
+                                mCompressFormat, 
+                                mCompressQuality
+                        );
+                    } catch (Exception e) {
+                        Log_OC.d(TAG, "Thumbnail cache could not be opened ", e);
+                        mThumbnailCache = null;
+                    }
                 }
                 mThumbnailCacheStarting = false; // Finished initialization
                 mThumbnailsDiskCacheLock.notifyAll(); // Wake any waiting threads
@@ -187,6 +192,9 @@ public class ThumbnailsCacheManager {
                         
                         if (bitmap != null) {
                             thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);
+                            
+                            // Rotate image, obeying exif tag
+                            thumbnail = BitmapUtils.rotateImage(thumbnail, mFile.getStoragePath());
     
                             // Add thumbnail to cache
                             addBitmapToCache(imageKey, thumbnail);
@@ -258,5 +266,5 @@ public class ThumbnailsCacheManager {
             mThumbnailsDiskCacheLock.notifyAll(); // Wake any waiting threads
         }
     }
-
+    
 }