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