CoverCache.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package eu.kanade.mangafeed.data.cache;
  2. import android.content.Context;
  3. import android.widget.ImageView;
  4. import com.bumptech.glide.Glide;
  5. import com.bumptech.glide.load.engine.DiskCacheStrategy;
  6. import com.bumptech.glide.load.model.GlideUrl;
  7. import com.bumptech.glide.load.model.LazyHeaders;
  8. import com.bumptech.glide.request.animation.GlideAnimation;
  9. import com.bumptech.glide.request.target.SimpleTarget;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.io.OutputStream;
  16. import eu.kanade.mangafeed.util.DiskUtils;
  17. public class CoverCache {
  18. private static final String PARAMETER_CACHE_DIRECTORY = "cover_disk_cache";
  19. private Context context;
  20. private File cacheDir;
  21. public CoverCache(Context context) {
  22. this.context = context;
  23. cacheDir = new File(context.getCacheDir(), PARAMETER_CACHE_DIRECTORY);
  24. createCacheDir();
  25. }
  26. private boolean createCacheDir() {
  27. return !cacheDir.exists() && cacheDir.mkdirs();
  28. }
  29. public void save(String thumbnailUrl, LazyHeaders headers) {
  30. save(thumbnailUrl, headers, null);
  31. }
  32. // Download the cover with Glide (it can avoid repeating requests) and save the file on this cache
  33. // Optionally, load the image in the given image view when the resource is ready, if not null
  34. public void save(String thumbnailUrl, LazyHeaders headers, ImageView imageView) {
  35. GlideUrl url = new GlideUrl(thumbnailUrl, headers);
  36. Glide.with(context)
  37. .load(url)
  38. .downloadOnly(new SimpleTarget<File>() {
  39. @Override
  40. public void onResourceReady(File resource, GlideAnimation<? super File> anim) {
  41. try {
  42. add(thumbnailUrl, resource);
  43. if (imageView != null) {
  44. loadFromCache(imageView, resource);
  45. }
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. });
  51. }
  52. // Copy the cover from Glide's cache to this cache
  53. public void add(String thumbnailUrl, File source) throws IOException {
  54. createCacheDir();
  55. File dest = new File(cacheDir, DiskUtils.hashKeyForDisk(thumbnailUrl));
  56. if (dest.exists())
  57. dest.delete();
  58. InputStream in = new FileInputStream(source);
  59. try {
  60. OutputStream out = new FileOutputStream(dest);
  61. try {
  62. // Transfer bytes from in to out
  63. byte[] buf = new byte[1024];
  64. int len;
  65. while ((len = in.read(buf)) > 0) {
  66. out.write(buf, 0, len);
  67. }
  68. } finally {
  69. out.close();
  70. }
  71. } finally {
  72. in.close();
  73. }
  74. }
  75. // Get the cover from cache
  76. public File get(String thumbnailUrl) {
  77. return new File(cacheDir, DiskUtils.hashKeyForDisk(thumbnailUrl));
  78. }
  79. // Delete the cover from cache
  80. public boolean delete(String thumbnailUrl) {
  81. File file = new File(cacheDir, DiskUtils.hashKeyForDisk(thumbnailUrl));
  82. return file.exists() && file.delete();
  83. }
  84. // Save and load the image from cache
  85. public void saveAndLoadFromCache(ImageView imageView, String thumbnailUrl, LazyHeaders headers) {
  86. File localCover = get(thumbnailUrl);
  87. if (localCover.exists()) {
  88. loadFromCache(imageView, localCover);
  89. } else {
  90. save(thumbnailUrl, headers, imageView);
  91. }
  92. }
  93. // Helper method to load the cover from the cache directory into the specified image view
  94. // The file must exist
  95. private void loadFromCache(ImageView imageView, File file) {
  96. Glide.with(context)
  97. .load(file)
  98. .diskCacheStrategy(DiskCacheStrategy.NONE)
  99. .centerCrop()
  100. .into(imageView);
  101. }
  102. // Helper method to load the cover from network into the specified image view.
  103. // It does NOT save the image in cache
  104. public void loadFromNetwork(ImageView imageView, String thumbnailUrl, LazyHeaders headers) {
  105. GlideUrl url = new GlideUrl(thumbnailUrl, headers);
  106. Glide.with(context)
  107. .load(url)
  108. .diskCacheStrategy(DiskCacheStrategy.SOURCE)
  109. .centerCrop()
  110. .into(imageView);
  111. }
  112. }