Creation of ThumbnailsCacheManager class, grouping all the methods needed to access...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / adapter / DiskLruImageCache.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.ui.adapter;
19
20 import java.io.BufferedInputStream;
21 import java.io.BufferedOutputStream;
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27
28 import android.graphics.Bitmap;
29 import android.graphics.Bitmap.CompressFormat;
30 import android.graphics.BitmapFactory;
31
32 import com.jakewharton.disklrucache.DiskLruCache;
33 import com.owncloud.android.BuildConfig;
34 import com.owncloud.android.lib.common.utils.Log_OC;
35
36 public class DiskLruImageCache {
37
38 private DiskLruCache mDiskCache;
39 private CompressFormat mCompressFormat;
40 private int mCompressQuality;
41 private static final int CACHE_VERSION = 1;
42 private static final int VALUE_COUNT = 1;
43 private static final int IO_BUFFER_SIZE = 8 * 1024;
44
45 private static final String TAG = DiskLruImageCache.class.getSimpleName();
46
47 //public DiskLruImageCache( Context context,String uniqueName, int diskCacheSize,
48 public DiskLruImageCache(
49 File diskCacheDir, int diskCacheSize, CompressFormat compressFormat, int quality
50 ) throws IOException {
51
52 mDiskCache = DiskLruCache.open(
53 diskCacheDir, CACHE_VERSION, VALUE_COUNT, diskCacheSize
54 );
55 mCompressFormat = compressFormat;
56 mCompressQuality = quality;
57 }
58
59 private boolean writeBitmapToFile( Bitmap bitmap, DiskLruCache.Editor editor )
60 throws IOException, FileNotFoundException {
61 OutputStream out = null;
62 try {
63 out = new BufferedOutputStream( editor.newOutputStream( 0 ), IO_BUFFER_SIZE );
64 return bitmap.compress( mCompressFormat, mCompressQuality, out );
65 } finally {
66 if ( out != null ) {
67 out.close();
68 }
69 }
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_OC.d( "cache_test_DISK_", "image put on disk cache " + validKey );
87 }
88 } else {
89 editor.abort();
90 if ( BuildConfig.DEBUG ) {
91 Log_OC.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey );
92 }
93 }
94 } catch (IOException e) {
95 if ( BuildConfig.DEBUG ) {
96 Log_OC.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_OC.d("cache_test_DISK_", bitmap == null ?
135 "not found" : "image read from disk " + validKey);
136 }
137
138 return bitmap;
139
140 }
141
142 public boolean containsKey( String key ) {
143
144 boolean contained = false;
145 DiskLruCache.Snapshot snapshot = null;
146 String validKey = convertToValidKey(key);
147 try {
148 snapshot = mDiskCache.get( validKey );
149 contained = snapshot != null;
150 } catch (IOException e) {
151 e.printStackTrace();
152 } finally {
153 if ( snapshot != null ) {
154 snapshot.close();
155 }
156 }
157
158 return contained;
159
160 }
161
162 public void clearCache() {
163 if ( BuildConfig.DEBUG ) {
164 Log_OC.d( "cache_test_DISK_", "disk cache CLEARED");
165 }
166 try {
167 mDiskCache.delete();
168 } catch ( IOException e ) {
169 e.printStackTrace();
170 }
171 }
172
173 public File getCacheFolder() {
174 return mDiskCache.getDirectory();
175 }
176
177 private String convertToValidKey(String key) {
178 return Integer.toString(key.hashCode());
179 }
180
181 /**
182 * Remove passed key from cache
183 * @param key
184 */
185 public void removeKey( String key ) {
186 String validKey = convertToValidKey(key);
187 try {
188 mDiskCache.remove(validKey);
189 Log_OC.d(TAG, "removeKey from cache: " + validKey);
190 } catch (IOException e) {
191 e.printStackTrace();
192 }
193 }
194 }