2  *   ownCloud Android client application 
   4  *   Copyright (C) 2015 ownCloud Inc. 
   6  *   This program is free software: you can redistribute it and/or modify 
   7  *   it under the terms of the GNU General Public License version 2, 
   8  *   as published by the Free Software Foundation. 
  10  *   This program is distributed in the hope that it will be useful, 
  11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  13  *   GNU General Public License for more details. 
  15  *   You should have received a copy of the GNU General Public License 
  16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  20 package com
.owncloud
.android
.ui
.adapter
; 
  22 import java
.io
.BufferedInputStream
; 
  23 import java
.io
.BufferedOutputStream
; 
  25 import java
.io
.FileNotFoundException
; 
  26 import java
.io
.IOException
; 
  27 import java
.io
.InputStream
; 
  28 import java
.io
.OutputStream
; 
  30 import android
.graphics
.Bitmap
; 
  31 import android
.graphics
.Bitmap
.CompressFormat
; 
  32 import android
.graphics
.BitmapFactory
; 
  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     public DiskLruImageCache( 
  51             File diskCacheDir
, int diskCacheSize
, CompressFormat compressFormat
, int quality 
 
  52             ) throws IOException 
{ 
  54         mDiskCache 
= DiskLruCache
.open( 
  55                 diskCacheDir
, CACHE_VERSION
, VALUE_COUNT
, diskCacheSize 
 
  57         mCompressFormat 
= compressFormat
; 
  58         mCompressQuality 
= quality
; 
  61     private boolean writeBitmapToFile( Bitmap bitmap
, DiskLruCache
.Editor editor 
) 
  62         throws IOException
, FileNotFoundException 
{ 
  63         OutputStream out 
= null
; 
  65             out 
= new BufferedOutputStream( editor
.newOutputStream( 0 ), IO_BUFFER_SIZE 
); 
  66             return bitmap
.compress( mCompressFormat
, mCompressQuality
, out 
); 
  74     public void put( String key
, Bitmap data 
) { 
  76         DiskLruCache
.Editor editor 
= null
; 
  77         String validKey 
= convertToValidKey(key
); 
  79             editor 
= mDiskCache
.edit( validKey 
); 
  80             if ( editor 
== null 
) { 
  84             if( writeBitmapToFile( data
, editor 
) ) {                
  87                 if ( BuildConfig
.DEBUG 
) { 
  88                    Log_OC
.d( "cache_test_DISK_", "image put on disk cache " + validKey 
); 
  92                 if ( BuildConfig
.DEBUG 
) { 
  93                     Log_OC
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey 
); 
  96         } catch (IOException e
) { 
  97             if ( BuildConfig
.DEBUG 
) { 
  98                 Log_OC
.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey 
); 
 101                 if ( editor 
!= null 
) { 
 104             } catch (IOException ignored
) { 
 110     public Bitmap 
getBitmap( String key 
) { 
 112         Bitmap bitmap 
= null
; 
 113         DiskLruCache
.Snapshot snapshot 
= null
; 
 114         String validKey 
= convertToValidKey(key
); 
 117             snapshot 
= mDiskCache
.get( validKey 
); 
 118             if ( snapshot 
== null 
) { 
 121             final InputStream 
in = snapshot
.getInputStream( 0 ); 
 123                 final BufferedInputStream buffIn 
=  
 124                 new BufferedInputStream( in, IO_BUFFER_SIZE 
); 
 125                 bitmap 
= BitmapFactory
.decodeStream( buffIn 
);               
 127         } catch ( IOException e 
) { 
 130             if ( snapshot 
!= null 
) { 
 135         if ( BuildConfig
.DEBUG 
) { 
 136             Log_OC
.d("cache_test_DISK_", bitmap 
== null ? 
 
 137                     "not found" : "image read from disk " + validKey
); 
 144     public boolean containsKey( String key 
) { 
 146         boolean contained 
= false
; 
 147         DiskLruCache
.Snapshot snapshot 
= null
; 
 148         String validKey 
= convertToValidKey(key
); 
 150             snapshot 
= mDiskCache
.get( validKey 
); 
 151             contained 
= snapshot 
!= null
; 
 152         } catch (IOException e
) { 
 155             if ( snapshot 
!= null 
) { 
 164     public void clearCache() { 
 165         if ( BuildConfig
.DEBUG 
) { 
 166             Log_OC
.d( "cache_test_DISK_", "disk cache CLEARED"); 
 170         } catch ( IOException e 
) { 
 175     public File 
getCacheFolder() { 
 176         return mDiskCache
.getDirectory(); 
 179     private String 
convertToValidKey(String key
) { 
 180         return Integer
.toString(key
.hashCode()); 
 184      * Remove passed key from cache 
 187     public void removeKey( String key 
) { 
 188         String validKey 
= convertToValidKey(key
); 
 190             mDiskCache
.remove(validKey
); 
 191             Log_OC
.d(TAG
, "removeKey from cache: " + validKey
); 
 192         } catch (IOException e
) {