1 package com
.owncloud
.android
.ui
.adapter
;
3 import java
.io
.BufferedInputStream
;
4 import java
.io
.BufferedOutputStream
;
6 import java
.io
.FileNotFoundException
;
7 import java
.io
.IOException
;
8 import java
.io
.InputStream
;
9 import java
.io
.OutputStream
;
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
;
18 import com
.jakewharton
.disklrucache
.DiskLruCache
;
19 import com
.owncloud
.android
.BuildConfig
;
20 import com
.owncloud
.android
.utils
.Log_OC
;
22 public class DiskLruImageCache
{
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";
31 public DiskLruImageCache( Context context
,String uniqueName
, int diskCacheSize
,
32 CompressFormat compressFormat
, int quality
) {
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
) {
43 private boolean writeBitmapToFile( Bitmap bitmap
, DiskLruCache
.Editor editor
)
44 throws IOException
, FileNotFoundException
{
45 OutputStream out
= null
;
47 out
= new BufferedOutputStream( editor
.newOutputStream( 0 ), Utils
.IO_BUFFER_SIZE
);
48 return bitmap
.compress( mCompressFormat
, mCompressQuality
, out
);
56 private File
getDiskCacheDir(Context context
, String uniqueName
) {
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();
66 Log_OC
.d("DiskCache", "create dir: " + cachePath
+ File
.separator
+ uniqueName
);
68 return new File(cachePath
+ File
.separator
+ uniqueName
);
71 public void put( String key
, Bitmap data
) {
73 DiskLruCache
.Editor editor
= null
;
75 editor
= mDiskCache
.edit( key
);
76 if ( editor
== null
) {
80 if( writeBitmapToFile( data
, editor
) ) {
83 if ( BuildConfig
.DEBUG
) {
84 Log
.d( "cache_test_DISK_", "image put on disk cache " + key
);
88 if ( BuildConfig
.DEBUG
) {
89 Log
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key
);
92 } catch (IOException e
) {
93 if ( BuildConfig
.DEBUG
) {
94 Log
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key
);
97 if ( editor
!= null
) {
100 } catch (IOException ignored
) {
106 public Bitmap
getBitmap( String key
) {
108 Bitmap bitmap
= null
;
109 DiskLruCache
.Snapshot snapshot
= null
;
112 snapshot
= mDiskCache
.get( key
);
113 if ( snapshot
== null
) {
116 final InputStream
in = snapshot
.getInputStream( 0 );
118 final BufferedInputStream buffIn
=
119 new BufferedInputStream( in, Utils
.IO_BUFFER_SIZE
);
120 bitmap
= BitmapFactory
.decodeStream( buffIn
);
122 } catch ( IOException e
) {
125 if ( snapshot
!= null
) {
130 if ( BuildConfig
.DEBUG
) {
131 Log
.d( "cache_test_DISK_", bitmap
== null ?
"not found" : "image read from disk " + key
);
138 public boolean containsKey( String key
) {
140 boolean contained
= false
;
141 DiskLruCache
.Snapshot snapshot
= null
;
143 snapshot
= mDiskCache
.get( key
);
144 contained
= snapshot
!= null
;
145 } catch (IOException e
) {
148 if ( snapshot
!= null
) {
157 public void clearCache() {
158 if ( BuildConfig
.DEBUG
) {
159 Log
.d( "cache_test_DISK_", "disk cache CLEARED");
163 } catch ( IOException e
) {
168 public File
getCacheFolder() {
169 return mDiskCache
.getDirectory();