Skip to content

Commit 56a4275

Browse files
anpezsjudd
authored andcommitted
Add ExternalCache that falls back to InternalCache if not available.
1 parent d96a1e7 commit 56a4275

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

library/src/main/java/com/bumptech/glide/load/engine/cache/ExternalCacheDiskCacheFactory.java

+3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
* disk cache directory.
99
*
1010
* <p><b>Images can be read by everyone when using external disk cache.</b>
11+
*
12+
* @deprecated use {@link ExternalPreferredCacheDiskCacheFactory} instead.
1113
*/
14+
@Deprecated
1215
public final class ExternalCacheDiskCacheFactory extends DiskLruCacheFactory {
1316

1417
public ExternalCacheDiskCacheFactory(Context context) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.bumptech.glide.load.engine.cache;
2+
3+
import android.content.Context;
4+
5+
import android.support.annotation.Nullable;
6+
import java.io.File;
7+
8+
/**
9+
* Creates an {@link com.bumptech.glide.disklrucache.DiskLruCache} based disk cache in the external
10+
* disk cache directory, which falls back to the internal disk cache if no external storage is
11+
* available. If ever fell back to the internal disk cache, will use that one from that moment on.
12+
*
13+
* <p><b>Images can be read by everyone when using external disk cache.</b>
14+
*/
15+
public final class ExternalPreferredCacheDiskCacheFactory extends DiskLruCacheFactory {
16+
17+
public ExternalPreferredCacheDiskCacheFactory(Context context) {
18+
this(context, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR,
19+
DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE);
20+
}
21+
22+
public ExternalPreferredCacheDiskCacheFactory(Context context, int diskCacheSize) {
23+
this(context, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR, diskCacheSize);
24+
}
25+
26+
public ExternalPreferredCacheDiskCacheFactory(final Context context, final String diskCacheName,
27+
final int diskCacheSize) {
28+
super(new CacheDirectoryGetter() {
29+
@Nullable
30+
private File getInternalCacheDirectory() {
31+
File cacheDirectory = context.getCacheDir();
32+
if (cacheDirectory == null) {
33+
return null;
34+
}
35+
if (diskCacheName != null) {
36+
return new File(cacheDirectory, diskCacheName);
37+
}
38+
return cacheDirectory;
39+
}
40+
41+
@Override
42+
public File getCacheDirectory() {
43+
File internalCacheDirectory = getInternalCacheDirectory();
44+
45+
// Already used internal cache, so keep using that one,
46+
// thus avoiding using both external and internal with transient errors.
47+
if ((null != internalCacheDirectory) && internalCacheDirectory.exists()) {
48+
return internalCacheDirectory;
49+
}
50+
51+
File cacheDirectory = context.getExternalCacheDir();
52+
53+
// Shared storage is not available.
54+
if (cacheDirectory == null) {
55+
return internalCacheDirectory;
56+
}
57+
if (diskCacheName != null) {
58+
return new File(cacheDirectory, diskCacheName);
59+
}
60+
return cacheDirectory;
61+
}
62+
}, diskCacheSize);
63+
}
64+
}

0 commit comments

Comments
 (0)