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
.lib
.common
.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
) throws IOException
{
40 final File diskCacheDir
= getDiskCacheDir(context
, uniqueName
);
41 mDiskCache
= DiskLruCache
.open(
42 diskCacheDir
, CACHE_VERSION
, VALUE_COUNT
, diskCacheSize
44 mCompressFormat
= compressFormat
;
45 mCompressQuality
= quality
;
48 private boolean writeBitmapToFile( Bitmap bitmap
, DiskLruCache
.Editor editor
)
49 throws IOException
, FileNotFoundException
{
50 OutputStream out
= null
;
52 out
= new BufferedOutputStream( editor
.newOutputStream( 0 ), IO_BUFFER_SIZE
);
53 return bitmap
.compress( mCompressFormat
, mCompressQuality
, out
);
61 private File
getDiskCacheDir(Context context
, String uniqueName
) {
63 // Check if media is mounted or storage is built-in, if so, try and use external cache dir
64 // otherwise use internal cache dir
65 final String cachePath
= context
.getExternalCacheDir().getPath();
67 Log_OC
.d(TAG
, "create dir: " + cachePath
+ File
.separator
+ uniqueName
);
69 return new File(cachePath
+ File
.separator
+ uniqueName
);
72 public void put( String key
, Bitmap data
) {
74 DiskLruCache
.Editor editor
= null
;
75 String validKey
= convertToValidKey(key
);
77 editor
= mDiskCache
.edit( validKey
);
78 if ( editor
== null
) {
82 if( writeBitmapToFile( data
, editor
) ) {
85 if ( BuildConfig
.DEBUG
) {
86 Log
.d( "cache_test_DISK_", "image put on disk cache " + validKey
);
90 if ( BuildConfig
.DEBUG
) {
91 Log
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey
);
94 } catch (IOException e
) {
95 if ( BuildConfig
.DEBUG
) {
96 Log
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey
);
99 if ( editor
!= null
) {
102 } catch (IOException ignored
) {
108 public Bitmap
getBitmap( String key
) {
110 Bitmap bitmap
= null
;
111 DiskLruCache
.Snapshot snapshot
= null
;
112 String validKey
= convertToValidKey(key
);
115 snapshot
= mDiskCache
.get( validKey
);
116 if ( snapshot
== null
) {
119 final InputStream
in = snapshot
.getInputStream( 0 );
121 final BufferedInputStream buffIn
=
122 new BufferedInputStream( in, IO_BUFFER_SIZE
);
123 bitmap
= BitmapFactory
.decodeStream( buffIn
);
125 } catch ( IOException e
) {
128 if ( snapshot
!= null
) {
133 if ( BuildConfig
.DEBUG
) {
134 Log
.d("cache_test_DISK_", bitmap
== null ?
"not found" : "image read from disk " + validKey
);
141 public boolean containsKey( String key
) {
143 boolean contained
= false
;
144 DiskLruCache
.Snapshot snapshot
= null
;
145 String validKey
= convertToValidKey(key
);
147 snapshot
= mDiskCache
.get( validKey
);
148 contained
= snapshot
!= null
;
149 } catch (IOException e
) {
152 if ( snapshot
!= null
) {
161 public void clearCache() {
162 if ( BuildConfig
.DEBUG
) {
163 Log
.d( "cache_test_DISK_", "disk cache CLEARED");
167 } catch ( IOException e
) {
172 public File
getCacheFolder() {
173 return mDiskCache
.getDirectory();
176 private String
convertToValidKey(String key
) {
177 Matcher capitalLettersMatcher
= CAPITAL_LETTERS
.matcher(key
);
178 mValidKeyBuffer
.delete(0, mValidKeyBuffer
.length());
179 mConversionBuffer
.delete(1, mConversionBuffer
.length());
181 while (capitalLettersMatcher
.find()) {
182 mConversionBuffer
.replace(1, 2, capitalLettersMatcher
.group(0).toLowerCase());
183 capitalLettersMatcher
.appendReplacement(mValidKeyBuffer
, mConversionBuffer
.toString());
185 capitalLettersMatcher
.appendTail(mValidKeyBuffer
);
186 return mValidKeyBuffer
.toString();