X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/8c96447fe75afb512621a6c6bb134aa6852e9162..e901b609baa4dd5f681e2a5257c9e504997e3377:/src/com/owncloud/android/ui/adapter/DiskLruImageCache.java diff --git a/src/com/owncloud/android/ui/adapter/DiskLruImageCache.java b/src/com/owncloud/android/ui/adapter/DiskLruImageCache.java index 5b401c28..93efdf1c 100644 --- a/src/com/owncloud/android/ui/adapter/DiskLruImageCache.java +++ b/src/com/owncloud/android/ui/adapter/DiskLruImageCache.java @@ -1,3 +1,20 @@ +/* ownCloud Android client application + * Copyright (C) 2012-2014 ownCloud Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + package com.owncloud.android.ui.adapter; import java.io.BufferedInputStream; @@ -8,43 +25,42 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; -import android.os.Environment; -import android.util.Log; import com.jakewharton.disklrucache.DiskLruCache; import com.owncloud.android.BuildConfig; -import com.owncloud.android.utils.Log_OC; +import com.owncloud.android.lib.common.utils.Log_OC; public class DiskLruImageCache { private DiskLruCache mDiskCache; - private CompressFormat mCompressFormat = CompressFormat.JPEG; - private int mCompressQuality = 70; - private static final int APP_VERSION = 1; + private CompressFormat mCompressFormat; + private int mCompressQuality; + private static final int CACHE_VERSION = 1; private static final int VALUE_COUNT = 1; - private static final String TAG = "DiskLruImageCache"; - - public DiskLruImageCache( Context context,String uniqueName, int diskCacheSize, - CompressFormat compressFormat, int quality ) { - try { - final File diskCacheDir = getDiskCacheDir(context, uniqueName ); - mDiskCache = DiskLruCache.open( diskCacheDir, APP_VERSION, VALUE_COUNT, diskCacheSize ); - mCompressFormat = compressFormat; - mCompressQuality = quality; - } catch (IOException e) { - e.printStackTrace(); - } + private static final int IO_BUFFER_SIZE = 8 * 1024; + + private static final String TAG = DiskLruImageCache.class.getSimpleName(); + + //public DiskLruImageCache( Context context,String uniqueName, int diskCacheSize, + public DiskLruImageCache( + File diskCacheDir, int diskCacheSize, CompressFormat compressFormat, int quality + ) throws IOException { + + mDiskCache = DiskLruCache.open( + diskCacheDir, CACHE_VERSION, VALUE_COUNT, diskCacheSize + ); + mCompressFormat = compressFormat; + mCompressQuality = quality; } private boolean writeBitmapToFile( Bitmap bitmap, DiskLruCache.Editor editor ) throws IOException, FileNotFoundException { OutputStream out = null; try { - out = new BufferedOutputStream( editor.newOutputStream( 0 ), Utils.IO_BUFFER_SIZE ); + out = new BufferedOutputStream( editor.newOutputStream( 0 ), IO_BUFFER_SIZE ); return bitmap.compress( mCompressFormat, mCompressQuality, out ); } finally { if ( out != null ) { @@ -53,26 +69,12 @@ public class DiskLruImageCache { } } - private File getDiskCacheDir(Context context, String uniqueName) { - - // 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 = - Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || - !Utils.isExternalStorageRemovable() ? - Utils.getExternalCacheDir(context).getPath() : - context.getCacheDir().getPath(); - - Log_OC.d("DiskCache", "create dir: " + cachePath + File.separator + uniqueName); - - return new File(cachePath + File.separator + uniqueName); - } - public void put( String key, Bitmap data ) { DiskLruCache.Editor editor = null; + String validKey = convertToValidKey(key); try { - editor = mDiskCache.edit( key ); + editor = mDiskCache.edit( validKey ); if ( editor == null ) { return; } @@ -81,17 +83,17 @@ public class DiskLruImageCache { mDiskCache.flush(); editor.commit(); if ( BuildConfig.DEBUG ) { - Log.d( "cache_test_DISK_", "image put on disk cache " + key ); + Log_OC.d( "cache_test_DISK_", "image put on disk cache " + validKey ); } } else { editor.abort(); if ( BuildConfig.DEBUG ) { - Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key ); + Log_OC.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey ); } } } catch (IOException e) { if ( BuildConfig.DEBUG ) { - Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key ); + Log_OC.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey ); } try { if ( editor != null ) { @@ -107,16 +109,17 @@ public class DiskLruImageCache { Bitmap bitmap = null; DiskLruCache.Snapshot snapshot = null; + String validKey = convertToValidKey(key); try { - snapshot = mDiskCache.get( key ); + snapshot = mDiskCache.get( validKey ); if ( snapshot == null ) { return null; } final InputStream in = snapshot.getInputStream( 0 ); if ( in != null ) { final BufferedInputStream buffIn = - new BufferedInputStream( in, Utils.IO_BUFFER_SIZE ); + new BufferedInputStream( in, IO_BUFFER_SIZE ); bitmap = BitmapFactory.decodeStream( buffIn ); } } catch ( IOException e ) { @@ -128,7 +131,8 @@ public class DiskLruImageCache { } if ( BuildConfig.DEBUG ) { - Log.d( "cache_test_DISK_", bitmap == null ? "not found" : "image read from disk " + key); + Log_OC.d("cache_test_DISK_", bitmap == null ? + "not found" : "image read from disk " + validKey); } return bitmap; @@ -139,8 +143,9 @@ public class DiskLruImageCache { boolean contained = false; DiskLruCache.Snapshot snapshot = null; + String validKey = convertToValidKey(key); try { - snapshot = mDiskCache.get( key ); + snapshot = mDiskCache.get( validKey ); contained = snapshot != null; } catch (IOException e) { e.printStackTrace(); @@ -156,7 +161,7 @@ public class DiskLruImageCache { public void clearCache() { if ( BuildConfig.DEBUG ) { - Log.d( "cache_test_DISK_", "disk cache CLEARED"); + Log_OC.d( "cache_test_DISK_", "disk cache CLEARED"); } try { mDiskCache.delete(); @@ -168,5 +173,22 @@ public class DiskLruImageCache { public File getCacheFolder() { return mDiskCache.getDirectory(); } + + private String convertToValidKey(String key) { + return Integer.toString(key.hashCode()); + } + /** + * Remove passed key from cache + * @param key + */ + public void removeKey( String key ) { + String validKey = convertToValidKey(key); + try { + mDiskCache.remove(validKey); + Log_OC.d(TAG, "removeKey from cache: " + validKey); + } catch (IOException e) { + e.printStackTrace(); + } + } } \ No newline at end of file