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