[tx-robot] updated from transifex
[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.lib.common.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 ) throws IOException {
40 final File diskCacheDir = getDiskCacheDir(context, uniqueName );
41 mDiskCache = DiskLruCache.open(
42 diskCacheDir, CACHE_VERSION, VALUE_COUNT, diskCacheSize
43 );
44 mCompressFormat = compressFormat;
45 mCompressQuality = quality;
46 }
47
48 private boolean writeBitmapToFile( Bitmap bitmap, DiskLruCache.Editor editor )
49 throws IOException, FileNotFoundException {
50 OutputStream out = null;
51 try {
52 out = new BufferedOutputStream( editor.newOutputStream( 0 ), IO_BUFFER_SIZE );
53 return bitmap.compress( mCompressFormat, mCompressQuality, out );
54 } finally {
55 if ( out != null ) {
56 out.close();
57 }
58 }
59 }
60
61 private File getDiskCacheDir(Context context, String uniqueName) {
62
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();
66
67 Log_OC.d(TAG, "create dir: " + cachePath + File.separator + uniqueName);
68
69 return new File(cachePath + File.separator + uniqueName);
70 }
71
72 public void put( String key, Bitmap data ) {
73
74 DiskLruCache.Editor editor = null;
75 String validKey = convertToValidKey(key);
76 try {
77 editor = mDiskCache.edit( validKey );
78 if ( editor == null ) {
79 return;
80 }
81
82 if( writeBitmapToFile( data, editor ) ) {
83 mDiskCache.flush();
84 editor.commit();
85 if ( BuildConfig.DEBUG ) {
86 Log.d( "cache_test_DISK_", "image put on disk cache " + validKey );
87 }
88 } else {
89 editor.abort();
90 if ( BuildConfig.DEBUG ) {
91 Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey );
92 }
93 }
94 } catch (IOException e) {
95 if ( BuildConfig.DEBUG ) {
96 Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey );
97 }
98 try {
99 if ( editor != null ) {
100 editor.abort();
101 }
102 } catch (IOException ignored) {
103 }
104 }
105
106 }
107
108 public Bitmap getBitmap( String key ) {
109
110 Bitmap bitmap = null;
111 DiskLruCache.Snapshot snapshot = null;
112 String validKey = convertToValidKey(key);
113 try {
114
115 snapshot = mDiskCache.get( validKey );
116 if ( snapshot == null ) {
117 return null;
118 }
119 final InputStream in = snapshot.getInputStream( 0 );
120 if ( in != null ) {
121 final BufferedInputStream buffIn =
122 new BufferedInputStream( in, IO_BUFFER_SIZE );
123 bitmap = BitmapFactory.decodeStream( buffIn );
124 }
125 } catch ( IOException e ) {
126 e.printStackTrace();
127 } finally {
128 if ( snapshot != null ) {
129 snapshot.close();
130 }
131 }
132
133 if ( BuildConfig.DEBUG ) {
134 Log.d("cache_test_DISK_", bitmap == null ? "not found" : "image read from disk " + validKey);
135 }
136
137 return bitmap;
138
139 }
140
141 public boolean containsKey( String key ) {
142
143 boolean contained = false;
144 DiskLruCache.Snapshot snapshot = null;
145 String validKey = convertToValidKey(key);
146 try {
147 snapshot = mDiskCache.get( validKey );
148 contained = snapshot != null;
149 } catch (IOException e) {
150 e.printStackTrace();
151 } finally {
152 if ( snapshot != null ) {
153 snapshot.close();
154 }
155 }
156
157 return contained;
158
159 }
160
161 public void clearCache() {
162 if ( BuildConfig.DEBUG ) {
163 Log.d( "cache_test_DISK_", "disk cache CLEARED");
164 }
165 try {
166 mDiskCache.delete();
167 } catch ( IOException e ) {
168 e.printStackTrace();
169 }
170 }
171
172 public File getCacheFolder() {
173 return mDiskCache.getDirectory();
174 }
175
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());
180
181 while (capitalLettersMatcher.find()) {
182 mConversionBuffer.replace(1, 2, capitalLettersMatcher.group(0).toLowerCase());
183 capitalLettersMatcher.appendReplacement(mValidKeyBuffer, mConversionBuffer.toString());
184 }
185 capitalLettersMatcher.appendTail(mValidKeyBuffer);
186 return mValidKeyBuffer.toString();
187 }
188
189 }