1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.adapter
;
20 import java
.io
.BufferedInputStream
;
21 import java
.io
.BufferedOutputStream
;
23 import java
.io
.FileNotFoundException
;
24 import java
.io
.IOException
;
25 import java
.io
.InputStream
;
26 import java
.io
.OutputStream
;
28 import android
.content
.Context
;
29 import android
.graphics
.Bitmap
;
30 import android
.graphics
.Bitmap
.CompressFormat
;
31 import android
.graphics
.BitmapFactory
;
33 import com
.jakewharton
.disklrucache
.DiskLruCache
;
34 import com
.owncloud
.android
.BuildConfig
;
35 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
37 public class DiskLruImageCache
{
39 private DiskLruCache mDiskCache
;
40 private CompressFormat mCompressFormat
;
41 private int mCompressQuality
;
42 private static final int CACHE_VERSION
= 1;
43 private static final int VALUE_COUNT
= 1;
44 private static final int IO_BUFFER_SIZE
= 8 * 1024;
46 private static final String TAG
= DiskLruImageCache
.class.getSimpleName();
48 public DiskLruImageCache( Context context
,String uniqueName
, int diskCacheSize
,
49 CompressFormat compressFormat
, int quality
) throws IOException
{
50 final File diskCacheDir
= getDiskCacheDir(context
, uniqueName
);
51 mDiskCache
= DiskLruCache
.open(
52 diskCacheDir
, CACHE_VERSION
, VALUE_COUNT
, diskCacheSize
54 mCompressFormat
= compressFormat
;
55 mCompressQuality
= quality
;
58 private boolean writeBitmapToFile( Bitmap bitmap
, DiskLruCache
.Editor editor
)
59 throws IOException
, FileNotFoundException
{
60 OutputStream out
= null
;
62 out
= new BufferedOutputStream( editor
.newOutputStream( 0 ), IO_BUFFER_SIZE
);
63 return bitmap
.compress( mCompressFormat
, mCompressQuality
, out
);
71 private File
getDiskCacheDir(Context context
, String uniqueName
) {
73 // Check if media is mounted or storage is built-in, if so, try and use external cache dir
74 // otherwise use internal cache dir
75 final String cachePath
= context
.getExternalCacheDir().getPath();
77 Log_OC
.d(TAG
, "create dir: " + cachePath
+ File
.separator
+ uniqueName
);
79 return new File(cachePath
+ File
.separator
+ uniqueName
);
82 public void put( String key
, Bitmap data
) {
84 DiskLruCache
.Editor editor
= null
;
85 String validKey
= convertToValidKey(key
);
87 editor
= mDiskCache
.edit( validKey
);
88 if ( editor
== null
) {
92 if( writeBitmapToFile( data
, editor
) ) {
95 if ( BuildConfig
.DEBUG
) {
96 Log_OC
.d( "cache_test_DISK_", "image put on disk cache " + validKey
);
100 if ( BuildConfig
.DEBUG
) {
101 Log_OC
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey
);
104 } catch (IOException e
) {
105 if ( BuildConfig
.DEBUG
) {
106 Log_OC
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey
);
109 if ( editor
!= null
) {
112 } catch (IOException ignored
) {
118 public Bitmap
getBitmap( String key
) {
120 Bitmap bitmap
= null
;
121 DiskLruCache
.Snapshot snapshot
= null
;
122 String validKey
= convertToValidKey(key
);
125 snapshot
= mDiskCache
.get( validKey
);
126 if ( snapshot
== null
) {
129 final InputStream
in = snapshot
.getInputStream( 0 );
131 final BufferedInputStream buffIn
=
132 new BufferedInputStream( in, IO_BUFFER_SIZE
);
133 bitmap
= BitmapFactory
.decodeStream( buffIn
);
135 } catch ( IOException e
) {
138 if ( snapshot
!= null
) {
143 if ( BuildConfig
.DEBUG
) {
144 Log_OC
.d("cache_test_DISK_", bitmap
== null ?
145 "not found" : "image read from disk " + validKey
);
152 public boolean containsKey( String key
) {
154 boolean contained
= false
;
155 DiskLruCache
.Snapshot snapshot
= null
;
156 String validKey
= convertToValidKey(key
);
158 snapshot
= mDiskCache
.get( validKey
);
159 contained
= snapshot
!= null
;
160 } catch (IOException e
) {
163 if ( snapshot
!= null
) {
172 public void clearCache() {
173 if ( BuildConfig
.DEBUG
) {
174 Log_OC
.d( "cache_test_DISK_", "disk cache CLEARED");
178 } catch ( IOException e
) {
183 public File
getCacheFolder() {
184 return mDiskCache
.getDirectory();
187 private String
convertToValidKey(String key
) {
188 return Integer
.toString(key
.hashCode());
192 * Remove passed key from cache
195 public void removeKey( String key
) {
196 String validKey
= convertToValidKey(key
);
198 mDiskCache
.remove(validKey
);
199 Log_OC
.d(TAG
, "removeKey from cache: " + validKey
);
200 } catch (IOException e
) {