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
;
10 import java
.util
.regex
.Matcher
;
11 import java
.util
.regex
.Pattern
;
13 import android
.content
.Context
;
14 import android
.graphics
.Bitmap
;
15 import android
.graphics
.Bitmap
.CompressFormat
;
16 import android
.graphics
.BitmapFactory
;
17 import android
.util
.Log
;
19 import com
.jakewharton
.disklrucache
.DiskLruCache
;
20 import com
.owncloud
.android
.BuildConfig
;
21 import com
.owncloud
.android
.utils
.Log_OC
;
23 public class DiskLruImageCache
{
25 private DiskLruCache mDiskCache
;
26 private CompressFormat mCompressFormat
;
27 private int mCompressQuality
;
28 private static final int CACHE_VERSION
= 1;
29 private static final int VALUE_COUNT
= 1;
30 private static final int IO_BUFFER_SIZE
= 8 * 1024;
31 private static final Pattern CAPITAL_LETTERS
= Pattern
.compile("[A-Z]");
33 private StringBuffer mValidKeyBuffer
= new StringBuffer(64);
34 private StringBuffer mConversionBuffer
= new StringBuffer(2).append('_');
36 private static final String TAG
= "DiskLruImageCache";
38 public DiskLruImageCache( Context context
,String uniqueName
, int diskCacheSize
,
39 CompressFormat compressFormat
, int quality
) {
41 final File diskCacheDir
= getDiskCacheDir(context
, uniqueName
);
42 mDiskCache
= DiskLruCache
.open(
43 diskCacheDir
, CACHE_VERSION
, VALUE_COUNT
, diskCacheSize
45 mCompressFormat
= compressFormat
;
46 mCompressQuality
= quality
;
47 } catch (IOException e
) {
52 private boolean writeBitmapToFile( Bitmap bitmap
, DiskLruCache
.Editor editor
)
53 throws IOException
, FileNotFoundException
{
54 OutputStream out
= null
;
56 out
= new BufferedOutputStream( editor
.newOutputStream( 0 ), IO_BUFFER_SIZE
);
57 return bitmap
.compress( mCompressFormat
, mCompressQuality
, out
);
65 private File
getDiskCacheDir(Context context
, String uniqueName
) {
67 // Check if media is mounted or storage is built-in, if so, try and use external cache dir
68 // otherwise use internal cache dir
69 final String cachePath
= context
.getExternalCacheDir().getPath();
71 Log_OC
.d(TAG
, "create dir: " + cachePath
+ File
.separator
+ uniqueName
);
73 return new File(cachePath
+ File
.separator
+ uniqueName
);
76 public void put( String key
, Bitmap data
) {
78 DiskLruCache
.Editor editor
= null
;
79 String validKey
= convertToValidKey(key
);
81 editor
= mDiskCache
.edit( validKey
);
82 if ( editor
== null
) {
86 if( writeBitmapToFile( data
, editor
) ) {
89 if ( BuildConfig
.DEBUG
) {
90 Log
.d( "cache_test_DISK_", "image put on disk cache " + validKey
);
94 if ( BuildConfig
.DEBUG
) {
95 Log
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey
);
98 } catch (IOException e
) {
99 if ( BuildConfig
.DEBUG
) {
100 Log
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey
);
103 if ( editor
!= null
) {
106 } catch (IOException ignored
) {
112 public Bitmap
getBitmap( String key
) {
114 Bitmap bitmap
= null
;
115 DiskLruCache
.Snapshot snapshot
= null
;
116 String validKey
= convertToValidKey(key
);
119 snapshot
= mDiskCache
.get( validKey
);
120 if ( snapshot
== null
) {
123 final InputStream
in = snapshot
.getInputStream( 0 );
125 final BufferedInputStream buffIn
=
126 new BufferedInputStream( in, IO_BUFFER_SIZE
);
127 bitmap
= BitmapFactory
.decodeStream( buffIn
);
129 } catch ( IOException e
) {
132 if ( snapshot
!= null
) {
137 if ( BuildConfig
.DEBUG
) {
138 Log
.d("cache_test_DISK_", bitmap
== null ?
"not found" : "image read from disk " + validKey
);
145 public boolean containsKey( String key
) {
147 boolean contained
= false
;
148 DiskLruCache
.Snapshot snapshot
= null
;
149 String validKey
= convertToValidKey(key
);
151 snapshot
= mDiskCache
.get( validKey
);
152 contained
= snapshot
!= null
;
153 } catch (IOException e
) {
156 if ( snapshot
!= null
) {
165 public void clearCache() {
166 if ( BuildConfig
.DEBUG
) {
167 Log
.d( "cache_test_DISK_", "disk cache CLEARED");
171 } catch ( IOException e
) {
176 public File
getCacheFolder() {
177 return mDiskCache
.getDirectory();
180 private String
convertToValidKey(String key
) {
181 Matcher capitalLettersMatcher
= CAPITAL_LETTERS
.matcher(key
);
182 mValidKeyBuffer
.delete(0, mValidKeyBuffer
.length());
183 mConversionBuffer
.delete(1, mConversionBuffer
.length());
185 while (capitalLettersMatcher
.find()) {
186 mConversionBuffer
.replace(1, 2, capitalLettersMatcher
.group(0).toLowerCase());
187 capitalLettersMatcher
.appendReplacement(mValidKeyBuffer
, mConversionBuffer
.toString());
189 capitalLettersMatcher
.appendTail(mValidKeyBuffer
);
190 return mValidKeyBuffer
.toString();