ce60d55f3053e42b8ead33dba46ffbdcaeb111ac
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / adapter / DiskLruImageCache.java
1 package com.owncloud.android.ui.adapter;
2
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
5 import java.io.File;
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;
12
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;
18
19 import com.jakewharton.disklrucache.DiskLruCache;
20 import com.owncloud.android.BuildConfig;
21 import com.owncloud.android.utils.Log_OC;
22
23 public class DiskLruImageCache {
24
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]");
32
33 private StringBuffer mValidKeyBuffer = new StringBuffer(64);
34 private StringBuffer mConversionBuffer = new StringBuffer(2).append('_');
35
36 private static final String TAG = "DiskLruImageCache";
37
38 public DiskLruImageCache( Context context,String uniqueName, int diskCacheSize,
39 CompressFormat compressFormat, int quality ) {
40 try {
41 final File diskCacheDir = getDiskCacheDir(context, uniqueName );
42 mDiskCache = DiskLruCache.open(
43 diskCacheDir, CACHE_VERSION, VALUE_COUNT, diskCacheSize
44 );
45 mCompressFormat = compressFormat;
46 mCompressQuality = quality;
47 } catch (IOException e) {
48 e.printStackTrace();
49 }
50 }
51
52 private boolean writeBitmapToFile( Bitmap bitmap, DiskLruCache.Editor editor )
53 throws IOException, FileNotFoundException {
54 OutputStream out = null;
55 try {
56 out = new BufferedOutputStream( editor.newOutputStream( 0 ), IO_BUFFER_SIZE );
57 return bitmap.compress( mCompressFormat, mCompressQuality, out );
58 } finally {
59 if ( out != null ) {
60 out.close();
61 }
62 }
63 }
64
65 private File getDiskCacheDir(Context context, String uniqueName) {
66
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();
70
71 Log_OC.d(TAG, "create dir: " + cachePath + File.separator + uniqueName);
72
73 return new File(cachePath + File.separator + uniqueName);
74 }
75
76 public void put( String key, Bitmap data ) {
77
78 DiskLruCache.Editor editor = null;
79 String validKey = convertToValidKey(key);
80 try {
81 editor = mDiskCache.edit( validKey );
82 if ( editor == null ) {
83 return;
84 }
85
86 if( writeBitmapToFile( data, editor ) ) {
87 mDiskCache.flush();
88 editor.commit();
89 if ( BuildConfig.DEBUG ) {
90 Log.d( "cache_test_DISK_", "image put on disk cache " + validKey );
91 }
92 } else {
93 editor.abort();
94 if ( BuildConfig.DEBUG ) {
95 Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey );
96 }
97 }
98 } catch (IOException e) {
99 if ( BuildConfig.DEBUG ) {
100 Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey );
101 }
102 try {
103 if ( editor != null ) {
104 editor.abort();
105 }
106 } catch (IOException ignored) {
107 }
108 }
109
110 }
111
112 public Bitmap getBitmap( String key ) {
113
114 Bitmap bitmap = null;
115 DiskLruCache.Snapshot snapshot = null;
116 String validKey = convertToValidKey(key);
117 try {
118
119 snapshot = mDiskCache.get( validKey );
120 if ( snapshot == null ) {
121 return null;
122 }
123 final InputStream in = snapshot.getInputStream( 0 );
124 if ( in != null ) {
125 final BufferedInputStream buffIn =
126 new BufferedInputStream( in, IO_BUFFER_SIZE );
127 bitmap = BitmapFactory.decodeStream( buffIn );
128 }
129 } catch ( IOException e ) {
130 e.printStackTrace();
131 } finally {
132 if ( snapshot != null ) {
133 snapshot.close();
134 }
135 }
136
137 if ( BuildConfig.DEBUG ) {
138 Log.d("cache_test_DISK_", bitmap == null ? "not found" : "image read from disk " + validKey);
139 }
140
141 return bitmap;
142
143 }
144
145 public boolean containsKey( String key ) {
146
147 boolean contained = false;
148 DiskLruCache.Snapshot snapshot = null;
149 String validKey = convertToValidKey(key);
150 try {
151 snapshot = mDiskCache.get( validKey );
152 contained = snapshot != null;
153 } catch (IOException e) {
154 e.printStackTrace();
155 } finally {
156 if ( snapshot != null ) {
157 snapshot.close();
158 }
159 }
160
161 return contained;
162
163 }
164
165 public void clearCache() {
166 if ( BuildConfig.DEBUG ) {
167 Log.d( "cache_test_DISK_", "disk cache CLEARED");
168 }
169 try {
170 mDiskCache.delete();
171 } catch ( IOException e ) {
172 e.printStackTrace();
173 }
174 }
175
176 public File getCacheFolder() {
177 return mDiskCache.getDirectory();
178 }
179
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());
184
185 while (capitalLettersMatcher.find()) {
186 mConversionBuffer.replace(1, 2, capitalLettersMatcher.group(0).toLowerCase());
187 capitalLettersMatcher.appendReplacement(mValidKeyBuffer, mConversionBuffer.toString());
188 }
189 capitalLettersMatcher.appendTail(mValidKeyBuffer);
190 return mValidKeyBuffer.toString();
191 }
192
193 }