Added requested changes.
[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.os.Environment;
16 import android.util.Log;
17
18 import com.jakewharton.disklrucache.DiskLruCache;
19 import com.owncloud.android.BuildConfig;
20 import com.owncloud.android.utils.Log_OC;
21
22 public class DiskLruImageCache {
23
24 private DiskLruCache mDiskCache;
25 private CompressFormat mCompressFormat;
26 private int mCompressQuality;
27 private static final int CACHE_VERSION = 1;
28 private static final int VALUE_COUNT = 1;
29 private static final int IO_BUFFER_SIZE = 8 * 1024;
30 private static final String TAG = "DiskLruImageCache";
31
32 public DiskLruImageCache( Context context,String uniqueName, int diskCacheSize,
33 CompressFormat compressFormat, int quality ) {
34 try {
35 final File diskCacheDir = getDiskCacheDir(context, uniqueName );
36 mDiskCache = DiskLruCache.open( diskCacheDir, CACHE_VERSION, VALUE_COUNT, diskCacheSize );
37 mCompressFormat = compressFormat;
38 mCompressQuality = quality;
39 } catch (IOException e) {
40 e.printStackTrace();
41 }
42 }
43
44 private boolean writeBitmapToFile( Bitmap bitmap, DiskLruCache.Editor editor )
45 throws IOException, FileNotFoundException {
46 OutputStream out = null;
47 try {
48 out = new BufferedOutputStream( editor.newOutputStream( 0 ), IO_BUFFER_SIZE );
49 return bitmap.compress( mCompressFormat, mCompressQuality, out );
50 } finally {
51 if ( out != null ) {
52 out.close();
53 }
54 }
55 }
56
57 private File getDiskCacheDir(Context context, String uniqueName) {
58
59 // Check if media is mounted or storage is built-in, if so, try and use external cache dir
60 // otherwise use internal cache dir
61 final String cachePath = context.getExternalCacheDir().getPath();
62
63 Log_OC.d("DiskCache", "create dir: " + cachePath + File.separator + uniqueName);
64
65 return new File(cachePath + File.separator + uniqueName);
66 }
67
68 public void put( String key, Bitmap data ) {
69
70 DiskLruCache.Editor editor = null;
71 try {
72 editor = mDiskCache.edit( key );
73 if ( editor == null ) {
74 return;
75 }
76
77 if( writeBitmapToFile( data, editor ) ) {
78 mDiskCache.flush();
79 editor.commit();
80 if ( BuildConfig.DEBUG ) {
81 Log.d( "cache_test_DISK_", "image put on disk cache " + key );
82 }
83 } else {
84 editor.abort();
85 if ( BuildConfig.DEBUG ) {
86 Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key );
87 }
88 }
89 } catch (IOException e) {
90 if ( BuildConfig.DEBUG ) {
91 Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key );
92 }
93 try {
94 if ( editor != null ) {
95 editor.abort();
96 }
97 } catch (IOException ignored) {
98 }
99 }
100
101 }
102
103 public Bitmap getBitmap( String key ) {
104
105 Bitmap bitmap = null;
106 DiskLruCache.Snapshot snapshot = null;
107 try {
108
109 snapshot = mDiskCache.get( key );
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 " + key);
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 try {
140 snapshot = mDiskCache.get( key );
141 contained = snapshot != null;
142 } catch (IOException e) {
143 e.printStackTrace();
144 } finally {
145 if ( snapshot != null ) {
146 snapshot.close();
147 }
148 }
149
150 return contained;
151
152 }
153
154 public void clearCache() {
155 if ( BuildConfig.DEBUG ) {
156 Log.d( "cache_test_DISK_", "disk cache CLEARED");
157 }
158 try {
159 mDiskCache.delete();
160 } catch ( IOException e ) {
161 e.printStackTrace();
162 }
163 }
164
165 public File getCacheFolder() {
166 return mDiskCache.getDirectory();
167 }
168
169 }