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