1 package com
.owncloud
.android
.ui
.adapter
;
3 import java
.io
.BufferedInputStream
;
4 import java
.io
.BufferedOutputStream
;
6 import java
.io
.FileNotFoundException
;
7 import java
.io
.IOException
;
8 import java
.io
.InputStream
;
9 import java
.io
.OutputStream
;
11 import android
.content
.Context
;
12 import android
.graphics
.Bitmap
;
13 import android
.graphics
.Bitmap
.CompressFormat
;
14 import android
.graphics
.BitmapFactory
;
15 import android
.util
.Log
;
17 import com
.jakewharton
.disklrucache
.DiskLruCache
;
18 import com
.owncloud
.android
.BuildConfig
;
19 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
21 public class DiskLruImageCache
{
23 private DiskLruCache mDiskCache
;
24 private CompressFormat mCompressFormat
;
25 private int mCompressQuality
;
26 private static final int CACHE_VERSION
= 1;
27 private static final int VALUE_COUNT
= 1;
28 private static final int IO_BUFFER_SIZE
= 8 * 1024;
30 private static final String TAG
= DiskLruImageCache
.class.getSimpleName();
32 public DiskLruImageCache( Context context
,String uniqueName
, int diskCacheSize
,
33 CompressFormat compressFormat
, int quality
) throws IOException
{
34 final File diskCacheDir
= getDiskCacheDir(context
, uniqueName
);
35 mDiskCache
= DiskLruCache
.open(
36 diskCacheDir
, CACHE_VERSION
, VALUE_COUNT
, diskCacheSize
38 mCompressFormat
= compressFormat
;
39 mCompressQuality
= quality
;
42 private boolean writeBitmapToFile( Bitmap bitmap
, DiskLruCache
.Editor editor
)
43 throws IOException
, FileNotFoundException
{
44 OutputStream out
= null
;
46 out
= new BufferedOutputStream( editor
.newOutputStream( 0 ), IO_BUFFER_SIZE
);
47 return bitmap
.compress( mCompressFormat
, mCompressQuality
, out
);
55 private File
getDiskCacheDir(Context context
, String uniqueName
) {
57 // Check if media is mounted or storage is built-in, if so, try and use external cache dir
58 // otherwise use internal cache dir
59 final String cachePath
= context
.getExternalCacheDir().getPath();
61 Log_OC
.d(TAG
, "create dir: " + cachePath
+ File
.separator
+ uniqueName
);
63 return new File(cachePath
+ File
.separator
+ uniqueName
);
66 public void put( String key
, Bitmap data
) {
68 DiskLruCache
.Editor editor
= null
;
69 String validKey
= convertToValidKey(key
);
71 editor
= mDiskCache
.edit( validKey
);
72 if ( editor
== null
) {
76 if( writeBitmapToFile( data
, editor
) ) {
79 if ( BuildConfig
.DEBUG
) {
80 Log
.d( "cache_test_DISK_", "image put on disk cache " + validKey
);
84 if ( BuildConfig
.DEBUG
) {
85 Log
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey
);
88 } catch (IOException e
) {
89 if ( BuildConfig
.DEBUG
) {
90 Log
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey
);
93 if ( editor
!= null
) {
96 } catch (IOException ignored
) {
102 public Bitmap
getBitmap( String key
) {
104 Bitmap bitmap
= null
;
105 DiskLruCache
.Snapshot snapshot
= null
;
106 String validKey
= convertToValidKey(key
);
109 snapshot
= mDiskCache
.get( validKey
);
110 if ( snapshot
== null
) {
113 final InputStream
in = snapshot
.getInputStream( 0 );
115 final BufferedInputStream buffIn
=
116 new BufferedInputStream( in, IO_BUFFER_SIZE
);
117 bitmap
= BitmapFactory
.decodeStream( buffIn
);
119 } catch ( IOException e
) {
122 if ( snapshot
!= null
) {
127 if ( BuildConfig
.DEBUG
) {
128 Log
.d("cache_test_DISK_", bitmap
== null ?
"not found" : "image read from disk " + validKey
);
135 public boolean containsKey( String key
) {
137 boolean contained
= false
;
138 DiskLruCache
.Snapshot snapshot
= null
;
139 String validKey
= convertToValidKey(key
);
141 snapshot
= mDiskCache
.get( validKey
);
142 contained
= snapshot
!= null
;
143 } catch (IOException e
) {
146 if ( snapshot
!= null
) {
155 public void clearCache() {
156 if ( BuildConfig
.DEBUG
) {
157 Log
.d( "cache_test_DISK_", "disk cache CLEARED");
161 } catch ( IOException e
) {
166 public File
getCacheFolder() {
167 return mDiskCache
.getDirectory();
170 private String
convertToValidKey(String key
) {
171 return Integer
.toString(key
.hashCode());
175 * Remove passed key from cache
178 public void removeKey( String key
) {
179 String validKey
= convertToValidKey(key
);
181 mDiskCache
.remove(validKey
);
182 Log
.d(TAG
, "removeKey from cache: " + validKey
);
183 } catch (IOException e
) {