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.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;
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
);
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 =
final File diskCacheDir = new File(cachePath);
mThumbnailCache = new DiskLruImageCache(
diskCacheDir,
- DISK_CACHE_SIZE,
+ cacheSize,
mCompressFormat,
mCompressQuality
);
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;
}
protected void onPostExecute(Bitmap bitmap){
- if (isCancelled()) {
- bitmap = null;
- }
-
if (bitmap != null) {
final ImageView imageView = mImageViewReference.get();
final ThumbnailGenerationTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
int px = getThumbnailDimension();
if (file.isDown()) {
- Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(
+ Bitmap temp = BitmapUtils.decodeSampledBitmapFromFile(
file.getStoragePath(), px, px);
+ Bitmap bitmap = ThumbnailUtils.extractThumbnail(temp, px, px);
if (bitmap != null) {
+ // Handle PNG
+ if (file.getMimetype().equalsIgnoreCase("image/png")) {
+ bitmap = handlePNG(bitmap, px);
+ }
+
thumbnail = addThumbnailToCache(imageKey, bitmap, file.getStoragePath(), px);
file.setNeedsUpdateThumbnail(false);
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);
+ // Handle PNG
+ if (file.getMimetype().equalsIgnoreCase("image/png")) {
+ thumbnail = handlePNG(thumbnail, px);
+ }
+
// Add thumbnail to cache
if (thumbnail != null) {
addBitmapToCache(imageKey, thumbnail);
}
+ 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() {
File file = (File)mFile;
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;