[tx-robot] updated from transifex
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / adapter / DiskLruImageCache.java
1 /**
2 * ownCloud Android client application
3 *
4 * Copyright (C) 2015 ownCloud Inc.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 package com.owncloud.android.ui.adapter;
21
22 import java.io.BufferedInputStream;
23 import java.io.BufferedOutputStream;
24 import java.io.File;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29
30 import android.graphics.Bitmap;
31 import android.graphics.Bitmap.CompressFormat;
32 import android.graphics.BitmapFactory;
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 public DiskLruImageCache(
51 File diskCacheDir, int diskCacheSize, CompressFormat compressFormat, int quality
52 ) throws IOException {
53
54 mDiskCache = DiskLruCache.open(
55 diskCacheDir, CACHE_VERSION, VALUE_COUNT, diskCacheSize
56 );
57 mCompressFormat = compressFormat;
58 mCompressQuality = quality;
59 }
60
61 private boolean writeBitmapToFile( Bitmap bitmap, DiskLruCache.Editor editor )
62 throws IOException, FileNotFoundException {
63 OutputStream out = null;
64 try {
65 out = new BufferedOutputStream( editor.newOutputStream( 0 ), IO_BUFFER_SIZE );
66 return bitmap.compress( mCompressFormat, mCompressQuality, out );
67 } finally {
68 if ( out != null ) {
69 out.close();
70 }
71 }
72 }
73
74 public void put( String key, Bitmap data ) {
75
76 DiskLruCache.Editor editor = null;
77 String validKey = convertToValidKey(key);
78 try {
79 editor = mDiskCache.edit( validKey );
80 if ( editor == null ) {
81 return;
82 }
83
84 if( writeBitmapToFile( data, editor ) ) {
85 mDiskCache.flush();
86 editor.commit();
87 if ( BuildConfig.DEBUG ) {
88 Log_OC.d( "cache_test_DISK_", "image put on disk cache " + validKey );
89 }
90 } else {
91 editor.abort();
92 if ( BuildConfig.DEBUG ) {
93 Log_OC.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey );
94 }
95 }
96 } catch (IOException e) {
97 if ( BuildConfig.DEBUG ) {
98 Log_OC.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey );
99 }
100 try {
101 if ( editor != null ) {
102 editor.abort();
103 }
104 } catch (IOException ignored) {
105 }
106 }
107
108 }
109
110 public Bitmap getBitmap( String key ) {
111
112 Bitmap bitmap = null;
113 DiskLruCache.Snapshot snapshot = null;
114 String validKey = convertToValidKey(key);
115 try {
116
117 snapshot = mDiskCache.get( validKey );
118 if ( snapshot == null ) {
119 return null;
120 }
121 final InputStream in = snapshot.getInputStream( 0 );
122 if ( in != null ) {
123 final BufferedInputStream buffIn =
124 new BufferedInputStream( in, IO_BUFFER_SIZE );
125 bitmap = BitmapFactory.decodeStream( buffIn );
126 }
127 } catch ( IOException e ) {
128 e.printStackTrace();
129 } finally {
130 if ( snapshot != null ) {
131 snapshot.close();
132 }
133 }
134
135 if ( BuildConfig.DEBUG ) {
136 Log_OC.d("cache_test_DISK_", bitmap == null ?
137 "not found" : "image read from disk " + validKey);
138 }
139
140 return bitmap;
141
142 }
143
144 public boolean containsKey( String key ) {
145
146 boolean contained = false;
147 DiskLruCache.Snapshot snapshot = null;
148 String validKey = convertToValidKey(key);
149 try {
150 snapshot = mDiskCache.get( validKey );
151 contained = snapshot != null;
152 } catch (IOException e) {
153 e.printStackTrace();
154 } finally {
155 if ( snapshot != null ) {
156 snapshot.close();
157 }
158 }
159
160 return contained;
161
162 }
163
164 public void clearCache() {
165 if ( BuildConfig.DEBUG ) {
166 Log_OC.d( "cache_test_DISK_", "disk cache CLEARED");
167 }
168 try {
169 mDiskCache.delete();
170 } catch ( IOException e ) {
171 e.printStackTrace();
172 }
173 }
174
175 public File getCacheFolder() {
176 return mDiskCache.getDirectory();
177 }
178
179 private String convertToValidKey(String key) {
180 return Integer.toString(key.hashCode());
181 }
182
183 /**
184 * Remove passed key from cache
185 * @param key
186 */
187 public void removeKey( String key ) {
188 String validKey = convertToValidKey(key);
189 try {
190 mDiskCache.remove(validKey);
191 Log_OC.d(TAG, "removeKey from cache: " + validKey);
192 } catch (IOException e) {
193 e.printStackTrace();
194 }
195 }
196 }