|
@@ -20,33 +20,78 @@ import java.io.OutputStream;
|
|
|
|
|
|
import eu.kanade.tachiyomi.util.DiskUtils;
|
|
|
|
|
|
+
|
|
|
+ * Class used to create cover cache
|
|
|
+ * Makes use of Glide(which can avoid repeating requests) for saving the file.
|
|
|
+ * It is not necessary to load the images to the cache.
|
|
|
+ * Names of files are created with the md5 of the thumbnailURL
|
|
|
+ */
|
|
|
public class CoverCache {
|
|
|
|
|
|
+
|
|
|
+ * Name of cache directory.
|
|
|
+ */
|
|
|
private static final String PARAMETER_CACHE_DIRECTORY = "cover_disk_cache";
|
|
|
|
|
|
- private Context context;
|
|
|
- private File cacheDir;
|
|
|
-
|
|
|
+
|
|
|
+ * Interface to global information about an application environment.
|
|
|
+ */
|
|
|
+ private final Context context;
|
|
|
+
|
|
|
+
|
|
|
+ * Cache class used for cache management.
|
|
|
+ */
|
|
|
+ private final File cacheDir;
|
|
|
+
|
|
|
+
|
|
|
+ * Constructor of CoverCache.
|
|
|
+ *
|
|
|
+ * @param context application environment interface.
|
|
|
+ */
|
|
|
public CoverCache(Context context) {
|
|
|
this.context = context;
|
|
|
+
|
|
|
+
|
|
|
cacheDir = new File(context.getCacheDir(), PARAMETER_CACHE_DIRECTORY);
|
|
|
+
|
|
|
+
|
|
|
createCacheDir();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Check if cache dir exist if not create directory.
|
|
|
+ *
|
|
|
+ * @return true if cache dir does exist and is created.
|
|
|
+ */
|
|
|
private boolean createCacheDir() {
|
|
|
return !cacheDir.exists() && cacheDir.mkdirs();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Download the cover with Glide (it can avoid repeating requests) and save the file.
|
|
|
+ *
|
|
|
+ * @param thumbnailUrl url of thumbnail.
|
|
|
+ * @param headers headers included in Glide request.
|
|
|
+ */
|
|
|
public void save(String thumbnailUrl, LazyHeaders headers) {
|
|
|
save(thumbnailUrl, headers, null);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- public void save(String thumbnailUrl, LazyHeaders headers, ImageView imageView) {
|
|
|
+
|
|
|
+ * Download the cover with Glide (it can avoid repeating requests) and save the file.
|
|
|
+ *
|
|
|
+ * @param thumbnailUrl url of thumbnail.
|
|
|
+ * @param headers headers included in Glide request.
|
|
|
+ * @param imageView imageView where picture should be displayed.
|
|
|
+ */
|
|
|
+ private void save(String thumbnailUrl, LazyHeaders headers, ImageView imageView) {
|
|
|
+
|
|
|
+
|
|
|
if (TextUtils.isEmpty(thumbnailUrl))
|
|
|
+
|
|
|
return;
|
|
|
|
|
|
+
|
|
|
GlideUrl url = new GlideUrl(thumbnailUrl, headers);
|
|
|
Glide.with(context)
|
|
|
.load(url)
|
|
@@ -54,7 +99,10 @@ public class CoverCache {
|
|
|
@Override
|
|
|
public void onResourceReady(File resource, GlideAnimation<? super File> anim) {
|
|
|
try {
|
|
|
- add(thumbnailUrl, resource);
|
|
|
+
|
|
|
+ copyToLocalCache(thumbnailUrl, resource);
|
|
|
+
|
|
|
+
|
|
|
if (imageView != null) {
|
|
|
loadFromCache(imageView, resource);
|
|
|
}
|
|
@@ -65,18 +113,32 @@ public class CoverCache {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public void add(String thumbnailUrl, File source) throws IOException {
|
|
|
+
|
|
|
+
|
|
|
+ * Copy the cover from Glide's cache to local cache.
|
|
|
+ *
|
|
|
+ * @param thumbnailUrl url of thumbnail.
|
|
|
+ * @param source the cover image.
|
|
|
+ * @throws IOException TODO not returned atm?
|
|
|
+ */
|
|
|
+ private void copyToLocalCache(String thumbnailUrl, File source) throws IOException {
|
|
|
+
|
|
|
createCacheDir();
|
|
|
+
|
|
|
+
|
|
|
File dest = new File(cacheDir, DiskUtils.hashKeyForDisk(thumbnailUrl));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
if (dest.exists())
|
|
|
dest.delete();
|
|
|
|
|
|
+
|
|
|
InputStream in = new FileInputStream(source);
|
|
|
try {
|
|
|
OutputStream out = new FileOutputStream(dest);
|
|
|
try {
|
|
|
-
|
|
|
+
|
|
|
byte[] buf = new byte[1024];
|
|
|
int len;
|
|
|
while ((len = in.read(buf)) > 0) {
|
|
@@ -90,23 +152,43 @@ public class CoverCache {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public File get(String thumbnailUrl) {
|
|
|
+
|
|
|
+
|
|
|
+ * Returns the cover from cache.
|
|
|
+ *
|
|
|
+ * @param thumbnailUrl the thumbnail url.
|
|
|
+ * @return cover image.
|
|
|
+ */
|
|
|
+ private File getCoverFromCache(String thumbnailUrl) {
|
|
|
return new File(cacheDir, DiskUtils.hashKeyForDisk(thumbnailUrl));
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public boolean delete(String thumbnailUrl) {
|
|
|
+
|
|
|
+ * Delete the cover file from the cache.
|
|
|
+ *
|
|
|
+ * @param thumbnailUrl the thumbnail url.
|
|
|
+ * @return status of deletion.
|
|
|
+ */
|
|
|
+ public boolean deleteCoverFromCache(String thumbnailUrl) {
|
|
|
+
|
|
|
if (TextUtils.isEmpty(thumbnailUrl))
|
|
|
return false;
|
|
|
|
|
|
+
|
|
|
File file = new File(cacheDir, DiskUtils.hashKeyForDisk(thumbnailUrl));
|
|
|
return file.exists() && file.delete();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public void saveAndLoadFromCache(ImageView imageView, String thumbnailUrl, LazyHeaders headers) {
|
|
|
- File localCover = get(thumbnailUrl);
|
|
|
+
|
|
|
+ * Save or load the image from cache
|
|
|
+ *
|
|
|
+ * @param imageView imageView where picture should be displayed.
|
|
|
+ * @param thumbnailUrl the thumbnail url.
|
|
|
+ * @param headers headers included in Glide request.
|
|
|
+ */
|
|
|
+ public void saveOrLoadFromCache(ImageView imageView, String thumbnailUrl, LazyHeaders headers) {
|
|
|
+
|
|
|
+ File localCover = getCoverFromCache(thumbnailUrl);
|
|
|
if (localCover.exists()) {
|
|
|
loadFromCache(imageView, localCover);
|
|
|
} else {
|
|
@@ -114,9 +196,17 @@ public class CoverCache {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+ * If the image is already in our cache, use it. If not, load it with glide.
|
|
|
+ * TODO not used atm.
|
|
|
+ *
|
|
|
+ * @param imageView imageView where picture should be displayed.
|
|
|
+ * @param thumbnailUrl url of thumbnail.
|
|
|
+ * @param headers headers included in Glide request.
|
|
|
+ */
|
|
|
public void loadFromCacheOrNetwork(ImageView imageView, String thumbnailUrl, LazyHeaders headers) {
|
|
|
- File localCover = get(thumbnailUrl);
|
|
|
+
|
|
|
+ File localCover = getCoverFromCache(thumbnailUrl);
|
|
|
if (localCover.exists()) {
|
|
|
loadFromCache(imageView, localCover);
|
|
|
} else {
|
|
@@ -124,8 +214,12 @@ public class CoverCache {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+ * Helper method to load the cover from the cache directory into the specified image view.
|
|
|
+ *
|
|
|
+ * @param imageView imageView where picture should be displayed.
|
|
|
+ * @param file file to load. Must exist!.
|
|
|
+ */
|
|
|
private void loadFromCache(ImageView imageView, File file) {
|
|
|
Glide.with(context)
|
|
|
.load(file)
|
|
@@ -134,9 +228,19 @@ public class CoverCache {
|
|
|
.into(imageView);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+ * Helper method to load the cover from network into the specified image view.
|
|
|
+ * It does NOT save the image in cache!
|
|
|
+ *
|
|
|
+ * @param imageView imageView where picture should be displayed.
|
|
|
+ * @param thumbnailUrl url of thumbnail.
|
|
|
+ * @param headers headers included in Glide request.
|
|
|
+ */
|
|
|
public void loadFromNetwork(ImageView imageView, String thumbnailUrl, LazyHeaders headers) {
|
|
|
+
|
|
|
+ if (TextUtils.isEmpty(thumbnailUrl))
|
|
|
+ return;
|
|
|
+
|
|
|
GlideUrl url = new GlideUrl(thumbnailUrl, headers);
|
|
|
Glide.with(context)
|
|
|
.load(url)
|