-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ExternalCache that falls back to InternalCache if not available.
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...ain/java/com/bumptech/glide/load/engine/cache/ExternalPreferredCacheDiskCacheFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.bumptech.glide.load.engine.cache; | ||
|
||
import android.content.Context; | ||
|
||
import android.support.annotation.Nullable; | ||
import java.io.File; | ||
|
||
/** | ||
* Creates an {@link com.bumptech.glide.disklrucache.DiskLruCache} based disk cache in the external | ||
* disk cache directory, which falls back to the internal disk cache if no external storage is | ||
* available. If ever fell back to the internal disk cache, will use that one from that moment on. | ||
* | ||
* <p><b>Images can be read by everyone when using external disk cache.</b> | ||
*/ | ||
public final class ExternalPreferredCacheDiskCacheFactory extends DiskLruCacheFactory { | ||
|
||
public ExternalPreferredCacheDiskCacheFactory(Context context) { | ||
this(context, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR, | ||
DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE); | ||
} | ||
|
||
public ExternalPreferredCacheDiskCacheFactory(Context context, int diskCacheSize) { | ||
this(context, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR, diskCacheSize); | ||
} | ||
|
||
public ExternalPreferredCacheDiskCacheFactory(final Context context, final String diskCacheName, | ||
final int diskCacheSize) { | ||
super(new CacheDirectoryGetter() { | ||
@Nullable | ||
private File getInternalCacheDirectory() { | ||
File cacheDirectory = context.getCacheDir(); | ||
if (cacheDirectory == null) { | ||
return null; | ||
} | ||
if (diskCacheName != null) { | ||
return new File(cacheDirectory, diskCacheName); | ||
} | ||
return cacheDirectory; | ||
} | ||
|
||
@Override | ||
public File getCacheDirectory() { | ||
File internalCacheDirectory = getInternalCacheDirectory(); | ||
|
||
// Already used internal cache, so keep using that one, | ||
// thus avoiding using both external and internal with transient errors. | ||
if ((null != internalCacheDirectory) && internalCacheDirectory.exists()) { | ||
return internalCacheDirectory; | ||
} | ||
|
||
File cacheDirectory = context.getExternalCacheDir(); | ||
|
||
// Shared storage is not available. | ||
if (cacheDirectory == null) { | ||
return internalCacheDirectory; | ||
} | ||
if (diskCacheName != null) { | ||
return new File(cacheDirectory, diskCacheName); | ||
} | ||
return cacheDirectory; | ||
} | ||
}, diskCacheSize); | ||
} | ||
} |