DiskLruImageCache.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package com.owncloud.android.ui.adapter;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;
  11. import android.content.Context;
  12. import android.graphics.Bitmap;
  13. import android.graphics.Bitmap.CompressFormat;
  14. import android.graphics.BitmapFactory;
  15. import android.util.Log;
  16. import com.jakewharton.disklrucache.DiskLruCache;
  17. import com.owncloud.android.BuildConfig;
  18. import com.owncloud.android.utils.Log_OC;
  19. public class DiskLruImageCache {
  20. private DiskLruCache mDiskCache;
  21. private CompressFormat mCompressFormat;
  22. private int mCompressQuality;
  23. private static final int CACHE_VERSION = 1;
  24. private static final int VALUE_COUNT = 1;
  25. private static final int IO_BUFFER_SIZE = 8 * 1024;
  26. private static final Pattern CAPITAL_LETTERS = Pattern.compile("[A-Z]");
  27. private StringBuffer mValidKeyBuffer = new StringBuffer(64);
  28. private StringBuffer mConversionBuffer = new StringBuffer(2).append('_');
  29. private static final String TAG = "DiskLruImageCache";
  30. public DiskLruImageCache( Context context,String uniqueName, int diskCacheSize,
  31. CompressFormat compressFormat, int quality ) throws IOException {
  32. final File diskCacheDir = getDiskCacheDir(context, uniqueName );
  33. mDiskCache = DiskLruCache.open(
  34. diskCacheDir, CACHE_VERSION, VALUE_COUNT, diskCacheSize
  35. );
  36. mCompressFormat = compressFormat;
  37. mCompressQuality = quality;
  38. }
  39. private boolean writeBitmapToFile( Bitmap bitmap, DiskLruCache.Editor editor )
  40. throws IOException, FileNotFoundException {
  41. OutputStream out = null;
  42. try {
  43. out = new BufferedOutputStream( editor.newOutputStream( 0 ), IO_BUFFER_SIZE );
  44. return bitmap.compress( mCompressFormat, mCompressQuality, out );
  45. } finally {
  46. if ( out != null ) {
  47. out.close();
  48. }
  49. }
  50. }
  51. private File getDiskCacheDir(Context context, String uniqueName) {
  52. // Check if media is mounted or storage is built-in, if so, try and use external cache dir
  53. // otherwise use internal cache dir
  54. final String cachePath = context.getExternalCacheDir().getPath();
  55. Log_OC.d(TAG, "create dir: " + cachePath + File.separator + uniqueName);
  56. return new File(cachePath + File.separator + uniqueName);
  57. }
  58. public void put( String key, Bitmap data ) {
  59. DiskLruCache.Editor editor = null;
  60. String validKey = convertToValidKey(key);
  61. try {
  62. editor = mDiskCache.edit( validKey );
  63. if ( editor == null ) {
  64. return;
  65. }
  66. if( writeBitmapToFile( data, editor ) ) {
  67. mDiskCache.flush();
  68. editor.commit();
  69. if ( BuildConfig.DEBUG ) {
  70. Log.d( "cache_test_DISK_", "image put on disk cache " + validKey );
  71. }
  72. } else {
  73. editor.abort();
  74. if ( BuildConfig.DEBUG ) {
  75. Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey );
  76. }
  77. }
  78. } catch (IOException e) {
  79. if ( BuildConfig.DEBUG ) {
  80. Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + validKey );
  81. }
  82. try {
  83. if ( editor != null ) {
  84. editor.abort();
  85. }
  86. } catch (IOException ignored) {
  87. }
  88. }
  89. }
  90. public Bitmap getBitmap( String key ) {
  91. Bitmap bitmap = null;
  92. DiskLruCache.Snapshot snapshot = null;
  93. String validKey = convertToValidKey(key);
  94. try {
  95. snapshot = mDiskCache.get( validKey );
  96. if ( snapshot == null ) {
  97. return null;
  98. }
  99. final InputStream in = snapshot.getInputStream( 0 );
  100. if ( in != null ) {
  101. final BufferedInputStream buffIn =
  102. new BufferedInputStream( in, IO_BUFFER_SIZE );
  103. bitmap = BitmapFactory.decodeStream( buffIn );
  104. }
  105. } catch ( IOException e ) {
  106. e.printStackTrace();
  107. } finally {
  108. if ( snapshot != null ) {
  109. snapshot.close();
  110. }
  111. }
  112. if ( BuildConfig.DEBUG ) {
  113. Log.d("cache_test_DISK_", bitmap == null ? "not found" : "image read from disk " + validKey);
  114. }
  115. return bitmap;
  116. }
  117. public boolean containsKey( String key ) {
  118. boolean contained = false;
  119. DiskLruCache.Snapshot snapshot = null;
  120. String validKey = convertToValidKey(key);
  121. try {
  122. snapshot = mDiskCache.get( validKey );
  123. contained = snapshot != null;
  124. } catch (IOException e) {
  125. e.printStackTrace();
  126. } finally {
  127. if ( snapshot != null ) {
  128. snapshot.close();
  129. }
  130. }
  131. return contained;
  132. }
  133. public void clearCache() {
  134. if ( BuildConfig.DEBUG ) {
  135. Log.d( "cache_test_DISK_", "disk cache CLEARED");
  136. }
  137. try {
  138. mDiskCache.delete();
  139. } catch ( IOException e ) {
  140. e.printStackTrace();
  141. }
  142. }
  143. public File getCacheFolder() {
  144. return mDiskCache.getDirectory();
  145. }
  146. private String convertToValidKey(String key) {
  147. Matcher capitalLettersMatcher = CAPITAL_LETTERS.matcher(key);
  148. mValidKeyBuffer.delete(0, mValidKeyBuffer.length());
  149. mConversionBuffer.delete(1, mConversionBuffer.length());
  150. while (capitalLettersMatcher.find()) {
  151. mConversionBuffer.replace(1, 2, capitalLettersMatcher.group(0).toLowerCase());
  152. capitalLettersMatcher.appendReplacement(mValidKeyBuffer, mConversionBuffer.toString());
  153. }
  154. capitalLettersMatcher.appendTail(mValidKeyBuffer);
  155. return mValidKeyBuffer.toString();
  156. }
  157. }