fix wrong cast of cache size
[pub/Android/ownCloud.git] / src / com / owncloud / android / datamodel / ThumbnailsCacheManager.java
index 362106e..dd0a2cd 100644 (file)
@@ -29,17 +29,19 @@ import org.apache.commons.httpclient.HttpStatus;
 import org.apache.commons.httpclient.methods.GetMethod;
 
 import android.accounts.Account;
+import android.content.SharedPreferences;
 import android.content.res.Resources;
 import android.graphics.Bitmap;
 import android.graphics.Bitmap.CompressFormat;
 import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
-import android.graphics.Shader;
 import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.ColorDrawable;
 import android.graphics.drawable.Drawable;
 import android.media.ThumbnailUtils;
 import android.net.Uri;
 import android.os.AsyncTask;
+import android.preference.PreferenceManager;
 import android.widget.ImageView;
 
 import com.owncloud.android.MainApp;
@@ -62,20 +64,20 @@ public class ThumbnailsCacheManager {
     private static final String TAG = ThumbnailsCacheManager.class.getSimpleName();
     
     private static final String CACHE_FOLDER = "thumbnailCache";
+    private static final Integer CACHE_SIZE_MB = 10;
 
     private static final Object mThumbnailsDiskCacheLock = new Object();
     private static DiskLruImageCache mThumbnailCache = null;
     private static boolean mThumbnailCacheStarting = true;
     
-    private static final int DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB
     private static final CompressFormat mCompressFormat = CompressFormat.JPEG;
     private static final int mCompressQuality = 70;
     private static OwnCloudClient mClient = null;
 
     public static Bitmap mDefaultImg = 
             BitmapFactory.decodeResource(
-                    MainApp.getAppContext().getResources(), 
-                    DisplayUtils.getFileTypeIconId("image/png", "default.png")
+                    MainApp.getAppContext().getResources(),
+                    R.drawable.file_image
             );
 
     
@@ -88,6 +90,18 @@ public class ThumbnailsCacheManager {
 
                 if (mThumbnailCache == null) {
                     try {
+                        SharedPreferences appPrefs =
+                                PreferenceManager.getDefaultSharedPreferences(MainApp.getAppContext());
+                        // due to backward compatibility
+                        Integer cacheSize = CACHE_SIZE_MB * 1024 * 1024;
+                        try {
+                            cacheSize = appPrefs.getInt("pref_cache_size", cacheSize);
+                        } catch (ClassCastException e) {
+                            String temp = appPrefs.getString("pref_cache_size",
+                                    cacheSize.toString());
+                            cacheSize = Integer.decode(temp) * 1024 * 1024;
+                        }
+
                         // 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 = 
@@ -97,7 +111,7 @@ public class ThumbnailsCacheManager {
                         final File diskCacheDir = new File(cachePath);
                         mThumbnailCache = new DiskLruImageCache(
                                 diskCacheDir, 
-                                DISK_CACHE_SIZE, 
+                                cacheSize,
                                 mCompressFormat, 
                                 mCompressQuality
                         );
@@ -140,6 +154,31 @@ public class ThumbnailsCacheManager {
         return null;
     }
 
+    /**
+     * Sets max size of cache
+     * @param maxSize in MB
+     * @return
+     */
+    public static boolean setMaxSize(long maxSize){
+        if (mThumbnailCache != null){
+            mThumbnailCache.setMaxSize(maxSize * 1024 * 1024);
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    /**
+     * Shows max cache size
+     * @return max cache size in MB.
+     */
+    public static long getMaxSize(){
+        if (mThumbnailCache == null) {
+            new ThumbnailsCacheManager.InitDiskCacheTask().execute();
+        }
+        return mThumbnailCache.getMaxSize() / 1024 / 1024;
+    }
+
     public static class ThumbnailGenerationTask extends AsyncTask<Object, Void, Bitmap> {
         private final WeakReference<ImageView> mImageViewReference;
         private static Account mAccount;
@@ -195,10 +234,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);
@@ -290,9 +325,6 @@ 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, px, px);
@@ -326,16 +358,9 @@ public class ThumbnailsCacheManager {
                     px,
                     Bitmap.Config.ARGB_8888);
             Canvas c = new Canvas(resultBitmap);
-            Bitmap checker = BitmapFactory.decodeResource(MainApp.getAppContext().getResources(),
-                                                          R.drawable.checker_16_16);
-
-            BitmapDrawable background;
-            background = new BitmapDrawable(MainApp.getAppContext().getResources(), checker);
-
-            background.setBounds(0, 0, px, px);
-            background.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
-            background.draw(c);
 
+            c.drawColor(MainApp.getAppContext().getResources().
+                    getColor(R.color.background_color));
             c.drawBitmap(bitmap, 0, 0, null);
 
             return resultBitmap;
@@ -375,6 +400,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;