de6d10be60a310dc6d51e6e38228eab4f81fb4d9
[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
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;
16
17 import com.jakewharton.disklrucache.DiskLruCache;
18 import com.owncloud.android.BuildConfig;
19 import com.owncloud.android.lib.common.utils.Log_OC;
20
21 public class DiskLruImageCache {
22
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
30 private static final String TAG = DiskLruImageCache.class.getSimpleName();
31
32 public DiskLruImageCache( Context context,String uniqueName, int diskCacheSize,
33 CompressFormat compressFormat, int quality ) throws IOException {
34 final File diskCacheDir = getDiskCacheDir(context, uniqueName );
35 mDiskCache = DiskLruCache.open(
36 diskCacheDir, CACHE_VERSION, VALUE_COUNT, diskCacheSize
37 );
38 mCompressFormat = compressFormat;
39 mCompressQuality = quality;
40 }
41
42 private boolean writeBitmapToFile( Bitmap bitmap, DiskLruCache.Editor editor )
43 throws IOException, FileNotFoundException {
44 OutputStream out = null;
45 try {
46 out = new BufferedOutputStream( editor.newOutputStream( 0 ), IO_BUFFER_SIZE );
47 return bitmap.compress( mCompressFormat, mCompressQuality, out );
48 } finally {
49 if ( out != null ) {
50 out.close();
51 }
52 }
53 }
54
55 private File getDiskCacheDir(Context context, String uniqueName) {
56
57 // Check if media is mounted or storage is built-in, if so, try and use external cache dir
58 // otherwise use internal cache dir
59 final String cachePath = context.getExternalCacheDir().getPath();
60
61 Log_OC.d(TAG, "create dir: " + cachePath + File.separator + uniqueName);
62
63 return new File(cachePath + File.separator + uniqueName);
64 }
65
66 public void put( String key, Bitmap data ) {
67
68 DiskLruCache.Editor editor = null;
69 String validKey = convertToValidKey(key);
70 try {
71 editor = mDiskCache.edit( validKey );
72 if ( editor == null ) {
73 return;
74 }
75
76 if( writeBitmapToFile( data, editor ) ) {
77 mDiskCache.flush();
78 editor.commit();
79 if ( BuildConfig.DEBUG ) {
80 Log.d( "cache_test_DISK_", "image put on disk cache " + validKey );
81 }
82 } else {
83 editor.abort();
84 if ( BuildConfig.DEBUG ) {
85 Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey );
86 }
87 }
88 } catch (IOException e) {
89 if ( BuildConfig.DEBUG ) {
90 Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey );
91 }
92 try {
93 if ( editor != null ) {
94 editor.abort();
95 }
96 } catch (IOException ignored) {
97 }
98 }
99
100 }
101
102 public Bitmap getBitmap( String key ) {
103
104 Bitmap bitmap = null;
105 DiskLruCache.Snapshot snapshot = null;
106 String validKey = convertToValidKey(key);
107 try {
108
109 snapshot = mDiskCache.get( validKey );
110 if ( snapshot == null ) {
111 return null;
112 }
113 final InputStream in = snapshot.getInputStream( 0 );
114 if ( in != null ) {
115 final BufferedInputStream buffIn =
116 new BufferedInputStream( in, IO_BUFFER_SIZE );
117 bitmap = BitmapFactory.decodeStream( buffIn );
118 }
119 } catch ( IOException e ) {
120 e.printStackTrace();
121 } finally {
122 if ( snapshot != null ) {
123 snapshot.close();
124 }
125 }
126
127 if ( BuildConfig.DEBUG ) {
128 Log.d("cache_test_DISK_", bitmap == null ? "not found" : "image read from disk " + validKey);
129 }
130
131 return bitmap;
132
133 }
134
135 public boolean containsKey( String key ) {
136
137 boolean contained = false;
138 DiskLruCache.Snapshot snapshot = null;
139 String validKey = convertToValidKey(key);
140 try {
141 snapshot = mDiskCache.get( validKey );
142 contained = snapshot != null;
143 } catch (IOException e) {
144 e.printStackTrace();
145 } finally {
146 if ( snapshot != null ) {
147 snapshot.close();
148 }
149 }
150
151 return contained;
152
153 }
154
155 public void clearCache() {
156 if ( BuildConfig.DEBUG ) {
157 Log.d( "cache_test_DISK_", "disk cache CLEARED");
158 }
159 try {
160 mDiskCache.delete();
161 } catch ( IOException e ) {
162 e.printStackTrace();
163 }
164 }
165
166 public File getCacheFolder() {
167 return mDiskCache.getDirectory();
168 }
169
170 private String convertToValidKey(String key) {
171 return Integer.toString(key.hashCode());
172 }
173
174 /**
175 * Remove passed key from cache
176 * @param key
177 */
178 public void removeKey( String key ) {
179 String validKey = convertToValidKey(key);
180 try {
181 mDiskCache.remove(validKey);
182 Log.d(TAG, "removeKey from cache: " + validKey);
183 } catch (IOException e) {
184 e.printStackTrace();
185 }
186 }
187 }