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