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
.graphics
.Bitmap
;
29 import android
.graphics
.Bitmap
.CompressFormat
;
30 import android
.graphics
.BitmapFactory
;
32 import com
.jakewharton
.disklrucache
.DiskLruCache
;
33 import com
.owncloud
.android
.BuildConfig
;
34 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
36 public class DiskLruImageCache
{
38 private DiskLruCache mDiskCache
;
39 private CompressFormat mCompressFormat
;
40 private int mCompressQuality
;
41 private static final int CACHE_VERSION
= 1;
42 private static final int VALUE_COUNT
= 1;
43 private static final int IO_BUFFER_SIZE
= 8 * 1024;
45 private static final String TAG
= DiskLruImageCache
.class.getSimpleName();
47 //public DiskLruImageCache( Context context,String uniqueName, int diskCacheSize,
48 public DiskLruImageCache(
49 File diskCacheDir
, int diskCacheSize
, CompressFormat compressFormat
, int quality
50 ) throws IOException
{
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 public void put( String key
, Bitmap data
) {
74 DiskLruCache
.Editor editor
= null
;
75 String validKey
= convertToValidKey(key
);
77 editor
= mDiskCache
.edit( validKey
);
78 if ( editor
== null
) {
82 if( writeBitmapToFile( data
, editor
) ) {
85 if ( BuildConfig
.DEBUG
) {
86 Log_OC
.d( "cache_test_DISK_", "image put on disk cache " + validKey
);
90 if ( BuildConfig
.DEBUG
) {
91 Log_OC
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey
);
94 } catch (IOException e
) {
95 if ( BuildConfig
.DEBUG
) {
96 Log_OC
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey
);
99 if ( editor
!= null
) {
102 } catch (IOException ignored
) {
108 public Bitmap
getBitmap( String key
) {
110 Bitmap bitmap
= null
;
111 DiskLruCache
.Snapshot snapshot
= null
;
112 String validKey
= convertToValidKey(key
);
115 snapshot
= mDiskCache
.get( validKey
);
116 if ( snapshot
== null
) {
119 final InputStream
in = snapshot
.getInputStream( 0 );
121 final BufferedInputStream buffIn
=
122 new BufferedInputStream( in, IO_BUFFER_SIZE
);
123 bitmap
= BitmapFactory
.decodeStream( buffIn
);
125 } catch ( IOException e
) {
128 if ( snapshot
!= null
) {
133 if ( BuildConfig
.DEBUG
) {
134 Log_OC
.d("cache_test_DISK_", bitmap
== null ?
135 "not found" : "image read from disk " + validKey
);
142 public boolean containsKey( String key
) {
144 boolean contained
= false
;
145 DiskLruCache
.Snapshot snapshot
= null
;
146 String validKey
= convertToValidKey(key
);
148 snapshot
= mDiskCache
.get( validKey
);
149 contained
= snapshot
!= null
;
150 } catch (IOException e
) {
153 if ( snapshot
!= null
) {
162 public void clearCache() {
163 if ( BuildConfig
.DEBUG
) {
164 Log_OC
.d( "cache_test_DISK_", "disk cache CLEARED");
168 } catch ( IOException e
) {
173 public File
getCacheFolder() {
174 return mDiskCache
.getDirectory();
177 private String
convertToValidKey(String key
) {
178 return Integer
.toString(key
.hashCode());
182 * Remove passed key from cache
185 public void removeKey( String key
) {
186 String validKey
= convertToValidKey(key
);
188 mDiskCache
.remove(validKey
);
189 Log_OC
.d(TAG
, "removeKey from cache: " + validKey
);
190 } catch (IOException e
) {