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
.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;
29 //private static final String TAG = "DiskLruImageCache";
31 public DiskLruImageCache( Context context
,String uniqueName
, int diskCacheSize
,
32 CompressFormat compressFormat
, int quality
) {
34 final File diskCacheDir
= getDiskCacheDir(context
, uniqueName
);
35 mDiskCache
= DiskLruCache
.open(
36 diskCacheDir
, CACHE_VERSION
, VALUE_COUNT
, diskCacheSize
38 mCompressFormat
= compressFormat
;
39 mCompressQuality
= quality
;
40 } catch (IOException e
) {
45 private boolean writeBitmapToFile( Bitmap bitmap
, DiskLruCache
.Editor editor
)
46 throws IOException
, FileNotFoundException
{
47 OutputStream out
= null
;
49 out
= new BufferedOutputStream( editor
.newOutputStream( 0 ), IO_BUFFER_SIZE
);
50 return bitmap
.compress( mCompressFormat
, mCompressQuality
, out
);
58 private File
getDiskCacheDir(Context context
, String uniqueName
) {
60 // Check if media is mounted or storage is built-in, if so, try and use external cache dir
61 // otherwise use internal cache dir
62 final String cachePath
= context
.getExternalCacheDir().getPath();
64 Log_OC
.d("DiskCache", "create dir: " + cachePath
+ File
.separator
+ uniqueName
);
66 return new File(cachePath
+ File
.separator
+ uniqueName
);
69 public void put( String key
, Bitmap data
) {
71 DiskLruCache
.Editor editor
= null
;
73 editor
= mDiskCache
.edit( key
);
74 if ( editor
== null
) {
78 if( writeBitmapToFile( data
, editor
) ) {
81 if ( BuildConfig
.DEBUG
) {
82 Log
.d( "cache_test_DISK_", "image put on disk cache " + key
);
86 if ( BuildConfig
.DEBUG
) {
87 Log
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key
);
90 } catch (IOException e
) {
91 if ( BuildConfig
.DEBUG
) {
92 Log
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key
);
95 if ( editor
!= null
) {
98 } catch (IOException ignored
) {
104 public Bitmap
getBitmap( String key
) {
106 Bitmap bitmap
= null
;
107 DiskLruCache
.Snapshot snapshot
= null
;
110 snapshot
= mDiskCache
.get( key
);
111 if ( snapshot
== null
) {
114 final InputStream
in = snapshot
.getInputStream( 0 );
116 final BufferedInputStream buffIn
=
117 new BufferedInputStream( in, IO_BUFFER_SIZE
);
118 bitmap
= BitmapFactory
.decodeStream( buffIn
);
120 } catch ( IOException e
) {
123 if ( snapshot
!= null
) {
128 if ( BuildConfig
.DEBUG
) {
129 Log
.d("cache_test_DISK_", bitmap
== null ?
"not found" : "image read from disk " + key
);
136 public boolean containsKey( String key
) {
138 boolean contained
= false
;
139 DiskLruCache
.Snapshot snapshot
= null
;
141 snapshot
= mDiskCache
.get( key
);
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();