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
;
32 import android
.util
.Log
;
34 import com
.jakewharton
.disklrucache
.DiskLruCache
;
35 import com
.owncloud
.android
.BuildConfig
;
36 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
38 public class DiskLruImageCache
{
40 private DiskLruCache mDiskCache
;
41 private CompressFormat mCompressFormat
;
42 private int mCompressQuality
;
43 private static final int CACHE_VERSION
= 1;
44 private static final int VALUE_COUNT
= 1;
45 private static final int IO_BUFFER_SIZE
= 8 * 1024;
47 private static final String TAG
= DiskLruImageCache
.class.getSimpleName();
49 public DiskLruImageCache( Context context
,String uniqueName
, int diskCacheSize
,
50 CompressFormat compressFormat
, int quality
) throws IOException
{
51 final File diskCacheDir
= getDiskCacheDir(context
, uniqueName
);
52 mDiskCache
= DiskLruCache
.open(
53 diskCacheDir
, CACHE_VERSION
, VALUE_COUNT
, diskCacheSize
55 mCompressFormat
= compressFormat
;
56 mCompressQuality
= quality
;
59 private boolean writeBitmapToFile( Bitmap bitmap
, DiskLruCache
.Editor editor
)
60 throws IOException
, FileNotFoundException
{
61 OutputStream out
= null
;
63 out
= new BufferedOutputStream( editor
.newOutputStream( 0 ), IO_BUFFER_SIZE
);
64 return bitmap
.compress( mCompressFormat
, mCompressQuality
, out
);
72 private File
getDiskCacheDir(Context context
, String uniqueName
) {
74 // Check if media is mounted or storage is built-in, if so, try and use external cache dir
75 // otherwise use internal cache dir
76 final String cachePath
= context
.getExternalCacheDir().getPath();
78 Log_OC
.d(TAG
, "create dir: " + cachePath
+ File
.separator
+ uniqueName
);
80 return new File(cachePath
+ File
.separator
+ uniqueName
);
83 public void put( String key
, Bitmap data
) {
85 DiskLruCache
.Editor editor
= null
;
86 String validKey
= convertToValidKey(key
);
88 editor
= mDiskCache
.edit( validKey
);
89 if ( editor
== null
) {
93 if( writeBitmapToFile( data
, editor
) ) {
96 if ( BuildConfig
.DEBUG
) {
97 Log_OC
.d( "cache_test_DISK_", "image put on disk cache " + validKey
);
101 if ( BuildConfig
.DEBUG
) {
102 Log_OC
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey
);
105 } catch (IOException e
) {
106 if ( BuildConfig
.DEBUG
) {
107 Log_OC
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey
);
110 if ( editor
!= null
) {
113 } catch (IOException ignored
) {
119 public Bitmap
getBitmap( String key
) {
121 Bitmap bitmap
= null
;
122 DiskLruCache
.Snapshot snapshot
= null
;
123 String validKey
= convertToValidKey(key
);
126 snapshot
= mDiskCache
.get( validKey
);
127 if ( snapshot
== null
) {
130 final InputStream
in = snapshot
.getInputStream( 0 );
132 final BufferedInputStream buffIn
=
133 new BufferedInputStream( in, IO_BUFFER_SIZE
);
134 bitmap
= BitmapFactory
.decodeStream( buffIn
);
136 } catch ( IOException e
) {
139 if ( snapshot
!= null
) {
144 if ( BuildConfig
.DEBUG
) {
145 Log_OC
.d("cache_test_DISK_", bitmap
== null ?
146 "not found" : "image read from disk " + validKey
);
153 public boolean containsKey( String key
) {
155 boolean contained
= false
;
156 DiskLruCache
.Snapshot snapshot
= null
;
157 String validKey
= convertToValidKey(key
);
159 snapshot
= mDiskCache
.get( validKey
);
160 contained
= snapshot
!= null
;
161 } catch (IOException e
) {
164 if ( snapshot
!= null
) {
173 public void clearCache() {
174 if ( BuildConfig
.DEBUG
) {
175 Log_OC
.d( "cache_test_DISK_", "disk cache CLEARED");
179 } catch ( IOException e
) {
184 public File
getCacheFolder() {
185 return mDiskCache
.getDirectory();
188 private String
convertToValidKey(String key
) {
189 return Integer
.toString(key
.hashCode());
193 * Remove passed key from cache
196 public void removeKey( String key
) {
197 String validKey
= convertToValidKey(key
);
199 mDiskCache
.remove(validKey
);
200 Log_OC
.d(TAG
, "removeKey from cache: " + validKey
);
201 } catch (IOException e
) {